From 72097c1f42de24fa1c850edb8dd9d30f5f71875d Mon Sep 17 00:00:00 2001 From: Ed Gonzalez Date: Thu, 15 Jan 2015 12:02:18 -0600 Subject: [PATCH] When starting the Traffic Cop, check if the supplied host IP is the same as the waveforms. Use loopback if this is the case --- DSP_API/SmartSDR_Interface/traffic_cop.c | 45 +++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/DSP_API/SmartSDR_Interface/traffic_cop.c b/DSP_API/SmartSDR_Interface/traffic_cop.c index 6b6651d..0fcb385 100644 --- a/DSP_API/SmartSDR_Interface/traffic_cop.c +++ b/DSP_API/SmartSDR_Interface/traffic_cop.c @@ -44,6 +44,7 @@ #include #include #include +#include #include "common.h" #include "traffic_cop.h" @@ -613,8 +614,50 @@ void tc_Init(const char * hostname, const char * api_port) output("NULL Hostname - tc_setHostname()\n"); return; } + struct ifaddrs *ifaddr, *ifa; + int family, s, n; + char host[NI_MAXHOST]; + BOOL use_loopback = FALSE; + + if ( getifaddrs(&ifaddr) == -1 ) { + output("Error getting local interface addrs. Using ip supplied in discovery packet\n"); + } else { + /* Walk through linked list of interfaces. */ + ifa = ifaddr; + for ( n = 0; ifa != NULL; ifa = ifa->ifa_next, n++ ) { + if ( ifa->ifa_addr == NULL ) + continue; + + family = ifa->ifa_addr->sa_family; + + /* Only care about IPV4 adresses */ + if ( family == AF_INET ) { + s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + if ( s != 0 ) { + output("getnameinfo() failed: %s\n", gai_strerror(s)); + continue; + } else { + + /* We are on the same IP in the waveform and the radio hence we'll use local loopback interface instead + * of the radios IP + */ + if ( strncmp(hostname, host, NI_MAXHOST) == 0 ) { + use_loopback = TRUE; + output("We are on the same IP as the radio. Using loopback interface\n"); + break; + } + } + } else { + continue; + } + } + } + + if ( !use_loopback ) + strncpy(_hostname, hostname, 31); + else + strncpy(_hostname, "127.0.0.1", 31); - strncpy(_hostname, hostname, 31); strncpy(_api_port, api_port, 31);