diff options
author | Ondrej Zajicek <santiago@crfreenet.org> | 2013-11-25 18:42:47 +0100 |
---|---|---|
committer | Ondrej Zajicek <santiago@crfreenet.org> | 2013-11-25 18:42:47 +0100 |
commit | 283c7dfada53a6dee6a8a17ecab492ffafd44b66 (patch) | |
tree | 5edfb9df61c3b625967f3c65317f27c5051a8a4d /sysdep/bsd | |
parent | 736e143fa50607fcd88132291e96089b899af979 (diff) | |
parent | 0bb4e37db317a1290bad24fe430cac6569a9bd8c (diff) |
Merge branch 'master' into add-path
Diffstat (limited to 'sysdep/bsd')
-rw-r--r-- | sysdep/bsd/krt-sock.c | 33 |
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; +} |