LocalIP (Windows): detect gateway ip

This commit is contained in:
李通洲 2024-03-05 10:59:05 +08:00
parent b9dbb66bd7
commit 7eb1f616d9
4 changed files with 68 additions and 9 deletions

View File

@ -7,6 +7,8 @@ typedef struct FFLocalIpResult
FFstrbuf name;
FFstrbuf ipv4;
FFstrbuf ipv6;
FFstrbuf gateway4;
FFstrbuf gateway6;
FFstrbuf mac;
bool defaultRoute;
} FFLocalIpResult;

View File

@ -6,7 +6,7 @@
#include "util/windows/unicode.h"
#include "localip.h"
static void addNewIp(FFlist* list, const char* name, const char* value, int type, bool newIp, bool defaultRoute)
static void addNewIp(FFlist* list, const char* name, const char* value, int type, bool newIp, bool gateway, bool defaultRoute)
{
FFLocalIpResult* ip = NULL;
@ -17,6 +17,8 @@ static void addNewIp(FFlist* list, const char* name, const char* value, int type
ffStrbufInit(&ip->ipv4);
ffStrbufInit(&ip->ipv6);
ffStrbufInit(&ip->mac);
ffStrbufInit(&ip->gateway4);
ffStrbufInit(&ip->gateway6);
ip->defaultRoute = defaultRoute;
}
else
@ -27,10 +29,10 @@ static void addNewIp(FFlist* list, const char* name, const char* value, int type
switch (type)
{
case AF_INET:
ffStrbufSetS(&ip->ipv4, value);
ffStrbufSetS(gateway ? &ip->gateway4 : &ip->ipv4, value);
break;
case AF_INET6:
ffStrbufSetS(&ip->ipv6, value);
ffStrbufSetS(gateway ? &ip->gateway6 : &ip->ipv6, value);
break;
case -1:
ffStrbufSetS(&ip->mac, value);
@ -57,7 +59,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
options->showType & FF_LOCALIP_TYPE_IPV4_BIT
? options->showType & FF_LOCALIP_TYPE_IPV6_BIT ? AF_UNSPEC : AF_INET
: AF_INET6,
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER,
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | (options->showType & FF_LOCALIP_TYPE_GATEWAY_BIT ? GAA_FLAG_INCLUDE_GATEWAYS : 0),
NULL,
adapter_addresses,
&adapter_addresses_buffer_size);
@ -96,10 +98,30 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
uint8_t* ptr = adapter->PhysicalAddress;
snprintf(addressBuffer, sizeof(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x",
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
addNewIp(results, name, addressBuffer, -1, newIp, isDefaultRoute);
addNewIp(results, name, addressBuffer, -1, newIp, false, isDefaultRoute);
newIp = false;
}
for (IP_ADAPTER_GATEWAY_ADDRESS_LH* ifa = adapter->FirstGatewayAddress; ifa; ifa = ifa->Next)
{
if (ifa->Address.lpSockaddr->sa_family == AF_INET)
{
SOCKADDR_IN* ipv4 = (SOCKADDR_IN*) ifa->Address.lpSockaddr;
char addressBuffer[INET_ADDRSTRLEN + 4];
inet_ntop(AF_INET, &ipv4->sin_addr, addressBuffer, INET_ADDRSTRLEN);
addNewIp(results, name, addressBuffer, AF_INET, newIp, true, isDefaultRoute);
newIp = false;
}
else if (ifa->Address.lpSockaddr->sa_family == AF_INET6)
{
SOCKADDR_IN6* ipv6 = (SOCKADDR_IN6*) ifa->Address.lpSockaddr;
char addressBuffer[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &ipv6->sin6_addr, addressBuffer, INET6_ADDRSTRLEN);
addNewIp(results, name, addressBuffer, AF_INET6, newIp, true, isDefaultRoute);
newIp = false;
}
}
for (IP_ADAPTER_UNICAST_ADDRESS* ifa = adapter->FirstUnicastAddress; ifa; ifa = ifa->Next)
{
if (ifa->Address.lpSockaddr->sa_family == AF_INET)
@ -114,7 +136,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
snprintf(addressBuffer + len, 4, "/%u", (unsigned) ifa->OnLinkPrefixLength);
}
addNewIp(results, name, addressBuffer, AF_INET, newIp, isDefaultRoute);
addNewIp(results, name, addressBuffer, AF_INET, newIp, false, isDefaultRoute);
newIp = false;
}
else if (ifa->Address.lpSockaddr->sa_family == AF_INET6)
@ -122,7 +144,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
SOCKADDR_IN6* ipv6 = (SOCKADDR_IN6*) ifa->Address.lpSockaddr;
char addressBuffer[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &ipv6->sin6_addr, addressBuffer, INET6_ADDRSTRLEN);
addNewIp(results, name, addressBuffer, AF_INET6, newIp, isDefaultRoute);
addNewIp(results, name, addressBuffer, AF_INET6, newIp, false, isDefaultRoute);
newIp = false;
}
}

View File

@ -5,7 +5,7 @@
#include "util/stringUtils.h"
#define FF_LOCALIP_DISPLAY_NAME "Local IP"
#define FF_LOCALIP_NUM_FORMAT_ARGS 5
#define FF_LOCALIP_NUM_FORMAT_ARGS 7
#pragma GCC diagnostic ignored "-Wsign-conversion"
static int sortIps(const FFLocalIpResult* left, const FFLocalIpResult* right)
@ -40,12 +40,18 @@ static void printIp(FFLocalIpResult* ip, bool markDefaultRoute)
{
ffStrbufWriteTo(&ip->ipv4, stdout);
flag = true;
if (ip->gateway4.length)
printf(" [%s]", ip->gateway4.chars);
}
if (ip->ipv6.length)
{
if (flag) putchar(' ');
ffStrbufWriteTo(&ip->ipv6, stdout);
flag = true;
if (ip->gateway6.length)
printf(" [%s]", ip->gateway6.chars);
}
if (ip->mac.length)
{
@ -116,6 +122,8 @@ void ffPrintLocalIp(FFLocalIpOptions* options)
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->mac},
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->name},
{FF_FORMAT_ARG_TYPE_BOOL, &ip->defaultRoute},
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->gateway4},
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->gateway6},
});
}
++index;
@ -127,6 +135,8 @@ void ffPrintLocalIp(FFLocalIpOptions* options)
ffStrbufDestroy(&ip->name);
ffStrbufDestroy(&ip->ipv4);
ffStrbufDestroy(&ip->ipv6);
ffStrbufDestroy(&ip->gateway4);
ffStrbufDestroy(&ip->gateway6);
ffStrbufDestroy(&ip->mac);
}
}
@ -165,6 +175,15 @@ bool ffParseLocalIpCommandOptions(FFLocalIpOptions* options, const char* key, co
return true;
}
if (ffStrEqualsIgnCase(subKey, "show-gateway"))
{
if (ffOptionParseBoolean(value))
options->showType |= FF_LOCALIP_TYPE_GATEWAY_BIT;
else
options->showType &= ~FF_LOCALIP_TYPE_GATEWAY_BIT;
return true;
}
if (ffStrEqualsIgnCase(subKey, "show-loop"))
{
if (ffOptionParseBoolean(value))
@ -238,6 +257,15 @@ void ffParseLocalIpJsonObject(FFLocalIpOptions* options, yyjson_val* module)
continue;
}
if (ffStrEqualsIgnCase(key, "showGateway"))
{
if (yyjson_get_bool(val))
options->showType |= FF_LOCALIP_TYPE_GATEWAY_BIT;
else
options->showType &= ~FF_LOCALIP_TYPE_GATEWAY_BIT;
continue;
}
if (ffStrEqualsIgnCase(key, "showLoop"))
{
if (yyjson_get_bool(val))
@ -329,6 +357,8 @@ void ffGenerateLocalIpJsonResult(FF_MAYBE_UNUSED FFLocalIpOptions* options, yyjs
yyjson_mut_obj_add_bool(doc, obj, "defaultRoute", ip->defaultRoute);
yyjson_mut_obj_add_strbuf(doc, obj, "ipv4", &ip->ipv4);
yyjson_mut_obj_add_strbuf(doc, obj, "ipv6", &ip->ipv6);
yyjson_mut_obj_add_strbuf(doc, obj, "gateway4", &ip->gateway4);
yyjson_mut_obj_add_strbuf(doc, obj, "gateway6", &ip->gateway6);
yyjson_mut_obj_add_strbuf(doc, obj, "mac", &ip->mac);
yyjson_mut_obj_add_strbuf(doc, obj, "name", &ip->name);
}
@ -339,6 +369,8 @@ exit:
ffStrbufDestroy(&ip->name);
ffStrbufDestroy(&ip->ipv4);
ffStrbufDestroy(&ip->ipv6);
ffStrbufDestroy(&ip->gateway4);
ffStrbufDestroy(&ip->gateway6);
ffStrbufDestroy(&ip->mac);
}
}
@ -350,7 +382,9 @@ void ffPrintLocalIpHelpFormat(void)
"Local IPv6 address",
"Physical (MAC) address",
"Interface name",
"Is default route"
"Is default route",
"Gateway IPv4 address",
"Gateway IPv6 address",
});
}

View File

@ -11,6 +11,7 @@ typedef enum FFLocalIpType
FF_LOCALIP_TYPE_IPV4_BIT = 1 << 1,
FF_LOCALIP_TYPE_IPV6_BIT = 1 << 2,
FF_LOCALIP_TYPE_MAC_BIT = 1 << 3,
FF_LOCALIP_TYPE_GATEWAY_BIT = 1 << 4,
FF_LOCALIP_TYPE_COMPACT_BIT = 1 << 10,
} FFLocalIpType;