Commit 0b3197a3 authored by Thomas Haller's avatar Thomas Haller

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.
parent 58510ed5
...@@ -459,34 +459,25 @@ nm_utils_ip_is_site_local (int addr_family, ...@@ -459,34 +459,25 @@ nm_utils_ip_is_site_local (int addr_family,
gboolean gboolean
nm_utils_parse_inaddr_bin (int addr_family, nm_utils_parse_inaddr_bin (int addr_family,
const char *text, const char *text,
int *out_addr_family,
gpointer out_addr) gpointer out_addr)
{ {
NMIPAddr addrbin; NMIPAddr addrbin;
g_return_val_if_fail (text, FALSE); 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; 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); 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) if (inet_pton (addr_family, text, &addrbin) != 1)
return FALSE; return FALSE;
if (out_addr) { NM_SET_OUT (out_addr_family, addr_family);
switch (addr_family) { if (out_addr)
case AF_INET: nm_ip_addr_set (addr_family, out_addr, &addrbin);
*((in_addr_t *) out_addr) = addrbin.addr4;
break;
case AF_INET6:
*((struct in6_addr *) out_addr) = addrbin.addr6;
break;
default:
nm_assert_not_reached ();
}
}
return TRUE; return TRUE;
} }
...@@ -500,7 +491,7 @@ nm_utils_parse_inaddr (int addr_family, ...@@ -500,7 +491,7 @@ nm_utils_parse_inaddr (int addr_family,
nm_assert (!out_addr || !*out_addr); 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; return FALSE;
NM_SET_OUT (out_addr, g_strdup (inet_ntop (addr_family, &addrbin, addrstr_buf, sizeof (addrstr_buf)))); NM_SET_OUT (out_addr, g_strdup (inet_ntop (addr_family, &addrbin, addrstr_buf, sizeof (addrstr_buf))));
return TRUE; return TRUE;
...@@ -509,6 +500,7 @@ nm_utils_parse_inaddr (int addr_family, ...@@ -509,6 +500,7 @@ nm_utils_parse_inaddr (int addr_family,
gboolean gboolean
nm_utils_parse_inaddr_prefix_bin (int addr_family, nm_utils_parse_inaddr_prefix_bin (int addr_family,
const char *text, const char *text,
int *out_addr_family,
gpointer out_addr, gpointer out_addr,
int *out_prefix) int *out_prefix)
{ {
...@@ -517,19 +509,14 @@ nm_utils_parse_inaddr_prefix_bin (int addr_family, ...@@ -517,19 +509,14 @@ nm_utils_parse_inaddr_prefix_bin (int addr_family,
const char *slash; const char *slash;
const char *addrstr; const char *addrstr;
NMIPAddr addrbin; NMIPAddr addrbin;
int addr_len;
g_return_val_if_fail (text, FALSE); 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; addr_family = strchr (text, ':') ? AF_INET6 : AF_INET;
} else
if (addr_family == AF_INET) g_return_val_if_fail (NM_IN_SET (addr_family, AF_INET, AF_INET6), FALSE);
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);
slash = strchr (text, '/'); slash = strchr (text, '/');
if (slash) if (slash)
...@@ -549,8 +536,9 @@ nm_utils_parse_inaddr_prefix_bin (int addr_family, ...@@ -549,8 +536,9 @@ nm_utils_parse_inaddr_prefix_bin (int addr_family,
return FALSE; return FALSE;
} }
NM_SET_OUT (out_addr_family, addr_family);
if (out_addr) if (out_addr)
memcpy (out_addr, &addrbin, addr_len); nm_ip_addr_set (addr_family, out_addr, &addrbin);
NM_SET_OUT (out_prefix, prefix); NM_SET_OUT (out_prefix, prefix);
return TRUE; return TRUE;
} }
...@@ -564,7 +552,9 @@ nm_utils_parse_inaddr_prefix (int addr_family, ...@@ -564,7 +552,9 @@ nm_utils_parse_inaddr_prefix (int addr_family,
NMIPAddr addrbin; NMIPAddr addrbin;
char addrstr_buf[MAX (INET_ADDRSTRLEN, INET6_ADDRSTRLEN)]; 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; return FALSE;
NM_SET_OUT (out_addr, g_strdup (inet_ntop (addr_family, &addrbin, addrstr_buf, sizeof (addrstr_buf)))); NM_SET_OUT (out_addr, g_strdup (inet_ntop (addr_family, &addrbin, addrstr_buf, sizeof (addrstr_buf))));
return TRUE; return TRUE;
......
...@@ -86,16 +86,17 @@ typedef struct { ...@@ -86,16 +86,17 @@ typedef struct {
extern const NMIPAddr nm_ip_addr_zero; extern const NMIPAddr nm_ip_addr_zero;
static inline void 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_addr_family (addr_family);
nm_assert (dst); nm_assert (dst);
nm_assert (src); nm_assert (src);
if (addr_family != AF_INET6) memcpy (dst,
*((in_addr_t *) dst) = src->addr4; src,
else (addr_family != AF_INET6)
*((struct in6_addr *) dst) = src->addr6; ? sizeof (in_addr_t)
: sizeof (struct in6_addr));
} }
/*****************************************************************************/ /*****************************************************************************/
...@@ -331,6 +332,7 @@ gboolean nm_utils_ip_is_site_local (int addr_family, ...@@ -331,6 +332,7 @@ gboolean nm_utils_ip_is_site_local (int addr_family,
gboolean nm_utils_parse_inaddr_bin (int addr_family, gboolean nm_utils_parse_inaddr_bin (int addr_family,
const char *text, const char *text,
int *out_addr_family,
gpointer out_addr); gpointer out_addr);
gboolean nm_utils_parse_inaddr (int addr_family, gboolean nm_utils_parse_inaddr (int addr_family,
...@@ -339,6 +341,7 @@ 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, gboolean nm_utils_parse_inaddr_prefix_bin (int addr_family,
const char *text, const char *text,
int *out_addr_family,
gpointer out_addr, gpointer out_addr,
int *out_prefix); int *out_prefix);
......
...@@ -884,7 +884,7 @@ static_stage3_ip4_done (NMModemBroadband *self) ...@@ -884,7 +884,7 @@ static_stage3_ip4_done (NMModemBroadband *self)
/* Fully fail if invalid IP address retrieved */ /* Fully fail if invalid IP address retrieved */
address_string = mm_bearer_ip_config_get_address (self->_priv.ipv4_config); address_string = mm_bearer_ip_config_get_address (self->_priv.ipv4_config);
if ( !address_string 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, error = g_error_new (NM_DEVICE_ERROR,
NM_DEVICE_ERROR_INVALID_CONNECTION, NM_DEVICE_ERROR_INVALID_CONNECTION,
"(%s) retrieving IP4 configuration failed: invalid address given %s%s%s", "(%s) retrieving IP4 configuration failed: invalid address given %s%s%s",
...@@ -896,7 +896,7 @@ static_stage3_ip4_done (NMModemBroadband *self) ...@@ -896,7 +896,7 @@ static_stage3_ip4_done (NMModemBroadband *self)
/* Missing gateway not a hard failure */ /* Missing gateway not a hard failure */
gw_string = mm_bearer_ip_config_get_gateway (self->_priv.ipv4_config); gw_string = mm_bearer_ip_config_get_gateway (self->_priv.ipv4_config);
if ( gw_string 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, error = g_error_new (NM_DEVICE_ERROR,
NM_DEVICE_ERROR_INVALID_CONNECTION, NM_DEVICE_ERROR_INVALID_CONNECTION,
"(%s) retrieving IP4 configuration failed: invalid gateway address \"%s\"", "(%s) retrieving IP4 configuration failed: invalid gateway address \"%s\"",
...@@ -937,7 +937,7 @@ static_stage3_ip4_done (NMModemBroadband *self) ...@@ -937,7 +937,7 @@ static_stage3_ip4_done (NMModemBroadband *self)
/* DNS servers */ /* DNS servers */
dns = mm_bearer_ip_config_get_dns (self->_priv.ipv4_config); dns = mm_bearer_ip_config_get_dns (self->_priv.ipv4_config);
for (i = 0; dns && dns[i]; i++) { 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) { && address_network > 0) {
nm_ip4_config_add_nameserver (config, address_network); nm_ip4_config_add_nameserver (config, address_network);
_LOGI (" DNS %s", dns[i]); _LOGI (" DNS %s", dns[i]);
......
...@@ -883,7 +883,7 @@ context_property_changed (GDBusProxy *proxy, ...@@ -883,7 +883,7 @@ context_property_changed (GDBusProxy *proxy,
goto out; goto out;
} }
if ( !s 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 ?: ""); _LOGW ("can't convert 'Address' %s to addr", s ?: "");
goto out; goto out;
} }
...@@ -897,7 +897,7 @@ context_property_changed (GDBusProxy *proxy, ...@@ -897,7 +897,7 @@ context_property_changed (GDBusProxy *proxy,
goto out; goto out;
} }
if ( !s 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 ?: ""); _LOGW ("invalid 'Netmask': %s", s ?: "");
goto out; goto out;
} }
...@@ -911,7 +911,7 @@ context_property_changed (GDBusProxy *proxy, ...@@ -911,7 +911,7 @@ context_property_changed (GDBusProxy *proxy,
_LOGW ("Settings 'Gateway' missing"); _LOGW ("Settings 'Gateway' missing");
goto out; 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); _LOGW ("invalid 'Gateway': %s", s);
goto out; goto out;
} }
...@@ -938,7 +938,7 @@ context_property_changed (GDBusProxy *proxy, ...@@ -938,7 +938,7 @@ context_property_changed (GDBusProxy *proxy,
} }
if (array) { if (array) {
for (iter = array; *iter; iter++) { 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) { && address_network) {
_LOGI ("DNS: %s", *iter); _LOGI ("DNS: %s", *iter);
nm_ip4_config_add_nameserver (priv->ip4_config, address_network); nm_ip4_config_add_nameserver (priv->ip4_config, address_network);
...@@ -958,7 +958,7 @@ context_property_changed (GDBusProxy *proxy, ...@@ -958,7 +958,7 @@ context_property_changed (GDBusProxy *proxy,
if (g_variant_lookup (v_dict, "MessageProxy", "&s", &s)) { if (g_variant_lookup (v_dict, "MessageProxy", "&s", &s)) {
_LOGI ("MessageProxy: %s", s); _LOGI ("MessageProxy: %s", s);
if ( 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), nm_modem_get_route_parameters (NM_MODEM (self),
&ip4_route_table, &ip4_route_table,
&ip4_route_metric, &ip4_route_metric,
......
...@@ -873,7 +873,7 @@ parse_route_line (const char *line, ...@@ -873,7 +873,7 @@ parse_route_line (const char *line,
}; };
nm_assert (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); nm_assert (!options_route || nm_ip_route_get_family (options_route) == addr_family);
/* initscripts read the legacy route file line-by-line and /* initscripts read the legacy route file line-by-line and
...@@ -1018,6 +1018,7 @@ parse_line_type_addr_with_prefix: ...@@ -1018,6 +1018,7 @@ parse_line_type_addr_with_prefix:
if (info->type == PARSE_LINE_TYPE_ADDR) { if (info->type == PARSE_LINE_TYPE_ADDR) {
if (!nm_utils_parse_inaddr_bin (addr_family, if (!nm_utils_parse_inaddr_bin (addr_family,
s, s,
NULL,
&info->v.addr.addr)) { &info->v.addr.addr)) {
if ( info == &infos[PARSE_LINE_ATTR_ROUTE_VIA] if ( info == &infos[PARSE_LINE_ATTR_ROUTE_VIA]
&& nm_streq (s, "(null)")) { && nm_streq (s, "(null)")) {
...@@ -1045,6 +1046,7 @@ parse_line_type_addr_with_prefix: ...@@ -1045,6 +1046,7 @@ parse_line_type_addr_with_prefix:
prefix = 0; prefix = 0;
} else if (!nm_utils_parse_inaddr_prefix_bin (addr_family, } else if (!nm_utils_parse_inaddr_prefix_bin (addr_family,
s, s,
NULL,
&info->v.addr.addr, &info->v.addr.addr,
&prefix)) { &prefix)) {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment