Skip to content

Commit

Permalink
LocalIP: detect MTU
Browse files Browse the repository at this point in the history
  • Loading branch information
CarterLi committed Jun 14, 2024
1 parent cff2a18 commit 5539fec
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/detection/localip/localip.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ typedef struct FFLocalIpResult
FFstrbuf ipv4;
FFstrbuf ipv6;
FFstrbuf mac;
int32_t mtu;
bool defaultRoute;
} FFLocalIpResult;

Expand Down
14 changes: 14 additions & 0 deletions src/detection/localip/localip_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
6 changes: 5 additions & 1 deletion src/detection/localip/localip_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/modules/localip/localip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5539fec

Please sign in to comment.