From e237b28a4d4b17ab50182ac110f28594967e76dc Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Mon, 25 Nov 2013 01:21:39 +0100 Subject: Changes primary addr selection on BSD to respect SIOCGIFADDR ioctl() result. Thanks to Alexander V. Chernikov for the original patch. --- sysdep/bsd/krt-sock.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'sysdep/bsd/krt-sock.c') diff --git a/sysdep/bsd/krt-sock.c b/sysdep/bsd/krt-sock.c index 0bc29458..84ce9c60 100644 --- a/sysdep/bsd/krt-sock.c +++ b/sysdep/bsd/krt-sock.c @@ -1058,3 +1058,36 @@ kif_sys_shutdown(struct kif_proto *p) krt_buffer_release(&p->p); } + +struct ifa * +kif_get_primary_ip(struct iface *i) +{ +#ifndef IPV6 + static int fd = -1; + + if (fd < 0) + fd = socket(AF_INET, SOCK_DGRAM, 0); + + struct ifreq ifr; + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, i->name, IFNAMSIZ); + + int rv = ioctl(fd, SIOCGIFADDR, (char *) &ifr); + if (rv < 0) + return NULL; + + ip_addr addr; + struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr; + memcpy(&addr, &sin->sin_addr.s_addr, sizeof(ip_addr)); + ipa_ntoh(addr); + + struct ifa *a; + WALK_LIST(a, i->addrs) + { + if (ipa_equal(a->ip, addr)) + return a; + } +#endif + + return NULL; +} -- cgit v1.2.3