Linux C 获取本机所有网卡的 IP,Mask

0 运行环境

  • 本机系统:Windows 10

  • 虚拟机软件:Oracle VM VirtualBox 6

  • 虚拟机系统:Ubuntu 18

1 代码

#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>

int get_localIPAndMask(char *ip,char *mask)
{

        int fd, intrface, retn = 0;

        struct ifreq buf[INET_ADDRSTRLEN];

        struct ifconf ifc;

        if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)

        {

                ifc.ifc_len = sizeof(buf);

                // caddr_t,Linux内核源码里定义的:typedef void *caddr_t;

                ifc.ifc_buf = (caddr_t)buf;

                if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc))

                {
                        intrface = ifc.ifc_len/sizeof(struct ifreq);

                        while (intrface-- > 0)

                        {
                                if (!(ioctl(fd, SIOCGIFADDR, (char *)&buf[intrface])))
                                {

                                        ip = (inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));

                                        printf("IP:%s\n", ip);
                                }

                                 if (!(ioctl(fd, SIOCGIFNETMASK, (char *)&buf[intrface])))
                                {
                                        mask = (inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));

                                        printf("mask:%s\n", mask);
                                }

                        }

                }

        close(fd);

        return 0;

        }

}

int main()

{
        char ip[64];

        memset(ip, 0, sizeof(ip));

        char mask[64];

        memset(mask, 0, sizeof(mask));

        get_localIPAndMask(ip,mask);

        return 0;
}

2 运行结果#

与 ifconfig 验证一致,成功:

作者:PikapBai

出处:https://www.cnblogs.com/PikapBai/p/13699162.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

备注:转载请注明出处并附加链接

Linux C 获取本机所有网卡的 IP,Mask - 她爱喝水 - 博客园 (cnblogs.com)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,获取本机网卡IP地址通常涉及到网络编程和系统调用。下面是一个简单的示例,使用了Windows平台下的GetAdaptersAddresses函数,如果在Unix/Linux环境中,需要使用不同的库函数(如getifaddrs、getaddrinfo等)。请注意,这个示例假设你有网络访问权限并允许读取本地网络信息。 ```c #include <stdio.h> #include <string.h> #include <windows.h> // Windows-specific header for GetAdaptersAddresses // Structure to store IP address and prefix length typedef struct _IP_ADAPTER_ADDRESSES { ULONG Length; ULONG Flags; ULONG IfIndex; char* InterfaceDescription; ULONG FirstUnicastAddress; ULONG AddressLength; ULONG BroadcastAddress; ULONG Mask; ULONG Metrics; IP_ADAPTER_ADDRESSES* Next; IP_ADDRESS_UNICAST_ADDRESSses* UnicastAddresses; IP_ADDRESS_MULTICAST_ADDRESSses* MulticastAddresses; IP_PREFIX_INFORMATION* PrefixInformation; } IP_ADAPTER_ADDRESSES; void getLocalIPAddress(char* ipBuffer) { IP_ADAPTER_ADDRESSES *adapterInfo = NULL, *currentAdapter = NULL; ULONG outBufferSize = sizeof(IP_ADAPTER_ADDRESSES); ULONG neededSize = 0; // Call the Windows function to retrieve adapter info if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_INTERFACES, NULL, &adapterInfo, &outBufferSize)) { printf("Failed to get network interfaces.\n"); return; } // Check for any errors if (adapterInfo->Length != outBufferSize) { neededSize = adapterInfo->Length; HeapFree(GetProcessHeap(), 0, adapterInfo); adapterInfo = (IP_ADAPTER_ADDRESSES*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, neededSize); if (!adapterInfo) { printf("Memory allocation failed.\n"); return; } if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_INTERFACES, NULL, &adapterInfo, &outBufferSize)) { printf("Failed to get network interfaces.\n"); HeapFree(GetProcessHeap(), 0, adapterInfo); return; } } // Iterate through adapters to find the first non-loopback interface for (currentAdapter = adapterInfo; currentAdapter != NULL; currentAdapter = currentAdapter->Next) { if (strcmp(currentAdapter->InterfaceDescription, "Loopback Pseudo-Interface") != 0) { break; } } // Copy the first unicast IP address to the provided buffer if (currentAdapter->FirstUnicastAddress) { strncpy(ipBuffer, inet_ntoa(((IP_ADDRESS*)&currentAdapter->FirstUnicastAddress)->u.Byte), sizeof(ipBuffer)); } else { printf("No unicast IP addresses found.\n"); } // Clean up HeapFree(GetProcessHeap(), 0, adapterInfo); } int main() { char ipAddress[INET_ADDRSTRLEN]; getLocalIPAddress(ipAddress); printf("Your local IP address is: %s\n", ipAddress); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值