gpt4 book ai didi

c++ - Linux C : Get default interface's IP address

转载 作者:IT王子 更新时间:2023-10-29 01:27:05 24 4
gpt4 key购买 nike

我的问题是关于以下代码(在 this link 中):

#include <stdio.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>

int main (int argc, const char * argv[]) {
struct ifaddrs * ifAddrStruct = NULL;
struct ifaddrs * ifa = NULL;
void * tmpAddrPtr = NULL;

getifaddrs(&ifAddrStruct);

for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa ->ifa_addr->sa_family==AF_INET) { // Check it is
// a valid IPv4 address
tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
}
else if (ifa->ifa_addr->sa_family==AF_INET6) { // Check it is
// a valid IPv6 address
tmpAddrPtr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
char addressBuffer[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
}
}
if (ifAddrStruct != NULL)
freeifaddrs(ifAddrStruct);
return 0;
}

这段代码的输出是这样的:

lo IP Address 127.0.0.1
wlan0 IP Address 172.28.1.89 (I want to only this)
lo IP Address ::1
wlan0 IP Address fe80::6e71:d9ff:fe1d:b0

如何获取默认接口(interface)的 IP 地址? (你可以用另一段代码给出答案。)

最佳答案

这段代码将做的事情:

#include <stdio.h>
#include <unistd.h>
#include <string.h> /* For strncpy */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>

int
main()
{
int fd;
struct ifreq ifr;

fd = socket(AF_INET, SOCK_DGRAM, 0);

/* I want to get an IPv4 IP address */
ifr.ifr_addr.sa_family = AF_INET;

/* I want an IP address attached to "eth0" */
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);

ioctl(fd, SIOCGIFADDR, &ifr);

close(fd);

/* Display result */
printf("%s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));

return 0;
}

结果:

192.168.5.27

RUN SUCCESSFUL (total time: 52ms)

或者,您也可以使用 IP 地址掩码。例如,仅当掩码不同于 255.0.0.0(环回掩码)时才会打印

#include <stdio.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>

int main(int argc, const char * argv[]) {
struct ifaddrs * ifAddrStruct = NULL, * ifa = NULL;
void * tmpAddrPtr = NULL;

getifaddrs(&ifAddrStruct);
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa ->ifa_addr->sa_family == AF_INET) { // Check it is IPv4
char mask[INET_ADDRSTRLEN];
void* mask_ptr = &((struct sockaddr_in*) ifa->ifa_netmask)->sin_addr;
inet_ntop(AF_INET, mask_ptr, mask, INET_ADDRSTRLEN);
if (strcmp(mask, "255.0.0.0") != 0) {
printf("mask:%s\n", mask);
// Is a valid IPv4 Address
tmpAddrPtr = &((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
}
else if (ifa->ifa_addr->sa_family == AF_INET6) { // Check it is
// a valid IPv6 Address.

// Do something
}
}
}
if (ifAddrStruct != NULL)
freeifaddrs(ifAddrStruct);
return 0;
}

结果:

mask:255.255.255.0

eth0 IP Address 192.168.5.27

RUN SUCCESSFUL (total time: 53ms)

关于c++ - Linux C : Get default interface's IP address,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17909401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com