chiark / gitweb /
dhcp-lease: refactor lease parsing
[elogind.git] / src / libsystemd-network / sd-dhcp-lease.c
index f7a204a..2ec45ca 100644 (file)
@@ -36,6 +36,7 @@
 #include "dhcp-lease-internal.h"
 #include "sd-dhcp-lease.h"
 #include "sd-dhcp-client.h"
 #include "dhcp-lease-internal.h"
 #include "sd-dhcp-lease.h"
 #include "sd-dhcp-client.h"
+#include "network-internal.h"
 
 int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr) {
         assert_return(lease, -EINVAL);
 
 int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr) {
         assert_return(lease, -EINVAL);
@@ -72,6 +73,20 @@ int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, struct in_addr **addr, size_t *a
         return 0;
 }
 
         return 0;
 }
 
+int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, struct in_addr **addr, size_t *addr_size) {
+        assert_return(lease, -EINVAL);
+        assert_return(addr, -EINVAL);
+        assert_return(addr_size, -EINVAL);
+
+        if (lease->ntp_size) {
+                *addr_size = lease->ntp_size;
+                *addr = lease->ntp;
+        } else
+                return -ENOENT;
+
+        return 0;
+}
+
 int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname) {
         assert_return(lease, -EINVAL);
         assert_return(domainname, -EINVAL);
 int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname) {
         assert_return(lease, -EINVAL);
         assert_return(domainname, -EINVAL);
@@ -112,7 +127,10 @@ int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, struct in_addr *addr) {
         assert_return(lease, -EINVAL);
         assert_return(addr, -EINVAL);
 
         assert_return(lease, -EINVAL);
         assert_return(addr, -EINVAL);
 
-        addr->s_addr = lease->router;
+        if (lease->router != INADDR_ANY)
+                addr->s_addr = lease->router;
+        else
+                return -ENOENT;
 
         return 0;
 }
 
         return 0;
 }
@@ -156,113 +174,168 @@ sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) {
                 free(lease->hostname);
                 free(lease->domainname);
                 free(lease->dns);
                 free(lease->hostname);
                 free(lease->domainname);
                 free(lease->dns);
+                free(lease->ntp);
                 free(lease);
         }
 
         return NULL;
 }
 
                 free(lease);
         }
 
         return NULL;
 }
 
+static void lease_parse_u32(const uint8_t *option, size_t len, uint32_t *ret, uint32_t min) {
+        be32_t val;
+
+        assert(option);
+        assert(ret);
+
+        if (len == 4) {
+                memcpy(&val, option, 4);
+                *ret = be32toh(val);
+
+                if (*ret < min)
+                        *ret = min;
+        }
+}
+
+static void lease_parse_u16(const uint8_t *option, size_t len, uint16_t *ret, uint16_t min) {
+        be16_t val;
+
+        assert(option);
+        assert(ret);
+
+        if (len == 2) {
+                memcpy(&val, option, 2);
+                *ret = be16toh(val);
+
+                if (*ret < min)
+                        *ret = min;
+        }
+}
+
+static void lease_parse_be32(const uint8_t *option, size_t len, be32_t *ret) {
+        assert(option);
+        assert(ret);
+
+        if (len == 4)
+                memcpy(ret, option, 4);
+}
+
+static int lease_parse_string(const uint8_t *option, size_t len, char **ret) {
+        assert(option);
+        assert(ret);
+
+        if (len >= 1) {
+                char *string;
+
+                string = strndup((const char *)option, len);
+                if (!string)
+                        return -errno;
+
+                free(*ret);
+                *ret = string;
+        }
+
+        return 0;
+}
+
+static int lease_parse_in_addrs(const uint8_t *option, size_t len, struct in_addr **ret, size_t *ret_size) {
+        assert(option);
+        assert(ret);
+        assert(ret_size);
+
+        if (len && !(len % 4)) {
+                size_t size;
+                struct in_addr *addresses;
+
+                size = len / 4;
+
+                addresses = newdup(struct in_addr, option, size);
+                if (!addresses)
+                        return -ENOMEM;
+
+                free(*ret);
+                *ret = addresses;
+                *ret_size = size;
+        }
+
+        return 0;
+}
+
 int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option,
                               void *user_data) {
         sd_dhcp_lease *lease = user_data;
 int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option,
                               void *user_data) {
         sd_dhcp_lease *lease = user_data;
-        be32_t val;
+        int r;
+
+        assert(lease);
 
         switch(code) {
 
         case DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
 
         switch(code) {
 
         case DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
-                if (len == 4) {
-                        memcpy(&val, option, 4);
-                        lease->lifetime = be32toh(val);
-                }
+                lease_parse_u32(option, len, &lease->lifetime, 1);
 
                 break;
 
         case DHCP_OPTION_SERVER_IDENTIFIER:
 
                 break;
 
         case DHCP_OPTION_SERVER_IDENTIFIER:
-                if (len >= 4)
-                        memcpy(&lease->server_address, option, 4);
+                lease_parse_be32(option, len, &lease->server_address);
 
                 break;
 
         case DHCP_OPTION_SUBNET_MASK:
 
                 break;
 
         case DHCP_OPTION_SUBNET_MASK:
-                if (len >= 4)
-                        memcpy(&lease->subnet_mask, option, 4);
+                lease_parse_be32(option, len, &lease->subnet_mask);
 
                 break;
 
         case DHCP_OPTION_ROUTER:
 
                 break;
 
         case DHCP_OPTION_ROUTER:
-                if (len >= 4)
-                        memcpy(&lease->router, option, 4);
+                lease_parse_be32(option, len, &lease->router);
 
                 break;
 
         case DHCP_OPTION_DOMAIN_NAME_SERVER:
 
                 break;
 
         case DHCP_OPTION_DOMAIN_NAME_SERVER:
-                if (len && !(len % 4)) {
-                        unsigned i;
-
-                        lease->dns_size = len / 4;
+                r = lease_parse_in_addrs(option, len, &lease->dns, &lease->dns_size);
+                if (r < 0)
+                        return r;
 
 
-                        free(lease->dns);
-                        lease->dns = new0(struct in_addr, lease->dns_size);
-                        if (!lease->dns)
-                                return -ENOMEM;
+                break;
 
 
-                        for (i = 0; i < lease->dns_size; i++) {
-                                memcpy(&lease->dns[i].s_addr, option + 4 * i, 4);
-                        }
-                }
+        case DHCP_OPTION_NTP_SERVER:
+                r = lease_parse_in_addrs(option, len, &lease->ntp, &lease->ntp_size);
+                if (r < 0)
+                        return r;
 
                 break;
 
         case DHCP_OPTION_INTERFACE_MTU:
 
                 break;
 
         case DHCP_OPTION_INTERFACE_MTU:
-                if (len >= 2) {
-                        be16_t mtu;
-
-                        memcpy(&mtu, option, 2);
-                        lease->mtu = be16toh(mtu);
-
-                        if (lease->mtu < 68)
-                                lease->mtu = 0;
-                }
+                lease_parse_u16(option, len, &lease->mtu, 68);
 
                 break;
 
         case DHCP_OPTION_DOMAIN_NAME:
 
                 break;
 
         case DHCP_OPTION_DOMAIN_NAME:
-                if (len >= 1) {
-                        free(lease->domainname);
-                        lease->domainname = strndup((const char *)option, len);
-                }
+                r = lease_parse_string(option, len, &lease->domainname);
+                if (r < 0)
+                        return r;
 
                 break;
 
         case DHCP_OPTION_HOST_NAME:
 
                 break;
 
         case DHCP_OPTION_HOST_NAME:
-                if (len >= 1) {
-                        free(lease->hostname);
-                        lease->hostname = strndup((const char *)option, len);
-                }
+                r = lease_parse_string(option, len, &lease->hostname);
+                if (r < 0)
+                        return r;
 
                 break;
 
         case DHCP_OPTION_ROOT_PATH:
 
                 break;
 
         case DHCP_OPTION_ROOT_PATH:
-                if (len >= 1) {
-                        free(lease->root_path);
-                        lease->root_path = strndup((const char *)option, len);
-                }
+                r = lease_parse_string(option, len, &lease->root_path);
+                if (r < 0)
+                        return r;
 
                 break;
 
         case DHCP_OPTION_RENEWAL_T1_TIME:
 
                 break;
 
         case DHCP_OPTION_RENEWAL_T1_TIME:
-                if (len == 4) {
-                        memcpy(&val, option, 4);
-                        lease->t1 = be32toh(val);
-                }
+                lease_parse_u32(option, len, &lease->t1, 1);
 
                 break;
 
         case DHCP_OPTION_REBINDING_T2_TIME:
 
                 break;
 
         case DHCP_OPTION_REBINDING_T2_TIME:
-                if (len == 4) {
-                        memcpy(&val, option, 4);
-                        lease->t2 = be32toh(val);
-                }
+                lease_parse_u32(option, len, &lease->t2, 1);
 
                 break;
         }
 
                 break;
         }
@@ -271,25 +344,25 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option,
 }
 
 int dhcp_lease_new(sd_dhcp_lease **ret) {
 }
 
 int dhcp_lease_new(sd_dhcp_lease **ret) {
-        _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
+        sd_dhcp_lease *lease;
 
         lease = new0(sd_dhcp_lease, 1);
         if (!lease)
                 return -ENOMEM;
 
 
         lease = new0(sd_dhcp_lease, 1);
         if (!lease)
                 return -ENOMEM;
 
+        lease->router = INADDR_ANY;
         lease->n_ref = REFCNT_INIT;
 
         *ret = lease;
         lease->n_ref = REFCNT_INIT;
 
         *ret = lease;
-        lease = NULL;
-
         return 0;
 }
 
 int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
         return 0;
 }
 
 int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
-        char buf[INET_ADDRSTRLEN];
         struct in_addr address;
         struct in_addr address;
+        struct in_addr *addresses;
+        size_t addresses_size;
         const char *string;
         uint16_t mtu;
         int r;
         const char *string;
         uint16_t mtu;
         int r;
@@ -297,10 +370,6 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
         assert(lease);
         assert(lease_file);
 
         assert(lease);
         assert(lease_file);
 
-        r = mkdir_safe_label("/run/systemd/network/leases", 0755, 0, 0);
-        if (r < 0)
-                goto finish;
-
         r = fopen_temporary(lease_file, &f, &temp_path);
         if (r < 0)
                 goto finish;
         r = fopen_temporary(lease_file, &f, &temp_path);
         if (r < 0)
                 goto finish;
@@ -311,71 +380,40 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
         if (r < 0)
                 goto finish;
 
         if (r < 0)
                 goto finish;
 
-        string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
-        if (!string) {
-                r = -errno;
-                goto finish;
-        }
-
         fprintf(f,
                 "# This is private data. Do not parse.\n"
         fprintf(f,
                 "# This is private data. Do not parse.\n"
-                "ADDRESS=%s\n", string);
-
-        r = sd_dhcp_lease_get_router(lease, &address);
-        if (r < 0)
-                goto finish;
-
-        string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
-        if (!string) {
-                r = -errno;
-                goto finish;
-        }
-
-        fprintf(f,
-                "ROUTER=%s\n", string);
+                "ADDRESS=%s\n", inet_ntoa(address));
 
         r = sd_dhcp_lease_get_netmask(lease, &address);
         if (r < 0)
                 goto finish;
 
 
         r = sd_dhcp_lease_get_netmask(lease, &address);
         if (r < 0)
                 goto finish;
 
-        string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
-        if (!string) {
-                r = -errno;
-                goto finish;
-        }
+        fprintf(f, "NETMASK=%s\n", inet_ntoa(address));
 
 
-        fprintf(f,
-                "NETMASK=%s\n", string);
+        r = sd_dhcp_lease_get_router(lease, &address);
+        if (r >= 0)
+                fprintf(f, "ROUTER=%s\n", inet_ntoa(address));
 
         r = sd_dhcp_lease_get_server_identifier(lease, &address);
 
         r = sd_dhcp_lease_get_server_identifier(lease, &address);
-        if (r >= 0) {
-                string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
-                if (!string) {
-                        r = -errno;
-                        goto finish;
-                }
-
-                fprintf(f,
-                        "SERVER_ADDRESS=%s\n", string);
-        }
+        if (r >= 0)
+                fprintf(f, "SERVER_ADDRESS=%s\n",
+                        inet_ntoa(address));
 
         r = sd_dhcp_lease_get_next_server(lease, &address);
 
         r = sd_dhcp_lease_get_next_server(lease, &address);
-        if (r >= 0) {
-                string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
-                if (!string) {
-                        r = -errno;
-                        goto finish;
-                }
-
-                fprintf(f,
-                        "NEXT_SERVER=%s\n", string);
-        }
+        if (r >= 0)
+                fprintf(f, "NEXT_SERVER=%s\n", inet_ntoa(address));
 
         r = sd_dhcp_lease_get_mtu(lease, &mtu);
         if (r >= 0)
                 fprintf(f, "MTU=%" PRIu16 "\n", mtu);
 
 
         r = sd_dhcp_lease_get_mtu(lease, &mtu);
         if (r >= 0)
                 fprintf(f, "MTU=%" PRIu16 "\n", mtu);
 
-/* TODO: DNS. See resolv.conf writing in network-manager.c */
+        r = sd_dhcp_lease_get_dns(lease, &addresses, &addresses_size);
+        if (r >= 0)
+                serialize_in_addrs(f, "DNS", addresses, addresses_size);
+
+        r = sd_dhcp_lease_get_ntp(lease, &addresses, &addresses_size);
+        if (r >= 0)
+                serialize_in_addrs(f, "NTP", addresses, addresses_size);
 
         r = sd_dhcp_lease_get_domainname(lease, &string);
         if (r >= 0)
 
         r = sd_dhcp_lease_get_domainname(lease, &string);
         if (r >= 0)
@@ -410,7 +448,7 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
         _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
         _cleanup_free_ char *address = NULL, *router = NULL, *netmask = NULL,
                             *server_address = NULL, *next_server = NULL,
         _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
         _cleanup_free_ char *address = NULL, *router = NULL, *netmask = NULL,
                             *server_address = NULL, *next_server = NULL,
-                            *mtu = NULL;
+                            *dns = NULL, *ntp = NULL, *mtu = NULL;
         struct in_addr addr;
         int r;
 
         struct in_addr addr;
         int r;
 
@@ -427,6 +465,8 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
                            "NETMASK", &netmask,
                            "SERVER_IDENTIFIER", &server_address,
                            "NEXT_SERVER", &next_server,
                            "NETMASK", &netmask,
                            "SERVER_IDENTIFIER", &server_address,
                            "NEXT_SERVER", &next_server,
+                           "DNS", &dns,
+                           "NTP", &ntp,
                            "MTU", &mtu,
                            "DOMAINNAME", &lease->domainname,
                            "HOSTNAME", &lease->hostname,
                            "MTU", &mtu,
                            "DOMAINNAME", &lease->domainname,
                            "HOSTNAME", &lease->hostname,
@@ -446,11 +486,13 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
 
         lease->address = addr.s_addr;
 
 
         lease->address = addr.s_addr;
 
-        r = inet_pton(AF_INET, router, &addr);
-        if (r < 0)
-                return r;
+        if (router) {
+                r = inet_pton(AF_INET, router, &addr);
+                if (r < 0)
+                        return r;
 
 
-        lease->router = addr.s_addr;
+                lease->router = addr.s_addr;
+        }
 
         r = inet_pton(AF_INET, netmask, &addr);
         if (r < 0)
 
         r = inet_pton(AF_INET, netmask, &addr);
         if (r < 0)
@@ -474,6 +516,18 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
                 lease->next_server = addr.s_addr;
         }
 
                 lease->next_server = addr.s_addr;
         }
 
+        if (dns) {
+                r = deserialize_in_addrs(&lease->dns, &lease->dns_size, dns);
+                if (r < 0)
+                        return r;
+        }
+
+        if (ntp) {
+                r = deserialize_in_addrs(&lease->ntp, &lease->ntp_size, dns);
+                if (r < 0)
+                        return r;
+        }
+
         if (mtu) {
                 uint16_t u;
                 if (sscanf(mtu, "%" SCNu16, &u) > 0)
         if (mtu) {
                 uint16_t u;
                 if (sscanf(mtu, "%" SCNu16, &u) > 0)
@@ -485,3 +539,29 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
 
         return 0;
 }
 
         return 0;
 }
+
+int dhcp_lease_set_default_subnet_mask(sd_dhcp_lease *lease) {
+        uint32_t address;
+
+        assert(lease);
+        assert(lease->address != INADDR_ANY);
+
+        address = be32toh(lease->address);
+
+        /* fall back to the default subnet masks based on address class */
+
+        if ((address >> 31) == 0x0)
+                /* class A, leading bits: 0 */
+                lease->subnet_mask = htobe32(0xff000000);
+        else if ((address >> 30) == 0x2)
+                /* class B, leading bits 10 */
+                lease->subnet_mask = htobe32(0xffff0000);
+        else if ((address >> 29) == 0x6)
+                /* class C, leading bits 110 */
+                lease->subnet_mask = htobe32(0xffffff00);
+        else
+                /* class D or E, no default mask. give up */
+                return -ERANGE;
+
+        return 0;
+}