@@ -111,47 +111,32 @@ static const FFLocalIpNIFlag niFlagOptions[] = {
111
111
{},
112
112
};
113
113
114
- typedef enum __attribute__ ((__packed__ )) FFIPv6Type
115
- {
116
- FF_IPV6_Other ,
117
- FF_IPV6_GUA = 0b0001 ,
118
- FF_IPV6_GUA_SECONDARY = 0b0101 ,
119
- FF_IPV6_ULA = 0b0010 ,
120
- FF_IPV6_ULA_SECONDARY = 0b0110 ,
121
- FF_IPV6_TYPE_MASK = 0b0011 ,
122
- FF_IPV6_SECONDARY_FLAG = 0b1100 ,
123
- FF_IPV6_PREFERRED = UINT8_MAX ,
124
- } FFIPv6Type ;
125
-
126
- static FFIPv6Type getIpType (struct ifaddrs * ifa )
114
+ static FFLocalIpIpv6Type getIpv6Type (struct ifaddrs * ifa )
127
115
{
128
116
struct sockaddr_in6 * addr = (struct sockaddr_in6 * ) ifa -> ifa_addr ;
129
117
130
118
FF_DEBUG ("Checking IPv6 type for interface %s" , ifa -> ifa_name );
131
119
132
- #ifndef IN6_IS_ADDR_GLOBAL
133
- #define IN6_IS_ADDR_GLOBAL (a ) \
134
- ((((const uint32_t *) (a))[0] & htonl(0x70000000)) == htonl(0x20000000))
135
- #endif
136
- #ifndef IN6_IS_ADDR_UNIQUE_LOCAL
137
- #define IN6_IS_ADDR_UNIQUE_LOCAL (a ) \
138
- ((((const uint32_t *) (a))[0] & htonl(0xfe000000)) == htonl(0xfc000000))
139
- #endif
140
- FFIPv6Type result = FF_IPV6_Other ;
120
+ FFLocalIpIpv6Type result = FF_LOCALIP_IPV6_TYPE_NONE ;
141
121
if (IN6_IS_ADDR_GLOBAL (& addr -> sin6_addr ))
142
122
{
143
- result = FF_IPV6_GUA ;
123
+ result = FF_LOCALIP_IPV6_TYPE_GUA_BIT ;
144
124
FF_DEBUG ("Interface %s has Global Unicast Address" , ifa -> ifa_name );
145
125
}
146
126
else if (IN6_IS_ADDR_UNIQUE_LOCAL (& addr -> sin6_addr ))
147
127
{
148
- result = FF_IPV6_ULA ;
128
+ result = FF_LOCALIP_IPV6_TYPE_ULA_BIT ;
149
129
FF_DEBUG ("Interface %s has Unique Local Address" , ifa -> ifa_name );
150
130
}
131
+ else if (IN6_IS_ADDR_LINKLOCAL (& addr -> sin6_addr ))
132
+ {
133
+ result = FF_LOCALIP_IPV6_TYPE_LLA_BIT ;
134
+ FF_DEBUG ("Interface %s has Link-Local Address" , ifa -> ifa_name );
135
+ }
151
136
else
152
137
{
153
- FF_DEBUG ("Interface %s has other IPv6 address type" , ifa -> ifa_name );
154
- return FF_IPV6_Other ;
138
+ FF_DEBUG ("Interface %s has unknown IPv6 address type" , ifa -> ifa_name );
139
+ return FF_LOCALIP_IPV6_TYPE_UNKNOWN_BIT ;
155
140
}
156
141
157
142
#ifdef SIOCGIFAFLAG_IN6
@@ -178,13 +163,13 @@ static FFIPv6Type getIpType(struct ifaddrs* ifa)
178
163
179
164
#ifdef IN6_IFF_PREFER_SOURCE
180
165
if (ifr6 .ifr_ifru .ifru_flags6 & IN6_IFF_PREFER_SOURCE )
181
- return FF_IPV6_PREFERRED ;
166
+ result |= FF_LOCALIP_IPV6_TYPE_PREFERRED_BIT ;
182
167
#endif
183
168
if (ifr6 .ifr_ifru .ifru_flags6 & (IN6_IFF_DEPRECATED | IN6_IFF_TEMPORARY | IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED
184
169
#ifdef IN6_IFF_OPTIMISTIC
185
170
| IN6_IFF_OPTIMISTIC
186
171
#endif
187
- )) result |= FF_IPV6_SECONDARY_FLAG ;
172
+ )) result |= FF_LOCALIP_IPV6_TYPE_SECONDARY_BIT ;
188
173
return result ;
189
174
#elif __linux__
190
175
static FFlist addresses = {};
@@ -220,13 +205,13 @@ static FFIPv6Type getIpType(struct ifaddrs* ifa)
220
205
if (memcmp (& addr -> sin6_addr , entry , sizeof (struct in6_addr )) == 0 )
221
206
return result ;
222
207
}
223
- result |= FF_IPV6_SECONDARY_FLAG ;
208
+ result |= FF_LOCALIP_IPV6_TYPE_SECONDARY_BIT ;
224
209
return result ;
225
210
#elif __sun
226
211
if (ifa -> ifa_flags & IFF_PREFERRED )
227
- return FF_IPV6_PREFERRED ;
212
+ result |= FF_LOCALIP_IPV6_TYPE_PREFERRED_BIT ;
228
213
if (ifa -> ifa_flags & (IFF_DEPRECATED | IFF_TEMPORARY | IFF_DUPLICATE ))
229
- result |= FF_IPV6_SECONDARY_FLAG ;
214
+ result |= FF_LOCALIP_IPV6_TYPE_SECONDARY_BIT ;
230
215
return result ;
231
216
#else
232
217
return result ;
@@ -509,46 +494,65 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
509
494
}
510
495
}
511
496
512
- if (!( options -> showType & FF_LOCALIP_TYPE_ALL_IPS_BIT ) )
497
+ if (options -> ipv6Type == FF_LOCALIP_IPV6_TYPE_AUTO )
513
498
{
514
- struct ifaddrs * selected = NULL ;
515
- struct ifaddrs * secondary = NULL ;
516
-
517
- FF_LIST_FOR_EACH (struct ifaddrs * , pifa , adapter -> ipv6 )
499
+ if (!(options -> showType & FF_LOCALIP_TYPE_ALL_IPS_BIT ))
518
500
{
519
- FFIPv6Type type = getIpType (* pifa );
520
- if (type == FF_IPV6_PREFERRED )
521
- {
522
- selected = * pifa ;
523
- FF_DEBUG ("Found preferred IPv6 address for interface %s" , adapter -> mac -> ifa_name );
524
- break ;
525
- }
526
- else if (type == FF_IPV6_GUA && !selected )
501
+ struct ifaddrs * selected = NULL ;
502
+ struct ifaddrs * secondary = NULL ;
503
+
504
+ FF_LIST_FOR_EACH (struct ifaddrs * , pifa , adapter -> ipv6 )
527
505
{
528
- selected = * pifa ;
529
- FF_DEBUG ("Found GUA IPv6 address for interface %s" , adapter -> mac -> ifa_name );
506
+ FFLocalIpIpv6Type type = getIpv6Type (* pifa );
507
+ if (type & FF_LOCALIP_IPV6_TYPE_PREFERRED_BIT )
508
+ {
509
+ selected = * pifa ;
510
+ FF_DEBUG ("Found preferred IPv6 address for interface %s" , adapter -> mac -> ifa_name );
511
+ break ;
512
+ }
513
+ else if ((type & FF_LOCALIP_IPV6_TYPE_GUA_BIT ) && !(type & FF_LOCALIP_IPV6_TYPE_SECONDARY_BIT ) && !selected )
514
+ {
515
+ selected = * pifa ;
516
+ FF_DEBUG ("Found GUA IPv6 address for interface %s" , adapter -> mac -> ifa_name );
517
+ }
518
+ else if ((type & FF_LOCALIP_IPV6_TYPE_ULA_BIT ) && !(type & FF_LOCALIP_IPV6_TYPE_SECONDARY_BIT ) && !secondary )
519
+ {
520
+ secondary = * pifa ;
521
+ FF_DEBUG ("Found ULA IPv6 address for interface %s" , adapter -> mac -> ifa_name );
522
+ }
530
523
}
531
- else if (type == FF_IPV6_ULA && !secondary )
524
+ if (!selected ) selected = secondary ;
525
+
526
+ if (selected )
527
+ appendIpv6 (options , & item -> ipv6 , selected );
528
+ else if (adapter -> ipv6 .length > 0 )
532
529
{
533
- secondary = * pifa ;
534
- FF_DEBUG ("Found ULA IPv6 address for interface %s" , adapter -> mac -> ifa_name );
530
+ appendIpv6 ( options , & item -> ipv6 , * FF_LIST_GET ( struct ifaddrs * , adapter -> ipv6 , 0 )) ;
531
+ FF_DEBUG ("Using first IPv6 address for interface %s" , adapter -> mac -> ifa_name );
535
532
}
536
533
}
537
- if (!selected ) selected = secondary ;
538
-
539
- if (selected )
540
- appendIpv6 (options , & item -> ipv6 , selected );
541
- else if (adapter -> ipv6 .length > 0 )
534
+ else
542
535
{
543
- appendIpv6 (options , & item -> ipv6 , * FF_LIST_GET (struct ifaddrs * , adapter -> ipv6 , 0 ));
544
- FF_DEBUG ("Using first IPv6 address for interface %s" , adapter -> mac -> ifa_name );
536
+ FF_DEBUG ("Adding all IPv6 addresses for interface %s" , adapter -> mac -> ifa_name );
537
+ FF_LIST_FOR_EACH (struct ifaddrs * , pifa , adapter -> ipv6 )
538
+ appendIpv6 (options , & item -> ipv6 , * pifa );
545
539
}
546
540
}
547
541
else
548
542
{
549
- FF_DEBUG ("Adding all IPv6 addresses for interface %s" , adapter -> mac -> ifa_name );
550
543
FF_LIST_FOR_EACH (struct ifaddrs * , pifa , adapter -> ipv6 )
551
- appendIpv6 (options , & item -> ipv6 , * pifa );
544
+ {
545
+ FFLocalIpIpv6Type type = getIpv6Type (* pifa );
546
+ if (type & options -> ipv6Type )
547
+ {
548
+ if ((options -> showType & FF_LOCALIP_TYPE_ALL_IPS_BIT ) || !(type & FF_LOCALIP_IPV6_TYPE_SECONDARY_BIT ))
549
+ {
550
+ appendIpv6 (options , & item -> ipv6 , * pifa );
551
+ if (!(options -> showType & FF_LOCALIP_TYPE_ALL_IPS_BIT ))
552
+ break ;
553
+ }
554
+ }
555
+ }
552
556
}
553
557
}
554
558
mac :
0 commit comments