- Fix for IP Address discovery and broadcasting UDP servers

This commit is contained in:
Mark Vejvoda
2010-06-14 21:03:23 +00:00
parent cdb820a8e8
commit b3b2d098b7

View File

@@ -459,9 +459,11 @@ string getNetworkInterfaceBroadcastAddress(string ipAddress)
#if defined(USE_GETIFADDRS) #if defined(USE_GETIFADDRS)
// BSD-style implementation // BSD-style implementation
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
struct ifaddrs * ifap; struct ifaddrs * ifap;
if (getifaddrs(&ifap) == 0) if (getifaddrs(&ifap) == 0)
{ {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
struct ifaddrs * p = ifap; struct ifaddrs * p = ifap;
while(p) while(p)
{ {
@@ -470,6 +472,8 @@ string getNetworkInterfaceBroadcastAddress(string ipAddress)
uint32 dstAddr = SockAddrToUint32(p->ifa_dstaddr); uint32 dstAddr = SockAddrToUint32(p->ifa_dstaddr);
if (ifaAddr > 0) if (ifaAddr > 0)
{ {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
char ifaAddrStr[32]; Inet_NtoA(ifaAddr, ifaAddrStr); char ifaAddrStr[32]; Inet_NtoA(ifaAddr, ifaAddrStr);
char maskAddrStr[32]; Inet_NtoA(maskAddr, maskAddrStr); char maskAddrStr[32]; Inet_NtoA(maskAddr, maskAddrStr);
char dstAddrStr[32]; Inet_NtoA(dstAddr, dstAddrStr); char dstAddrStr[32]; Inet_NtoA(dstAddr, dstAddrStr);
@@ -477,6 +481,8 @@ string getNetworkInterfaceBroadcastAddress(string ipAddress)
if(strcmp(ifaAddrStr,ipAddress.c_str()) == 0) { if(strcmp(ifaAddrStr,ipAddress.c_str()) == 0) {
broadCastAddress = dstAddrStr; broadCastAddress = dstAddrStr;
} }
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ifaAddrStr [%s], maskAddrStr [%s], dstAddrStr[%s], ipAddress [%s], broadCastAddress [%s]\n",__FILE__,__FUNCTION__,__LINE__,ifaAddrStr,maskAddrStr,dstAddrStr,ipAddress.c_str(),broadCastAddress.c_str());
} }
p = p->ifa_next; p = p->ifa_next;
} }
@@ -600,9 +606,10 @@ std::vector<std::string> Socket::getLocalIPAddressList() {
/* get my host name */ /* get my host name */
char myhostname[101]=""; char myhostname[101]="";
gethostname(myhostname,100); gethostname(myhostname,100);
char myhostaddr[101] = "";
struct hostent* myhostent = gethostbyname(myhostname); struct hostent* myhostent = gethostbyname(myhostname);
if(myhostent) {
// get all host IP addresses (Except for loopback) // get all host IP addresses (Except for loopback)
char myhostaddr[101] = ""; char myhostaddr[101] = "";
int ipIdx = 0; int ipIdx = 0;
@@ -616,11 +623,19 @@ std::vector<std::string> Socket::getLocalIPAddressList() {
} }
ipIdx++; ipIdx++;
} }
}
#ifndef WIN32 #ifndef WIN32
// Now check all linux network devices // Now check all linux network devices
for(int idx = 0; idx < 5; ++idx) { std::vector<string> intfTypes;
intfTypes.push_back("eth");
intfTypes.push_back("wlan");
intfTypes.push_back("vboxnet");
for(int intfIdx = 0; intfIdx < intfTypes.size(); intfIdx++) {
string intfName = intfTypes[intfIdx];
for(int idx = 0; idx < 10; ++idx) {
PLATFORM_SOCKET fd = socket(AF_INET, SOCK_DGRAM, 0); PLATFORM_SOCKET fd = socket(AF_INET, SOCK_DGRAM, 0);
/* I want to get an IPv4 IP address */ /* I want to get an IPv4 IP address */
@@ -628,14 +643,14 @@ std::vector<std::string> Socket::getLocalIPAddressList() {
ifr.ifr_addr.sa_family = AF_INET; ifr.ifr_addr.sa_family = AF_INET;
/* I want IP address attached to "eth0" */ /* I want IP address attached to "eth0" */
char szBuf[10]=""; char szBuf[100]="";
sprintf(szBuf,"eth%d",idx); sprintf(szBuf,"%s%d",intfName.c_str(),idx);
strncpy(ifr.ifr_name, szBuf, IFNAMSIZ-1); strncpy(ifr.ifr_name, szBuf, IFNAMSIZ-1);
ioctl(fd, SIOCGIFADDR, &ifr); ioctl(fd, SIOCGIFADDR, &ifr);
close(fd); close(fd);
sprintf(myhostaddr, "%s",inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); sprintf(myhostaddr, "%s",inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] myhostaddr = [%s]\n",__FILE__,__FUNCTION__,__LINE__,myhostaddr); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] szBuf [%s], myhostaddr = [%s]\n",__FILE__,__FUNCTION__,__LINE__,szBuf,myhostaddr);
if(strlen(myhostaddr) > 0 && strncmp(myhostaddr,"127.",4) != 0) { if(strlen(myhostaddr) > 0 && strncmp(myhostaddr,"127.",4) != 0) {
if(std::find(ipList.begin(),ipList.end(),myhostaddr) == ipList.end()) { if(std::find(ipList.begin(),ipList.end(),myhostaddr) == ipList.end()) {
@@ -643,6 +658,7 @@ std::vector<std::string> Socket::getLocalIPAddressList() {
} }
} }
} }
}
#endif #endif
@@ -1659,6 +1675,12 @@ void BroadCastSocketThread::execute() {
bcLocal[idx].sin_family = AF_INET; bcLocal[idx].sin_family = AF_INET;
bcLocal[idx].sin_addr.s_addr = inet_addr(subnetmask[idx]); //htonl( INADDR_BROADCAST ); bcLocal[idx].sin_addr.s_addr = inet_addr(subnetmask[idx]); //htonl( INADDR_BROADCAST );
bcLocal[idx].sin_port = port; // We are letting the OS fill in the port number for the local machine. bcLocal[idx].sin_port = port; // We are letting the OS fill in the port number for the local machine.
#ifdef WIN32
bcfd[idx] = INVALID_SOCKET;
#else
bcfd[idx] = -1;
#endif
if(strlen(subnetmask[idx]) > 0) {
bcfd[idx] = socket( AF_INET, SOCK_DGRAM, 0 ); bcfd[idx] = socket( AF_INET, SOCK_DGRAM, 0 );
if( bcfd[idx] <= 0 ) { if( bcfd[idx] <= 0 ) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Unable to allocate broadcast socket [%s]: %s\n", subnetmask[idx], getLastSocketErrorFormattedText().c_str()); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Unable to allocate broadcast socket [%s]: %s\n", subnetmask[idx], getLastSocketErrorFormattedText().c_str());
@@ -1669,6 +1691,7 @@ void BroadCastSocketThread::execute() {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Could not set socket to broadcast [%s]: %s\n", subnetmask[idx], getLastSocketErrorFormattedText().c_str()); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Could not set socket to broadcast [%s]: %s\n", subnetmask[idx], getLastSocketErrorFormattedText().c_str());
//exit(-1); //exit(-1);
} }
}
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] setting up broadcast on address [%s]\n",__FILE__,__FUNCTION__,__LINE__,subnetmask[idx]); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] setting up broadcast on address [%s]\n",__FILE__,__FUNCTION__,__LINE__,subnetmask[idx]);
} }