1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-24 21:34:08 +03:00

util: introduce 'struct in_addr_full' and its helper functions

This commit is contained in:
Yu Watanabe 2020-07-13 07:09:55 +09:00
parent a723fb85da
commit 408f8fbc0f
2 changed files with 82 additions and 0 deletions

View File

@ -454,3 +454,70 @@ int in_addr_port_ifindex_name_from_string_auto(
return r;
}
struct in_addr_full *in_addr_full_free(struct in_addr_full *a) {
if (!a)
return NULL;
free(a->server_name);
free(a->cached_server_string);
return mfree(a);
}
int in_addr_full_new(int family, union in_addr_union *a, uint16_t port, int ifindex, const char *server_name, struct in_addr_full **ret) {
_cleanup_free_ char *name = NULL;
struct in_addr_full *x;
assert(ret);
if (!isempty(server_name)) {
name = strdup(server_name);
if (!name)
return -ENOMEM;
}
x = new(struct in_addr_full, 1);
if (!x)
return -ENOMEM;
*x = (struct in_addr_full) {
.family = family,
.address = *a,
.port = port,
.ifindex = ifindex,
.server_name = TAKE_PTR(name),
};
*ret = x;
return 0;
}
int in_addr_full_new_from_string(const char *s, struct in_addr_full **ret) {
_cleanup_free_ char *server_name = NULL;
int family, ifindex, r;
union in_addr_union a;
uint16_t port;
assert(s);
r = in_addr_port_ifindex_name_from_string_auto(s, &family, &a, &port, &ifindex, &server_name);
if (r < 0)
return r;
return in_addr_full_new(family, &a, port, ifindex, server_name, ret);
}
const char *in_addr_full_to_string(struct in_addr_full *a) {
assert(a);
if (!a->cached_server_string)
(void) in_addr_port_ifindex_name_to_string(
a->family,
&a->address,
a->port,
a->ifindex,
a->server_name,
&a->cached_server_string);
return a->cached_server_string;
}

View File

@ -33,3 +33,18 @@ static inline int in_addr_ifindex_name_from_string_auto(const char *s, int *fami
static inline int in_addr_ifindex_from_string_auto(const char *s, int *family, union in_addr_union *ret, int *ifindex) {
return in_addr_ifindex_name_from_string_auto(s, family, ret, ifindex, NULL);
}
struct in_addr_full {
int family;
union in_addr_union address;
uint16_t port;
int ifindex;
char *server_name;
char *cached_server_string; /* Should not be handled directly, but through in_addr_full_to_string(). */
};
struct in_addr_full *in_addr_full_free(struct in_addr_full *a);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct in_addr_full*, in_addr_full_free);
int in_addr_full_new(int family, union in_addr_union *a, uint16_t port, int ifindex, const char *server_name, struct in_addr_full **ret);
int in_addr_full_new_from_string(const char *s, struct in_addr_full **ret);
const char *in_addr_full_to_string(struct in_addr_full *a);