]> git.decadent.org.uk Git - odhcp6c.git/blobdiff - src/ra.c
properly handle return codes
[odhcp6c.git] / src / ra.c
index b21bc5615e5acb65ea1938e742437a80bac22f98..09d5c1d9ce5985dd6300c4dca3d53ff3c0d60849 100644 (file)
--- a/src/ra.c
+++ b/src/ra.c
@@ -13,7 +13,6 @@
  */
 
 #include <fcntl.h>
-#include <ifaddrs.h>
 #include <stdio.h>
 #include <signal.h>
 #include <string.h>
@@ -161,19 +160,21 @@ static void ra_send_rs(int signal __attribute__((unused)))
 static int16_t pref_to_priority(uint8_t flags)
 {
        flags = (flags >> 3) & 0x03;
-       return (flags == 0x0) ? 1024 : (flags == 0x1) ? 512 :
-                       (flags == 0x3) ? 2048 : -1;
+       return (flags == 0x0) ? 512 : (flags == 0x1) ? 384 :
+                       (flags == 0x3) ? 640 : -1;
 }
 
 
-static void update_proc(const char *sect, const char *opt, uint32_t value)
+static int update_proc(const char *sect, const char *opt, uint32_t value)
 {
        char buf[64];
        snprintf(buf, sizeof(buf), "/proc/sys/net/ipv6/%s/%s/%s", sect, if_name, opt);
 
        int fd = open(buf, O_WRONLY);
-       write(fd, buf, snprintf(buf, sizeof(buf), "%u", value));
+       int ret = write(fd, buf, snprintf(buf, sizeof(buf), "%u", value));
        close(fd);
+
+       return ret;
 }
 
 
@@ -252,50 +253,41 @@ bool ra_process(void)
 {
        bool found = false;
        bool changed = false;
-       bool has_lladdr = !IN6_IS_ADDR_UNSPECIFIED(&lladdr);
        uint8_t buf[1500], cmsg_buf[128];
        struct nd_router_advert *adv = (struct nd_router_advert*)buf;
        struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT, 0, 0, IN6ADDR_ANY_INIT, 0, 0, 0, 0, 0, 0};
        const struct in6_addr any = IN6ADDR_ANY_INIT;
 
-       if (!has_lladdr) {
-               // Autodetect interface-id if not specified
-               struct ifaddrs *ifaddr, *ifa;
-
-               if (getifaddrs(&ifaddr) == 0) {
-                       for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
-                               struct sockaddr_in6 *addr;
+       if (IN6_IS_ADDR_UNSPECIFIED(&lladdr)) {
+               struct sockaddr_in6 addr = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, if_index};
+               socklen_t alen = sizeof(addr);
+               int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
 
-                               if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6)
-                                       continue;
-
-                               addr = (struct sockaddr_in6*)ifa->ifa_addr;
+               if (!connect(sock, (struct sockaddr*)&addr, sizeof(addr)) &&
+                               !getsockname(sock, (struct sockaddr*)&addr, &alen))
+                       lladdr = addr.sin6_addr;
 
-                               if (!IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr))
-                                       continue;
-
-                               if (!strcmp(ifa->ifa_name, if_name)) {
-                                       lladdr = addr->sin6_addr;
-                                       has_lladdr = true;
-                                       break;
-                               }
-                       }
-
-                       freeifaddrs(ifaddr);
-               }
+               close(sock);
        }
 
        while (true) {
                struct sockaddr_in6 from;
                struct iovec iov = {buf, sizeof(buf)};
-               struct msghdr msg = {&from, sizeof(from), &iov, 1,
-                               cmsg_buf, sizeof(cmsg_buf), 0};
+               struct msghdr msg = {
+                       .msg_name = (void *) &from,
+                       .msg_namelen = sizeof(from),
+                       .msg_iov = &iov,
+                       .msg_iovlen = 1,
+                       .msg_control = cmsg_buf,
+                       .msg_controllen = sizeof(cmsg_buf),
+                       .msg_flags = 0
+               };
 
                ssize_t len = recvmsg(sock, &msg, MSG_DONTWAIT);
                if (len <= 0)
                        break;
 
-               if (!has_lladdr)
+               if (IN6_IS_ADDR_UNSPECIFIED(&lladdr))
                        continue;
 
                int hlim = 0;