OSX compilation compatibility

This commit is contained in:
brunoherbelin
2020-10-26 19:32:19 +01:00
parent 7438b257ae
commit a4621f31e3

View File

@@ -4,10 +4,13 @@
#include <unistd.h> #include <unistd.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <net/if.h>
// TODO OSX implementation #ifdef linux
#include <linux/netdevice.h> #include <linux/netdevice.h>
#endif
// OSC IP gethostbyname // OSC IP gethostbyname
#include "ip/NetworkingUtils.h" #include "ip/NetworkingUtils.h"
@@ -87,11 +90,14 @@ const std::vector<std::string> NetworkToolkit::protocol_receive_pipeline {
std::vector<std::string> ipstrings; std::vector<std::string> ipstrings;
std::vector<unsigned long> iplongs; std::vector<unsigned long> iplongs;
std::vector<std::string> NetworkToolkit::host_ips() std::vector<std::string> NetworkToolkit::host_ips()
{ {
// fill the list of IPs only once // fill the list of IPs only once
if (ipstrings.empty()) { if (ipstrings.empty()) {
// fprintf(stderr, "List of ips: \n" );
int s = socket(AF_INET, SOCK_STREAM, 0); int s = socket(AF_INET, SOCK_STREAM, 0);
if (s > -1) { if (s > -1) {
struct ifconf ifconf; struct ifconf ifconf;
@@ -100,8 +106,11 @@ std::vector<std::string> NetworkToolkit::host_ips()
int i; int i;
ifconf.ifc_buf = (char *) ifr; ifconf.ifc_buf = (char *) ifr;
#ifndef linux
ifconf.ifc_len = IFNAMSIZ + ifr->ifr_addr.sa_len;
#else
ifconf.ifc_len = sizeof ifr; ifconf.ifc_len = sizeof ifr;
#endif
if (ioctl(s, SIOCGIFCONF, &ifconf) > -1) { if (ioctl(s, SIOCGIFCONF, &ifconf) > -1) {
ifs = ifconf.ifc_len / sizeof(ifr[0]); ifs = ifconf.ifc_len / sizeof(ifr[0]);
for (i = 0; i < ifs; i++) { for (i = 0; i < ifs; i++) {
@@ -111,6 +120,7 @@ std::vector<std::string> NetworkToolkit::host_ips()
if (inet_ntop(AF_INET, &s_in->sin_addr, ip, sizeof(ip))) { if (inet_ntop(AF_INET, &s_in->sin_addr, ip, sizeof(ip))) {
ipstrings.push_back( std::string(ip) ); ipstrings.push_back( std::string(ip) );
iplongs.push_back( GetHostByName(ip) ); iplongs.push_back( GetHostByName(ip) );
// fprintf(stderr, "%s %lu", ip, GetHostByName(ip) );
} }
} }
close(s); 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 host = GetHostByName( ip.c_str() );
unsigned long mini = host; 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; unsigned long diff = host > iplongs[i] ? host-iplongs[i] : iplongs[i]-host;
if (diff < mini) { if (diff < mini) {
mini = diff; mini = diff;
index_mini = i; index_mini = (int) i;
} }
} }