我的问题是关于以下代码(在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
如何获取默认界面的IP地址? (你可以用另一个代码片给出答案。)
答案 0 :(得分:3)
此代码将执行以下操作:
#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地址掩码。例如,仅当mask与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)
答案 1 :(得分:0)
涵盖更多情况的方法将包含以下内容:
getifaddrs
)来确定该界面的IP地址。路由文件解析可能如下所示(C ++代码简洁):
std::string defaultInterface;
std::ifstream routeFile(NET_ROUTE_FILEPATH, std::ios_base::in);
if (!routeFile.good())
{
return;
}
std::string line;
std::vector<std::string> tokens;
while(std::getline(routeFile, line))
{
std::istringstream stream(line);
std::copy(std::istream_iterator<std::string>(stream),
std::istream_iterator<std::string>(),
std::back_inserter<std::vector<std::string> >(tokens));
// the default interface is the one having the second
// field, Destination, set to "00000000"
if ((tokens.size() >= 2) && (tokens[1] == std::string("00000000")))
{
defaultInterface = tokens[0];
break;
}
tokens.clear();
}
routeFile.close();
然后,在您的代码中,在for
循环中迭代ifAddrStruct
结构,您可以将ifa->ifa_name
的测试添加为上面确定的defaultInterface
。< / p>