【问题标题】:C code to get the IP address获取IP地址的C代码
【发布时间】:2009-10-15 05:52:12
【问题描述】:

如何使用C代码获取本地机器的IP地址?

如果有多个接口,那么我应该能够显示每个接口的 IP 地址。

注意:请勿在 C 代码中使用 ifconfig 等任何命令来检索 IP 地址。

【问题讨论】:

  • 家庭作业?到目前为止你有什么?
  • 您必须注意,在生产代码中使用ifconfig 并不是最糟糕的解决方案。
  • 别担心,这不是我的作业难题...。我越来越多地参与一些严肃的 C 语言编程,因此试图修复我的应用程序中的一些缺失链接...。

标签: c network-programming


【解决方案1】:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>

int main()
{
    int fd;
    struct ifreq ifr;

    fd = socket(AF_INET, SOCK_DGRAM, 0);

    ifr.ifr_addr.sa_family = AF_INET;

    snprintf(ifr.ifr_name, IFNAMSIZ, "eth0");

    ioctl(fd, SIOCGIFADDR, &ifr);

    /* and more importantly */
    printf("%s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));

    close(fd);
}

如果您想枚举所有接口,请查看getifaddrs() 函数 - 如果您使用的是 Linux。

【讨论】:

    【解决方案2】:

    通过Michael Foukarakis 的输入,我可以显示同一台机器上各种接口的 IP 地址:

    #include <arpa/inet.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <ifaddrs.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int
    main(int argc, char *argv[])
    {
        struct ifaddrs *ifaddr, *ifa;
        int family, s;
        char host[NI_MAXHOST];
    
        if (getifaddrs(&ifaddr) == -1) {
            perror("getifaddrs");
            exit(EXIT_FAILURE);
        }
    
        for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
            family = ifa->ifa_addr->sa_family;
    
            if (family == AF_INET) {
                s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
                                               host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
                if (s != 0) {
                    printf("getnameinfo() failed: %s\n", gai_strerror(s));
                    exit(EXIT_FAILURE);
                }
                printf("<Interface>: %s \t <Address> %s\n", ifa->ifa_name, host);
            }
        }
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      从“/proc/net/dev”获取所有接口。注意:仅使用 ioctl 无法获取所有接口。

      #define PROC_NETDEV "/proc/net/dev"
      
      fp = fopen(PROC_NETDEV, "r");
      while (NULL != fgets(buf, sizeof buf, fp)) {
          s = strchr(buf, ':');
          *s = '\0';
          s = buf;
      
          // Filter all space ' ' here
          got one interface name here, continue for others
      }
      fclose(fp);
      

      然后使用ioctl()获取地址:

      struct ifreq ifr;
      struct ifreq ifr_copy;
      struct sockaddr_in *sin;
      
      for each interface name {
          strncpy(ifr.ifr_name, ifi->name, sizeof(ifr.ifr_name) - 1);
          ifr_copy = ifr;
          ioctl(fd, SIOCGIFFLAGS, &ifr_copy);
          ifi->flags = ifr_copy.ifr_flags;
          ioctl(fd, SIOCGIFADDR, &ifr_copy);
          sin = (struct sockaddr_in*)&ifr_copy.ifr_addr;
          ifi->addr = allocating address memory here
          bzero(ifi->addr, sizeof *ifi->addr);
          *(struct sockaddr_in*)ifi->addr = *sin;
      
          /* Here also you could get netmask and hwaddr. */
      }
      

      【讨论】:

      • 查看其他帖子了解如何打开“fd”
      猜你喜欢
      • 2011-04-03
      • 2018-08-05
      • 2014-01-10
      • 2021-11-20
      • 2013-07-02
      • 2011-08-29
      相关资源
      最近更新 更多