GetHostByName( GetHostName() )

GetHostByName( GetHostName() )

Postby byron.hopp » Wed Mar 06, 2024 7:35 pm

I use WSAStartup() ; cIp := GetHostByName( GetHostName() ) ; WSACleanup(), to get the local IP address. However my system has an EtherNet, Hamachi, and 2 VMware showing as adapters. The address returned by the function GetHostByName returns a different one that the one that has the same subnet as the network my system is on. Is this a bad configuration on my workstation? How do I get it to return the IP on the same subnet as my system?
Thanks,
Byron Hopp
Matrix Computer Services
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Re: GetHostByName( GetHostName() )

Postby Antonio Linares » Wed Mar 06, 2024 9:44 pm

Dear Byron,

You may ask chatgpt and Google Gemini about it and both offer good advice:
chatgpt:
Enumerate Network Interfaces:
Use the GetAdaptersInfo or GetAdaptersAddresses function to enumerate all network interfaces on your system. These functions provide information about each network adapter, including IP addresses.

Filter by Subnet:
Once you have the information about each network adapter, filter the results to get the one that belongs to the subnet you are interested in. You can compare the subnet of each adapter's IP address with the subnet of your system.

Code: Select all  Expand view
#include <WinSock2.h>
#include <IPHlpApi.h>

#pragma comment(lib, "IPHLPAPI.lib")

int main() {
    // Initialize Winsock
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        // Handle error
        return 1;
    }

    // Buffer for adapter information
    ULONG outBufLen = 15000;
    PIP_ADAPTER_ADDRESSES pAddresses = (IP_ADAPTER_ADDRESSES*)malloc(outBufLen);

    // Get adapter information
    if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &outBufLen) != ERROR_SUCCESS) {
        // Handle error
        free(pAddresses);
        WSACleanup();
        return 1;
    }

    // Iterate through the list of adapters
    for (PIP_ADAPTER_ADDRESSES pCurrAddresses = pAddresses; pCurrAddresses != NULL; pCurrAddresses = pCurrAddresses->Next) {
        // Check if the adapter is active and not a loopback adapter
        if ((pCurrAddresses->OperStatus == IfOperStatusUp) && (pCurrAddresses->IfType != IF_TYPE_SOFTWARE_LOOPBACK)) {
            // Filter by subnet or any other criteria you need
            // For example, check if the adapter's IP address is on a specific subnet
            // ...

            // Print the IP address
            printf("Local IP Address: %s\n", pCurrAddresses->FirstUnicastAddress->Address.lpSockaddr->sa_data);
        }
    }

    // Clean up
    free(pAddresses);
    WSACleanup();

    return 0;
}
 

Google Gemini:
The behavior you're experiencing with GetHostByName returning an IP address different from the one on your desired subnet is not necessarily a bad configuration. Here's why:

Function Behavior: GetHostByName typically returns the primary IP address associated with the hostname. This might not always be the IP address you're expecting, especially when your system has multiple network adapters.

Multiple Adapters: With Ethernet, Hamachi, and VMware adapters present, your system can have multiple IP addresses assigned, each potentially belonging to different subnets. GetHostByName might pick the address associated with a different adapter than the one you intend to use.

Here are some approaches to get the local IP address on the desired subnet:

1. Enumerate Adapters:

Use WSAEnumNetworkAdapters to enumerate all network adapters on your system.
Loop through each adapter and check its network mask and IP address using GetIpAddrTable.
Look for an adapter where the network mask matches the subnet mask of your desired network (usually obtained through other means).
The IP address associated with this adapter will be the local IP on the desired subnet.

This also seems interesting and may help:
Consider using platform-specific APIs like ifconfig (Linux/macOS) or ipconfig (Windows) through system calls. These tools often provide more granular control over retrieving information specific to a particular adapter.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 46 guests