From 5539fecb9915aea812c65d52183eba339b9c82e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 14 Jun 2024 11:17:45 +0800 Subject: [PATCH] LocalIP: detect MTU --- src/detection/localip/localip.h | 1 + src/detection/localip/localip_linux.c | 14 ++++++++++++++ src/detection/localip/localip_windows.c | 6 +++++- src/modules/localip/localip.c | 1 + 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/detection/localip/localip.h b/src/detection/localip/localip.h index fe50575ad..4b4162604 100644 --- a/src/detection/localip/localip.h +++ b/src/detection/localip/localip.h @@ -8,6 +8,7 @@ typedef struct FFLocalIpResult FFstrbuf ipv4; FFstrbuf ipv6; FFstrbuf mac; + int32_t mtu; bool defaultRoute; } FFLocalIpResult; diff --git a/src/detection/localip/localip_linux.c b/src/detection/localip/localip_linux.c index 9c23b987f..0a8e3a05d 100644 --- a/src/detection/localip/localip_linux.c +++ b/src/detection/localip/localip_linux.c @@ -10,6 +10,7 @@ #include #include #include +#include #if defined(__FreeBSD__) || defined(__APPLE__) #include @@ -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; } diff --git a/src/detection/localip/localip_windows.c b/src/detection/localip/localip_windows.c index 0650ae54a..8ac766660 100644 --- a/src/detection/localip/localip_windows.c +++ b/src/detection/localip/localip_windows.c @@ -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; diff --git a/src/modules/localip/localip.c b/src/modules/localip/localip.c index 28aa7d4d3..53bed6363 100644 --- a/src/modules/localip/localip.c +++ b/src/modules/localip/localip.c @@ -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)