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/libsystemd-network/network-internal.c | |
parent | a077b666cb883213fd85be21b518c2de54cb70b1 (diff) |
sd-dhcp-lease: move in_addr (de)serialization to shared network code
Diffstat (limited to 'src/libsystemd-network/network-internal.c')
-rw-r--r-- | src/libsystemd-network/network-internal.c | 95 |
1 files changed, 95 insertions, 0 deletions
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; +} |