shared: let nm_utils_parse_inaddr_bin() return the detected address family

As we accept addr_family %AF_UNSPEC to detect the address family,
we also need to return it. Just returning the binary address without
the address family makes no sense.
This commit is contained in:
Thomas Haller 2018-09-17 14:18:47 +02:00
parent 58510ed566
commit 0b3197a3fd
5 changed files with 37 additions and 42 deletions

View file

@ -459,34 +459,25 @@ nm_utils_ip_is_site_local (int addr_family,
gboolean
nm_utils_parse_inaddr_bin (int addr_family,
const char *text,
int *out_addr_family,
gpointer out_addr)
{
NMIPAddr addrbin;
g_return_val_if_fail (text, FALSE);
if (addr_family == AF_UNSPEC)
if (addr_family == AF_UNSPEC) {
g_return_val_if_fail (!out_addr || out_addr_family, FALSE);
addr_family = strchr (text, ':') ? AF_INET6 : AF_INET;
else
} else
g_return_val_if_fail (NM_IN_SET (addr_family, AF_INET, AF_INET6), FALSE);
/* use a temporary variable @addrbin, to guarantee that @out_addr
* is only modified on success. */
if (inet_pton (addr_family, text, &addrbin) != 1)
return FALSE;
if (out_addr) {
switch (addr_family) {
case AF_INET:
*((in_addr_t *) out_addr) = addrbin.addr4;
break;
case AF_INET6:
*((struct in6_addr *) out_addr) = addrbin.addr6;
break;
default:
nm_assert_not_reached ();
}
}
NM_SET_OUT (out_addr_family, addr_family);
if (out_addr)
nm_ip_addr_set (addr_family, out_addr, &addrbin);
return TRUE;
}
@ -500,7 +491,7 @@ nm_utils_parse_inaddr (int addr_family,
nm_assert (!out_addr || !*out_addr);
if (!nm_utils_parse_inaddr_bin (addr_family, text, &addrbin))
if (!nm_utils_parse_inaddr_bin (addr_family, text, &addr_family, &addrbin))
return FALSE;
NM_SET_OUT (out_addr, g_strdup (inet_ntop (addr_family, &addrbin, addrstr_buf, sizeof (addrstr_buf))));
return TRUE;
@ -509,6 +500,7 @@ nm_utils_parse_inaddr (int addr_family,
gboolean
nm_utils_parse_inaddr_prefix_bin (int addr_family,
const char *text,
int *out_addr_family,
gpointer out_addr,
int *out_prefix)
{
@ -517,19 +509,14 @@ nm_utils_parse_inaddr_prefix_bin (int addr_family,
const char *slash;
const char *addrstr;
NMIPAddr addrbin;
int addr_len;
g_return_val_if_fail (text, FALSE);
if (addr_family == AF_UNSPEC)
if (addr_family == AF_UNSPEC) {
g_return_val_if_fail (!out_addr || out_addr_family, FALSE);
addr_family = strchr (text, ':') ? AF_INET6 : AF_INET;
if (addr_family == AF_INET)
addr_len = sizeof (in_addr_t);
else if (addr_family == AF_INET6)
addr_len = sizeof (struct in6_addr);
else
g_return_val_if_reached (FALSE);
} else
g_return_val_if_fail (NM_IN_SET (addr_family, AF_INET, AF_INET6), FALSE);
slash = strchr (text, '/');
if (slash)
@ -549,8 +536,9 @@ nm_utils_parse_inaddr_prefix_bin (int addr_family,
return FALSE;
}
NM_SET_OUT (out_addr_family, addr_family);
if (out_addr)
memcpy (out_addr, &addrbin, addr_len);
nm_ip_addr_set (addr_family, out_addr, &addrbin);
NM_SET_OUT (out_prefix, prefix);
return TRUE;
}
@ -564,7 +552,9 @@ nm_utils_parse_inaddr_prefix (int addr_family,
NMIPAddr addrbin;
char addrstr_buf[MAX (INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
if (!nm_utils_parse_inaddr_prefix_bin (addr_family, text, &addrbin, out_prefix))
nm_assert (!out_addr || !*out_addr);
if (!nm_utils_parse_inaddr_prefix_bin (addr_family, text, &addr_family, &addrbin, out_prefix))
return FALSE;
NM_SET_OUT (out_addr, g_strdup (inet_ntop (addr_family, &addrbin, addrstr_buf, sizeof (addrstr_buf))));
return TRUE;

View file

@ -86,16 +86,17 @@ typedef struct {
extern const NMIPAddr nm_ip_addr_zero;
static inline void
nm_ip_addr_set (int addr_family, gpointer dst, const NMIPAddr *src)
nm_ip_addr_set (int addr_family, gpointer dst, gconstpointer src)
{
nm_assert_addr_family (addr_family);
nm_assert (dst);
nm_assert (src);
if (addr_family != AF_INET6)
*((in_addr_t *) dst) = src->addr4;
else
*((struct in6_addr *) dst) = src->addr6;
memcpy (dst,
src,
(addr_family != AF_INET6)
? sizeof (in_addr_t)
: sizeof (struct in6_addr));
}
/*****************************************************************************/
@ -331,6 +332,7 @@ gboolean nm_utils_ip_is_site_local (int addr_family,
gboolean nm_utils_parse_inaddr_bin (int addr_family,
const char *text,
int *out_addr_family,
gpointer out_addr);
gboolean nm_utils_parse_inaddr (int addr_family,
@ -339,6 +341,7 @@ gboolean nm_utils_parse_inaddr (int addr_family,
gboolean nm_utils_parse_inaddr_prefix_bin (int addr_family,
const char *text,
int *out_addr_family,
gpointer out_addr,
int *out_prefix);

View file

@ -884,7 +884,7 @@ static_stage3_ip4_done (NMModemBroadband *self)
/* Fully fail if invalid IP address retrieved */
address_string = mm_bearer_ip_config_get_address (self->_priv.ipv4_config);
if ( !address_string
|| !nm_utils_parse_inaddr_bin (AF_INET, address_string, &address_network)) {
|| !nm_utils_parse_inaddr_bin (AF_INET, address_string, NULL, &address_network)) {
error = g_error_new (NM_DEVICE_ERROR,
NM_DEVICE_ERROR_INVALID_CONNECTION,
"(%s) retrieving IP4 configuration failed: invalid address given %s%s%s",
@ -896,7 +896,7 @@ static_stage3_ip4_done (NMModemBroadband *self)
/* Missing gateway not a hard failure */
gw_string = mm_bearer_ip_config_get_gateway (self->_priv.ipv4_config);
if ( gw_string
&& !nm_utils_parse_inaddr_bin (AF_INET, gw_string, &gw)) {
&& !nm_utils_parse_inaddr_bin (AF_INET, gw_string, NULL, &gw)) {
error = g_error_new (NM_DEVICE_ERROR,
NM_DEVICE_ERROR_INVALID_CONNECTION,
"(%s) retrieving IP4 configuration failed: invalid gateway address \"%s\"",
@ -937,7 +937,7 @@ static_stage3_ip4_done (NMModemBroadband *self)
/* DNS servers */
dns = mm_bearer_ip_config_get_dns (self->_priv.ipv4_config);
for (i = 0; dns && dns[i]; i++) {
if ( nm_utils_parse_inaddr_bin (AF_INET, dns[i], &address_network)
if ( nm_utils_parse_inaddr_bin (AF_INET, dns[i], NULL, &address_network)
&& address_network > 0) {
nm_ip4_config_add_nameserver (config, address_network);
_LOGI (" DNS %s", dns[i]);

View file

@ -883,7 +883,7 @@ context_property_changed (GDBusProxy *proxy,
goto out;
}
if ( !s
|| !nm_utils_parse_inaddr_bin (AF_INET, s, &address_network)) {
|| !nm_utils_parse_inaddr_bin (AF_INET, s, NULL, &address_network)) {
_LOGW ("can't convert 'Address' %s to addr", s ?: "");
goto out;
}
@ -897,7 +897,7 @@ context_property_changed (GDBusProxy *proxy,
goto out;
}
if ( !s
|| !nm_utils_parse_inaddr_bin (AF_INET, s, &address_network)) {
|| !nm_utils_parse_inaddr_bin (AF_INET, s, NULL, &address_network)) {
_LOGW ("invalid 'Netmask': %s", s ?: "");
goto out;
}
@ -911,7 +911,7 @@ context_property_changed (GDBusProxy *proxy,
_LOGW ("Settings 'Gateway' missing");
goto out;
}
if (!nm_utils_parse_inaddr_bin (AF_INET, s, &gateway_network)) {
if (!nm_utils_parse_inaddr_bin (AF_INET, s, NULL, &gateway_network)) {
_LOGW ("invalid 'Gateway': %s", s);
goto out;
}
@ -938,7 +938,7 @@ context_property_changed (GDBusProxy *proxy,
}
if (array) {
for (iter = array; *iter; iter++) {
if ( nm_utils_parse_inaddr_bin (AF_INET, *iter, &address_network)
if ( nm_utils_parse_inaddr_bin (AF_INET, *iter, NULL, &address_network)
&& address_network) {
_LOGI ("DNS: %s", *iter);
nm_ip4_config_add_nameserver (priv->ip4_config, address_network);
@ -958,7 +958,7 @@ context_property_changed (GDBusProxy *proxy,
if (g_variant_lookup (v_dict, "MessageProxy", "&s", &s)) {
_LOGI ("MessageProxy: %s", s);
if ( s
&& nm_utils_parse_inaddr_bin (AF_INET, s, &address_network)) {
&& nm_utils_parse_inaddr_bin (AF_INET, s, NULL, &address_network)) {
nm_modem_get_route_parameters (NM_MODEM (self),
&ip4_route_table,
&ip4_route_metric,

View file

@ -873,7 +873,7 @@ parse_route_line (const char *line,
};
nm_assert (line);
nm_assert (NM_IN_SET (addr_family, AF_INET, AF_INET6));
nm_assert_addr_family (addr_family);
nm_assert (!options_route || nm_ip_route_get_family (options_route) == addr_family);
/* initscripts read the legacy route file line-by-line and
@ -1018,6 +1018,7 @@ parse_line_type_addr_with_prefix:
if (info->type == PARSE_LINE_TYPE_ADDR) {
if (!nm_utils_parse_inaddr_bin (addr_family,
s,
NULL,
&info->v.addr.addr)) {
if ( info == &infos[PARSE_LINE_ATTR_ROUTE_VIA]
&& nm_streq (s, "(null)")) {
@ -1045,6 +1046,7 @@ parse_line_type_addr_with_prefix:
prefix = 0;
} else if (!nm_utils_parse_inaddr_prefix_bin (addr_family,
s,
NULL,
&info->v.addr.addr,
&prefix)) {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,