From 7f2c3d531cd44799b9ade66131de8ca0dbfa4eb9 Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Mon, 26 Oct 2020 21:40:21 +0100 Subject: [PATCH] OSX compatibility posix for NetworkToolkit --- Connection.h | 2 ++ NetworkSource.cpp | 1 + NetworkToolkit.cpp | 85 +++++++++++++++++++++++++++++----------------- Streamer.cpp | 2 +- 4 files changed, 57 insertions(+), 33 deletions(-) diff --git a/Connection.h b/Connection.h index 2d38209..ac56d2a 100644 --- a/Connection.h +++ b/Connection.h @@ -1,6 +1,8 @@ #ifndef CONNECTION_H #define CONNECTION_H +#include + #include "osc/OscReceivedElements.h" #include "osc/OscPacketListener.h" #include "ip/UdpSocket.h" diff --git a/NetworkSource.cpp b/NetworkSource.cpp index bf3d20f..7206e0f 100644 --- a/NetworkSource.cpp +++ b/NetworkSource.cpp @@ -228,6 +228,7 @@ void NetworkStream::update() Log::Warning("Cannot connect to shared memory."); failed_ = true; } + parameter = "\"" + parameter + "\""; } // general case : create pipeline and open diff --git a/NetworkToolkit.cpp b/NetworkToolkit.cpp index 8e49b6f..c4a9b9c 100644 --- a/NetworkToolkit.cpp +++ b/NetworkToolkit.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #ifdef linux #include @@ -86,47 +87,67 @@ const std::vector NetworkToolkit::protocol_receive_pipeline { "tcpclientsrc timeout=1 port=XXXX ! queue max-size-buffers=3 ! application/x-rtp-stream,media=video,encoding-name=H264,payload=96,clock-rate=90000 ! rtpstreamdepay ! rtph264depay ! avdec_h264" }; - std::vector ipstrings; std::vector iplongs; -std::vector NetworkToolkit::host_ips() +void add_interface(int fd, const char *name) { + struct ifreq ifreq; + char host[128]; + memset(&ifreq, 0, sizeof ifreq); + strncpy(ifreq.ifr_name, name, IFNAMSIZ); + if(ioctl(fd, SIOCGIFADDR, &ifreq)==0) { + int family; + switch(family=ifreq.ifr_addr.sa_family) { + case AF_INET: + case AF_INET6: + getnameinfo(&ifreq.ifr_addr, sizeof ifreq.ifr_addr, host, sizeof host, 0, 0, NI_NUMERICHOST); + break; + default: + case AF_UNSPEC: + return; /* ignore */ + } + // add only if not already listed + if ( std::find(ipstrings.begin(), ipstrings.end(), std::string(host)) == ipstrings.end() ) + { + ipstrings.push_back( std::string(host) ); + iplongs.push_back( GetHostByName(host) ); +// printf("%-24s%s %lu\n", name, host, GetHostByName(host)); + } + } +} + +void list_interfaces() { - // fill the list of IPs only once - if (ipstrings.empty()) { - -// fprintf(stderr, "List of ips: \n" ); - - int s = socket(AF_INET, SOCK_STREAM, 0); - if (s > -1) { - struct ifconf ifconf; - struct ifreq ifr[50]; - int ifs; - int i; - - ifconf.ifc_buf = (char *) ifr; + struct ifreq *ifreq; + struct ifconf ifconf; + char buf[16384]; + int fd=socket(PF_INET, SOCK_DGRAM, 0); + if(fd > -1) { + ifconf.ifc_len=sizeof buf; + ifconf.ifc_buf=buf; + if(ioctl(fd, SIOCGIFCONF, &ifconf)==0) { + ifreq=ifconf.ifc_req; + for(int i=0;iifr_addr.sa_len; + len=IFNAMSIZ + ifreq->ifr_addr.sa_len; #else - ifconf.ifc_len = sizeof ifr; + len=sizeof *ifreq; #endif - if (ioctl(s, SIOCGIFCONF, &ifconf) > -1) { - ifs = ifconf.ifc_len / sizeof(ifr[0]); - for (i = 0; i < ifs; i++) { - char ip[INET_ADDRSTRLEN]; - struct sockaddr_in *s_in = (struct sockaddr_in *) &ifr[i].ifr_addr; - - if (inet_ntop(AF_INET, &s_in->sin_addr, ip, sizeof(ip))) { - ipstrings.push_back( std::string(ip) ); - iplongs.push_back( GetHostByName(ip) ); -// fprintf(stderr, "%s %lu", ip, GetHostByName(ip) ); - } - } - close(s); + add_interface(fd, ifreq->ifr_name); + ifreq=(struct ifreq*)((char*)ifreq+len); + i+=len; } } } + close(fd); +} + +std::vector NetworkToolkit::host_ips() +{ + if (ipstrings.empty()) + list_interfaces(); return ipstrings; } @@ -137,7 +158,7 @@ bool NetworkToolkit::is_host_ip(const std::string &ip) return true; if (ipstrings.empty()) - host_ips(); + list_interfaces(); return std::find(ipstrings.begin(), ipstrings.end(), ip) != ipstrings.end(); } @@ -147,7 +168,7 @@ std::string NetworkToolkit::closest_host_ip(const std::string &ip) std::string address = "localhost"; if (iplongs.empty()) - host_ips(); + list_interfaces(); // discard trivial case if ( ip.compare("localhost") != 0) diff --git a/Streamer.cpp b/Streamer.cpp index 643c89a..3ef14b2 100644 --- a/Streamer.cpp +++ b/Streamer.cpp @@ -231,7 +231,7 @@ void Streaming::addStream(const std::string &sender, int reply_to, const std::st else { conf.protocol = NetworkToolkit::UDP_JPEG; } - conf.protocol = NetworkToolkit::UDP_JPEG; // force udp for testing +// conf.protocol = NetworkToolkit::UDP_JPEG; // force udp for testing // build OSC message char buffer[IP_MTU_SIZE];