@@ -111,47 +111,32 @@ static const FFLocalIpNIFlag niFlagOptions[] = {
111111 {},
112112};
113113
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 )
127115{
128116 struct sockaddr_in6 * addr = (struct sockaddr_in6 * ) ifa -> ifa_addr ;
129117
130118 FF_DEBUG ("Checking IPv6 type for interface %s" , ifa -> ifa_name );
131119
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 ;
141121 if (IN6_IS_ADDR_GLOBAL (& addr -> sin6_addr ))
142122 {
143- result = FF_IPV6_GUA ;
123+ result = FF_LOCALIP_IPV6_TYPE_GUA_BIT ;
144124 FF_DEBUG ("Interface %s has Global Unicast Address" , ifa -> ifa_name );
145125 }
146126 else if (IN6_IS_ADDR_UNIQUE_LOCAL (& addr -> sin6_addr ))
147127 {
148- result = FF_IPV6_ULA ;
128+ result = FF_LOCALIP_IPV6_TYPE_ULA_BIT ;
149129 FF_DEBUG ("Interface %s has Unique Local Address" , ifa -> ifa_name );
150130 }
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+ }
151136 else
152137 {
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 ;
155140 }
156141
157142#ifdef SIOCGIFAFLAG_IN6
@@ -178,13 +163,13 @@ static FFIPv6Type getIpType(struct ifaddrs* ifa)
178163
179164 #ifdef IN6_IFF_PREFER_SOURCE
180165 if (ifr6 .ifr_ifru .ifru_flags6 & IN6_IFF_PREFER_SOURCE )
181- return FF_IPV6_PREFERRED ;
166+ result |= FF_LOCALIP_IPV6_TYPE_PREFERRED_BIT ;
182167 #endif
183168 if (ifr6 .ifr_ifru .ifru_flags6 & (IN6_IFF_DEPRECATED | IN6_IFF_TEMPORARY | IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED
184169 #ifdef IN6_IFF_OPTIMISTIC
185170 | IN6_IFF_OPTIMISTIC
186171 #endif
187- )) result |= FF_IPV6_SECONDARY_FLAG ;
172+ )) result |= FF_LOCALIP_IPV6_TYPE_SECONDARY_BIT ;
188173 return result ;
189174#elif __linux__
190175 static FFlist addresses = {};
@@ -220,13 +205,13 @@ static FFIPv6Type getIpType(struct ifaddrs* ifa)
220205 if (memcmp (& addr -> sin6_addr , entry , sizeof (struct in6_addr )) == 0 )
221206 return result ;
222207 }
223- result |= FF_IPV6_SECONDARY_FLAG ;
208+ result |= FF_LOCALIP_IPV6_TYPE_SECONDARY_BIT ;
224209 return result ;
225210#elif __sun
226211 if (ifa -> ifa_flags & IFF_PREFERRED )
227- return FF_IPV6_PREFERRED ;
212+ result |= FF_LOCALIP_IPV6_TYPE_PREFERRED_BIT ;
228213 if (ifa -> ifa_flags & (IFF_DEPRECATED | IFF_TEMPORARY | IFF_DUPLICATE ))
229- result |= FF_IPV6_SECONDARY_FLAG ;
214+ result |= FF_LOCALIP_IPV6_TYPE_SECONDARY_BIT ;
230215 return result ;
231216#else
232217 return result ;
@@ -509,46 +494,65 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
509494 }
510495 }
511496
512- if (!( options -> showType & FF_LOCALIP_TYPE_ALL_IPS_BIT ) )
497+ if (options -> ipv6Type == FF_LOCALIP_IPV6_TYPE_AUTO )
513498 {
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 ))
518500 {
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 )
527505 {
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+ }
530523 }
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 )
532529 {
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 );
535532 }
536533 }
537- if (!selected ) selected = secondary ;
538-
539- if (selected )
540- appendIpv6 (options , & item -> ipv6 , selected );
541- else if (adapter -> ipv6 .length > 0 )
534+ else
542535 {
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 );
545539 }
546540 }
547541 else
548542 {
549- FF_DEBUG ("Adding all IPv6 addresses for interface %s" , adapter -> mac -> ifa_name );
550543 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+ }
552556 }
553557 }
554558 mac :
0 commit comments