LocalIP: detect MTU

This commit is contained in:
李通洲
2024-06-14 11:17:45 +08:00
parent cff2a18466
commit 5539fecb99
4 changed files with 21 additions and 1 deletions
+1
View File
@@ -8,6 +8,7 @@ typedef struct FFLocalIpResult
FFstrbuf ipv4;
FFstrbuf ipv6;
FFstrbuf mac;
int32_t mtu;
bool defaultRoute;
} FFLocalIpResult;
+14
View File
@@ -10,6 +10,7 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <sys/ioctl.h>
#if defined(__FreeBSD__) || defined(__APPLE__)
#include <net/if_dl.h>
@@ -35,6 +36,7 @@ static void addNewIp(FFlist* list, const char* name, const char* addr, int type,
ffStrbufInit(&ip->ipv6);
ffStrbufInit(&ip->mac);
ip->defaultRoute = defaultRoute;
ip->mtu = -1;
}
switch (type)
@@ -160,5 +162,17 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
if (ifAddrStruct) freeifaddrs(ifAddrStruct);
FF_AUTO_CLOSE_FD int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd > 0)
{
FF_LIST_FOR_EACH(FFLocalIpResult, iface, *results)
{
struct ifreq ifr = {};
strncpy(ifr.ifr_name, iface->name.chars, IFNAMSIZ - 1);
if (ioctl(sockfd, SIOCGIFMTU, &ifr) == 0)
iface->mtu = ifr.ifr_mtu;
}
}
return NULL;
}
+5 -1
View File
@@ -18,10 +18,11 @@ static void addNewIp(FFlist* list, const char* name, const char* addr, int type,
ffStrbufInit(&ip->ipv6);
ffStrbufInit(&ip->mac);
ip->defaultRoute = defaultRoute;
ip->mtu = -1;
}
else
{
ip = ffListGet(list, list->length - 1);
ip = FF_LIST_GET(FFLocalIpResult, *list, list->length - 1);
}
switch (type)
@@ -147,6 +148,9 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
if (typesToAdd == 0) break;
}
}
if (!newIp)
FF_LIST_GET(FFLocalIpResult, *results, results->length - 1)->mtu = (int32_t) adapter->Mtu;
}
return NULL;
+1
View File
@@ -373,6 +373,7 @@ void ffGenerateLocalIpJsonResult(FF_MAYBE_UNUSED FFLocalIpOptions* options, yyjs
yyjson_mut_obj_add_strbuf(doc, obj, "ipv6", &ip->ipv6);
yyjson_mut_obj_add_strbuf(doc, obj, "mac", &ip->mac);
yyjson_mut_obj_add_strbuf(doc, obj, "name", &ip->name);
yyjson_mut_obj_add_int(doc, obj, "mtu", ip->mtu);
}
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)