static void get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { NMSettingIP4Config *setting = NM_SETTING_IP4_CONFIG (object); NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); switch (prop_id) { case PROP_METHOD: g_value_set_string (value, nm_setting_ip4_config_get_method (setting)); break; case PROP_DNS: g_value_set_boxed (value, priv->dns); break; case PROP_DNS_SEARCH: g_value_set_boxed (value, priv->dns_search); break; case PROP_ADDRESSES: nm_utils_ip4_addresses_to_gvalue (priv->addresses, value); break; case PROP_ROUTES: nm_utils_ip4_routes_to_gvalue (priv->routes, value); break; case PROP_IGNORE_AUTO_ROUTES: g_value_set_boolean (value, nm_setting_ip4_config_get_ignore_auto_routes (setting)); break; case PROP_IGNORE_AUTO_DNS: g_value_set_boolean (value, nm_setting_ip4_config_get_ignore_auto_dns (setting)); break; case PROP_DHCP_CLIENT_ID: g_value_set_string (value, nm_setting_ip4_config_get_dhcp_client_id (setting)); break; case PROP_DHCP_SEND_HOSTNAME: g_value_set_boolean (value, nm_setting_ip4_config_get_dhcp_send_hostname (setting)); break; case PROP_DHCP_HOSTNAME: g_value_set_string (value, nm_setting_ip4_config_get_dhcp_hostname (setting)); break; case PROP_NEVER_DEFAULT: g_value_set_boolean (value, priv->never_default); break; case PROP_MAY_FAIL: g_value_set_boolean (value, priv->may_fail); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static void get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (object); switch (prop_id) { case PROP_DHCP_CLIENT_ID: g_value_set_string (value, nm_setting_ip4_config_get_dhcp_client_id (s_ip4)); break; case PROP_DHCP_FQDN: g_value_set_string (value, nm_setting_ip4_config_get_dhcp_fqdn (s_ip4)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } }
static void add_ip4_config (GString *str, NMSettingIP4Config *s_ip4, const char *hostname) { if (s_ip4) { const char *tmp; tmp = nm_setting_ip4_config_get_dhcp_client_id (s_ip4); if (tmp) { gboolean is_octets = TRUE; const char *p = tmp; while (*p) { if (!g_ascii_isxdigit (*p) && (*p != ':')) { is_octets = FALSE; break; } p++; } /* If the client ID is just hex digits and : then don't use quotes, * because dhclient expects either a quoted ASCII string, or a byte * array formated as hex octets separated by : */ if (is_octets) g_string_append_printf (str, CLIENTID_FORMAT_OCTETS "\n", tmp); else g_string_append_printf (str, CLIENTID_FORMAT "\n", tmp); } } add_hostname (str, HOSTNAME4_FORMAT "\n", hostname); g_string_append_c (str, '\n'); /* Define options for classless static routes */ g_string_append (str, "option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;\n"); g_string_append (str, "option ms-classless-static-routes code 249 = array of unsigned integer 8;\n"); /* Web Proxy Auto-Discovery option (bgo #368423) */ g_string_append (str, "option wpad code 252 = string;\n"); g_string_append_c (str, '\n'); }
char * nm_dhcp_dhclient_create_config (const char *interface, NMSettingIP4Config *s_ip4, guint8 *anycast_addr, const char *hostname, const char *orig_path, const char *orig_contents) { GString *new_contents; GPtrArray *alsoreq; int i; new_contents = g_string_new (_("# Created by NetworkManager\n")); alsoreq = g_ptr_array_sized_new (5); if (orig_contents) { char **lines, **line; gboolean in_alsoreq = FALSE; g_string_append_printf (new_contents, _("# Merged from %s\n\n"), orig_path); lines = g_strsplit_set (orig_contents, "\n\r", 0); for (line = lines; lines && *line; line++) { char *p = *line; if (!strlen (g_strstrip (p))) continue; /* Override config file "dhcp-client-id" and use one from the * connection. */ if ( s_ip4 && nm_setting_ip4_config_get_dhcp_client_id (s_ip4) && !strncmp (p, CLIENTID_TAG, strlen (CLIENTID_TAG))) continue; /* Override config file hostname and use one from the connection */ if (hostname && !strncmp (p, HOSTNAME_TAG, strlen (HOSTNAME_TAG))) continue; /* Check for "also require" */ if (!strncmp (p, ALSOREQ_TAG, strlen (ALSOREQ_TAG))) { in_alsoreq = TRUE; p += strlen (ALSOREQ_TAG); } if (in_alsoreq) { char **areq, **aiter; /* Grab each 'also require' option and save for later */ areq = g_strsplit_set (p, "\t ,", -1); for (aiter = areq; aiter && *aiter; aiter++) { if (!strlen (g_strstrip (*aiter))) continue; if (*aiter[0] == ';') { /* all done */ in_alsoreq = FALSE; break; } if (!isalnum ((*aiter)[0])) continue; if ((*aiter)[strlen (*aiter) - 1] == ';') { /* Remove the EOL marker */ (*aiter)[strlen (*aiter) - 1] = '\0'; in_alsoreq = FALSE; } add_also_request (alsoreq, *aiter); } if (areq) g_strfreev (areq); continue; } /* Existing configuration line is OK, add it to new configuration */ g_string_append (new_contents, *line); g_string_append_c (new_contents, '\n'); } if (lines) g_strfreev (lines); } else g_string_append_c (new_contents, '\n'); /* Add NM options from connection */ if (s_ip4) { const char *tmp; gboolean added = FALSE; tmp = nm_setting_ip4_config_get_dhcp_client_id (s_ip4); if (tmp) { gboolean is_octets = TRUE; const char *p = tmp; while (*p) { if (!isxdigit (*p) && (*p != ':')) { is_octets = FALSE; break; } p++; } /* If the client ID is just hex digits and : then don't use quotes, * because dhclient expects either a quoted ASCII string, or a byte * array formated as hex octets separated by : */ if (is_octets) g_string_append_printf (new_contents, CLIENTID_FORMAT_OCTETS "\n", tmp); else g_string_append_printf (new_contents, CLIENTID_FORMAT "\n", tmp); added = TRUE; } if (hostname) { char *plain_hostname, *dot; plain_hostname = g_strdup (hostname); dot = strchr (plain_hostname, '.'); /* get rid of the domain */ if (dot) *dot = '\0'; g_string_append_printf (new_contents, HOSTNAME_FORMAT "\n", plain_hostname); added = TRUE; g_free (plain_hostname); } if (added) g_string_append_c (new_contents, '\n'); } /* Define options for classless static routes */ g_string_append (new_contents, "option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;\n"); g_string_append (new_contents, "option ms-classless-static-routes code 249 = array of unsigned integer 8;\n"); /* Web Proxy Auto-Discovery option (bgo #368423) */ g_string_append (new_contents, "option wpad code 252 = string;\n"); g_string_append_c (new_contents, '\n'); /* Everything we want to request from the DHCP server */ add_also_request (alsoreq, "rfc3442-classless-static-routes"); add_also_request (alsoreq, "ms-classless-static-routes"); add_also_request (alsoreq, "wpad"); add_also_request (alsoreq, "ntp-servers"); /* And add it to the dhclient configuration */ for (i = 0; i < alsoreq->len; i++) { char *t = g_ptr_array_index (alsoreq, i); g_string_append_printf (new_contents, "also request %s;\n", t); g_free (t); } g_ptr_array_free (alsoreq, TRUE); g_string_append_c (new_contents, '\n'); if (anycast_addr) { g_string_append_printf (new_contents, "interface \"%s\" {\n" " initial-interval 1; \n" " anycast-mac ethernet %02x:%02x:%02x:%02x:%02x:%02x;\n" "}\n", interface, anycast_addr[0], anycast_addr[1], anycast_addr[2], anycast_addr[3], anycast_addr[4], anycast_addr[5]); } return g_string_free (new_contents, FALSE); }
static void test_basic_import (NMVpnPluginUiInterface *plugin, const char *dir) { NMConnection *connection; NMSettingConnection *s_con; NMSettingIP4Config *s_ip4; NMSettingVPN *s_vpn; NMIP4Route *route; struct in_addr tmp; const char *expected_id = "Basic VPN"; const char *expected_route1_dest = "10.0.0.0"; const char *expected_route1_gw = "0.0.0.0"; const char *expected_route2_dest = "172.16.0.0"; const char *expected_route2_gw = "0.0.0.0"; connection = get_basic_connection ("basic-import", plugin, dir, "basic.pcf"); ASSERT (connection != NULL, "basic-import", "failed to import connection"); /* Connection setting */ s_con = nm_connection_get_setting_connection (connection); ASSERT (s_con != NULL, "basic-import", "missing 'connection' setting"); ASSERT (strcmp (nm_setting_connection_get_id (s_con), expected_id) == 0, "basic-import", "unexpected connection ID"); ASSERT (nm_setting_connection_get_uuid (s_con) == NULL, "basic-import", "unexpected valid UUID"); /* IP4 setting */ s_ip4 = nm_connection_get_setting_ip4_config (connection); ASSERT (s_ip4 != NULL, "basic-import", "missing 'ip4-config' setting"); ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == 0, "basic-import", "unexpected addresses"); ASSERT (nm_setting_ip4_config_get_never_default (s_ip4) == TRUE, "basic-import", "never-default unexpectedly FALSE"); ASSERT (nm_setting_ip4_config_get_method (s_ip4) == NULL, "basic-import", "unexpected IPv4 method"); ASSERT (nm_setting_ip4_config_get_dhcp_client_id (s_ip4) == NULL, "basic-import", "unexpected valid DHCP client ID"); ASSERT (nm_setting_ip4_config_get_dhcp_hostname (s_ip4) == NULL, "basic-import", "unexpected valid DHCP hostname"); ASSERT (nm_setting_ip4_config_get_num_dns_searches (s_ip4) == 0, "basic-import", "unexpected DNS searches"); ASSERT (nm_setting_ip4_config_get_num_dns (s_ip4) == 0, "basic-import", "unexpected DNS servers"); ASSERT (nm_setting_ip4_config_get_num_routes (s_ip4) == 2, "basic-import", "unexpected number of routes"); /* Route #1 */ route = nm_setting_ip4_config_get_route (s_ip4, 0); ASSERT (inet_pton (AF_INET, expected_route1_dest, &tmp) > 0, "basic-import", "couldn't convert expected route destination #1"); ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr, "basic-import", "unexpected route #1 destination"); ASSERT (inet_pton (AF_INET, expected_route1_gw, &tmp) > 0, "basic-import", "couldn't convert expected route next hop #1"); ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr, "basic-import", "unexpected route #1 next hop"); ASSERT (nm_ip4_route_get_prefix (route) == 8, "basic-import", "unexpected route #1 prefix"); ASSERT (nm_ip4_route_get_metric (route) == 0, "basic-import", "unexpected route #1 metric"); /* Route #2 */ route = nm_setting_ip4_config_get_route (s_ip4, 1); ASSERT (inet_pton (AF_INET, expected_route2_dest, &tmp) > 0, "basic-import", "couldn't convert expected route destination #2"); ASSERT (nm_ip4_route_get_dest (route) == tmp.s_addr, "basic-import", "unexpected route #2 destination"); ASSERT (inet_pton (AF_INET, expected_route2_gw, &tmp) > 0, "basic-import", "couldn't convert expected route next hop #2"); ASSERT (nm_ip4_route_get_next_hop (route) == tmp.s_addr, "basic-import", "unexpected route #2 next hop"); ASSERT (nm_ip4_route_get_prefix (route) == 16, "basic-import", "unexpected route #2 prefix"); ASSERT (nm_ip4_route_get_metric (route) == 0, "basic-import", "unexpected route #2 metric"); /* VPN setting */ s_vpn = nm_connection_get_setting_vpn (connection); ASSERT (s_vpn != NULL, "basic-import", "missing 'vpn' setting"); /* Data items */ test_items ("basic-import-data", s_vpn, &basic_items[0], FALSE); /* Secrets */ test_items ("basic-import-secrets", s_vpn, &basic_secrets[0], TRUE); g_object_unref (connection); }
static void populate_ui (CEPageIP4 *self) { CEPageIP4Private *priv = CE_PAGE_IP4_GET_PRIVATE (self); NMSettingIPConfig *setting = priv->setting; GtkListStore *store; GtkTreeIter model_iter; int method = IP4_METHOD_AUTO; GString *string = NULL; SetMethodInfo info; const char *str_method; int i; /* Method */ gtk_combo_box_set_active (priv->method, 0); str_method = nm_setting_ip_config_get_method (setting); if (str_method) { if (!strcmp (str_method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL)) method = IP4_METHOD_LINK_LOCAL; else if (!strcmp (str_method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) method = IP4_METHOD_MANUAL; else if (!strcmp (str_method, NM_SETTING_IP4_CONFIG_METHOD_SHARED)) method = IP4_METHOD_SHARED; else if (!strcmp (str_method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)) method = IP4_METHOD_DISABLED; } if (method == IP4_METHOD_AUTO && nm_setting_ip_config_get_ignore_auto_dns (setting)) method = IP4_METHOD_AUTO_ADDRESSES; info.method = method; info.combo = priv->method; gtk_tree_model_foreach (GTK_TREE_MODEL (priv->method_store), set_method, &info); /* Addresses */ store = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING); for (i = 0; i < nm_setting_ip_config_get_num_addresses (setting); i++) { NMIPAddress *addr = nm_setting_ip_config_get_address (setting, i); char buf[32]; if (!addr) { g_warning ("%s: empty IP4 Address structure!", __func__); continue; } snprintf (buf, sizeof (buf), "%u", nm_ip_address_get_prefix (addr)); gtk_list_store_append (store, &model_iter); gtk_list_store_set (store, &model_iter, COL_ADDRESS, nm_ip_address_get_address (addr), COL_PREFIX, buf, /* FIXME */ COL_GATEWAY, i == 0 ? nm_setting_ip_config_get_gateway (setting) : NULL, -1); } gtk_tree_view_set_model (priv->addr_list, GTK_TREE_MODEL (store)); g_signal_connect_swapped (store, "row-inserted", G_CALLBACK (ce_page_changed), self); g_signal_connect_swapped (store, "row-deleted", G_CALLBACK (ce_page_changed), self); g_object_unref (store); /* DNS servers */ string = g_string_new (""); for (i = 0; i < nm_setting_ip_config_get_num_dns (setting); i++) { const char *dns; dns = nm_setting_ip_config_get_dns (setting, i); if (!dns) continue; if (string->len) g_string_append (string, ", "); g_string_append (string, dns); } gtk_entry_set_text (priv->dns_servers, string->str); g_string_free (string, TRUE); /* DNS searches */ string = g_string_new (""); for (i = 0; i < nm_setting_ip_config_get_num_dns_searches (setting); i++) { if (string->len) g_string_append (string, ", "); g_string_append (string, nm_setting_ip_config_get_dns_search (setting, i)); } gtk_entry_set_text (priv->dns_searches, string->str); g_string_free (string, TRUE); if ((method == IP4_METHOD_AUTO) || (method == IP4_METHOD_AUTO_ADDRESSES)) { if (nm_setting_ip4_config_get_dhcp_client_id (NM_SETTING_IP4_CONFIG (setting))) { gtk_entry_set_text (priv->dhcp_client_id, nm_setting_ip4_config_get_dhcp_client_id (NM_SETTING_IP4_CONFIG (setting))); } } /* IPv4 required */ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->ip4_required), !nm_setting_ip_config_get_may_fail (setting)); }
static void populate_ui (CEPageIP4 *self) { CEPageIP4Private *priv = CE_PAGE_IP4_GET_PRIVATE (self); NMSettingIP4Config *setting = priv->setting; GtkListStore *store; GtkTreeIter model_iter; int method = IP4_METHOD_AUTO; GString *string = NULL; SetMethodInfo info; const char *str_method; int i; /* Method */ gtk_combo_box_set_active (priv->method, 0); str_method = nm_setting_ip4_config_get_method (setting); if (str_method) { if (!strcmp (str_method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL)) method = IP4_METHOD_LINK_LOCAL; else if (!strcmp (str_method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) method = IP4_METHOD_MANUAL; else if (!strcmp (str_method, NM_SETTING_IP4_CONFIG_METHOD_SHARED)) method = IP4_METHOD_SHARED; } if (method == IP4_METHOD_AUTO && nm_setting_ip4_config_get_ignore_auto_dns (setting)) method = IP4_METHOD_AUTO_ADDRESSES; info.method = method; info.combo = priv->method; gtk_tree_model_foreach (GTK_TREE_MODEL (priv->method_store), set_method, &info); /* Addresses */ store = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING); for (i = 0; i < nm_setting_ip4_config_get_num_addresses (setting); i++) { NMIP4Address *addr = nm_setting_ip4_config_get_address (setting, i); struct in_addr tmp_addr; char buf[INET_ADDRSTRLEN + 1]; const char *ignored; if (!addr) { g_warning ("%s: empty IP4 Address structure!", __func__); continue; } gtk_list_store_append (store, &model_iter); tmp_addr.s_addr = nm_ip4_address_get_address (addr); ignored = inet_ntop (AF_INET, &tmp_addr, &buf[0], sizeof (buf)); gtk_list_store_set (store, &model_iter, COL_ADDRESS, buf, -1); tmp_addr.s_addr = nm_utils_ip4_prefix_to_netmask (nm_ip4_address_get_prefix (addr)); ignored = inet_ntop (AF_INET, &tmp_addr, &buf[0], sizeof (buf)); gtk_list_store_set (store, &model_iter, COL_PREFIX, buf, -1); tmp_addr.s_addr = nm_ip4_address_get_gateway (addr); ignored = inet_ntop (AF_INET, &tmp_addr, &buf[0], sizeof (buf)); gtk_list_store_set (store, &model_iter, COL_GATEWAY, buf, -1); } gtk_tree_view_set_model (priv->addr_list, GTK_TREE_MODEL (store)); g_signal_connect_swapped (store, "row-inserted", G_CALLBACK (ce_page_changed), self); g_signal_connect_swapped (store, "row-deleted", G_CALLBACK (ce_page_changed), self); g_object_unref (store); /* DNS servers */ string = g_string_new (""); for (i = 0; i < nm_setting_ip4_config_get_num_dns (setting); i++) { struct in_addr tmp_addr; char buf[INET_ADDRSTRLEN + 1]; const char *ignored; tmp_addr.s_addr = nm_setting_ip4_config_get_dns (setting, i); if (!tmp_addr.s_addr) continue; ignored = inet_ntop (AF_INET, &tmp_addr, &buf[0], sizeof (buf)); if (string->len) g_string_append (string, ", "); g_string_append (string, buf); } gtk_entry_set_text (priv->dns_servers, string->str); g_string_free (string, TRUE); /* DNS searches */ string = g_string_new (""); for (i = 0; i < nm_setting_ip4_config_get_num_dns_searches (setting); i++) { if (string->len) g_string_append (string, ", "); g_string_append (string, nm_setting_ip4_config_get_dns_search (setting, i)); } gtk_entry_set_text (priv->dns_searches, string->str); g_string_free (string, TRUE); if ((method == IP4_METHOD_AUTO) || (method = IP4_METHOD_AUTO_ADDRESSES)) { if (nm_setting_ip4_config_get_dhcp_client_id (setting)) { gtk_entry_set_text (priv->dhcp_client_id, nm_setting_ip4_config_get_dhcp_client_id (setting)); } } }
char * nm_dhcp_dhclient_create_config (const char *interface, gboolean is_ip6, NMSettingIP4Config *s_ip4, NMSettingIP6Config *s_ip6, guint8 *anycast_addr, const char *hostname, const char *orig_path, const char *orig_contents) { GString *new_contents; GPtrArray *alsoreq; int i; new_contents = g_string_new (_("# Created by NetworkManager\n")); alsoreq = g_ptr_array_sized_new (5); if (orig_contents) { char **lines, **line; gboolean in_alsoreq = FALSE; g_string_append_printf (new_contents, _("# Merged from %s\n\n"), orig_path); lines = g_strsplit_set (orig_contents, "\n\r", 0); for (line = lines; lines && *line; line++) { char *p = *line; if (!strlen (g_strstrip (p))) continue; /* Override config file "dhcp-client-id" and use one from the * connection. */ if ( s_ip4 && nm_setting_ip4_config_get_dhcp_client_id (s_ip4) && !strncmp (p, CLIENTID_TAG, strlen (CLIENTID_TAG))) continue; /* Override config file hostname and use one from the connection */ if (hostname) { if (strncmp (p, HOSTNAME4_TAG, strlen (HOSTNAME4_TAG)) == 0) continue; if (strncmp (p, HOSTNAME6_TAG, strlen (HOSTNAME6_TAG)) == 0) continue; } /* Ignore 'script' since we pass our own */ if (g_str_has_prefix (p, "script ")) continue; /* Check for "also require" */ if (!strncmp (p, ALSOREQ_TAG, strlen (ALSOREQ_TAG))) { in_alsoreq = TRUE; p += strlen (ALSOREQ_TAG); } if (in_alsoreq) { char **areq, **aiter; /* Grab each 'also require' option and save for later */ areq = g_strsplit_set (p, "\t ,", -1); for (aiter = areq; aiter && *aiter; aiter++) { if (!strlen (g_strstrip (*aiter))) continue; if (*aiter[0] == ';') { /* all done */ in_alsoreq = FALSE; break; } if (!g_ascii_isalnum ((*aiter)[0])) continue; if ((*aiter)[strlen (*aiter) - 1] == ';') { /* Remove the EOL marker */ (*aiter)[strlen (*aiter) - 1] = '\0'; in_alsoreq = FALSE; } add_also_request (alsoreq, *aiter); } if (areq) g_strfreev (areq); continue; } /* Existing configuration line is OK, add it to new configuration */ g_string_append (new_contents, *line); g_string_append_c (new_contents, '\n'); } if (lines) g_strfreev (lines); } else g_string_append_c (new_contents, '\n'); if (is_ip6) { add_ip6_config (new_contents, s_ip6, hostname); add_also_request (alsoreq, "dhcp6.name-servers"); add_also_request (alsoreq, "dhcp6.domain-search"); add_also_request (alsoreq, "dhcp6.client-id"); add_also_request (alsoreq, "dhcp6.server-id"); } else { add_ip4_config (new_contents, s_ip4, hostname); add_also_request (alsoreq, "rfc3442-classless-static-routes"); add_also_request (alsoreq, "ms-classless-static-routes"); add_also_request (alsoreq, "static-routes"); add_also_request (alsoreq, "wpad"); add_also_request (alsoreq, "ntp-servers"); } /* And add it to the dhclient configuration */ for (i = 0; i < alsoreq->len; i++) { char *t = g_ptr_array_index (alsoreq, i); g_string_append_printf (new_contents, "also request %s;\n", t); g_free (t); } g_ptr_array_free (alsoreq, TRUE); g_string_append_c (new_contents, '\n'); if (anycast_addr) { g_string_append_printf (new_contents, "interface \"%s\" {\n" " initial-interval 1; \n" " anycast-mac ethernet %02x:%02x:%02x:%02x:%02x:%02x;\n" "}\n", interface, anycast_addr[0], anycast_addr[1], anycast_addr[2], anycast_addr[3], anycast_addr[4], anycast_addr[5]); } return g_string_free (new_contents, FALSE); }