What version are we talking about?
Have you tried using the NetworkInterface::map() function in trunk and just iterating over that until you get a match?
-Philip
On 9/13/12 1:38 AM, Michele Pradella wrote:
> I submit you the function patched just to give you an idea. It's a patch only when the define POCO_WIN32_UTF8 is not defined!
>
> //The second parameters is changed from bool to IPAddress
> IPAddress subnetMaskForInterface(const std::string& name, IPAddress address)
> {
> int iIdx=0;
> if (address.isLoopback())
> {
> return IPAddress::parse("255.0.0.0");
> }
> else
> {
> std::string subKey("SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces\\");
> subKey += name;
> std::string netmask;
> HKEY hKey;
> #if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
> std::wstring usubKey;
> Poco::UnicodeConverter::toUTF16(subKey, usubKey);
> if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, usubKey.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
> return IPAddress();
> wchar_t unetmask[16];
> DWORD size = sizeof(unetmask);
> if (RegQueryValueExW(hKey, L"DhcpSubnetMask", NULL, NULL, (LPBYTE) &unetmask, &size) != ERROR_SUCCESS)
> {
> if (RegQueryValueExW(hKey, L"SubnetMask", NULL, NULL, (LPBYTE) &unetmask, &size) != ERROR_SUCCESS)
> {
> RegCloseKey(hKey);
> return IPAddress();
> }
> }
> Poco::UnicodeConverter::toUTF8(unetmask, netmask);
> #else
> if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subKey.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
> return IPAddress();
>
> //make enough room for multiple IP/MASK
> char unetmask[256]={0};
> DWORD size = sizeof(unetmask)-1;
> if (RegQueryValueEx(hKey, "IPAddress", NULL, NULL, (LPBYTE) &unetmask, &size) != ERROR_SUCCESS)
> {
> RegCloseKey(hKey);
> return IPAddress();
> }
> //unetmask is filled with IP1\0IP2\0 so I change the \0 with ; to mnake it work with StringTokenizer
> for(int i=0;i<size;i++){
> if(unetmask[i]=='\0')
> unetmask[i]=';';
> }
> unetmask[size]='\0';
> Poco::StringTokenizer stIP(unetmask,";",Poco::StringTokenizer::TOK_IGNORE_EMPTY|Poco::StringTokenizer::TOK_TRIM);
> //look for current IP address to find out the iIdx position
> std::string sIP=address.toString();
> for(auto it=stIP.begin();it!=stIP.end();it++){
> if(sIP==*it){
> iIdx=it-stIP.begin();
> break;
> }
> }
>
> size = sizeof(unetmask)-1;
> if (RegQueryValueExA(hKey, "DhcpSubnetMask", NULL, NULL, (LPBYTE) &unetmask, &size) != ERROR_SUCCESS)
> {
> if (RegQueryValueExA(hKey, "SubnetMask", NULL, NULL, (LPBYTE) &unetmask, &size) != ERROR_SUCCESS)
> {
> RegCloseKey(hKey);
> return IPAddress();
> }
> }
> //unetmask is filled with MASK1\0MASK2\0 so I change the \0 with ; to mnake it work with StringTokenizer
> for(int i=0;i<size;i++){
> if(unetmask[i]=='\0')
> unetmask[i]=';';
> }
> unetmask[size]='\0';
> Poco::StringTokenizer stMask(unetmask,";",Poco::StringTokenizer::TOK_IGNORE_EMPTY|Poco::StringTokenizer::TOK_TRIM);
> //Get the mask in the same index of the IP address
> if(stMask.count()>iIdx){
> netmask=stMask[iIdx];
> }else{
> netmask = unetmask;
> }
> #endif
> RegCloseKey(hKey);
> return IPAddress::parse(netmask);
> }
> }
>
> Il 13/09/2012 08:25, Michele Pradella ha scritto:
>> The function
>> IPAddress subnetMaskForInterface
>> do not take the correct subnetmask when you have more than 1 IP address configured in your network card
>>
>> --
>> Selea s.r.l.
>>
>>
>> Michele Pradella R&D
>>
>>
>> SELEA s.r.l.
>>
|