]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
networkd: serialize the private options
authorAlex Crawford <alex.crawford@coreos.com>
Sat, 1 Aug 2015 03:18:51 +0000 (20:18 -0700)
committerAlex Crawford <alex.crawford@coreos.com>
Wed, 5 Aug 2015 16:00:57 +0000 (09:00 -0700)
Save the private options along side the rest of the options in the lease
files.

src/libsystemd-network/network-internal.c
src/libsystemd-network/network-internal.h
src/libsystemd-network/sd-dhcp-lease.c

index d579755cc81b15d17d58f76ee71c526366d9a50b..3d78bf8b35f2f9430aead4236e2cdf9d8c4b3225 100644 (file)
@@ -509,3 +509,30 @@ int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t
 
         return 0;
 }
+
+int serialize_dhcp_option(FILE *f, const char *key, const uint8_t *data, size_t size) {
+        _cleanup_free_ char *hex_buf = NULL;
+
+        assert(f);
+        assert(key);
+        assert(data);
+
+        hex_buf = hexmem(data, size);
+        if (hex_buf == NULL)
+                return -ENOMEM;
+
+        fprintf(f, "%s=%s\n", key, hex_buf);
+
+        return 0;
+}
+
+int deserialize_dhcp_option(uint8_t **data, size_t *data_len, const char *string) {
+        assert(data);
+        assert(data_len);
+        assert(string);
+
+        if (strlen(string) % 2)
+                return -EINVAL;
+
+        return unhexmem(string, strlen(string), (void **)data, data_len);
+}
index 06aba893ceba9fa62f2bc755ec4432bd0fe09f42..7aaecbb10d9c9c7beb521e0d59082f7bdffc7f6f 100644 (file)
@@ -74,3 +74,6 @@ struct sd_dhcp_route;
 
 void serialize_dhcp_routes(FILE *f, const char *key, struct sd_dhcp_route *routes, size_t size);
 int deserialize_dhcp_routes(struct sd_dhcp_route **ret, size_t *ret_size, size_t *ret_allocated, const char *string);
+
+int serialize_dhcp_option(FILE *f, const char *key, const uint8_t *data, size_t size);
+int deserialize_dhcp_option(uint8_t **data, size_t *data_len, const char *string);
index c6b4f0a3ae373872fc8d11fa53be450acdb1bd48..3d0c8ff890e2d7a058b06fe921c5b420b3cb4d46 100644 (file)
@@ -676,6 +676,7 @@ int dhcp_lease_new(sd_dhcp_lease **ret) {
 int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
+        struct sd_dhcp_raw_option *option;
         struct in_addr address;
         const struct in_addr *addresses;
         const uint8_t *client_id, *data;
@@ -777,6 +778,14 @@ int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
                 fprintf(f, "VENDOR_SPECIFIC=%s\n", option_hex);
         }
 
+        LIST_FOREACH(options, option, lease->private_options) {
+                char key[strlen("OPTION_000")];
+                snprintf(key, sizeof(key), "OPTION_%"PRIu8, option->tag);
+                r = serialize_dhcp_option(f, key, option->data, option->length);
+                if (r < 0)
+                        goto fail;
+        }
+
         r = fflush_and_check(f);
         if (r < 0)
                 goto fail;
@@ -801,9 +810,11 @@ int sd_dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) {
                             *server_address = NULL, *next_server = NULL,
                             *dns = NULL, *ntp = NULL, *mtu = NULL,
                             *routes = NULL, *client_id_hex = NULL,
-                            *vendor_specific_hex = NULL;
+                            *vendor_specific_hex = NULL,
+                            *options[DHCP_OPTION_PRIVATE_LAST -
+                                     DHCP_OPTION_PRIVATE_BASE + 1] = { NULL };
         struct in_addr addr;
-        int r;
+        int r, i;
 
         assert(lease_file);
         assert(ret);
@@ -827,6 +838,37 @@ int sd_dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) {
                            "ROUTES", &routes,
                            "CLIENTID", &client_id_hex,
                            "VENDOR_SPECIFIC", &vendor_specific_hex,
+                           "OPTION_224", &options[0],
+                           "OPTION_225", &options[1],
+                           "OPTION_226", &options[2],
+                           "OPTION_227", &options[3],
+                           "OPTION_228", &options[4],
+                           "OPTION_229", &options[5],
+                           "OPTION_230", &options[6],
+                           "OPTION_231", &options[7],
+                           "OPTION_232", &options[8],
+                           "OPTION_233", &options[9],
+                           "OPTION_234", &options[10],
+                           "OPTION_235", &options[11],
+                           "OPTION_236", &options[12],
+                           "OPTION_237", &options[13],
+                           "OPTION_238", &options[14],
+                           "OPTION_239", &options[15],
+                           "OPTION_240", &options[16],
+                           "OPTION_241", &options[17],
+                           "OPTION_242", &options[18],
+                           "OPTION_243", &options[19],
+                           "OPTION_244", &options[20],
+                           "OPTION_245", &options[21],
+                           "OPTION_246", &options[22],
+                           "OPTION_247", &options[23],
+                           "OPTION_248", &options[24],
+                           "OPTION_249", &options[25],
+                           "OPTION_250", &options[26],
+                           "OPTION_251", &options[27],
+                           "OPTION_252", &options[28],
+                           "OPTION_253", &options[29],
+                           "OPTION_254", &options[30],
                            NULL);
         if (r < 0) {
                 if (r == -ENOENT)
@@ -918,6 +960,22 @@ int sd_dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) {
                         return r;
         }
 
+        for (i = 0; i <= DHCP_OPTION_PRIVATE_LAST - DHCP_OPTION_PRIVATE_BASE; i++) {
+                uint8_t *data;
+                size_t len;
+
+                if (!options[i])
+                        continue;
+
+                r = deserialize_dhcp_option(&data, &len, options[i]);
+                if (r < 0)
+                        return r;
+
+                r = dhcp_lease_insert_private_option(lease, DHCP_OPTION_PRIVATE_BASE + i, data, len);
+                if (r < 0)
+                        return r;
+        }
+
         *ret = lease;
         lease = NULL;