Comments (13)
@ADTmux next time you can just copied the code here if it is not large (more than 200 lines).
The file you provided:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netdb.h>
int main() {
struct ifaddrs *ifaddr, *ifa;
int family, s;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
printf("Interface: %s\n", ifa->ifa_name);
printf(" Family: ");
switch (family) {
case AF_INET:
printf("AF_INET (IPv4)\n");
break;
case AF_INET6:
printf("AF_INET6 (IPv6)\n");
break;
default:
printf("Unknown\n");
continue;
}
s = getnameinfo(ifa->ifa_addr, (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0) {
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
printf(" Address: %s\n", host);
if (family == AF_INET) {
struct sockaddr_in *netmask = (struct sockaddr_in *)ifa->ifa_netmask;
if (netmask != NULL) {
s = getnameinfo((struct sockaddr *)netmask, sizeof(struct sockaddr_in),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0) {
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
printf(" Netmask: %s\n", host);
}
}
printf(" Flags: %x\n", ifa->ifa_flags);
}
freeifaddrs(ifaddr);
return 0;
}
I will try to run it later.
from pcapplusplus.
@ADTmux Just in case if you don't fully understand the requirement.
In the code snip at the beginning of the issue, there is a line:
std::string command = "netstat -nr | grep default | grep " + m_Name;
we parse the result (ifaceInfo
) and use the IP to feed to IPv4Address
:
m_DefaultGateway = IPv4Address(ifaceInfo);
your mission is to find out the correct ifaceInfo
(of course you will need to change the name) by the C API.
Hint: the default route has the destination as 0.0.0.0.
from pcapplusplus.
Hey, I would really like to work on this!
from pcapplusplus.
@ADTmux sure, let me know if you encounter any issue.
from pcapplusplus.
Hey @tigercosmos, I did experiment with the C API - https://man7.org/linux/man-pages/man3/getifaddrs.3.html , but I found that it does not directly contain the gateway for macOS that I can fetch. Are we thinking about any particular API here? Other methods could include using the routing table or ioctl() system call.
from pcapplusplus.
@ADTmux That would be interesting. I tested with getifaddrs
on macOS 14 (arm) and it works fine to me. What is your platform? Could you also post the testing code (a runnable minimum code in a single cpp file) here?
from pcapplusplus.
@tigercosmos I used this code snippet (https://file.io/sCmaptmi8x5A) in PcapPlusPlus/Examples/Tutorials/Tutorial-LiveTraffic/main.cpp for testing the C library, and tried to print as much info as I can get, and I can see all network interfaces' details except the Default Gateway
The platform I used is macOS 13.4 (Intel)
from pcapplusplus.
Yes I did get the requirement. Basically if I can obtain the Default Gateway as a string I can pass it to IPv4Address()
from pcapplusplus.
FYI, not sure if it helps or not, but you may want to refer to the source code of route
, which provides the default route by calling route -n get default
https://opensource.apple.com/source/network_cmds/network_cmds-396.6/route.tproj/route.c.auto.html
from pcapplusplus.
regarding your code, obviously using getnameinfo()
is not enough, you may need to play around with C structs and C APIs more.
from pcapplusplus.
@ADTmux please let me know if you encounter any issue
from pcapplusplus.
Hi I'd like to work on this issue. Can you re reassign this issue to me @tigercosmos ?
from pcapplusplus.
@zhengfeihe sure, please go ahead.
from pcapplusplus.
Related Issues (20)
- Pcapng Parsing failure/confusion if unknown interface ID HOT 12
- Error when reading a packet HOT 1
- Why is std::byte not used? HOT 2
- Jumbo Frames in DPDK devices HOT 7
- Power of 2 restriction for number of queues in DpdkDevice HOT 3
- Fix building warning due to LightPcapNg HOT 10
- Migrate LightPcapNg to the forked repo HOT 9
- Error linking pcapplusplus using vcpkg and CMake. HOT 2
- PcapPlusPlus Configuration Error in CMake with vcpkg HOT 8
- PcapLiveDevice socket is not close HOT 2
- Problem with TcpReassembly and chunked transfer encoding HOT 6
- SNI Extension with zero length HOT 2
- Find alternative package for netifaces HOT 1
- Transfer this repository into PcapPlusPlus GitHub organization HOT 12
- warning C4193 HOT 4
- warning C4133 HOT 1
- warning C4129 HOT 1
- -DBUILD_SHARED_LIBS=ON cannot work in Windows HOT 12
- Address all MSVC compilation warnings
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pcapplusplus.