|
|
@ -378,6 +378,49 @@ bool enumerateDevices(enumeratorInfo *mlist, REFGUID category){ |
|
|
|
|
|
|
|
|
localIpAddresses g_vLocalIpAdressess; |
|
|
localIpAddresses g_vLocalIpAdressess; |
|
|
bool g_bAdressesAreDiscovered = false; |
|
|
bool g_bAdressesAreDiscovered = false; |
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
|
#include <linux/if.h>
|
|
|
|
|
|
localIpAddresses *getLocalIP(){ |
|
|
|
|
|
if( !g_bAdressesAreDiscovered ) { |
|
|
|
|
|
struct ifaddrs *ifaddr, *ifa; |
|
|
|
|
|
localIp4Info *tIpAddress = NULL; |
|
|
|
|
|
if (getifaddrs(&ifaddr) != -1) { |
|
|
|
|
|
/* Walk through linked list, maintaining head pointer so we can free list later */ |
|
|
|
|
|
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
|
|
|
|
|
char protocol[IFNAMSIZ] = {0}; |
|
|
|
|
|
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET) continue; |
|
|
|
|
|
tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info)); |
|
|
|
|
|
if(tIpAddress!=NULL){ |
|
|
|
|
|
sprintf( tIpAddress->address , "%d.%d.%d.%d", |
|
|
|
|
|
(uint8_t)ifa->ifa_addr->sa_data[2], |
|
|
|
|
|
(uint8_t)ifa->ifa_addr->sa_data[3], |
|
|
|
|
|
(uint8_t)ifa->ifa_addr->sa_data[4], |
|
|
|
|
|
(uint8_t)ifa->ifa_addr->sa_data[5] |
|
|
|
|
|
); |
|
|
|
|
|
sprintf( tIpAddress->netmask , "%d.%d.%d.%d", |
|
|
|
|
|
(uint8_t)ifa->ifa_netmask->sa_data[2], |
|
|
|
|
|
(uint8_t)ifa->ifa_netmask->sa_data[3], |
|
|
|
|
|
(uint8_t)ifa->ifa_netmask->sa_data[4], |
|
|
|
|
|
(uint8_t)ifa->ifa_netmask->sa_data[5] |
|
|
|
|
|
); |
|
|
|
|
|
sprintf( tIpAddress->broadcast , "%d.%d.%d.%d", |
|
|
|
|
|
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[2], |
|
|
|
|
|
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[3], |
|
|
|
|
|
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[4], |
|
|
|
|
|
(uint8_t)ifa->ifa_ifu.ifu_broadaddr->sa_data[5] |
|
|
|
|
|
); |
|
|
|
|
|
g_vLocalIpAdressess.push_back(tIpAddress); |
|
|
|
|
|
}; |
|
|
|
|
|
}; |
|
|
|
|
|
freeifaddrs(ifaddr); |
|
|
|
|
|
}; |
|
|
|
|
|
g_bAdressesAreDiscovered = true; |
|
|
|
|
|
}; |
|
|
|
|
|
return &g_vLocalIpAdressess; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
//https://stackoverflow.com/questions/5213629/how-to-enumerate-all-available-network-interfaces
|
|
|
|
|
|
|
|
|
localIpAddresses *getLocalIP(){ |
|
|
localIpAddresses *getLocalIP(){ |
|
|
char psBuffer[128]; |
|
|
char psBuffer[128]; |
|
|
@ -387,33 +430,21 @@ localIpAddresses *getLocalIP(){ |
|
|
|
|
|
|
|
|
if( g_bAdressesAreDiscovered ) return &g_vLocalIpAdressess; |
|
|
if( g_bAdressesAreDiscovered ) return &g_vLocalIpAdressess; |
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
|
fd = popen("ifconfig", "r"); |
|
|
|
|
|
#else
|
|
|
|
|
|
fd = _popen("ipconfig.exe","r"); |
|
|
fd = _popen("ipconfig.exe","r"); |
|
|
#endif
|
|
|
|
|
|
mB = 0; |
|
|
mB = 0; |
|
|
tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info)); |
|
|
tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info)); |
|
|
if (tIpAddress == NULL){ |
|
|
if (tIpAddress == NULL){ |
|
|
#ifdef __linux__
|
|
|
|
|
|
pclose(fd); |
|
|
|
|
|
#else
|
|
|
|
|
|
_pclose(fd); |
|
|
_pclose(fd); |
|
|
#endif
|
|
|
|
|
|
g_bAdressesAreDiscovered = true; |
|
|
g_bAdressesAreDiscovered = true; |
|
|
return &g_vLocalIpAdressess; |
|
|
return &g_vLocalIpAdressess; |
|
|
}; |
|
|
}; |
|
|
char tBuff[64]; |
|
|
char tBuff[64]; |
|
|
//#if defined( WIN_XP ) || defined( __linux__ )
|
|
|
//#if defined( WIN_XP ) || defined( __linux__ )
|
|
|
#ifdef __linux__
|
|
|
|
|
|
sprintf(tBuff, "inet addr"); |
|
|
|
|
|
#else
|
|
|
|
|
|
if (IsWindows7OrGreater()){ |
|
|
if (IsWindows7OrGreater()){ |
|
|
sprintf(tBuff, "IPv4 Address" ); |
|
|
sprintf(tBuff, "IPv4 Address" ); |
|
|
} else { |
|
|
} else { |
|
|
sprintf(tBuff, "IP Address"); |
|
|
sprintf(tBuff, "IP Address"); |
|
|
}; |
|
|
}; |
|
|
#endif
|
|
|
|
|
|
while (!feof(fd)){ |
|
|
while (!feof(fd)){ |
|
|
if( fgets( psBuffer, 128, fd ) != NULL ){ |
|
|
if( fgets( psBuffer, 128, fd ) != NULL ){ |
|
|
if( strstr( psBuffer, tBuff ) != NULL ){ |
|
|
if( strstr( psBuffer, tBuff ) != NULL ){ |
|
|
@ -422,35 +453,19 @@ localIpAddresses *getLocalIP(){ |
|
|
int tLen; |
|
|
int tLen; |
|
|
tLen = strlen(psBuffer) - (tVal - psBuffer) - 3; |
|
|
tLen = strlen(psBuffer) - (tVal - psBuffer) - 3; |
|
|
if( tLen > 4){ |
|
|
if( tLen > 4){ |
|
|
#ifdef __linux__
|
|
|
|
|
|
sscanf( tVal+1,"%s ", &tIpAddress->address ); |
|
|
|
|
|
#else
|
|
|
|
|
|
strncpy(tIpAddress->address, tVal + 2, tLen); |
|
|
strncpy(tIpAddress->address, tVal + 2, tLen); |
|
|
#endif
|
|
|
|
|
|
tIpAddress->address[tLen] = (char)0; |
|
|
tIpAddress->address[tLen] = (char)0; |
|
|
} else { |
|
|
} else { |
|
|
tIpAddress->address[0] = (char)0; |
|
|
tIpAddress->address[0] = (char)0; |
|
|
} |
|
|
} |
|
|
mB |= 1; |
|
|
mB |= 1; |
|
|
}; |
|
|
}; |
|
|
#ifdef __linux__
|
|
|
|
|
|
if (strstr(psBuffer, "Mask") != NULL){ |
|
|
|
|
|
CSTR tVal = strstr(psBuffer, "Mask:"); |
|
|
|
|
|
int tLen; |
|
|
|
|
|
tLen = strlen(tVal) - 6; |
|
|
|
|
|
#else
|
|
|
|
|
|
if (strstr(psBuffer, "Subnet Mask") != NULL){ |
|
|
if (strstr(psBuffer, "Subnet Mask") != NULL){ |
|
|
CSTR tVal = strstr(psBuffer, ":"); |
|
|
CSTR tVal = strstr(psBuffer, ":"); |
|
|
int tLen; |
|
|
int tLen; |
|
|
tLen = strlen(psBuffer) - (tVal - psBuffer) - 3; |
|
|
tLen = strlen(psBuffer) - (tVal - psBuffer) - 3; |
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
if (tLen > 7){ |
|
|
if (tLen > 7){ |
|
|
#ifdef __linux__
|
|
|
|
|
|
strncpy(tIpAddress->netmask, tVal + 5, tLen); |
|
|
|
|
|
#else
|
|
|
|
|
|
strncpy(tIpAddress->netmask, tVal + 2, tLen); |
|
|
strncpy(tIpAddress->netmask, tVal + 2, tLen); |
|
|
#endif
|
|
|
|
|
|
tIpAddress->netmask[tLen] = (char)0; |
|
|
tIpAddress->netmask[tLen] = (char)0; |
|
|
char A[4], B[4], C[4], D[4]; |
|
|
char A[4], B[4], C[4], D[4]; |
|
|
strncpy(A, tIpAddress->address, 3); |
|
|
strncpy(A, tIpAddress->address, 3); |
|
|
@ -494,19 +509,11 @@ localIpAddresses *getLocalIP(){ |
|
|
} |
|
|
} |
|
|
mB |= 4; |
|
|
mB |= 4; |
|
|
}; |
|
|
}; |
|
|
#ifdef __linux__
|
|
|
|
|
|
if ((mB & 3) == 3){ |
|
|
|
|
|
#else
|
|
|
|
|
|
if ((mB & 7) == 7){ |
|
|
if ((mB & 7) == 7){ |
|
|
#endif
|
|
|
|
|
|
g_vLocalIpAdressess.push_back(tIpAddress); |
|
|
g_vLocalIpAdressess.push_back(tIpAddress); |
|
|
tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info)); |
|
|
tIpAddress = (localIp4Info*)malloc(sizeof(localIp4Info)); |
|
|
if (tIpAddress == NULL){ |
|
|
if (tIpAddress == NULL){ |
|
|
#ifdef __linux__
|
|
|
|
|
|
pclose(fd); |
|
|
|
|
|
#else
|
|
|
|
|
|
_pclose(fd); |
|
|
_pclose(fd); |
|
|
#endif
|
|
|
|
|
|
g_bAdressesAreDiscovered = true; |
|
|
g_bAdressesAreDiscovered = true; |
|
|
return &g_vLocalIpAdressess; |
|
|
return &g_vLocalIpAdressess; |
|
|
}; |
|
|
}; |
|
|
@ -514,14 +521,11 @@ localIpAddresses *getLocalIP(){ |
|
|
}; |
|
|
}; |
|
|
}; |
|
|
}; |
|
|
}; |
|
|
}; |
|
|
#ifdef __linux__
|
|
|
|
|
|
pclose(fd); |
|
|
|
|
|
#else
|
|
|
|
|
|
_pclose(fd); |
|
|
_pclose(fd); |
|
|
#endif
|
|
|
|
|
|
g_bAdressesAreDiscovered = true; |
|
|
g_bAdressesAreDiscovered = true; |
|
|
return &g_vLocalIpAdressess; |
|
|
return &g_vLocalIpAdressess; |
|
|
}; |
|
|
}; |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
//#ifdef DEBUG
|
|
|
//#ifdef DEBUG
|
|
|
#undef URGANT_LOG
|
|
|
#undef URGANT_LOG
|
|
|
|