Esempio n. 1
0
LPCTSTR GetAdapterType( UINT uType)
{
	if (IsEthernet( uType))
		return ETHERNET_ADAPTER;
	if (IsTokenRing( uType))
		return TOKENRING_ADAPTER;
	if (IsTokenBus( uType))
		return TOKENBUS_ADAPTER;
	if (IsATM( uType))
		return ATM_ADAPTER;
	if (IsDialup( uType))
		return DIALUP_ADAPTER;
	if (IsLoopback( uType))
		return LOOPBACK_ADAPTER;
	return OTHER_ADAPTER;
}
Esempio n. 2
0
int syscfg_GetIFInfo_2000(int *count, int *addrs, int *masks, int *mtus, int *flags)
{
    int maxCount = *count;
    char *IFListBase = NULL;
    char *IFList, *ifname;
    HKEY skey;
    int i, n, nConfig;

    if (RegOpenKeyAlt(AFSREG_NULL_KEY, AFSREG_IPSRV_KEY,
		      KEY_READ, 0, &skey, NULL))
	return -1;

    if ((nConfig = GetInterfaceList(skey, &IFListBase)) < 0) {
	(void) RegCloseKey(skey);
	return -1;
    }

    IFList = IFListBase;
    n = 0;

    while ((n < maxCount) && (ifname = GetNextInterface(IFList))) {
	if (!IsLoopback(ifname) && GetIP(skey, ifname, &addrs[n], &masks[n]) == 0 && addrs[n] != 0) {
	    n++;
	} else {
            maxCount--;
        }
	IFList = ifname;
    }

    /* And until we get mtu's and flags */
    for (i = 0; i < n; i++) {
	mtus[i] = 1500;
	flags[i] = 0;
    }

    (void) RegCloseKey(skey);
    free(IFListBase);

    *count = n;
    return nConfig;
}
Esempio n. 3
0
int syscfg_GetIFInfo(int *count, int *addrs, int *masks, int *mtus, int *flags)
{
    PMIB_IPADDRTABLE pIpAddrTable = NULL;
    DWORD            validAddrs = 0;

    int maxCount = *count;
    int nConfig = 0;
    PIP_ADAPTER_ADDRESSES pAddresses, cAddress;
    PMIB_IPADDRTABLE pIpTbl;
    ULONG outBufLen = 0;
    DWORD dwRetVal = 0;
    int n = 0;
    DWORD i;

    HMODULE hIpHlp;
    DWORD (WINAPI *pGetAdaptersAddresses)(ULONG, DWORD, PVOID, 
                                          PIP_ADAPTER_ADDRESSES, PULONG) = 0;

    hIpHlp = LoadLibrary("iphlpapi");
    if (hIpHlp != NULL) {
        (FARPROC) pGetAdaptersAddresses = GetProcAddress(hIpHlp, "GetAdaptersAddresses");
        if (pGetAdaptersAddresses == NULL)
            FreeLibrary(hIpHlp);
    }

    if (pGetAdaptersAddresses == NULL)
        return syscfg_GetIFInfo_2000(count, addrs, masks, mtus, flags);

    /* first pass to get the required size of the IP table */
    pIpTbl = (PMIB_IPADDRTABLE) malloc(sizeof(MIB_IPADDRTABLE));
    outBufLen = sizeof(MIB_IPADDRTABLE);
    
    dwRetVal = GetIpAddrTable(pIpTbl, &outBufLen, FALSE);
    if (dwRetVal != ERROR_INSUFFICIENT_BUFFER) {
        /* this should have failed with an insufficient buffer because we
           didn't give any space to place the IP addresses */
        free(pIpTbl);
        *count = 0;
        nConfig = -1;
        goto done;
    }
    
    /* second pass to get the actual data */
    free(pIpTbl);
    pIpTbl = (PMIB_IPADDRTABLE) malloc(outBufLen);
    
    dwRetVal = GetIpAddrTable(pIpTbl, &outBufLen, FALSE);
    if (dwRetVal != NO_ERROR) {
        free(pIpTbl);
        *count = 0;
        nConfig = -1;
        goto done;
    }
    
    pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(sizeof(IP_ADAPTER_ADDRESSES));
    
    /* first call gets required buffer size */
    if (pGetAdaptersAddresses(AF_INET, 
                              0, 
                              NULL, 
                              pAddresses, 
                              &outBufLen) == ERROR_BUFFER_OVERFLOW) 
    {
        free(pAddresses);
        pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(outBufLen);
    } else {
        free(pIpTbl);
        *count = 0;
        nConfig = -1;
        goto done;
    }
    
    /* second call to get the actual data */
    if ((dwRetVal = pGetAdaptersAddresses(AF_INET, 
                                          0, 
                                          NULL, 
                                          pAddresses, 
                                          &outBufLen)) == NO_ERROR) 
    {
        /* we have a list of addresses.  go through them and figure out
           the IP addresses */
        for (cAddress = pAddresses; cAddress; cAddress = cAddress->Next) {
            
            /* skip software loopback adapters */
            if (cAddress->IfType == IF_TYPE_SOFTWARE_LOOPBACK)
                continue;
            
            /* also skip interfaces that are not up */
            if (cAddress->OperStatus != 1)
                continue;
            
            /* starting with the AdapterName, which is actually the adapter
               instance GUID, check if this is a MS loopback device */
            if (IsLoopback(cAddress->AdapterName))
                continue;
            
            /* ok. looks good.  Now fish out all the addresses from the
               address table corresponding to the interface, and add them
               to the list */
            for (i=0;i<pIpTbl->dwNumEntries;i++) {
                if (pIpTbl->table[i].dwIndex == cAddress->IfIndex)
                {
                    if (n < maxCount) {
                        addrs[n] = ntohl(pIpTbl->table[i].dwAddr);
                        masks[n] = ntohl(pIpTbl->table[i].dwMask);
                        mtus[n] = cAddress->Mtu;
                        flags[n] = 0;
                        n++;
                    }
                    nConfig++;
                }
            }
        }
        
        free(pAddresses);
        free(pIpTbl);
        
        *count = n;
    } else { 
        /* again. this is bad */
        free(pAddresses);
        free(pIpTbl);
        *count = 0;
        nConfig = -1;
    }

  done:
    FreeLibrary(hIpHlp);
    return nConfig;
}
BOOL CIPHelper::GetNetworkAdapters(CNetworkAdapterList *pList)
{
    HINSTANCE			hDll;
	DWORD				(WINAPI *lpfnGetAdaptersInfo)( PIP_ADAPTER_INFO myInfo, ULONG *pLength);
	DWORD				(WINAPI *lpfnGetIfTable)( PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder);
	PMIB_IFTABLE		pIfTable;
	PMIB_IFROW			pIfEntry;
	PIP_ADAPTER_INFO	pAdapterTable, pAdapterInfo;
	ULONG				ulLength = 0;
	UINT				uIndex = 0;
	DWORD				dwIndex;
	CNetworkAdapter		cAdapter;
	PIP_ADDR_STRING		pAddressList;
	CString				csMAC,
						csAddress,
						csGateway,
						csDhcpServer;

	AddLog( _T( "IpHlpAPI GetNetworkAdapters...\n"));
	// Reset network adapter list content
	while (!(pList->GetCount() == 0))
		pList->RemoveHead();
    // Load the IpHlpAPI dll and get the addresses of necessary functions
    if ((hDll = LoadLibrary(_T( "iphlpapi.dll"))) < (HINSTANCE) HINSTANCE_ERROR) 
	{
		// Cannot load IpHlpAPI MIB
 		AddLog( _T( "IpHlpAPI GetNetworkAdapters: Failed because unable to load <iphlpapi.dll> !\n"));
		hDll = NULL;
        return FALSE;
    }
	if ((*(FARPROC*)&lpfnGetIfTable = GetProcAddress( hDll, _T( "GetIfTable"))) == NULL)
	{
		// Tell the user that we could not find a usable IpHlpAPI DLL.                                  
		FreeLibrary( hDll);
		AddLog( _T( "IpHlpAPI GetNetworkAdapters: Failed because unable to load <iphlpapi.dll> !\n"));
		return FALSE;
	}
	if ((*(FARPROC*)&lpfnGetAdaptersInfo = GetProcAddress( hDll, _T( "GetAdaptersInfo"))) == NULL)
	{
		// Tell the user that we could not find a usable IpHlpAPI DLL.                                  
		FreeLibrary( hDll);
		AddLog( _T( "IpHlpAPI GetNetworkAdapters: Failed because unable to load <iphlpapi.dll> !\n"));
		return FALSE;
	}

	// Call GetIfTable to get memory size
	AddLog( _T( "IpHlpAPI GetNetworkAdapters: Calling GetIfTable to determine network adapter properties..."));
	pIfTable = NULL;
	ulLength = 0;
	switch( lpfnGetIfTable( pIfTable, &ulLength, TRUE))
	{
	case NO_ERROR: // No error => no adapters
		FreeLibrary( hDll);
		AddLog( _T( "Failed because no network adapters !\n"));
		return FALSE;
	case ERROR_NOT_SUPPORTED: // Not supported
		FreeLibrary( hDll);
		AddLog( _T( "Failed because OS not support GetIfTable API function !\n" ));
		return FALSE;
	case ERROR_BUFFER_OVERFLOW: // We must allocate memory
	case ERROR_INSUFFICIENT_BUFFER:
		break;
	default:
		FreeLibrary( hDll);
		AddLog( _T( "Failed because unknown error !\n" ));
		return FALSE;
	}
	if ((pIfTable = (PMIB_IFTABLE) malloc( ulLength+1)) == NULL)
	{
		FreeLibrary( hDll);
		AddLog( _T( "Failed because memory error !\n" ));
		return FALSE;
	}
	// Recall GetIfTable
	switch( lpfnGetIfTable( pIfTable, &ulLength, TRUE))
	{
	case NO_ERROR: // No error
		break;
	case ERROR_NOT_SUPPORTED: // Not supported
		free( pIfTable);
		FreeLibrary( hDll);
		AddLog( _T( "Failed because OS not support GetIfTable API function !\n" ));
		return FALSE;
	case ERROR_BUFFER_OVERFLOW: // We have allocated needed memory, but not sufficient
	case ERROR_INSUFFICIENT_BUFFER:
		free( pIfTable);
		FreeLibrary( hDll);
		AddLog( _T( "Failed because memory error !\n" ));
		return FALSE;
	default:
		free( pIfTable);
		FreeLibrary( hDll);
		AddLog( _T( "Failed because unknown error !\n" ));
		return FALSE;
	}
/*
	// Dump IfTable
	AddLog("\n=== DEBUG : dump IfTable ===\n");
	for (dwIndex = 0; dwIndex < pIfTable->dwNumEntries; dwIndex ++)
	{
//		char wszName[MAX_INTERFACE_NAME_LEN];
//		WideCharToMultiByte(CP_ACP, 0, pIfTable->table[dwIndex].wszName, -1, wszName, MAX_INTERFACE_NAME_LEN, NULL, NULL);

		AddLog("+ Interface #%d\n", dwIndex);
//		AddLog("\twszName       = %s\n", wszName);
		AddLog("\tdwIndex       = %d\n", pIfTable->table[dwIndex].dwIndex);
		AddLog("\tdwType        = %d\n", pIfTable->table[dwIndex].dwType);
		AddLog("\tdwMtu         = %d\n", pIfTable->table[dwIndex].dwMtu);
		AddLog("\tdwSpeed       = %d\n", pIfTable->table[dwIndex].dwSpeed);
		AddLog("\tdwPhysAddrLen = %d\n", pIfTable->table[dwIndex].dwPhysAddrLen);
		AddLog("\tbPhysAddr     =");
		for (DWORD i=0 ; i<pIfTable->table[dwIndex].dwPhysAddrLen ; i++)
			AddLog(" %02x", pIfTable->table[dwIndex].bPhysAddr[i]);
		AddLog("\n");
		AddLog("\tbPhysAddr     = %d\n", pIfTable->table[dwIndex].dwPhysAddrLen);
		AddLog("\tdwAdminStatus = %d\n", pIfTable->table[dwIndex].dwAdminStatus);

		AddLog("\tdwOperStatus  = %d\n", pIfTable->table[dwIndex].dwOperStatus);
		AddLog("\tdwLastChange  = %d\n", pIfTable->table[dwIndex].dwLastChange);
		AddLog("\tbDescr        = %s\n", pIfTable->table[dwIndex].bDescr);
	}
*/
	// Call GetAdptersInfo with length to 0 to get size of required buffer
	AddLog( _T( "OK\nIpHlpAPI GetNetworkAdapters: Calling GetAdapterInfo to determine IP Infos..."));
	pAdapterTable = NULL;
	switch( lpfnGetAdaptersInfo( pAdapterTable, &ulLength))
	{
	case NO_ERROR: // No error => no adapters
	case ERROR_NO_DATA:
		free( pIfTable);
		FreeLibrary( hDll);
		AddLog( _T( "Failed because no network adapters !\n"));
		return FALSE;
	case ERROR_NOT_SUPPORTED: // Not supported
		free( pIfTable);
		FreeLibrary( hDll);
		AddLog( _T( "Failed because OS not support GetAdaptersInfo API function !\n" ));
		return FALSE;
	case ERROR_BUFFER_OVERFLOW: // We must allocate memory
		break;
	default:
		free( pIfTable);
		FreeLibrary( hDll);
		AddLog( _T( "Failed because unknown error !\n" ));
		return FALSE;
	}
	if ((pAdapterTable = (PIP_ADAPTER_INFO) malloc( ulLength+1)) == NULL)
	{
		FreeLibrary( hDll);
		AddLog( _T( "Failed because memory error !\n" ));
		return FALSE;
	}

	// Recall GetAdptersInfo
	switch( lpfnGetAdaptersInfo( pAdapterTable, &ulLength))
	{
	case 0: // No error
		break;
	case ERROR_NO_DATA: // No adapters
		free( pIfTable);
		free( pAdapterTable);
		FreeLibrary( hDll);
		AddLog( _T( "Failed because no network adapters !\n"));
		return FALSE;
	case ERROR_NOT_SUPPORTED: // Not supported
		free( pIfTable);
		free( pAdapterTable);
		FreeLibrary( hDll);
		AddLog( _T( "Failed because OS not support GetAdaptersInfo API function !\n" ));
		return FALSE;
	case ERROR_BUFFER_OVERFLOW: // We have allocated needed memory, but not sufficient
		free( pIfTable);
		free( pAdapterTable);
		FreeLibrary( hDll);
		AddLog( _T( "Failed because memory error !\n" ));
		return FALSE;
	default:
		free( pIfTable);
		free( pAdapterTable);
		FreeLibrary( hDll);
		AddLog( _T( "Failed because unknown error !\n" ));
		return FALSE;
	}
/*
	// Dump IfTable
	AddLog("\n=== DEBUG : dump AdaptersInfo ===\n");
	for (dwIndex=0, pAdapterInfo=pAdapterTable ; pAdapterInfo != NULL ; pAdapterInfo = pAdapterInfo->Next, dwIndex++)
	{
		AddLog("+ Adaptater #%d\n", dwIndex);
		AddLog("\tAdapterName   = %s\n", pAdapterInfo->AdapterName); 
		AddLog("\tDescription   = %s\n", pAdapterInfo->Description); 
		AddLog("\tAddressLength = %d\n", pAdapterInfo->AddressLength); 
		AddLog("\tAddress       =");
		for (DWORD i=0 ; i<pAdapterInfo->AddressLength ; i++)
			AddLog(" %02x", pAdapterInfo->Address[i]);
		AddLog("\n");
		AddLog("\tIndex         = %d\n", pAdapterInfo->Index); 
		AddLog("\tType          = %d\n", pAdapterInfo->Type); 
		AddLog("\tDhcpEnabled   = %d\n", pAdapterInfo->DhcpEnabled); 
	
	}
	AddLog("=== DEBUG : fin===\n");

*/
	// Call GetIfEntry for each interface
	for (dwIndex = 0; dwIndex < pIfTable->dwNumEntries; dwIndex ++)
		if (!IsLoopback( pIfTable->table[dwIndex].dwType))
	{
		pIfEntry = &(pIfTable->table[dwIndex]);
		// Get the Index
		cAdapter.SetIfIndex( pIfEntry->dwIndex);
		// Get the type
		cAdapter.SetType( GetAdapterType( pIfEntry->dwType));
		// Get the MIB type
		cAdapter.SetTypeMIB( GetIfType( pIfEntry->dwType));
		// Get the description
		_tcsncpy( csAddress.GetBuffer( pIfEntry->dwDescrLen), (LPCTSTR) pIfEntry->bDescr,
										(size_t) pIfEntry->dwDescrLen);
		csAddress.ReleaseBuffer( (int) pIfEntry->dwDescrLen);
		cAdapter.SetDescription( csAddress);
		// Get MAC Address 
		csMAC.Format( _T("%02X:%02X:%02X:%02X:%02X:%02X"),
					   pIfEntry->bPhysAddr[0], pIfEntry->bPhysAddr[1],
					   pIfEntry->bPhysAddr[2], pIfEntry->bPhysAddr[3],
					   pIfEntry->bPhysAddr[4], pIfEntry->bPhysAddr[5]);
		cAdapter.SetMACAddress( csMAC);
		// Get the Speed
		cAdapter.SetSpeed( pIfEntry->dwSpeed);
		// Get the status
		cAdapter.SetIpHelperStatus( pIfEntry->dwOperStatus);
			
		// AddLog("+ IfTable => %s\n", csMAC);
		
		// Now parse the Adapter info
		for (pAdapterInfo=pAdapterTable ; pAdapterInfo != NULL ; pAdapterInfo = pAdapterInfo->Next)
			// Check the MAC Address (bad idea)
			//if (pIfEntry->dwPhysAddrLen==pAdapterInfo->AddressLength && !memcmp(pIfEntry->bPhysAddr, pAdapterInfo->Address, pAdapterInfo->AddressLength))
			// Check the AdapterIndex (better idea)
			if (pIfEntry->dwIndex == pAdapterInfo->Index)
			{
				// AddLog("++ AdapterTable : %02X:%02X:%02X:%02X:%02X:%02X\n", pAdapterInfo->Address[0], pAdapterInfo->Address[1], pAdapterInfo->Address[2], pAdapterInfo->Address[3], pAdapterInfo->Address[4], pAdapterInfo->Address[5]);
				// Get Gateways
				pAddressList = &(pAdapterInfo->GatewayList);
				csGateway = _T("");
				do
				{
					csGateway += pAddressList->IpAddress.String;
					pAddressList = pAddressList->Next;
					if( pAddressList != NULL )
						csGateway += _T( ",");
				}
				while( pAddressList != NULL );
				cAdapter.SetGateway(csGateway);

				// Get DHCP server
				pAddressList = &(pAdapterInfo->DhcpServer);
				csDhcpServer = _T("");
				do
				{
					csDhcpServer += pAddressList->IpAddress.String;
					pAddressList = pAddressList->Next;
					if( pAddressList != NULL )
						csDhcpServer += _T( ",");
				}
				while( pAddressList != NULL );
				cAdapter.SetDhcpServer(csDhcpServer);

				// Get IP addresses and NetMasks
				pAddressList = &(pAdapterInfo->IpAddressList);
				do
				{
					in_addr ipa;
					ULONG   ipAdr, ipMsk, nbRez;		

					cAdapter.SetIPAddress( pAddressList->IpAddress.String );
					cAdapter.SetIPNetMask( pAddressList->IpMask.String );

					// Update network number if possible		
					if( inet_addr( pAddressList->IpAddress.String ) != INADDR_NONE ){
						ipAdr = ntohl(inet_addr ( pAddressList->IpAddress.String ));
						ipMsk = ntohl(inet_addr ( pAddressList->IpMask.String ));
						nbRez = ipAdr & ipMsk ;
	
						ipa.S_un.S_addr = htonl(nbRez);
					
						cAdapter.SetNetNumber( inet_ntoa(ipa) );
					}

					// Create en Adapter entry for each Adapter/Address
					pList->AddTail( cAdapter);
					uIndex ++;

					pAddressList = pAddressList->Next;

				}
				while( pAddressList != NULL );

			// End If sameMac Address
			}
	// End For each interface
	}

	// Free memory
	free( pIfTable);
	free( pAdapterTable);

	// Unload library
	FreeLibrary( hDll);

	AddLog( _T( "OK\n"));
	if (uIndex > 0)
	{
		AddLog( _T( "IpHlpAPI GetNetworkAdapters: OK (%u objects).\n"), uIndex);
		return TRUE;
	}
	AddLog( _T( "IpHlpAPI GetNetworkAdapters: Failed because no network adapter object !\n"));
	return FALSE;
}