How to get interface index from ip addres interface

Can someone tell me how to get the interface index from the interface IP address? For example, if the IP address of the interface is 192.168.23.25, then what is the interface index.

I want to add that I need to use it in one code written in c, so if any function with some option can give me an index index of the interface based on the IP address of the interface.

+3
source share
5 answers

You cannot do this, you need to look at all the interfaces, and then iterate over all the IP addresses until you find the one you need. I think this code does what you want.

#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
    in_addr_t ia;
    int id;

    ia = inet_addr(argv[1]);

    id = do_lookup(ia);
}

int do_lookup(in_addr_t ia) {
    char          buf[1024];
    struct ifconf ifc;
    struct ifreq *ifr;
    int           sck;
    int           nInterfaces;
    int           i;

/* Get a socket handle. */
    sck = socket(AF_INET, SOCK_DGRAM, 0);
    if(sck < 0)
    {
        perror("socket");
        return -1;
    }

/* Query available interfaces. */
    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = buf;
    if(ioctl(sck, SIOCGIFCONF, &ifc) < 0)
    {
        perror("ioctl(SIOCGIFCONF)");
        return -1;
    }

/* Iterate through the list of interfaces. */
    ifr         = ifc.ifc_req;
    nInterfaces = ifc.ifc_len / sizeof(struct ifreq);
    for(i = 0; i < nInterfaces; i++)
    {
        struct ifreq *item = &ifr[i];
        if(((struct sockaddr_in *)&item->ifr_addr)->sin_addr.s_addr == ia) {
            return i;
        }
    }

    return -1;
}
+2
source

getifaddrs(). MarkR .

- :

ip addr add 192.168.25.23/24 dev eth0

man - :

lo  address family: 17 (AF_PACKET)
eth0  address family: 17 (AF_PACKET)
lo  address family: 2 (AF_INET)
        address: <127.0.0.1>
eth0  address family: 2 (AF_INET)
        address: <192.168.1.105>
eth0  address family: 2 (AF_INET)
        address: <192.168.25.23>
lo  address family: 10 (AF_INET6)
        address: <::1>
eth0  address family: 10 (AF_INET6)
        address: <fe84::82d6:baaf:fe14:4c22%eth0>

, if_nametoindex(), if_indextoname() if_nameindex(). , .

+2

:. Linux.

    #include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <arpa/inet.h>

int main(void)
{
    char          buf[1024];
    struct ifconf ifc;
    struct ifreq *ifr;
    int           sck;
    int           nInterfaces;
    int           i;

/* Get a socket handle. */
    sck = socket(AF_INET, SOCK_DGRAM, 0);
    if(sck < 0)
    {
        perror("socket");
        return 1;
    }

/* Query available interfaces. */
    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = buf;
    if(ioctl(sck, SIOCGIFCONF, &ifc) < 0)
    {
        perror("ioctl(SIOCGIFCONF)");
        return 1;
    }

/* Iterate through the list of interfaces. */
    ifr         = ifc.ifc_req;
    nInterfaces = ifc.ifc_len / sizeof(struct ifreq);
    for(i = 0; i < nInterfaces; i++)
    {
        struct ifreq *item = &ifr[i];

    /* Show the device name and IP address */
        printf("%s: IP %s",
               item->ifr_name,
               inet_ntoa(((struct sockaddr_in *)&item->ifr_addr)->sin_addr));

    /* Get the MAC address */
        if(ioctl(sck, SIOCGIFHWADDR, item) < 0)
        {
            perror("ioctl(SIOCGIFHWADDR)");
            return 1;
        }

    /* Get the broadcast address (added by Eric) */
        if(ioctl(sck, SIOCGIFBRDADDR, item) >= 0)
            printf(", BROADCAST %s", inet_ntoa(((struct sockaddr_in *)&item->ifr_broadaddr)->sin_addr));
        printf("\n");
    }

        return 0;
}

.

0

SIOCGIFCONF IP-,

ip addr add 192.168.25.23/24 dev eth1

, , "ip addr sh" - , netlink ( )

0

if_nametoindex(). Ubuntu 12.04 ( 3.11.0-15-generic).

,

#include <net/if.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char *argv[])
{
    for (int ix=1; ix<argc; ix++)
    {
        unsigned int rc = if_nametoindex(argv[ix]);
        if (rc) {
            printf("interface [%s] has index : %d\n", argv[ix], rc);
        }
        else {
            perror("if_nametoindex");
        }
    }
}

:

$ ./if_index eth0
interface [eth0] has index : 2

, - /proc/net/if _inet6. - .

$ cat /proc/net/if_inet6 
00000000000000000000000000000001 01 80 10 80       lo
fe800000000000000a0027fffe1a2a32 03 40 20 80     eth1
fe800000000000000a0027fffe08b9ca 02 40 20 80     eth0
0

Source: https://habr.com/ru/post/1735781/


All Articles