diff options
Diffstat (limited to 'src/settings/plugins/ifnet/net_utils.c')
-rw-r--r-- | src/settings/plugins/ifnet/net_utils.c | 120 |
1 files changed, 47 insertions, 73 deletions
diff --git a/src/settings/plugins/ifnet/net_utils.c b/src/settings/plugins/ifnet/net_utils.c index be19134e96..c4302418ec 100644 --- a/src/settings/plugins/ifnet/net_utils.c +++ b/src/settings/plugins/ifnet/net_utils.c @@ -366,23 +366,21 @@ create_ip4_block (gchar * ip) ip_mask = g_strsplit (ip, "/", 0); length = g_strv_length (ip_mask); - if (!inet_pton (AF_INET, ip_mask[0], &tmp_ip4_addr)) + if (!nm_utils_ipaddr_valid (AF_INET, ip_mask[0])) goto error; - iblock->ip = tmp_ip4_addr; + iblock->ip = g_strdup (ip_mask[0]); prefix = ip_mask[1]; i = 0; while (i < length && g_ascii_isdigit (prefix[i])) i++; prefix[i] = '\0'; - iblock->netmask = nm_utils_ip4_prefix_to_netmask ((guint32) - atoi (ip_mask - [1])); + iblock->prefix = (guint32) atoi (ip_mask[1]); } else if (strstr (ip, "netmask")) { ip_mask = g_strsplit (ip, " ", 0); length = g_strv_length (ip_mask); - if (!inet_pton (AF_INET, ip_mask[0], &tmp_ip4_addr)) + if (!nm_utils_ipaddr_valid (AF_INET, ip_mask[0])) goto error; - iblock->ip = tmp_ip4_addr; + iblock->ip = g_strdup (ip_mask[0]); i = 0; while (i < length && !strstr (ip_mask[++i], "netmask")) ; while (i < length && ip_mask[++i][0] == '\0') ; @@ -390,7 +388,7 @@ create_ip4_block (gchar * ip) goto error; if (!inet_pton (AF_INET, ip_mask[i], &tmp_ip4_addr)) goto error; - iblock->netmask = tmp_ip4_addr; + iblock->prefix = nm_utils_ip4_netmask_to_prefix (tmp_ip4_addr); } else { g_slice_free (ip_block, iblock); if (!is_ip6_address (ip) && !strstr (ip, "dhcp")) @@ -403,26 +401,25 @@ error: if (!is_ip6_address (ip)) nm_log_warn (LOGD_SETTINGS, "Can't handle IPv4 address: %s", ip); g_strfreev (ip_mask); + g_free (iblock->ip); g_slice_free (ip_block, iblock); return NULL; } -static ip6_block * -create_ip6_block (gchar * ip) +static ip_block * +create_ip_block (gchar * ip) { - ip6_block *iblock = g_slice_new0 (ip6_block); + ip_block *iblock = g_slice_new0 (ip_block); gchar *dup_ip = g_strdup (ip); - struct in6_addr *tmp_ip6_addr = g_slice_new0 (struct in6_addr); gchar *prefix = NULL; if ((prefix = strstr (dup_ip, "/")) != NULL) { *prefix = '\0'; prefix++; } - if (!inet_pton (AF_INET6, dup_ip, tmp_ip6_addr)) { + if (!nm_utils_ipaddr_valid (AF_INET6, dup_ip)) goto error; - } - iblock->ip = tmp_ip6_addr; + iblock->ip = dup_ip; if (prefix) { errno = 0; iblock->prefix = strtol (prefix, NULL, 10); @@ -431,30 +428,26 @@ create_ip6_block (gchar * ip) } } else iblock->prefix = 64; - g_free (dup_ip); return iblock; error: if (!is_ip4_address (ip)) nm_log_warn (LOGD_SETTINGS, "Can't handle IPv6 address: %s", ip); - g_slice_free (ip6_block, iblock); - g_slice_free (struct in6_addr, tmp_ip6_addr); - + g_slice_free (ip_block, iblock); g_free (dup_ip); return NULL; } -static guint32 +static char * get_ip4_gateway (gchar * gateway) { gchar *tmp, *split; - guint32 tmp_ip4_addr; if (!gateway) - return 0; + return NULL; tmp = find_gateway_str (gateway); if (!tmp) { nm_log_warn (LOGD_SETTINGS, "Couldn't obtain gateway in \"%s\"", gateway); - return 0; + return NULL; } tmp = g_strdup (tmp); strip_string (tmp, ' '); @@ -464,43 +457,39 @@ get_ip4_gateway (gchar * gateway) if ((split = strstr (tmp, "\"")) != NULL) *split = '\0'; - if (!inet_pton (AF_INET, tmp, &tmp_ip4_addr)) + if (!nm_utils_ipaddr_valid (AF_INET, tmp)) goto error; - g_free (tmp); - return tmp_ip4_addr; + return tmp; error: if (!is_ip6_address (tmp)) nm_log_warn (LOGD_SETTINGS, "Can't handle IPv4 gateway: %s", tmp); g_free (tmp); - return 0; + return NULL; } -static struct in6_addr * +static char * get_ip6_next_hop (gchar * next_hop) { gchar *tmp; - struct in6_addr *tmp_ip6_addr = g_slice_new0 (struct in6_addr); if (!next_hop) - return 0; + return NULL; tmp = find_gateway_str (next_hop); if (!tmp) { nm_log_warn (LOGD_SETTINGS, "Couldn't obtain next_hop in \"%s\"", next_hop); - return 0; + return NULL; } tmp = g_strdup (tmp); strip_string (tmp, ' '); strip_string (tmp, '"'); g_strstrip (tmp); - if (!inet_pton (AF_INET6, tmp, tmp_ip6_addr)) + if (!nm_utils_ipaddr_valid (AF_INET6, tmp)) goto error; - g_free (tmp); - return tmp_ip6_addr; + return tmp; error: if (!is_ip4_address (tmp)) nm_log_warn (LOGD_SETTINGS, "Can't handle IPv6 next_hop: %s", tmp); g_free (tmp); - g_slice_free (struct in6_addr, tmp_ip6_addr); return NULL; } @@ -512,7 +501,7 @@ convert_ip4_config_block (const char *conn_name) guint length; guint i; gchar *ip; - guint32 def_gateway = 0; + char *def_gateway = NULL; const char *routes; ip_block *start = NULL, *current = NULL, *iblock = NULL; @@ -531,8 +520,8 @@ convert_ip4_config_block (const char *conn_name) iblock = create_ip4_block (ip); if (iblock == NULL) continue; - if (!iblock->gateway && def_gateway != 0) - iblock->gateway = def_gateway; + if (!iblock->next_hop && def_gateway != NULL) + iblock->next_hop = g_strdup (def_gateway); if (start == NULL) start = current = iblock; else { @@ -541,17 +530,18 @@ convert_ip4_config_block (const char *conn_name) } } g_strfreev (ipset); + g_free (def_gateway); return start; } -ip6_block * +ip_block * convert_ip6_config_block (const char *conn_name) { gchar **ipset; guint length; guint i; gchar *ip; - ip6_block *start = NULL, *current = NULL, *iblock = NULL; + ip_block *start = NULL, *current = NULL, *iblock = NULL; g_return_val_if_fail (conn_name != NULL, NULL); ipset = split_addresses (ifnet_get_data (conn_name, "config")); @@ -559,7 +549,7 @@ convert_ip6_config_block (const char *conn_name) for (i = 0; i < length; i++) { ip = ipset[i]; ip = strip_string (ip, '"'); - iblock = create_ip6_block (ip); + iblock = create_ip_block (ip); if (iblock == NULL) continue; if (start == NULL) @@ -595,7 +585,7 @@ convert_ip4_routes_block (const char *conn_name) iblock = create_ip4_block (ip); if (iblock == NULL) continue; - iblock->gateway = get_ip4_gateway (ip); + iblock->next_hop = get_ip4_gateway (ip); if (start == NULL) start = current = iblock; else { @@ -607,15 +597,14 @@ convert_ip4_routes_block (const char *conn_name) return start; } -ip6_block * +ip_block * convert_ip6_routes_block (const char *conn_name) { gchar **ipset; guint length; guint i; gchar *ip, *tmp_addr; - ip6_block *start = NULL, *current = NULL, *iblock = NULL; - struct in6_addr *tmp_ip6_addr; + ip_block *start = NULL, *current = NULL, *iblock = NULL; g_return_val_if_fail (conn_name != NULL, NULL); ipset = split_routes (ifnet_get_data (conn_name, "routes")); @@ -629,25 +618,17 @@ convert_ip6_routes_block (const char *conn_name) if (!is_ip6_address (tmp_addr)) continue; else { - tmp_ip6_addr = g_slice_new0 (struct in6_addr); - - if (inet_pton (AF_INET6, "::", tmp_ip6_addr)) { - iblock = g_slice_new0 (ip6_block); - iblock->ip = tmp_ip6_addr; - iblock->prefix = 128; - } else { - g_slice_free (struct in6_addr, - tmp_ip6_addr); - continue; - } + iblock = g_slice_new0 (ip_block); + iblock->ip = g_strdup ("::"); + iblock->prefix = 128; } } else - iblock = create_ip6_block (ip); + iblock = create_ip_block (ip); if (iblock == NULL) continue; iblock->next_hop = get_ip6_next_hop (ip); if (iblock->next_hop == NULL) { - destroy_ip6_block (iblock); + destroy_ip_block (iblock); continue; } if (start == NULL) @@ -664,20 +645,13 @@ convert_ip6_routes_block (const char *conn_name) void destroy_ip_block (ip_block * iblock) { + g_free (iblock->ip); + g_free (iblock->next_hop); g_slice_free (ip_block, iblock); } void -destroy_ip6_block (ip6_block * iblock) -{ - g_slice_free (struct in6_addr, iblock->ip); - g_slice_free (struct in6_addr, iblock->next_hop); - - g_slice_free (ip6_block, iblock); -} - -void -set_ip4_dns_servers (NMSettingIP4Config *s_ip4, const char *conn_name) +set_ip4_dns_servers (NMSettingIPConfig *s_ip4, const char *conn_name) { const char *dns_servers; gchar **server_list, *stripped; @@ -694,7 +668,7 @@ set_ip4_dns_servers (NMSettingIP4Config *s_ip4, const char *conn_name) length = g_strv_length (server_list); if (length) - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, TRUE, NULL); for (i = 0; i < length; i++) { g_strstrip (server_list[i]); @@ -705,14 +679,14 @@ set_ip4_dns_servers (NMSettingIP4Config *s_ip4, const char *conn_name) nm_log_warn (LOGD_SETTINGS, "ignored dns: %s\n", server_list[i]); continue; } - if (!nm_setting_ip4_config_add_dns (s_ip4, server_list[i])) + if (!nm_setting_ip_config_add_dns (s_ip4, server_list[i])) nm_log_warn (LOGD_SETTINGS, "warning: duplicate DNS server %s", server_list[i]); } g_strfreev (server_list); } void -set_ip6_dns_servers (NMSettingIP6Config *s_ip6, const char *conn_name) +set_ip6_dns_servers (NMSettingIPConfig *s_ip6, const char *conn_name) { const char *dns_servers; gchar **server_list, *stripped; @@ -730,7 +704,7 @@ set_ip6_dns_servers (NMSettingIP6Config *s_ip6, const char *conn_name) length = g_strv_length (server_list); if (length) - g_object_set (s_ip6, NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS, + g_object_set (s_ip6, NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, TRUE, NULL); for (i = 0; i < length; i++) { g_strstrip (server_list[i]); @@ -741,7 +715,7 @@ set_ip6_dns_servers (NMSettingIP6Config *s_ip6, const char *conn_name) nm_log_warn (LOGD_SETTINGS, "ignored dns: %s\n", server_list[i]); continue; } - if (!nm_setting_ip6_config_add_dns (s_ip6, server_list[i])) + if (!nm_setting_ip_config_add_dns (s_ip6, server_list[i])) nm_log_warn (LOGD_SETTINGS, "warning: duplicate DNS server %s", server_list[i]); } g_strfreev (server_list); |