summaryrefslogtreecommitdiff
path: root/sysdep/bsd/krt-sock.c
diff options
context:
space:
mode:
authorOndrej Zajicek <santiago@crfreenet.org>2013-11-25 01:21:39 +0100
committerOndrej Zajicek <santiago@crfreenet.org>2013-11-25 01:21:39 +0100
commite237b28a4d4b17ab50182ac110f28594967e76dc (patch)
tree6ddd5c6b7138ed0bd60a925859d76005f21447e6 /sysdep/bsd/krt-sock.c
parent5ebc92935cb58c78286d91f0831de94cd4ae0f9e (diff)
Changes primary addr selection on BSD to respect SIOCGIFADDR ioctl() result.
Thanks to Alexander V. Chernikov for the original patch.
Diffstat (limited to 'sysdep/bsd/krt-sock.c')
-rw-r--r--sysdep/bsd/krt-sock.c33
1 files changed, 33 insertions, 0 deletions
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;
+}