11#include <winsock2.h>
22#include <ws2tcpip.h>
33#include <iphlpapi.h>
4- #include <wchar.h>
54
5+ #include "util/mallocHelper.h"
6+ #include "util/windows/unicode.h"
67#include "localip.h"
78
8- static void addNewIp (FFlist * list , const char * name , const char * addr , bool ipv6 )
9+ static void addNewIp (FFlist * list , const char * name , const char * value , int type , bool newIp )
910{
10- FFLocalIpResult * ip = (FFLocalIpResult * ) ffListAdd (list );
11- ffStrbufInitS (& ip -> name , name );
12- ffStrbufInitS (& ip -> addr , addr );
13- ip -> ipv6 = ipv6 ;
11+ FFLocalIpResult * ip = NULL ;
12+
13+ if (newIp )
14+ {
15+ ip = (FFLocalIpResult * ) ffListAdd (list );
16+ ffStrbufInitS (& ip -> name , name );
17+ ffStrbufInit (& ip -> ipv4 );
18+ ffStrbufInit (& ip -> ipv6 );
19+ ffStrbufInit (& ip -> mac );
20+ }
21+ else
22+ {
23+ ip = ffListGet (list , list -> length - 1 );
24+ }
25+
26+ switch (type )
27+ {
28+ case AF_INET :
29+ ffStrbufSetS (& ip -> ipv4 , value );
30+ break ;
31+ case AF_INET6 :
32+ ffStrbufSetS (& ip -> ipv6 , value );
33+ break ;
34+ case -1 :
35+ ffStrbufSetS (& ip -> mac , value );
36+ break ;
37+ }
1438}
1539
1640const char * ffDetectLocalIps (const FFinstance * instance , FFlist * results )
1741{
18- IP_ADAPTER_ADDRESSES * adapter_addresses = NULL ;
42+ IP_ADAPTER_ADDRESSES * FF_AUTO_FREE adapter_addresses = NULL ;
1943
2044 // Start with a 16 KB buffer and resize if needed -
2145 // multiple attempts in case interfaces change while
@@ -27,11 +51,10 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
2751 assert (adapter_addresses );
2852
2953 DWORD error = GetAdaptersAddresses (
30- AF_UNSPEC ,
31- GAA_FLAG_SKIP_ANYCAST |
32- GAA_FLAG_SKIP_MULTICAST |
33- GAA_FLAG_SKIP_DNS_SERVER |
34- GAA_FLAG_SKIP_FRIENDLY_NAME ,
54+ instance -> config .localIpShowType & FF_LOCALIP_TYPE_IPV4_BIT
55+ ? instance -> config .localIpShowType & FF_LOCALIP_TYPE_IPV6_BIT ? AF_UNSPEC : AF_INET
56+ : AF_INET6 ,
57+ GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER ,
3558 NULL ,
3659 adapter_addresses ,
3760 & adapter_addresses_buffer_size );
@@ -47,41 +70,47 @@ const char* ffDetectLocalIps(const FFinstance* instance, FFlist* results)
4770 // Iterate through all of the adapters
4871 for (IP_ADAPTER_ADDRESSES * adapter = adapter_addresses ; adapter ; adapter = adapter -> Next )
4972 {
50- if (adapter -> IfType == IF_TYPE_SOFTWARE_LOOPBACK && !instance -> config .localIpShowLoop )
73+ bool isLoop = adapter -> IfType == IF_TYPE_SOFTWARE_LOOPBACK ;
74+ if (isLoop && !(instance -> config .localIpShowType & FF_LOCALIP_TYPE_LOOP_BIT ))
5175 continue ;
5276
77+ bool newIp = true;
78+
5379 char name [128 ];
5480 WideCharToMultiByte (CP_UTF8 , 0 , adapter -> FriendlyName , -1 , name , sizeof (name ), NULL , NULL );
5581 if (instance -> config .localIpNamePrefix .length && strncmp (name , instance -> config .localIpNamePrefix .chars , instance -> config .localIpNamePrefix .length ) != 0 )
5682 continue ;
5783
84+ if (instance -> config .localIpShowType & FF_LOCALIP_TYPE_MAC_BIT && adapter -> PhysicalAddressLength == 6 )
85+ {
86+ char addressBuffer [32 ];
87+ uint8_t * ptr = adapter -> PhysicalAddress ;
88+ snprintf (addressBuffer , sizeof (addressBuffer ), "%02x:%02x:%02x:%02x:%02x:%02x" ,
89+ ptr [0 ], ptr [1 ], ptr [2 ], ptr [3 ], ptr [4 ], ptr [5 ]);
90+ addNewIp (results , name , addressBuffer , -1 , newIp );
91+ newIp = false;
92+ }
93+
5894 for (IP_ADAPTER_UNICAST_ADDRESS * ifa = adapter -> FirstUnicastAddress ; ifa ; ifa = ifa -> Next )
5995 {
6096 if (ifa -> Address .lpSockaddr -> sa_family == AF_INET )
6197 {
62- // IPv4
63- if (!instance -> config .localIpShowIpV4 )
64- continue ;
65-
6698 SOCKADDR_IN * ipv4 = (SOCKADDR_IN * ) ifa -> Address .lpSockaddr ;
6799 char addressBuffer [INET_ADDRSTRLEN ];
68100 inet_ntop (AF_INET , & ipv4 -> sin_addr , addressBuffer , INET_ADDRSTRLEN );
69- addNewIp (results , name , addressBuffer , false);
101+ addNewIp (results , name , addressBuffer , AF_INET , newIp );
102+ newIp = false;
70103 }
71104 else if (ifa -> Address .lpSockaddr -> sa_family == AF_INET6 )
72105 {
73- // IPv6
74- if (!instance -> config .localIpShowIpV6 )
75- continue ;
76-
77106 SOCKADDR_IN6 * ipv6 = (SOCKADDR_IN6 * ) ifa -> Address .lpSockaddr ;
78107 char addressBuffer [INET6_ADDRSTRLEN ];
79108 inet_ntop (AF_INET6 , & ipv6 -> sin6_addr , addressBuffer , INET6_ADDRSTRLEN );
80- addNewIp (results , name , addressBuffer , true);
109+ addNewIp (results , name , addressBuffer , AF_INET6 , newIp );
110+ newIp = false;
81111 }
82112 }
83113 }
84114
85- free (adapter_addresses );
86115 return NULL ;
87116}
0 commit comments