summaryrefslogtreecommitdiff
path: root/sysdep/bsd
diff options
context:
space:
mode:
Diffstat (limited to 'sysdep/bsd')
-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 69a476d9..176e11ed 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;
+}