mirror of
https://github.com/g4klx/MMDVMHost
synced 2025-12-21 15:09:23 +08:00
CNetworkInfo::getNetworkInterface() OpenBSD/NetBSD support
added IPv4 default routing information request code for OpenBSD/NetBSD.
This commit is contained in:
@@ -26,13 +26,18 @@
|
|||||||
#include <clocale>
|
#include <clocale>
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#if !defined(_WIN32) && !defined(_WIN64)
|
#if defined(__linux__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
||||||
#include <ifaddrs.h>
|
#include <ifaddrs.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#else
|
#if defined(__OpenBSD__) || defined(__NetBSD__)
|
||||||
|
#include <sys/sysctl.h>
|
||||||
|
#include <net/if.h>
|
||||||
|
#include <net/route.h>
|
||||||
|
#endif
|
||||||
|
#elif defined(_WIN32) || defined(_WIN64)
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#include <iphlpapi.h>
|
#include <iphlpapi.h>
|
||||||
#pragma comment(lib, "iphlpapi.lib")
|
#pragma comment(lib, "iphlpapi.lib")
|
||||||
@@ -61,17 +66,16 @@ void CNetworkInfo::getNetworkInterface(unsigned char* info)
|
|||||||
|
|
||||||
::strcpy((char*)info, "(address unknown)");
|
::strcpy((char*)info, "(address unknown)");
|
||||||
|
|
||||||
#if !defined(_WIN32) && !defined(_WIN64)
|
#if defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
||||||
const unsigned int IFLISTSIZ = 25U;
|
char* dflt = NULL;
|
||||||
|
|
||||||
FILE* fp = ::fopen("/proc/net/route" , "r");
|
#if defined(__linux__)
|
||||||
|
FILE* fp = ::fopen("/proc/net/route" , "r"); // IPv4 routing
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
LogError("Unabled to open /proc/route");
|
LogError("Unabled to open /proc/route");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* dflt = NULL;
|
|
||||||
|
|
||||||
char line[100U];
|
char line[100U];
|
||||||
while (::fgets(line, 100U, fp)) {
|
while (::fgets(line, 100U, fp)) {
|
||||||
char* p1 = strtok(line , " \t");
|
char* p1 = strtok(line , " \t");
|
||||||
@@ -87,11 +91,60 @@ void CNetworkInfo::getNetworkInterface(unsigned char* info)
|
|||||||
|
|
||||||
::fclose(fp);
|
::fclose(fp);
|
||||||
|
|
||||||
|
#elif defined(__OpenBSD__) || defined(__NetBSD__)
|
||||||
|
const int mib[] = {
|
||||||
|
CTL_NET,
|
||||||
|
PF_ROUTE,
|
||||||
|
0, // protocol
|
||||||
|
AF_INET, // IPv4 routing
|
||||||
|
NET_RT_DUMP,
|
||||||
|
0, // show all routes
|
||||||
|
#if defined(__OpenBSD__)
|
||||||
|
0, // table id
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
const int cnt = sizeof(mib) / sizeof(int);
|
||||||
|
size_t size;
|
||||||
|
char ifname[IF_NAMESIZE] = {};
|
||||||
|
|
||||||
|
if (::sysctl(mib, cnt, NULL, &size, NULL, 0) == -1 || size <= 0) {
|
||||||
|
LogError("Unable to estimate routing table size");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *buf = new char[size];
|
||||||
|
if (::sysctl(mib, cnt, buf, &size, NULL, 0) == -1) {
|
||||||
|
LogError("Unable to get routing table");
|
||||||
|
delete[] buf;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct rt_msghdr *rtm;
|
||||||
|
for (char *p = buf; p < buf + size; p += rtm->rtm_msglen) {
|
||||||
|
rtm = (struct rt_msghdr *)p;
|
||||||
|
if (rtm->rtm_version != RTM_VERSION)
|
||||||
|
continue;
|
||||||
|
#if defined(__OpenBSD__)
|
||||||
|
struct sockaddr_in *sa = (struct sockaddr_in *)(p + rtm->rtm_hdrlen);
|
||||||
|
#elif defined(__NetBSD__)
|
||||||
|
struct sockaddr_in *sa = (struct sockaddr_in *)(rtm + 1);
|
||||||
|
#endif
|
||||||
|
if (sa->sin_addr.s_addr == INADDR_ANY) {
|
||||||
|
::if_indextoname(rtm->rtm_index, ifname);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delete[] buf;
|
||||||
|
if (::strlen(ifname))
|
||||||
|
dflt = ifname;
|
||||||
|
#endif
|
||||||
if (dflt == NULL) {
|
if (dflt == NULL) {
|
||||||
LogError("Unable to find the default route");
|
LogError("Unable to find the default route");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const unsigned int IFLISTSIZ = 25U;
|
||||||
char interfacelist[IFLISTSIZ][50+INET6_ADDRSTRLEN];
|
char interfacelist[IFLISTSIZ][50+INET6_ADDRSTRLEN];
|
||||||
for (unsigned int n = 0U; n < IFLISTSIZ; n++)
|
for (unsigned int n = 0U; n < IFLISTSIZ; n++)
|
||||||
interfacelist[n][0] = 0;
|
interfacelist[n][0] = 0;
|
||||||
@@ -144,7 +197,7 @@ void CNetworkInfo::getNetworkInterface(unsigned char* info)
|
|||||||
}
|
}
|
||||||
|
|
||||||
LogInfo(" IP to show: %s", info);
|
LogInfo(" IP to show: %s", info);
|
||||||
#else
|
#elif defined(_WIN32) || defined(_WIN64)
|
||||||
PMIB_IPFORWARDTABLE pIpForwardTable = (MIB_IPFORWARDTABLE *)::malloc(sizeof(MIB_IPFORWARDTABLE));
|
PMIB_IPFORWARDTABLE pIpForwardTable = (MIB_IPFORWARDTABLE *)::malloc(sizeof(MIB_IPFORWARDTABLE));
|
||||||
if (pIpForwardTable == NULL) {
|
if (pIpForwardTable == NULL) {
|
||||||
LogError("Error allocating memory");
|
LogError("Error allocating memory");
|
||||||
|
|||||||
Reference in New Issue
Block a user