linux获取本地ip

code

#ifdef __linux__
#include <stdio.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <vector>

int32_t get_local_ip(std::vector<std::string>& vIP)
{
    int sockfd;
    struct ifconf ifc;
    char buf[1024] = {0};
    char ipbuf[20] = {0};
    struct ifreq *ifr;

    ifc.ifc_len = 1024;
    ifc.ifc_buf = buf;

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        return -1;
    }

    ioctl(sockfd, SIOCGIFCONF, &ifc);
    ifr = (struct ifreq*)buf;

    char ip_buf[20] = {0};
    for (int i = (ifc.ifc_len / sizeof(struct ifreq)); i > 0; i--) 
    {
        (void)inet_ntop(AF_INET, &((struct sockaddr_in*)&ifr->ifr_addr)->sin_addr, ip_buf, 20);

        fprintf(stdout, "ifname:%s,ip:%s\n", ifr->ifr_name, ip_buf);

        vIP.push_back(ip_buf);

        ifr = ifr + 1;
    }

    close(sockfd);

    return 0;
}
int32_t get_local_ip(const char* ifname, std::string& ip) {
    int sockfd;
    struct ifconf ifc;
    char buf[1024] = {0};
    char ipbuf[20] = {0};
    struct ifreq *ifr;

    ifc.ifc_len = 1024;
    ifc.ifc_buf = buf;

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        return -1;
    }

    ioctl(sockfd, SIOCGIFCONF, &ifc);
    ifr = (struct ifreq*)buf;

    char ip_buf[20] = {0};
    for (int i = (ifc.ifc_len / sizeof(struct ifreq)); i > 0; i--) 
    {
        (void)inet_ntop(AF_INET, &((struct sockaddr_in*)&ifr->ifr_addr)->sin_addr, ip_buf, 20);

        // fprintf(stdout, "ifname:%s,ip:%s\n", ifr->ifr_name, ip_buf);

        if (strcmp(ifr->ifr_name, ifname) == 0) {
            ip = ip_buf;
            close(sockfd);
            return 0;
        }

        ifr = ifr + 1;
    }

    close(sockfd);

    return -1;
}
void get_local_ip_test(void) {
    std::vector<std::string> vIPs;
    (void)get_local_ip(vIPs);

    std::string res;
    (void)get_local_ip("eth0", res);
    // fprintf(stdout, "res:%s\n", res.c_str());
}

#endif

performance

相关推荐

  1. linuxIP及虚IP(附加IP获取打印

    2023-12-23 14:52:04       28 阅读
  2. Linux命令-dhclient命令(动态获取或释放IP地址)

    2023-12-23 14:52:04       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-23 14:52:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-23 14:52:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-23 14:52:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-23 14:52:04       18 阅读

热门阅读

  1. 使用xuggle_5.4 实现视频加水印

    2023-12-23 14:52:04       38 阅读
  2. mockito-study-api

    2023-12-23 14:52:04       36 阅读
  3. Mac[M1]安装mongodb

    2023-12-23 14:52:04       34 阅读
  4. 掌握 Rust 中的建造者模式

    2023-12-23 14:52:04       46 阅读
  5. Harmonyos系统使用http访问网络和应用数据管理

    2023-12-23 14:52:04       31 阅读
  6. 数据处理演进:EtLT崛起,ELT正在告别历史舞台

    2023-12-23 14:52:04       34 阅读
  7. Golang leetcode59 螺旋矩阵

    2023-12-23 14:52:04       34 阅读
  8. 基于汉宁窗FIR滤波器实现语音信号加噪去噪

    2023-12-23 14:52:04       41 阅读
  9. MySQL数据库

    2023-12-23 14:52:04       32 阅读
  10. 理解计算机中的中断与中断处理

    2023-12-23 14:52:04       45 阅读
  11. log4j rename方法

    2023-12-23 14:52:04       35 阅读
  12. python使用selenium无法获取frame完整内容的问题

    2023-12-23 14:52:04       45 阅读