当前位置:网站首页>Use getifaddrs to obtain the IP address of the local network interface
Use getifaddrs to obtain the IP address of the local network interface
2022-07-25 05:02:00 【togolife】
getifaddrs Function is used to get the data on the local network card ip Address information , As shown below, we implement a tool class , And test and verify .
#include <string>
#include <vector>
#include <map>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <string.h>
struct IfaceIpInfo {
std::string ifName;
std::vector<std::string> ipv4;
std::vector<std::string> ipv6;
};
class IpTool {
public:
static bool FetchAllIpAddress(std::map<std::string, IfaceIpInfo> &local)
{
struct ifaddrs *ifaddr = nullptr;
struct ifaddrs *ifa = nullptr;
if (getifaddrs(&ifaddr) == -1) {
printf("getifaddrs failed, %s", strerror(errno));
return false;
}
for (ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == nullptr) {
continue;
}
int family = ifa->ifa_addr->sa_family;
if (family != AF_INET && family != AF_INET6) {
continue;
}
char addr[NI_MAXHOST] = {
0};
int ret = getnameinfo(ifa->ifa_addr,
(family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6),
addr, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST);
if (ret != 0) {
printf("getnameinfo() failed: %s\n", gai_strerror(ret));
freeifaddrs(ifaddr);
return false;
}
std::string ifName = ifa->ifa_name;
std::map<std::string, IfaceIpInfo>::iterator iter = local.find(ifName);
if (iter == local.end()) {
IfaceIpInfo tmp;
iter = local.emplace(ifName, tmp).first;
}
if (family == AF_INET) {
iter->second.ipv4.push_back(addr);
} else {
/* For an IPv6 address, the suffix %wlan0 exists. */
char *sepNum = strchr(addr, '%');
if (sepNum != nullptr) {
*sepNum = '\0';
}
iter->second.ipv6.push_back(addr);
}
}
freeifaddrs(ifaddr);
return true;
}
};
int main(void)
{
std::map<std::string, IfaceIpInfo> local;
if (!IpTool::FetchAllIpAddress(local)) {
printf("fetch ip address failed!");
return -1;
}
for (auto iter = local.begin(); iter != local.end(); ++iter) {
printf("iface [%s]\n", iter->first.c_str());
printf(" ipv4 address is [\n");
for (auto it = iter->second.ipv4.begin(); it != iter->second.ipv4.end(); ++it) {
printf(" %s\n", it->c_str());
}
printf("]\n ipv6 address is [\n");
for (auto it = iter->second.ipv6.begin(); it != iter->second.ipv6.end(); ++it) {
printf(" %s\n", it->c_str());
}
printf("]\n");
}
return 0;
}
边栏推荐
- Pyg builds GCN to realize link prediction
- How to transfer NFT metadata from IPFs to smart contracts
- HMS core discovery Episode 16 live broadcast preview | play AI's new "sound" state with tiger pier
- If you don't know these 20 classic redis interview questions, don't go to the interview!
- Leetcode55. Jumping game
- MCU experiment record
- Get the parameters of the browser address bar
- [untitled]
- STM32 development note 117: generate IIR low-pass filter coefficients using MATLAB
- 中创算力荣获「2022年科技型中小企业」认定
猜你喜欢

Basic knowledge of scratch crawler framework
![[sht30 temperature and humidity display based on STM32F103]](/img/43/bbc66ab2d56cfa9dc05d795e8fe456.jpg)
[sht30 temperature and humidity display based on STM32F103]

Leetcode55. Jumping game

How can test / development programmers with 5 years of experience break through the technical bottleneck? Common problems in big factories

龙蜥社区发布首个 Anolis OS 安全指南 为用户业务系统保驾护航

Three must know and know problems of redis

The interviewer asked MySQL transactions, locks and mvcc at one go. I
![[CTF learning] steganography set in CTF -- picture steganography](/img/32/2da78bd5866cfab9ee64dfcb1c1204.png)
[CTF learning] steganography set in CTF -- picture steganography

1. If function of Excel

2022-7-18 summary
随机推荐
How to test data in the process of data warehouse migration?
Bypass XSS filters in Web Applications
Teach you three ways to optimize the performance from 20s to 500ms
How can test / development programmers with 5 years of experience break through the technical bottleneck? Common problems in big factories
nacos中哪边有这个列的sql脚本啊?
Zhongchuang computing power won the recognition of "2022 technology-based small and medium-sized enterprises"
Anaconda installs jupyter
How can I check if the number of RDS links in MySQL suddenly rises?
[untitled]
Etcd learning
Very clear organization
The market is right
教你如何定位不合理的SQL?并优化之
How to get the database creation time?
2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f
Baklib: share some methods about building enterprise knowledge management (km)
市场是对的
I will write some Q & A blogs recently, mainly focusing on the points that are easy to have doubts.
four hundred and forty-four thousand one hundred and forty-one
Permanent magnet synchronous motor 36 question (1) -- what is the difference between salient pole motor and salient pole motor?