Skip to content

Commit 320283a

Browse files
committed
LocalIP: fix bug that flags are appended multiple times
1 parent 40ab735 commit 320283a

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

src/detection/localip/localip.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ typedef struct FFLocalIpResult
1414
bool defaultRoute;
1515
} FFLocalIpResult;
1616

17-
typedef struct NIFlag
17+
typedef struct FFLocalIpNIFlag
1818
{
19-
int flag;
19+
uint32_t flag;
2020
const char *name;
21-
} NIFlag;
21+
} FFLocalIpNIFlag;
2222

23-
static inline void writeNIFlagsToStrBuf(FFstrbuf *buf, int flag, const NIFlag names[])
23+
static inline void ffLocalIpFillNIFlags(FFstrbuf *buf, uint32_t flag, const FFLocalIpNIFlag names[])
2424
{
25-
for (const NIFlag *nf = names; nf->name != NULL && flag != 0; ++nf)
25+
for (const FFLocalIpNIFlag *nf = names; flag && nf->name; ++nf)
2626
{
27-
if (flag & nf->flag) {
28-
if (nf - names != 0)
27+
if (flag & nf->flag)
28+
{
29+
if (buf->length > 0)
2930
ffStrbufAppendC(buf, ',');
3031
ffStrbufAppendS(buf, nf->name);
3132
flag &= ~nf->flag;

src/detection/localip/localip_linux.c

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <sys/sockio.h>
2929
#endif
3030

31-
static const NIFlag niFlags[] = {
31+
static const FFLocalIpNIFlag niFlagOptions[] = {
3232
{ IFF_UP, "UP" },
3333
{ IFF_BROADCAST, "BROADCAST" },
3434
{ IFF_DEBUG, "DEBUG" },
@@ -66,10 +66,10 @@ static const NIFlag niFlags[] = {
6666
{ IFF_CANTCONFIG, "CANTCONFIG" },
6767
#endif
6868
// sentinel
69-
{ 0, NULL }
69+
{},
7070
};
7171

72-
static void addNewIp(FFlist* list, const char* name, const char* addr, int type, bool defaultRoute, bool firstOnly)
72+
static void addNewIp(FFlist* list, const char* name, const char* addr, int type, bool defaultRoute, uint32_t flags, bool firstOnly)
7373
{
7474
FFLocalIpResult* ip = NULL;
7575

@@ -90,6 +90,8 @@ static void addNewIp(FFlist* list, const char* name, const char* addr, int type,
9090
ip->defaultRoute = defaultRoute;
9191
ip->mtu = -1;
9292
ip->speed = -1;
93+
94+
ffLocalIpFillNIFlags(&ip->flags, flags, niFlagOptions);
9395
}
9496

9597
switch (type)
@@ -139,7 +141,8 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
139141
if (options->namePrefix.length && strncmp(ifa->ifa_name, options->namePrefix.chars, options->namePrefix.length) != 0)
140142
continue;
141143

142-
bool newIP = false;
144+
uint32_t flags = options->showType & FF_LOCALIP_TYPE_FLAGS_BIT ? ifa->ifa_flags : 0;
145+
143146
if (ifa->ifa_addr->sa_family == AF_INET)
144147
{
145148
if (!(options->showType & FF_LOCALIP_TYPE_IPV4_BIT))
@@ -160,8 +163,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
160163
}
161164
}
162165

163-
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET, isDefaultRoute, !(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT));
164-
newIP = true;
166+
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET, isDefaultRoute, flags, !(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT));
165167
}
166168
else if (ifa->ifa_addr->sa_family == AF_INET6)
167169
{
@@ -186,8 +188,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
186188
}
187189
}
188190

189-
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET6, isDefaultRoute, !(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT));
190-
newIP = true;
191+
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET6, isDefaultRoute, flags, !(options->showType & FF_LOCALIP_TYPE_ALL_IPS_BIT));
191192
}
192193
#if __FreeBSD__ || __OpenBSD__ || __APPLE__
193194
else if (ifa->ifa_addr->sa_family == AF_LINK)
@@ -199,8 +200,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
199200
uint8_t* ptr = (uint8_t*) LLADDR((struct sockaddr_dl *)ifa->ifa_addr);
200201
snprintf(addressBuffer, sizeof(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x",
201202
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
202-
addNewIp(results, ifa->ifa_name, addressBuffer, -1, isDefaultRoute, false);
203-
newIP = true;
203+
addNewIp(results, ifa->ifa_name, addressBuffer, -1, isDefaultRoute, flags, false);
204204
}
205205
#else
206206
else if (ifa->ifa_addr->sa_family == AF_PACKET)
@@ -212,17 +212,9 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
212212
uint8_t* ptr = ((struct sockaddr_ll *)ifa->ifa_addr)->sll_addr;
213213
snprintf(addressBuffer, sizeof(addressBuffer), "%02x:%02x:%02x:%02x:%02x:%02x",
214214
ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
215-
addNewIp(results, ifa->ifa_name, addressBuffer, -1, isDefaultRoute, false);
216-
newIP = true;
215+
addNewIp(results, ifa->ifa_name, addressBuffer, -1, isDefaultRoute, flags, false);
217216
}
218217
#endif
219-
220-
if (newIP)
221-
{
222-
FFLocalIpResult* result = FF_LIST_GET(FFLocalIpResult, *results, results->length - 1);
223-
if (options->showType & FF_LOCALIP_TYPE_FLAGS_BIT)
224-
writeNIFlagsToStrBuf(&result->flags, (int)ifa->ifa_flags, niFlags);
225-
}
226218
}
227219

228220
if (ifAddrStruct) freeifaddrs(ifAddrStruct);

src/detection/localip/localip_windows.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "util/windows/unicode.h"
77
#include "localip.h"
88

9-
static const NIFlag niFlags[] = {
9+
static const FFLocalIpNIFlag niFlagOptions[] = {
1010
{ IP_ADAPTER_DDNS_ENABLED, "DDNS_ENABLED" },
1111
{ IP_ADAPTER_REGISTER_ADAPTER_SUFFIX, "REGISTER_ADAPTER_SUFFIX" },
1212
{ IP_ADAPTER_DHCP_ENABLED, "DHCP_ENABLED" },
@@ -18,7 +18,7 @@ static const NIFlag niFlags[] = {
1818
{ IP_ADAPTER_IPV6_ENABLED, "IPV6_ENABLED" },
1919
{ IP_ADAPTER_IPV6_MANAGE_ADDRESS_CONFIG, "IPV6_MANAGE_ADDRESS_CONFIG" },
2020
// sentinel
21-
{ 0, NULL }
21+
{},
2222
};
2323

2424
static void addNewIp(FFlist* list, const char* name, const char* addr, int type, bool newIp, bool defaultRoute)
@@ -174,7 +174,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
174174
if (options->showType & FF_LOCALIP_TYPE_MTU_BIT)
175175
result->mtu = (int32_t) adapter->Mtu;
176176
if (options->showType & FF_LOCALIP_TYPE_FLAGS_BIT)
177-
writeNIFlagsToStrBuf(&result->flags, (int)adapter->Flags, niFlags);
177+
ffLocalIpFillNIFlags(&result->flags, adapter->Flags, niFlagOptions);
178178
}
179179
}
180180

0 commit comments

Comments
 (0)