From a4621f31e3693f5b09c0cc2ce7017b78a175c05f Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Mon, 26 Oct 2020 19:32:19 +0100 Subject: [PATCH] OSX compilation compatibility --- NetworkToolkit.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/NetworkToolkit.cpp b/NetworkToolkit.cpp index 6f8f1a0..8e49b6f 100644 --- a/NetworkToolkit.cpp +++ b/NetworkToolkit.cpp @@ -4,10 +4,13 @@ #include #include #include +#include #include +#include -// TODO OSX implementation +#ifdef linux #include +#endif // OSC IP gethostbyname #include "ip/NetworkingUtils.h" @@ -87,11 +90,14 @@ const std::vector NetworkToolkit::protocol_receive_pipeline { std::vector ipstrings; std::vector iplongs; + std::vector NetworkToolkit::host_ips() { // 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; @@ -100,8 +106,11 @@ std::vector NetworkToolkit::host_ips() int i; ifconf.ifc_buf = (char *) ifr; +#ifndef linux + ifconf.ifc_len = IFNAMSIZ + ifr->ifr_addr.sa_len; +#else ifconf.ifc_len = sizeof ifr; - +#endif if (ioctl(s, SIOCGIFCONF, &ifconf) > -1) { ifs = ifconf.ifc_len / sizeof(ifr[0]); for (i = 0; i < ifs; i++) { @@ -111,6 +120,7 @@ std::vector NetworkToolkit::host_ips() 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); @@ -146,11 +156,11 @@ std::string NetworkToolkit::closest_host_ip(const std::string &ip) unsigned long host = GetHostByName( ip.c_str() ); unsigned long mini = host; - for (int i=0; i < iplongs.size(); i++){ + for (size_t i=0; i < iplongs.size(); i++){ unsigned long diff = host > iplongs[i] ? host-iplongs[i] : iplongs[i]-host; if (diff < mini) { mini = diff; - index_mini = i; + index_mini = (int) i; } }