Christopher
Christopher

Reputation: 8982

How to get your own (local) IP-Address from an udp-socket (C/C++)

  1. You have multiple network adapters.
  2. Bind a UDP socket to an local port, without specifying an address.
  3. Receive packets on one of the adapters.

How do you get the local ip address of the adapter which received the packet?

The question is, "What is the ip address from the receiver adapter?" not the address from the sender which we get in the

receive_from( ..., &senderAddr, ... );

call.

Upvotes: 11

Views: 18841

Answers (7)

xyanping
xyanping

Reputation: 31

In Linux environment, you can use recvmsg to get local ip address.

    //create socket and bind to local address:INADDR_ANY:
    int s = socket(PF_INET,SOCK_DGRAM,0);
    bind(s,(struct sockaddr *)&myAddr,sizeof(myAddr)) ;
    // set option
    int onFlag=1;
    int ret = setsockopt(s,IPPROTO_IP,IP_PKTINFO,&onFlag,sizeof(onFlag));
    // prepare buffers
    // receive data buffer
    char dataBuf[1024] ;
    struct iovec iov = {
        .iov_base=dataBuf,
        .iov_len=sizeof(dataBuf)
    } ;
    // control buffer
    char cBuf[1024] ;
    // message
    struct msghdr  msg = {
        .msg_name=NULL, // to receive peer addr with struct sockaddr_in
        .msg_namelen=0, // sizeof(struct sockaddr_in)
        .msg_iov=&iov,
        .msg_iovlen=1,
        .msg_control=cBuf,
        .msg_controllen=sizeof(cBuf)
    } ;
    while(1) {
        // reset buffers
        msg.msg_iov[0].iov_base = dataBuf ;
        msg.msg_iov[0].iov_len = sizeof(dataBuf) ;
        msg.msg_control = cBuf ;
        msg.msg_controllen = sizeof(cBuf) ;
        // receive
        recvmsg(s,&msg,0);
        for( struct cmsghdr* pcmsg=CMSG_FIRSTHDR(&msg);
             pcmsg!=NULL; pcmsg=CMSG_NXTHDR(&msg,pcmsg) ) {
            if(pcmsg->cmsg_level==IPPROTO_IP && pcmsg->cmsg_type==IP_PKTINFO) {
                struct in_pktinfo * pktinfo=(struct in_pktinfo *)CMSG_DATA(pcmsg);
                printf("ifindex=%d ip=%s\n", pktinfo->ipi_ifindex, inet_ntoa(pktinfo->ipi_addr)) ;
            }
        }
    }

The following does not work in asymmetric routing environment.

you can first set SO_REUSEADDR to true

BOOL bOptVal = 1;
setsockopt(so, SOL_SOCKET, SO_REUSEADDR, (char *)&boOptVal, sizeof(bOptVal));

after receive_from( ..., &remoteAddr, ... ); create another socket, and connect back to remoteAddr. Then call getsockname can get the ip address.

SOCKET skNew = socket( )
// Same local address and port as that of your first socket 
// INADDR_ANY
bind(skNew, ,  )
// set SO_REUSEADDR to true again
setsockopt(skNew, SOL_SOCKET, SO_REUSEADDR, (char *)&boOptVal, sizeof(bOptVal));

// connect back  
connect(skNew, remoteAddr)

// get local address of the socket
getsocketname(skNew, )

Upvotes: 1

plugwash
plugwash

Reputation: 10494

Unfortunately the sendto and recvfrom API calls are fundamentally broken when used with sockets bound to "Any IP" because they have no field for local IP information.

So what can you do about it?

  1. You can guess (for example based on the routing table).
  2. You can get a list of local addresses and bind a seperate socket to each local address.
  3. You can use newer APIs that support this information. There are two parts to this, firstly you have to use the relavent socket option (ip_recvif for IPv4, ipv6_recvif for IPv6) to tell the stack you want this information. Then you have to use a different function (recvmsg on linux and several other unix-like systems, WSArecvmsg on windows) to receive the packet.

None of these options are great. Guessing will obviously produce wrong answers soemtimes. Binding seperate sockets increases the complexity of your software and causes problems if the list of local addresses changes will your program is running. The newer APIs are the correct techical soloution but may reduce portability (in particular it looks like WSArecvmsg is not available on windows XP) and may require modifications to the socket wrapper library you are using.

Edit looks like I was wrong, it seems the MS documentation is misleading and that WSArecvmsg is available on windows XP. See https://stackoverflow.com/a/37334943/5083516

Upvotes: 1

Chris Evans
Chris Evans

Reputation: 1

Try this:

gethostbyname("localhost");

Upvotes: -4

Andrew Edgecombe
Andrew Edgecombe

Reputation: 40362

The solution provided by timbo assumes that the address ranges are unique and not overlapping. While this is usually the case, it isn't a generic solution.

There is an excellent implementation of a function that does exactly what you're after provided in the Steven's book "Unix network programming" (section 20.2) This is a function based on recvmsg(), rather than recvfrom(). If your socket has the IP_RECVIF option enabled then recvmsg() will return the index of the interface on which the packet was received. This can then be used to look up the destination address.

The source code is available here. The function in question is 'recvfrom_flags()'

Upvotes: 5

Rob Wells
Rob Wells

Reputation: 37103

G'day,

I assume that you've done your bind using INADDR_ANY to specify the address.

If this is the case, then the semantics of INADDR_ANY is such that a UDP socket is created on the port specified on all of your interfaces. The socket is going to get all packets sent to all interfaces on the port specified.

When sending using this socket, the lowest numbered interface is used. The outgoing sender's address field is set to the IP address of that first outgoing interface used.

First outgoing interface is defined as the sequence when you do an ifconfig -a. It will probably be eth0.

HTH.

cheers, Rob

Upvotes: 3

diciu
diciu

Reputation: 29333

ssize_t
     recvfrom(int socket, void *restrict buffer, size_t length, int flags,
         struct sockaddr *restrict address, socklen_t *restrict address_len);

     ssize_t
     recvmsg(int socket, struct msghdr *message, int flags);

[..]
     If address is not a null pointer and the socket is not connection-oriented, the
     source address of the message is filled in.

Actual code:

int nbytes = recvfrom(sock, buf, MAXBUFSIZE, MSG_WAITALL, (struct sockaddr *)&bindaddr, &addrlen);

fprintf(stdout, "Read %d bytes on local address %s\n", nbytes, inet_ntoa(bindaddr.sin_addr.s_addr));

hope this helps.

Upvotes: -3

Timbo
Timbo

Reputation: 28050

You could enumerate all the network adapters, get their IP addresses and compare the part covered by the subnet mask with the sender's address.

Like:

IPAddress FindLocalIPAddressOfIncomingPacket( senderAddr )
{
    foreach( adapter in EnumAllNetworkAdapters() )
    {
        adapterSubnet = adapter.subnetmask & adapter.ipaddress;
        senderSubnet = adapter.subnetmask & senderAddr;
        if( adapterSubnet == senderSubnet )
        {
            return adapter.ipaddress;
        }
    }
}

Upvotes: 4

Related Questions