Dual hosting Ethernet+WiFi, get IP address(es) programatically

bill chadwick 136 Reputation points
2020-11-25T15:50:00.227+00:00

Using azsphere device wifi show-status I can easily determine the WiFi IP address of the device. Can I do the same for the ethernet interface from the command line? This is useful to do if the address is allocated by an external DHCP e.g. on a router.

I have modified EchoServer_Start in the PrivateNetworkServices sample as follows

    struct sockaddr_in bound;
    size_t szBound = sizeof(bound);
    getsockname(serverState->listenFd, &bound, &szBound);

    Log_Debug("INFO: TCP server: Listening for client connection (fd %d) on %s\n",
              serverState->listenFd, inet_ntoa(bound.sin_addr));

and it duly prints out an expected IP address.

However when the device is multi-homed with both eth0 and wlan0 I do not seem to be able to chose which interface to run the TCP server on.

The output from the app with static const char NetworkInterface[] = "eth0"; is as below (192.168.178.70 is the device's Ethernet IP address).

INFO: Networking_GetInterfaceCount: count=2
INFO: Networking_GetInterfaces: actualCount=2
INFO: interface #0
INFO: interfaceName="eth0"
INFO: isEnabled="1"
INFO: ipConfigurationType=1 (DhcpClient)
INFO: interfaceMediumType=2 (Ethernet)
INFO: interfaceStatus=0x0f
INFO: interface #1
INFO: interfaceName="wlan0"
INFO: isEnabled="1"
INFO: ipConfigurationType=1 (DhcpClient)
INFO: interfaceMediumType=1 (Wi-Fi)
INFO: interfaceStatus=0x0f
INFO: SNTP server has started on network interface: eth0.
INFO: TCP server: Listening for client connection (fd 8) on 192.168.178.70

The output from the app with static const char NetworkInterface[] = "wlan0"; is as below

INFO: Networking_GetInterfaceCount: count=2
INFO: Networking_GetInterfaces: actualCount=2
INFO: interface #0
INFO: interfaceName="eth0"
INFO: isEnabled="1"
INFO: ipConfigurationType=1 (DhcpClient)
INFO: interfaceMediumType=2 (Ethernet)
INFO: interfaceStatus=0x0f
INFO: interface #1
INFO: interfaceName="wlan0"
INFO: isEnabled="1"
INFO: ipConfigurationType=1 (DhcpClient)
INFO: interfaceMediumType=1 (Wi-Fi)
INFO: interfaceStatus=0x0f
INFO: SNTP server has started on network interface: wlan0.
INFO: TCP server: Listening for client connection (fd 8) on 192.168.178.70

I have tried adding to OpenIpV4Socket after the existing setsockopt, as below

        char *devname = "wlan0";
        r = setsockopt(localFd, SOL_SOCKET, SO_BINDTODEVICE, devname, strlen(devname));
        if (r != 0) {
            Log_Debug("INFO: TCP server: setsockopt failed with %d\n", errno);
        }

but this always fails. It seems odd that Networking_SntpServer_Start can accept an interface but we do not seem to have a way for specifying an interface for a TCP server (or generic socket).

Is there a reason for this restriction or have I done something wrong or missed something?

Thanks.

Azure Sphere
Azure Sphere
An Azure internet of things security solution including hardware, operating system, and cloud components.
168 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AshokPeddakotla-MSFT 34,701 Reputation points
    2020-12-04T16:32:31.767+00:00

    @bill chadwick Below is the response received from our product group on your queries.

    Q: Why is there no way to determine the Ethernet IP address from the command line tool?

    A: This is a good design change request, thank you for the feedback.

    Update:
    Q: It seems odd that Networking_SntpServer_Start can accept an interface but we do not seem to have a way for specifying an interface for a TCP server (or generic socket). Is there a reason for this restriction or have I done something wrong or missed something? ... Why does setsockopt to specify an interface not work? (especially when the NTP server can be started on a nominated interface)

    A: You cannot use SO_BINDTODEVICE due to security issues with allowing CAP_NET_ADMIN or CAP_NET_RAW. Instead you can query for the IP via getifadddr and bind to this IP on the interface of choice as show in https://github.com/Azure/azure-sphere-samples/blob/b26a88e8ee2fba679f4d28a3184b4d0c95ad5547/Samples/PrivateNetworkServices/echo_tcp_server.c

    Q: Can you please also suggest a way that the IP address of both interfaces might easily be determined programmatically

    A: getifaddrs()

    Please let us know if you have any further queries.

    If an answer is helpful, please "Accept answer" or "Up-Vote" for the same which might be beneficial to other community members reading this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.