获取linux内核所有ip
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,获取linux内核所有ip(C语言)经常遇到获取接口ip。记录一下,方便后续使用。#include #include #include #include #include #include #incl
千家信息网最后更新 2026年10月31日获取linux内核所有ip
获取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_tget_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();}
内核
接口
语言
R.
W.
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网与科技
科技互联网龙头
圆梦互联网科技
互联网科技集团
互联网巨头投资的硬核科技
须眉科技是互联网公司吗
中国的互联网科技企业的崛起
互联网企业和科技型企业
黑科技闪耀互联网之光
崇明区互联网教育科技售后服务
宜昌元轩互联网科技有限公司
洛阳昕诺飞互联网科技有限公司
互联网科技创新的路径
特约风豹互联网科技为什么扣费
伍佰万互联网科技怎么赚钱
广州梦谷互联网科技有限公司领导
湖北辰鑫科技互联网
广州智享互联网科技
互联网科技收购价值
美国五大互联网科技公司市值多少
联恩商钥互联网科技有限公司
云创客互联网科技公司
互联网教育科技公司vipkid
明优互联网科技有限公司
华中科技大学互联网的牛人
科技互联网海报
浙南科技城互联网大厦招聘
众联互联网络科技
盐城市攀科互联网科技有限公司
数字科技和互联网金融哪个好