Linux C get local IP

Usually it can be used under Linux when querying IPifconfig查看对应网卡信息:

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.99  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::976c:f683:736c:c13a  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:9a:e8:6a  txqueuelen 1000  (Ethernet)
        RX packets 53  bytes 6171 (6.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 52  bytes 7183 (7.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.113  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::b986:5019:f3b0:31c6  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:30:cb:51  txqueuelen 1000  (Ethernet)
        RX packets 2908  bytes 204037 (199.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58  bytes 5325 (5.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Two ways for C code to obtain the local IP:

1. Interface API   This interface can get a result similar to a linked listgetifaddrs 

struct ifaddrs {
        struct ifaddrs  *ifa_next;    /* Next item in list */
        char            *ifa_name;    /* Name of interface */
        unsigned int     ifa_flags;   /* Flags from SIOCGIFFLAGS */
        struct sockaddr *ifa_addr;    /* Address of interface */
        struct sockaddr *ifa_netmask; /* Netmask of interface */
        union {
           struct sockaddr *ifu_broadaddr;
                            /* Broadcast address of interface */
           struct sockaddr *ifu_dstaddr;
                            /* Point-to-point destination address */
        } ifa_ifu;
        #define              ifa_broadaddr ifa_ifu.ifu_broadaddr
        #define              ifa_dstaddr   ifa_ifu.ifu_dstaddr
        void            *ifa_data;    /* Address-specific data */
        };
ifa_name: enp0s3这样的接口名
ifa_addr: 对应的地址参数
ifa_next: 链表的下一个

Code:

#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) {
       if (ifa->ifa_addr == NULL)
           continue;

        family = ifa->ifa_addr->sa_family;

        if (family == AF_INET)
        {
            printf("interfac: %s, ip: %s\n", ifa->ifa_name, inet_ntoa(((struct sockaddr_in*)ifa->ifa_addr)->sin_addr));
        }
   }

   freeifaddrs(ifaddr);
   exit(EXIT_SUCCESS);
}

operation result:

interfac: lo, ip: 127.0.0.1
interfac: enp0s3, ip: 192.168.56.99
interfac: enp0s8, ip: 192.168.1.113

Ififaddrs this method uses pppoe dial-up Internet access, the IP cannot be obtained, and the structure of the corresponding name can be obtained , but the ifa_addr in the structure is NULL  .

2. Linux original method (ioctl)

 Directly on the code:

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h> 
#include <unistd.h> 
#include <netdb.h>  
#include <net/if.h>  
#include <arpa/inet.h> 
#include <sys/ioctl.h>  
#include <sys/types.h>  
#include <sys/time.h> 
// 获取本机ip  eth_inf网卡名称 调用方法get_local_ip("apcli0", ip);
int get_local_ip(const char *eth_inf)
{
    int sd;
    struct sockaddr_in sin;
    struct ifreq ifr;
 
    sd = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == sd)
    {
        printf("socket error: %s\n", strerror(errno));
        return -1;
    }
 
    strncpy(ifr.ifr_name, eth_inf, IFNAMSIZ);
    ifr.ifr_name[IFNAMSIZ - 1] = 0;
 
    // if error: No such device  
    if (ioctl(sd, SIOCGIFADDR, &ifr) < 0)
    {
        printf("ioctl error: %s\n", strerror(errno));
        close(sd);
        return -1;
    }
    
    printf("interfac: %s, ip: %s\n", eth_inf, inet_ntoa(((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr)); 

    close(sd);
    return 0;
}

int main(int argc, const char *argv[])
{
    get_local_ip(argv[1]);
    return 0;
}

Results of the:

./a.out enp0s3
interfac: enp0s3, ip: 192.168.56.99

This method does not have the problem of the previous one.

Reprinted: https://www.jianshu.com/p/cbb3e21ef685

Guess you like

Origin blog.csdn.net/weixin_38293850/article/details/108512102