@@ -36,6 +36,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
36
#include <linux/netlink.h>
37
37
#include <linux/rtnetlink.h>
38
38
#include <linux/if_packet.h>
39
+ #include <dlfcn.h>
39
40
40
41
typedef struct NetlinkList
41
42
{
@@ -534,7 +535,7 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
534
535
{
535
536
if (l_entry -> ifa_addr )
536
537
{
537
- l_entry -> ifa_dstaddr = (struct sockaddr * )l_addr ;
538
+ l_entry -> ifa_ifu . ifu_dstaddr = (struct sockaddr * )l_addr ;
538
539
}
539
540
else
540
541
{
@@ -545,7 +546,7 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
545
546
{
546
547
if (l_entry -> ifa_addr )
547
548
{
548
- l_entry -> ifa_dstaddr = l_entry -> ifa_addr ;
549
+ l_entry -> ifa_ifu . ifu_dstaddr = l_entry -> ifa_addr ;
549
550
}
550
551
l_entry -> ifa_addr = (struct sockaddr * )l_addr ;
551
552
}
@@ -652,8 +653,52 @@ static int interpretAddrs(int p_socket, pid_t p_pid, NetlinkList *p_netlinkList,
652
653
return 0 ;
653
654
}
654
655
656
+ // Tries to get the system's getifaddrs if it's available.
657
+ void * _dlopen_ifaddrs_handle = NULL ;
658
+ void init_dlopen_ifaddrs_handle (void ) __attribute__((constructor ));
659
+ void destroy_dlopen_ifaddrs_handle (void ) __attribute__((destructor ));
660
+
661
+ void init_dlopen_ifaddrs_handle (void )
662
+ {
663
+ _dlopen_ifaddrs_handle = dlopen ("libc.so" , RTLD_NOW |RTLD_LOCAL );
664
+ }
665
+
666
+ void destroy_dlopen_ifaddrs_handle (void )
667
+ {
668
+ if (_dlopen_ifaddrs_handle != NULL )
669
+ {
670
+ dlclose (_dlopen_ifaddrs_handle );
671
+ _dlopen_ifaddrs_handle = NULL ;
672
+ }
673
+ }
674
+
675
+ int (* get_getifaddrs_native_func (void ))(struct ifaddrs * * )
676
+ {
677
+ if (!_dlopen_ifaddrs_handle )
678
+ {
679
+ return NULL ;
680
+ }
681
+ return dlsym (_dlopen_ifaddrs_handle , "getifaddrs" );
682
+ }
683
+
684
+ void (* get_freeifaddrs_native_func (void ))(struct ifaddrs * )
685
+ {
686
+ if (!_dlopen_ifaddrs_handle )
687
+ {
688
+ return NULL ;
689
+ }
690
+ return dlsym (_dlopen_ifaddrs_handle , "freeifaddrs" );
691
+ }
692
+
655
693
int getifaddrs (struct ifaddrs * * ifap )
656
694
{
695
+ // If the native getifaddrs is available, call it.
696
+ int (* getifaddrs_native )(struct ifaddrs * * ) = get_getifaddrs_native_func ();
697
+ if (getifaddrs_native != NULL )
698
+ {
699
+ return getifaddrs_native (ifap );
700
+ }
701
+
657
702
int l_socket ;
658
703
int l_result ;
659
704
int l_numLinks ;
@@ -703,6 +748,14 @@ int getifaddrs(struct ifaddrs **ifap)
703
748
704
749
void freeifaddrs (struct ifaddrs * ifa )
705
750
{
751
+ // If the native freeifaddrs is available, call it.
752
+ void (* freeifaddrs_native )(struct ifaddrs * ) = get_freeifaddrs_native_func ();
753
+ if (freeifaddrs_native != NULL )
754
+ {
755
+ freeifaddrs_native (ifa );
756
+ return ;
757
+ }
758
+
706
759
struct ifaddrs * l_cur ;
707
760
while (ifa )
708
761
{
0 commit comments