- Fix for IP Address discovery and broadcasting UDP servers
This commit is contained in:
parent
cdb820a8e8
commit
b3b2d098b7
|
@ -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,13 +472,17 @@ 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);
|
||||||
//printf(" Found interface: name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", p->ifa_name, "unavailable", ifaAddrStr, maskAddrStr, dstAddrStr);
|
//printf(" Found interface: name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", p->ifa_name, "unavailable", ifaAddrStr, maskAddrStr, dstAddrStr);
|
||||||
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,46 +606,56 @@ 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)
|
||||||
|
char myhostaddr[101] = "";
|
||||||
|
int ipIdx = 0;
|
||||||
|
while (myhostent->h_addr_list[ipIdx] != 0) {
|
||||||
|
sprintf(myhostaddr, "%s",inet_ntoa(*(struct in_addr *)myhostent->h_addr_list[ipIdx]));
|
||||||
|
//printf("%s\n",myhostaddr);
|
||||||
|
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] myhostaddr = [%s]\n",__FILE__,__FUNCTION__,__LINE__,myhostaddr);
|
||||||
|
|
||||||
// get all host IP addresses (Except for loopback)
|
if(strlen(myhostaddr) > 0 && strncmp(myhostaddr,"127.",4) != 0) {
|
||||||
char myhostaddr[101] = "";
|
ipList.push_back(myhostaddr);
|
||||||
int ipIdx = 0;
|
}
|
||||||
while (myhostent->h_addr_list[ipIdx] != 0) {
|
ipIdx++;
|
||||||
sprintf(myhostaddr, "%s",inet_ntoa(*(struct in_addr *)myhostent->h_addr_list[ipIdx]));
|
}
|
||||||
//printf("%s\n",myhostaddr);
|
}
|
||||||
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] myhostaddr = [%s]\n",__FILE__,__FUNCTION__,__LINE__,myhostaddr);
|
|
||||||
|
|
||||||
if(strlen(myhostaddr) > 0 && strncmp(myhostaddr,"127.",4) != 0) {
|
|
||||||
ipList.push_back(myhostaddr);
|
|
||||||
}
|
|
||||||
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;
|
||||||
PLATFORM_SOCKET fd = socket(AF_INET, SOCK_DGRAM, 0);
|
intfTypes.push_back("eth");
|
||||||
|
intfTypes.push_back("wlan");
|
||||||
|
intfTypes.push_back("vboxnet");
|
||||||
|
|
||||||
/* I want to get an IPv4 IP address */
|
for(int intfIdx = 0; intfIdx < intfTypes.size(); intfIdx++) {
|
||||||
struct ifreq ifr;
|
string intfName = intfTypes[intfIdx];
|
||||||
ifr.ifr_addr.sa_family = AF_INET;
|
for(int idx = 0; idx < 10; ++idx) {
|
||||||
|
PLATFORM_SOCKET fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
/* I want IP address attached to "eth0" */
|
/* I want to get an IPv4 IP address */
|
||||||
char szBuf[10]="";
|
struct ifreq ifr;
|
||||||
sprintf(szBuf,"eth%d",idx);
|
ifr.ifr_addr.sa_family = AF_INET;
|
||||||
strncpy(ifr.ifr_name, szBuf, IFNAMSIZ-1);
|
|
||||||
ioctl(fd, SIOCGIFADDR, &ifr);
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
sprintf(myhostaddr, "%s",inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
|
/* I want IP address attached to "eth0" */
|
||||||
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] myhostaddr = [%s]\n",__FILE__,__FUNCTION__,__LINE__,myhostaddr);
|
char szBuf[100]="";
|
||||||
|
sprintf(szBuf,"%s%d",intfName.c_str(),idx);
|
||||||
|
strncpy(ifr.ifr_name, szBuf, IFNAMSIZ-1);
|
||||||
|
ioctl(fd, SIOCGIFADDR, &ifr);
|
||||||
|
close(fd);
|
||||||
|
|
||||||
if(strlen(myhostaddr) > 0 && strncmp(myhostaddr,"127.",4) != 0) {
|
sprintf(myhostaddr, "%s",inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
|
||||||
if(std::find(ipList.begin(),ipList.end(),myhostaddr) == ipList.end()) {
|
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] szBuf [%s], myhostaddr = [%s]\n",__FILE__,__FUNCTION__,__LINE__,szBuf,myhostaddr);
|
||||||
ipList.push_back(myhostaddr);
|
|
||||||
|
if(strlen(myhostaddr) > 0 && strncmp(myhostaddr,"127.",4) != 0) {
|
||||||
|
if(std::find(ipList.begin(),ipList.end(),myhostaddr) == ipList.end()) {
|
||||||
|
ipList.push_back(myhostaddr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1659,15 +1675,22 @@ 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.
|
||||||
bcfd[idx] = socket( AF_INET, SOCK_DGRAM, 0 );
|
#ifdef WIN32
|
||||||
if( bcfd[idx] <= 0 ) {
|
bcfd[idx] = INVALID_SOCKET;
|
||||||
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Unable to allocate broadcast socket [%s]: %s\n", subnetmask[idx], getLastSocketErrorFormattedText().c_str());
|
#else
|
||||||
//exit(-1);
|
bcfd[idx] = -1;
|
||||||
}
|
#endif
|
||||||
// Mark the socket for broadcast.
|
if(strlen(subnetmask[idx]) > 0) {
|
||||||
else if( setsockopt( bcfd[idx], SOL_SOCKET, SO_BROADCAST, (const char *) &one, sizeof( int ) ) < 0 ) {
|
bcfd[idx] = socket( AF_INET, SOCK_DGRAM, 0 );
|
||||||
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Could not set socket to broadcast [%s]: %s\n", subnetmask[idx], getLastSocketErrorFormattedText().c_str());
|
if( bcfd[idx] <= 0 ) {
|
||||||
//exit(-1);
|
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Unable to allocate broadcast socket [%s]: %s\n", subnetmask[idx], getLastSocketErrorFormattedText().c_str());
|
||||||
|
//exit(-1);
|
||||||
|
}
|
||||||
|
// Mark the socket for broadcast.
|
||||||
|
else if( setsockopt( bcfd[idx], SOL_SOCKET, SO_BROADCAST, (const char *) &one, sizeof( int ) ) < 0 ) {
|
||||||
|
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"Could not set socket to broadcast [%s]: %s\n", subnetmask[idx], getLastSocketErrorFormattedText().c_str());
|
||||||
|
//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]);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user