summaryrefslogtreecommitdiffstats
path: root/src/libsystemd-network
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-05-18 22:02:42 +0200
committerTom Gundersen <teg@jklm.no>2014-05-19 18:14:56 +0200
commit09bee74d7a5f266b175baa19892fa84a9da51d7f (patch)
tree75c11ceda996f9a098461ef41b0ee1de045d1ce2 /src/libsystemd-network
parentsd-login: add C API to query primary session of a user (diff)
downloadsystemd-09bee74d7a5f266b175baa19892fa84a9da51d7f.tar.xz
systemd-09bee74d7a5f266b175baa19892fa84a9da51d7f.zip
sd-dhcp-lease: move in_addr (de)serialization to shared network code
Diffstat (limited to 'src/libsystemd-network')
-rw-r--r--src/libsystemd-network/dhcp-lease-internal.h1
-rw-r--r--src/libsystemd-network/network-internal.c95
-rw-r--r--src/libsystemd-network/network-internal.h4
-rw-r--r--src/libsystemd-network/sd-dhcp-lease.c49
4 files changed, 105 insertions, 44 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;
}