diff options
author | Tom Gundersen <teg@jklm.no> | 2014-05-18 22:02:42 +0200 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2014-05-19 18:14:56 +0200 |
commit | 09bee74d7a5f266b175baa19892fa84a9da51d7f (patch) | |
tree | 75c11ceda996f9a098461ef41b0ee1de045d1ce2 /src | |
parent | a077b666cb883213fd85be21b518c2de54cb70b1 (diff) |
sd-dhcp-lease: move in_addr (de)serialization to shared network code
Diffstat (limited to 'src')
-rw-r--r-- | src/libsystemd-network/dhcp-lease-internal.h | 1 | ||||
-rw-r--r-- | src/libsystemd-network/network-internal.c | 95 | ||||
-rw-r--r-- | src/libsystemd-network/network-internal.h | 4 | ||||
-rw-r--r-- | src/libsystemd-network/sd-dhcp-lease.c | 49 | ||||
-rw-r--r-- | src/network/test-network.c | 35 |
5 files changed, 139 insertions, 45 deletions
diff --git a/src/libsystemd-network/dhcp-lease-internal.h b/src/libsystemd-network/dhcp-lease-internal.h index ba2fd73b45..297bacbb78 100644 --- a/src/libsystemd-network/dhcp-lease-internal.h +++ b/src/libsystemd-network/dhcp-lease-internal.h @@ -26,6 +26,7 @@ #include <linux/if_packet.h> #include "refcnt.h" +#include "util.h" #include "dhcp-protocol.h" diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c index f3dc42f6dd..261603f841 100644 --- a/src/libsystemd-network/network-internal.c +++ b/src/libsystemd-network/network-internal.c @@ -361,3 +361,98 @@ int load_module(struct kmod_ctx *ctx, const char *mod_name) { return r; } + +void serialize_in_addrs(FILE *f, const char *key, struct in_addr *addresses, size_t size) { + unsigned i; + + assert(f); + assert(key); + assert(addresses); + assert(size); + + fprintf(f, "%s=", key); + + for (i = 0; i < size; i++) + fprintf(f, "%s%s", inet_ntoa(addresses[i]), + (i < (size - 1)) ? " ": ""); + + fputs("\n", f); +} + +int deserialize_in_addrs(struct in_addr **ret, size_t *ret_size, const char *string) { + _cleanup_free_ struct in_addr *addresses = NULL; + size_t size = 0; + char *word, *state; + size_t len; + + assert(ret); + assert(ret_size); + assert(string); + + FOREACH_WORD(word, len, string, state) { + _cleanup_free_ char *addr_str = NULL; + struct in_addr *new_addresses; + int r; + + new_addresses = realloc(addresses, (size + 1) * sizeof(struct in_addr)); + if (!new_addresses) + return -ENOMEM; + else + addresses = new_addresses; + + addr_str = strndup(word, len); + if (!addr_str) + return -ENOMEM; + + r = inet_pton(AF_INET, addr_str, &(addresses[size])); + if (r <= 0) + continue; + + size ++; + } + + *ret_size = size; + *ret = addresses; + addresses = NULL; + + return 0; +} + +int deserialize_in6_addrs(struct in6_addr **ret, size_t *ret_size, const char *string) { + _cleanup_free_ struct in6_addr *addresses = NULL; + size_t size = 0; + char *word, *state; + size_t len; + + assert(ret); + assert(ret_size); + assert(string); + + FOREACH_WORD(word, len, string, state) { + _cleanup_free_ char *addr_str = NULL; + struct in6_addr *new_addresses; + int r; + + new_addresses = realloc(addresses, (size + 1) * sizeof(struct in6_addr)); + if (!new_addresses) + return -ENOMEM; + else + addresses = new_addresses; + + addr_str = strndup(word, len); + if (!addr_str) + return -ENOMEM; + + r = inet_pton(AF_INET6, addr_str, &(addresses[size])); + if (r <= 0) + continue; + + size++; + } + + *ret_size = size; + *ret = addresses; + addresses = NULL; + + return 0; +} diff --git a/src/libsystemd-network/network-internal.h b/src/libsystemd-network/network-internal.h index 1c77d33274..c08cddd799 100644 --- a/src/libsystemd-network/network-internal.h +++ b/src/libsystemd-network/network-internal.h @@ -68,3 +68,7 @@ int net_parse_inaddr(const char *address, unsigned char *family, void *dst); int net_get_unique_predictable_data(struct udev_device *device, uint8_t result[8]); int load_module(struct kmod_ctx *ctx, const char *mod_name); + +void serialize_in_addrs(FILE *f, const char *key, struct in_addr *addresses, size_t size); +int deserialize_in_addrs(struct in_addr **addresses, size_t *size, const char *string); +int deserialize_in6_addrs(struct in6_addr **addresses, size_t *size, const char *string); diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c index 3e43ab635f..4993fe3641 100644 --- a/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/libsystemd-network/sd-dhcp-lease.c @@ -36,6 +36,7 @@ #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); @@ -307,46 +308,6 @@ int dhcp_lease_new(sd_dhcp_lease **ret) { return 0; } -static void serialize_addresses(FILE *f, const char *key, struct in_addr *addresses, size_t size) { - unsigned i; - - assert(key); - assert(addresses); - assert(size); - - fputs("DNS=", f); - - for (i = 0; i < size; i++) - fprintf(f, "%s%s", inet_ntoa(addresses[i]), - (i < (size - 1)) ? " ": ""); - - fputs("\n", f); -} - -static int deserialize_addresses(struct in_addr **addresses, size_t *size, const char *string) { - char *word, *state; - size_t len; - - FOREACH_WORD(word, len, string, state) { - struct in_addr *new_addresses; - int r; - - new_addresses = realloc(*addresses, (*size + 1) * sizeof(struct in_addr)); - if (!new_addresses) - return -ENOMEM; - else - *addresses = new_addresses; - - r = inet_aton(word, &(new_addresses[*size])); - if (r < 0) - continue; - - (*size)++; - } - - 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; @@ -399,11 +360,11 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) { r = sd_dhcp_lease_get_dns(lease, &addresses, &addresses_size); if (r >= 0) - serialize_addresses(f, "DNS", addresses, addresses_size); + serialize_in_addrs(f, "DNS", addresses, addresses_size); r = sd_dhcp_lease_get_ntp(lease, &addresses, &addresses_size); if (r >= 0) - serialize_addresses(f, "NTP", addresses, addresses_size); + serialize_in_addrs(f, "NTP", addresses, addresses_size); r = sd_dhcp_lease_get_domainname(lease, &string); if (r >= 0) @@ -507,13 +468,13 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) { } if (dns) { - r = deserialize_addresses(&lease->dns, &lease->dns_size, dns); + r = deserialize_in_addrs(&lease->dns, &lease->dns_size, dns); if (r < 0) return r; } if (ntp) { - r = deserialize_addresses(&lease->ntp, &lease->ntp_size, dns); + r = deserialize_in_addrs(&lease->ntp, &lease->ntp_size, dns); if (r < 0) return r; } diff --git a/src/network/test-network.c b/src/network/test-network.c index a0e04f8fd2..eb6ebe70b3 100644 --- a/src/network/test-network.c +++ b/src/network/test-network.c @@ -20,6 +20,38 @@ ***/ #include "networkd.h" +#include "network-internal.h" + +static void test_deserialize_in_addr(void) { + _cleanup_free_ struct in_addr *addresses = NULL; + _cleanup_free_ struct in6_addr *addresses6 = NULL; + struct in_addr a, b, c; + struct in6_addr d, e, f; + size_t size; + const char *addresses_string = "192.168.0.1 0:0:0:0:0:FFFF:204.152.189.116 192.168.0.2 ::1 192.168.0.3 1:0:0:0:0:0:0:8"; + + assert_se(inet_pton(AF_INET, "0:0:0:0:0:FFFF:204.152.189.116", &a) == 0); + assert_se(inet_pton(AF_INET6, "192.168.0.1", &d) == 0); + + assert_se(inet_pton(AF_INET, "192.168.0.1", &a) == 1); + assert_se(inet_pton(AF_INET, "192.168.0.2", &b) == 1); + assert_se(inet_pton(AF_INET, "192.168.0.3", &c) == 1); + assert_se(inet_pton(AF_INET6, "0:0:0:0:0:FFFF:204.152.189.116", &d) == 1); + assert_se(inet_pton(AF_INET6, "::1", &e) == 1); + assert_se(inet_pton(AF_INET6, "1:0:0:0:0:0:0:8", &f) == 1); + + assert_se(deserialize_in_addrs(&addresses, &size, addresses_string) >= 0); + assert_se(size == 3); + assert_se(!memcmp(&a, &addresses[0], sizeof(struct in_addr))); + assert_se(!memcmp(&b, &addresses[1], sizeof(struct in_addr))); + assert_se(!memcmp(&c, &addresses[2], sizeof(struct in_addr))); + + assert_se(deserialize_in6_addrs(&addresses6, &size, addresses_string) >= 0); + assert_se(size == 3); + assert_se(!memcmp(&d, &addresses6[0], sizeof(struct in6_addr))); + assert_se(!memcmp(&e, &addresses6[1], sizeof(struct in6_addr))); + assert_se(!memcmp(&f, &addresses6[2], sizeof(struct in6_addr))); +} static void test_load_config(Manager *manager) { /* TODO: should_reload, is false if the config dirs do not exist, so @@ -43,7 +75,7 @@ static void test_network_get(Manager *manager, struct udev_device *loopback) { } static void test_address_equality(void) { - Address *a1, *a2; + _cleanup_address_free_ Address *a1 = NULL, *a2 = NULL; assert_se(address_new_dynamic(&a1) >= 0); assert_se(address_new_dynamic(&a2) >= 0); @@ -92,6 +124,7 @@ int main(void) { struct udev *udev; struct udev_device *loopback; + test_deserialize_in_addr(); test_address_equality(); assert_se(manager_new(&manager) >= 0); |