开发者

get LAN ip and print it

Does anyone know how do I get my LAN IP and print it on the screen. *I don't mean on shell, but in c programming. **I will apprec开发者_StackOverflow中文版iate if you'll post me a sample code.


There's a few approaches; first, you could set up a connection to a known peer using connect(2) and then read the local socket 'name' with getsockname(2). This is a pretty poor mechanism, but it is easy.

But, getsockname(2) will only report one IP address, when a machine may have thousands of IP addresses, and which IP is returned will depend in part upon the peer that you chose! So, not very reliable.

A much better answer is to use rtnetlink(7) to read the IP addresses for the machine directly from the kernel: you would send RTM_GETADDR messages to the kernel for each interface on the machine and read the answers back. Your best bet for understanding how to use this is probably to read the source code for the ip program.

Another option is to use the SIOCGIFCONF ioctl(2) on an IP socket. This interface isn't as flexible as the rtnetlink(7) interface, so it might not always be correct, but it'll be a mid-way point. The ifconfig(8) utility uses this approach for displaying ifconfig -a output. Again, your best bet would be to read the source. (There is some slight documentation in ioctl_list(2).)


The getifaddrs() function from <ifaddrs.h> is the simplest way to get the current interfaces and corresponding addresses:

#include <stdio.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    struct ifaddrs *iflist, *iface;

    if (getifaddrs(&iflist) < 0) {
        perror("getifaddrs");
        return 1;
    }

    for (iface = iflist; iface; iface = iface->ifa_next) {
        int af = iface->ifa_addr->sa_family;
        const void *addr;
        char addrp[INET6_ADDRSTRLEN];

        switch (af) {
            case AF_INET:
                addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
                break;
            case AF_INET6:
                addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
                break;
            default:
                addr = NULL;
        }

        if (addr) {
            if (inet_ntop(af, addr, addrp, sizeof addrp) == NULL) {
                perror("inet_ntop");
                continue;
            }

            printf("Interface %s has address %s\n", iface->ifa_name, addrp);
        }
    }

    freeifaddrs(iflist);
    return 0;
}


gethostbyname should help you to do this. Example:

GetLocalAddress()
{
  char ac[80];

  // Get my host name
  if (gethostname(ac, sizeof(ac)) != -1)
  {
    printf("Host name: %s\n", ac);

    // Find IP addresses
    struct hostent* p_he = gethostbyname(ac);

    if (p_he != 0)
    {
        for (int i=0; p_he->h_addr_list[i] != 0; ++i)
        {
            struct in_addr addr;
            memcpy(&addr, p_he->h_addr_list[i], sizeof(struct in_addr));
            printf("IP address %d: %s\n", i, inet_ntoa(addr));
        }
    }
}

You might want to filter to remove 127.0.0.1 from the list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜