获取linux内核所有ip
admin
2023-02-18 17:20:08
0

获取linux内核所有ip(C语言)

经常遇到获取接口ip。记录一下,方便后续使用。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
typedef unsigned int    sf_uint32_t;
typedef unsigned int    in_addr_t;
static inline char* print_ip1(sf_uint32_t ip)
{
    unsigned char* ip_tmp = (unsigned char*) &ip;
    static char buff[20];
    memset(buff ,0 ,sizeof(buff));
    snprintf(buff, sizeof(buff), "%d.%d.%d.%d", ip_tmp[0], ip_tmp[1], ip_tmp[2],
             ip_tmp[3]);
    return buff;
}

in_addr_t
get_myaddr(void)
{
    int             sd, i, lastlen = 0;
    struct ifconf   ifc;
    struct ifreq   *ifrp = NULL;
    in_addr_t       addr;
    char           *buf = NULL;

    if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        return 0;
    }

    /*
     * Cope with lots of interfaces and brokenness of ioctl SIOCGIFCONF on
     * some platforms; see W. R. Stevens, ``Unix Network Programming Volume
     * I'', p.435.  
     */

    for (i = 8;; i += 8) {
        buf = (char *) calloc(i, sizeof(struct ifreq));
        if (buf == NULL) {
            close(sd);
            return 0;
        }
        ifc.ifc_len = i * sizeof(struct ifreq);
        ifc.ifc_buf = (caddr_t) buf;

        if (ioctl(sd, SIOCGIFCONF, (char *) &ifc) < 0) {
            if (errno != EINVAL || lastlen != 0) {
                /*
                 * Something has gone genuinely wrong.  
                 */
                free(buf);
                close(sd);
                return 0;
            }
            /*
             * Otherwise, it could just be that the buffer is too small.  
             */
        } else {
            if (ifc.ifc_len == lastlen) {
                /*
                 * The length is the same as the last time; we're done.  
                 */
                break;
            }
            lastlen = ifc.ifc_len;
        }
        free(buf);
    }

    for (ifrp = ifc.ifc_req;
        (char *)ifrp < (char *)ifc.ifc_req + ifc.ifc_len;
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
        ifrp = (struct ifreq *)(((char *) ifrp) +
                                sizeof(ifrp->ifr_name) +
                                ifrp->ifr_addr.sa_len)
#else
        ifrp++
#endif
        ) {
        if (ifrp->ifr_addr.sa_family != AF_INET) {
            continue;
        }
        addr = ((struct sockaddr_in *) &(ifrp->ifr_addr))->sin_addr.s_addr;

        if (ioctl(sd, SIOCGIFFLAGS, (char *) ifrp) < 0) {
            continue;
        }
        if ((ifrp->ifr_flags & IFF_UP)
#ifdef IFF_RUNNING
            && (ifrp->ifr_flags & IFF_RUNNING)
#endif                          /* IFF_RUNNING */
            /*&& !(ifrp->ifr_flags & IFF_LOOPBACK)
            && addr != LOOPBACK*/) {
            /*
             * I *really* don't understand why this is necessary.  Perhaps for
             * some broken platform?  Leave it for now.  JBPN  
             */
#ifdef SYS_IOCTL_H_HAS_SIOCGIFADDR
            if (ioctl(sd, SIOCGIFADDR, (char *) ifrp) < 0) {
                continue;
            }
            addr =
                ((struct sockaddr_in *) &(ifrp->ifr_addr))->sin_addr.
                s_addr;
#endif
            printf("current ip : %s\n",print_ip1(addr));
        }
    }
    free(buf);
    close(sd);
    return 0;
}

int main()
{
    get_myaddr();
}

相关内容

热门资讯

【今日要闻】“葫芦三娃有没有挂... 您好:葫芦三娃这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游戏...
今日重大通报“家家乐牌吧开挂神... 您好:家家乐牌吧这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款游...
最新引进“闽乐乐究竟有挂吗?”... 最新引进“闽乐乐究竟有挂吗?”(详细开挂教程)您好,闽乐乐这个游戏其实有挂的,确实是有挂的,需要了解...
终于懂了“星悦麻将有没有挂?”... 家人们!今天小编来为大家解答星悦麻将透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买很...
我来教教您“新版悟空到底是不是... 家人们!今天小编来为大家解答新版悟空透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买很...
最新引进“新大圣炸/金/花怎么... 家人们!今天小编来为大家解答新大圣炸/金/花透视挂怎么安装这个问题咨询软件客服徽9752949的挂在...
终于懂了“丫丫古诗怎么装挂?”... 家人们!今天小编来为大家解答丫丫古诗透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里买很...
【今日要闻】“网易棋牌有没有挂... 网上科普关于“网易棋牌有没有挂”话题很是火热,小编也是针对网易棋牌作*弊开挂的方法以及开挂对应的知识...
【第一资讯】“樱花之盛炸/金/... 家人们!今天小编来为大家解答樱花之盛炸/金/花透视挂怎么安装这个问题咨询软件客服徽9784099的挂...
重磅消息“小闲54棋牌是不是有... 有 亲,根据资深记者爆料小闲54棋牌是可以开挂的,确实有挂(咨询软件无需...