@@ -36,6 +36,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3636#include <linux/netlink.h>
3737#include <linux/rtnetlink.h>
3838#include <linux/if_packet.h>
39+ #include <dlfcn.h>
3940
4041typedef struct NetlinkList
4142{
@@ -534,7 +535,7 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
534535 {
535536 if (l_entry -> ifa_addr )
536537 {
537- l_entry -> ifa_dstaddr = (struct sockaddr * )l_addr ;
538+ l_entry -> ifa_ifu . ifu_dstaddr = (struct sockaddr * )l_addr ;
538539 }
539540 else
540541 {
@@ -545,7 +546,7 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
545546 {
546547 if (l_entry -> ifa_addr )
547548 {
548- l_entry -> ifa_dstaddr = l_entry -> ifa_addr ;
549+ l_entry -> ifa_ifu . ifu_dstaddr = l_entry -> ifa_addr ;
549550 }
550551 l_entry -> ifa_addr = (struct sockaddr * )l_addr ;
551552 }
@@ -652,8 +653,52 @@ static int interpretAddrs(int p_socket, pid_t p_pid, NetlinkList *p_netlinkList,
652653 return 0 ;
653654}
654655
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+
655693int getifaddrs (struct ifaddrs * * ifap )
656694{
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+
657702 int l_socket ;
658703 int l_result ;
659704 int l_numLinks ;
@@ -703,6 +748,14 @@ int getifaddrs(struct ifaddrs **ifap)
703748
704749void freeifaddrs (struct ifaddrs * ifa )
705750{
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+
706759 struct ifaddrs * l_cur ;
707760 while (ifa )
708761 {
0 commit comments