From 9f6c81caa00e1470a5428cc200292df5384f8ba2 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 31 Oct 2014 11:42:53 -0500 Subject: sd-dhcp6-client: fix off-by-two error in DUID length The duid data passed by the client does not include the DUID type, but client->duid_len does account for the size of the DUID type. --- .../systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c index fa4f9b5dc2..dbec1a2a8b 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c @@ -200,19 +200,19 @@ int sd_dhcp6_client_set_duid(sd_dhcp6_client *client, uint16_t type, uint8_t *du switch (type) { case DHCP6_DUID_LLT: - if (duid_len <= sizeof(client->duid.llt)) + if (duid_len <= sizeof(client->duid.llt) - 2) return -EINVAL; break; case DHCP6_DUID_EN: - if (duid_len != sizeof(client->duid.en)) + if (duid_len != sizeof(client->duid.en) - 2) return -EINVAL; break; case DHCP6_DUID_LL: - if (duid_len <= sizeof(client->duid.ll)) + if (duid_len <= sizeof(client->duid.ll) - 2) return -EINVAL; break; case DHCP6_DUID_UUID: - if (duid_len != sizeof(client->duid.uuid)) + if (duid_len != sizeof(client->duid.uuid) - 2) return -EINVAL; break; default: @@ -222,7 +222,7 @@ int sd_dhcp6_client_set_duid(sd_dhcp6_client *client, uint16_t type, uint8_t *du client->duid.raw.type = htobe16(type); memcpy(&client->duid.raw.data, duid, duid_len); - client->duid_len = duid_len; + client->duid_len = duid_len + 2; /* +2 for sizeof(type) */ return 0; } -- cgit v1.2.1 From 7c9db27a2e9181feb70fcee9b43259f29be416b4 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Tue, 5 Aug 2014 17:51:39 -0500 Subject: sd-dhcp-client: support custom client IDs Non-ethernet interface types use different client identifier formats, plus when doing DHCPv4 and DHCPv6 on the same interface, the client identifier should be related per RFC 4361. Thus let the caller override the existing MAC-based client identifier if necessary. --- .../src/libsystemd-network/sd-dhcp-client.c | 112 ++++++++++++++++++--- .../systemd-dhcp/src/systemd/sd-dhcp-client.h | 5 + 2 files changed, 105 insertions(+), 12 deletions(-) diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c index 0eba4c379d..32476ece2d 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c @@ -38,6 +38,7 @@ #include "dhcp-lease-internal.h" #include "sd-dhcp-client.h" +#define MAX_CLIENT_ID_LEN 32 #define MAX_MAC_ADDR_LEN INFINIBAND_ALEN struct sd_dhcp_client { @@ -56,13 +57,33 @@ struct sd_dhcp_client { size_t req_opts_allocated; size_t req_opts_size; be32_t last_addr; - struct { - uint8_t type; - struct ether_addr mac_addr; - } _packed_ client_id; uint8_t mac_addr[MAX_MAC_ADDR_LEN]; size_t mac_addr_len; uint16_t arp_type; + union { + struct { + uint8_t type; /* 0: Generic (non-LL) (RFC 2132) */ + uint8_t data[MAX_CLIENT_ID_LEN]; + } _packed_ gen; + struct { + uint8_t type; /* 1: Ethernet Link-Layer (RFC 2132) */ + uint8_t haddr[ETH_ALEN]; + } _packed_ eth; + struct { + uint8_t type; /* 2 - 254: ARP/Link-Layer (RFC 2132) */ + uint8_t haddr[0]; + } _packed_ ll; + struct { + uint8_t type; /* 255: Node-specific (RFC 4361) */ + uint8_t iaid[4]; + uint8_t duid[MAX_CLIENT_ID_LEN - 4]; + } _packed_ ns; + struct { + uint8_t type; + uint8_t data[MAX_CLIENT_ID_LEN]; + } _packed_ raw; + } client_id; + size_t client_id_len; char *hostname; char *vendor_class_identifier; uint32_t mtu; @@ -201,8 +222,65 @@ int sd_dhcp_client_set_mac(sd_dhcp_client *client, const uint8_t *addr, client->mac_addr_len = addr_len; client->arp_type = arp_type; - memcpy(&client->client_id.mac_addr, addr, ETH_ALEN); - client->client_id.type = 0x01; + if (need_restart && client->state != DHCP_STATE_STOPPED) + sd_dhcp_client_start(client); + + return 0; +} + +const uint8_t *sd_dhcp_client_get_client_id(sd_dhcp_client *client, + uint8_t *type, + size_t *len) { + + assert_return(client, NULL); + assert_return(type, NULL); + assert_return(len, NULL); + + if (!client->client_id_len) + return NULL; + + *type = client->client_id.raw.type; + *len = client->client_id_len - 1; /* -1 for sizeof(type) */ + return (const uint8_t *) &client->client_id.raw.data; +} + +int sd_dhcp_client_set_client_id(sd_dhcp_client *client, uint8_t type, + const uint8_t *data, size_t data_len) { + DHCP_CLIENT_DONT_DESTROY(client); + bool need_restart = false; + + assert_return(client, -EINVAL); + assert_return(data, -EINVAL); + assert_return(data_len > 0 && data_len <= MAX_CLIENT_ID_LEN, -EINVAL); + + switch (type) { + case ARPHRD_ETHER: + if (data_len != ETH_ALEN) + return -EINVAL; + break; + case ARPHRD_INFINIBAND: + if (data_len != INFINIBAND_ALEN) + return -EINVAL; + break; + default: + break; + } + + if (client->client_id_len == data_len + 1 && + client->client_id.raw.type == type && + memcmp(&client->client_id.raw.data, data, data_len) == 0) + return 0; + + if (!IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_STOPPED)) { + log_dhcp_client(client, "Changing client ID on running DHCP " + "client, restarting"); + need_restart = true; + client_stop(client, DHCP_EVENT_STOP); + } + + client->client_id.raw.type = type; + memcpy(&client->client_id.raw.data, data, data_len); + client->client_id_len = data_len + 1; /* +1 for sizeof(type) */ if (need_restart && client->state != DHCP_STATE_STOPPED) sd_dhcp_client_start(client); @@ -369,14 +447,24 @@ static int client_message_init(sd_dhcp_client *client, DHCPPacket **ret, if (client->arp_type == ARPHRD_ETHER) memcpy(&packet->dhcp.chaddr, &client->mac_addr, ETH_ALEN); + /* If no client identifier exists, construct one from an ethernet + address if present */ + if (client->client_id_len == 0 && client->arp_type == ARPHRD_ETHER) { + client->client_id.eth.type = ARPHRD_ETHER; + memcpy(&client->client_id.eth.haddr, &client->mac_addr, ETH_ALEN); + client->client_id_len = sizeof (client->client_id.eth); + } + /* Some DHCP servers will refuse to issue an DHCP lease if the Client Identifier option is not set */ - r = dhcp_option_append(&packet->dhcp, optlen, &optoffset, 0, - DHCP_OPTION_CLIENT_IDENTIFIER, - sizeof(client->client_id), &client->client_id); - if (r < 0) - return r; - + if (client->client_id_len) { + r = dhcp_option_append(&packet->dhcp, optlen, &optoffset, 0, + DHCP_OPTION_CLIENT_IDENTIFIER, + client->client_id_len, + &client->client_id.raw); + if (r < 0) + return r; + } /* RFC2131 section 3.5: in its initial DHCPDISCOVER or DHCPREQUEST message, a diff --git a/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-client.h b/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-client.h index 7416f82193..953f6cddef 100644 --- a/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-client.h +++ b/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-client.h @@ -51,6 +51,11 @@ int sd_dhcp_client_set_request_broadcast(sd_dhcp_client *client, int broadcast); int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index); int sd_dhcp_client_set_mac(sd_dhcp_client *client, const uint8_t *addr, size_t addr_len, uint16_t arp_type); +int sd_dhcp_client_set_client_id(sd_dhcp_client *client, uint8_t type, + const uint8_t *data, size_t data_len); +const uint8_t *sd_dhcp_client_get_client_id(sd_dhcp_client *client, + uint8_t *type, + size_t *len); int sd_dhcp_client_set_mtu(sd_dhcp_client *client, uint32_t mtu); int sd_dhcp_client_set_hostname(sd_dhcp_client *client, const char *hostname); int sd_dhcp_client_set_vendor_class_identifier(sd_dhcp_client *client, const char *vci); -- cgit v1.2.1 From 0c14c88745eff99a4cb9b033369b5f40397bbf1d Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 31 Oct 2014 14:58:12 -0500 Subject: sd-dhcp-lease: load/save client ID The lease is intimately tied to the client ID in use, so when loading and renewing a lease, the same client ID should be used. --- .../src/libsystemd-network/dhcp-lease-internal.h | 5 ++ .../src/libsystemd-network/sd-dhcp-client.c | 16 ++++++ .../src/libsystemd-network/sd-dhcp-lease.c | 58 +++++++++++++++++++++- .../systemd-dhcp/src/systemd/sd-dhcp-lease.h | 2 + 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-lease-internal.h b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-lease-internal.h index d4675f3e47..79dee42386 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-lease-internal.h +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-lease-internal.h @@ -70,6 +70,8 @@ struct sd_dhcp_lease { char *domainname; char *hostname; char *root_path; + uint8_t *client_id; + size_t client_id_len; }; int dhcp_lease_new(sd_dhcp_lease **ret); @@ -81,5 +83,8 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret); int dhcp_lease_set_default_subnet_mask(sd_dhcp_lease *lease); +int dhcp_lease_set_client_id(sd_dhcp_lease *lease, const uint8_t *client_id, + size_t client_id_len); + DEFINE_TRIVIAL_CLEANUP_FUNC(sd_dhcp_lease*, sd_dhcp_lease_unref); #define _cleanup_dhcp_lease_unref_ _cleanup_(sd_dhcp_lease_unrefp) diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c index 32476ece2d..3077b06019 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c @@ -1032,6 +1032,14 @@ static int client_handle_offer(sd_dhcp_client *client, DHCPMessage *offer, if (r < 0) return r; + if (client->client_id_len) { + r = dhcp_lease_set_client_id(lease, + (uint8_t *) &client->client_id, + client->client_id_len); + if (r < 0) + return r; + } + r = dhcp_option_parse(offer, len, dhcp_lease_parse_options, lease); if (r != DHCP_OFFER) { log_dhcp_client(client, "received message was not an OFFER, ignoring"); @@ -1091,6 +1099,14 @@ static int client_handle_ack(sd_dhcp_client *client, DHCPMessage *ack, if (r < 0) return r; + if (client->client_id_len) { + r = dhcp_lease_set_client_id(lease, + (uint8_t *) &client->client_id, + client->client_id_len); + if (r < 0) + return r; + } + r = dhcp_option_parse(ack, len, dhcp_lease_parse_options, lease); if (r == DHCP_NAK) { log_dhcp_client(client, "NAK"); diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c index 4fb01c0729..347377148e 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c @@ -198,6 +198,7 @@ sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) { free(lease->dns); free(lease->ntp); free(lease->static_route); + free(lease->client_id); free(lease); } @@ -605,6 +606,8 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) { _cleanup_fclose_ FILE *f = NULL; struct in_addr address; const struct in_addr *addresses; + const uint8_t *client_id; + size_t client_id_len; const char *string; uint16_t mtu; struct sd_dhcp_route *routes; @@ -678,6 +681,18 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) { if (r >= 0) serialize_dhcp_routes(f, "ROUTES", routes, r); + r = sd_dhcp_lease_get_client_id(lease, &client_id, &client_id_len); + if (r >= 0) { + _cleanup_free_ char *client_id_hex; + + client_id_hex = hexmem (client_id, client_id_len); + if (!client_id_hex) { + r = -ENOMEM; + goto finish; + } + fprintf(f, "CLIENTID=%s\n", client_id_hex); + } + r = 0; fflush(f); @@ -699,7 +714,8 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) { _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL; _cleanup_free_ char *address = NULL, *router = NULL, *netmask = NULL, *server_address = NULL, *next_server = NULL, - *dns = NULL, *ntp = NULL, *mtu = NULL, *routes = NULL; + *dns = NULL, *ntp = NULL, *mtu = NULL, + *routes = NULL, *client_id_hex = NULL; struct in_addr addr; int r; @@ -723,6 +739,7 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) { "HOSTNAME", &lease->hostname, "ROOT_PATH", &lease->root_path, "ROUTES", &routes, + "CLIENTID", &client_id_hex, NULL); if (r < 0) { if (r == -ENOENT) @@ -797,6 +814,16 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) { return r; } + if (client_id_hex) { + if (strlen (client_id_hex) % 2) + return -EINVAL; + + lease->client_id = unhexmem (client_id_hex, strlen (client_id_hex)); + if (!lease->client_id) + return -ENOMEM; + lease->client_id_len = strlen (client_id_hex) / 2; + } + *ret = lease; lease = NULL; @@ -821,3 +848,32 @@ int dhcp_lease_set_default_subnet_mask(sd_dhcp_lease *lease) { return 0; } + +int sd_dhcp_lease_get_client_id(sd_dhcp_lease *lease, const uint8_t **client_id, + size_t *client_id_len) { + assert_return(lease, -EINVAL); + assert_return(client_id, -EINVAL); + assert_return(client_id_len, -EINVAL); + + *client_id = lease->client_id; + *client_id_len = lease->client_id_len; + return 0; +} + +int dhcp_lease_set_client_id(sd_dhcp_lease *lease, const uint8_t *client_id, + size_t client_id_len) { + assert_return(lease, -EINVAL); + assert_return((!client_id && !client_id_len) || + (client_id && client_id_len), -EINVAL); + + free (lease->client_id); + lease->client_id = NULL; + lease->client_id_len = 0; + + if (client_id) { + lease->client_id = memdup (client_id, client_id_len); + lease->client_id_len = client_id_len; + } + + return 0; +} diff --git a/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-lease.h b/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-lease.h index a3728a702f..5fafc04e3b 100644 --- a/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-lease.h +++ b/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-lease.h @@ -45,5 +45,7 @@ int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname); int sd_dhcp_lease_get_hostname(sd_dhcp_lease *lease, const char **hostname); int sd_dhcp_lease_get_root_path(sd_dhcp_lease *lease, const char **root_path); int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, struct sd_dhcp_route **routesgn); +int sd_dhcp_lease_get_client_id(sd_dhcp_lease *lease, const uint8_t **client_id, + size_t *client_id_len); #endif -- cgit v1.2.1 From d0bb1592f6c728630b2972918080710e97672468 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 31 Oct 2014 16:08:14 -0500 Subject: sd-dhcp-lease: expose load/save functions They're useful outside of networkd itself. --- .../systemd-dhcp/src/libsystemd-network/dhcp-lease-internal.h | 3 --- src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c | 4 ++-- src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-lease.h | 3 +++ 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-lease-internal.h b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-lease-internal.h index 79dee42386..375ded31a5 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-lease-internal.h +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-lease-internal.h @@ -78,9 +78,6 @@ int dhcp_lease_new(sd_dhcp_lease **ret); int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option, void *user_data); -int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file); -int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret); - int dhcp_lease_set_default_subnet_mask(sd_dhcp_lease *lease); int dhcp_lease_set_client_id(sd_dhcp_lease *lease, const uint8_t *client_id, diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c index 347377148e..ebd031bb03 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c @@ -601,7 +601,7 @@ int dhcp_lease_new(sd_dhcp_lease **ret) { return 0; } -int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) { +int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) { _cleanup_free_ char *temp_path = NULL; _cleanup_fclose_ FILE *f = NULL; struct in_addr address; @@ -710,7 +710,7 @@ finish: return r; } -int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) { +int sd_dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) { _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL; _cleanup_free_ char *address = NULL, *router = NULL, *netmask = NULL, *server_address = NULL, *next_server = NULL, diff --git a/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-lease.h b/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-lease.h index 5fafc04e3b..1b0207b1c4 100644 --- a/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-lease.h +++ b/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-lease.h @@ -48,4 +48,7 @@ int sd_dhcp_lease_get_routes(sd_dhcp_lease *lease, struct sd_dhcp_route **routes int sd_dhcp_lease_get_client_id(sd_dhcp_lease *lease, const uint8_t **client_id, size_t *client_id_len); +int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file); +int sd_dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret); + #endif -- cgit v1.2.1 From 389daa38f795adca98d3c4ca5efeff4a79f0590f Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Thu, 30 Oct 2014 13:59:00 -0500 Subject: sd-dhcp-client: clean up raw socket sd_event_source when creating UDP source The raw socket sd_event_source used for DHCP server solicitations was simply dropped on the floor when creating the new UDP socket after a lease has been acquired. Clean it up properly so we're not still listening and responding to events on it. --- src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c index 3077b06019..a8ec6542a1 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c @@ -1373,6 +1373,9 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message, if (r >= 0) { client->timeout_resend = sd_event_source_unref(client->timeout_resend); + client->receive_message = + sd_event_source_unref(client->receive_message); + client->fd = asynchronous_close(client->fd); if (IN_SET(client->state, DHCP_STATE_REQUESTING, DHCP_STATE_REBOOTING)) -- cgit v1.2.1 From 9c3dbb02682e244c37bfc54da6bb98cbf241b643 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 3 Nov 2014 22:31:46 -0600 Subject: sd-dhcp-client: fix REBOOT state handling client->secs wasn't getting set in the REBOOT state, causing an assertion. REBOOT should work the same way as INIT, per RFC 2131: secs 2 Filled in by client, seconds elapsed since client began address acquisition or renewal process. REBOOT is necessary because many DHCP servers (especially on home routers) do not hand back the same IP address when in response to a DISCOVER packet, even if the same client ID is used. --- .../src/libsystemd-network/sd-dhcp-client.c | 33 ++++++++++------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c index a8ec6542a1..1a54d3abee 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c @@ -89,7 +89,6 @@ struct sd_dhcp_client { uint32_t mtu; uint32_t xid; usec_t start_time; - uint16_t secs; unsigned int attempt; usec_t request_sent; sd_event_source *timeout_t1; @@ -399,15 +398,19 @@ static int client_message_init(sd_dhcp_client *client, DHCPPacket **ret, _cleanup_free_ DHCPPacket *packet; size_t optlen, optoffset, size; be16_t max_size; + usec_t time_now; + uint16_t secs; int r; assert(client); - assert(client->secs); + assert(client->start_time); assert(ret); assert(_optlen); assert(_optoffset); assert(type == DHCP_DISCOVER || type == DHCP_REQUEST); + /* See RFC2131 section 4.4.1 */ + optlen = DHCP_MIN_OPTIONS_SIZE; size = sizeof(DHCPPacket) + optlen; @@ -422,7 +425,15 @@ static int client_message_init(sd_dhcp_client *client, DHCPPacket **ret, /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers refuse to issue an DHCP lease if 'secs' is set to zero */ - packet->dhcp.secs = htobe16(client->secs); + r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now); + if (r < 0) + return r; + assert(time_now >= client->start_time); + + /* seconds between sending first and last DISCOVER + * must always be strictly positive to deal with broken servers */ + secs = ((time_now - client->start_time) / USEC_PER_SEC) ? : 1; + packet->dhcp.secs = htobe16(secs); /* RFC2132 section 4.1 A client that cannot receive unicast IP datagrams until its protocol @@ -529,24 +540,12 @@ static int dhcp_client_send_raw(sd_dhcp_client *client, DHCPPacket *packet, static int client_send_discover(sd_dhcp_client *client) { _cleanup_free_ DHCPPacket *discover = NULL; size_t optoffset, optlen; - usec_t time_now; int r; assert(client); assert(client->state == DHCP_STATE_INIT || client->state == DHCP_STATE_SELECTING); - /* See RFC2131 section 4.4.1 */ - - r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now); - if (r < 0) - return r; - assert(time_now >= client->start_time); - - /* seconds between sending first and last DISCOVER - * must always be strictly positive to deal with broken servers */ - client->secs = ((time_now - client->start_time) / USEC_PER_SEC) ? : 1; - r = client_message_init(client, &discover, DHCP_DISCOVER, &optlen, &optoffset); if (r < 0) @@ -963,10 +962,8 @@ static int client_start(sd_dhcp_client *client) { } client->fd = r; - if (client->state == DHCP_STATE_INIT) { + if (client->state == DHCP_STATE_INIT || client->state == DHCP_STATE_INIT_REBOOT) client->start_time = now(clock_boottime_or_monotonic()); - client->secs = 0; - } return client_initialize_events(client, client_receive_message_raw); } -- cgit v1.2.1 From 78539e5ad62fc0bc1c16fcaa777c6b58fd090832 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 29 Oct 2014 21:29:58 +0100 Subject: dhcp: fix compiler warning in IN_SET macro clang warns: dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c:120:24: error: duplicate 'const' declaration specifier [-Werror,-Wduplicate-decl-specifier] assert_return (IN_SET(client->state, DHCP_STATE_INIT, ^ ../src/dhcp-manager/systemd-dhcp/src/shared/macro.h:376:17: note: expanded from macro 'IN_SET' const typeof(_y) _x = (x); \ ^ ../src/dhcp-manager/systemd-dhcp/src/shared/macro.h:238:34: note: expanded from macro 'assert_return' if (_unlikely_(!(expr))) { \ ^ ../src/dhcp-manager/systemd-dhcp/src/shared/macro.h:42:44: note: expanded from macro '_unlikely_' #define _unlikely_(x) (__builtin_expect(!!(x),0)) ^ dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c:120:24: error: duplicate 'const' declaration specifier [-Werror,-Wduplicate-decl-specifier] ../src/dhcp-manager/systemd-dhcp/src/shared/macro.h:379:47: note: expanded from macro 'IN_SET' for (_i = 0; _i < 1 + sizeof((const typeof(_x)[]) { __VA_ARGS__ })/sizeof(const typeof(_x)); _i++) \ ^ ../src/dhcp-manager/systemd-dhcp/src/shared/macro.h:238:34: note: expanded from macro 'assert_return' if (_unlikely_(!(expr))) { \ ^ ../src/dhcp-manager/systemd-dhcp/src/shared/macro.h:42:44: note: expanded from macro '_unlikely_' #define _unlikely_(x) (__builtin_expect(!!(x),0)) ^ dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c:120:24: error: duplicate 'const' declaration specifier [-Werror,-Wduplicate-decl-specifier] ../src/dhcp-manager/systemd-dhcp/src/shared/macro.h:379:91: note: expanded from macro 'IN_SET' for (_i = 0; _i < 1 + sizeof((const typeof(_x)[]) { __VA_ARGS__ })/sizeof(const typeof(_x)); _i++) \ ^ ../src/dhcp-manager/systemd-dhcp/src/shared/macro.h:238:34: note: expanded from macro 'assert_return' if (_unlikely_(!(expr))) { \ ^ ../src/dhcp-manager/systemd-dhcp/src/shared/macro.h:42:44: note: expanded from macro '_unlikely_' #define _unlikely_(x) (__builtin_expect(!!(x),0)) ^ dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c:120:24: error: duplicate 'const' declaration specifier [-Werror,-Wduplicate-decl-specifier] ../src/dhcp-manager/systemd-dhcp/src/shared/macro.h:380:31: note: expanded from macro 'IN_SET' if (((const typeof(_x)[]) { _y, __VA_ARGS__ })[_i] == _x) { \ ^ ../src/dhcp-manager/systemd-dhcp/src/shared/macro.h:238:34: note: expanded from macro 'assert_return' if (_unlikely_(!(expr))) { \ ^ ../src/dhcp-manager/systemd-dhcp/src/shared/macro.h:42:44: note: expanded from macro '_unlikely_' #define _unlikely_(x) (__builtin_expect(!!(x),0)) ^ Signed-off-by: Thomas Haller --- src/dhcp-manager/systemd-dhcp/src/shared/macro.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dhcp-manager/systemd-dhcp/src/shared/macro.h b/src/dhcp-manager/systemd-dhcp/src/shared/macro.h index 9ee332c8df..ceef491268 100644 --- a/src/dhcp-manager/systemd-dhcp/src/shared/macro.h +++ b/src/dhcp-manager/systemd-dhcp/src/shared/macro.h @@ -373,11 +373,11 @@ do { \ #define IN_SET(x, y, ...) \ ({ \ const typeof(y) _y = (y); \ - const typeof(_y) _x = (x); \ + typeof(_y) _x = (x); \ unsigned _i; \ bool _found = false; \ - for (_i = 0; _i < 1 + sizeof((const typeof(_x)[]) { __VA_ARGS__ })/sizeof(const typeof(_x)); _i++) \ - if (((const typeof(_x)[]) { _y, __VA_ARGS__ })[_i] == _x) { \ + for (_i = 0; _i < 1 + sizeof((typeof(_x)[]) { __VA_ARGS__ })/sizeof(typeof(_x)); _i++) \ + if (((typeof(_x)[]) { _y, __VA_ARGS__ })[_i] == _x) { \ _found = true; \ break; \ } \ -- cgit v1.2.1 From 1b1222ffdf4d022e482c70af6d745cb8fb6c5fdf Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Tue, 22 Jul 2014 12:55:23 -0500 Subject: dhcp: make systemd DHCP code into a library The systemd code was modified to add "#if 0 /* NM_IGNORED */" around lines that cause problems for compilation or code that is not actually used in the library. An adaptation layer (nm-sd-adapt) was added for glue between systemd functions and NetworkManager, but changes to the actual systemd code have been kept to a minimum. The sd_event/sd_event_source functions of systemd have been re-implemented on top of the GLib main loop. --- src/dhcp-manager/systemd-dhcp/nm-sd-adapt.c | 208 +++++++++++++++++++++ src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h | 104 +++++++++++ .../src/libsystemd-network/dhcp-internal.h | 2 + .../src/libsystemd-network/dhcp-network.c | 2 + .../src/libsystemd-network/dhcp-option.c | 2 + .../src/libsystemd-network/dhcp-packet.c | 2 + .../src/libsystemd-network/dhcp6-internal.h | 2 + .../src/libsystemd-network/dhcp6-lease-internal.h | 2 + .../src/libsystemd-network/dhcp6-network.c | 2 + .../src/libsystemd-network/dhcp6-option.c | 2 + .../src/libsystemd-network/network-internal.c | 10 + .../src/libsystemd-network/network-internal.h | 4 + .../src/libsystemd-network/sd-dhcp-client.c | 2 + .../src/libsystemd-network/sd-dhcp-lease.c | 4 + .../src/libsystemd-network/sd-dhcp6-client.c | 12 ++ .../src/libsystemd-network/sd-dhcp6-lease.c | 2 + src/dhcp-manager/systemd-dhcp/src/shared/macro.h | 2 + src/dhcp-manager/systemd-dhcp/src/shared/strv.c | 2 + .../systemd-dhcp/src/shared/time-util.c | 9 + .../systemd-dhcp/src/shared/time-util.h | 2 + src/dhcp-manager/systemd-dhcp/src/shared/utf8.c | 2 + src/dhcp-manager/systemd-dhcp/src/shared/util.c | 45 +++++ src/dhcp-manager/systemd-dhcp/src/shared/util.h | 10 + 23 files changed, 434 insertions(+) create mode 100644 src/dhcp-manager/systemd-dhcp/nm-sd-adapt.c create mode 100644 src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h diff --git a/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.c b/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.c new file mode 100644 index 0000000000..dbc922e9eb --- /dev/null +++ b/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.c @@ -0,0 +1,208 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2014 Red Hat, Inc. + */ + +#include +#include +#include +#include + +#include "sd-event.h" +#include "time-util.h" + +struct sd_event_source { + guint refcount; + guint id; + gpointer user_data; + + GIOChannel *channel; + sd_event_io_handler_t io_cb; + + uint64_t usec; + sd_event_time_handler_t time_cb; +}; + +int +sd_event_source_set_priority (sd_event_source *s, int64_t priority) +{ + return 0; +} + +sd_event_source* +sd_event_source_unref (sd_event_source *s) +{ + + if (!s) + return NULL; + + g_return_val_if_fail (s->refcount, NULL); + + s->refcount--; + if (s->refcount == 0) { + if (s->id) + g_source_remove (s->id); + if (s->channel) { + /* Don't shut down the channel since systemd will soon close + * the file descriptor itself, which would cause -EBADF. + */ + g_io_channel_unref (s->channel); + } + g_free (s); + } + return NULL; +} + +int +sd_event_source_set_name(sd_event_source *s, const char *name) +{ + if (!s) + return -EINVAL; + + g_source_set_name_by_id (s->id, name); + return 0; +} + +static gboolean +io_ready (GIOChannel *channel, GIOCondition condition, struct sd_event_source *source) +{ + int r, revents = 0; + + if (condition & G_IO_IN) + revents |= EPOLLIN; + if (condition & G_IO_OUT) + revents |= EPOLLOUT; + if (condition & G_IO_PRI) + revents |= EPOLLPRI; + if (condition & G_IO_ERR) + revents |= EPOLLERR; + if (condition & G_IO_HUP) + revents |= EPOLLHUP; + + r = source->io_cb (source, g_io_channel_unix_get_fd (channel), revents, source->user_data); + if (r < 0) { + source->id = 0; + return G_SOURCE_REMOVE; + } + + return G_SOURCE_CONTINUE; +} + +int +sd_event_add_io (sd_event *e, sd_event_source **s, int fd, uint32_t events, sd_event_io_handler_t callback, void *userdata) +{ + struct sd_event_source *source; + GIOChannel *channel; + GIOCondition condition = 0; + + channel = g_io_channel_unix_new (fd); + if (!channel) + return -EINVAL; + + source = g_new0 (struct sd_event_source, 1); + source->refcount = 1; + source->io_cb = callback; + source->user_data = userdata; + source->channel = channel; + + if (events & EPOLLIN) + condition |= G_IO_IN; + if (events & EPOLLOUT) + condition |= G_IO_OUT; + if (events & EPOLLPRI) + condition |= G_IO_PRI; + if (events & EPOLLERR) + condition |= G_IO_ERR; + if (events & EPOLLHUP) + condition |= G_IO_HUP; + + g_io_channel_set_encoding (source->channel, NULL, NULL); + g_io_channel_set_buffered (source->channel, FALSE); + source->id = g_io_add_watch (source->channel, condition, (GIOFunc) io_ready, source); + + *s = source; + return 0; +} + +static gboolean +time_ready (struct sd_event_source *source) +{ + int r; + + r = source->time_cb (source, source->usec, source->user_data); + if (r < 0) { + source->id = 0; + return G_SOURCE_REMOVE; + } + + return G_SOURCE_CONTINUE; +} + +int +sd_event_add_time(sd_event *e, sd_event_source **s, clockid_t clock, uint64_t usec, uint64_t accuracy, sd_event_time_handler_t callback, void *userdata) +{ + struct sd_event_source *source; + uint64_t n = now (clock); + + source = g_new0 (struct sd_event_source, 1); + source->refcount = 1; + source->time_cb = callback; + source->user_data = userdata; + source->usec = usec; + + if (usec > 1000) + usec = n < usec - 1000 ? usec - n : 1000; + source->id = g_timeout_add (usec / 1000, (GSourceFunc) time_ready, source); + + *s = source; + return 0; +} + +/* sd_event is basically a GMainContext; but since we only + * ever use the default context, nothing to do here. + */ + +int +sd_event_default (sd_event **e) +{ + *e = GUINT_TO_POINTER (1); + return 0; +} + +sd_event* +sd_event_ref (sd_event *e) +{ + return e; +} + +sd_event* +sd_event_unref (sd_event *e) +{ + return NULL; +} + +int +sd_event_now (sd_event *e, clockid_t clock, uint64_t *usec) +{ + *usec = now (clock); + return 0; +} + +int asynchronous_close(int fd) { + safe_close(fd); + return -1; +} + diff --git a/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h b/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h new file mode 100644 index 0000000000..9282aaaf94 --- /dev/null +++ b/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h @@ -0,0 +1,104 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2014 Red Hat, Inc. + */ + +#ifndef NM_SD_ADAPT_H +#define NM_SD_ADAPT_H + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "nm-logging.h" + +static inline guint32 +_slog_level_to_nm (int slevel) +{ + switch (slevel) { + case LOG_DEBUG: return LOGL_DEBUG; + case LOG_WARNING: return LOGL_WARN; + case LOG_ERR: return LOGL_ERR; + case LOG_INFO: + case LOG_NOTICE: + default: return LOGL_INFO; + } +} + +#define log_meta(level, file, line, func, format, ...) \ +G_STMT_START { \ + guint32 _l = _slog_level_to_nm ((level)); \ + if (nm_logging_enabled (_l, LOGD_DHCP)) \ + _nm_log (#file ":" #line, func, LOGD_DHCP, _l, format, ## __VA_ARGS__); \ +} G_STMT_END + +#define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__) +#define log_error(...) log_full(LOG_ERR, __VA_ARGS__) +#define log_full(level, ...) log_meta((level), __FILE__, __LINE__, __func__, __VA_ARGS__); + +#define log_dhcp_client(client, fmt, ...) \ + log_meta(LOG_DEBUG, __FILE__, __LINE__, __func__, "DHCP CLIENT (0x%x): " fmt, client->xid, ##__VA_ARGS__) + +#define log_assert_failed(e, file, line, func) \ +G_STMT_START { \ + nm_log_err (LOGD_DHCP, #file ":" #line "(" #func "): assertion failed: " # e); \ + g_assert (FALSE); \ +} G_STMT_END + +#define log_assert_failed_unreachable(t, file, line, func) \ +G_STMT_START { \ + nm_log_err (LOGD_DHCP, #file ":" #line "(" #func "): assert unreachable: " # t); \ + g_assert_not_reached (); \ +} G_STMT_END + +#define log_assert_failed_return(e, file, line, func) \ + nm_log_err (LOGD_DHCP, #file ":" #line "(" #func "): assert return: " # e); \ + +#define log_oom nm_log_err(LOGD_CORE, "%s:%s/%s: OOM", __FILE__, __LINE__, __func__) + +/* Can't include both net/if.h and linux/if.h; so have to define this here */ +#ifndef IFNAMSIZ +#define IFNAMSIZ 16 +#endif + +#ifndef MAX_HANDLE_SZ +#define MAX_HANDLE_SZ 128 +#endif + +#define noreturn G_GNUC_NORETURN + +#include "sd-id128.h" +#include "sparse-endian.h" +#include "async.h" +#include "util.h" + +static inline pid_t gettid(void) { + return (pid_t) syscall(SYS_gettid); +} + +#endif /* NM_SD_ADAPT_H */ + diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-internal.h b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-internal.h index d358a49307..28c0e63454 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-internal.h +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-internal.h @@ -22,6 +22,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-network.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-network.c index 29e9993f66..26e6e9f3df 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-network.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-network.c @@ -17,6 +17,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-option.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-option.c index b6110c5f16..50ecf87383 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-option.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-option.c @@ -19,6 +19,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-packet.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-packet.c index 7581daeeeb..e6ebf86ae1 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-packet.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-packet.c @@ -18,6 +18,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-internal.h b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-internal.h index 6cc0aa8a8d..e29e2f0ee0 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-internal.h +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-internal.h @@ -21,6 +21,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-lease-internal.h b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-lease-internal.h index 109e0f4f21..9f7826c466 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-lease-internal.h +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-lease-internal.h @@ -22,6 +22,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include "refcnt.h" diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-network.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-network.c index fe56c10273..626a4d6a7f 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-network.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-network.c @@ -17,6 +17,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-option.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-option.c index e6a31778f4..c18fb2a660 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-option.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-option.c @@ -19,6 +19,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/network-internal.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/network-internal.c index 372f3ed371..f870e3bb42 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/network-internal.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/network-internal.c @@ -19,22 +19,31 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include #include +#if 0 /* NM_IGNORED */ #include "strv.h" #include "siphash24.h" #include "libudev-private.h" +#endif #include "dhcp-lease-internal.h" +#if 0 /* NM_IGNORED */ #include "log.h" #include "utf8.h" +#endif #include "util.h" +#if 0 /* NM_IGNORED */ #include "conf-parser.h" #include "condition.h" +#endif #include "network-internal.h" +#if 0 /* NM_IGNORED */ const char *net_get_name(struct udev_device *device) { const char *name, *field; @@ -295,6 +304,7 @@ int config_parse_hwaddr(const char *unit, return 0; } +#endif void serialize_in_addrs(FILE *f, const struct in_addr *addresses, size_t size) { unsigned i; diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/network-internal.h b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/network-internal.h index 49387d03cf..f4ff1dd4e8 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/network-internal.h +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/network-internal.h @@ -21,10 +21,13 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include +#if 0 /* NM_IGNORED */ #include "udev.h" #include "condition-util.h" @@ -62,6 +65,7 @@ int config_parse_ifalias(const char *unit, const char *filename, unsigned line, int net_get_unique_predictable_data(struct udev_device *device, uint8_t result[8]); const char *net_get_name(struct udev_device *device); +#endif void serialize_in_addrs(FILE *f, const struct in_addr *addresses, size_t size); int deserialize_in_addrs(struct in_addr **addresses, const char *string); diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c index 1a54d3abee..df8abdf3a3 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c @@ -17,6 +17,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c index ebd031bb03..4d078b4c8a 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c @@ -18,6 +18,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include @@ -28,7 +30,9 @@ #include "util.h" #include "list.h" +#if 0 /* NM_IGNORED */ #include "mkdir.h" +#endif #include "fileio.h" #include "in-addr-util.h" diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c index dbec1a2a8b..faf490e141 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c @@ -19,14 +19,18 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include #include +#if 0 /* NM_IGNORED */ #include "udev.h" #include "udev-util.h" #include "virt.h" +#endif #include "siphash24.h" #include "util.h" #include "refcnt.h" @@ -630,6 +634,7 @@ error: static int client_ensure_iaid(sd_dhcp6_client *client) { /* name is a pointer to memory in the udev_device struct, so must have the same scope */ +#if 0 /* NM_IGNORED */ _cleanup_udev_device_unref_ struct udev_device *device = NULL; const char *name = NULL; uint64_t id; @@ -671,6 +676,9 @@ static int client_ensure_iaid(sd_dhcp6_client *client) { client->ia_na.id = (id & 0xffffffff) ^ (id >> 32); return 0; +#else + return -1; +#endif } static int client_parse_message(sd_dhcp6_client *client, @@ -1199,8 +1207,10 @@ sd_dhcp6_client *sd_dhcp6_client_unref(sd_dhcp6_client *client) { int sd_dhcp6_client_new(sd_dhcp6_client **ret) { _cleanup_dhcp6_client_unref_ sd_dhcp6_client *client = NULL; +#if 0 /* NM_IGNORED */ sd_id128_t machine_id; int r; +#endif size_t t; assert_return(ret, -EINVAL); @@ -1217,6 +1227,7 @@ int sd_dhcp6_client_new(sd_dhcp6_client **ret) client->fd = -1; +#if 0 /* NM_IGNORED */ /* initialize DUID */ client->duid.en.type = htobe16(DHCP6_DUID_EN); client->duid.en.pen = htobe32(SYSTEMD_PEN); @@ -1229,6 +1240,7 @@ int sd_dhcp6_client_new(sd_dhcp6_client **ret) /* a bit of snake-oil perhaps, but no need to expose the machine-id directly */ siphash24(client->duid.en.id, &machine_id, sizeof(machine_id), HASH_KEY.bytes); +#endif client->req_opts_len = ELEMENTSOF(default_req_opts); diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-lease.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-lease.c index e2715ea659..6a1cb22daa 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-lease.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-lease.c @@ -18,6 +18,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include "util.h" diff --git a/src/dhcp-manager/systemd-dhcp/src/shared/macro.h b/src/dhcp-manager/systemd-dhcp/src/shared/macro.h index ceef491268..e6cf6eecd7 100644 --- a/src/dhcp-manager/systemd-dhcp/src/shared/macro.h +++ b/src/dhcp-manager/systemd-dhcp/src/shared/macro.h @@ -384,6 +384,7 @@ do { \ _found; \ }) +#if 0 /* NM_IGNORED */ /* Define C11 thread_local attribute even on older gcc compiler * version */ #ifndef thread_local @@ -409,3 +410,4 @@ do { \ #endif #include "log.h" +#endif diff --git a/src/dhcp-manager/systemd-dhcp/src/shared/strv.c b/src/dhcp-manager/systemd-dhcp/src/shared/strv.c index 00857e40a7..9d1e6b22ea 100644 --- a/src/dhcp-manager/systemd-dhcp/src/shared/strv.c +++ b/src/dhcp-manager/systemd-dhcp/src/shared/strv.c @@ -19,6 +19,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include diff --git a/src/dhcp-manager/systemd-dhcp/src/shared/time-util.c b/src/dhcp-manager/systemd-dhcp/src/shared/time-util.c index d3404afd55..efc18bc319 100644 --- a/src/dhcp-manager/systemd-dhcp/src/shared/time-util.c +++ b/src/dhcp-manager/systemd-dhcp/src/shared/time-util.c @@ -19,6 +19,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include @@ -26,7 +28,9 @@ #include "util.h" #include "time-util.h" +#if 0 /* NM_IGNORED */ #include "strv.h" +#endif usec_t now(clockid_t clock_id) { struct timespec ts; @@ -118,6 +122,7 @@ struct timespec *timespec_store(struct timespec *ts, usec_t u) { return ts; } +#if 0 /* NM_IGNORED */ usec_t timeval_load(const struct timeval *tv) { assert(tv); @@ -272,6 +277,7 @@ char *format_timestamp_relative(char *buf, size_t l, usec_t t) { buf[l-1] = 0; return buf; } +#endif char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) { static const struct { @@ -383,6 +389,7 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) { return buf; } +#if 0 /* NM_IGNORED */ void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) { assert(f); @@ -974,6 +981,7 @@ bool timezone_is_valid(const char *name) { return true; } +#endif clockid_t clock_boottime_or_monotonic(void) { static clockid_t clock = -1; @@ -992,3 +1000,4 @@ clockid_t clock_boottime_or_monotonic(void) { return clock; } + diff --git a/src/dhcp-manager/systemd-dhcp/src/shared/time-util.h b/src/dhcp-manager/systemd-dhcp/src/shared/time-util.h index b55a660bb0..578a0c1ca4 100644 --- a/src/dhcp-manager/systemd-dhcp/src/shared/time-util.h +++ b/src/dhcp-manager/systemd-dhcp/src/shared/time-util.h @@ -27,6 +27,8 @@ typedef uint64_t usec_t; typedef uint64_t nsec_t; +#include "nm-sd-adapt.h" + #define NSEC_FMT "%" PRIu64 #define USEC_FMT "%" PRIu64 diff --git a/src/dhcp-manager/systemd-dhcp/src/shared/utf8.c b/src/dhcp-manager/systemd-dhcp/src/shared/utf8.c index 9353559b76..dd3adf0add 100644 --- a/src/dhcp-manager/systemd-dhcp/src/shared/utf8.c +++ b/src/dhcp-manager/systemd-dhcp/src/shared/utf8.c @@ -43,6 +43,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "nm-sd-adapt.h" + #include #include #include diff --git a/src/dhcp-manager/systemd-dhcp/src/shared/util.c b/src/dhcp-manager/systemd-dhcp/src/shared/util.c index 4143f6d643..af607386ec 100644 --- a/src/dhcp-manager/systemd-dhcp/src/shared/util.c +++ b/src/dhcp-manager/systemd-dhcp/src/shared/util.c @@ -19,6 +19,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include @@ -69,6 +71,7 @@ #include "macro.h" #include "util.h" +#if 0 /* NM_IGNORED */ #include "ioprio.h" #include "missing.h" #include "log.h" @@ -85,7 +88,9 @@ #include "gunicode.h" #include "virt.h" #include "def.h" +#endif +#if 0 /* NM_IGNORED */ int saved_argc = 0; char **saved_argv = NULL; @@ -105,6 +110,7 @@ size_t page_size(void) { pgsz = (size_t) r; return pgsz; } +#endif bool streq_ptr(const char *a, const char *b) { @@ -219,6 +225,7 @@ int safe_close(int fd) { return -1; } +#if 0 /* NM_IGNORED */ void close_many(const int fds[], unsigned n_fd) { unsigned i; @@ -302,6 +309,7 @@ int parse_uid(const char *s, uid_t* ret_uid) { *ret_uid = uid; return 0; } +#endif int safe_atou(const char *s, unsigned *ret_u) { char *x = NULL; @@ -323,6 +331,7 @@ int safe_atou(const char *s, unsigned *ret_u) { return 0; } +#if 0 /* NM_IGNORED */ int safe_atoi(const char *s, int *ret_i) { char *x = NULL; long l; @@ -415,6 +424,7 @@ int safe_atod(const char *s, double *ret_d) { *ret_d = (double) d; return 0; } +#endif static size_t strcspn_escaped(const char *s, const char *reject) { bool escaped = false; @@ -472,6 +482,7 @@ const char* split(const char **state, size_t *l, const char *separator, bool quo return current; } +#if 0 /* NM_IGNORED */ int get_parent_of_pid(pid_t pid, pid_t *_ppid) { int r; _cleanup_free_ char *line = NULL; @@ -564,6 +575,7 @@ int get_starttime_of_pid(pid_t pid, unsigned long long *st) { return 0; } +#endif int fchmod_umask(int fd, mode_t m) { mode_t u; @@ -583,6 +595,7 @@ char *truncate_nl(char *s) { return s; } +#if 0 /* NM_IGNORED */ int get_process_state(pid_t pid) { const char *p; char state; @@ -820,6 +833,7 @@ int get_process_gid(pid_t pid, gid_t *gid) { assert_cc(sizeof(uid_t) == sizeof(gid_t)); return get_process_id(pid, "Gid:", gid); } +#endif char *strnappend(const char *s, const char *suffix, size_t b) { size_t a; @@ -856,6 +870,7 @@ char *strappend(const char *s, const char *suffix) { return strnappend(s, suffix, suffix ? strlen(suffix) : 0); } +#if 0 /* NM_IGNORED */ int readlinkat_malloc(int fd, const char *p, char **ret) { size_t l = 100; int r; @@ -970,6 +985,7 @@ int reset_signal_mask(void) { return 0; } +#endif char *strstrip(char *s) { char *e; @@ -988,6 +1004,7 @@ char *strstrip(char *s) { return s; } +#if 0 /* NM_IGNORED */ char *delete_chars(char *s, const char *bad) { char *f, *t; @@ -1077,6 +1094,7 @@ int rmdir_parents(const char *path, const char *stop) { return 0; } +#endif char hexchar(int x) { static const char table[16] = "0123456789abcdef"; @@ -1374,6 +1392,7 @@ char *cunescape_length(const char *s, size_t length) { return cunescape_length_with_prefix(s, length, NULL); } +#if 0 /* NM_IGNORED */ char *cunescape(const char *s) { assert(s); @@ -1563,6 +1582,7 @@ int close_all_fds(const int except[], unsigned n_except) { return r; } +#endif bool chars_intersect(const char *a, const char *b) { const char *p; @@ -1575,6 +1595,7 @@ bool chars_intersect(const char *a, const char *b) { return false; } +#if 0 /* NM_IGNORED */ bool fstype_is_network(const char *fstype) { static const char table[] = "cifs\0" @@ -2184,6 +2205,7 @@ void safe_close_pair(int p[]) { p[0] = safe_close(p[0]); p[1] = safe_close(p[1]); } +#endif ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) { uint8_t *p = buf; @@ -2220,6 +2242,7 @@ ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) { return n; } +#if 0 /* NM_IGNORED */ ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) { const uint8_t *p = buf; ssize_t n = 0; @@ -2464,6 +2487,7 @@ char* dirname_malloc(const char *path) { return dir; } +#endif int dev_urandom(void *p, size_t n) { _cleanup_close_ int fd; @@ -2521,6 +2545,7 @@ void random_bytes(void *p, size_t n) { *q = rand(); } +#if 0 /* NM_IGNORED */ void rename_process(const char name[8]) { assert(name); @@ -4021,6 +4046,7 @@ char* strshorten(char *s, size_t l) { return s; } +#endif static bool hostname_valid_char(char c) { return @@ -4062,6 +4088,7 @@ bool hostname_is_valid(const char *s) { return true; } +#if 0 /* NM_IGNORED */ char* hostname_cleanup(char *s, bool lowercase) { char *p, *d; bool dot; @@ -4122,6 +4149,7 @@ int pipe_eof(int fd) { return pollfd.revents & POLLHUP; } +#endif int fd_wait_for_event(int fd, int event, usec_t t) { @@ -4175,6 +4203,7 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) { return 0; } +#if 0 /* NM_IGNORED */ int terminal_vhangup_fd(int fd) { assert(fd >= 0); @@ -4653,6 +4682,7 @@ int get_files_in_directory(const char *path, char ***list) { return n; } +#endif char *strjoin(const char *x, ...) { va_list ap; @@ -4711,6 +4741,7 @@ char *strjoin(const char *x, ...) { return r; } +#if 0 /* NM_IGNORED */ bool is_main_thread(void) { static thread_local int cached = 0; @@ -5031,6 +5062,7 @@ finish: return buf; } +#endif void* memdup(const void *p, size_t l) { void *r; @@ -5045,6 +5077,7 @@ void* memdup(const void *p, size_t l) { return r; } +#if 0 /* NM_IGNORED */ int fd_inc_sndbuf(int fd, size_t n) { int r, value; socklen_t l = sizeof(value); @@ -5476,6 +5509,7 @@ bool string_is_safe(const char *p) { return true; } +#endif /** * Check if a string contains control characters. If 'ok' is non-NULL @@ -5500,6 +5534,7 @@ bool string_has_cc(const char *p, const char *ok) { return false; } +#if 0 /* NM_IGNORED */ bool path_is_safe(const char *p) { if (isempty(p)) @@ -5977,6 +6012,7 @@ char *strrep(const char *s, unsigned n) { *p = 0; return r; } +#endif void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) { size_t a, newalloc; @@ -6004,6 +6040,7 @@ void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size) { return q; } +#if 0 /* NM_IGNORED */ void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size) { size_t prev; uint8_t *q; @@ -6406,6 +6443,7 @@ int getpeersec(int fd, char **ret) { *ret = s; return 0; } +#endif /* This is much like like mkostemp() but is subject to umask(). */ int mkostemp_safe(char *pattern, int flags) { @@ -6423,6 +6461,7 @@ int mkostemp_safe(char *pattern, int flags) { return fd; } +#if 0 /* NM_IGNORED */ int open_tmpfile(const char *path, int flags) { char *p; int fd; @@ -6809,6 +6848,7 @@ int bind_remount_recursive(const char *prefix, bool ro) { } } } +#endif int fflush_and_check(FILE *f) { assert(f); @@ -6841,6 +6881,7 @@ char *tempfn_xxxxxx(const char *p) { return t; } +#if 0 /* NM_IGNORED */ char *tempfn_random(const char *p) { const char *fn; char *t, *x; @@ -6869,6 +6910,7 @@ char *tempfn_random(const char *p) { return t; } +#endif /* make sure the hostname is not "localhost" */ bool is_localhost(const char *hostname) { @@ -6887,6 +6929,7 @@ bool is_localhost(const char *hostname) { endswith(hostname, ".localdomain."); } +#if 0 /* NM_IGNORED */ int take_password_lock(const char *root) { struct flock flock = { @@ -7195,3 +7238,5 @@ int sethostname_idempotent(const char *s) { return 1; } +#endif + diff --git a/src/dhcp-manager/systemd-dhcp/src/shared/util.h b/src/dhcp-manager/systemd-dhcp/src/shared/util.h index 35584467c1..f6ff8ea40f 100644 --- a/src/dhcp-manager/systemd-dhcp/src/shared/util.h +++ b/src/dhcp-manager/systemd-dhcp/src/shared/util.h @@ -21,6 +21,8 @@ along with systemd; If not, see . ***/ +#include "nm-sd-adapt.h" + #include #include #include @@ -43,6 +45,7 @@ #include #include +#if 0 /* NM_IGNORED */ #if SIZEOF_PID_T == 4 # define PID_FMT "%" PRIu32 #elif SIZEOF_PID_T == 2 @@ -82,9 +85,12 @@ #else # error Unknown rlim_t size #endif +#endif #include "macro.h" +#if 0 /* NM_IGNORED */ #include "missing.h" +#endif #include "time-util.h" /* What is interpreted as whitespace? */ @@ -951,8 +957,10 @@ int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int root_fd); bool pid_is_alive(pid_t pid); bool pid_is_unwaited(pid_t pid); +#if 0 /* NM_IGNORED */ int getpeercred(int fd, struct ucred *ucred); int getpeersec(int fd, char **ret); +#endif int writev_safe(int fd, const struct iovec *w, int j); @@ -970,10 +978,12 @@ char* mount_test_option(const char *haystack, const char *needle); void hexdump(FILE *f, const void *p, size_t s); +#if 0 /* NM_IGNORED */ union file_handle_union { struct file_handle handle; char padding[sizeof(struct file_handle) + MAX_HANDLE_SZ]; }; +#endif int update_reboot_param_file(const char *param); -- cgit v1.2.1 From cd12e97620faffeb76ba1fff85867a3cdb8a4ea8 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Tue, 22 Jul 2014 12:56:46 -0500 Subject: dhcp: systemd DHCP code changes necessary for NM integration Random functionality that's necessary for our use of the library. --- .../src/libsystemd-network/sd-dhcp6-client.c | 46 ++++++++-------------- .../systemd-dhcp/src/systemd/sd-dhcp6-client.h | 1 + 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c index faf490e141..68625cc851 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c @@ -61,6 +61,7 @@ struct sd_dhcp6_client { uint8_t mac_addr[MAX_MAC_ADDR_LEN]; size_t mac_addr_len; uint16_t arp_type; + char ifname[IFNAMSIZ]; DHCP6IA ia_na; be32_t transaction_id; usec_t transaction_start; @@ -632,11 +633,7 @@ error: } static int client_ensure_iaid(sd_dhcp6_client *client) { - /* name is a pointer to memory in the udev_device struct, so must - have the same scope */ -#if 0 /* NM_IGNORED */ - _cleanup_udev_device_unref_ struct udev_device *device = NULL; - const char *name = NULL; + const char *name; uint64_t id; assert(client); @@ -644,27 +641,7 @@ static int client_ensure_iaid(sd_dhcp6_client *client) { if (client->ia_na.id) return 0; - if (detect_container(NULL) <= 0) { - /* not in a container, udev will be around */ - _cleanup_udev_unref_ struct udev *udev; - char ifindex_str[2 + DECIMAL_STR_MAX(int)]; - - udev = udev_new(); - if (!udev) - return -ENOMEM; - - sprintf(ifindex_str, "n%d", client->index); - device = udev_device_new_from_device_id(udev, ifindex_str); - if (!device) - return -errno; - - if (udev_device_get_is_initialized(device) <= 0) - /* not yet ready */ - return -EBUSY; - - name = net_get_name(device); - } - + name = client->ifname; if (name) siphash24((uint8_t*)&id, name, strlen(name), HASH_KEY.bytes); else @@ -676,9 +653,6 @@ static int client_ensure_iaid(sd_dhcp6_client *client) { client->ia_na.id = (id & 0xffffffff) ^ (id >> 32); return 0; -#else - return -1; -#endif } static int client_parse_message(sd_dhcp6_client *client, @@ -1256,3 +1230,17 @@ int sd_dhcp6_client_new(sd_dhcp6_client **ret) return 0; } + +/*******************************************/ +/* NetworkManager additions */ + +int sd_dhcp6_client_set_ifname(sd_dhcp6_client *client, const char *ifname) +{ + assert_return(client, -EINVAL); + assert_return(ifname, -EINVAL); + assert_return(strlen (ifname) < sizeof (client->ifname), -EINVAL); + + strcpy(client->ifname, ifname); + return 0; +} + diff --git a/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp6-client.h b/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp6-client.h index c7f168fe21..38c1c3fb12 100644 --- a/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp6-client.h +++ b/src/dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp6-client.h @@ -47,6 +47,7 @@ int sd_dhcp6_client_set_mac(sd_dhcp6_client *client, const uint8_t *addr, size_t addr_len, uint16_t arp_type); int sd_dhcp6_client_set_duid(sd_dhcp6_client *client, uint16_t type, uint8_t *duid, size_t duid_len); +int sd_dhcp6_client_set_ifname(sd_dhcp6_client *client, const char *ifname); int sd_dhcp6_client_set_request_option(sd_dhcp6_client *client, uint16_t option); -- cgit v1.2.1 From 3c34f1d92fb1f8cb01e3d54216d80eb2dd85cedf Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 3 Nov 2014 15:16:31 -0600 Subject: dhcp: add nm_dhcp_utils_client_id_string_to_bytes() Generic function to convert a DHCP client identifier string in either hex form ("aa:bb:cc") or string form ("blahblah") to bytes. --- src/dhcp-manager/nm-dhcp-utils.c | 65 +++ src/dhcp-manager/nm-dhcp-utils.h | 2 + src/dhcp-manager/tests/Makefile.am | 12 +- src/dhcp-manager/tests/test-dhcp-options.c | 684 --------------------------- src/dhcp-manager/tests/test-dhcp-utils.c | 721 +++++++++++++++++++++++++++++ 5 files changed, 794 insertions(+), 690 deletions(-) delete mode 100644 src/dhcp-manager/tests/test-dhcp-options.c create mode 100644 src/dhcp-manager/tests/test-dhcp-utils.c diff --git a/src/dhcp-manager/nm-dhcp-utils.c b/src/dhcp-manager/nm-dhcp-utils.c index 014937f6ea..02fc4fe513 100644 --- a/src/dhcp-manager/nm-dhcp-utils.c +++ b/src/dhcp-manager/nm-dhcp-utils.c @@ -692,3 +692,68 @@ nm_dhcp_utils_duid_to_string (const GByteArray *duid) return g_string_free (s, FALSE); } +/** + * nm_dhcp_utils_client_id_string_to_bytes: + * @client_id: the client ID string + * + * Accepts either a hex string ("aa:bb:cc") representing a binary client ID + * (the first byte is assumed to be the 'type' field per RFC 2132 section 9.14), + * or a string representing a non-hardware-address client ID, in which case + * the 'type' field is set to 0. + * + * Returns: the binary client ID suitable for sending over the wire + * to the DHCP server. + */ +GBytes * +nm_dhcp_utils_client_id_string_to_bytes (const char *client_id) +{ + GBytes *bytes = NULL; + guint i = 0, x = 0; + guint len; + char *c; + int a; + + g_return_val_if_fail (client_id && client_id[0], NULL); + + /* Accept a binary client ID in hex digits with the ':' delimiter, + * otherwise treat it as a string. + */ + len = strlen (client_id); + c = g_malloc0 (len / 2 + 1); + while (client_id[i]) { + a = g_ascii_xdigit_value (client_id[i++]); + if (a >= 0) { + if (client_id[i] != ':') { + c[x] = ((guint8) a << 4); + a = g_ascii_xdigit_value (client_id[i++]); + } + if (a >= 0) + c[x++] |= (guint8) a; + } + if (client_id[i]) { + if (client_id[i] != ':' || !client_id[i + 1]) { + /* missing or trailing ':' is invalid for hex-format */ + a = -1; + } + i++; + } + + if (a < 0) { + g_clear_pointer (&c, g_free); + break; + } + } + + if (c) { + g_assert (x > 0); + bytes = g_bytes_new_take (c, x); + } else { + c = g_malloc (len + 1); + c[0] = 0; /* type: non-hardware address per RFC 2132 section 9.14 */ + memcpy (c + 1, client_id, len); + bytes = g_bytes_new_take (c, len + 1); + } + + return bytes; +} + diff --git a/src/dhcp-manager/nm-dhcp-utils.h b/src/dhcp-manager/nm-dhcp-utils.h index ab9d45fe4b..a01cf4c267 100644 --- a/src/dhcp-manager/nm-dhcp-utils.h +++ b/src/dhcp-manager/nm-dhcp-utils.h @@ -35,5 +35,7 @@ NMIP6Config *nm_dhcp_utils_ip6_config_from_options (const char *iface, char * nm_dhcp_utils_duid_to_string (const GByteArray *duid); +GBytes * nm_dhcp_utils_client_id_string_to_bytes (const char *client_id); + #endif /* __NETWORKMANAGER_DHCP_UTILS_H__ */ diff --git a/src/dhcp-manager/tests/Makefile.am b/src/dhcp-manager/tests/Makefile.am index 9d6e7b97b4..0292db2b7a 100644 --- a/src/dhcp-manager/tests/Makefile.am +++ b/src/dhcp-manager/tests/Makefile.am @@ -13,7 +13,7 @@ AM_CPPFLAGS = \ noinst_PROGRAMS = \ test-dhcp-dhclient \ - test-dhcp-options + test-dhcp-utils ####### dhclient leases test ####### @@ -23,17 +23,17 @@ test_dhcp_dhclient_SOURCES = \ test_dhcp_dhclient_LDADD = \ $(top_builddir)/src/libNetworkManager.la -####### DHCP options test ####### +####### DHCP utils test ####### -test_dhcp_options_SOURCES = \ - test-dhcp-options.c +test_dhcp_utils_SOURCES = \ + test-dhcp-utils.c -test_dhcp_options_LDADD = \ +test_dhcp_utils_LDADD = \ $(top_builddir)/src/libNetworkManager.la ################################# -TESTS = test-dhcp-dhclient test-dhcp-options +TESTS = test-dhcp-dhclient test-dhcp-utils EXTRA_DIST = \ test-dhclient-duid.leases \ diff --git a/src/dhcp-manager/tests/test-dhcp-options.c b/src/dhcp-manager/tests/test-dhcp-options.c deleted file mode 100644 index 15f5ee60b5..0000000000 --- a/src/dhcp-manager/tests/test-dhcp-options.c +++ /dev/null @@ -1,684 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2008 - 2014 Red Hat, Inc. - * - */ - -#include -#include -#include -#include - -#include - -#include "nm-dhcp-utils.h" -#include "nm-logging.h" -#include "nm-platform.h" - -#include "nm-test-utils.h" - -typedef struct { - const char *name; - const char *value; -} Option; - -static GHashTable * -fill_table (const Option *test_options, GHashTable *table) -{ - const Option *opt; - - if (!table) - table = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL); - for (opt = test_options; opt->name; opt++) - g_hash_table_insert (table, (gpointer) opt->name, (gpointer) opt->value); - return table; -} - -static const Option generic_options[] = { - { "subnet_mask", "255.255.255.0" }, - { "ip_address", "192.168.1.106" }, - { "network_number", "192.168.1.0" }, - { "expiry", "1232324877" }, - { "dhcp_lease_time", "3600" }, - { "dhcp_server_identifier", "192.168.1.1" }, - { "routers", "192.168.1.1" }, - { "domain_name_servers", "216.254.95.2 216.231.41.2" }, - { "dhcp_message_type", "5" }, - { "broadcast_address", "192.168.1.255" }, - { "domain_search", "foobar.com blah.foobar.com" }, - { "host_name", "nmreallywhipsthe" }, - { "domain_name", "lamasass.com" }, - { "interface_mtu", "987" }, - { "static_routes", "10.1.1.5 10.1.1.1 100.99.88.56 10.1.1.1" }, - { NULL, NULL } -}; - -static void -test_generic_options (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const NMPlatformIP4Address *address; - const NMPlatformIP4Route *route; - guint32 tmp; - const char *expected_addr = "192.168.1.106"; - const char *expected_gw = "192.168.1.1"; - const char *expected_dns1 = "216.254.95.2"; - const char *expected_dns2 = "216.231.41.2"; - const char *expected_search1 = "foobar.com"; - const char *expected_search2 = "blah.foobar.com"; - const char *expected_route1_dest = "10.1.1.5"; - const char *expected_route1_gw = "10.1.1.1"; - const char *expected_route2_dest = "100.99.88.56"; - const char *expected_route2_gw = "10.1.1.1"; - - options = fill_table (generic_options, NULL); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - - /* IP4 address */ - g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1); - address = nm_ip4_config_get_address (ip4_config, 0); - g_assert (inet_pton (AF_INET, expected_addr, &tmp) > 0); - g_assert (address->address == tmp); - g_assert (address->peer_address == 0); - g_assert_cmpint (address->plen, ==, 24); - - /* Gateway */ - g_assert (inet_pton (AF_INET, expected_gw, &tmp) > 0); - g_assert (nm_ip4_config_get_gateway (ip4_config) == tmp); - - g_assert_cmpint (nm_ip4_config_get_num_wins (ip4_config), ==, 0); - - g_assert_cmpint (nm_ip4_config_get_mtu (ip4_config), ==, 987); - - /* Domain searches */ - g_assert_cmpint (nm_ip4_config_get_num_searches (ip4_config), ==, 2); - g_assert_cmpstr (nm_ip4_config_get_search (ip4_config, 0), ==, expected_search1); - g_assert_cmpstr (nm_ip4_config_get_search (ip4_config, 1), ==, expected_search2); - - /* DNS servers */ - g_assert_cmpint (nm_ip4_config_get_num_nameservers (ip4_config), ==, 2); - g_assert (inet_pton (AF_INET, expected_dns1, &tmp) > 0); - g_assert (nm_ip4_config_get_nameserver (ip4_config, 0) == tmp); - g_assert (inet_pton (AF_INET, expected_dns2, &tmp) > 0); - g_assert (nm_ip4_config_get_nameserver (ip4_config, 1) == tmp); - - /* Routes */ - g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); - - /* Route #1 */ - route = nm_ip4_config_get_route (ip4_config, 0); - g_assert (inet_pton (AF_INET, expected_route1_dest, &tmp) > 0); - g_assert (route->network == tmp); - g_assert (inet_pton (AF_INET, expected_route1_gw, &tmp) > 0); - g_assert (route->gateway == tmp); - g_assert_cmpint (route->plen, ==, 32); - g_assert_cmpint (route->metric, ==, 0); - - /* Route #2 */ - route = nm_ip4_config_get_route (ip4_config, 1); - g_assert (inet_pton (AF_INET, expected_route2_dest, &tmp) > 0); - g_assert (route->network == tmp); - g_assert (inet_pton (AF_INET, expected_route2_gw, &tmp) > 0); - g_assert (route->gateway == tmp); - g_assert_cmpint (route->plen, ==, 32); - g_assert_cmpint (route->metric, ==, 0); - - g_hash_table_destroy (options); -} - -static void -test_wins_options (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const NMPlatformIP4Address *address; - guint32 tmp; - const char *expected_wins1 = "63.12.199.5"; - const char *expected_wins2 = "150.4.88.120"; - static const Option data[] = { - { "netbios_name_servers", "63.12.199.5 150.4.88.120" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - - /* IP4 address */ - g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1); - address = nm_ip4_config_get_address (ip4_config, 0); - g_assert (address); - g_assert_cmpint (nm_ip4_config_get_num_wins (ip4_config), ==, 2); - g_assert (inet_pton (AF_INET, expected_wins1, &tmp) > 0); - g_assert (nm_ip4_config_get_wins (ip4_config, 0) == tmp); - g_assert (inet_pton (AF_INET, expected_wins2, &tmp) > 0); - g_assert (nm_ip4_config_get_wins (ip4_config, 1) == tmp); - - g_hash_table_destroy (options); -} - -static void -ip4_test_route (NMIP4Config *ip4_config, - guint route_num, - const char *expected_dest, - const char *expected_gw, - guint expected_prefix) -{ - const NMPlatformIP4Route *route; - guint32 tmp; - - route = nm_ip4_config_get_route (ip4_config, route_num); - g_assert (inet_pton (AF_INET, expected_dest, &tmp) > 0); - g_assert (route->network == tmp); - g_assert (inet_pton (AF_INET, expected_gw, &tmp) > 0); - g_assert (route->gateway == tmp); - g_assert_cmpint (route->plen, ==, expected_prefix); - g_assert_cmpint (route->metric, ==, 0); -} - -static void -ip4_test_gateway (NMIP4Config *ip4_config, const char *expected_gw) -{ - guint32 tmp; - - g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1); - g_assert (inet_pton (AF_INET, expected_gw, &tmp) > 0); - g_assert (nm_ip4_config_get_gateway (ip4_config) == tmp); -} - -static void -test_classless_static_routes_1 (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const char *expected_route1_dest = "192.168.10.0"; - const char *expected_route1_gw = "192.168.1.1"; - const char *expected_route2_dest = "10.0.0.0"; - const char *expected_route2_gw = "10.17.66.41"; - static const Option data[] = { - /* dhclient custom format */ - { "rfc3442_classless_static_routes", "24 192 168 10 192 168 1 1 8 10 10 17 66 41" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - - /* IP4 routes */ - g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); - ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); - ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 8); - - g_hash_table_destroy (options); -} - -static void -test_classless_static_routes_2 (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const char *expected_route1_dest = "192.168.10.0"; - const char *expected_route1_gw = "192.168.1.1"; - const char *expected_route2_dest = "10.0.0.0"; - const char *expected_route2_gw = "10.17.66.41"; - static const Option data[] = { - /* dhcpcd format */ - { "classless_static_routes", "192.168.10.0/24 192.168.1.1 10.0.0.0/8 10.17.66.41" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - - /* IP4 routes */ - g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); - ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); - ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 8); - - g_hash_table_destroy (options); -} - -static void -test_fedora_dhclient_classless_static_routes (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const char *expected_route1_dest = "129.210.177.128"; - const char *expected_route1_gw = "192.168.0.113"; - const char *expected_route2_dest = "2.0.0.0"; - const char *expected_route2_gw = "10.34.255.6"; - const char *expected_gateway = "192.168.0.113"; - static const Option data[] = { - /* Fedora dhclient format */ - { "classless_static_routes", "0 192.168.0.113 25.129.210.177.132 192.168.0.113 7.2 10.34.255.6" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - - /* IP4 routes */ - g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); - ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 25); - ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 7); - - /* Gateway */ - ip4_test_gateway (ip4_config, expected_gateway); - - g_hash_table_destroy (options); -} - -static void -test_dhclient_invalid_classless_routes_1 (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const char *expected_route1_dest = "192.168.10.0"; - const char *expected_route1_gw = "192.168.1.1"; - static const Option data[] = { - /* dhclient format */ - { "rfc3442_classless_static_routes", "24 192 168 10 192 168 1 1 45 10 17 66 41" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - - g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, - "*ignoring invalid classless static routes*"); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - g_test_assert_expected_messages (); - - /* IP4 routes */ - g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1); - ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); - - g_hash_table_destroy (options); -} - -static void -test_dhcpcd_invalid_classless_routes_1 (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const char *expected_route1_dest = "10.1.1.5"; - const char *expected_route1_gw = "10.1.1.1"; - const char *expected_route2_dest = "100.99.88.56"; - const char *expected_route2_gw = "10.1.1.1"; - static const Option data[] = { - /* dhcpcd format */ - { "classless_static_routes", "192.168.10.0/24 192.168.1.1 10.0.adfadf/44 10.17.66.41" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - - g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, - "*ignoring invalid classless static routes*"); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - g_test_assert_expected_messages (); - - /* Test falling back to old-style static routes if the classless static - * routes are invalid. - */ - g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); - ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 32); - ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 32); - - g_hash_table_destroy (options); -} - -static void -test_dhclient_invalid_classless_routes_2 (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const char *expected_route1_dest = "10.1.1.5"; - const char *expected_route1_gw = "10.1.1.1"; - const char *expected_route2_dest = "100.99.88.56"; - const char *expected_route2_gw = "10.1.1.1"; - static const Option data[] = { - { "rfc3442_classless_static_routes", "45 10 17 66 41 24 192 168 10 192 168 1 1" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - - g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, - "*ignoring invalid classless static routes*"); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - g_test_assert_expected_messages (); - - /* Test falling back to old-style static routes if the classless static - * routes are invalid. - */ - g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); - ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 32); - ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 32); - - g_hash_table_destroy (options); -} - -static void -test_dhcpcd_invalid_classless_routes_2 (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const char *expected_route1_dest = "10.1.1.5"; - const char *expected_route1_gw = "10.1.1.1"; - const char *expected_route2_dest = "100.99.88.56"; - const char *expected_route2_gw = "10.1.1.1"; - static const Option data[] = { - { "classless_static_routes", "10.0.adfadf/44 10.17.66.41 192.168.10.0/24 192.168.1.1" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - - g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, - "*ignoring invalid classless static routes*"); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - g_test_assert_expected_messages (); - - /* Test falling back to old-style static routes if the classless static - * routes are invalid. - */ - - /* Routes */ - g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); - ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 32); - ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 32); - - g_hash_table_destroy (options); -} - -static void -test_dhclient_invalid_classless_routes_3 (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const char *expected_route1_dest = "192.168.10.0"; - const char *expected_route1_gw = "192.168.1.1"; - static const Option data[] = { - { "rfc3442_classless_static_routes", "24 192 168 10 192 168 1 1 32 128 10 17 66 41" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - - g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, - "*ignoring invalid classless static routes*"); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - g_test_assert_expected_messages (); - - /* IP4 routes */ - g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1); - ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); - - g_hash_table_destroy (options); -} - -static void -test_dhcpcd_invalid_classless_routes_3 (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const char *expected_route1_dest = "192.168.10.0"; - const char *expected_route1_gw = "192.168.1.1"; - static Option data[] = { - { "classless_static_routes", "192.168.10.0/24 192.168.1.1 128/32 10.17.66.41" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - - g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, - "*DHCP provided invalid classless static route*"); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - g_test_assert_expected_messages (); - - /* IP4 routes */ - g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1); - ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); - - g_hash_table_destroy (options); -} - -static void -test_dhclient_gw_in_classless_routes (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const char *expected_route1_dest = "192.168.10.0"; - const char *expected_route1_gw = "192.168.1.1"; - const char *expected_gateway = "192.2.3.4"; - static Option data[] = { - { "rfc3442_classless_static_routes", "24 192 168 10 192 168 1 1 0 192 2 3 4" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - - /* IP4 routes */ - g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1); - ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); - - /* Gateway */ - ip4_test_gateway (ip4_config, expected_gateway); - - g_hash_table_destroy (options); -} - -static void -test_dhcpcd_gw_in_classless_routes (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const char *expected_route1_dest = "192.168.10.0"; - const char *expected_route1_gw = "192.168.1.1"; - const char *expected_gateway = "192.2.3.4"; - static Option data[] = { - { "classless_static_routes", "192.168.10.0/24 192.168.1.1 0.0.0.0/0 192.2.3.4" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - - /* IP4 routes */ - g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1); - ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); - - /* Gateway */ - ip4_test_gateway (ip4_config, expected_gateway); - - g_hash_table_destroy (options); -} - -static void -test_escaped_domain_searches (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const char *expected_search0 = "host1"; - const char *expected_search1 = "host2"; - const char *expected_search2 = "host3"; - static const Option data[] = { - { "domain_search", "host1\\032host2\\032host3" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - - /* domain searches */ - g_assert_cmpint (nm_ip4_config_get_num_searches (ip4_config), ==, 3); - g_assert_cmpstr (nm_ip4_config_get_search (ip4_config, 0), ==, expected_search0); - g_assert_cmpstr (nm_ip4_config_get_search (ip4_config, 1), ==, expected_search1); - g_assert_cmpstr (nm_ip4_config_get_search (ip4_config, 2), ==, expected_search2); - - g_hash_table_destroy (options); -} - -static void -test_invalid_escaped_domain_searches (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - static const Option data[] = { - { "domain_search", "host1\\aahost2\\032host3" }, - { NULL, NULL } - }; - - options = fill_table (generic_options, NULL); - options = fill_table (data, options); - - g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, - "*invalid domain search*"); - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - g_test_assert_expected_messages (); - - /* domain searches */ - g_assert_cmpint (nm_ip4_config_get_num_searches (ip4_config), ==, 0); - - g_hash_table_destroy (options); -} - -static void -test_ip4_missing_prefix (const char *ip, guint32 expected_prefix) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const NMPlatformIP4Address *address; - - options = fill_table (generic_options, NULL); - g_hash_table_insert (options, "ip_address", (gpointer) ip); - g_hash_table_remove (options, "subnet_mask"); - - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - - g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1); - address = nm_ip4_config_get_address (ip4_config, 0); - g_assert (address); - g_assert_cmpint (address->plen, ==, expected_prefix); - - g_hash_table_destroy (options); -} - -static void -test_ip4_missing_prefix_24 (void) -{ - test_ip4_missing_prefix ("192.168.1.10", 24); -} - -static void -test_ip4_missing_prefix_16 (void) -{ - test_ip4_missing_prefix ("172.16.54.50", 16); -} - -static void -test_ip4_missing_prefix_8 (void) -{ - test_ip4_missing_prefix ("10.1.2.3", 8); -} - -static void -test_ip4_prefix_classless (void) -{ - GHashTable *options; - NMIP4Config *ip4_config; - const NMPlatformIP4Address *address; - - /* Ensure that the missing-subnet-mask handler doesn't mangle classless - * subnet masks at all. The handler should trigger only if the server - * doesn't send the subnet mask. - */ - - options = fill_table (generic_options, NULL); - g_hash_table_insert (options, "ip_address", "172.16.54.22"); - g_hash_table_insert (options, "subnet_mask", "255.255.252.0"); - - ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); - g_assert (ip4_config); - - g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1); - address = nm_ip4_config_get_address (ip4_config, 0); - g_assert (address); - g_assert_cmpint (address->plen, ==, 22); - - g_hash_table_destroy (options); -} - -NMTST_DEFINE (); - -int main (int argc, char **argv) -{ - nmtst_init_assert_logging (&argc, &argv); - nm_logging_setup ("WARN", "DEFAULT", NULL, NULL); - - g_test_add_func ("/dhcp/generic-options", test_generic_options); - g_test_add_func ("/dhcp/wins-options", test_wins_options); - g_test_add_func ("/dhcp/classless-static-routes-1", test_classless_static_routes_1); - g_test_add_func ("/dhcp/classless-static-routes-2", test_classless_static_routes_2); - g_test_add_func ("/dhcp/fedora-dhclient-classless-static-routes", test_fedora_dhclient_classless_static_routes); - g_test_add_func ("/dhcp/dhclient-invalid-classless-routes-1", test_dhclient_invalid_classless_routes_1); - g_test_add_func ("/dhcp/dhcpcd-invalid-classless-routes-1", test_dhcpcd_invalid_classless_routes_1); - g_test_add_func ("/dhcp/dhclient-invalid-classless-routes-2", test_dhclient_invalid_classless_routes_2); - g_test_add_func ("/dhcp/dhcpcd-invalid-classless-routes-2", test_dhcpcd_invalid_classless_routes_2); - g_test_add_func ("/dhcp/dhclient-invalid-classless-routes-3", test_dhclient_invalid_classless_routes_3); - g_test_add_func ("/dhcp/dhcpcd-invalid-classless-routes-3", test_dhcpcd_invalid_classless_routes_3); - g_test_add_func ("/dhcp/dhclient-gw-in-classless-routes", test_dhclient_gw_in_classless_routes); - g_test_add_func ("/dhcp/dhcpcd-gw-in-classless-routes", test_dhcpcd_gw_in_classless_routes); - g_test_add_func ("/dhcp/escaped-domain-searches", test_escaped_domain_searches); - g_test_add_func ("/dhcp/invalid-escaped-domain-searches", test_invalid_escaped_domain_searches); - g_test_add_func ("/dhcp/ip4-missing-prefix-24", test_ip4_missing_prefix_24); - g_test_add_func ("/dhcp/ip4-missing-prefix-16", test_ip4_missing_prefix_16); - g_test_add_func ("/dhcp/ip4-missing-prefix-8", test_ip4_missing_prefix_8); - g_test_add_func ("/dhcp/ip4-prefix-classless", test_ip4_prefix_classless); - - return g_test_run (); -} - diff --git a/src/dhcp-manager/tests/test-dhcp-utils.c b/src/dhcp-manager/tests/test-dhcp-utils.c new file mode 100644 index 0000000000..6ed10e0474 --- /dev/null +++ b/src/dhcp-manager/tests/test-dhcp-utils.c @@ -0,0 +1,721 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2008 - 2014 Red Hat, Inc. + * + */ + +#include +#include +#include +#include + +#include + +#include "nm-dhcp-utils.h" +#include "nm-logging.h" +#include "nm-platform.h" + +#include "nm-test-utils.h" + +typedef struct { + const char *name; + const char *value; +} Option; + +static GHashTable * +fill_table (const Option *test_options, GHashTable *table) +{ + const Option *opt; + + if (!table) + table = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL); + for (opt = test_options; opt->name; opt++) + g_hash_table_insert (table, (gpointer) opt->name, (gpointer) opt->value); + return table; +} + +static const Option generic_options[] = { + { "subnet_mask", "255.255.255.0" }, + { "ip_address", "192.168.1.106" }, + { "network_number", "192.168.1.0" }, + { "expiry", "1232324877" }, + { "dhcp_lease_time", "3600" }, + { "dhcp_server_identifier", "192.168.1.1" }, + { "routers", "192.168.1.1" }, + { "domain_name_servers", "216.254.95.2 216.231.41.2" }, + { "dhcp_message_type", "5" }, + { "broadcast_address", "192.168.1.255" }, + { "domain_search", "foobar.com blah.foobar.com" }, + { "host_name", "nmreallywhipsthe" }, + { "domain_name", "lamasass.com" }, + { "interface_mtu", "987" }, + { "static_routes", "10.1.1.5 10.1.1.1 100.99.88.56 10.1.1.1" }, + { NULL, NULL } +}; + +static void +test_generic_options (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const NMPlatformIP4Address *address; + const NMPlatformIP4Route *route; + guint32 tmp; + const char *expected_addr = "192.168.1.106"; + const char *expected_gw = "192.168.1.1"; + const char *expected_dns1 = "216.254.95.2"; + const char *expected_dns2 = "216.231.41.2"; + const char *expected_search1 = "foobar.com"; + const char *expected_search2 = "blah.foobar.com"; + const char *expected_route1_dest = "10.1.1.5"; + const char *expected_route1_gw = "10.1.1.1"; + const char *expected_route2_dest = "100.99.88.56"; + const char *expected_route2_gw = "10.1.1.1"; + + options = fill_table (generic_options, NULL); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + + /* IP4 address */ + g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1); + address = nm_ip4_config_get_address (ip4_config, 0); + g_assert (inet_pton (AF_INET, expected_addr, &tmp) > 0); + g_assert (address->address == tmp); + g_assert (address->peer_address == 0); + g_assert_cmpint (address->plen, ==, 24); + + /* Gateway */ + g_assert (inet_pton (AF_INET, expected_gw, &tmp) > 0); + g_assert (nm_ip4_config_get_gateway (ip4_config) == tmp); + + g_assert_cmpint (nm_ip4_config_get_num_wins (ip4_config), ==, 0); + + g_assert_cmpint (nm_ip4_config_get_mtu (ip4_config), ==, 987); + + /* Domain searches */ + g_assert_cmpint (nm_ip4_config_get_num_searches (ip4_config), ==, 2); + g_assert_cmpstr (nm_ip4_config_get_search (ip4_config, 0), ==, expected_search1); + g_assert_cmpstr (nm_ip4_config_get_search (ip4_config, 1), ==, expected_search2); + + /* DNS servers */ + g_assert_cmpint (nm_ip4_config_get_num_nameservers (ip4_config), ==, 2); + g_assert (inet_pton (AF_INET, expected_dns1, &tmp) > 0); + g_assert (nm_ip4_config_get_nameserver (ip4_config, 0) == tmp); + g_assert (inet_pton (AF_INET, expected_dns2, &tmp) > 0); + g_assert (nm_ip4_config_get_nameserver (ip4_config, 1) == tmp); + + /* Routes */ + g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); + + /* Route #1 */ + route = nm_ip4_config_get_route (ip4_config, 0); + g_assert (inet_pton (AF_INET, expected_route1_dest, &tmp) > 0); + g_assert (route->network == tmp); + g_assert (inet_pton (AF_INET, expected_route1_gw, &tmp) > 0); + g_assert (route->gateway == tmp); + g_assert_cmpint (route->plen, ==, 32); + g_assert_cmpint (route->metric, ==, 0); + + /* Route #2 */ + route = nm_ip4_config_get_route (ip4_config, 1); + g_assert (inet_pton (AF_INET, expected_route2_dest, &tmp) > 0); + g_assert (route->network == tmp); + g_assert (inet_pton (AF_INET, expected_route2_gw, &tmp) > 0); + g_assert (route->gateway == tmp); + g_assert_cmpint (route->plen, ==, 32); + g_assert_cmpint (route->metric, ==, 0); + + g_hash_table_destroy (options); +} + +static void +test_wins_options (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const NMPlatformIP4Address *address; + guint32 tmp; + const char *expected_wins1 = "63.12.199.5"; + const char *expected_wins2 = "150.4.88.120"; + static const Option data[] = { + { "netbios_name_servers", "63.12.199.5 150.4.88.120" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + + /* IP4 address */ + g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1); + address = nm_ip4_config_get_address (ip4_config, 0); + g_assert (address); + g_assert_cmpint (nm_ip4_config_get_num_wins (ip4_config), ==, 2); + g_assert (inet_pton (AF_INET, expected_wins1, &tmp) > 0); + g_assert (nm_ip4_config_get_wins (ip4_config, 0) == tmp); + g_assert (inet_pton (AF_INET, expected_wins2, &tmp) > 0); + g_assert (nm_ip4_config_get_wins (ip4_config, 1) == tmp); + + g_hash_table_destroy (options); +} + +static void +ip4_test_route (NMIP4Config *ip4_config, + guint route_num, + const char *expected_dest, + const char *expected_gw, + guint expected_prefix) +{ + const NMPlatformIP4Route *route; + guint32 tmp; + + route = nm_ip4_config_get_route (ip4_config, route_num); + g_assert (inet_pton (AF_INET, expected_dest, &tmp) > 0); + g_assert (route->network == tmp); + g_assert (inet_pton (AF_INET, expected_gw, &tmp) > 0); + g_assert (route->gateway == tmp); + g_assert_cmpint (route->plen, ==, expected_prefix); + g_assert_cmpint (route->metric, ==, 0); +} + +static void +ip4_test_gateway (NMIP4Config *ip4_config, const char *expected_gw) +{ + guint32 tmp; + + g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1); + g_assert (inet_pton (AF_INET, expected_gw, &tmp) > 0); + g_assert (nm_ip4_config_get_gateway (ip4_config) == tmp); +} + +static void +test_classless_static_routes_1 (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const char *expected_route1_dest = "192.168.10.0"; + const char *expected_route1_gw = "192.168.1.1"; + const char *expected_route2_dest = "10.0.0.0"; + const char *expected_route2_gw = "10.17.66.41"; + static const Option data[] = { + /* dhclient custom format */ + { "rfc3442_classless_static_routes", "24 192 168 10 192 168 1 1 8 10 10 17 66 41" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + + /* IP4 routes */ + g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); + ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); + ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 8); + + g_hash_table_destroy (options); +} + +static void +test_classless_static_routes_2 (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const char *expected_route1_dest = "192.168.10.0"; + const char *expected_route1_gw = "192.168.1.1"; + const char *expected_route2_dest = "10.0.0.0"; + const char *expected_route2_gw = "10.17.66.41"; + static const Option data[] = { + /* dhcpcd format */ + { "classless_static_routes", "192.168.10.0/24 192.168.1.1 10.0.0.0/8 10.17.66.41" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + + /* IP4 routes */ + g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); + ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); + ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 8); + + g_hash_table_destroy (options); +} + +static void +test_fedora_dhclient_classless_static_routes (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const char *expected_route1_dest = "129.210.177.128"; + const char *expected_route1_gw = "192.168.0.113"; + const char *expected_route2_dest = "2.0.0.0"; + const char *expected_route2_gw = "10.34.255.6"; + const char *expected_gateway = "192.168.0.113"; + static const Option data[] = { + /* Fedora dhclient format */ + { "classless_static_routes", "0 192.168.0.113 25.129.210.177.132 192.168.0.113 7.2 10.34.255.6" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + + /* IP4 routes */ + g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); + ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 25); + ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 7); + + /* Gateway */ + ip4_test_gateway (ip4_config, expected_gateway); + + g_hash_table_destroy (options); +} + +static void +test_dhclient_invalid_classless_routes_1 (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const char *expected_route1_dest = "192.168.10.0"; + const char *expected_route1_gw = "192.168.1.1"; + static const Option data[] = { + /* dhclient format */ + { "rfc3442_classless_static_routes", "24 192 168 10 192 168 1 1 45 10 17 66 41" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + + g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, + "*ignoring invalid classless static routes*"); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + g_test_assert_expected_messages (); + + /* IP4 routes */ + g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1); + ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); + + g_hash_table_destroy (options); +} + +static void +test_dhcpcd_invalid_classless_routes_1 (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const char *expected_route1_dest = "10.1.1.5"; + const char *expected_route1_gw = "10.1.1.1"; + const char *expected_route2_dest = "100.99.88.56"; + const char *expected_route2_gw = "10.1.1.1"; + static const Option data[] = { + /* dhcpcd format */ + { "classless_static_routes", "192.168.10.0/24 192.168.1.1 10.0.adfadf/44 10.17.66.41" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + + g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, + "*ignoring invalid classless static routes*"); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + g_test_assert_expected_messages (); + + /* Test falling back to old-style static routes if the classless static + * routes are invalid. + */ + g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); + ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 32); + ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 32); + + g_hash_table_destroy (options); +} + +static void +test_dhclient_invalid_classless_routes_2 (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const char *expected_route1_dest = "10.1.1.5"; + const char *expected_route1_gw = "10.1.1.1"; + const char *expected_route2_dest = "100.99.88.56"; + const char *expected_route2_gw = "10.1.1.1"; + static const Option data[] = { + { "rfc3442_classless_static_routes", "45 10 17 66 41 24 192 168 10 192 168 1 1" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + + g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, + "*ignoring invalid classless static routes*"); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + g_test_assert_expected_messages (); + + /* Test falling back to old-style static routes if the classless static + * routes are invalid. + */ + g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); + ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 32); + ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 32); + + g_hash_table_destroy (options); +} + +static void +test_dhcpcd_invalid_classless_routes_2 (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const char *expected_route1_dest = "10.1.1.5"; + const char *expected_route1_gw = "10.1.1.1"; + const char *expected_route2_dest = "100.99.88.56"; + const char *expected_route2_gw = "10.1.1.1"; + static const Option data[] = { + { "classless_static_routes", "10.0.adfadf/44 10.17.66.41 192.168.10.0/24 192.168.1.1" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + + g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, + "*ignoring invalid classless static routes*"); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + g_test_assert_expected_messages (); + + /* Test falling back to old-style static routes if the classless static + * routes are invalid. + */ + + /* Routes */ + g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 2); + ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 32); + ip4_test_route (ip4_config, 1, expected_route2_dest, expected_route2_gw, 32); + + g_hash_table_destroy (options); +} + +static void +test_dhclient_invalid_classless_routes_3 (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const char *expected_route1_dest = "192.168.10.0"; + const char *expected_route1_gw = "192.168.1.1"; + static const Option data[] = { + { "rfc3442_classless_static_routes", "24 192 168 10 192 168 1 1 32 128 10 17 66 41" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + + g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, + "*ignoring invalid classless static routes*"); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + g_test_assert_expected_messages (); + + /* IP4 routes */ + g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1); + ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); + + g_hash_table_destroy (options); +} + +static void +test_dhcpcd_invalid_classless_routes_3 (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const char *expected_route1_dest = "192.168.10.0"; + const char *expected_route1_gw = "192.168.1.1"; + static Option data[] = { + { "classless_static_routes", "192.168.10.0/24 192.168.1.1 128/32 10.17.66.41" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + + g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, + "*DHCP provided invalid classless static route*"); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + g_test_assert_expected_messages (); + + /* IP4 routes */ + g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1); + ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); + + g_hash_table_destroy (options); +} + +static void +test_dhclient_gw_in_classless_routes (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const char *expected_route1_dest = "192.168.10.0"; + const char *expected_route1_gw = "192.168.1.1"; + const char *expected_gateway = "192.2.3.4"; + static Option data[] = { + { "rfc3442_classless_static_routes", "24 192 168 10 192 168 1 1 0 192 2 3 4" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + + /* IP4 routes */ + g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1); + ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); + + /* Gateway */ + ip4_test_gateway (ip4_config, expected_gateway); + + g_hash_table_destroy (options); +} + +static void +test_dhcpcd_gw_in_classless_routes (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const char *expected_route1_dest = "192.168.10.0"; + const char *expected_route1_gw = "192.168.1.1"; + const char *expected_gateway = "192.2.3.4"; + static Option data[] = { + { "classless_static_routes", "192.168.10.0/24 192.168.1.1 0.0.0.0/0 192.2.3.4" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + + /* IP4 routes */ + g_assert_cmpint (nm_ip4_config_get_num_routes (ip4_config), ==, 1); + ip4_test_route (ip4_config, 0, expected_route1_dest, expected_route1_gw, 24); + + /* Gateway */ + ip4_test_gateway (ip4_config, expected_gateway); + + g_hash_table_destroy (options); +} + +static void +test_escaped_domain_searches (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const char *expected_search0 = "host1"; + const char *expected_search1 = "host2"; + const char *expected_search2 = "host3"; + static const Option data[] = { + { "domain_search", "host1\\032host2\\032host3" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + + /* domain searches */ + g_assert_cmpint (nm_ip4_config_get_num_searches (ip4_config), ==, 3); + g_assert_cmpstr (nm_ip4_config_get_search (ip4_config, 0), ==, expected_search0); + g_assert_cmpstr (nm_ip4_config_get_search (ip4_config, 1), ==, expected_search1); + g_assert_cmpstr (nm_ip4_config_get_search (ip4_config, 2), ==, expected_search2); + + g_hash_table_destroy (options); +} + +static void +test_invalid_escaped_domain_searches (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + static const Option data[] = { + { "domain_search", "host1\\aahost2\\032host3" }, + { NULL, NULL } + }; + + options = fill_table (generic_options, NULL); + options = fill_table (data, options); + + g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, + "*invalid domain search*"); + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + g_test_assert_expected_messages (); + + /* domain searches */ + g_assert_cmpint (nm_ip4_config_get_num_searches (ip4_config), ==, 0); + + g_hash_table_destroy (options); +} + +static void +test_ip4_missing_prefix (const char *ip, guint32 expected_prefix) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const NMPlatformIP4Address *address; + + options = fill_table (generic_options, NULL); + g_hash_table_insert (options, "ip_address", (gpointer) ip); + g_hash_table_remove (options, "subnet_mask"); + + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + + g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1); + address = nm_ip4_config_get_address (ip4_config, 0); + g_assert (address); + g_assert_cmpint (address->plen, ==, expected_prefix); + + g_hash_table_destroy (options); +} + +static void +test_ip4_missing_prefix_24 (void) +{ + test_ip4_missing_prefix ("192.168.1.10", 24); +} + +static void +test_ip4_missing_prefix_16 (void) +{ + test_ip4_missing_prefix ("172.16.54.50", 16); +} + +static void +test_ip4_missing_prefix_8 (void) +{ + test_ip4_missing_prefix ("10.1.2.3", 8); +} + +static void +test_ip4_prefix_classless (void) +{ + GHashTable *options; + NMIP4Config *ip4_config; + const NMPlatformIP4Address *address; + + /* Ensure that the missing-subnet-mask handler doesn't mangle classless + * subnet masks at all. The handler should trigger only if the server + * doesn't send the subnet mask. + */ + + options = fill_table (generic_options, NULL); + g_hash_table_insert (options, "ip_address", "172.16.54.22"); + g_hash_table_insert (options, "subnet_mask", "255.255.252.0"); + + ip4_config = nm_dhcp_utils_ip4_config_from_options ("eth0", options, 0); + g_assert (ip4_config); + + g_assert_cmpint (nm_ip4_config_get_num_addresses (ip4_config), ==, 1); + address = nm_ip4_config_get_address (ip4_config, 0); + g_assert (address); + g_assert_cmpint (address->plen, ==, 22); + + g_hash_table_destroy (options); +} + +#define COMPARE_ID(src, is_str, expected, expected_len) \ +G_STMT_START { \ + gs_unref_bytes GBytes *b = NULL; \ + gconstpointer p; \ + gsize l; \ + \ + b = nm_dhcp_utils_client_id_string_to_bytes (src); \ + g_assert (b); \ + p = g_bytes_get_data (b, &l); \ + if (is_str) { \ + g_assert_cmpint (l, ==, expected_len + 1); \ + g_assert_cmpint (((const char *) p)[0], ==, 0); \ + g_assert (memcmp (p + 1, expected, expected_len) == 0); \ + } else { \ + g_assert_cmpint (l, ==, expected_len); \ + g_assert (memcmp (p, expected, expected_len) == 0); \ + } \ +} G_STMT_END + +static void +test_client_id_from_string (void) +{ + const char *nothex = "asdfasdfasdfasdfasdfasdfasdf"; + const char *allhex = "00:11:22:33:4:55:66:77:88"; + const guint8 allhex_bin[] = { 0x00, 0x11, 0x22, 0x33, 0x04, 0x55, 0x66, 0x77, 0x88 }; + const char *somehex = "00:11:22:33:44:55:asdfasdfasdf:99:10"; + const char *nocolons = "0011223344559910"; + const char *endcolon = "00:11:22:33:44:55:"; + + COMPARE_ID (nothex, TRUE, nothex, strlen (nothex)); + COMPARE_ID (allhex, FALSE, allhex_bin, sizeof (allhex_bin)); + COMPARE_ID (somehex, TRUE, somehex, strlen (somehex)); + COMPARE_ID (nocolons, TRUE, nocolons, strlen (nocolons)); + COMPARE_ID (endcolon, TRUE, endcolon, strlen (endcolon)); +} + +NMTST_DEFINE (); + +int main (int argc, char **argv) +{ + nmtst_init_assert_logging (&argc, &argv); + nm_logging_setup ("WARN", "DEFAULT", NULL, NULL); + + g_test_add_func ("/dhcp/generic-options", test_generic_options); + g_test_add_func ("/dhcp/wins-options", test_wins_options); + g_test_add_func ("/dhcp/classless-static-routes-1", test_classless_static_routes_1); + g_test_add_func ("/dhcp/classless-static-routes-2", test_classless_static_routes_2); + g_test_add_func ("/dhcp/fedora-dhclient-classless-static-routes", test_fedora_dhclient_classless_static_routes); + g_test_add_func ("/dhcp/dhclient-invalid-classless-routes-1", test_dhclient_invalid_classless_routes_1); + g_test_add_func ("/dhcp/dhcpcd-invalid-classless-routes-1", test_dhcpcd_invalid_classless_routes_1); + g_test_add_func ("/dhcp/dhclient-invalid-classless-routes-2", test_dhclient_invalid_classless_routes_2); + g_test_add_func ("/dhcp/dhcpcd-invalid-classless-routes-2", test_dhcpcd_invalid_classless_routes_2); + g_test_add_func ("/dhcp/dhclient-invalid-classless-routes-3", test_dhclient_invalid_classless_routes_3); + g_test_add_func ("/dhcp/dhcpcd-invalid-classless-routes-3", test_dhcpcd_invalid_classless_routes_3); + g_test_add_func ("/dhcp/dhclient-gw-in-classless-routes", test_dhclient_gw_in_classless_routes); + g_test_add_func ("/dhcp/dhcpcd-gw-in-classless-routes", test_dhcpcd_gw_in_classless_routes); + g_test_add_func ("/dhcp/escaped-domain-searches", test_escaped_domain_searches); + g_test_add_func ("/dhcp/invalid-escaped-domain-searches", test_invalid_escaped_domain_searches); + g_test_add_func ("/dhcp/ip4-missing-prefix-24", test_ip4_missing_prefix_24); + g_test_add_func ("/dhcp/ip4-missing-prefix-16", test_ip4_missing_prefix_16); + g_test_add_func ("/dhcp/ip4-missing-prefix-8", test_ip4_missing_prefix_8); + g_test_add_func ("/dhcp/ip4-prefix-classless", test_ip4_prefix_classless); + g_test_add_func ("/dhcp/client-id-from-string", test_client_id_from_string); + + return g_test_run (); +} + -- cgit v1.2.1 From d2dd3b2c90221fdfa40ca81a9fcffe6a777d95de Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Tue, 22 Jul 2014 12:38:20 -0500 Subject: dhcp: add systemd-based "internal" DHCP client We must also remove -Waggregate-return from m4/compiler-warnings.m4 because systemd uses aggregate return (correctly) in a couple cases, and we cannot keep single-level makefiles and override aggregate-return only for the systemd sub-library. This client currently only supports DHCPv4 because the base systemd code does not yet fully support DHCPv6. --- m4/compiler_warnings.m4 | 2 +- src/Makefile.am | 76 +++- src/dhcp-manager/nm-dhcp-manager.c | 6 + src/dhcp-manager/nm-dhcp-systemd.c | 808 +++++++++++++++++++++++++++++++++++++ src/dhcp-manager/nm-dhcp-systemd.h | 49 +++ 5 files changed, 938 insertions(+), 3 deletions(-) create mode 100644 src/dhcp-manager/nm-dhcp-systemd.c create mode 100644 src/dhcp-manager/nm-dhcp-systemd.h diff --git a/m4/compiler_warnings.m4 b/m4/compiler_warnings.m4 index 3fde795504..c05a26d098 100644 --- a/m4/compiler_warnings.m4 +++ b/m4/compiler_warnings.m4 @@ -29,7 +29,7 @@ if test "$GCC" = "yes" -a "$set_more_warnings" != "no"; then -fno-strict-aliasing -Wno-unused-but-set-variable \ -Wundef -Wimplicit-function-declaration \ -Wpointer-arith -Winit-self \ - -Wmissing-include-dirs -Waggregate-return; do + -Wmissing-include-dirs; do CFLAGS="$CFLAGS_MORE_WARNINGS $CFLAGS_EXTRA $option $CFLAGS_SAVED" AC_MSG_CHECKING([whether gcc understands $option]) AC_TRY_COMPILE([], [], diff --git a/src/Makefile.am b/src/Makefile.am index d568c00a46..60f7ef6a30 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,6 +48,74 @@ AM_CPPFLAGS = \ # primarily for its side effect of removing duplicates. AM_CPPFLAGS += $(foreach d,$(sort $(dir $(libNetworkManager_la_SOURCES))),-I$(top_srcdir)/src/$d) +noinst_LTLIBRARIES = libNetworkManager.la libsystemd-dhcp.la + +###################### +# libsystemd-dhcp +###################### + +SYSTEMD_DHCP_CFLAGS = \ + -I$(top_srcdir)/src/dhcp-manager/systemd-dhcp/src/systemd \ + -I$(top_srcdir)/src/dhcp-manager/systemd-dhcp/src/libsystemd-network \ + -I$(top_srcdir)/src/dhcp-manager/systemd-dhcp/src/shared \ + -I$(top_srcdir)/src/dhcp-manager/systemd-dhcp + +libsystemd_dhcp_la_SOURCES = \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-network.c \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-packet.c \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-internal.h \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-network.c \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-lease-internal.h \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-option.c \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/network-internal.c \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-option.c \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/network-internal.h \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-lease.c \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-protocol.h \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-internal.h \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp6-protocol.h \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/dhcp-lease-internal.h \ + dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c \ + dhcp-manager/systemd-dhcp/src/shared/async.h \ + dhcp-manager/systemd-dhcp/src/shared/time-util.h \ + dhcp-manager/systemd-dhcp/src/shared/siphash24.h \ + dhcp-manager/systemd-dhcp/src/shared/time-util.c \ + dhcp-manager/systemd-dhcp/src/shared/socket-util.h \ + dhcp-manager/systemd-dhcp/src/shared/sparse-endian.h \ + dhcp-manager/systemd-dhcp/src/shared/macro.h \ + dhcp-manager/systemd-dhcp/src/shared/refcnt.h \ + dhcp-manager/systemd-dhcp/src/shared/util.c \ + dhcp-manager/systemd-dhcp/src/shared/in-addr-util.c \ + dhcp-manager/systemd-dhcp/src/shared/siphash24.c \ + dhcp-manager/systemd-dhcp/src/shared/util.h \ + dhcp-manager/systemd-dhcp/src/shared/in-addr-util.h \ + dhcp-manager/systemd-dhcp/src/shared/list.h \ + dhcp-manager/systemd-dhcp/src/shared/fileio.h \ + dhcp-manager/systemd-dhcp/src/shared/fileio.c \ + dhcp-manager/systemd-dhcp/src/shared/strv.h \ + dhcp-manager/systemd-dhcp/src/shared/strv.c \ + dhcp-manager/systemd-dhcp/src/shared/utf8.h \ + dhcp-manager/systemd-dhcp/src/shared/utf8.c \ + dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-lease.h \ + dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-client.h \ + dhcp-manager/systemd-dhcp/src/systemd/sd-id128.h \ + dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp6-lease.h \ + dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp6-client.h \ + dhcp-manager/systemd-dhcp/src/systemd/sd-event.h \ + dhcp-manager/systemd-dhcp/src/systemd/_sd-common.h \ + dhcp-manager/systemd-dhcp/nm-sd-adapt.h \ + dhcp-manager/systemd-dhcp/nm-sd-adapt.c + +libsystemd_dhcp_la_CPPFLAGS = \ + -I$(top_srcdir)/include \ + $(SYSTEMD_DHCP_CFLAGS) \ + $(GLIB_CFLAGS) + +libsystemd_dhcp_la_LIBADD = \ + $(GLIB_LIBS) + ########################################### # NetworkManager ########################################### @@ -60,8 +128,6 @@ NetworkManager_SOURCES = \ NetworkManager_LDADD = libNetworkManager.la -noinst_LTLIBRARIES = libNetworkManager.la - nm_device_sources = \ devices/nm-device-bond.c \ devices/nm-device-bridge.c \ @@ -110,6 +176,8 @@ nm_sources = \ dhcp-manager/nm-dhcp-dhclient-utils.h \ dhcp-manager/nm-dhcp-dhcpcd.c \ dhcp-manager/nm-dhcp-dhcpcd.h \ + dhcp-manager/nm-dhcp-systemd.h \ + dhcp-manager/nm-dhcp-systemd.c \ dhcp-manager/nm-dhcp-manager.c \ dhcp-manager/nm-dhcp-manager.h \ \ @@ -328,6 +396,7 @@ AM_CPPFLAGS += \ $(LIBNDP_CFLAGS) \ $(LIBSOUP_CFLAGS) \ $(SYSTEMD_LOGIN_CFLAGS) \ + $(SYSTEMD_DHCP_CFLAGS) \ \ -DBINDIR=\"$(bindir)\" \ -DDATADIR=\"$(datadir)\" \ @@ -359,6 +428,7 @@ libNetworkManager_la_SOURCES = \ libNetworkManager_la_LIBADD = \ $(top_builddir)/libnm-core/libnm-core.la \ + libsystemd-dhcp.la \ $(DBUS_LIBS) \ $(GLIB_LIBS) \ $(GUDEV_LIBS) \ @@ -374,6 +444,8 @@ endif NetworkManager_LDFLAGS = -rdynamic +###################### + dbusservicedir = $(DBUS_SYS_DIR) dbusservice_DATA = org.freedesktop.NetworkManager.conf diff --git a/src/dhcp-manager/nm-dhcp-manager.c b/src/dhcp-manager/nm-dhcp-manager.c index b19590a69b..b98260d431 100644 --- a/src/dhcp-manager/nm-dhcp-manager.c +++ b/src/dhcp-manager/nm-dhcp-manager.c @@ -37,6 +37,7 @@ #include "nm-dhcp-manager.h" #include "nm-dhcp-dhclient.h" #include "nm-dhcp-dhcpcd.h" +#include "nm-dhcp-systemd.h" #include "nm-logging.h" #include "nm-dbus-manager.h" #include "nm-config.h" @@ -313,6 +314,9 @@ get_client_type (const char *client, GError **error) return NM_TYPE_DHCP_DHCPCD; } + if (!strcmp (client, "internal")) + return NM_TYPE_DHCP_SYSTEMD; + g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED, _("unsupported DHCP client '%s'"), client); @@ -537,6 +541,8 @@ nm_dhcp_manager_init (NMDhcpManager *self) if (priv->client_type == NM_TYPE_DHCP_DHCLIENT) priv->get_lease_ip_configs_func = nm_dhcp_dhclient_get_lease_ip_configs; + else if (priv->client_type == NM_TYPE_DHCP_SYSTEMD) + priv->get_lease_ip_configs_func = nm_dhcp_systemd_get_lease_ip_configs; else if (priv->client_type == G_TYPE_INVALID) { nm_log_warn (LOGD_DHCP, "No usable DHCP client found (%s)! DHCP configurations will fail.", error->message); diff --git a/src/dhcp-manager/nm-dhcp-systemd.c b/src/dhcp-manager/nm-dhcp-systemd.c new file mode 100644 index 0000000000..804be3590a --- /dev/null +++ b/src/dhcp-manager/nm-dhcp-systemd.c @@ -0,0 +1,808 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2014 Red Hat, Inc. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "nm-dhcp-systemd.h" +#include "nm-utils.h" +#include "nm-logging.h" +#include "nm-dhcp-utils.h" +#include "NetworkManagerUtils.h" + +#include "nm-sd-adapt.h" + +#include "sd-dhcp-client.h" +#include "sd-dhcp6-client.h" +#include "dhcp-protocol.h" +#include "dhcp-lease-internal.h" +#include "dhcp6-protocol.h" +#include "dhcp6-lease-internal.h" + +G_DEFINE_TYPE (NMDhcpSystemd, nm_dhcp_systemd, NM_TYPE_DHCP_CLIENT) + +#define NM_DHCP_SYSTEMD_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_SYSTEMD, NMDhcpSystemdPrivate)) + +typedef struct { + struct sd_dhcp_client *client4; + struct sd_dhcp6_client *client6; + char *lease_file; + + guint timeout_id; + guint request_count; + + gboolean privacy; +} NMDhcpSystemdPrivate; + +/************************************************************/ + +#define DHCP_OPTION_NIS_DOMAIN 40 +#define DHCP_OPTION_NIS_SERVERS 41 +#define DHCP_OPTION_DOMAIN_SEARCH 119 +#define DHCP_OPTION_RFC3442_ROUTES 121 +#define DHCP_OPTION_MS_ROUTES 249 +#define DHCP_OPTION_WPAD 252 + +/* Internal values */ +#define DHCP_OPTION_IP_ADDRESS 1024 +#define DHCP_OPTION_EXPIRY 1025 +#define DHCP6_OPTION_IP_ADDRESS 1026 +#define DHCP6_OPTION_PREFIXLEN 1027 +#define DHCP6_OPTION_PREFERRED_LIFE 1028 +#define DHCP6_OPTION_MAX_LIFE 1029 +#define DHCP6_OPTION_STARTS 1030 +#define DHCP6_OPTION_LIFE_STARTS 1031 +#define DHCP6_OPTION_RENEW 1032 +#define DHCP6_OPTION_REBIND 1033 +#define DHCP6_OPTION_IAID 1034 + +typedef struct { + guint num; + const char *name; + gboolean include; +} ReqOption; + +#define REQPREFIX "requested_" + +static const ReqOption dhcp4_requests[] = { + { DHCP_OPTION_SUBNET_MASK, REQPREFIX "subnet_mask", TRUE }, + { DHCP_OPTION_TIME_OFFSET, REQPREFIX "time_offset", TRUE }, + { DHCP_OPTION_ROUTER, REQPREFIX "routers", TRUE }, + { DHCP_OPTION_DOMAIN_NAME_SERVER, REQPREFIX "domain_name_servers", TRUE }, + { DHCP_OPTION_HOST_NAME, REQPREFIX "host_name", TRUE }, + { DHCP_OPTION_DOMAIN_NAME, REQPREFIX "domain_name", TRUE }, + { DHCP_OPTION_INTERFACE_MTU, REQPREFIX "interface_mtu", TRUE }, + { DHCP_OPTION_BROADCAST, REQPREFIX "broadcast_address", TRUE }, + { DHCP_OPTION_STATIC_ROUTE, REQPREFIX "static_routes", TRUE }, + { DHCP_OPTION_NIS_DOMAIN, REQPREFIX "nis_domain", TRUE }, + { DHCP_OPTION_NIS_SERVERS, REQPREFIX "nis_servers", TRUE }, + { DHCP_OPTION_NTP_SERVER, REQPREFIX "ntp_servers", TRUE }, + { DHCP_OPTION_SERVER_IDENTIFIER, REQPREFIX "dhcp_server_identifier", TRUE }, + { DHCP_OPTION_DOMAIN_SEARCH, REQPREFIX "domain_search", TRUE }, + { DHCP_OPTION_CLASSLESS_STATIC_ROUTE, REQPREFIX "rfc3442_classless_static_routes", TRUE }, + { DHCP_OPTION_MS_ROUTES, REQPREFIX "ms_classless_static_routes", TRUE }, + { DHCP_OPTION_WPAD, REQPREFIX "wpad", TRUE }, + + /* Internal values */ + { DHCP_OPTION_IP_ADDRESS_LEASE_TIME, REQPREFIX "expiry", FALSE }, + { DHCP_OPTION_CLIENT_IDENTIFIER, REQPREFIX "dhcp_client_identifier", FALSE }, + { DHCP_OPTION_IP_ADDRESS, REQPREFIX "ip_address", FALSE }, + { 0, NULL, FALSE } +}; + +static const ReqOption dhcp6_requests[] = { + { DHCP6_OPTION_CLIENTID, REQPREFIX "dhcp6_client_id", TRUE }, + + /* Don't request server ID by default; some servers don't reply to + * Information Requests that request the Server ID. + */ + { DHCP6_OPTION_SERVERID, REQPREFIX "dhcp6_server_id", FALSE }, + + { DHCP6_OPTION_DNS_SERVERS, REQPREFIX "dhcp6_name_servers", TRUE }, + { DHCP6_OPTION_DOMAIN_LIST, REQPREFIX "dhcp6_domain_search", TRUE }, + { DHCP6_OPTION_SNTP_SERVERS, REQPREFIX "dhcp6_sntp_servers", TRUE }, + + /* Internal values */ + { DHCP6_OPTION_IP_ADDRESS, REQPREFIX "ip6_address", FALSE }, + { DHCP6_OPTION_PREFIXLEN, REQPREFIX "ip6_prefixlen", FALSE }, + { DHCP6_OPTION_PREFERRED_LIFE, REQPREFIX "preferred_life", FALSE }, + { DHCP6_OPTION_MAX_LIFE, REQPREFIX "max_life", FALSE }, + { DHCP6_OPTION_STARTS, REQPREFIX "starts", FALSE }, + { DHCP6_OPTION_LIFE_STARTS, REQPREFIX "life_starts", FALSE }, + { DHCP6_OPTION_RENEW, REQPREFIX "renew", FALSE }, + { DHCP6_OPTION_REBIND, REQPREFIX "rebind", FALSE }, + { DHCP6_OPTION_IAID, REQPREFIX "iaid", FALSE }, + { 0, NULL, FALSE } +}; + +static void +take_option (GHashTable *options, + const ReqOption *requests, + guint option, + char *value) +{ + guint i; + + g_return_if_fail (value != NULL); + + for (i = 0; requests[i].name; i++) { + if (requests[i].num == option) { + g_hash_table_insert (options, + (gpointer) (requests[i].name + STRLEN (REQPREFIX)), + value); + break; + } + } + /* Option should always be found */ + g_assert (requests[i].name); +} + +static void +add_option (GHashTable *options, const ReqOption *requests, guint option, const char *value) +{ + if (options) + take_option (options, requests, option, g_strdup (value)); +} + +static void +add_option_u32 (GHashTable *options, const ReqOption *requests, guint option, guint32 value) +{ + if (options) + take_option (options, requests, option, g_strdup_printf ("%u", value)); +} + +static void +add_requests_to_options (GHashTable *options, const ReqOption *requests) +{ + guint i; + + for (i = 0; options && requests[i].name; i++) { + if (requests[i].include) + g_hash_table_insert (options, (gpointer) requests[i].name, g_strdup ("1")); + } +} + +#define LOG_LEASE(domain, ...) \ +G_STMT_START { \ + if (log_lease) { \ + nm_log (LOGL_INFO, (domain), __VA_ARGS__); \ + } \ +} G_STMT_END + +static NMIP4Config * +lease_to_ip4_config (sd_dhcp_lease *lease, + GHashTable *options, + guint32 default_priority, + gboolean log_lease, + GError **error) +{ + NMIP4Config *ip4_config = NULL; + struct in_addr tmp_addr; + const struct in_addr *addr_list; + char buf[INET_ADDRSTRLEN]; + const char *str; + guint32 lifetime = 0, plen = 0, i; + NMPlatformIP4Address address; + GString *l; + struct sd_dhcp_route *routes; + guint16 mtu; + int r, num; + gint64 end_time; + + r = sd_dhcp_lease_get_address (lease, &tmp_addr); + if (r < 0) { + g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED, + "failed to read address from lease"); + return NULL; + } + + ip4_config = nm_ip4_config_new (); + + /* Address */ + memset (&address, 0, sizeof (address)); + address.address = tmp_addr.s_addr; + str = nm_utils_inet4_ntop (tmp_addr.s_addr, NULL); + LOG_LEASE (LOGD_DHCP4, " address %s", str); + add_option (options, dhcp4_requests, DHCP_OPTION_IP_ADDRESS, str); + + /* Prefix/netmask */ + r = sd_dhcp_lease_get_netmask (lease, &tmp_addr); + if (r < 0) { + /* Get default netmask for the IP according to appropriate class. */ + plen = nm_utils_ip4_get_default_prefix (address.address); + LOG_LEASE (LOGD_DHCP4, " plen %d (default)", plen); + } else { + plen = nm_utils_ip4_netmask_to_prefix (tmp_addr.s_addr); + LOG_LEASE (LOGD_DHCP4, " plen %d", plen); + } + address.plen = plen; + tmp_addr.s_addr = nm_utils_ip4_prefix_to_netmask (plen); + add_option (options, + dhcp4_requests, + DHCP_OPTION_SUBNET_MASK, + nm_utils_inet4_ntop (tmp_addr.s_addr, NULL)); + + /* Lease time */ + r = sd_dhcp_lease_get_lifetime (lease, &lifetime); + if (r < 0) + lifetime = 3600; /* one hour */ + address.timestamp = nm_utils_get_monotonic_timestamp_s (); + address.lifetime = address.preferred = lifetime; + end_time = (gint64) time (NULL) + lifetime; + add_option_u32 (options, + dhcp4_requests, + DHCP_OPTION_IP_ADDRESS_LEASE_TIME, + (guint) CLAMP (end_time, 0, G_MAXUINT32 - 1)); + + address.source = NM_IP_CONFIG_SOURCE_DHCP; + nm_ip4_config_add_address (ip4_config, &address); + + /* Gateway */ + r = sd_dhcp_lease_get_router (lease, &tmp_addr); + if (r == 0) { + nm_ip4_config_set_gateway (ip4_config, tmp_addr.s_addr); + str = nm_utils_inet4_ntop (tmp_addr.s_addr, NULL); + LOG_LEASE (LOGD_DHCP4, " gateway %s", str); + add_option (options, dhcp4_requests, DHCP_OPTION_ROUTER, str); + } + + /* DNS Servers */ + num = sd_dhcp_lease_get_dns (lease, &addr_list); + if (num > 0) { + l = g_string_sized_new (30); + for (i = 0; i < num; i++) { + if (addr_list[i].s_addr) { + nm_ip4_config_add_nameserver (ip4_config, addr_list[i].s_addr); + str = nm_utils_inet4_ntop (addr_list[i].s_addr, NULL); + LOG_LEASE (LOGD_DHCP4, " nameserver '%s'", str); + g_string_append_printf (l, "%s%s", l->len ? " " : "", str); + } + } + if (l->len) + add_option (options, dhcp4_requests, DHCP_OPTION_DOMAIN_NAME_SERVER, l->str); + g_string_free (l, TRUE); + } + + /* Domain Name */ + r = sd_dhcp_lease_get_domainname (lease, &str); + if (r == 0) { + /* Multiple domains sometimes stuffed into the option */ + char **domains = g_strsplit (str, " ", 0); + char **s; + + for (s = domains; *s; s++) { + LOG_LEASE (LOGD_DHCP4, " domain name '%s'", *s); + nm_ip4_config_add_domain (ip4_config, *s); + } + g_strfreev (domains); + add_option (options, dhcp4_requests, DHCP_OPTION_DOMAIN_NAME, str); + } + + /* Hostname */ + r = sd_dhcp_lease_get_hostname (lease, &str); + if (r == 0) { + LOG_LEASE (LOGD_DHCP4, " hostname '%s'", str); + add_option (options, dhcp4_requests, DHCP_OPTION_HOST_NAME, str); + } + + /* Routes */ + num = sd_dhcp_lease_get_routes (lease, &routes); + if (num > 0) { + l = g_string_sized_new (30); + for (i = 0; i < num; i++) { + NMPlatformIP4Route route; + const char *gw_str; + + memset (&route, 0, sizeof (route)); + route.network = routes[i].dst_addr.s_addr; + route.plen = routes[i].dst_prefixlen; + route.gateway = routes[i].gw_addr.s_addr; + route.source = NM_IP_CONFIG_SOURCE_DHCP; + route.metric = default_priority; + nm_ip4_config_add_route (ip4_config, &route); + + str = nm_utils_inet4_ntop (route.network, buf); + gw_str = nm_utils_inet4_ntop (route.gateway, NULL); + LOG_LEASE (LOGD_DHCP4, " static route %s/%d gw %s", str, route.plen, gw_str); + + g_string_append_printf (l, "%s%s/%d %s", l->len ? " " : "", str, route.plen, gw_str); + } + add_option (options, dhcp4_requests, DHCP_OPTION_RFC3442_ROUTES, l->str); + g_string_free (l, TRUE); + } + + /* MTU */ + r = sd_dhcp_lease_get_mtu (lease, &mtu); + if (r == 0 && mtu) { + nm_ip4_config_set_mtu (ip4_config, mtu, NM_IP_CONFIG_SOURCE_DHCP); + add_option_u32 (options, dhcp4_requests, DHCP_OPTION_INTERFACE_MTU, mtu); + LOG_LEASE (LOGD_DHCP4, " mtu %u", mtu); + } + + /* NTP servers */ + num = sd_dhcp_lease_get_ntp(lease, &addr_list); + if (num > 0) { + l = g_string_sized_new (30); + for (i = 0; i < num; i++) { + str = nm_utils_inet4_ntop (addr_list[i].s_addr, buf); + g_string_append_printf (l, "%s%s", l->len ? " " : "", str); + } + add_option (options, dhcp4_requests, DHCP_OPTION_NTP_SERVER, l->str); + g_string_free (l, TRUE); + } + + return ip4_config; +} + +/************************************************************/ + +static char * +get_leasefile_path (const char *iface, const char *uuid, gboolean ipv6) +{ + return g_strdup_printf (NMSTATEDIR "/internal%s-%s-%s.lease", + ipv6 ? "6" : "", + uuid, + iface); +} + +GSList * +nm_dhcp_systemd_get_lease_ip_configs (const char *iface, + const char *uuid, + gboolean ipv6) +{ + GSList *leases = NULL; + gs_free char *path = NULL; + sd_dhcp_lease *lease = NULL; + NMIP4Config *ip4_config; + int r; + + if (ipv6) + return NULL; + + path = get_leasefile_path (iface, uuid, FALSE); + r = sd_dhcp_lease_load (path, &lease); + if (r == 0) { + ip4_config = lease_to_ip4_config (lease, NULL, 0, FALSE, NULL); + if (ip4_config) + leases = g_slist_append (leases, ip4_config); + } + + return leases; +} + +/************************************************************/ + +static void +bound4_handle (NMDhcpSystemd *self) +{ + NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (self); + const char *iface = nm_dhcp_client_get_iface (NM_DHCP_CLIENT (self)); + sd_dhcp_lease *lease; + NMIP4Config *ip4_config; + GHashTable *options; + GError *error = NULL; + int r; + + nm_log_dbg (LOGD_DHCP4, "(%s): lease available", iface); + + r = sd_dhcp_client_get_lease (priv->client4, &lease); + if (r < 0 || !lease) { + nm_log_warn (LOGD_DHCP4, "(%s): no lease!", iface); + nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), NM_DHCP_STATE_FAIL, NULL, NULL); + return; + } + + options = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free); + ip4_config = lease_to_ip4_config (lease, + options, + nm_dhcp_client_get_priority (NM_DHCP_CLIENT (self)), + TRUE, + &error); + if (ip4_config) { + add_requests_to_options (options, dhcp4_requests); + sd_dhcp_lease_save (lease, priv->lease_file); + + nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), + NM_DHCP_STATE_BOUND, + G_OBJECT (ip4_config), + options); + } else { + nm_log_warn (LOGD_DHCP4, "(%s): %s", iface, error->message); + nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), NM_DHCP_STATE_FAIL, NULL, NULL); + g_clear_error (&error); + } + + sd_dhcp_lease_unref (lease); + g_hash_table_destroy (options); + g_clear_object (&ip4_config); +} + +static void +dhcp_event_cb (sd_dhcp_client *client, int event, gpointer user_data) +{ + NMDhcpSystemd *self = NM_DHCP_SYSTEMD (user_data); + NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (self); + const char *iface = nm_dhcp_client_get_iface (NM_DHCP_CLIENT (self)); + + g_assert (priv->client4 == client); + + nm_log_dbg (LOGD_DHCP4, "(%s): DHCPv4 client event %d", iface, event); + + switch (event) { + case DHCP_EVENT_EXPIRED: + case DHCP_EVENT_STOP: + nm_dhcp_client_set_state (NM_DHCP_CLIENT (user_data), NM_DHCP_STATE_FAIL, NULL, NULL); + break; + case DHCP_EVENT_RENEW: + case DHCP_EVENT_IP_CHANGE: + case DHCP_EVENT_IP_ACQUIRE: + bound4_handle (self); + break; + default: + nm_log_warn (LOGD_DHCP4, "(%s): unhandled DHCP event %d", iface, event); + break; + } +} + +static guint16 +get_arp_type (const GByteArray *hwaddr) +{ + if (hwaddr->len == ETH_ALEN) + return ARPHRD_ETHER; + else if (hwaddr->len == INFINIBAND_ALEN) + return ARPHRD_INFINIBAND; + else + g_assert_not_reached (); +} + +static gboolean +ip4_start (NMDhcpClient *client, + const char *dhcp_client_id, + const char *dhcp_anycast_addr, + const char *hostname) +{ + NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (client); + const char *iface = nm_dhcp_client_get_iface (client); + const GByteArray *hwaddr; + sd_dhcp_lease *lease = NULL; + const uint8_t *client_id = NULL; + size_t client_id_len = 0; + struct in_addr last_addr; + int r, i; + + g_assert (priv->client4 == NULL); + g_assert (priv->client6 == NULL); + + g_free (priv->lease_file); + priv->lease_file = get_leasefile_path (iface, nm_dhcp_client_get_uuid (client), FALSE); + + r = sd_dhcp_client_new (&priv->client4); + if (r < 0) { + nm_log_warn (LOGD_DHCP4, "(%s): failed to create DHCPv4 client (%d)", iface, r); + return FALSE; + } + + r = sd_dhcp_client_attach_event (priv->client4, NULL, 0); + if (r < 0) { + nm_log_warn (LOGD_DHCP4, "(%s): failed to attach DHCP event (%d)", iface, r); + goto error; + } + + hwaddr = nm_dhcp_client_get_hw_addr (client); + if (hwaddr) { + r = sd_dhcp_client_set_mac (priv->client4, + hwaddr->data, + hwaddr->len, + get_arp_type (hwaddr)); + if (r < 0) { + nm_log_warn (LOGD_DHCP4, "(%s): failed to set DHCP MAC address (%d)", iface, r); + goto error; + } + } + + r = sd_dhcp_client_set_index (priv->client4, nm_dhcp_client_get_ifindex (client)); + if (r < 0) { + nm_log_warn (LOGD_DHCP4, "(%s): failed to set DHCP ifindex (%d)", iface, r); + goto error; + } + + r = sd_dhcp_client_set_callback (priv->client4, dhcp_event_cb, client); + if (r < 0) { + nm_log_warn (LOGD_DHCP4, "(%s): failed to set DHCP callback (%d)", iface, r); + goto error; + } + + r = sd_dhcp_client_set_request_broadcast (priv->client4, true); + if (r < 0) { + nm_log_warn (LOGD_DHCP4, "(%s): failed to set DHCP broadcast (%d)", iface, r); + goto error; + } + + sd_dhcp_lease_load (priv->lease_file, &lease); + + if (lease) { + r = sd_dhcp_lease_get_address (lease, &last_addr); + if (r == 0) { + r = sd_dhcp_client_set_request_address (priv->client4, &last_addr); + if (r < 0) { + nm_log_warn (LOGD_DHCP4, "(%s): failed to set last IPv4 address (%d)", iface, r); + goto error; + } + } + } + + if (dhcp_client_id) { + gs_unref_bytes GBytes *b = NULL; + + b = nm_dhcp_utils_client_id_string_to_bytes (dhcp_client_id); + if (b) { + client_id = (const guint8 *) g_bytes_get_data (b, &client_id_len); + g_assert (client_id && client_id_len); + sd_dhcp_client_set_client_id (priv->client4, + client_id[0], + client_id + 1, + client_id_len - 1); + } + } else { + r = sd_dhcp_lease_get_client_id (lease, &client_id, &client_id_len); + if (r == 0 && client_id_len) { + sd_dhcp_client_set_client_id (priv->client4, + client_id[0], + client_id + 1, + client_id_len - 1); + } + } + + if (lease) + sd_dhcp_lease_unref (lease); + + /* Add requested options */ + for (i = 0; dhcp4_requests[i].name; i++) { + if (dhcp4_requests[i].include) + sd_dhcp_client_set_request_option (priv->client4, dhcp4_requests[i].num); + } + + if (hostname) { + r = sd_dhcp_client_set_hostname (priv->client4, hostname); + if (r < 0) { + nm_log_warn (LOGD_DHCP4, "(%s): failed to set DHCP hostname (%d)", iface, r); + goto error; + } + } + + r = sd_dhcp_client_start (priv->client4); + if (r < 0) { + nm_log_warn (LOGD_DHCP4, "(%s): failed to start DHCP (%d)", iface, r); + goto error; + } + + return TRUE; + +error: + sd_dhcp_client_unref (priv->client4); + priv->client4 = NULL; + return FALSE; +} + +static void +bound6_handle (NMDhcpSystemd *self) +{ + /* not yet supported... */ + nm_log_warn (LOGD_DHCP6, "(%s): internal DHCP does not yet support DHCPv6", + nm_dhcp_client_get_iface (NM_DHCP_CLIENT (self))); + nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), NM_DHCP_STATE_FAIL, NULL, NULL); +} + +static void +dhcp6_event_cb (sd_dhcp6_client *client, int event, gpointer user_data) +{ + NMDhcpSystemd *self = NM_DHCP_SYSTEMD (user_data); + NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (self); + const char *iface = nm_dhcp_client_get_iface (NM_DHCP_CLIENT (self)); + + g_assert (priv->client6 == client); + + nm_log_dbg (LOGD_DHCP6, "(%s): DHCPv6 client event %d", iface, event); + + switch (event) { + case DHCP6_EVENT_RETRANS_MAX: + nm_dhcp_client_set_state (NM_DHCP_CLIENT (user_data), NM_DHCP_STATE_TIMEOUT, NULL, NULL); + break; + case DHCP6_EVENT_RESEND_EXPIRE: + case DHCP6_EVENT_STOP: + nm_dhcp_client_set_state (NM_DHCP_CLIENT (user_data), NM_DHCP_STATE_FAIL, NULL, NULL); + break; + case DHCP6_EVENT_IP_ACQUIRE: + bound6_handle (self); + break; + default: + nm_log_warn (LOGD_DHCP6, "(%s): unhandled DHCPv6 event %d", iface, event); + break; + } +} + +static gboolean +ip6_start (NMDhcpClient *client, + const char *dhcp_anycast_addr, + const char *hostname, + gboolean info_only, + NMSettingIP6ConfigPrivacy privacy, + const GByteArray *duid) +{ + NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (client); + const char *iface = nm_dhcp_client_get_iface (client); + const GByteArray *hwaddr; + int r, i; + + g_assert (priv->client4 == NULL); + g_assert (priv->client6 == NULL); + g_return_val_if_fail (duid != NULL, FALSE); + + g_free (priv->lease_file); + priv->lease_file = get_leasefile_path (iface, nm_dhcp_client_get_uuid (client), TRUE); + + r = sd_dhcp6_client_new (&priv->client6); + if (r < 0) { + nm_log_warn (LOGD_DHCP6, "(%s): failed to create DHCPv6 client (%d)", iface, r); + return FALSE; + } + + /* NM stores the entire DUID which includes the uint16 "type", while systemd + * wants the type passed separately from the following data. + */ + r = sd_dhcp6_client_set_duid (priv->client6, + ntohs (((const guint16 *) duid->data)[0]), + duid->data + 2, + duid->len - 2); + if (r < 0) { + nm_log_warn (LOGD_DHCP6, "(%s): failed to create DHCPv6 client (%d)", iface, r); + return FALSE; + } + + r = sd_dhcp6_client_attach_event (priv->client6, NULL, 0); + if (r < 0) { + nm_log_warn (LOGD_DHCP6, "(%s): failed to attach DHCP event (%d)", iface, r); + goto error; + } + + hwaddr = nm_dhcp_client_get_hw_addr (client); + if (hwaddr) { + r = sd_dhcp6_client_set_mac (priv->client6, + hwaddr->data, + hwaddr->len, + get_arp_type (hwaddr)); + if (r < 0) { + nm_log_warn (LOGD_DHCP6, "(%s): failed to set DHCP MAC address (%d)", iface, r); + goto error; + } + } + + r = sd_dhcp6_client_set_index (priv->client6, nm_dhcp_client_get_ifindex (client)); + if (r < 0) { + nm_log_warn (LOGD_DHCP6, "(%s): failed to set DHCP ifindex (%d)", iface, r); + goto error; + } + + r = sd_dhcp6_client_set_ifname (priv->client6, iface); + if (r < 0) { + nm_log_warn (LOGD_DHCP6, "(%s): failed to set DHCP ifname (%d)", iface, r); + goto error; + } + + r = sd_dhcp6_client_set_callback (priv->client6, dhcp6_event_cb, client); + if (r < 0) { + nm_log_warn (LOGD_DHCP6, "(%s): failed to set DHCP callback (%d)", iface, r); + goto error; + } + + /* Add requested options */ + for (i = 0; dhcp6_requests[i].name; i++) { + if (dhcp6_requests[i].include) + sd_dhcp6_client_set_request_option (priv->client6, dhcp6_requests[i].num); + } + + r = sd_dhcp6_client_start (priv->client6); + if (r < 0) { + nm_log_warn (LOGD_DHCP6, "(%s): failed to start DHCP (%d)", iface, r); + goto error; + } + + return TRUE; + +error: + sd_dhcp6_client_unref (priv->client6); + priv->client6 = NULL; + return FALSE; +} + +static void +stop (NMDhcpClient *client, gboolean release, const GByteArray *duid) +{ + NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (client); + int r = 0; + + if (priv->client4) + r = sd_dhcp_client_stop (priv->client4); + else if (priv->client6) + r = sd_dhcp6_client_stop (priv->client6); + + if (r) { + nm_log_warn (priv->client6 ? LOGD_DHCP6 : LOGD_DHCP4, + "(%s): failed to stop DHCP client (%d)", + nm_dhcp_client_get_iface (client), + r); + } +} + +/***************************************************/ + +static void +nm_dhcp_systemd_init (NMDhcpSystemd *self) +{ +} + +static void +dispose (GObject *object) +{ + NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (object); + + g_clear_pointer (&priv->lease_file, g_free); + + if (priv->client4) { + sd_dhcp_client_stop (priv->client4); + sd_dhcp_client_unref (priv->client4); + priv->client4 = NULL; + } + + if (priv->client6) { + sd_dhcp6_client_stop (priv->client6); + sd_dhcp6_client_unref (priv->client6); + priv->client6 = NULL; + } + + G_OBJECT_CLASS (nm_dhcp_systemd_parent_class)->dispose (object); +} + +static void +nm_dhcp_systemd_class_init (NMDhcpSystemdClass *sdhcp_class) +{ + NMDhcpClientClass *client_class = NM_DHCP_CLIENT_CLASS (sdhcp_class); + GObjectClass *object_class = G_OBJECT_CLASS (sdhcp_class); + + g_type_class_add_private (sdhcp_class, sizeof (NMDhcpSystemdPrivate)); + + /* virtual methods */ + object_class->dispose = dispose; + + client_class->ip4_start = ip4_start; + client_class->ip6_start = ip6_start; + client_class->stop = stop; +} + diff --git a/src/dhcp-manager/nm-dhcp-systemd.h b/src/dhcp-manager/nm-dhcp-systemd.h new file mode 100644 index 0000000000..05e8e8da33 --- /dev/null +++ b/src/dhcp-manager/nm-dhcp-systemd.h @@ -0,0 +1,49 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2014 Red Hat, Inc. + */ + +#ifndef NM_DHCP_SYSTEMD_H +#define NM_DHCP_SYSTEMD_H + +#include +#include + +#include "nm-dhcp-client.h" + +#define NM_TYPE_DHCP_SYSTEMD (nm_dhcp_systemd_get_type ()) +#define NM_DHCP_SYSTEMD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DHCP_SYSTEMD, NMDhcpSystemd)) +#define NM_DHCP_SYSTEMD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DHCP_SYSTEMD, NMDhcpSystemdClass)) +#define NM_IS_DHCP_SYSTEMD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DHCP_SYSTEMD)) +#define NM_IS_DHCP_SYSTEMD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DHCP_SYSTEMD)) +#define NM_DHCP_SYSTEMD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DHCP_SYSTEMD, NMDhcpSystemdClass)) + +typedef struct { + NMDhcpClient parent; +} NMDhcpSystemd; + +typedef struct { + NMDhcpClientClass parent; +} NMDhcpSystemdClass; + +GType nm_dhcp_systemd_get_type (void); + +GSList *nm_dhcp_systemd_get_lease_ip_configs (const char *iface, + const char *uuid, + gboolean ipv6); + +#endif /* NM_DHCP_SYSTEMD_H */ + -- cgit v1.2.1 From 801fc34d6fae4f1431348ba41264f5846b687dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Tue, 14 Oct 2014 12:37:00 +0200 Subject: clients: move secret agent to common directory The agent code will be shared by both nmtui and nmcli. --- clients/common/nm-secret-agent-simple.c | 639 +++++++++++++++++++++++++++++++ clients/common/nm-secret-agent-simple.h | 57 +++ clients/tui/Makefile.am | 5 +- clients/tui/nmt-password-dialog.c | 14 +- clients/tui/nmt-secret-agent.c | 644 -------------------------------- clients/tui/nmt-secret-agent.h | 57 --- clients/tui/nmtui-connect.c | 20 +- po/POTFILES.in | 2 +- 8 files changed, 717 insertions(+), 721 deletions(-) create mode 100644 clients/common/nm-secret-agent-simple.c create mode 100644 clients/common/nm-secret-agent-simple.h delete mode 100644 clients/tui/nmt-secret-agent.c delete mode 100644 clients/tui/nmt-secret-agent.h diff --git a/clients/common/nm-secret-agent-simple.c b/clients/common/nm-secret-agent-simple.c new file mode 100644 index 0000000000..d85c0a5cfe --- /dev/null +++ b/clients/common/nm-secret-agent-simple.c @@ -0,0 +1,639 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Copyright 2011-2013 Red Hat, Inc. + * Copyright 2011 Giovanni Campagna + */ + +/** + * SECTION:nm-secret-agent-simple + * @short_description: A simple secret agent for NetworkManager + * + * #NMSecretAgentSimple is the secret agent used by nmtui-connect and nmcli. + * + * This is a stripped-down version of gnome-shell's ShellNetworkAgent, + * with bits of the corresponding JavaScript code squished down into + * it. It is intended to eventually be generic enough that it could + * replace ShellNetworkAgent. + */ + +#include "config.h" + +#include +#include + +#include "nm-secret-agent-simple.h" + +G_DEFINE_TYPE (NMSecretAgentSimple, nm_secret_agent_simple, NM_TYPE_SECRET_AGENT) + +#define NM_SECRET_AGENT_SIMPLE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SECRET_AGENT_SIMPLE, NMSecretAgentSimplePrivate)) + +enum { + REQUEST_SECRETS, + + LAST_SIGNAL +}; + +static guint signals[LAST_SIGNAL] = { 0 }; + +typedef struct { + NMSecretAgentSimple *self; + + gchar *request_id; + NMConnection *connection; + gchar **hints; + NMSecretAgentGetSecretsFunc callback; + gpointer callback_data; +} NMSecretAgentSimpleRequest; + +typedef struct { + /* */ + GHashTable *requests; +} NMSecretAgentSimplePrivate; + +static void +nm_secret_agent_simple_request_free (gpointer data) +{ + NMSecretAgentSimpleRequest *request = data; + + g_object_unref (request->self); + g_object_unref (request->connection); + g_strfreev (request->hints); + + g_slice_free (NMSecretAgentSimpleRequest, request); +} + +static void +nm_secret_agent_simple_init (NMSecretAgentSimple *agent) +{ + NMSecretAgentSimplePrivate *priv = NM_SECRET_AGENT_SIMPLE_GET_PRIVATE (agent); + + priv->requests = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, nm_secret_agent_simple_request_free); +} + +static void +nm_secret_agent_simple_finalize (GObject *object) +{ + NMSecretAgentSimplePrivate *priv = NM_SECRET_AGENT_SIMPLE_GET_PRIVATE (object); + GError *error; + GHashTableIter iter; + gpointer key; + gpointer value; + + error = g_error_new (NM_SECRET_AGENT_ERROR, + NM_SECRET_AGENT_ERROR_AGENT_CANCELED, + "The secret agent is going away"); + + g_hash_table_iter_init (&iter, priv->requests); + while (g_hash_table_iter_next (&iter, &key, &value)) { + NMSecretAgentSimpleRequest *request = value; + + request->callback (NM_SECRET_AGENT (object), + request->connection, + NULL, error, + request->callback_data); + } + + g_hash_table_destroy (priv->requests); + g_error_free (error); + + G_OBJECT_CLASS (nm_secret_agent_simple_parent_class)->finalize (object); +} + +static gboolean +strv_has (gchar **haystack, + gchar *needle) +{ + gchar *iter; + + for (iter = *haystack; iter; iter++) { + if (g_strcmp0 (iter, needle) == 0) + return TRUE; + } + + return FALSE; +} + +/** + * NMSecretAgentSimpleSecret: + * @name: the user-visible name of the secret. Eg, "WEP Passphrase". + * @value: the value of the secret + * @password: %TRUE if this secret represents a password, %FALSE + * if it represents non-secret data. + * + * A single "secret" being requested. + */ + +typedef struct { + NMSecretAgentSimpleSecret base; + + NMSetting *setting; + char *property; +} NMSecretAgentSimpleSecretReal; + +static void +nm_secret_agent_simple_secret_free (NMSecretAgentSimpleSecret *secret) +{ + NMSecretAgentSimpleSecretReal *real = (NMSecretAgentSimpleSecretReal *)secret; + + g_free (secret->name); + g_free (secret->value); + g_free (real->property); + g_clear_object (&real->setting); + + g_slice_free (NMSecretAgentSimpleSecretReal, real); +} + +static NMSecretAgentSimpleSecret * +nm_secret_agent_simple_secret_new (const char *name, + NMSetting *setting, + const char *property, + gboolean password) +{ + NMSecretAgentSimpleSecretReal *real; + + real = g_slice_new0 (NMSecretAgentSimpleSecretReal); + real->base.name = g_strdup (name); + real->base.password = password; + + if (setting) { + real->setting = g_object_ref (setting); + real->property = g_strdup (property); + + g_object_get (setting, property, &real->base.value, NULL); + } + + return &real->base; +} + +static gboolean +add_8021x_secrets (NMSecretAgentSimpleRequest *request, + GPtrArray *secrets) +{ + NMSetting8021x *s_8021x = nm_connection_get_setting_802_1x (request->connection); + const char *eap_method; + NMSecretAgentSimpleSecret *secret; + + eap_method = nm_setting_802_1x_get_eap_method (s_8021x, 0); + if (!eap_method) + return FALSE; + + if ( !strcmp (eap_method, "md5") + || !strcmp (eap_method, "leap") + || !strcmp (eap_method, "ttls") + || !strcmp (eap_method, "peap")) { + /* TTLS and PEAP are actually much more complicated, but this complication + * is not visible here since we only care about phase2 authentication + * (and don't even care of which one) + */ + secret = nm_secret_agent_simple_secret_new (_("Username"), + NM_SETTING (s_8021x), + NM_SETTING_802_1X_IDENTITY, + FALSE); + g_ptr_array_add (secrets, secret); + secret = nm_secret_agent_simple_secret_new (_("Password"), + NM_SETTING (s_8021x), + NM_SETTING_802_1X_PASSWORD, + TRUE); + g_ptr_array_add (secrets, secret); + return TRUE; + } + + if (!strcmp (eap_method, "tls")) { + secret = nm_secret_agent_simple_secret_new (_("Identity"), + NM_SETTING (s_8021x), + NM_SETTING_802_1X_IDENTITY, + FALSE); + g_ptr_array_add (secrets, secret); + secret = nm_secret_agent_simple_secret_new (_("Private key password"), + NM_SETTING (s_8021x), + NM_SETTING_802_1X_PRIVATE_KEY_PASSWORD, + TRUE); + g_ptr_array_add (secrets, secret); + return TRUE; + } + + return FALSE; +} + +static gboolean +add_wireless_secrets (NMSecretAgentSimpleRequest *request, + GPtrArray *secrets) +{ + NMSettingWirelessSecurity *s_wsec = nm_connection_get_setting_wireless_security (request->connection); + const char *key_mgmt = nm_setting_wireless_security_get_key_mgmt (s_wsec); + NMSecretAgentSimpleSecret *secret; + + if (!key_mgmt) + return FALSE; + + if (!strcmp (key_mgmt, "wpa-none") || !strcmp (key_mgmt, "wpa-psk")) { + secret = nm_secret_agent_simple_secret_new (_("Password"), + NM_SETTING (s_wsec), + NM_SETTING_WIRELESS_SECURITY_PSK, + TRUE); + g_ptr_array_add (secrets, secret); + return TRUE; + } + + if (!strcmp (key_mgmt, "none")) { + int index; + char *key; + + index = nm_setting_wireless_security_get_wep_tx_keyidx (s_wsec); + key = g_strdup_printf ("wep-key%d", index); + secret = nm_secret_agent_simple_secret_new (_("Key"), + NM_SETTING (s_wsec), + key, + TRUE); + g_free (key); + + g_ptr_array_add (secrets, secret); + return TRUE; + } + + if (!strcmp (key_mgmt, "iee8021x")) { + if (!g_strcmp0 (nm_setting_wireless_security_get_auth_alg (s_wsec), "leap")) { + secret = nm_secret_agent_simple_secret_new (_("Password"), + NM_SETTING (s_wsec), + NM_SETTING_WIRELESS_SECURITY_LEAP_PASSWORD, + TRUE); + g_ptr_array_add (secrets, secret); + return TRUE; + } else + return add_8021x_secrets (request, secrets); + } + + if (!strcmp (key_mgmt, "wpa-eap")) + return add_8021x_secrets (request, secrets); + + return FALSE; +} + +static gboolean +add_pppoe_secrets (NMSecretAgentSimpleRequest *request, + GPtrArray *secrets) +{ + NMSettingPppoe *s_pppoe = nm_connection_get_setting_pppoe (request->connection); + NMSecretAgentSimpleSecret *secret; + + secret = nm_secret_agent_simple_secret_new (_("Username"), + NM_SETTING (s_pppoe), + NM_SETTING_PPPOE_USERNAME, + FALSE); + g_ptr_array_add (secrets, secret); + secret = nm_secret_agent_simple_secret_new (_("Service"), + NM_SETTING (s_pppoe), + NM_SETTING_PPPOE_SERVICE, + FALSE); + g_ptr_array_add (secrets, secret); + secret = nm_secret_agent_simple_secret_new (_("Password"), + NM_SETTING (s_pppoe), + NM_SETTING_PPPOE_PASSWORD, + TRUE); + g_ptr_array_add (secrets, secret); + return TRUE; +} + +static void +request_secrets_from_ui (NMSecretAgentSimpleRequest *request) +{ + GPtrArray *secrets; + NMSecretAgentSimpleSecret *secret; + const char *title; + char *msg; + gboolean ok = TRUE; + + secrets = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_secret_agent_simple_secret_free); + + if (nm_connection_is_type (request->connection, NM_SETTING_WIRELESS_SETTING_NAME)) { + NMSettingWireless *s_wireless; + GBytes *ssid; + char *ssid_utf8; + + s_wireless = nm_connection_get_setting_wireless (request->connection); + ssid = nm_setting_wireless_get_ssid (s_wireless); + ssid_utf8 = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL), + g_bytes_get_size (ssid)); + + title = _("Authentication required by wireless network"); + msg = g_strdup_printf (_("Passwords or encryption keys are required to access the wireless network '%s'."), ssid_utf8); + + ok = add_wireless_secrets (request, secrets); + } else if (nm_connection_is_type (request->connection, NM_SETTING_WIRED_SETTING_NAME)) { + NMSettingConnection *s_con; + + s_con = nm_connection_get_setting_connection (request->connection); + + title = _("Wired 802.1X authentication"); + msg = NULL; + + secret = nm_secret_agent_simple_secret_new (_("Network name"), + NM_SETTING (s_con), + NM_SETTING_CONNECTION_ID, + FALSE); + g_ptr_array_add (secrets, secret); + ok = add_8021x_secrets (request, secrets); + } else if (nm_connection_is_type (request->connection, NM_SETTING_PPPOE_SETTING_NAME)) { + title = _("DSL authentication"); + msg = NULL; + + ok = add_pppoe_secrets (request, secrets); + } else if (nm_connection_is_type (request->connection, NM_SETTING_GSM_SETTING_NAME)) { + NMSettingGsm *s_gsm = nm_connection_get_setting_gsm (request->connection); + + if (strv_has (request->hints, "pin")) { + title = _("PIN code required"); + msg = g_strdup (_("PIN code is needed for the mobile broadband device")); + + secret = nm_secret_agent_simple_secret_new (_("PIN"), + NM_SETTING (s_gsm), + NM_SETTING_GSM_PIN, + FALSE); + g_ptr_array_add (secrets, secret); + } else { + title = _("Mobile broadband network password"); + msg = g_strdup_printf (_("A password is required to connect to '%s'."), + nm_connection_get_id (request->connection)); + + secret = nm_secret_agent_simple_secret_new (_("Password"), + NM_SETTING (s_gsm), + NM_SETTING_GSM_PASSWORD, + TRUE); + g_ptr_array_add (secrets, secret); + } + } else if (nm_connection_is_type (request->connection, NM_SETTING_CDMA_SETTING_NAME)) { + NMSettingCdma *s_cdma = nm_connection_get_setting_cdma (request->connection); + + title = _("Mobile broadband network password"); + msg = g_strdup_printf (_("A password is required to connect to '%s'."), + nm_connection_get_id (request->connection)); + + secret = nm_secret_agent_simple_secret_new (_("Password"), + NM_SETTING (s_cdma), + NM_SETTING_CDMA_PASSWORD, + TRUE); + g_ptr_array_add (secrets, secret); + } else if (nm_connection_is_type (request->connection, NM_SETTING_BLUETOOTH_SETTING_NAME)) { + NMSetting *setting; + + setting = nm_connection_get_setting_by_name (request->connection, NM_SETTING_GSM_SETTING_NAME); + if (!setting) + setting = nm_connection_get_setting_by_name (request->connection, NM_SETTING_CDMA_SETTING_NAME); + + title = _("Mobile broadband network password"); + msg = g_strdup_printf (_("A password is required to connect to '%s'."), + nm_connection_get_id (request->connection)); + + secret = nm_secret_agent_simple_secret_new (_("Password"), + setting, + "password", + TRUE); + g_ptr_array_add (secrets, secret); + } else + ok = FALSE; + + if (!ok) { + g_ptr_array_unref (secrets); + return; + } + + g_signal_emit (request->self, signals[REQUEST_SECRETS], 0, + request->request_id, title, msg, secrets); +} + +static void +nm_secret_agent_simple_get_secrets (NMSecretAgent *agent, + NMConnection *connection, + const gchar *connection_path, + const gchar *setting_name, + const gchar **hints, + NMSecretAgentGetSecretsFlags flags, + NMSecretAgentGetSecretsFunc callback, + gpointer callback_data) +{ + NMSecretAgentSimple *self = NM_SECRET_AGENT_SIMPLE (agent); + NMSecretAgentSimplePrivate *priv = NM_SECRET_AGENT_SIMPLE_GET_PRIVATE (self); + NMSecretAgentSimpleRequest *request; + NMSettingConnection *s_con; + const char *connection_type; + char *request_id; + GError *error; + + request_id = g_strdup_printf ("%s/%s", connection_path, setting_name); + if (g_hash_table_lookup (priv->requests, request_id) != NULL) { + /* We already have a request pending for this (connection, setting) */ + error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_FAILED, + "Request for %s secrets already pending", request_id); + nope: + callback (agent, connection, NULL, error, callback_data); + g_error_free (error); + g_free (request_id); + return; + } + + s_con = nm_connection_get_setting_connection (connection); + connection_type = nm_setting_connection_get_connection_type (s_con); + + if (!strcmp (connection_type, NM_SETTING_VPN_SETTING_NAME)) { + /* We don't support VPN secrets yet */ + error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_NO_SECRETS, + "VPN secrets not supported"); + goto nope; + } + + if (!(flags & NM_SECRET_AGENT_GET_SECRETS_FLAG_ALLOW_INTERACTION)) { + /* We don't do stored passwords */ + error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_NO_SECRETS, + "Stored passwords not supported"); + goto nope; + } + + request = g_slice_new (NMSecretAgentSimpleRequest); + request->self = g_object_ref (self); + request->connection = g_object_ref (connection); + request->hints = g_strdupv ((gchar **)hints); + request->callback = callback; + request->callback_data = callback_data; + request->request_id = request_id; + g_hash_table_replace (priv->requests, request->request_id, request); + + request_secrets_from_ui (request); +} + +/** + * nm_secret_agent_simple_response: + * @self: the #NMSecretAgentSimple + * @request_id: the request ID being responded to + * @secrets: (allow-none): the array of secrets, or %NULL + * + * Response to a #NMSecretAgentSimple::get-secrets signal. + * + * If the user provided secrets, the caller should set the + * corresponding value fields in the + * #NMSecretAgentSimpleSecrets (freeing any initial values they had), and + * pass the array to nm_secret_agent_simple_response(). If the user + * cancelled the request, @secrets should be NULL. + */ +void +nm_secret_agent_simple_response (NMSecretAgentSimple *self, + const char *request_id, + GPtrArray *secrets) +{ + NMSecretAgentSimplePrivate *priv; + NMSecretAgentSimpleRequest *request; + GVariant *dict = NULL; + GError *error = NULL; + int i; + + g_return_if_fail (NM_IS_SECRET_AGENT_SIMPLE (self)); + + priv = NM_SECRET_AGENT_SIMPLE_GET_PRIVATE (self); + request = g_hash_table_lookup (priv->requests, request_id); + g_return_if_fail (request != NULL); + + if (secrets) { + GVariantBuilder conn_builder, *setting_builder; + GHashTable *settings; + GHashTableIter iter; + const char *name; + + settings = g_hash_table_new (g_str_hash, g_str_equal); + for (i = 0; i < secrets->len; i++) { + NMSecretAgentSimpleSecretReal *secret = secrets->pdata[i]; + + setting_builder = g_hash_table_lookup (settings, nm_setting_get_name (secret->setting)); + if (!setting_builder) { + setting_builder = g_variant_builder_new (NM_VARIANT_TYPE_SETTING); + g_hash_table_insert (settings, (char *) nm_setting_get_name (secret->setting), + setting_builder); + } + + g_variant_builder_add (setting_builder, "{sv}", + secret->property, + g_variant_new_string (secret->base.value)); + } + + g_variant_builder_init (&conn_builder, NM_VARIANT_TYPE_CONNECTION); + g_hash_table_iter_init (&iter, settings); + while (g_hash_table_iter_next (&iter, (gpointer *) &name, (gpointer *) &setting_builder)) + g_variant_builder_add (&conn_builder, "{sa{sv}}", name, setting_builder); + dict = g_variant_builder_end (&conn_builder); + g_hash_table_destroy (settings); + } else { + error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_USER_CANCELED, + "User cancelled"); + } + + request->callback (NM_SECRET_AGENT (self), request->connection, dict, error, request->callback_data); + + g_clear_error (&error); + g_hash_table_remove (priv->requests, request_id); +} + +static void +nm_secret_agent_simple_cancel_get_secrets (NMSecretAgent *agent, + const gchar *connection_path, + const gchar *setting_name) +{ + /* We don't support cancellation. Sorry! */ +} + +static void +nm_secret_agent_simple_save_secrets (NMSecretAgent *agent, + NMConnection *connection, + const gchar *connection_path, + NMSecretAgentSaveSecretsFunc callback, + gpointer callback_data) +{ + /* We don't support secret storage */ + callback (agent, connection, NULL, callback_data); +} + +static void +nm_secret_agent_simple_delete_secrets (NMSecretAgent *agent, + NMConnection *connection, + const gchar *connection_path, + NMSecretAgentDeleteSecretsFunc callback, + gpointer callback_data) +{ + /* We don't support secret storage, so there's nothing to delete. */ + callback (agent, connection, NULL, callback_data); +} + +void +nm_secret_agent_simple_class_init (NMSecretAgentSimpleClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + NMSecretAgentClass *agent_class = NM_SECRET_AGENT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (NMSecretAgentSimplePrivate)); + + gobject_class->finalize = nm_secret_agent_simple_finalize; + + agent_class->get_secrets = nm_secret_agent_simple_get_secrets; + agent_class->cancel_get_secrets = nm_secret_agent_simple_cancel_get_secrets; + agent_class->save_secrets = nm_secret_agent_simple_save_secrets; + agent_class->delete_secrets = nm_secret_agent_simple_delete_secrets; + + /** + * NMSecretAgentSimple::request-secrets: + * @agent: the #NMSecretAgentSimple + * @request_id: request ID, to eventually pass to + * nm_secret_agent_simple_response(). + * @title: a title for the password dialog + * @prompt: a prompt message for the password dialog + * @secrets: (element-type #NMSecretAgentSimpleSecret): array of secrets + * being requested. + * + * Emitted when the agent requires secrets from the user. + * + * The application should ask user for the secrets. For example, + * nmtui should create a password dialog (#NmtPasswordDialog) + * with the given title and prompt, and an entry for each + * element of @secrets. If any of the secrets already have a + * value filled in, the corresponding entry + * should be initialized to that value. + * + * When the dialog is complete, the app must call + * nm_secret_agent_simple_response() with the results. + */ + signals[REQUEST_SECRETS] = g_signal_new ("request-secrets", + G_TYPE_FROM_CLASS (klass), + 0, 0, NULL, NULL, NULL, + G_TYPE_NONE, + 4, + G_TYPE_STRING, /* request_id */ + G_TYPE_STRING, /* title */ + G_TYPE_STRING, /* prompt */ + G_TYPE_PTR_ARRAY); +} + +/** + * nm_secret_agent_simple_new: + * @name: the identifier of secret agent + * + * Creates a new #NMSecretAgentSimple. + * + * Returns: a new #NMSecretAgentSimple + */ +NMSecretAgent * +nm_secret_agent_simple_new (const char *name) +{ + return g_initable_new (NM_TYPE_SECRET_AGENT_SIMPLE, NULL, NULL, + NM_SECRET_AGENT_IDENTIFIER, name, + NULL); +} diff --git a/clients/common/nm-secret-agent-simple.h b/clients/common/nm-secret-agent-simple.h new file mode 100644 index 0000000000..afac59c49f --- /dev/null +++ b/clients/common/nm-secret-agent-simple.h @@ -0,0 +1,57 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Copyright 2013 - 2014 Red Hat, Inc. + */ + +#ifndef __NM_SECRET_AGENT_SIMPLE_H__ +#define __NM_SECRET_AGENT_SIMPLE_H__ + +#include + +G_BEGIN_DECLS + +#define NM_TYPE_SECRET_AGENT_SIMPLE (nm_secret_agent_simple_get_type ()) +#define NM_SECRET_AGENT_SIMPLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_SECRET_AGENT, NMSecretAgentSimple)) +#define NM_SECRET_AGENT_SIMPLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_SECRET_AGENT, NMSecretAgentSimpleClass)) +#define NM_IS_SECRET_AGENT_SIMPLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_SECRET_AGENT)) +#define NM_IS_SECRET_AGENT_SIMPLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_SECRET_AGENT)) +#define NM_SECRET_AGENT_SIMPLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_SECRET_AGENT, NMSecretAgentSimpleClass)) + +typedef struct { + NMSecretAgent parent; + +} NMSecretAgentSimple; + +typedef struct { + NMSecretAgentClass parent; + +} NMSecretAgentSimpleClass; + +typedef struct { + char *name, *value; + gboolean password; +} NMSecretAgentSimpleSecret; + +GType nm_secret_agent_simple_get_type (void); + +NMSecretAgent *nm_secret_agent_simple_new (const char *name); +void nm_secret_agent_simple_response (NMSecretAgentSimple *self, + const char *request_id, + GPtrArray *secrets); + +G_END_DECLS + +#endif /* __NM_SECRET_AGENT_SIMPLE_H__ */ diff --git a/clients/tui/Makefile.am b/clients/tui/Makefile.am index fb5ad8fcf0..290ef155a3 100644 --- a/clients/tui/Makefile.am +++ b/clients/tui/Makefile.am @@ -10,6 +10,7 @@ AM_CPPFLAGS= \ -I$(top_srcdir)/libnm \ -I$(top_builddir)/libnm \ -I$(srcdir)/newt \ + -I$(top_srcdir)/clients/common \ $(GLIB_CFLAGS) \ $(NEWT_CFLAGS) \ $(GUDEV_CFLAGS) \ @@ -106,14 +107,14 @@ nmtui_SOURCES = \ nmt-route-entry.h \ nmt-route-table.c \ nmt-route-table.h \ - nmt-secret-agent.c \ - nmt-secret-agent.h \ nmt-slave-list.c \ nmt-slave-list.h \ nmt-utils.c \ nmt-utils.h \ nmt-widget-list.c \ nmt-widget-list.h \ + $(srcdir)/../common/nm-secret-agent-simple.c \ + $(srcdir)/../common/nm-secret-agent-simple.h \ $(NULL) nmtui_LDADD = \ diff --git a/clients/tui/nmt-password-dialog.c b/clients/tui/nmt-password-dialog.c index 22ba85d2c5..aeece1211f 100644 --- a/clients/tui/nmt-password-dialog.c +++ b/clients/tui/nmt-password-dialog.c @@ -29,7 +29,7 @@ #include #include "nmt-password-dialog.h" -#include "nmt-secret-agent.h" +#include "nm-secret-agent-simple.h" #include "nmtui.h" G_DEFINE_TYPE (NmtPasswordDialog, nmt_password_dialog, NMT_TYPE_NEWT_FORM) @@ -60,10 +60,10 @@ enum { /** * nmt_password_dialog_new: - * @request_id: the request ID from the #NmtSecretAgent + * @request_id: the request ID from the #NMSecretAgentSimple * @title: the dialog title * @prompt: the prompt text to display - * @secrets: (element-type #NmtSecretAgentSecret): the secrets requested + * @secrets: (element-type #NMSecretAgentSimpleSecret): the secrets requested * * Creates a new #NmtPasswordDialog to request passwords from * the user. @@ -109,7 +109,7 @@ maybe_save_input_and_exit (NmtNewtWidget *widget, priv->succeeded = TRUE; for (i = 0; i < priv->secrets->len; i++) { - NmtSecretAgentSecret *secret = priv->secrets->pdata[i]; + NMSecretAgentSimpleSecret *secret = priv->secrets->pdata[i]; g_free (secret->value); g_object_get (priv->entries->pdata[i], "text", &secret->value, NULL); @@ -143,7 +143,7 @@ nmt_password_dialog_constructed (GObject *object) secret_grid = NMT_NEWT_GRID (widget); for (i = 0; i < priv->secrets->len; i++) { - NmtSecretAgentSecret *secret = priv->secrets->pdata[i]; + NMSecretAgentSimpleSecret *secret = priv->secrets->pdata[i]; NmtNewtEntryFlags flags; widget = nmt_newt_label_new (secret->name); @@ -258,7 +258,7 @@ nmt_password_dialog_class_init (NmtPasswordDialogClass *dialog_class) /** * NmtPasswordDialog:request-id: * - * The request ID from the #NmtSecretAgent + * The request ID from the #NMSecretAgentSimple */ g_object_class_install_property (object_class, PROP_REQUEST_ID, @@ -284,7 +284,7 @@ nmt_password_dialog_class_init (NmtPasswordDialogClass *dialog_class) * * The array of request secrets * - * Element-Type: #NmtSecretAgentSecret. + * Element-Type: #NMSecretAgentSimpleSecret. */ g_object_class_install_property (object_class, PROP_SECRETS, diff --git a/clients/tui/nmt-secret-agent.c b/clients/tui/nmt-secret-agent.c deleted file mode 100644 index 40ab9c14cb..0000000000 --- a/clients/tui/nmt-secret-agent.c +++ /dev/null @@ -1,644 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * Copyright 2011-2013 Red Hat, Inc. - * Copyright 2011 Giovanni Campagna - */ - -/** - * SECTION:nmt-secret-agent - * @short_description: A secret agent - * - * #NmtSecretAgent is the secret agent used by nmtui-connect. - * - * This is a stripped-down version of gnome-shell's ShellNetworkAgent, - * with bits of the corresponding JavaScript code squished down into - * it. It is intended to eventually be generic enough that it could - * replace ShellNetworkAgent. - */ - -#include "config.h" - -#include -#include - -#include "nmt-secret-agent.h" -#include "nmt-newt.h" - -G_DEFINE_TYPE (NmtSecretAgent, nmt_secret_agent, NM_TYPE_SECRET_AGENT) - -#define NMT_SECRET_AGENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NMT_TYPE_SECRET_AGENT, NmtSecretAgentPrivate)) - -enum { - REQUEST_SECRETS, - - LAST_SIGNAL -}; - -static guint signals[LAST_SIGNAL] = { 0 }; - -typedef struct { - NmtSecretAgent *self; - - gchar *request_id; - NMConnection *connection; - gchar **hints; - NMSecretAgentGetSecretsFunc callback; - gpointer callback_data; -} NmtSecretAgentRequest; - -typedef struct { - /* */ - GHashTable *requests; -} NmtSecretAgentPrivate; - -static void -nmt_secret_agent_request_free (gpointer data) -{ - NmtSecretAgentRequest *request = data; - - g_object_unref (request->self); - g_object_unref (request->connection); - g_strfreev (request->hints); - - g_slice_free (NmtSecretAgentRequest, request); -} - -static void -nmt_secret_agent_init (NmtSecretAgent *agent) -{ - NmtSecretAgentPrivate *priv = NMT_SECRET_AGENT_GET_PRIVATE (agent); - - priv->requests = g_hash_table_new_full (g_str_hash, g_str_equal, - g_free, nmt_secret_agent_request_free); -} - -static void -nmt_secret_agent_finalize (GObject *object) -{ - NmtSecretAgentPrivate *priv = NMT_SECRET_AGENT_GET_PRIVATE (object); - GError *error; - GHashTableIter iter; - gpointer key; - gpointer value; - - error = g_error_new (NM_SECRET_AGENT_ERROR, - NM_SECRET_AGENT_ERROR_AGENT_CANCELED, - "The secret agent is going away"); - - g_hash_table_iter_init (&iter, priv->requests); - while (g_hash_table_iter_next (&iter, &key, &value)) { - NmtSecretAgentRequest *request = value; - - request->callback (NM_SECRET_AGENT (object), - request->connection, - NULL, error, - request->callback_data); - } - - g_hash_table_destroy (priv->requests); - g_error_free (error); - - G_OBJECT_CLASS (nmt_secret_agent_parent_class)->finalize (object); -} - -static gboolean -strv_has (gchar **haystack, - gchar *needle) -{ - gchar *iter; - - for (iter = *haystack; iter; iter++) { - if (g_strcmp0 (iter, needle) == 0) - return TRUE; - } - - return FALSE; -} - -/** - * NmtSecretAgentSecret: - * @name: the user-visible name of the secret. Eg, "WEP Passphrase". - * @value: the value of the secret - * @password: %TRUE if this secret represents a password, %FALSE - * if it represents non-secret data. - * - * A single "secret" being requested. - */ - -typedef struct { - NmtSecretAgentSecret base; - - NMSetting *setting; - char *property; - - NmtNewtEntryValidator validator; - gpointer validator_data; -} NmtSecretAgentSecretReal; - -static void -nmt_secret_agent_secret_free (NmtSecretAgentSecret *secret) -{ - NmtSecretAgentSecretReal *real = (NmtSecretAgentSecretReal *)secret; - - g_free (secret->name); - g_free (secret->value); - g_free (real->property); - g_clear_object (&real->setting); - - g_slice_free (NmtSecretAgentSecretReal, real); -} - -static NmtSecretAgentSecret * -nmt_secret_agent_secret_new (const char *name, - NMSetting *setting, - const char *property, - gboolean password) -{ - NmtSecretAgentSecretReal *real; - - real = g_slice_new0 (NmtSecretAgentSecretReal); - real->base.name = g_strdup (name); - real->base.password = password; - - if (setting) { - real->setting = g_object_ref (setting); - real->property = g_strdup (property); - - g_object_get (setting, property, &real->base.value, NULL); - } - - return &real->base; -} - -static gboolean -add_8021x_secrets (NmtSecretAgentRequest *request, - GPtrArray *secrets) -{ - NMSetting8021x *s_8021x = nm_connection_get_setting_802_1x (request->connection); - const char *eap_method; - NmtSecretAgentSecret *secret; - - eap_method = nm_setting_802_1x_get_eap_method (s_8021x, 0); - if (!eap_method) - return FALSE; - - if ( !strcmp (eap_method, "md5") - || !strcmp (eap_method, "leap") - || !strcmp (eap_method, "ttls") - || !strcmp (eap_method, "peap")) { - /* TTLS and PEAP are actually much more complicated, but this complication - * is not visible here since we only care about phase2 authentication - * (and don't even care of which one) - */ - secret = nmt_secret_agent_secret_new (_("Username"), - NM_SETTING (s_8021x), - NM_SETTING_802_1X_IDENTITY, - FALSE); - g_ptr_array_add (secrets, secret); - secret = nmt_secret_agent_secret_new (_("Password"), - NM_SETTING (s_8021x), - NM_SETTING_802_1X_PASSWORD, - TRUE); - g_ptr_array_add (secrets, secret); - return TRUE; - } - - if (!strcmp (eap_method, "tls")) { - secret = nmt_secret_agent_secret_new (_("Identity"), - NM_SETTING (s_8021x), - NM_SETTING_802_1X_IDENTITY, - FALSE); - g_ptr_array_add (secrets, secret); - secret = nmt_secret_agent_secret_new (_("Private key password"), - NM_SETTING (s_8021x), - NM_SETTING_802_1X_PRIVATE_KEY_PASSWORD, - TRUE); - g_ptr_array_add (secrets, secret); - return TRUE; - } - - return FALSE; -} - -static gboolean -add_wireless_secrets (NmtSecretAgentRequest *request, - GPtrArray *secrets) -{ - NMSettingWirelessSecurity *s_wsec = nm_connection_get_setting_wireless_security (request->connection); - const char *key_mgmt = nm_setting_wireless_security_get_key_mgmt (s_wsec); - NmtSecretAgentSecret *secret; - - if (!key_mgmt) - return FALSE; - - if (!strcmp (key_mgmt, "wpa-none") || !strcmp (key_mgmt, "wpa-psk")) { - secret = nmt_secret_agent_secret_new (_("Password"), - NM_SETTING (s_wsec), - NM_SETTING_WIRELESS_SECURITY_PSK, - TRUE); - g_ptr_array_add (secrets, secret); - return TRUE; - } - - if (!strcmp (key_mgmt, "none")) { - int index; - char *key; - - index = nm_setting_wireless_security_get_wep_tx_keyidx (s_wsec); - key = g_strdup_printf ("wep-key%d", index); - secret = nmt_secret_agent_secret_new (_("Key"), - NM_SETTING (s_wsec), - key, - TRUE); - g_free (key); - -#if 0 - nmt_secret_agent_secret_set_validator (secret, static_wep_key_validate, - nm_setting_wireless_security_get_wep_key_type (s_wsec)); -#endif - g_ptr_array_add (secrets, secret); - return TRUE; - } - - if (!strcmp (key_mgmt, "iee8021x")) { - if (!g_strcmp0 (nm_setting_wireless_security_get_auth_alg (s_wsec), "leap")) { - secret = nmt_secret_agent_secret_new (_("Password"), - NM_SETTING (s_wsec), - NM_SETTING_WIRELESS_SECURITY_LEAP_PASSWORD, - TRUE); - g_ptr_array_add (secrets, secret); - return TRUE; - } else - return add_8021x_secrets (request, secrets); - } - - if (!strcmp (key_mgmt, "wpa-eap")) - return add_8021x_secrets (request, secrets); - - return FALSE; -} - -static gboolean -add_pppoe_secrets (NmtSecretAgentRequest *request, - GPtrArray *secrets) -{ - NMSettingPppoe *s_pppoe = nm_connection_get_setting_pppoe (request->connection); - NmtSecretAgentSecret *secret; - - secret = nmt_secret_agent_secret_new (_("Username"), - NM_SETTING (s_pppoe), - NM_SETTING_PPPOE_USERNAME, - FALSE); - g_ptr_array_add (secrets, secret); - secret = nmt_secret_agent_secret_new (_("Service"), - NM_SETTING (s_pppoe), - NM_SETTING_PPPOE_SERVICE, - FALSE); - g_ptr_array_add (secrets, secret); - secret = nmt_secret_agent_secret_new (_("Password"), - NM_SETTING (s_pppoe), - NM_SETTING_PPPOE_PASSWORD, - TRUE); - g_ptr_array_add (secrets, secret); - return TRUE; -} - -static void -request_secrets_from_ui (NmtSecretAgentRequest *request) -{ - GPtrArray *secrets; - NmtSecretAgentSecret *secret; - const char *title; - char *msg; - gboolean ok = TRUE; - - secrets = g_ptr_array_new_with_free_func ((GDestroyNotify) nmt_secret_agent_secret_free); - - if (nm_connection_is_type (request->connection, NM_SETTING_WIRELESS_SETTING_NAME)) { - NMSettingWireless *s_wireless; - GBytes *ssid; - char *ssid_utf8; - - s_wireless = nm_connection_get_setting_wireless (request->connection); - ssid = nm_setting_wireless_get_ssid (s_wireless); - ssid_utf8 = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL), - g_bytes_get_size (ssid)); - - title = _("Authentication required by wireless network"); - msg = g_strdup_printf (_("Passwords or encryption keys are required to access the wireless network '%s'."), ssid_utf8); - - ok = add_wireless_secrets (request, secrets); - } else if (nm_connection_is_type (request->connection, NM_SETTING_WIRED_SETTING_NAME)) { - NMSettingConnection *s_con; - - s_con = nm_connection_get_setting_connection (request->connection); - - title = _("Wired 802.1X authentication"); - msg = NULL; - - secret = nmt_secret_agent_secret_new (_("Network name"), - NM_SETTING (s_con), - NM_SETTING_CONNECTION_ID, - FALSE); - g_ptr_array_add (secrets, secret); - ok = add_8021x_secrets (request, secrets); - } else if (nm_connection_is_type (request->connection, NM_SETTING_PPPOE_SETTING_NAME)) { - title = _("DSL authentication"); - msg = NULL; - - ok = add_pppoe_secrets (request, secrets); - } else if (nm_connection_is_type (request->connection, NM_SETTING_GSM_SETTING_NAME)) { - NMSettingGsm *s_gsm = nm_connection_get_setting_gsm (request->connection); - - if (strv_has (request->hints, "pin")) { - title = _("PIN code required"); - msg = g_strdup (_("PIN code is needed for the mobile broadband device")); - - secret = nmt_secret_agent_secret_new (_("PIN"), - NM_SETTING (s_gsm), - NM_SETTING_GSM_PIN, - FALSE); - g_ptr_array_add (secrets, secret); - } else { - title = _("Mobile broadband network password"); - msg = g_strdup_printf (_("A password is required to connect to '%s'."), - nm_connection_get_id (request->connection)); - - secret = nmt_secret_agent_secret_new (_("Password"), - NM_SETTING (s_gsm), - NM_SETTING_GSM_PASSWORD, - TRUE); - g_ptr_array_add (secrets, secret); - } - } else if (nm_connection_is_type (request->connection, NM_SETTING_CDMA_SETTING_NAME)) { - NMSettingCdma *s_cdma = nm_connection_get_setting_cdma (request->connection); - - title = _("Mobile broadband network password"); - msg = g_strdup_printf (_("A password is required to connect to '%s'."), - nm_connection_get_id (request->connection)); - - secret = nmt_secret_agent_secret_new (_("Password"), - NM_SETTING (s_cdma), - NM_SETTING_CDMA_PASSWORD, - TRUE); - g_ptr_array_add (secrets, secret); - } else if (nm_connection_is_type (request->connection, NM_SETTING_BLUETOOTH_SETTING_NAME)) { - NMSetting *setting; - - setting = nm_connection_get_setting_by_name (request->connection, NM_SETTING_GSM_SETTING_NAME); - if (!setting) - setting = nm_connection_get_setting_by_name (request->connection, NM_SETTING_CDMA_SETTING_NAME); - - title = _("Mobile broadband network password"); - msg = g_strdup_printf (_("A password is required to connect to '%s'."), - nm_connection_get_id (request->connection)); - - secret = nmt_secret_agent_secret_new (_("Password"), - setting, - "password", - TRUE); - g_ptr_array_add (secrets, secret); - } else - ok = FALSE; - - if (!ok) { - g_ptr_array_unref (secrets); - return; - } - - g_signal_emit (request->self, signals[REQUEST_SECRETS], 0, - request->request_id, title, msg, secrets); -} - -static void -nmt_secret_agent_get_secrets (NMSecretAgent *agent, - NMConnection *connection, - const gchar *connection_path, - const gchar *setting_name, - const gchar **hints, - NMSecretAgentGetSecretsFlags flags, - NMSecretAgentGetSecretsFunc callback, - gpointer callback_data) -{ - NmtSecretAgent *self = NMT_SECRET_AGENT (agent); - NmtSecretAgentPrivate *priv = NMT_SECRET_AGENT_GET_PRIVATE (self); - NmtSecretAgentRequest *request; - NMSettingConnection *s_con; - const char *connection_type; - char *request_id; - GError *error; - - request_id = g_strdup_printf ("%s/%s", connection_path, setting_name); - if (g_hash_table_lookup (priv->requests, request_id) != NULL) { - /* We already have a request pending for this (connection, setting) */ - error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_FAILED, - "Request for %s secrets already pending", request_id); - nope: - callback (agent, connection, NULL, error, callback_data); - g_error_free (error); - g_free (request_id); - return; - } - - s_con = nm_connection_get_setting_connection (connection); - connection_type = nm_setting_connection_get_connection_type (s_con); - - if (!strcmp (connection_type, NM_SETTING_VPN_SETTING_NAME)) { - /* We don't support VPN secrets yet */ - error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_NO_SECRETS, - "VPN secrets not supported"); - goto nope; - } - - if (!(flags & NM_SECRET_AGENT_GET_SECRETS_FLAG_ALLOW_INTERACTION)) { - /* We don't do stored passwords */ - error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_NO_SECRETS, - "Stored passwords not supported"); - goto nope; - } - - request = g_slice_new (NmtSecretAgentRequest); - request->self = g_object_ref (self); - request->connection = g_object_ref (connection); - request->hints = g_strdupv ((gchar **)hints); - request->callback = callback; - request->callback_data = callback_data; - request->request_id = request_id; - g_hash_table_replace (priv->requests, request->request_id, request); - - request_secrets_from_ui (request); -} - -/** - * nmt_secret_agent_response: - * @self: the #NmtSecretAgent - * @request_id: the request ID being responded to - * @secrets: (allow-none): the array of secrets, or %NULL - * - * Response to a #NmtSecretAgent::get-secrets signal. - * - * If the user provided secrets, the caller should set the - * corresponding value fields in the - * #NmtSecretAgentSecrets (freeing any initial values they had), and - * pass the array to nmt_secret_agent_response(). If the user - * cancelled the request, @secrets should be NULL. - */ -void -nmt_secret_agent_response (NmtSecretAgent *self, - const char *request_id, - GPtrArray *secrets) -{ - NmtSecretAgentPrivate *priv; - NmtSecretAgentRequest *request; - GVariant *dict = NULL; - GError *error = NULL; - int i; - - g_return_if_fail (NMT_IS_SECRET_AGENT (self)); - - priv = NMT_SECRET_AGENT_GET_PRIVATE (self); - request = g_hash_table_lookup (priv->requests, request_id); - g_return_if_fail (request != NULL); - - if (secrets) { - GVariantBuilder conn_builder, *setting_builder; - GHashTable *settings; - GHashTableIter iter; - const char *name; - - settings = g_hash_table_new (g_str_hash, g_str_equal); - for (i = 0; i < secrets->len; i++) { - NmtSecretAgentSecretReal *secret = secrets->pdata[i]; - - setting_builder = g_hash_table_lookup (settings, nm_setting_get_name (secret->setting)); - if (!setting_builder) { - setting_builder = g_variant_builder_new (NM_VARIANT_TYPE_SETTING); - g_hash_table_insert (settings, (char *) nm_setting_get_name (secret->setting), - setting_builder); - } - - g_variant_builder_add (setting_builder, "{sv}", - secret->property, - g_variant_new_string (secret->base.value)); - } - - g_variant_builder_init (&conn_builder, NM_VARIANT_TYPE_CONNECTION); - g_hash_table_iter_init (&iter, settings); - while (g_hash_table_iter_next (&iter, (gpointer *) &name, (gpointer *) &setting_builder)) - g_variant_builder_add (&conn_builder, "{sa{sv}}", name, setting_builder); - dict = g_variant_builder_end (&conn_builder); - g_hash_table_destroy (settings); - } else { - error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_USER_CANCELED, - "User cancelled"); - } - - request->callback (NM_SECRET_AGENT (self), request->connection, dict, error, request->callback_data); - - g_clear_error (&error); - g_hash_table_remove (priv->requests, request_id); -} - -static void -nmt_secret_agent_cancel_get_secrets (NMSecretAgent *agent, - const gchar *connection_path, - const gchar *setting_name) -{ - /* We don't support cancellation. Sorry! */ -} - -static void -nmt_secret_agent_save_secrets (NMSecretAgent *agent, - NMConnection *connection, - const gchar *connection_path, - NMSecretAgentSaveSecretsFunc callback, - gpointer callback_data) -{ - /* We don't support secret storage */ - callback (agent, connection, NULL, callback_data);} - -static void -nmt_secret_agent_delete_secrets (NMSecretAgent *agent, - NMConnection *connection, - const gchar *connection_path, - NMSecretAgentDeleteSecretsFunc callback, - gpointer callback_data) -{ - /* We don't support secret storage, so there's nothing to delete. */ - callback (agent, connection, NULL, callback_data); -} - -void -nmt_secret_agent_class_init (NmtSecretAgentClass *klass) -{ - GObjectClass *gobject_class = G_OBJECT_CLASS (klass); - NMSecretAgentClass *agent_class = NM_SECRET_AGENT_CLASS (klass); - - g_type_class_add_private (klass, sizeof (NmtSecretAgentPrivate)); - - gobject_class->finalize = nmt_secret_agent_finalize; - - agent_class->get_secrets = nmt_secret_agent_get_secrets; - agent_class->cancel_get_secrets = nmt_secret_agent_cancel_get_secrets; - agent_class->save_secrets = nmt_secret_agent_save_secrets; - agent_class->delete_secrets = nmt_secret_agent_delete_secrets; - - /** - * NmtSecretAgent::request-secrets: - * @agent: the #NmtSecretAgent - * @request_id: request ID, to eventually pass to - * nmt_secret_agent_response(). - * @title: a title for the password dialog - * @prompt: a prompt message for the password dialog - * @secrets: (element-type #NmtSecretAgentSecret): array of secrets - * being requested. - * - * Emitted when the agent requires secrets from the user. - * - * The application should create a password dialog (eg, - * #NmtPasswordDialog) with the given title and prompt, and an - * entry for each element of @secrets. If any of the secrets - * already have a value filled in, the - * corresponding entry should be initialized to that value. - * - * When the dialog is complete, the app must call - * nmt_secret_agent_response() with the results. - */ - signals[REQUEST_SECRETS] = g_signal_new ("request-secrets", - G_TYPE_FROM_CLASS (klass), - 0, 0, NULL, NULL, NULL, - G_TYPE_NONE, - 4, - G_TYPE_STRING, /* request_id */ - G_TYPE_STRING, /* title */ - G_TYPE_STRING, /* prompt */ - G_TYPE_PTR_ARRAY); -} - -/** - * nmt_secret_agent_new: - * - * Creates a new #NmtSecretAgent. - * - * Returns: a new #NmtSecretAgent - */ -NMSecretAgent * -nmt_secret_agent_new (void) -{ - return g_initable_new (NMT_TYPE_SECRET_AGENT, NULL, NULL, - NM_SECRET_AGENT_IDENTIFIER, "nmtui", - NULL); -} diff --git a/clients/tui/nmt-secret-agent.h b/clients/tui/nmt-secret-agent.h deleted file mode 100644 index e7f6ef846b..0000000000 --- a/clients/tui/nmt-secret-agent.h +++ /dev/null @@ -1,57 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * Copyright 2013 Red Hat, Inc. - */ - -#ifndef NMT_SECRET_AGENT_H -#define NMT_SECRET_AGENT_H - -#include - -G_BEGIN_DECLS - -#define NMT_TYPE_SECRET_AGENT (nmt_secret_agent_get_type ()) -#define NMT_SECRET_AGENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NMT_TYPE_SECRET_AGENT, NmtSecretAgent)) -#define NMT_SECRET_AGENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NMT_TYPE_SECRET_AGENT, NmtSecretAgentClass)) -#define NMT_IS_SECRET_AGENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NMT_TYPE_SECRET_AGENT)) -#define NMT_IS_SECRET_AGENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NMT_TYPE_SECRET_AGENT)) -#define NMT_SECRET_AGENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NMT_TYPE_SECRET_AGENT, NmtSecretAgentClass)) - -typedef struct { - NMSecretAgent parent; - -} NmtSecretAgent; - -typedef struct { - NMSecretAgentClass parent; - -} NmtSecretAgentClass; - -typedef struct { - char *name, *value; - gboolean password; -} NmtSecretAgentSecret; - -GType nmt_secret_agent_get_type (void); - -NMSecretAgent *nmt_secret_agent_new (void); -void nmt_secret_agent_response (NmtSecretAgent *self, - const char *request_id, - GPtrArray *secrets); - -G_END_DECLS - -#endif /* NMT_SECRET_AGENT_H */ diff --git a/clients/tui/nmtui-connect.c b/clients/tui/nmtui-connect.c index be03796986..e2ffeb6495 100644 --- a/clients/tui/nmtui-connect.c +++ b/clients/tui/nmtui-connect.c @@ -36,16 +36,16 @@ #include "nmtui-connect.h" #include "nmt-connect-connection-list.h" #include "nmt-password-dialog.h" -#include "nmt-secret-agent.h" +#include "nm-secret-agent-simple.h" #include "nmt-utils.h" static void -secrets_requested (NmtSecretAgent *agent, - const char *request_id, - const char *title, - const char *msg, - GPtrArray *secrets, - gpointer user_data) +secrets_requested (NMSecretAgentSimple *agent, + const char *request_id, + const char *title, + const char *msg, + GPtrArray *secrets, + gpointer user_data) { NmtNewtForm *form; @@ -53,9 +53,9 @@ secrets_requested (NmtSecretAgent *agent, nmt_newt_form_run_sync (form); if (nmt_password_dialog_succeeded (NMT_PASSWORD_DIALOG (form))) - nmt_secret_agent_response (agent, request_id, secrets); + nm_secret_agent_simple_response (agent, request_id, secrets); else - nmt_secret_agent_response (agent, request_id, NULL); + nm_secret_agent_simple_response (agent, request_id, NULL); g_object_unref (form); } @@ -145,7 +145,7 @@ activate_connection (NMConnection *connection, label = nmt_newt_label_new (_("Connecting...")); nmt_newt_form_set_content (form, label); - agent = nmt_secret_agent_new (); + agent = nm_secret_agent_simple_new ("nmtui"); g_signal_connect (agent, "request-secrets", G_CALLBACK (secrets_requested), NULL); specific_object_path = specific_object ? nm_object_get_path (specific_object) : NULL; diff --git a/po/POTFILES.in b/po/POTFILES.in index 0acabd9173..869cd6e7c3 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -8,6 +8,7 @@ clients/cli/general.c clients/cli/nmcli.c clients/cli/settings.c clients/cli/utils.c +clients/common/nm-secret-agent-simple.c clients/nm-online.c clients/tui/newt/nmt-newt-utils.c clients/tui/nm-editor-utils.c @@ -34,7 +35,6 @@ clients/tui/nmt-password-dialog.c clients/tui/nmt-password-fields.c clients/tui/nmt-route-editor.c clients/tui/nmt-route-table.c -clients/tui/nmt-secret-agent.c clients/tui/nmt-slave-list.c clients/tui/nmt-widget-list.c clients/tui/nmtui-connect.c -- cgit v1.2.1 From b41cb60b450fcf27f068a35496f577a7f935033a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Mon, 3 Nov 2014 09:53:50 +0100 Subject: clients: add real property name to NNSecretAgentSimpleSecret It is necessary to identify secrets uniquely in nmcli. --- clients/common/nm-secret-agent-simple.c | 2 ++ clients/common/nm-secret-agent-simple.h | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/clients/common/nm-secret-agent-simple.c b/clients/common/nm-secret-agent-simple.c index d85c0a5cfe..cb1f086016 100644 --- a/clients/common/nm-secret-agent-simple.c +++ b/clients/common/nm-secret-agent-simple.c @@ -150,6 +150,7 @@ nm_secret_agent_simple_secret_free (NMSecretAgentSimpleSecret *secret) NMSecretAgentSimpleSecretReal *real = (NMSecretAgentSimpleSecretReal *)secret; g_free (secret->name); + g_free (secret->prop_name); g_free (secret->value); g_free (real->property); g_clear_object (&real->setting); @@ -167,6 +168,7 @@ nm_secret_agent_simple_secret_new (const char *name, real = g_slice_new0 (NMSecretAgentSimpleSecretReal); real->base.name = g_strdup (name); + real->base.prop_name = g_strdup_printf ("%s.%s", nm_setting_get_name (setting), property); real->base.password = password; if (setting) { diff --git a/clients/common/nm-secret-agent-simple.h b/clients/common/nm-secret-agent-simple.h index afac59c49f..7e11d2a5c0 100644 --- a/clients/common/nm-secret-agent-simple.h +++ b/clients/common/nm-secret-agent-simple.h @@ -41,7 +41,7 @@ typedef struct { } NMSecretAgentSimpleClass; typedef struct { - char *name, *value; + char *name, *prop_name, *value; gboolean password; } NMSecretAgentSimpleSecret; -- cgit v1.2.1 From de7f85bdec86c8cd8e08a57f4217bc0ccaf96f6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Mon, 6 Oct 2014 15:25:53 +0200 Subject: cli: use secret agent for getting passwords from user --ask option has to be used, so that nmcli can be interactive. --- clients/cli/Makefile.am | 7 +++- clients/cli/connections.c | 84 +++++++++++++++++++++++++++++++++++++++-------- clients/cli/nmcli.c | 7 ++++ clients/cli/nmcli.h | 1 + 4 files changed, 85 insertions(+), 14 deletions(-) diff --git a/clients/cli/Makefile.am b/clients/cli/Makefile.am index 94a249749e..4f4fcf4c6d 100644 --- a/clients/cli/Makefile.am +++ b/clients/cli/Makefile.am @@ -9,6 +9,7 @@ AM_CPPFLAGS = \ -I${top_builddir}/libnm-core \ -I${top_srcdir}/libnm \ -I${top_builddir}/libnm \ + -I${top_srcdir}/clients/common \ $(GLIB_CFLAGS) \ -DG_LOG_DOMAIN=\""nmcli"\" \ -DNM_VERSION_MAX_ALLOWED=NM_VERSION_NEXT_STABLE \ @@ -28,7 +29,11 @@ nmcli_SOURCES = \ nmcli.c \ nmcli.h \ utils.c \ - utils.h + utils.h \ + \ + $(srcdir)/../common/nm-secret-agent-simple.c \ + $(srcdir)/../common/nm-secret-agent-simple.h \ + $(NULL) nmcli_LDADD = \ $(GLIB_LIBS) \ diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 4d48e2fb60..1e95b023ff 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -35,6 +35,7 @@ #include "common.h" #include "settings.h" #include "connections.h" +#include "nm-secret-agent-simple.h" /* define some prompts for connection editor */ #define EDITOR_PROMPT_SETTING _("Setting name? ") @@ -507,6 +508,20 @@ quit (void) g_main_loop_quit (loop); /* quit main loop */ } +/* for pre-filling a string to readline prompt */ +static char *pre_input_deftext; +static int +set_deftext (void) +{ + if (pre_input_deftext && rl_startup_hook) { + rl_insert_text (pre_input_deftext); + g_free (pre_input_deftext); + pre_input_deftext = NULL; + rl_startup_hook = NULL; + } + return 0; +} + static const char * construct_header_name (const char *base, const char *spec) { @@ -1926,6 +1941,57 @@ activate_connection_cb (GObject *client, GAsyncResult *result, gpointer user_dat g_free (info); } +static gboolean +get_secrets_from_user (const char *request_id, + const char *title, + const char *msg, + GPtrArray *secrets) +{ + int i; + char *pwd; + + g_print ("%s\n", msg); + for (i = 0; i < secrets->len; i++) { + NMSecretAgentSimpleSecret *secret = secrets->pdata[i]; + if (secret->value) { + /* Prefill the password if we have it. */ + rl_startup_hook = set_deftext; + pre_input_deftext = g_strdup (secret->value); + } + pwd = nmc_readline ("%s (%s): ", secret->name, secret->prop_name); + g_free (secret->value); + secret->value = pwd ? pwd : g_strdup (""); + } + return TRUE; +} + +static void +secrets_requested (NMSecretAgentSimple *agent, + const char *request_id, + const char *title, + const char *msg, + GPtrArray *secrets, + gpointer user_data) +{ + NmCli *nmc = (NmCli *) user_data; + gboolean success = FALSE; + + if (nmc->print_output == NMC_PRINT_PRETTY) + nmc_terminal_erase_line (); + + if (nmc->ask) { + success = get_secrets_from_user (request_id, title, msg, secrets); + } else { + g_print ("%s\n", msg); + g_print ("%s\n", _("Warning: nmcli does not ask for password without '--ask' argument.")); + } + + if (success) + nm_secret_agent_simple_response (agent, request_id, secrets); + else + nm_secret_agent_simple_response (agent, request_id, NULL); +} + static gboolean nmc_activate_connection (NmCli *nmc, NMConnection *connection, @@ -1967,6 +2033,11 @@ nmc_activate_connection (NmCli *nmc, return FALSE; } + /* Create secret agent */ + nmc->secret_agent = nm_secret_agent_simple_new ("nmcli-connect"); + if (nmc->secret_agent) + g_signal_connect (nmc->secret_agent, "request-secrets", G_CALLBACK (secrets_requested), nmc); + info = g_malloc0 (sizeof (ActivateConnectionInfo)); info->nmc = nmc; info->device = device; @@ -5426,19 +5497,6 @@ uuid_display_hook (char **array, int len, int max_len) rl_forced_update_display (); } -static char *pre_input_deftext; -static int -set_deftext (void) -{ - if (pre_input_deftext && rl_startup_hook) { - rl_insert_text (pre_input_deftext); - g_free (pre_input_deftext); - pre_input_deftext = NULL; - rl_startup_hook = NULL; - } - return 0; -} - static char * gen_nmcli_cmds_menu (const char *text, int state) { diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index b1a810ce7a..74029438f1 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -499,6 +499,7 @@ nmc_init (NmCli *nmc) nmc->timeout = -1; nmc->connections = NULL; + nmc->secret_agent = NULL; nmc->should_wait = FALSE; nmc->nowait_flag = TRUE; @@ -525,6 +526,12 @@ nmc_cleanup (NmCli *nmc) g_string_free (nmc->return_text, TRUE); + if (nmc->secret_agent) { + /* Destroy secret agent if we have one. */ + nm_secret_agent_unregister (nmc->secret_agent, NULL, NULL); + g_object_unref (nmc->secret_agent); + } + g_free (nmc->required_fields); nmc_empty_output_fields (nmc); g_ptr_array_unref (nmc->output_data); diff --git a/clients/cli/nmcli.h b/clients/cli/nmcli.h index 0a87bd9147..6fe39392ff 100644 --- a/clients/cli/nmcli.h +++ b/clients/cli/nmcli.h @@ -111,6 +111,7 @@ typedef struct _NmCli { int timeout; /* Operation timeout */ const GPtrArray *connections; /* List of connections */ + NMSecretAgent *secret_agent; /* Secret agent */ gboolean should_wait; /* Indication that nmcli should not end yet */ gboolean nowait_flag; /* '--nowait' option; used for passing to callbacks */ -- cgit v1.2.1 From 3c9b8671fa2f338dccbb92084ead9f1707f29a40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Tue, 14 Oct 2014 15:53:34 +0200 Subject: cli: add 'passwd-file' option for 'nmcli connection up' to provide passwords It is useful for running nmcli without --ask option, i.e. non-interactively. Example contents of the file: wifi.psk: s e c r e t 12345 802-1x.password:kili manjaro 802-1x.pin:987654321 --- clients/cli/connections.c | 179 ++++++++++++++++++++++++++++++++++++------- clients/cli/nmcli-completion | 7 +- clients/cli/nmcli.c | 4 + clients/cli/nmcli.h | 2 + man/nmcli.1.in | 21 ++++- 5 files changed, 182 insertions(+), 31 deletions(-) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 1e95b023ff..717945b33d 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -251,9 +251,9 @@ usage (void) "COMMAND := { show | up | down | add | modify | edit | delete | reload | load }\n\n" " show [--active] [[--show-secrets] [id | uuid | path | apath] ] ...\n\n" #if WITH_WIMAX - " up [[id | uuid | path] ] [ifname ] [ap ] [nsp ]\n\n" + " up [[id | uuid | path] ] [ifname ] [ap ] [nsp ] [passwd-file ]\n\n" #else - " up [[id | uuid | path] ] [ifname ] [ap ]\n\n" + " up [[id | uuid | path] ] [ifname ] [ap ] [passwd-file ]\n\n" #endif " down [id | uuid | path | apath] \n\n" " add COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS IP_OPTIONS\n\n" @@ -291,19 +291,20 @@ usage_connection_up (void) { g_printerr (_("Usage: nmcli connection up { ARGUMENTS | help }\n" "\n" - "ARGUMENTS := [id | uuid | path] [ifname ] [ap ] [nsp ]\n" + "ARGUMENTS := [id | uuid | path] [ifname ] [ap ] [nsp ] [passwd-file ]\n" "\n" "Activate a connection on a device. The profile to activate is identified by its\n" "name, UUID or D-Bus path.\n" "\n" - "ARGUMENTS := ifname [ap ] [nsp ]\n" + "ARGUMENTS := ifname [ap ] [nsp ] [passwd-file ]\n" "\n" "Activate a device with a connection. The connection profile is selected\n" "automatically by NetworkManager.\n" "\n" - "ifname - specifies the device to active the connection on\n" - "ap - specifies AP to connect to (only valid for Wi-Fi)\n" - "nsp - specifies NSP to connect to (only valid for WiMAX)\n\n")); + "ifname - specifies the device to active the connection on\n" + "ap - specifies AP to connect to (only valid for Wi-Fi)\n" + "nsp - specifies NSP to connect to (only valid for WiMAX)\n" + "passwd-file - file with password(s) required to activate the connection\n\n")); } static void @@ -1941,26 +1942,129 @@ activate_connection_cb (GObject *client, GAsyncResult *result, gpointer user_dat g_free (info); } +/** + * parse_passwords: + * @passwd_file: file with passwords to parse + * @error: location to store error, or %NULL + * + * Parse passwords given in @passwd_file and insert them into a hash table. + * Example of @passwd_file contents: + * wifi.psk:tajne heslo + * 802-1x.password:krakonos + * 802-11-wireless-security:leap-password:my leap password + * + * Returns: hash table with parsed passwords, or %NULL on an error + */ +static GHashTable * +parse_passwords (const char *passwd_file, GError **error) +{ + GHashTable *pwds_hash; + char *contents = NULL; + gsize len = 0; + GError *local_err = NULL; + char **lines, **iter; + char *pwd_spec, *pwd, *prop; + const char *setting; + + pwds_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + + if (!passwd_file) + return pwds_hash; + + /* Read the passwords file */ + if (!g_file_get_contents (passwd_file, &contents, &len, &local_err)) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("failed to read passwd-file '%s': %s"), + passwd_file, local_err->message); + g_error_free (local_err); + g_hash_table_destroy (pwds_hash); + return NULL; + } + + lines = nmc_strsplit_set (contents, "\r\n", -1); + for (iter = lines; *iter; iter++) { + pwd = strchr (*iter, ':'); + if (!pwd) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("missing colon in 'password' entry '%s'"), *iter); + goto failure; + } + *(pwd++) = '\0'; + + prop = strchr (*iter, '.'); + if (!prop) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("missing dot in 'password' entry '%s'"), *iter); + goto failure; + } + *(prop++) = '\0'; + + setting = *iter; + while (g_ascii_isspace (*setting)) + setting++; + /* Accept wifi-sec or wifi instead of cumbersome '802-11-wireless-security' */ + if (!strcmp (setting, "wifi-sec") || !strcmp (setting, "wifi")) + setting = NM_SETTING_WIRELESS_SECURITY_SETTING_NAME; + if (nm_setting_lookup_type (setting) == G_TYPE_INVALID) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("invalid setting name in 'password' entry '%s'"), setting); + goto failure; + } + + pwd_spec = g_strdup_printf ("%s.%s", setting, prop); + g_hash_table_insert (pwds_hash, pwd_spec, g_strdup (pwd)); + } + g_strfreev (lines); + g_free (contents); + return pwds_hash; + +failure: + g_strfreev (lines); + g_free (contents); + g_hash_table_destroy (pwds_hash); + return NULL; +} + static gboolean get_secrets_from_user (const char *request_id, const char *title, const char *msg, + gboolean ask, + GHashTable *pwds_hash, GPtrArray *secrets) { int i; - char *pwd; - g_print ("%s\n", msg); for (i = 0; i < secrets->len; i++) { NMSecretAgentSimpleSecret *secret = secrets->pdata[i]; - if (secret->value) { - /* Prefill the password if we have it. */ - rl_startup_hook = set_deftext; - pre_input_deftext = g_strdup (secret->value); + char *pwd = NULL; + + /* First try to find the password in provided passwords file, + * then ask user. */ + if (pwds_hash && (pwd = g_hash_table_lookup (pwds_hash, secret->prop_name))) { + pwd = g_strdup (pwd); + } else { + g_print ("%s\n", msg); + if (ask) { + if (secret->value) { + /* Prefill the password if we have it. */ + rl_startup_hook = set_deftext; + pre_input_deftext = g_strdup (secret->value); + } + pwd = nmc_readline ("%s (%s): ", secret->name, secret->prop_name); + if (!pwd) + pwd = g_strdup (""); + } else { + g_printerr (_("Warning: password for '%s' not given in 'passwd-file' " + "and nmcli cannot ask without '--ask' option.\n"), + secret->prop_name); + } } - pwd = nmc_readline ("%s (%s): ", secret->name, secret->prop_name); + /* No password provided, cancel the secrets. */ + if (!pwd) + return FALSE; g_free (secret->value); - secret->value = pwd ? pwd : g_strdup (""); + secret->value = pwd; } return TRUE; } @@ -1979,17 +2083,18 @@ secrets_requested (NMSecretAgentSimple *agent, if (nmc->print_output == NMC_PRINT_PRETTY) nmc_terminal_erase_line (); - if (nmc->ask) { - success = get_secrets_from_user (request_id, title, msg, secrets); - } else { - g_print ("%s\n", msg); - g_print ("%s\n", _("Warning: nmcli does not ask for password without '--ask' argument.")); - } - + success = get_secrets_from_user (request_id, title, msg, nmc->in_editor || nmc->ask, + nmc->pwds_hash, secrets); if (success) nm_secret_agent_simple_response (agent, request_id, secrets); - else - nm_secret_agent_simple_response (agent, request_id, NULL); + else { + /* Unregister our secret agent on failure, so that another agent + * may be tried */ + if (nmc->secret_agent) { + nm_secret_agent_unregister (nmc->secret_agent, NULL, NULL); + g_clear_object (&nmc->secret_agent); + } + } } static gboolean @@ -1998,10 +2103,12 @@ nmc_activate_connection (NmCli *nmc, const char *ifname, const char *ap, const char *nsp, + const char *pwds, GAsyncReadyCallback callback, GError **error) { ActivateConnectionInfo *info; + GHashTable *pwds_hash; NMDevice *device = NULL; const char *spec_object = NULL; gboolean device_found; @@ -2033,6 +2140,16 @@ nmc_activate_connection (NmCli *nmc, return FALSE; } + /* Parse passwords given in passwords file */ + pwds_hash = parse_passwords (pwds, &local); + if (local) { + g_propagate_error (error, local); + return FALSE; + } + if (nmc->pwds_hash) + g_hash_table_destroy (nmc->pwds_hash); + nmc->pwds_hash = pwds_hash; + /* Create secret agent */ nmc->secret_agent = nm_secret_agent_simple_new ("nmcli-connect"); if (nmc->secret_agent) @@ -2059,6 +2176,7 @@ do_connection_up (NmCli *nmc, int argc, char **argv) const char *ifname = NULL; const char *ap = NULL; const char *nsp = NULL; + const char *pwds = NULL; GError *error = NULL; const char *selector = NULL; const char *name = NULL; @@ -2126,6 +2244,15 @@ do_connection_up (NmCli *nmc, int argc, char **argv) nsp = *argv; } #endif + else if (strcmp (*argv, "passwd-file") == 0) { + if (next_arg (&argc, &argv) != 0) { + g_string_printf (nmc->return_text, _("Error: %s argument is missing."), *(argv-1)); + nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; + goto error; + } + + pwds = *argv; + } else { g_printerr (_("Unknown parameter: %s\n"), *argv); } @@ -2141,7 +2268,7 @@ do_connection_up (NmCli *nmc, int argc, char **argv) nmc->nowait_flag = (nmc->timeout == 0); nmc->should_wait = TRUE; - if (!nmc_activate_connection (nmc, connection, ifname, ap, nsp, activate_connection_cb, &error)) { + if (!nmc_activate_connection (nmc, connection, ifname, ap, nsp, pwds, activate_connection_cb, &error)) { g_string_printf (nmc->return_text, _("Error: %s."), error ? error->message : _("unknown error")); nmc->return_value = error ? error->code : NMC_RESULT_ERROR_CON_ACTIVATION; @@ -7712,7 +7839,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t nmc->nowait_flag = FALSE; nmc->should_wait = TRUE; nmc->print_output = NMC_PRINT_PRETTY; - if (!nmc_activate_connection (nmc, NM_CONNECTION (rem_con), ifname, ap_nsp, ap_nsp, + if (!nmc_activate_connection (nmc, NM_CONNECTION (rem_con), ifname, ap_nsp, ap_nsp, NULL, activate_connection_editor_cb, &tmp_err)) { g_print (_("Error: Cannot activate connection: %s.\n"), tmp_err->message); g_clear_error (&tmp_err); diff --git a/clients/cli/nmcli-completion b/clients/cli/nmcli-completion index a9dfc41774..1e087f7419 100644 --- a/clients/cli/nmcli-completion +++ b/clients/cli/nmcli-completion @@ -502,7 +502,8 @@ _nmcli_compl_ARGS() user| \ username| \ service| \ - password) + password| \ + passwd-file) if [[ "${#words[@]}" -eq 2 ]]; then return 0 fi @@ -845,9 +846,9 @@ _nmcli() _nmcli_compl_ARGS_CONNECTION && return 0 if [[ "$COMMAND_CONNECTION_TYPE" = "ifname" ]]; then - OPTIONS=(ap nsp) + OPTIONS=(ap nsp passwd-file) else - OPTIONS=(ifname ap nsp) + OPTIONS=(ifname ap nsp passwd-file) fi _nmcli_compl_ARGS fi diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index 74029438f1..8410020fee 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -499,7 +499,9 @@ nmc_init (NmCli *nmc) nmc->timeout = -1; nmc->connections = NULL; + nmc->secret_agent = NULL; + nmc->pwds_hash = NULL; nmc->should_wait = FALSE; nmc->nowait_flag = TRUE; @@ -531,6 +533,8 @@ nmc_cleanup (NmCli *nmc) nm_secret_agent_unregister (nmc->secret_agent, NULL, NULL); g_object_unref (nmc->secret_agent); } + if (nmc->pwds_hash) + g_hash_table_destroy (nmc->pwds_hash); g_free (nmc->required_fields); nmc_empty_output_fields (nmc); diff --git a/clients/cli/nmcli.h b/clients/cli/nmcli.h index 6fe39392ff..a18de18546 100644 --- a/clients/cli/nmcli.h +++ b/clients/cli/nmcli.h @@ -111,7 +111,9 @@ typedef struct _NmCli { int timeout; /* Operation timeout */ const GPtrArray *connections; /* List of connections */ + NMSecretAgent *secret_agent; /* Secret agent */ + GHashTable *pwds_hash; /* Hash table with passwords in passwd-file */ gboolean should_wait; /* Indication that nmcli should not end yet */ gboolean nowait_flag; /* '--nowait' option; used for passing to callbacks */ diff --git a/man/nmcli.1.in b/man/nmcli.1.in index 109317f768..e2ac9d5aa7 100644 --- a/man/nmcli.1.in +++ b/man/nmcli.1.in @@ -327,10 +327,10 @@ When no command is given to the \fIconnection\fP object, the default action is 'nmcli connection show'. .RE .TP -.B up [ id | uuid | path ] [ifname ] [ap ] [nsp ] +.B up [ id | uuid | path ] [ifname ] [ap ] [nsp ] [passwd ] .RE .RS -.B up ifname [ap ] [nsp ] +.B up ifname [ap ] [nsp ] [passwd ] .RS .br Activate a connection. The connection is identified by its name, UUID or D-Bus @@ -355,6 +355,23 @@ Available options are: \(en BSSID of the AP which the command should connect to (for Wi\(hyFi connections) .IP \fInsp\fP 13 \(en NSP (Network Service Provider) which the command should connect to (for WiMAX connections) +.IP \fIpasswd-file\fP 13 +\(en some networks may require credentials during activation. You can give these +credentials using this option. +Each line of the file should contain one password in the form of +.br +\fBsetting_name.property_name:the password\fP +.br +For example, for WPA Wi-Fi with PSK, the line would be +.br +\fI802-11-wireless-security.psk:secret12345\fP +.br +For 802.1X password, the line would be +.br +\fI802-1x.password:my 1X password\fP +.br +nmcli also accepts "wifi-sec" and "wifi" strings instead of "802-11-wireless-security". +When a required password is not given, nmcli will ask for it when run with --ask. .RE .RE .TP -- cgit v1.2.1 From 252c8bf4edd5a0a85da74ffd4cdc87c41d082875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Fri, 7 Nov 2014 09:55:29 +0100 Subject: man: document/clarify --ask option in relation to password prompting --- man/nmcli.1.in | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/man/nmcli.1.in b/man/nmcli.1.in index e2ac9d5aa7..2d142f4eaa 100644 --- a/man/nmcli.1.in +++ b/man/nmcli.1.in @@ -131,6 +131,8 @@ incompatible versions may produce incorrect results. .B \-a, \-\-ask When using this option \fInmcli\fP will stop and ask for any missing required arguments, so do not use this option for non-interactive purposes like scripts. +This option controls, for example, whether you will be prompted for a password +if it is required for connecting to a network. .TP .B \-w, \-\-wait This option sets a timeout period for which \fInmcli\fP will wait for \fINetworkManager\fP @@ -371,7 +373,10 @@ For 802.1X password, the line would be \fI802-1x.password:my 1X password\fP .br nmcli also accepts "wifi-sec" and "wifi" strings instead of "802-11-wireless-security". -When a required password is not given, nmcli will ask for it when run with --ask. +When NetworkManager requires a password and it is not given, nmcli will ask for it +when run with --ask. If --ask was not passed, NetworkManager can ask another secret +agent that may be running (typically a GUI secret agent, such as nm-applet or +gnome-shell). .RE .RE .TP -- cgit v1.2.1 From ca5d6be99c622c37c9f7999fb47e2186cd837689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Wed, 29 Oct 2014 13:15:29 +0100 Subject: clients: add common code for polkit agent listener that can be used by nmcli and nmtui --- clients/common/nm-polkit-listener.c | 415 ++++++++++++++++++++++++++++++++++++ clients/common/nm-polkit-listener.h | 104 +++++++++ po/POTFILES.in | 1 + 3 files changed, 520 insertions(+) create mode 100644 clients/common/nm-polkit-listener.c create mode 100644 clients/common/nm-polkit-listener.h diff --git a/clients/common/nm-polkit-listener.c b/clients/common/nm-polkit-listener.c new file mode 100644 index 0000000000..82df1b2de2 --- /dev/null +++ b/clients/common/nm-polkit-listener.c @@ -0,0 +1,415 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Copyright 2014 Red Hat, Inc. + */ + +/** + * SECTION:nm-polkit-listener + * @short_description: A polkit agent listener + * + * #NMPolkitListener is the polkit agent listener used by nmcli and nmtui. + * http://www.freedesktop.org/software/polkit/docs/latest/index.html + * + * For an example polkit agent you can look at polkit source tree: + * http://cgit.freedesktop.org/polkit/tree/src/polkitagent/polkitagenttextlistener.c + * http://cgit.freedesktop.org/polkit/tree/src/programs/pkttyagent.c + * or LXDE polkit agent: + * http://git.lxde.org/gitweb/?p=debian/lxpolkit.git;a=blob;f=src/lxpolkit-listener.c + * https://github.com/lxde/lxqt-policykit/tree/master/src + */ + +#include "config.h" + +#include +#include +#include +#include +#include + +#include "nm-glib-compat.h" +#include "nm-polkit-listener.h" + +G_DEFINE_TYPE (NMPolkitListener, nm_polkit_listener, POLKIT_AGENT_TYPE_LISTENER) + +#define NM_POLKIT_LISTENER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_POLKIT_LISTENER, NMPolkitListenerPrivate)) + +typedef struct { + gpointer reg_handle; /* handle of polkit agent registration */ + + GSimpleAsyncResult *simple; + PolkitAgentSession *active_session; + gulong cancel_id; + GCancellable *cancellable; + + char *action_id; + char *message; + char *icon_name; + char *identity; + + /* callbacks */ + NMPolkitListenerOnRequestFunc on_request_callback; + NMPolkitListenerOnShowInfoFunc on_show_info_callback; + NMPolkitListenerOnShowErrorFunc on_show_error_callback; + NMPolkitListenerOnCompletedFunc on_completed_callback; + gpointer request_callback_data; +} NMPolkitListenerPrivate; + + +static void +on_request (PolkitAgentSession *session, + const char *request, + gboolean echo_on, + gpointer user_data) +{ + NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (user_data); + char *response = NULL; + + if (priv->on_request_callback) { + response = priv->on_request_callback (request, priv->action_id, + priv->message, priv->icon_name, + priv->identity, echo_on, + priv->request_callback_data); + } + + if (response) { + polkit_agent_session_response (session, response); + g_free (response); + } else { + //FIXME: polkit_agent_session_cancel() should emit "completed", but it doesn't work for me ??? + //polkit_agent_session_cancel (session); + polkit_agent_session_response (session, ""); + } +} + +static void +on_show_info (PolkitAgentSession *session, + const char *text, + gpointer user_data) +{ + NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (user_data); + + if (priv->on_show_info_callback) + priv->on_show_info_callback (text); +} + +static void +on_show_error (PolkitAgentSession *session, + const char *text, + gpointer user_data) +{ + NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (user_data); + + if (priv->on_show_error_callback) + priv->on_show_error_callback (text); +} + +static void +on_completed (PolkitAgentSession *session, + gboolean gained_authorization, + gpointer user_data) +{ + NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (user_data); + + if (priv->on_completed_callback) + priv->on_completed_callback (gained_authorization); + + g_simple_async_result_complete_in_idle (priv->simple); + + g_object_unref (priv->simple); + g_object_unref (priv->active_session); + if (priv->cancellable) { + g_cancellable_disconnect (priv->cancellable, priv->cancel_id); + g_object_unref (priv->cancellable); + } + + priv->simple = NULL; + priv->active_session = NULL; + priv->cancel_id = 0; + + g_clear_pointer (&priv->action_id, g_free); + g_clear_pointer (&priv->message, g_free); + g_clear_pointer (&priv->icon_name, g_free); + g_clear_pointer (&priv->identity, g_free); +} + +static void +on_cancelled (GCancellable *cancellable, gpointer user_data) +{ + NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (user_data); + + polkit_agent_session_cancel (priv->active_session); +} + +static gint +compare_users (gconstpointer a, gconstpointer b) +{ + char *user; + int ret; + + if (POLKIT_IS_UNIX_USER (a)) + user = g_strdup (polkit_unix_user_get_name (POLKIT_UNIX_USER (a))); + else + user = polkit_identity_to_string (POLKIT_IDENTITY (a)); + + ret = g_strcmp0 ((const char *) user, (const char *) b); + g_free (user); + return ret; +} + +static PolkitIdentity * +choose_identity (GList *identities) +{ + const char *user; + GList *elem; + + /* Choose identity. First try current user, then root, and else + * take the firts one */ + user = getenv("USER"); + elem = g_list_find_custom (identities, user, (GCompareFunc) compare_users); + if (!elem) { + elem = g_list_find_custom (identities, "root", (GCompareFunc) compare_users); + if (!elem) + elem = identities; + } + + return elem->data; +} + +static void +initiate_authentication (PolkitAgentListener *listener, + const char *action_id, + const char *message, + const char *icon_name, + PolkitDetails *details, + const char *cookie, + GList *identities, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (listener); + GSimpleAsyncResult *simple; + PolkitIdentity *identity; + + simple = g_simple_async_result_new (G_OBJECT (listener), + callback, + user_data, + initiate_authentication); + if (priv->active_session != NULL) { + g_simple_async_result_set_error (simple, + POLKIT_ERROR, + POLKIT_ERROR_FAILED, + _("An authentication session is already underway.")); + g_simple_async_result_complete_in_idle (simple); + g_object_unref (simple); + return; + } + + /* Choose identity */ + identity = choose_identity (identities); + + priv->active_session = polkit_agent_session_new (identity, cookie); + g_signal_connect (priv->active_session, + "completed", + G_CALLBACK (on_completed), + listener); + g_signal_connect (priv->active_session, + "request", + G_CALLBACK (on_request), + listener); + g_signal_connect (priv->active_session, + "show-info", + G_CALLBACK (on_show_info), + listener); + g_signal_connect (priv->active_session, + "show-error", + G_CALLBACK (on_show_error), + listener); + + priv->action_id = g_strdup (action_id); + priv->message = g_strdup (message); + priv->icon_name = g_strdup (icon_name); + if (POLKIT_IS_UNIX_USER (identity)) + priv->identity = g_strdup (polkit_unix_user_get_name (POLKIT_UNIX_USER (identity))); + else + priv->identity = polkit_identity_to_string (identity); + + priv->simple = simple; + priv->cancellable = g_object_ref (cancellable); + priv->cancel_id = g_cancellable_connect (cancellable, + G_CALLBACK (on_cancelled), + listener, + NULL); + + polkit_agent_session_initiate (priv->active_session); +} + +static gboolean +initiate_authentication_finish (PolkitAgentListener *listener, + GAsyncResult *result, + GError **error) +{ + return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error); +} + + +static void +nm_polkit_listener_init (NMPolkitListener *agent) +{ +} + +static void +nm_polkit_listener_finalize (GObject *object) +{ + NMPolkitListenerPrivate *priv = NM_POLKIT_LISTENER_GET_PRIVATE (object); + + if (priv->reg_handle) + polkit_agent_listener_unregister (priv->reg_handle); + + g_free (priv->action_id); + g_free (priv->message); + g_free (priv->icon_name); + g_free (priv->identity); + + G_OBJECT_CLASS (nm_polkit_listener_parent_class)->finalize (object); +} + +static void +nm_polkit_listener_class_init (NMPolkitListenerClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + PolkitAgentListenerClass *pkal_class = POLKIT_AGENT_LISTENER_CLASS (klass); + + g_type_class_add_private (klass, sizeof (NMPolkitListenerPrivate)); + + gobject_class->finalize = nm_polkit_listener_finalize; + + pkal_class->initiate_authentication = initiate_authentication; + pkal_class->initiate_authentication_finish = initiate_authentication_finish; +} + +/** + * nm_polkit_listener_new: + * @for_session: %TRUE for registering the polkit agent for the user session, + * %FALSE for registering it for the running process + * @error: location to store error, or %NULL + * + * Creates a new #NMPolkitListener and registers it as a polkit agent. + * + * Returns: a new #NMPolkitListener + */ +PolkitAgentListener * +nm_polkit_listener_new (gboolean for_session, GError **error) +{ + PolkitAgentListener *listener; + PolkitSubject* session; + NMPolkitListenerPrivate *priv; + + g_return_val_if_fail (error == NULL || *error == NULL, NULL); + + listener = g_object_new (NM_TYPE_POLKIT_LISTENER, NULL); + priv = NM_POLKIT_LISTENER_GET_PRIVATE (listener); + + if (for_session) + session = polkit_unix_session_new_for_process_sync (getpid (), NULL, NULL); + else + session = polkit_unix_process_new_for_owner (getpid (), 0, getuid ()); + + priv->reg_handle = polkit_agent_listener_register (listener, POLKIT_AGENT_REGISTER_FLAGS_NONE, + session, NULL, NULL, error); + if (!priv->reg_handle) { + g_object_unref (listener); + g_object_unref (session); + return NULL; + } + + return listener; +} + +/** + * nm_polkit_listener_set_request_callback: + * @self: a #NMPolkitListener object + * @request_callback: callback to install for polkit requests + * @request_callback_data: usaer data passed to request_callback when it is called + * + * Set a callback for "request" signal. The callback will be invoked when polkit + * requests an authorization. + */ +void +nm_polkit_listener_set_request_callback (NMPolkitListener *self, + NMPolkitListenerOnRequestFunc request_callback, + gpointer request_callback_data) +{ + NMPolkitListenerPrivate *priv; + + g_return_if_fail (NM_IS_POLKIT_LISTENER (self)); + + priv = NM_POLKIT_LISTENER_GET_PRIVATE (self); + + priv->on_request_callback = request_callback; + priv->request_callback_data = request_callback_data; +} + +/** + * nm_polkit_listener_set_show_info_callback: + * @self: a #NMPolkitListener object + * @show_info_callback: callback to install for polkit show info trigger + * + * Set a callback for "show-info" signal. The callback will be invoked when polkit + * has an info text to display. + */ +void +nm_polkit_listener_set_show_info_callback (NMPolkitListener *self, + NMPolkitListenerOnShowInfoFunc show_info_callback) +{ + g_return_if_fail (NM_IS_POLKIT_LISTENER (self)); + + NM_POLKIT_LISTENER_GET_PRIVATE (self)->on_show_info_callback = show_info_callback; +} + +/** + * nm_polkit_listener_set_show_error_callback: + * @self: a #NMPolkitListener object + * @show_error_callback: callback to install for polkit show error trigger + * + * Set a callback for "show-error" signal. The callback will be invoked when polkit + * has an error text to display. + */ +void +nm_polkit_listener_set_show_error_callback (NMPolkitListener *self, + NMPolkitListenerOnShowErrorFunc show_error_callback) +{ + g_return_if_fail (NM_IS_POLKIT_LISTENER (self)); + + NM_POLKIT_LISTENER_GET_PRIVATE (self)->on_show_error_callback = show_error_callback; +} + +/** + * nm_polkit_listener_set_completed_callback: + * @self: a #NMPolkitListener object + * @completed_callback: callback to install for polkit completing authorization + * + * Set a callback for "completed" signal. The callback will be invoked when polkit + * completed the request. + */ +void +nm_polkit_listener_set_completed_callback (NMPolkitListener *self, + NMPolkitListenerOnCompletedFunc completed_callback) +{ + g_return_if_fail (NM_IS_POLKIT_LISTENER (self)); + + NM_POLKIT_LISTENER_GET_PRIVATE (self)->on_completed_callback = completed_callback; +} diff --git a/clients/common/nm-polkit-listener.h b/clients/common/nm-polkit-listener.h new file mode 100644 index 0000000000..3cd7501924 --- /dev/null +++ b/clients/common/nm-polkit-listener.h @@ -0,0 +1,104 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Copyright 2014 Red Hat, Inc. + */ + +#ifndef __NM_POLKIT_LISTENER_H__ +#define __NM_POLKIT_LISTENER_H__ + +#include + +#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE +#include + +G_BEGIN_DECLS + +#define NM_TYPE_POLKIT_LISTENER (nm_polkit_listener_get_type ()) +#define NM_POLKIT_LISTENER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_POLKIT_LISTENER, NMPolkitListener)) +#define NM_POLKIT_LISTENER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_POLKIT_LISTENER, NMPolkitListenerClass)) +#define NM_IS_POLKIT_LISTENER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_POLKIT_LISTENER)) +#define NM_IS_POLKIT_LISTENER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_POLKIT_LISTENER)) +#define NM_POLKIT_LISTENER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_POLKIT_LISTENER, NMPolkitListenerClass)) + +/** + * NMPolkitListenerOnRequestFunc: + * @request: the request asked by polkit agent + * @action_id: the action_id of the polkit request + * @message: the message of the polkit request + * @icon_name: the icon name of the polkit request + * @user: user name + * @echo_on: whether the response to the request should be echoed to the screen + * @user_data: user data for the callback + * + * Called as a result of a request by polkit. The function should obtain response + * to the request from user, i.e. get the password required. + */ +typedef char * (*NMPolkitListenerOnRequestFunc) (const char *request, + const char *action_id, + const char *message, + const char *icon_name, + const char *user, + gboolean echo_on, + gpointer user_data); +/** + * NMPolkitListenerOnShowInfoFunc: + * @text: the info text from polkit + * + * Called as a result of show-info signal by polkit. + */ +typedef void (*NMPolkitListenerOnShowInfoFunc) (const char *text); +/** + * NMPolkitListenerOnShowErrorFunc: + * @text: the error text from polkit + * + * Called as a result of show-error signal by polkit. + */ +typedef void (*NMPolkitListenerOnShowErrorFunc) (const char *text); +/** + * NMPolkitListenerCompletedFunc: + * @gained_authorization: whether the autorization was successful + * + * Called as a result of completed signal by polkit. + */ +typedef void (*NMPolkitListenerOnCompletedFunc) (gboolean gained_authorization); + + +typedef struct { + PolkitAgentListener parent; + +} NMPolkitListener; + +typedef struct { + PolkitAgentListenerClass parent; + +} NMPolkitListenerClass; + +GType nm_polkit_listener_get_type (void); + +PolkitAgentListener* nm_polkit_listener_new (gboolean for_session, GError **error); +void nm_polkit_listener_set_request_callback (NMPolkitListener *self, + NMPolkitListenerOnRequestFunc request_callback, + gpointer request_callback_data); +void nm_polkit_listener_set_show_info_callback (NMPolkitListener *self, + NMPolkitListenerOnShowInfoFunc show_info_callback); +void nm_polkit_listener_set_show_error_callback (NMPolkitListener *self, + NMPolkitListenerOnShowErrorFunc show_error_callback); +void nm_polkit_listener_set_completed_callback (NMPolkitListener *self, + NMPolkitListenerOnCompletedFunc completed_callback); + +G_END_DECLS + +#endif /* __NM_POLKIT_LISTENER_H__ */ diff --git a/po/POTFILES.in b/po/POTFILES.in index 869cd6e7c3..bbe4bb1bde 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -8,6 +8,7 @@ clients/cli/general.c clients/cli/nmcli.c clients/cli/settings.c clients/cli/utils.c +clients/common/nm-polkit-listener.c clients/common/nm-secret-agent-simple.c clients/nm-online.c clients/tui/newt/nmt-newt-utils.c -- cgit v1.2.1 From c7aaee107e5858a5642f3ec82e5985fac0f4a115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Wed, 29 Oct 2014 13:18:40 +0100 Subject: configure: check whether polkit-agent-1 is available --- configure.ac | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/configure.ac b/configure.ac index f14fb8e0b5..61f8656871 100644 --- a/configure.ac +++ b/configure.ac @@ -498,6 +498,22 @@ else fi AC_SUBST(NM_CONFIG_DEFAULT_AUTH_POLKIT_TEXT) +PKG_CHECK_MODULES(POLKIT, [polkit-agent-1 >= 0.97], [have_pk_agent=yes],[have_pk_agent=no]) +AC_ARG_ENABLE(polkit-agent, AS_HELP_STRING([--enable-polkit-agent], [enable polkit agent for clients]), + [enable_polkit_agent=${enableval}], [enable_polkit_agent=${have_pk_agent}]) +if (test "${enable_polkit_agent}" = "yes"); then + if test x"$have_pk_agent" = x"no"; then + AC_MSG_ERROR(Polkit agent is required) + fi + + AC_SUBST(POLKIT_CFLAGS) + AC_SUBST(POLKIT_LIBS) + AC_DEFINE(WITH_POLKIT_AGENT, 1, [Define if you have polkit agent]) +else + AC_DEFINE(WITH_POLKIT_AGENT, 0, [Define if you have polkit agent]) +fi +AM_CONDITIONAL(WITH_POLKIT_AGENT, test "${enable_polkit_agent}" = "yes") + AC_ARG_ENABLE(modify-system, AS_HELP_STRING([--enable-modify-system], [Allow users to modify system connections])) if test "${enable_modify_system}" = "yes"; then @@ -1013,6 +1029,7 @@ if test "${enable_polkit}" = "yes"; then else echo " policykit: no" fi +echo " polkit agent: ${enable_polkit_agent}" echo " selinux: $have_selinux" echo -- cgit v1.2.1 From e517061203912381c954475a73ebbe428529cd7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Thu, 30 Oct 2014 11:25:55 +0100 Subject: cli: add a polkit agent support for nmcli Example: nmcli --ask general hostname computer007 --- clients/cli/Makefile.am | 8 +++ clients/cli/nmcli.c | 24 +++++++- clients/cli/nmcli.h | 10 ++++ clients/cli/polkit-agent.c | 146 +++++++++++++++++++++++++++++++++++++++++++++ clients/cli/polkit-agent.h | 28 +++++++++ po/POTFILES.in | 1 + 6 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 clients/cli/polkit-agent.c create mode 100644 clients/cli/polkit-agent.h diff --git a/clients/cli/Makefile.am b/clients/cli/Makefile.am index 4f4fcf4c6d..2060c4bfff 100644 --- a/clients/cli/Makefile.am +++ b/clients/cli/Makefile.am @@ -30,6 +30,8 @@ nmcli_SOURCES = \ nmcli.h \ utils.c \ utils.h \ + polkit-agent.c \ + polkit-agent.h \ \ $(srcdir)/../common/nm-secret-agent-simple.c \ $(srcdir)/../common/nm-secret-agent-simple.h \ @@ -40,6 +42,12 @@ nmcli_LDADD = \ $(READLINE_LIBS) \ $(top_builddir)/libnm/libnm.la +if WITH_POLKIT_AGENT +AM_CPPFLAGS += $(POLKIT_CFLAGS) +nmcli_SOURCES += $(srcdir)/../common/nm-polkit-listener.c $(srcdir)/../common/nm-polkit-listener.h +nmcli_LDADD += $(POLKIT_LIBS) +endif + if BUILD_SETTING_DOCS settings-docs.c: settings-docs.xsl $(top_builddir)/libnm-util/nm-setting-docs.xml $(AM_V_GEN) xsltproc --output $@ $^ diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index 8410020fee..76033417ee 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -27,6 +27,8 @@ #include #include #include +#include +#include #include #include #include @@ -34,6 +36,7 @@ #include #include +#include "polkit-agent.h" #include "nmcli.h" #include "utils.h" #include "common.h" @@ -61,6 +64,7 @@ typedef struct { /* --- Global variables --- */ GMainLoop *loop = NULL; static sigset_t signal_set; +struct termios termios_orig; /* Get an error quark for use with GError */ @@ -261,8 +265,18 @@ parse_command_line (NmCli *nmc, int argc, char **argv) argv++; } - if (argc > 1) + if (argc > 1) { + GError *error = NULL; + + /* Initialize polkit agent */ + if (!nmc_polkit_agent_init (&nm_cli, FALSE, &error)) { + g_printerr ("Polkit agent initialization failed: %s\n", error->message); + g_error_free (error); + } + + /* Now run the requested command */ return do_cmd (nmc, argv[1], argc-1, argv+1); + } usage (base); return nmc->return_value; @@ -332,6 +346,7 @@ signal_handling_thread (void *arg) { pthread_mutex_unlock (&sigint_mutex); } else { /* We can quit nmcli */ + tcsetattr (STDIN_FILENO, TCSADRAIN, &termios_orig); nmc_cleanup_readline (); g_print (_("\nError: nmcli terminated by signal %s (%d)\n"), strsignal (signo), signo); @@ -340,6 +355,7 @@ signal_handling_thread (void *arg) { break; case SIGQUIT: case SIGTERM: + tcsetattr (STDIN_FILENO, TCSADRAIN, &termios_orig); nmc_cleanup_readline (); if (!nmcli_sigquit_internal) g_print (_("\nError: nmcli terminated by signal %s (%d)\n"), @@ -502,6 +518,7 @@ nmc_init (NmCli *nmc) nmc->secret_agent = NULL; nmc->pwds_hash = NULL; + nmc->pk_listener = NULL; nmc->should_wait = FALSE; nmc->nowait_flag = TRUE; @@ -539,6 +556,8 @@ nmc_cleanup (NmCli *nmc) g_free (nmc->required_fields); nmc_empty_output_fields (nmc); g_ptr_array_unref (nmc->output_data); + + nmc_polkit_agent_fini (nmc); } static gboolean @@ -576,6 +595,9 @@ main (int argc, char *argv[]) #if !GLIB_CHECK_VERSION (2, 35, 0) g_type_init (); #endif + + /* Save terminal settings */ + tcgetattr (STDIN_FILENO, &termios_orig); /* readline init */ rl_event_hook = event_hook_for_readline; diff --git a/clients/cli/nmcli.h b/clients/cli/nmcli.h index a18de18546..5cd7526a8d 100644 --- a/clients/cli/nmcli.h +++ b/clients/cli/nmcli.h @@ -20,8 +20,17 @@ #ifndef NMC_NMCLI_H #define NMC_NMCLI_H +#include "config.h" + #include +#if WITH_POLKIT_AGENT +#include "nm-polkit-listener.h" +#else +/* polkit agent is not available; define fake NMPolkitListener */ +typedef gpointer NMPolkitListener; +#endif + /* nmcli exit codes */ typedef enum { /* Indicates successful execution */ @@ -114,6 +123,7 @@ typedef struct _NmCli { NMSecretAgent *secret_agent; /* Secret agent */ GHashTable *pwds_hash; /* Hash table with passwords in passwd-file */ + NMPolkitListener *pk_listener ; /* polkit agent listener */ gboolean should_wait; /* Indication that nmcli should not end yet */ gboolean nowait_flag; /* '--nowait' option; used for passing to callbacks */ diff --git a/clients/cli/polkit-agent.c b/clients/cli/polkit-agent.c new file mode 100644 index 0000000000..e8502884e4 --- /dev/null +++ b/clients/cli/polkit-agent.c @@ -0,0 +1,146 @@ +/* nmcli - command-line tool to control NetworkManager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright 2014 Red Hat, Inc. + */ + +#include "config.h" + +#if WITH_POLKIT_AGENT + +#include +#include +#include +#include +#include + +#include +#include + +#include "polkit-agent.h" +#include "nm-polkit-listener.h" +#include "common.h" + +static char * +polkit_request (const char *request, + const char *action_id, + const char *message, + const char *icon_name, + const char *user, + gboolean echo_on, + gpointer user_data) +{ + char *response, *tmp, *p; + struct termios termios_orig, termios_new; + + g_print ("%s\n", message); + g_print ("(action_id: %s)\n", action_id); + + if (!echo_on) { + tcgetattr (STDIN_FILENO, &termios_orig); + termios_new = termios_orig; + termios_new.c_lflag &= ~(ECHO); + tcsetattr (STDIN_FILENO, TCSADRAIN, &termios_new); + } + + /* Ask user for polkit authorization password */ + if (user) { + /* chop of ": " if present */ + tmp = g_strdup (request); + p = strrchr (tmp, ':'); + if (p && !strcmp (p, ": ")) + *p = '\0'; + response = nmc_readline ("%s (%s): ", tmp, user); + g_free (tmp); + } else + response = nmc_readline ("%s", request); + g_print ("\n"); + + /* Restore original terminal settings */ + if (!echo_on) + tcsetattr (STDIN_FILENO, TCSADRAIN, &termios_orig); + + return response; +} + +static void +polkit_show_info (const char *text) +{ + g_print (_("Authentication message: %s\n"), text); +} + +static void +polkit_show_error (const char *text) +{ + g_print (_("Authentication error: %s\n"), text); +} + +static void +polkit_completed (gboolean gained_authorization) +{ + /* We don't print anything here. The outcome will be evident from + * the operation result anyway. */ +} + +gboolean +nmc_polkit_agent_init (NmCli* nmc, gboolean for_session, GError **error) +{ + PolkitAgentListener *listener; + + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + /* We don't register polkit agent at all when running non-interactively */ + if (!nmc->ask) + return TRUE; + + listener = nm_polkit_listener_new (for_session, error); + if (!listener) + return FALSE; + + nm_polkit_listener_set_request_callback (NM_POLKIT_LISTENER (listener), polkit_request, nmc); + nm_polkit_listener_set_show_info_callback (NM_POLKIT_LISTENER (listener), polkit_show_info); + nm_polkit_listener_set_show_error_callback (NM_POLKIT_LISTENER (listener), polkit_show_error); + nm_polkit_listener_set_completed_callback (NM_POLKIT_LISTENER (listener), polkit_completed); + + nmc->pk_listener = NM_POLKIT_LISTENER (listener); + return TRUE; +} + +void +nmc_polkit_agent_fini (NmCli* nmc) +{ + g_clear_object (&nmc->pk_listener); +} + +#else +/* polkit agent is not avalable; implement stub functions. */ + +#include +#include "nmcli.h" +#include "polkit-agent.h" + +gboolean +nmc_polkit_agent_init (NmCli* nmc, gboolean for_session, GError **error) +{ + return TRUE; +} + +void +nmc_polkit_agent_fini (NmCli* nmc) +{ +} + +#endif /* #if WITH_POLKIT_AGENT */ diff --git a/clients/cli/polkit-agent.h b/clients/cli/polkit-agent.h new file mode 100644 index 0000000000..2e0326bc97 --- /dev/null +++ b/clients/cli/polkit-agent.h @@ -0,0 +1,28 @@ +/* nmcli - command-line tool to control NetworkManager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright 2014 Red Hat, Inc. + */ + +#ifndef __NMC_POLKIT_AGENT_H__ +#define __NMC_POLKIT_AGENT_H__ + +#include "nmcli.h" + +gboolean nmc_polkit_agent_init (NmCli *nmc, gboolean for_session, GError **error); +void nmc_polkit_agent_fini (NmCli* nmc); + +#endif /* __NMC_POLKIT_AGENT_H__ */ diff --git a/po/POTFILES.in b/po/POTFILES.in index bbe4bb1bde..5417b07dc3 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -6,6 +6,7 @@ clients/cli/connections.c clients/cli/devices.c clients/cli/general.c clients/cli/nmcli.c +clients/cli/polkit-agent.c clients/cli/settings.c clients/cli/utils.c clients/common/nm-polkit-listener.c -- cgit v1.2.1 From 3a551664dfe3477487065528ffcade41281f4f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Thu, 30 Oct 2014 15:45:43 +0100 Subject: cli: add 'nmcli agent' command (bgo #739568) Synopsis: nmcli agent { secret | polkit | all } The command runs separate NetworkManager secret agent or session polkit agent, or both. It is useful when - no other secret agent is available (such as GUI nm-applet, gnome-shell, KDE applet) - no other polkit agent is available (such as polkit-gnome-authentication-agent-1, polkit-kde-authentication-agent-1 or lxpolkit) https://bugzilla.gnome.org/show_bug.cgi?id=739568 --- clients/cli/Makefile.am | 2 + clients/cli/agent.c | 251 +++++++++++++++++++++++++++++++++++++++++++ clients/cli/agent.h | 29 +++++ clients/cli/connections.c | 4 + clients/cli/devices.c | 4 + clients/cli/general.c | 10 ++ clients/cli/nmcli-completion | 7 +- clients/cli/nmcli.c | 11 +- clients/cli/polkit-agent.c | 28 ++++- clients/cli/polkit-agent.h | 2 + man/nmcli.1.in | 37 ++++++- po/POTFILES.in | 1 + 12 files changed, 371 insertions(+), 15 deletions(-) create mode 100644 clients/cli/agent.c create mode 100644 clients/cli/agent.h diff --git a/clients/cli/Makefile.am b/clients/cli/Makefile.am index 2060c4bfff..0820aab045 100644 --- a/clients/cli/Makefile.am +++ b/clients/cli/Makefile.am @@ -16,6 +16,8 @@ AM_CPPFLAGS = \ -DNMCLI_LOCALEDIR=\"$(datadir)/locale\" nmcli_SOURCES = \ + agent.c \ + agent.h \ common.c \ common.h \ connections.c \ diff --git a/clients/cli/agent.c b/clients/cli/agent.c new file mode 100644 index 0000000000..9d4869b11e --- /dev/null +++ b/clients/cli/agent.c @@ -0,0 +1,251 @@ +/* + * nmcli - command-line tool for controlling NetworkManager + * Functions for running NM secret agent. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright 2014 Red Hat, Inc. + */ + +#include "config.h" + +#include +#include +#include +#include +#include + +#include +#include + +#include "common.h" +#include "utils.h" +#include "nm-secret-agent-simple.h" +#include "polkit-agent.h" +#include "agent.h" + +static void +usage (void) +{ + g_printerr (_("Usage: nmcli agent { COMMAND | help }\n\n" + "COMMAND := { secret | polkit | all }\n\n" + )); +} + +static void +usage_agent_secret (void) +{ + g_printerr (_("Usage: nmcli agent secret { help }\n" + "\n" + "Runs nmcli as NetworkManager secret agent. When NetworkManager requires\n" + "a password it asks registered agents for it. This command keeps nmcli running\n" + "and if a password is required asks the user for it.\n\n")); +} + +static void +usage_agent_polkit (void) +{ + g_printerr (_("Usage: nmcli agent polkit { help }\n" + "\n" + "Registers nmcli as a polkit action for the user session.\n" + "When a polkit daemon requires an authorization, nmcli asks the user and gives\n" + "the reponse back to polkit.\n\n")); +} + +static void +usage_agent_all (void) +{ + g_printerr (_("Usage: nmcli agent all { help }\n" + "\n" + "Runs nmcli as both NetworkManager secret and a polkit agent.\n\n")); +} + +/* for pre-filling a string to readline prompt */ +static char *pre_input_deftext; +static int +set_deftext (void) +{ + if (pre_input_deftext && rl_startup_hook) { + rl_insert_text (pre_input_deftext); + g_free (pre_input_deftext); + pre_input_deftext = NULL; + rl_startup_hook = NULL; + } + return 0; +} + +static gboolean +get_secrets_from_user (const char *request_id, + const char *title, + const char *msg, + GPtrArray *secrets) +{ + int i; + + for (i = 0; i < secrets->len; i++) { + NMSecretAgentSimpleSecret *secret = secrets->pdata[i]; + char *pwd = NULL; + + /* Ask user for the password */ + g_print ("%s\n", msg); + if (secret->value) { + /* Prefill the password if we have it. */ + rl_startup_hook = set_deftext; + pre_input_deftext = g_strdup (secret->value); + } + pwd = nmc_readline ("%s (%s): ", secret->name, secret->prop_name); + + /* No password provided, cancel the secrets. */ + if (!pwd) + return FALSE; + g_free (secret->value); + secret->value = pwd; + } + return TRUE; +} + +static void +secrets_requested (NMSecretAgentSimple *agent, + const char *request_id, + const char *title, + const char *msg, + GPtrArray *secrets, + gpointer user_data) +{ + NmCli *nmc = (NmCli *) user_data; + gboolean success = FALSE; + + if (nmc->print_output == NMC_PRINT_PRETTY) + nmc_terminal_erase_line (); + + success = get_secrets_from_user (request_id, title, msg, secrets); + if (success) + nm_secret_agent_simple_response (agent, request_id, secrets); + else + nm_secret_agent_simple_response (agent, request_id, NULL); +} + + +static NMCResultCode +do_agent_secret (NmCli *nmc, int argc, char **argv) +{ + /* Create secret agent */ + nmc->secret_agent = nm_secret_agent_simple_new ("nmcli-agent"); + if (nmc->secret_agent) { + /* We keep running */ + nmc->should_wait = TRUE; + + g_signal_connect (nmc->secret_agent, "request-secrets", G_CALLBACK (secrets_requested), nmc); + g_print (_("nmcli successfully registered as a NetworkManager's secret agent.\n")); + } else { + g_string_printf (nmc->return_text, _("Error: secret agent initialization failed")); + nmc->return_value = NMC_RESULT_ERROR_UNKNOWN; + } + + return nmc->return_value; +} + +static NMCResultCode +do_agent_polkit (NmCli *nmc, int argc, char **argv) +{ + GError *error = NULL; + + /* Initialize polkit agent */ + if (!nmc_polkit_agent_init (nmc, TRUE, &error)) { + g_dbus_error_strip_remote_error (error); + g_string_printf (nmc->return_text, _("Error: polkit agent initialization failed: %s"), + error->message); + nmc->return_value = NMC_RESULT_ERROR_UNKNOWN; + g_error_free (error); + } else { + /* We keep running */ + nmc->should_wait = TRUE; + + g_print (_("nmcli successfully registered as a polkit agent.\n")); + } + + return nmc->return_value; +} + +static NMCResultCode +do_agent_all (NmCli *nmc, int argc, char **argv) +{ + NMCResultCode secret_res; + + /* Run both secret and polkit agent */ + secret_res = do_agent_secret (nmc, argc, argv); + if (secret_res != NMC_RESULT_SUCCESS) + g_printerr ("%s\n", nmc->return_text->str); + + nmc->return_value = do_agent_polkit (nmc, argc, argv); + + if (nmc->return_value == NMC_RESULT_SUCCESS && secret_res != NMC_RESULT_SUCCESS) + nmc->return_value = secret_res; + + return nmc->return_value; +} + +NMCResultCode +do_agent (NmCli *nmc, int argc, char **argv) +{ + /* Get NMClient object */ + nmc->get_client (nmc); + + /* Check whether NetworkManager is running */ + if (!nm_client_get_nm_running (nmc->client)) { + g_string_printf (nmc->return_text, _("Error: NetworkManager is not running.")); + nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING; + return nmc->return_value; + } + /* Compare NM and nmcli versions */ + if (!nmc_versions_match (nmc)) + return nmc->return_value; + + if (argc == 0) { + nmc->return_value = do_agent_all (nmc, 0, NULL); + } + + if (argc > 0) { + if (nmc_arg_is_help (*argv)) { + usage (); + goto usage_exit; + } else if (matches (*argv, "secret") == 0) { + if (nmc_arg_is_help (*(argv+1))) { + usage_agent_secret (); + goto usage_exit; + } + nmc->return_value = do_agent_secret (nmc, argc-1, argv+1); + } else if (matches (*argv, "polkit") == 0) { + if (nmc_arg_is_help (*(argv+1))) { + usage_agent_polkit (); + goto usage_exit; + } + nmc->return_value = do_agent_polkit (nmc, argc-1, argv+1); + } else if (matches (*argv, "all") == 0) { + if (nmc_arg_is_help (*(argv+1))) { + usage_agent_all (); + goto usage_exit; + } + nmc->return_value = do_agent_all (nmc, argc-1, argv+1); + } else { + usage (); + g_string_printf (nmc->return_text, _("Error: 'agent' command '%s' is not valid."), *argv); + nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; + } + } + +usage_exit: + return nmc->return_value; +} diff --git a/clients/cli/agent.h b/clients/cli/agent.h new file mode 100644 index 0000000000..70ea0d9b23 --- /dev/null +++ b/clients/cli/agent.h @@ -0,0 +1,29 @@ +/* + * nmcli - command-line tool for controlling NetworkManager + * Functions for running NM secret agent. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright 2014 Red Hat, Inc. + */ + +#ifndef __NMC_AGENT_H__ +#define __NMC_AGENT_H__ + +#include "nmcli.h" + +NMCResultCode do_agent (NmCli *nmc, int argc, char **argv); + +#endif /* __NMC_AGENT_H__ */ diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 717945b33d..554e2107e6 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -36,6 +36,7 @@ #include "settings.h" #include "connections.h" #include "nm-secret-agent-simple.h" +#include "polkit-agent.h" /* define some prompts for connection editor */ #define EDITOR_PROMPT_SETTING _("Setting name? ") @@ -8796,6 +8797,9 @@ do_connections (NmCli *nmc, int argc, char **argv) { GError *error = NULL; + /* Register polkit agent */ + nmc_start_polkit_agent_start_try (nmc); + /* Set completion function for 'nmcli con' */ rl_attempted_completion_function = (rl_completion_func_t *) nmcli_con_tab_completion; diff --git a/clients/cli/devices.c b/clients/cli/devices.c index 3d9598fb41..c073ec252b 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -28,6 +28,7 @@ #include #include +#include "polkit-agent.h" #include "utils.h" #include "common.h" #include "devices.h" @@ -2776,6 +2777,9 @@ do_devices (NmCli *nmc, int argc, char **argv) { GError *error = NULL; + /* Register polkit agent */ + nmc_start_polkit_agent_start_try (nmc); + rl_attempted_completion_function = (rl_completion_func_t *) nmcli_device_tab_completion; /* Get NMClient object early */ diff --git a/clients/cli/general.c b/clients/cli/general.c index 531d4f7acd..44c63c356c 100644 --- a/clients/cli/general.c +++ b/clients/cli/general.c @@ -25,6 +25,7 @@ #include #include +#include "polkit-agent.h" #include "utils.h" #include "general.h" @@ -558,6 +559,9 @@ do_general (NmCli *nmc, int argc, char **argv) { GError *error = NULL; + /* Register polkit agent */ + nmc_start_polkit_agent_start_try (nmc); + if (argc == 0) { if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error)) { g_string_printf (nmc->return_text, _("Error: %s."), error->message); @@ -726,6 +730,9 @@ do_networking (NmCli *nmc, int argc, char **argv) { gboolean enable_flag; + /* Register polkit agent */ + nmc_start_polkit_agent_start_try (nmc); + if (argc == 0) nmc_switch_show (nmc, NMC_FIELDS_NM_NETWORKING, _("Networking")); else if (argc > 0) { @@ -787,6 +794,9 @@ do_radio (NmCli *nmc, int argc, char **argv) GError *error = NULL; gboolean enable_flag; + /* Register polkit agent */ + nmc_start_polkit_agent_start_try (nmc); + if (argc == 0) { if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error)) { g_string_printf (nmc->return_text, _("Error: %s."), error->message); diff --git a/clients/cli/nmcli-completion b/clients/cli/nmcli-completion index 1e087f7419..72648d43cb 100644 --- a/clients/cli/nmcli-completion +++ b/clients/cli/nmcli-completion @@ -701,7 +701,7 @@ _nmcli() # (if the current word starts with a dash) or the OBJECT list # otherwise. if [[ "${words[0]:0:1}" != '-' ]]; then - OPTIONS=(help general networking radio connection device) + OPTIONS=(help general networking radio connection device agent) elif [[ "${words[0]:1:1}" == '-' || "${words[0]}" == "-" ]]; then OPTIONS=("${LONG_OPTIONS[@]/#/--}") else @@ -1268,6 +1268,11 @@ _nmcli() esac fi ;; + a|ag|age|agen|agent) + if [[ ${#words[@]} -eq 2 ]]; then + _nmcli_compl_COMMAND "$command" secret polkit all + fi + ;; esac return 0 diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index 76033417ee..ade0a6bd4d 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -43,6 +43,7 @@ #include "connections.h" #include "devices.h" #include "general.h" +#include "agent.h" #if defined(NM_DIST_VERSION) # define NMCLI_VERSION NM_DIST_VERSION @@ -102,6 +103,7 @@ usage (const char *prog_name) " r[adio] NetworkManager radio switches\n" " c[onnection] NetworkManager's connections\n" " d[evice] devices managed by NetworkManager\n" + " a[gent] NetworkManager secret agent or polkit agent\n" "\n"), prog_name); } @@ -122,6 +124,7 @@ static const struct cmd { { "radio", do_radio }, { "connection", do_connections }, { "device", do_devices }, + { "agent", do_agent }, { "help", do_help }, { 0 } }; @@ -266,14 +269,6 @@ parse_command_line (NmCli *nmc, int argc, char **argv) } if (argc > 1) { - GError *error = NULL; - - /* Initialize polkit agent */ - if (!nmc_polkit_agent_init (&nm_cli, FALSE, &error)) { - g_printerr ("Polkit agent initialization failed: %s\n", error->message); - g_error_free (error); - } - /* Now run the requested command */ return do_cmd (nmc, argv[1], argc-1, argv+1); } diff --git a/clients/cli/polkit-agent.c b/clients/cli/polkit-agent.c index e8502884e4..a163b824fa 100644 --- a/clients/cli/polkit-agent.c +++ b/clients/cli/polkit-agent.c @@ -102,10 +102,6 @@ nmc_polkit_agent_init (NmCli* nmc, gboolean for_session, GError **error) g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - /* We don't register polkit agent at all when running non-interactively */ - if (!nmc->ask) - return TRUE; - listener = nm_polkit_listener_new (for_session, error); if (!listener) return FALSE; @@ -125,6 +121,24 @@ nmc_polkit_agent_fini (NmCli* nmc) g_clear_object (&nmc->pk_listener); } +gboolean +nmc_start_polkit_agent_start_try (NmCli *nmc) +{ + GError *error = NULL; + + /* We don't register polkit agent at all when running non-interactively */ + if (!nmc->ask) + return TRUE; + + if (!nmc_polkit_agent_init (nmc, FALSE, &error)) { + g_printerr (_("Warning: polkit agent initialization failed: %s\n"), + error->message); + g_error_free (error); + return FALSE; + } + return TRUE; +} + #else /* polkit agent is not avalable; implement stub functions. */ @@ -143,4 +157,10 @@ nmc_polkit_agent_fini (NmCli* nmc) { } +gboolean +nmc_start_polkit_agent_start_try (NmCli *nmc) +{ + return TRUE; +} + #endif /* #if WITH_POLKIT_AGENT */ diff --git a/clients/cli/polkit-agent.h b/clients/cli/polkit-agent.h index 2e0326bc97..e2902dc0b1 100644 --- a/clients/cli/polkit-agent.h +++ b/clients/cli/polkit-agent.h @@ -25,4 +25,6 @@ gboolean nmc_polkit_agent_init (NmCli *nmc, gboolean for_session, GError **error); void nmc_polkit_agent_fini (NmCli* nmc); +gboolean nmc_start_polkit_agent_start_try (NmCli *nmc); + #endif /* __NMC_POLKIT_AGENT_H__ */ diff --git a/man/nmcli.1.in b/man/nmcli.1.in index 2d142f4eaa..66e1567d45 100644 --- a/man/nmcli.1.in +++ b/man/nmcli.1.in @@ -21,7 +21,7 @@ .\" .\" Copyright 2010 - 2014 Red Hat, Inc. .\" -.TH NMCLI "1" "11 September 2014" +.TH NMCLI "1" "4 November 2014" .SH NAME nmcli \- command\(hyline tool for controlling NetworkManager @@ -33,7 +33,7 @@ nmcli \- command\(hyline tool for controlling NetworkManager .sp .IR OBJECT " := { " -.BR general " | " networking " | " radio " | " connection " | " device +.BR general " | " networking " | " radio " | " connection " | " device " | " agent .RI " }" .sp @@ -793,6 +793,39 @@ This command does not show the APs, use 'nmcli device wifi list' for that. List available WiMAX NSP. The \fIifname\fP and \fInsp\fP options can be used to list networks for a particular interface or with a specific NSP, respectively. +.RE + +.TP +.B agent \- run nmcli as a NetworkManager secret agent, or polkit agent +.br +.TP +.SS \fICOMMAND\fP := { secret | polkit | all } +.sp +.RS +.TP +.B secret +.br +Register nmcli as a NetworkManager secret agent and listen for secret requests. +You do usually not need this command, because nmcli can handle secrets when +connecting to networks. However, you may find the command useful when you use +another tool for activating connections and you do not have a secret agent +available (like nm-applet). +.TP +.B polkit +.br +Register nmcli as a polkit agent for the user session and listen for +authorization requests. You do not usually need this command, because nmcli can +handle polkit actions related to NetworkManager operations (when run with +--ask). However, you may find the command useful when you want to run a simple +text based polkit agent and you do not have an agent of a desktop environment. +Note that running this command makes nmcli handle all polkit requests, not only +NetworkManager related ones, because only one polkit agent can run for the +session. +.TP +.B all +.br +Runs nmcli as both NetworkManager secret and a polkit agent. +.RE .SH ENVIRONMENT VARIABLES \fInmcli\fP's behavior is affected by the following environment variables. diff --git a/po/POTFILES.in b/po/POTFILES.in index 5417b07dc3..6d23a67b77 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -1,6 +1,7 @@ [encoding: UTF-8] # List of source files containing translatable strings. # Please keep this file sorted alphabetically. +clients/cli/agent.c clients/cli/common.c clients/cli/connections.c clients/cli/devices.c -- cgit v1.2.1 From b1550df6f5cdbb1a4ac5f9550f260c5b1fff8aca Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Fri, 7 Nov 2014 11:47:19 +0100 Subject: sd-dhcp-client: Add missing initializers Missing initializers together with automatic cleanup seem to annoy GCC's -Werror=maybe-uninitialized, breaking the --enable-more-warnings=error builds. --- .../systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c | 2 +- .../systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c | 2 +- .../systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c | 2 +- src/dhcp-manager/systemd-dhcp/src/shared/fileio.c | 8 +++----- src/dhcp-manager/systemd-dhcp/src/shared/time-util.c | 6 ++---- src/dhcp-manager/systemd-dhcp/src/shared/util.c | 8 ++++---- 6 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c index df8abdf3a3..18c23f15c1 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-client.c @@ -397,7 +397,7 @@ static void client_stop(sd_dhcp_client *client, int error) { static int client_message_init(sd_dhcp_client *client, DHCPPacket **ret, uint8_t type, size_t *_optlen, size_t *_optoffset) { - _cleanup_free_ DHCPPacket *packet; + _cleanup_free_ DHCPPacket *packet = NULL; size_t optlen, optoffset, size; be16_t max_size; usec_t time_now; diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c index 4d078b4c8a..e61ae051f3 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp-lease.c @@ -687,7 +687,7 @@ int sd_dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) { r = sd_dhcp_lease_get_client_id(lease, &client_id, &client_id_len); if (r >= 0) { - _cleanup_free_ char *client_id_hex; + _cleanup_free_ char *client_id_hex = NULL; client_id_hex = hexmem (client_id, client_id_len); if (!client_id_hex) { diff --git a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c index 68625cc851..f9a9d5e9e5 100644 --- a/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/dhcp-manager/systemd-dhcp/src/libsystemd-network/sd-dhcp6-client.c @@ -844,7 +844,7 @@ static int client_receive_message(sd_event_source *s, int fd, uint32_t revents, void *userdata) { sd_dhcp6_client *client = userdata; DHCP6_CLIENT_DONT_DESTROY(client); - _cleanup_free_ DHCP6Message *message; + _cleanup_free_ DHCP6Message *message = NULL; int r, buflen, len; assert(s); diff --git a/src/dhcp-manager/systemd-dhcp/src/shared/fileio.c b/src/dhcp-manager/systemd-dhcp/src/shared/fileio.c index 38028b972e..ffdc122110 100644 --- a/src/dhcp-manager/systemd-dhcp/src/shared/fileio.c +++ b/src/dhcp-manager/systemd-dhcp/src/shared/fileio.c @@ -145,7 +145,7 @@ int read_one_line_file(const char *fn, char **line) { } ssize_t sendfile_full(int out_fd, const char *fn) { - _cleanup_fclose_ FILE *f; + _cleanup_fclose_ FILE *f = NULL; struct stat st; int r; ssize_t s; @@ -589,17 +589,15 @@ static int parse_env_file_push( va_list aq, *ap = userdata; if (!utf8_is_valid(key)) { - _cleanup_free_ char *p; + _cleanup_free_ char *p = utf8_escape_invalid(key); - p = utf8_escape_invalid(key); log_error("%s:%u: invalid UTF-8 in key '%s', ignoring.", strna(filename), line, p); return -EINVAL; } if (value && !utf8_is_valid(value)) { - _cleanup_free_ char *p; + _cleanup_free_ char *p = utf8_escape_invalid(value); - p = utf8_escape_invalid(value); log_error("%s:%u: invalid UTF-8 value for key %s: '%s', ignoring.", strna(filename), line, key, p); return -EINVAL; } diff --git a/src/dhcp-manager/systemd-dhcp/src/shared/time-util.c b/src/dhcp-manager/systemd-dhcp/src/shared/time-util.c index efc18bc319..184f57bdc6 100644 --- a/src/dhcp-manager/systemd-dhcp/src/shared/time-util.c +++ b/src/dhcp-manager/systemd-dhcp/src/shared/time-util.c @@ -507,9 +507,8 @@ int parse_timestamp(const char *t, usec_t *usec) { return parse_sec(t + 1, usec); else if (endswith(t, " ago")) { - _cleanup_free_ char *z; + _cleanup_free_ char *z = strndup(t, strlen(t) - 4); - z = strndup(t, strlen(t) - 4); if (!z) return -ENOMEM; @@ -519,9 +518,8 @@ int parse_timestamp(const char *t, usec_t *usec) { goto finish; } else if (endswith(t, " left")) { - _cleanup_free_ char *z; + _cleanup_free_ char *z = strndup(t, strlen(t) - 4); - z = strndup(t, strlen(t) - 4); if (!z) return -ENOMEM; diff --git a/src/dhcp-manager/systemd-dhcp/src/shared/util.c b/src/dhcp-manager/systemd-dhcp/src/shared/util.c index af607386ec..56fea661ae 100644 --- a/src/dhcp-manager/systemd-dhcp/src/shared/util.c +++ b/src/dhcp-manager/systemd-dhcp/src/shared/util.c @@ -1619,7 +1619,7 @@ bool fstype_is_network(const char *fstype) { } int chvt(int vt) { - _cleanup_close_ int fd; + _cleanup_close_ int fd = -1; fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC); if (fd < 0) @@ -2490,7 +2490,7 @@ char* dirname_malloc(const char *path) { #endif int dev_urandom(void *p, size_t n) { - _cleanup_close_ int fd; + _cleanup_close_ int fd = -1; ssize_t k; fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY); @@ -3492,7 +3492,7 @@ char *ellipsize(const char *s, size_t length, unsigned percent) { } int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode) { - _cleanup_close_ int fd; + _cleanup_close_ int fd = -1; int r; assert(path); @@ -4214,7 +4214,7 @@ int terminal_vhangup_fd(int fd) { } int terminal_vhangup(const char *name) { - _cleanup_close_ int fd; + _cleanup_close_ int fd = -1; fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC); if (fd < 0) -- cgit v1.2.1 From 252286729cdc7b1e6079140fe7f41d17190812ad Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Nov 2014 12:25:25 +0100 Subject: gitignore: ignore dhcp test binary Signed-off-by: Thomas Haller --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 86209c29bc..d7c47e3899 100644 --- a/.gitignore +++ b/.gitignore @@ -230,6 +230,7 @@ valgrind-*.log /src/dhcp-manager/nm-dhcp-helper /src/dhcp-manager/tests/test-dhcp-dhclient /src/dhcp-manager/tests/test-dhcp-options +/src/dhcp-manager/tests/test-dhcp-utils /src/dnsmasq-manager/tests/test-dnsmasq-utils /src/settings/plugins/ibft/tests/test-ibft /src/settings/plugins/ifcfg-rh/tests/network-scripts/*-Test_Write_* -- cgit v1.2.1 From 890fff4be758376a0a6f1bb508514c2c247cdc28 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Nov 2014 12:33:09 +0100 Subject: dhcp: fix swapped order of argument for logging function Signed-off-by: Thomas Haller --- src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h b/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h index 9282aaaf94..1308109d9a 100644 --- a/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h +++ b/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h @@ -53,7 +53,7 @@ _slog_level_to_nm (int slevel) G_STMT_START { \ guint32 _l = _slog_level_to_nm ((level)); \ if (nm_logging_enabled (_l, LOGD_DHCP)) \ - _nm_log (#file ":" #line, func, LOGD_DHCP, _l, format, ## __VA_ARGS__); \ + _nm_log (#file ":" #line, func, _l, LOGD_DHCP, format, ## __VA_ARGS__); \ } G_STMT_END #define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__) -- cgit v1.2.1 From 57133a389f326940e745031e5fef7dccc434247e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Nov 2014 12:38:12 +0100 Subject: dhcp/test: fix compiler warning about pointer arithmetric for void* clang warns: make[4]: Entering directory `./NetworkManager/src/dhcp-manager/tests' CC test-dhcp-utils.o test-dhcp-utils.c:684:2: error: arithmetic on a pointer to void is a GNU extension [-Werror,-Wpointer-arith] COMPARE_ID (nothex, TRUE, nothex, strlen (nothex)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test-dhcp-utils.c:667:23: note: expanded from macro 'COMPARE_ID' g_assert (memcmp (p + 1, expected, expected_len) == 0); \ ~ ^ Signed-off-by: Thomas Haller --- src/dhcp-manager/tests/test-dhcp-utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dhcp-manager/tests/test-dhcp-utils.c b/src/dhcp-manager/tests/test-dhcp-utils.c index 6ed10e0474..2c87664fa9 100644 --- a/src/dhcp-manager/tests/test-dhcp-utils.c +++ b/src/dhcp-manager/tests/test-dhcp-utils.c @@ -655,7 +655,7 @@ test_ip4_prefix_classless (void) #define COMPARE_ID(src, is_str, expected, expected_len) \ G_STMT_START { \ gs_unref_bytes GBytes *b = NULL; \ - gconstpointer p; \ + const char *p; \ gsize l; \ \ b = nm_dhcp_utils_client_id_string_to_bytes (src); \ -- cgit v1.2.1 From 5bcd54ad82b961cb5b794d86e70ba99244fa1e6d Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Nov 2014 13:42:40 +0100 Subject: vpn: fix cleanup of connection timeout in maybe_complete() Fixes: a966a5e8b5c0b62975833b12bbb67d7e87f307ce Signed-off-by: Thomas Haller --- src/vpn-manager/nm-vpn-connection.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vpn-manager/nm-vpn-connection.c b/src/vpn-manager/nm-vpn-connection.c index a348ab7182..46e14ca153 100644 --- a/src/vpn-manager/nm-vpn-connection.c +++ b/src/vpn-manager/nm-vpn-connection.c @@ -959,7 +959,7 @@ nm_vpn_connection_config_maybe_complete (NMVpnConnection *connection, } } - if (priv->connect_timeout == 0) { + if (priv->connect_timeout) { g_source_remove (priv->connect_timeout); priv->connect_timeout = 0; } -- cgit v1.2.1 From ec976324b8adf0eed6f3b4fbee8ebce5094161b2 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 14 Oct 2014 08:56:50 -0400 Subject: libnm-core: fix NMSetting property override docs The docs for _nm_setting_class_add_dbus_only_property() and _nm_setting_class_override_property() mistakenly still referred to some functionality that didn't make it into the final version, and also had only been partially updated for the GValue->GVariant change. --- libnm-core/nm-setting.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/libnm-core/nm-setting.c b/libnm-core/nm-setting.c index 645d8d3a8f..ff23d9861b 100644 --- a/libnm-core/nm-setting.c +++ b/libnm-core/nm-setting.c @@ -374,11 +374,8 @@ add_property_override (NMSettingClass *setting_class, * the serialization.) * * When deserializing a D-Bus representation into a setting, if @property_name - * is present, then @set_func will be called to set (and/or verify) it. If it - * returns %TRUE, the value is considered to have been successfully set; if it - * returns %FALSE then the deserializing operation as a whole will fail with the - * returned #GError. (If @set_func is %NULL then the property will be ignored - * when deserializing.) + * is present, then @set_func will be called to set it. (If @set_func is %NULL + * then the property will be ignored when deserializing.) */ void _nm_setting_class_add_dbus_only_property (NMSettingClass *setting_class, @@ -412,17 +409,15 @@ _nm_setting_class_add_dbus_only_property (NMSettingClass *setting_class, * @property_name on @setting_class. * * When serializing a setting to D-Bus, if @get_func is non-%NULL, then it will - * be called to get the property's value. If it returns %TRUE, the value will be - * added to the hash, and if %FALSE, it will not. (If @get_func is %NULL, the - * property will be read normally with g_object_get_property(), and added to the - * hash if it is not the default value.) + * be called to get the property's value. If it returns a #GVariant, the + * property will be added to the hash, and if it returns %NULL, the property + * will be omitted. (If @get_func is %NULL, the property will be read normally + * with g_object_get_property(), and added to the hash if it is not the default + * value.) * * When deserializing a D-Bus representation into a setting, if @property_name - * is present, then @set_func will be called to set (and/or verify) it. If it - * returns %TRUE, the value is considered to have been successfully set; if it - * returns %FALSE then the deserializing operation as a whole will fail with the - * returned #GError. (If @set_func is %NULL then the property will be set normally - * with g_object_set_property().) + * is present, then @set_func will be called to set it. (If @set_func is %NULL + * then the property will be set normally with g_object_set_property().) * * If @not_set_func is non-%NULL, then it will be called when deserializing a * representation that does NOT contain @property_name. This can be used, eg, if -- cgit v1.2.1 From 303e84e65e5b9b5a403e4f8366e094447d51a9fa Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 14 Oct 2014 09:09:15 -0400 Subject: libnm-core: tweak handling of overridden properties nm_setting_compare() and nm_setting_diff() were ignoring the get_func of overridden properties, because that function requires passing an NMConnection, and they didn't have one to pass. This wasn't a problem yet because the only user of _nm_setting_class_override_property() wasn't using a get_func anyway, but it would cause problems later. The connection arg to NMSettingPropertyGetFunc is really there to be used by D-Bus-only properties (which don't get compared anyway), not for ordinary property overrides. So split it into two different function types: NMSettingPropertySynthFunc (used by D-Bus-only properties, to synthesize a fake property value for D-Bus, possibly using other properties in the NMConnection), and NMSettingPropertyGetFunc (used by overridden properties for both D-Bus and comparison purposes, and not getting an NMConnection argument). --- libnm-core/nm-setting-private.h | 4 +++- libnm-core/nm-setting.c | 38 +++++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/libnm-core/nm-setting-private.h b/libnm-core/nm-setting-private.h index 56c4f46c48..bb9f1d8ee1 100644 --- a/libnm-core/nm-setting-private.h +++ b/libnm-core/nm-setting-private.h @@ -111,6 +111,8 @@ NMSetting *_nm_setting_new_from_dbus (GType setting_type, GError **error); typedef GVariant * (*NMSettingPropertyGetFunc) (NMSetting *setting, + const char *property); +typedef GVariant * (*NMSettingPropertySynthFunc) (NMSetting *setting, NMConnection *connection, const char *property); typedef void (*NMSettingPropertySetFunc) (NMSetting *setting, @@ -124,7 +126,7 @@ typedef void (*NMSettingPropertyNotSetFunc) (NMSetting *setting, void _nm_setting_class_add_dbus_only_property (NMSettingClass *setting_class, const char *property_name, const GVariantType *dbus_type, - NMSettingPropertyGetFunc get_func, + NMSettingPropertySynthFunc synth_func, NMSettingPropertySetFunc set_func); void _nm_setting_class_override_property (NMSettingClass *setting_class, diff --git a/libnm-core/nm-setting.c b/libnm-core/nm-setting.c index ff23d9861b..680db50938 100644 --- a/libnm-core/nm-setting.c +++ b/libnm-core/nm-setting.c @@ -291,6 +291,7 @@ typedef struct { const GVariantType *dbus_type; NMSettingPropertyGetFunc get_func; + NMSettingPropertySynthFunc synth_func; NMSettingPropertySetFunc set_func; NMSettingPropertyNotSetFunc not_set_func; @@ -325,6 +326,7 @@ add_property_override (NMSettingClass *setting_class, GParamSpec *param_spec, const GVariantType *dbus_type, NMSettingPropertyGetFunc get_func, + NMSettingPropertySynthFunc synth_func, NMSettingPropertySetFunc set_func, NMSettingPropertyNotSetFunc not_set_func, NMSettingPropertyTransformToFunc to_dbus, @@ -341,6 +343,7 @@ add_property_override (NMSettingClass *setting_class, override.param_spec = param_spec; override.dbus_type = dbus_type; override.get_func = get_func; + override.synth_func = synth_func; override.set_func = set_func; override.not_set_func = not_set_func; override.to_dbus = to_dbus; @@ -361,17 +364,17 @@ add_property_override (NMSettingClass *setting_class, * @setting_class: the setting class * @property_name: the name of the property to override * @dbus_type: the type of the property (in its D-Bus representation) - * @get_func: (allow-none): function to call to get the value of the property + * @synth_func: (allow-none): function to call to synthesize a value for the property * @set_func: (allow-none): function to call to set the value of the property * * Registers a property named @property_name, which will be used in the D-Bus * serialization of objects of @setting_class, but which does not correspond to * a #GObject property. * - * When serializing a setting to D-Bus, @get_func will be called to get the - * property's value. (If it returns %NULL, no value will be added to the - * serialization. If @get_func is %NULL, the property will always be omitted in - * the serialization.) + * When serializing a setting to D-Bus, @synth_func will be called to synthesize + * a value for the property. (If it returns %NULL, no value will be added to the + * serialization. If @synth_func is %NULL, the property will always be omitted + * in the serialization.) * * When deserializing a D-Bus representation into a setting, if @property_name * is present, then @set_func will be called to set it. (If @set_func is %NULL @@ -381,7 +384,7 @@ void _nm_setting_class_add_dbus_only_property (NMSettingClass *setting_class, const char *property_name, const GVariantType *dbus_type, - NMSettingPropertyGetFunc get_func, + NMSettingPropertySynthFunc synth_func, NMSettingPropertySetFunc set_func) { g_return_if_fail (NM_IS_SETTING_CLASS (setting_class)); @@ -392,7 +395,7 @@ _nm_setting_class_add_dbus_only_property (NMSettingClass *setting_class, add_property_override (setting_class, property_name, NULL, dbus_type, - get_func, set_func, NULL, + NULL, synth_func, set_func, NULL, NULL, NULL); } @@ -439,7 +442,7 @@ _nm_setting_class_override_property (NMSettingClass *setting_class, add_property_override (setting_class, property_name, param_spec, dbus_type, - get_func, set_func, not_set_func, + get_func, NULL, set_func, not_set_func, NULL, NULL); } @@ -473,7 +476,7 @@ _nm_setting_class_transform_property (NMSettingClass *setting_class, add_property_override (setting_class, property, param_spec, dbus_type, - NULL, NULL, NULL, + NULL, NULL, NULL, NULL, to_dbus, from_dbus); } @@ -583,7 +586,10 @@ get_property_for_dbus (NMSetting *setting, GValue prop_value = { 0, }; GVariant *dbus_value; - g_return_val_if_fail (property->param_spec != NULL, NULL); + if (property->get_func) + return property->get_func (setting, property->name); + else + g_return_val_if_fail (property->param_spec != NULL, NULL); g_value_init (&prop_value, property->param_spec->value_type); g_object_get_property (G_OBJECT (setting), property->param_spec->name, &prop_value); @@ -651,8 +657,8 @@ _nm_setting_to_dbus (NMSetting *setting, NMConnection *connection, NMConnectionS const NMSettingProperty *property = &properties[i]; GParamSpec *prop_spec = property->param_spec; - if (!prop_spec && !property->get_func) { - /* Override property with no get_func, so we skip it. */ + if (!prop_spec && !property->synth_func) { + /* D-Bus-only property with no synth_func, so we skip it. */ continue; } @@ -667,12 +673,10 @@ _nm_setting_to_dbus (NMSetting *setting, NMConnection *connection, NMConnectionS && !(prop_spec && (prop_spec->flags & NM_SETTING_PARAM_SECRET))) continue; - if (property->get_func) - dbus_value = property->get_func (setting, connection, property->name); - else if (prop_spec) - dbus_value = get_property_for_dbus (setting, property, TRUE); + if (property->synth_func) + dbus_value = property->synth_func (setting, connection, property->name); else - g_assert_not_reached (); + dbus_value = get_property_for_dbus (setting, property, TRUE); if (dbus_value) { /* Allow dbus_value to be either floating or not. */ g_variant_take_ref (dbus_value); -- cgit v1.2.1 From 21c8a6b20effbe1e689505a0cbb23594be06068c Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 16 Sep 2014 16:42:46 -0400 Subject: libnm-core, all: merge IPv4 and IPv6 address/route types Merge NMIP4Address and NMIP6Address into NMIPAddress, and NMIP4Route and NMIP6Route into NMIPRoute. The new types represent IP addresses as strings, rather than in binary, and so are address-family agnostic. --- callouts/nm-dispatcher-utils.c | 64 +- callouts/tests/test-dispatcher-envp.c | 76 +- clients/cli/common.c | 351 +++------ clients/cli/common.h | 7 +- clients/cli/connections.c | 36 +- clients/cli/settings.c | 229 +++--- clients/tui/nm-editor-bindings.c | 825 +++++-------------- clients/tui/nm-editor-bindings.h | 38 +- clients/tui/nmt-page-ip4.c | 21 +- clients/tui/nmt-page-ip6.c | 21 +- clients/tui/nmt-route-editor.c | 4 +- clients/tui/nmt-route-entry.c | 76 +- clients/tui/nmt-route-table.c | 131 +-- docs/libnm/libnm-docs.xml | 1 + include/nm-test-utils.h | 12 - libnm-core/Makefile.libnm-core | 2 + libnm-core/nm-core-internal.h | 2 +- libnm-core/nm-setting-ip-config.c | 874 +++++++++++++++++++++ libnm-core/nm-setting-ip-config.h | 117 +++ libnm-core/nm-setting-ip4-config.c | 593 +------------- libnm-core/nm-setting-ip4-config.h | 64 +- libnm-core/nm-setting-ip6-config.c | 538 +------------ libnm-core/nm-setting-ip6-config.h | 64 +- libnm-core/nm-utils-private.h | 23 +- libnm-core/nm-utils.c | 273 ++++--- libnm-core/nm-utils.h | 2 + libnm-core/tests/test-general.c | 76 +- libnm/NetworkManager.h | 1 + libnm/libnm.ver | 87 +- libnm/nm-ip4-config.c | 20 +- libnm/nm-ip6-config.c | 20 +- src/NetworkManagerUtils.c | 39 +- src/devices/nm-device.c | 12 +- src/nm-ip4-config.c | 47 +- src/nm-ip6-config.c | 50 +- src/settings/plugins/ibft/reader.c | 26 +- src/settings/plugins/ibft/tests/test-ibft.c | 16 +- src/settings/plugins/ifcfg-rh/reader.c | 406 ++++------ .../plugins/ifcfg-rh/tests/test-ifcfg-rh.c | 783 +++++------------- src/settings/plugins/ifcfg-rh/writer.c | 139 +--- src/settings/plugins/ifnet/connection_parser.c | 219 +++--- src/settings/plugins/ifnet/net_utils.c | 108 +-- src/settings/plugins/ifnet/net_utils.h | 18 +- src/settings/plugins/ifnet/tests/test_all.c | 38 +- src/settings/plugins/ifupdown/parser.c | 50 +- .../plugins/ifupdown/tests/test-ifupdown.c | 80 +- src/settings/plugins/keyfile/reader.c | 148 +--- src/settings/plugins/keyfile/tests/test-keyfile.c | 190 ++--- src/settings/plugins/keyfile/writer.c | 237 ++---- src/tests/test-general.c | 24 +- src/vpn-manager/nm-vpn-connection.c | 20 +- 51 files changed, 2796 insertions(+), 4502 deletions(-) create mode 100644 libnm-core/nm-setting-ip-config.c create mode 100644 libnm-core/nm-setting-ip-config.h diff --git a/callouts/nm-dispatcher-utils.c b/callouts/nm-dispatcher-utils.c index 66b6a07fd8..d2deb41fdf 100644 --- a/callouts/nm-dispatcher-utils.c +++ b/callouts/nm-dispatcher-utils.c @@ -95,8 +95,6 @@ construct_ip4_items (GSList *items, GVariant *ip4_config, const char *prefix) char **dns, **wins; GString *tmp; GVariant *val; - char str_addr[INET_ADDRSTRLEN]; - char str_gw[INET_ADDRSTRLEN]; int i; if (ip4_config == NULL) @@ -111,14 +109,18 @@ construct_ip4_items (GSList *items, GVariant *ip4_config, const char *prefix) addresses = nm_utils_ip4_addresses_from_variant (val); for (i = 0; i < addresses->len; i++) { - NMIP4Address *addr = addresses->pdata[i]; - guint32 ip_prefix = nm_ip4_address_get_prefix (addr); + NMIPAddress *addr = addresses->pdata[i]; + const char *gw; char *addrtmp; - nm_utils_inet4_ntop (nm_ip4_address_get_address (addr), str_addr); - nm_utils_inet4_ntop (nm_ip4_address_get_gateway (addr), str_gw); + gw = nm_ip_address_get_gateway (addr); + if (!gw) + gw = "0.0.0.0"; - addrtmp = g_strdup_printf ("%sIP4_ADDRESS_%d=%s/%d %s", prefix, i, str_addr, ip_prefix, str_gw); + addrtmp = g_strdup_printf ("%sIP4_ADDRESS_%d=%s/%d %s", prefix, i, + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr), + gw); items = g_slist_prepend (items, addrtmp); } if (addresses->len) @@ -177,15 +179,19 @@ construct_ip4_items (GSList *items, GVariant *ip4_config, const char *prefix) routes = nm_utils_ip4_routes_from_variant (val); for (i = 0; i < routes->len; i++) { - NMIP4Route *route = routes->pdata[i]; - guint32 ip_prefix = nm_ip4_route_get_prefix (route); - guint32 metric = nm_ip4_route_get_metric (route); + NMIPRoute *route = routes->pdata[i]; + const char *next_hop; char *routetmp; - nm_utils_inet4_ntop (nm_ip4_route_get_dest (route), str_addr); - nm_utils_inet4_ntop (nm_ip4_route_get_next_hop (route), str_gw); + next_hop = nm_ip_route_get_next_hop (route); + if (!next_hop) + next_hop = "0.0.0.0"; - routetmp = g_strdup_printf ("%sIP4_ROUTE_%d=%s/%d %s %d", prefix, i, str_addr, ip_prefix, str_gw, metric); + routetmp = g_strdup_printf ("%sIP4_ROUTE_%d=%s/%d %s %d", prefix, i, + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + next_hop, + nm_ip_route_get_metric (route)); items = g_slist_prepend (items, routetmp); } items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ROUTES=%d", prefix, routes->len)); @@ -225,8 +231,6 @@ construct_ip6_items (GSList *items, GVariant *ip6_config, const char *prefix) char **dns; GString *tmp; GVariant *val; - char str_addr[INET6_ADDRSTRLEN]; - char str_gw[INET6_ADDRSTRLEN]; int i; if (ip6_config == NULL) @@ -241,14 +245,18 @@ construct_ip6_items (GSList *items, GVariant *ip6_config, const char *prefix) addresses = nm_utils_ip6_addresses_from_variant (val); for (i = 0; i < addresses->len; i++) { - NMIP6Address *addr = addresses->pdata[i]; - guint32 ip_prefix = nm_ip6_address_get_prefix (addr); + NMIPAddress *addr = addresses->pdata[i]; + const char *gw; char *addrtmp; - nm_utils_inet6_ntop (nm_ip6_address_get_address (addr), str_addr); - nm_utils_inet6_ntop (nm_ip6_address_get_gateway (addr), str_gw); + gw = nm_ip_address_get_gateway (addr); + if (!gw) + gw = "::"; - addrtmp = g_strdup_printf ("%sIP6_ADDRESS_%d=%s/%d %s", prefix, i, str_addr, ip_prefix, str_gw); + addrtmp = g_strdup_printf ("%sIP6_ADDRESS_%d=%s/%d %s", prefix, i, + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr), + gw); items = g_slist_prepend (items, addrtmp); } if (addresses->len) @@ -287,15 +295,19 @@ construct_ip6_items (GSList *items, GVariant *ip6_config, const char *prefix) routes = nm_utils_ip6_routes_from_variant (val); for (i = 0; i < routes->len; i++) { - NMIP6Route *route = routes->pdata[i]; - guint32 ip_prefix = nm_ip6_route_get_prefix (route); - guint32 metric = nm_ip6_route_get_metric (route); + NMIPRoute *route = routes->pdata[i]; + const char *next_hop; char *routetmp; - nm_utils_inet6_ntop (nm_ip6_route_get_dest (route), str_addr); - nm_utils_inet6_ntop (nm_ip6_route_get_next_hop (route), str_gw); + next_hop = nm_ip_route_get_next_hop (route); + if (!next_hop) + next_hop = "::"; - routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %d", prefix, i, str_addr, ip_prefix, str_gw, metric); + routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %d", prefix, i, + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + next_hop, + nm_ip_route_get_metric (route)); items = g_slist_prepend (items, routetmp); } if (routes->len) diff --git a/callouts/tests/test-dispatcher-envp.c b/callouts/tests/test-dispatcher-envp.c index a8c65f8413..5daf5e9a64 100644 --- a/callouts/tests/test-dispatcher-envp.c +++ b/callouts/tests/test-dispatcher-envp.c @@ -218,32 +218,29 @@ parse_ip4 (GKeyFile *kf, GVariant **out_props, const char *section, GError **err g_free (tmp); if (g_strv_length (split) > 0) { - addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_address_unref); + addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_address_unref); for (iter = split; iter && *iter; iter++) { - NMIP4Address *addr; - guint32 a; - char *p; + NMIPAddress *addr; + char *ip, *prefix, *gw; if (strlen (g_strstrip (*iter)) == 0) continue; - addr = nm_ip4_address_new (); + ip = *iter; - p = strchr (*iter, '/'); - g_assert (p); - *p++ = '\0'; + prefix = strchr (ip, '/'); + g_assert (prefix); + *prefix++ = '\0'; - g_assert_cmpint (inet_pton (AF_INET, *iter, &a), ==, 1); - nm_ip4_address_set_address (addr, a); - nm_ip4_address_set_prefix (addr, (guint) atoi (p)); - - p = strchr (p, ' '); - g_assert (p); - p++; - - g_assert_cmpint (inet_pton (AF_INET, p, &a), ==, 1); - nm_ip4_address_set_gateway (addr, a); + gw = strchr (prefix, ' '); + g_assert (gw); + gw++; + addr = nm_ip_address_new (AF_INET, ip, (guint) atoi (prefix), gw, error); + if (!addr) { + g_ptr_array_unref (addresses); + return FALSE; + } g_ptr_array_add (addresses, addr); } @@ -261,37 +258,36 @@ parse_ip4 (GKeyFile *kf, GVariant **out_props, const char *section, GError **err g_free (tmp); if (g_strv_length (split) > 0) { - routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_route_unref); + routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref); for (iter = split; iter && *iter; iter++) { - NMIP4Route *route; - guint32 a; - char *p; + NMIPRoute *route; + char *dest, *prefix, *next_hop, *metric; if (strlen (g_strstrip (*iter)) == 0) continue; - route = nm_ip4_route_new (); - - p = strchr (*iter, '/'); - g_assert (p); - *p++ = '\0'; - - g_assert_cmpint (inet_pton (AF_INET, *iter, &a), ==, 1); - nm_ip4_route_set_dest (route, a); - nm_ip4_route_set_prefix (route, (guint) atoi (p)); + dest = *iter; - p = strchr (p, ' '); - g_assert (p); - p++; + prefix = strchr (dest, '/'); + g_assert (prefix); + *prefix++ = '\0'; - g_assert_cmpint (inet_pton (AF_INET, p, &a), ==, 1); - nm_ip4_route_set_next_hop (route, a); + next_hop = strchr (prefix, ' '); + g_assert (next_hop); + next_hop++; - p = strchr (p, ' '); - g_assert (p); - p++; - nm_ip4_route_set_metric (route, (guint) atoi (p)); + metric = strchr (next_hop, ' '); + g_assert (metric); + metric++; + route = nm_ip_route_new (AF_INET, + dest, (guint) atoi (prefix), + next_hop, (guint) atoi (metric), + error); + if (!route) { + g_ptr_array_unref (routes); + return FALSE; + } g_ptr_array_add (routes, route); } diff --git a/clients/cli/common.c b/clients/cli/common.c index 0a7541c976..e7382b985b 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -30,6 +30,8 @@ #include #include +#include "nm-glib-compat.h" + #include "common.h" #include "utils.h" @@ -104,17 +106,17 @@ print_ip4_config (NMIP4Config *cfg4, if (ptr_array) { addr_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { - NMIP4Address *addr = (NMIP4Address *) g_ptr_array_index (ptr_array, i); - guint32 prefix; - char *ip_str, *gw_str; + NMIPAddress *addr = (NMIPAddress *) g_ptr_array_index (ptr_array, i); + const char *gw; - ip_str = nmc_ip4_address_as_string (nm_ip4_address_get_address (addr), NULL); - prefix = nm_ip4_address_get_prefix (addr); - gw_str = nmc_ip4_address_as_string (nm_ip4_address_get_gateway (addr), NULL); + gw = nm_ip_address_get_gateway (addr); + if (!gw) + gw = "0.0.0.0"; - addr_arr[i] = g_strdup_printf ("ip = %s/%u, gw = %s", ip_str, prefix, gw_str); - g_free (ip_str); - g_free (gw_str); + addr_arr[i] = g_strdup_printf ("ip = %s/%u, gw = %s", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr), + gw); } addr_arr[i] = NULL; } @@ -124,18 +126,18 @@ print_ip4_config (NMIP4Config *cfg4, if (ptr_array) { route_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { - NMIP4Route *route = (NMIP4Route *) g_ptr_array_index (ptr_array, i); - guint32 prefix, metric; - char *dest_str, *nexthop_str; - - dest_str = nmc_ip4_address_as_string (nm_ip4_route_get_dest (route), NULL); - nexthop_str = nmc_ip4_address_as_string (nm_ip4_route_get_next_hop (route), NULL); - prefix = nm_ip4_route_get_prefix (route); - metric = nm_ip4_route_get_metric (route); - - route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s, mt = %u", dest_str, prefix, nexthop_str, metric); - g_free (dest_str); - g_free (nexthop_str); + NMIPRoute *route = (NMIPRoute *) g_ptr_array_index (ptr_array, i); + const char *next_hop; + + next_hop = nm_ip_route_get_next_hop (route); + if (!next_hop) + next_hop = "0.0.0.0"; + + route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s, mt = %u", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + next_hop, + nm_ip_route_get_metric (route)); } route_arr[i] = NULL; } @@ -196,17 +198,17 @@ print_ip6_config (NMIP6Config *cfg6, if (ptr_array) { addr_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { - NMIP6Address *addr = (NMIP6Address *) g_ptr_array_index (ptr_array, i); - guint32 prefix; - char *ip_str, *gw_str; + NMIPAddress *addr = (NMIPAddress *) g_ptr_array_index (ptr_array, i); + const char *gw; - ip_str = nmc_ip6_address_as_string (nm_ip6_address_get_address (addr), NULL); - prefix = nm_ip6_address_get_prefix (addr); - gw_str = nmc_ip6_address_as_string (nm_ip6_address_get_gateway (addr), NULL); + gw = nm_ip_address_get_gateway (addr); + if (!gw) + gw = "::"; - addr_arr[i] = g_strdup_printf ("ip = %s/%u, gw = %s", ip_str, prefix, gw_str); - g_free (ip_str); - g_free (gw_str); + addr_arr[i] = g_strdup_printf ("ip = %s/%u, gw = %s", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr), + gw); } addr_arr[i] = NULL; } @@ -216,18 +218,18 @@ print_ip6_config (NMIP6Config *cfg6, if (ptr_array) { route_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { - NMIP6Route *route = (NMIP6Route *) g_ptr_array_index (ptr_array, i); - guint32 prefix, metric; - char *dest_str, *nexthop_str; - - dest_str = nmc_ip6_address_as_string (nm_ip6_route_get_dest (route), NULL); - nexthop_str = nmc_ip6_address_as_string (nm_ip6_route_get_next_hop (route), NULL); - prefix = nm_ip6_route_get_prefix (route); - metric = nm_ip6_route_get_metric (route); - - route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s, mt = %u", dest_str, prefix, nexthop_str, metric); - g_free (dest_str); - g_free (nexthop_str); + NMIPRoute *route = (NMIPRoute *) g_ptr_array_index (ptr_array, i); + const char *next_hop; + + next_hop = nm_ip_route_get_next_hop (route); + if (!next_hop) + next_hop = "::"; + + route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s, mt = %u", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + next_hop, + nm_ip_route_get_metric (route)); } route_arr[i] = NULL; } @@ -351,18 +353,20 @@ print_dhcp6_config (NMDhcp6Config *dhcp6, } /* - * Parse IPv4 address from string to NMIP4Address stucture. - * ip_str is the IPv4 address in the form address/prefix + * Parse IP address from string to NMIPAddress stucture. + * ip_str is the IP address in the form address/prefix * gw_str is the gateway address (it is optional) */ -NMIP4Address * -nmc_parse_and_build_ip4_address (const char *ip_str, const char *gw_str, GError **error) +NMIPAddress * +nmc_parse_and_build_address (int family, const char *ip_str, const char *gw_str, GError **error) { - NMIP4Address *addr = NULL; - guint32 ip4_addr, gw_addr; + int max_prefix = (family == AF_INET) ? 32 : 128; + NMIPAddress *addr = NULL; + const char *ip; char *tmp; char *plen; long int prefix; + GError *local = NULL; g_return_val_if_fail (ip_str != NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); @@ -372,158 +376,71 @@ nmc_parse_and_build_ip4_address (const char *ip_str, const char *gw_str, GError if (plen) *plen++ = '\0'; - if (inet_pton (AF_INET, tmp, &ip4_addr) < 1) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid IPv4 address '%s'"), tmp); - goto finish; - } + ip = tmp; - prefix = 32; + prefix = max_prefix; if (plen) { - if (!nmc_string_to_int (plen, TRUE, 1, 32, &prefix)) { + if (!nmc_string_to_int (plen, TRUE, 1, max_prefix, &prefix)) { g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid prefix '%s'; <1-32> allowed"), plen); + _("invalid prefix '%s'; <1-%d> allowed"), plen, max_prefix); goto finish; } } - if (inet_pton (AF_INET, gw_str ? gw_str : "0.0.0.0", &gw_addr) < 1) { + addr = nm_ip_address_new (family, ip, (guint32) prefix, gw_str, &local); + if (!addr) { g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid gateway '%s'"), gw_str); - goto finish; + _("invalid IP address: %s"), local->message); + g_clear_error (&local); } - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip4_addr); - nm_ip4_address_set_prefix (addr, (guint32) prefix); - nm_ip4_address_set_gateway (addr, gw_addr); - finish: g_free (tmp); return addr; } /* - * Parse IPv6 address from string to NMIP6Address stucture. - * ip_str is the IPv6 address in the form address/prefix - * gw_str is the gateway address (it is optional) - */ -NMIP6Address * -nmc_parse_and_build_ip6_address (const char *ip_str, const char *gw_str, GError **error) -{ - NMIP6Address *addr = NULL; - struct in6_addr ip_addr, gw_addr; - char *tmp; - char *plen; - long int prefix; - - g_return_val_if_fail (ip_str != NULL, NULL); - g_return_val_if_fail (error == NULL || *error == NULL, NULL); - - tmp = g_strdup (ip_str); - plen = strchr (tmp, '/'); /* prefix delimiter */ - if (plen) - *plen++ = '\0'; - - if (inet_pton (AF_INET6, tmp, &ip_addr) < 1) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid IPv6 address '%s'"), tmp); - goto finish; - } - - prefix = 128; - if (plen) { - if (!nmc_string_to_int (plen, TRUE, 1, 128, &prefix)) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid prefix '%s'; <1-128> allowed"), plen); - goto finish; - } - } - - if (inet_pton (AF_INET6, gw_str ? gw_str : "::", &gw_addr) < 1) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid gateway '%s'"), gw_str); - goto finish; - } - - addr = nm_ip6_address_new (); - nm_ip6_address_set_address (addr, &ip_addr); - nm_ip6_address_set_prefix (addr, (guint32) prefix); - nm_ip6_address_set_gateway (addr, &gw_addr); - -finish: - g_free (tmp); - return addr; -} - -typedef struct { - long int prefix; - long int metric; - union _IpDest { - guint32 ip4_dst; - struct in6_addr ip6_dst; - } dst; - union _IpNextHop { - guint32 ip4_nh; - struct in6_addr ip6_nh; - } nh; -} ParsedRoute; - -/* - * _parse_and_build_route: + * nmc_parse_and_build_route: * @family: AF_INET or AF_INET6 * @first: the route destination in the form of "address/prefix" (/prefix is optional) * @second: (allow-none): next hop address, if third is not NULL. Otherwise it could be either next hop address or metric. (It can be NULL when @third is NULL). * @third: (allow-none): route metric - * @out: (out): route struct to fill * @error: location to store GError * - * Parse route from strings and fill @out parameter. + * Parse route from strings and return an #NMIPRoute * * Returns: %TRUE on success, %FALSE on failure */ -static gboolean -_parse_and_build_route (int family, - const char *first, - const char *second, - const char *third, - ParsedRoute *out, - GError **error) +NMIPRoute * +nmc_parse_and_build_route (int family, + const char *first, + const char *second, + const char *third, + GError **error) { - int max_prefix; - char *tmp, *plen; + int max_prefix = (family == AF_INET) ? 32 : 128; + char *dest = NULL, *plen = NULL; + const char *next_hop = NULL; + const char *canon_dest; + long int prefix = max_prefix, metric = 0; + NMIPRoute *route = NULL; gboolean success = FALSE; + GError *local = NULL; g_return_val_if_fail (family == AF_INET || family == AF_INET6, FALSE); g_return_val_if_fail (first != NULL, FALSE); g_return_val_if_fail (second || !third, FALSE); - g_return_val_if_fail (out, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - max_prefix = (family == AF_INET) ? 32 : 128; - /* initialize default values */ - out->prefix = max_prefix; - out->metric = 0; - if (family == AF_INET) - out->nh.ip4_nh = 0; - else - out->nh.ip6_nh = in6addr_any; - - tmp = g_strdup (first); - plen = strchr (tmp, '/'); /* prefix delimiter */ + dest = g_strdup (first); + plen = strchr (dest, '/'); /* prefix delimiter */ if (plen) *plen++ = '\0'; - if (inet_pton (family, tmp, family == AF_INET ? (void *) &out->dst.ip4_dst : (void *) &out->dst.ip6_dst) < 1) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid route destination address '%s'"), tmp); - goto finish; - } - if (plen) { - if (!nmc_string_to_int (plen, TRUE, 1, max_prefix, &out->prefix)) { + if (!nmc_string_to_int (plen, TRUE, 1, max_prefix, &prefix)) { g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, _("invalid prefix '%s'; <1-%d> allowed"), plen, max_prefix); @@ -532,113 +449,49 @@ _parse_and_build_route (int family, } if (second) { - if (inet_pton (family, second, family == AF_INET ? (void *) &out->nh.ip4_nh : (void *) &out->nh.ip6_nh) < 1) { - if (third) { - g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, - _("invalid next hop address '%s'"), second); + if (third || nm_utils_ipaddr_valid (family, second)) + next_hop = second; + else { + /* 'second' can be a metric */ + if (!nmc_string_to_int (second, TRUE, 0, G_MAXUINT32, &metric)) { + g_set_error (error, 1, 0, _("the second component of route ('%s') is neither " + "a next hop address nor a metric"), second); goto finish; - } else { - /* 'second' can be a metric */ - if (!nmc_string_to_int (second, TRUE, 0, G_MAXUINT32, &out->metric)) { - g_set_error (error, 1, 0, _("the second component of route ('%s') is neither " - "a next hop address nor a metric"), second); - goto finish; - } } } } if (third) { - if (!nmc_string_to_int (third, TRUE, 0, G_MAXUINT32, &out->metric)) { + if (!nmc_string_to_int (third, TRUE, 0, G_MAXUINT32, &metric)) { g_set_error (error, 1, 0, _("invalid metric '%s'"), third); goto finish; } } - /* We don't accept default routes as NetworkManager handles it itself */ - if ( (family == AF_INET && out->dst.ip4_dst == 0) - || (family == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED (&out->dst.ip6_dst))) { + route = nm_ip_route_new (family, dest, prefix, next_hop, metric, &local); + if (!route) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("invalid route: %s"), local->message); + g_clear_error (&local); + goto finish; + } + + /* We don't accept default routes as NetworkManager handles it + * itself. But we have to check this after @route has normalized the + * dest string. + */ + canon_dest = nm_ip_route_get_dest (route); + if (!strcmp (canon_dest, "0.0.0.0") || !strcmp (canon_dest, "::")) { g_set_error_literal (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, _("default route cannot be added (NetworkManager handles it by itself)")); + g_clear_pointer (&route, nm_ip_route_unref); goto finish; } success = TRUE; finish: - g_free (tmp); - return success; -} - -/* - * nmc_parse_and_build_ip4_route: - * @first: the IPv4 route destination in the form of "address/prefix" - (/prefix is optional) - * @second: (allow-none): next hop address, if third is not NULL. Otherwise it could be - either next hop address or metric. (It can be NULL when @third is NULL). - * @third: (allow-none): route metric - * @error: location to store GError - * - * Parse IPv4 route from strings to NMIP4Route stucture. - * - * Returns: route as a NMIP4Route object, or %NULL on failure - */ -NMIP4Route * -nmc_parse_and_build_ip4_route (const char *first, - const char *second, - const char *third, - GError **error) -{ - ParsedRoute tmp_route; - NMIP4Route *route = NULL; - - g_return_val_if_fail (first != NULL, NULL); - g_return_val_if_fail (second || !third, NULL); - g_return_val_if_fail (error == NULL || *error == NULL, NULL); - - if (_parse_and_build_route (AF_INET, first, second, third, &tmp_route, error)) { - route = nm_ip4_route_new (); - nm_ip4_route_set_dest (route, tmp_route.dst.ip4_dst); - nm_ip4_route_set_prefix (route, (guint32) tmp_route.prefix); - nm_ip4_route_set_next_hop (route, tmp_route.nh.ip4_nh); - nm_ip4_route_set_metric (route, (guint32) tmp_route.metric); - } - return route; -} - -/* - * nmc_parse_and_build_ip6_route: - * @first: the IPv6 route destination in the form of "address/prefix" - (/prefix is optional) - * @second: (allow-none): next hop address, if third is not NULL. Otherwise it could be - either next hop address or metric. (It can be NULL when @third is NULL). - * @third: (allow-none): route metric - * @error: location to store GError - * - * Parse IPv6 route from strings to NMIP6Route stucture. - * - * Returns: route as a NMIP6Route object, or %NULL on failure - */ -NMIP6Route * -nmc_parse_and_build_ip6_route (const char *first, - const char *second, - const char *third, - GError **error) -{ - ParsedRoute tmp_route; - NMIP6Route *route = NULL; - - g_return_val_if_fail (first != NULL, NULL); - g_return_val_if_fail (second || !third, NULL); - g_return_val_if_fail (error == NULL || *error == NULL, NULL); - - if (_parse_and_build_route (AF_INET6, first, second, third, &tmp_route, error)) { - route = nm_ip6_route_new (); - nm_ip6_route_set_dest (route, &tmp_route.dst.ip6_dst); - nm_ip6_route_set_prefix (route, (guint32) tmp_route.prefix); - nm_ip6_route_set_next_hop (route, &tmp_route.nh.ip6_nh); - nm_ip6_route_set_metric (route, (guint32) tmp_route.metric); - } + g_free (dest); return route; } diff --git a/clients/cli/common.h b/clients/cli/common.h index 4f43674793..d608ef5ce0 100644 --- a/clients/cli/common.h +++ b/clients/cli/common.h @@ -29,11 +29,8 @@ gboolean print_ip6_config (NMIP6Config *cfg6, NmCli *nmc, const char *group_pref gboolean print_dhcp4_config (NMDhcp4Config *dhcp4, NmCli *nmc, const char *group_prefix, const char *one_field); gboolean print_dhcp6_config (NMDhcp6Config *dhcp6, NmCli *nmc, const char *group_prefix, const char *one_field); -NMIP4Address *nmc_parse_and_build_ip4_address (const char *ip_str, const char *gw_str, GError **error); -NMIP6Address *nmc_parse_and_build_ip6_address (const char *ip_str, const char *gw_str, GError **error); - -NMIP4Route *nmc_parse_and_build_ip4_route (const char *first, const char *second, const char *third, GError **error); -NMIP6Route *nmc_parse_and_build_ip6_route (const char *first, const char *second, const char *third, GError **error); +NMIPAddress *nmc_parse_and_build_address (int family, const char *ip_str, const char *gw_str, GError **error); +NMIPRoute *nmc_parse_and_build_route (int family, const char *first, const char *second, const char *third, GError **error); const char * nmc_device_state_to_string (NMDeviceState state); const char * nmc_device_reason_to_string (NMDeviceStateReason reason); diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 554e2107e6..d92b913cf8 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -2887,7 +2887,7 @@ check_and_convert_vlan_prio_maps (const char *prio_map, } static gboolean -add_ip4_address_to_connection (NMIP4Address *ip4addr, NMConnection *connection) +add_ip4_address_to_connection (NMIPAddress *ip4addr, NMConnection *connection) { NMSettingIP4Config *s_ip4; gboolean ret; @@ -2904,13 +2904,13 @@ add_ip4_address_to_connection (NMIP4Address *ip4addr, NMConnection *connection) NULL); } ret = nm_setting_ip4_config_add_address (s_ip4, ip4addr); - nm_ip4_address_unref (ip4addr); + nm_ip_address_unref (ip4addr); return ret; } static gboolean -add_ip6_address_to_connection (NMIP6Address *ip6addr, NMConnection *connection) +add_ip6_address_to_connection (NMIPAddress *ip6addr, NMConnection *connection) { NMSettingIP6Config *s_ip6; gboolean ret; @@ -2927,7 +2927,7 @@ add_ip6_address_to_connection (NMIP6Address *ip6addr, NMConnection *connection) NULL); } ret = nm_setting_ip6_config_add_address (s_ip6, ip6addr); - nm_ip6_address_unref (ip6addr); + nm_ip_address_unref (ip6addr); return ret; } @@ -3841,9 +3841,9 @@ ask_for_ip_addresses (NMConnection *connection, int family) char *str, *ip, *gw, *rest; const char *prompt; gboolean added; - gpointer ipaddr; + NMIPAddress *ipaddr; - if (family == 4) + if (family == AF_INET) prompt =_("IPv4 address (IP[/plen] [gateway]) [none]: "); else prompt =_("IPv6 address (IP[/plen] [gateway]) [none]: "); @@ -3853,16 +3853,13 @@ ask_for_ip_addresses (NMConnection *connection, int family) str = nmc_readline ("%s", prompt); split_address (str, &ip, &gw, &rest); if (ip) { - if (family == 4) - ipaddr = nmc_parse_and_build_ip4_address (ip, gw, &error); - else - ipaddr = nmc_parse_and_build_ip6_address (ip, gw, &error); + ipaddr = nmc_parse_and_build_address (family, ip, gw, &error); if (ipaddr) { - if (family == 4) - added = add_ip4_address_to_connection ((NMIP4Address *) ipaddr, connection); + if (family == AF_INET) + added = add_ip4_address_to_connection (ipaddr, connection); else - added = add_ip6_address_to_connection ((NMIP6Address *) ipaddr, connection); - gw = gw ? gw : (family == 4) ? "0.0.0.0" : "::"; + added = add_ip6_address_to_connection (ipaddr, connection); + gw = gw ? gw : (family == AF_INET) ? "0.0.0.0" : "::"; if (added) g_print (_(" Address successfully added: %s %s\n"), ip, gw); else @@ -3896,8 +3893,8 @@ do_questionnaire_ip (NMConnection *connection) g_print (_("Press to finish adding addresses.\n")); - ask_for_ip_addresses (connection, 4); - ask_for_ip_addresses (connection, 6); + ask_for_ip_addresses (connection, AF_INET); + ask_for_ip_addresses (connection, AF_INET6); g_free (answer); return; @@ -5151,8 +5148,7 @@ cleanup_olpc: && strcmp (con_type, "team-slave") != 0 && strcmp (con_type, "bridge-slave") != 0) { - NMIP4Address *ip4addr = NULL; - NMIP6Address *ip6addr = NULL; + NMIPAddress *ip4addr = NULL, *ip6addr = NULL; const char *ip4 = NULL, *gw4 = NULL, *ip6 = NULL, *gw6 = NULL; nmc_arg_t exp_args[] = { {"ip4", TRUE, &ip4, FALSE}, {"gw4", TRUE, &gw4, FALSE}, {"ip6", TRUE, &ip6, FALSE}, {"gw6", TRUE, &gw6, FALSE}, @@ -5172,7 +5168,7 @@ cleanup_olpc: /* coverity[dead_error_begin] */ if (ip4) { - ip4addr = nmc_parse_and_build_ip4_address (ip4, gw4, error); + ip4addr = nmc_parse_and_build_address (AF_INET, ip4, gw4, error); if (!ip4addr) { g_prefix_error (error, _("Error: ")); return FALSE; @@ -5182,7 +5178,7 @@ cleanup_olpc: /* coverity[dead_error_begin] */ if (ip6) { - ip6addr = nmc_parse_and_build_ip6_address (ip6, gw6, error); + ip6addr = nmc_parse_and_build_address (AF_INET6, ip6, gw6, error); if (!ip6addr) { g_prefix_error (error, _("Error: ")); return FALSE; diff --git a/clients/cli/settings.c b/clients/cli/settings.c index 136c818641..19dc06d702 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -1209,8 +1209,7 @@ nmc_property_ipv4_get_addresses (NMSetting *setting) NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting); GString *printable; guint32 num_addresses, i; - NMIP4Address *addr; - char buf[INET_ADDRSTRLEN]; + NMIPAddress *addr; printable = g_string_new (NULL); @@ -1223,14 +1222,13 @@ nmc_property_ipv4_get_addresses (NMSetting *setting) g_string_append (printable, "{ "); - nm_utils_inet4_ntop (nm_ip4_address_get_address (addr), buf); - g_string_append_printf (printable, "ip = %s", buf); + g_string_append_printf (printable, "ip = %s/%u", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr)); - g_string_append_printf (printable, "/%u", nm_ip4_address_get_prefix (addr)); - - if (nm_ip4_address_get_gateway (addr)) { - nm_utils_inet4_ntop (nm_ip4_address_get_gateway (addr), buf); - g_string_append_printf (printable, ", gw = %s", buf); + if (nm_ip_address_get_gateway (addr)) { + g_string_append_printf (printable, ", gw = %s", + nm_ip_address_get_gateway (addr)); } g_string_append (printable, " }"); @@ -1245,8 +1243,7 @@ nmc_property_ipv4_get_routes (NMSetting *setting) NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting); GString *printable; guint32 num_routes, i; - NMIP4Route *route; - char buf[INET_ADDRSTRLEN]; + NMIPRoute *route; printable = g_string_new (NULL); @@ -1259,18 +1256,17 @@ nmc_property_ipv4_get_routes (NMSetting *setting) g_string_append (printable, "{ "); - nm_utils_inet4_ntop (nm_ip4_route_get_dest (route), buf); - g_string_append_printf (printable, "ip = %s", buf); - - g_string_append_printf (printable, "/%u", nm_ip4_route_get_prefix (route)); + g_string_append_printf (printable, "ip = %s/%u", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route)); - if (nm_ip4_route_get_next_hop (route)) { - nm_utils_inet4_ntop (nm_ip4_route_get_next_hop (route), buf); - g_string_append_printf (printable, ", nh = %s", buf); + if (nm_ip_route_get_next_hop (route)) { + g_string_append_printf (printable, ", nh = %s", + nm_ip_route_get_next_hop (route)); } - if (nm_ip4_route_get_metric (route)) - g_string_append_printf (printable, ", mt = %u", nm_ip4_route_get_metric (route)); + if (nm_ip_route_get_metric (route)) + g_string_append_printf (printable, ", mt = %u", nm_ip_route_get_metric (route)); g_string_append (printable, " }"); } @@ -1297,8 +1293,7 @@ nmc_property_ipv6_get_addresses (NMSetting *setting) NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting); GString *printable; guint32 num_addresses, i; - NMIP6Address *addr; - char buf[INET6_ADDRSTRLEN]; + NMIPAddress *addr; printable = g_string_new (NULL); @@ -1311,14 +1306,13 @@ nmc_property_ipv6_get_addresses (NMSetting *setting) g_string_append (printable, "{ "); - nm_utils_inet6_ntop (nm_ip6_address_get_address (addr), buf); - g_string_append_printf (printable, "ip = %s", buf); - - g_string_append_printf (printable, "/%u", nm_ip6_address_get_prefix (addr)); + g_string_append_printf (printable, "ip = %s/%u", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr)); - if (nm_ip6_address_get_gateway (addr)) { - nm_utils_inet6_ntop (nm_ip6_address_get_gateway (addr), buf); - g_string_append_printf (printable, ", gw = %s", buf); + if (nm_ip_address_get_gateway (addr)) { + g_string_append_printf (printable, ", gw = %s", + nm_ip_address_get_gateway (addr)); } g_string_append (printable, " }"); @@ -1333,8 +1327,7 @@ nmc_property_ipv6_get_routes (NMSetting *setting) NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting); GString *printable; guint32 num_routes, i; - NMIP6Route *route; - char buf[INET6_ADDRSTRLEN]; + NMIPRoute *route; printable = g_string_new (NULL); @@ -1347,18 +1340,17 @@ nmc_property_ipv6_get_routes (NMSetting *setting) g_string_append (printable, "{ "); - nm_utils_inet6_ntop (nm_ip6_route_get_dest (route), buf); - g_string_append_printf (printable, "ip = %s", buf); + g_string_append_printf (printable, "ip = %s/%u", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route)); - g_string_append_printf (printable, "/%u", nm_ip6_route_get_prefix (route)); - - if (nm_ip6_route_get_next_hop (route)) { - nm_utils_inet6_ntop (nm_ip6_route_get_next_hop (route), buf); - g_string_append_printf (printable, ", nh = %s", buf); + if (nm_ip_route_get_next_hop (route)) { + g_string_append_printf (printable, ", nh = %s", + nm_ip_route_get_next_hop (route)); } - if (nm_ip6_route_get_metric (route)) - g_string_append_printf (printable, ", mt = %u", nm_ip6_route_get_metric (route)); + if (nm_ip_route_get_metric (route)) + g_string_append_printf (printable, ", mt = %u", nm_ip_route_get_metric (route)); g_string_append (printable, " }"); } @@ -3074,6 +3066,51 @@ nmc_property_ib_set_p_key (NMSetting *setting, const char *prop, const char *val return TRUE; } +/* --- IP4 / IP6 shared functions --- */ +static NMIPAddress * +_parse_ip_address (int family, const char *address, GError **error) +{ + char *value = g_strdup (address); + char **addrv; + NMIPAddress *ipaddr; + + addrv = nmc_strsplit_set (g_strstrip (value), " \t", 0); + if (addrv[0] == NULL || g_strv_length (addrv) > 2) { + g_set_error (error, 1, 0, _("'%s' is not valid (use ip[/prefix] [gateway])"), + address); + g_free (value); + g_strfreev (addrv); + return NULL; + } + ipaddr = nmc_parse_and_build_address (family, addrv[0], addrv[1], error); + g_free (value); + g_strfreev (addrv); + return ipaddr; +} + +static NMIPRoute * +_parse_ip_route (int family, const char *route, GError **error) +{ + char *value = g_strdup (route); + char **routev; + guint len; + NMIPRoute *iproute = NULL; + + routev = nmc_strsplit_set (g_strstrip (value), " \t", 0); + len = g_strv_length (routev); + if (len < 1 || len > 3) { + g_set_error (error, 1, 0, _("'%s' is not valid (the format is: ip[/prefix] [next-hop] [metric])"), + route); + goto finish; + } + iproute = nmc_parse_and_build_route (family, routev[0], routev[1], len >= 2 ? routev[2] : NULL, error); + +finish: + g_free (value); + g_strfreev (routev); + return iproute; +} + /* --- NM_SETTING_IP4_CONFIG_SETTING_NAME property setter functions --- */ /* 'method' */ static const char *ipv4_valid_methods[] = { @@ -3194,32 +3231,17 @@ DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_dns_search, _validate_and_remove_ipv4_dns_search) /* 'addresses' */ -static NMIP4Address * +static NMIPAddress * _parse_ipv4_address (const char *address, GError **error) { - char *value = g_strdup (address); - char **addrv; - NMIP4Address *ip4addr; - - addrv = nmc_strsplit_set (g_strstrip (value), " \t", 0); - if (addrv[0] == NULL || g_strv_length (addrv) > 2) { - g_set_error (error, 1, 0, _("'%s' is not valid (use ip[/prefix] [gateway])"), - address); - g_free (value); - g_strfreev (addrv); - return NULL; - } - ip4addr = nmc_parse_and_build_ip4_address (addrv[0], addrv[1], error); - g_free (value); - g_strfreev (addrv); - return ip4addr; + return _parse_ip_address (AF_INET, address, error); } static gboolean nmc_property_ipv4_set_addresses (NMSetting *setting, const char *prop, const char *val, GError **error) { char **strv = NULL, **iter; - NMIP4Address *ip4addr; + NMIPAddress *ip4addr; g_return_val_if_fail (error == NULL || *error == NULL, FALSE); @@ -3231,7 +3253,7 @@ nmc_property_ipv4_set_addresses (NMSetting *setting, const char *prop, const cha return FALSE; } nm_setting_ip4_config_add_address (NM_SETTING_IP4_CONFIG (setting), ip4addr); - nm_ip4_address_unref (ip4addr); + nm_ip_address_unref (ip4addr); } g_strfreev (strv); return TRUE; @@ -3242,7 +3264,7 @@ _validate_and_remove_ipv4_address (NMSettingIP4Config *setting, const char *address, GError **error) { - NMIP4Address *ip4addr; + NMIPAddress *ip4addr; gboolean ret; ip4addr = _parse_ipv4_address (address, error); @@ -3253,7 +3275,7 @@ _validate_and_remove_ipv4_address (NMSettingIP4Config *setting, if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain IP address '%s'"), address); - nm_ip4_address_unref (ip4addr); + nm_ip_address_unref (ip4addr); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_addresses, @@ -3307,34 +3329,17 @@ nmc_property_out2in_addresses (const char *out_format) } /* 'routes' */ -static NMIP4Route * +static NMIPRoute * _parse_ipv4_route (const char *route, GError **error) { - char *value = g_strdup (route); - char **routev; - guint len; - NMIP4Route *ip4route = NULL; - - routev = nmc_strsplit_set (g_strstrip (value), " \t", 0); - len = g_strv_length (routev); - if (len < 1 || len > 3) { - g_set_error (error, 1, 0, _("'%s' is not valid (the format is: ip[/prefix] [next-hop] [metric])"), - route); - goto finish; - } - ip4route = nmc_parse_and_build_ip4_route (routev[0], routev[1], len >= 2 ? routev[2] : NULL, error); - -finish: - g_free (value); - g_strfreev (routev); - return ip4route; + return _parse_ip_route (AF_INET, route, error); } static gboolean nmc_property_ipv4_set_routes (NMSetting *setting, const char *prop, const char *val, GError **error) { char **strv = NULL, **iter; - NMIP4Route *ip4route; + NMIPRoute *ip4route; g_return_val_if_fail (error == NULL || *error == NULL, FALSE); @@ -3346,7 +3351,7 @@ nmc_property_ipv4_set_routes (NMSetting *setting, const char *prop, const char * return FALSE; } nm_setting_ip4_config_add_route (NM_SETTING_IP4_CONFIG (setting), ip4route); - nm_ip4_route_unref (ip4route); + nm_ip_route_unref (ip4route); } g_strfreev (strv); return TRUE; @@ -3357,7 +3362,7 @@ _validate_and_remove_ipv4_route (NMSettingIP4Config *setting, const char *route, GError **error) { - NMIP4Route *ip4route; + NMIPRoute *ip4route; gboolean ret; ip4route = _parse_ipv4_route (route, error); @@ -3367,7 +3372,7 @@ _validate_and_remove_ipv4_route (NMSettingIP4Config *setting, ret = nm_setting_ip4_config_remove_route_by_value (setting, ip4route); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain route '%s'"), route); - nm_ip4_route_unref (ip4route); + nm_ip_route_unref (ip4route); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_routes, @@ -3546,32 +3551,17 @@ DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_dns_search, _validate_and_remove_ipv6_dns_search) /* 'addresses' */ -static NMIP6Address * +static NMIPAddress * _parse_ipv6_address (const char *address, GError **error) { - char *value = g_strstrip (g_strdup (address)); - char **addrv; - NMIP6Address *ip6addr; - - addrv = nmc_strsplit_set (g_strstrip (value), " \t", 0); - if (addrv[0] == NULL || g_strv_length (addrv) > 2) { - g_set_error (error, 1, 0, _("'%s' is not valid (use ip[/prefix] [gateway])"), - address); - g_free (value); - g_strfreev (addrv); - return NULL; - } - ip6addr = nmc_parse_and_build_ip6_address (addrv[0], addrv[1], error); - g_free (value); - g_strfreev (addrv); - return ip6addr; + return _parse_ip_address (AF_INET6, address, error); } static gboolean nmc_property_ipv6_set_addresses (NMSetting *setting, const char *prop, const char *val, GError **error) { char **strv = NULL, **iter; - NMIP6Address *ip6addr; + NMIPAddress *ip6addr; g_return_val_if_fail (error == NULL || *error == NULL, FALSE); @@ -3583,7 +3573,7 @@ nmc_property_ipv6_set_addresses (NMSetting *setting, const char *prop, const cha return FALSE; } nm_setting_ip6_config_add_address (NM_SETTING_IP6_CONFIG (setting), ip6addr); - nm_ip6_address_unref (ip6addr); + nm_ip_address_unref (ip6addr); } g_strfreev (strv); return TRUE; @@ -3594,7 +3584,7 @@ _validate_and_remove_ipv6_address (NMSettingIP6Config *setting, const char *address, GError **error) { - NMIP6Address *ip6addr; + NMIPAddress *ip6addr; gboolean ret; ip6addr = _parse_ipv6_address (address, error); @@ -3604,7 +3594,7 @@ _validate_and_remove_ipv6_address (NMSettingIP6Config *setting, ret = nm_setting_ip6_config_remove_address_by_value (setting, ip6addr); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain IP address '%s'"), address); - nm_ip6_address_unref (ip6addr); + nm_ip_address_unref (ip6addr); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_addresses, @@ -3623,34 +3613,17 @@ nmc_property_ipv6_describe_addresses (NMSetting *setting, const char *prop) } /* 'routes' */ -static NMIP6Route * +static NMIPRoute * _parse_ipv6_route (const char *route, GError **error) { - char *value = g_strdup (route); - char **routev; - guint len; - NMIP6Route *ip6route = NULL; - - routev = nmc_strsplit_set (g_strstrip (value), " \t", 0); - len = g_strv_length (routev); - if (len < 1 || len > 3) { - g_set_error (error, 1, 0, _("'%s' is not valid (the format is: ip[/prefix] [next-hop] [metric])"), - route); - goto finish; - } - ip6route = nmc_parse_and_build_ip6_route (routev[0], routev[1], len >= 2 ? routev[2] : NULL, error); - -finish: - g_free (value); - g_strfreev (routev); - return ip6route; + return _parse_ip_route (AF_INET6, route, error); } static gboolean nmc_property_ipv6_set_routes (NMSetting *setting, const char *prop, const char *val, GError **error) { char **strv = NULL, **iter; - NMIP6Route *ip6route; + NMIPRoute *ip6route; g_return_val_if_fail (error == NULL || *error == NULL, FALSE); @@ -3662,7 +3635,7 @@ nmc_property_ipv6_set_routes (NMSetting *setting, const char *prop, const char * return FALSE; } nm_setting_ip6_config_add_route (NM_SETTING_IP6_CONFIG (setting), ip6route); - nm_ip6_route_unref (ip6route); + nm_ip_route_unref (ip6route); } g_strfreev (strv); return TRUE; @@ -3673,7 +3646,7 @@ _validate_and_remove_ipv6_route (NMSettingIP6Config *setting, const char *route, GError **error) { - NMIP6Route *ip6route; + NMIPRoute *ip6route; gboolean ret; ip6route = _parse_ipv6_route (route, error); @@ -3683,7 +3656,7 @@ _validate_and_remove_ipv6_route (NMSettingIP6Config *setting, ret = nm_setting_ip6_config_remove_route_by_value (setting, ip6route); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain route '%s'"), route); - nm_ip6_route_unref (ip6route); + nm_ip_route_unref (ip6route); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_routes, diff --git a/clients/tui/nm-editor-bindings.c b/clients/tui/nm-editor-bindings.c index e3b210dec6..4c42caad35 100644 --- a/clients/tui/nm-editor-bindings.c +++ b/clients/tui/nm-editor-bindings.c @@ -72,10 +72,10 @@ nm_editor_bindings_init (void) } static gboolean -ip_string_parse (const char *text, - int family, - gpointer addr, - guint32 *prefix) +parse_addr_prefix (const char *text, + int family, + char **addr, + guint32 *prefix) { const char *slash; char *addrstr, *end; @@ -83,17 +83,11 @@ ip_string_parse (const char *text, slash = strchr (text, '/'); - if (slash) { - if (!prefix) - return FALSE; + if (slash) addrstr = g_strndup (text, slash - text); - } else + else addrstr = g_strdup (text); - valid = (inet_pton (family, addrstr, addr) == 1); - g_free (addrstr); - - if (!valid) - return FALSE; + valid = nm_utils_ipaddr_valid (family, addrstr); if (slash) { *prefix = strtoul (slash + 1, &end, 10); @@ -109,19 +103,24 @@ ip_string_parse (const char *text, *prefix = 128; } + if (addr && valid) + *addr = addrstr; + else + g_free (addrstr); return valid; } static gboolean -ip4_addresses_with_prefix_to_strv (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_addresses_with_prefix_to_strv (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { GPtrArray *addrs; - NMIP4Address *addr; - guint32 addrbytes, prefix; - char buf[INET_ADDRSTRLEN], **strings; + NMIPAddress *addr; + const char *addrstr; + guint32 prefix; + char **strings; int i; addrs = g_value_get_boxed (source_value); @@ -129,14 +128,12 @@ ip4_addresses_with_prefix_to_strv (GBinding *binding, for (i = 0; i < addrs->len; i++) { addr = addrs->pdata[i]; - addrbytes = nm_ip4_address_get_address (addr); - prefix = nm_ip4_address_get_prefix (addr); + addrstr = nm_ip_address_get_address (addr); + prefix = nm_ip_address_get_prefix (addr); - if (addrbytes) { - strings[i] = g_strdup_printf ("%s/%d", - inet_ntop (AF_INET, &addrbytes, buf, sizeof (buf)), - (int) prefix); - } else + if (addrstr) + strings[i] = g_strdup_printf ("%s/%d", addrstr, (int) prefix); + else strings[i] = g_strdup (""); } @@ -145,15 +142,17 @@ ip4_addresses_with_prefix_to_strv (GBinding *binding, } static gboolean -ip4_addresses_with_prefix_from_strv (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_addresses_with_prefix_from_strv (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { + int family = GPOINTER_TO_INT (user_data); char **strings; GPtrArray *addrs; - NMIP4Address *addr; - guint32 addrbytes, prefix; + NMIPAddress *addr; + char *addrstr; + guint32 prefix; int i; strings = g_value_get_boxed (source_value); @@ -164,19 +163,22 @@ ip4_addresses_with_prefix_from_strv (GBinding *binding, for (i = 0; strings[i]; i++) { if (i >= addrs->len) { - addr = nm_ip4_address_new (); - nm_ip4_address_set_prefix (addr, 32); + if (family == AF_INET) + addr = nm_ip_address_new (AF_INET, "0.0.0.0", 32, NULL, NULL); + else + addr = nm_ip_address_new (AF_INET6, "::", 128, NULL, NULL); g_ptr_array_add (addrs, addr); } else addr = addrs->pdata[i]; - if (!ip_string_parse (strings[i], AF_INET, &addrbytes, &prefix)) { + if (!parse_addr_prefix (strings[i], family, &addrstr, &prefix)) { g_ptr_array_unref (addrs); return FALSE; } - nm_ip4_address_set_address (addr, addrbytes); - nm_ip4_address_set_prefix (addr, prefix); + nm_ip_address_set_address (addr, addrstr); + nm_ip_address_set_prefix (addr, prefix); + g_free (addrstr); } g_ptr_array_set_size (addrs, i); @@ -185,7 +187,8 @@ ip4_addresses_with_prefix_from_strv (GBinding *binding, } /** - * nm_editor_bind_ip4_addresses_with_prefix_to_strv: + * nm_editor_bind_ip_addresses_with_prefix_to_strv: + * @family: the IP address family * @source: the source object (eg, an #NMSettingIP4Config) * @source_property: the property on @source to bind (eg, * %NM_SETTING_IP4_CONFIG_ADDRESSES) @@ -194,44 +197,45 @@ ip4_addresses_with_prefix_from_strv (GBinding *binding, * (eg, "strings") * @flags: %GBindingFlags * - * Binds the #GPtrArray-of-#NMIP4Address property @source_property on @source to + * Binds the #GPtrArray-of-#NMIPAddress property @source_property on @source to * the %G_TYPE_STRV property @target_property on @target. * - * Each #NMIP4Address in @source_property will be converted to a string of the - * form "ip.ad.dr.ess/prefix" in @target_property (and vice versa if - * %G_BINDING_BIDIRECTIONAL) is specified. The "gateway" fields in + * Each #NMIPAddress in @source_property will be converted to a string of the + * form "ip.ad.dr.ess/prefix" or "ip:ad:dr:ess/prefix" in @target_property (and + * vice versa if %G_BINDING_BIDIRECTIONAL) is specified. The "gateway" fields in * @source_property are ignored when converting to strings, and unmodified when * converting from strings. */ void -nm_editor_bind_ip4_addresses_with_prefix_to_strv (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) +nm_editor_bind_ip_addresses_with_prefix_to_strv (int family, + gpointer source, + const gchar *source_property, + gpointer target, + const gchar *target_property, + GBindingFlags flags) { g_object_bind_property_full (source, source_property, target, target_property, flags, - ip4_addresses_with_prefix_to_strv, - ip4_addresses_with_prefix_from_strv, - NULL, NULL); + ip_addresses_with_prefix_to_strv, + ip_addresses_with_prefix_from_strv, + GINT_TO_POINTER (family), NULL); } static gboolean -ip4_addresses_check_and_copy (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_addresses_check_and_copy (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { + int family = GPOINTER_TO_INT (user_data); char **strings; - guint32 addr; int i; strings = g_value_get_boxed (source_value); for (i = 0; strings[i]; i++) { - if (!ip_string_parse (strings[i], AF_INET, &addr, NULL)) + if (!nm_utils_ipaddr_valid (family, strings[i])) return FALSE; } @@ -240,7 +244,8 @@ ip4_addresses_check_and_copy (GBinding *binding, } /** - * nm_editor_bind_ip4_addresses_to_strv: + * nm_editor_bind_ip_addresses_to_strv: + * @family: the IP address family * @source: the source object (eg, an #NMSettingIP4Config) * @source_property: the property on @source to bind (eg, * %NM_SETTING_IP4_CONFIG_DNS) @@ -251,67 +256,67 @@ ip4_addresses_check_and_copy (GBinding *binding, * * Binds the %G_TYPE_STRV property @source_property on @source to the * %G_TYPE_STRV property @target_property on @target, verifying that - * each string is a valid IPv4 address when copying. + * each string is a valid address of type @family when copying. */ void -nm_editor_bind_ip4_addresses_to_strv (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) +nm_editor_bind_ip_addresses_to_strv (int family, + gpointer source, + const gchar *source_property, + gpointer target, + const gchar *target_property, + GBindingFlags flags) { g_object_bind_property_full (source, source_property, target, target_property, flags, - ip4_addresses_check_and_copy, - ip4_addresses_check_and_copy, - NULL, NULL); + ip_addresses_check_and_copy, + ip_addresses_check_and_copy, + GINT_TO_POINTER (family), NULL); } static gboolean -ip4_gateway_to_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_gateway_to_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { GPtrArray *addrs; - NMIP4Address *addr; - guint32 gateway = 0; - const char *str; - char buf[INET_ADDRSTRLEN]; + NMIPAddress *addr; + const char *gateway = NULL; int i; addrs = g_value_get_boxed (source_value); for (i = 0; i < addrs->len; i++) { addr = addrs->pdata[i]; - gateway = nm_ip4_address_get_gateway (addr); + gateway = nm_ip_address_get_gateway (addr); if (gateway) break; } - if (gateway) - str = inet_ntop (AF_INET, &gateway, buf, sizeof (buf)); - else - str = ""; - g_value_set_string (target_value, str); + if (!gateway) + gateway = ""; + g_value_set_string (target_value, gateway); return TRUE; } static gboolean -ip4_gateway_from_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_gateway_from_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { + int family = GPOINTER_TO_INT (user_data); const char *text; GPtrArray *addrs; - NMIP4Address *addr; - guint32 addrbytes; + NMIPAddress *addr; int i; text = g_value_get_string (source_value); - if (!ip_string_parse (text, AF_INET, &addrbytes, NULL)) - return FALSE; + if (*text) { + if (!nm_utils_ipaddr_valid (family, text)) + return FALSE; + } else + text = NULL; /* Fetch the original property value, so as to preserve the IP address elements */ g_object_get (g_binding_get_source (binding), @@ -322,15 +327,15 @@ ip4_gateway_from_string (GBinding *binding, return FALSE; } addr = addrs->pdata[0]; - if (addrbytes == nm_ip4_address_get_gateway (addr)) { + if (!g_strcmp0 (text, nm_ip_address_get_gateway (addr))) { g_ptr_array_unref (addrs); return FALSE; } - nm_ip4_address_set_gateway (addr, addrbytes); + nm_ip_address_set_gateway (addr, text); for (i = 1; i < addrs->len; i++) { addr = addrs->pdata[i]; - nm_ip4_address_set_gateway (addr, 0); + nm_ip_address_set_gateway (addr, NULL); } g_value_take_boxed (target_value, addrs); @@ -338,7 +343,8 @@ ip4_gateway_from_string (GBinding *binding, } /** - * nm_editor_bind_ip4_gateway_to_string: + * nm_editor_bind_ip_gateway_to_string: + * @family: the IP address family * @source: the source object (eg, an #NMSettingIP4Config) * @source_property: the property on @source to bind (eg, * %NM_SETTING_IP4_CONFIG_ADDRESSES) @@ -347,7 +353,7 @@ ip4_gateway_from_string (GBinding *binding, * (eg, "text") * @flags: %GBindingFlags * - * Binds the #GPtrArray-of-#NMIP4Route property @source_property on @source to + * Binds the #GPtrArray-of-#NMIPRoute property @source_property on @source to * the %G_TYPE_STRING property @target_property on @target. * * Specifically, this binds the "gateway" field of the first address in @@ -355,495 +361,39 @@ ip4_gateway_from_string (GBinding *binding, * its "address" and "prefix" fields are unmodified. */ void -nm_editor_bind_ip4_gateway_to_string (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) -{ - g_object_bind_property_full (source, source_property, - target, target_property, - flags, - ip4_gateway_to_string, - ip4_gateway_from_string, - NULL, NULL); -} - -static gboolean -ip4_route_transform_to_dest_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP4Route *route; - char buf[INET_ADDRSTRLEN], *string; - guint32 addrbytes; - - route = g_value_get_boxed (source_value); - if (route) - addrbytes = nm_ip4_route_get_dest (route); - else - addrbytes = 0; - - if (addrbytes) { - string = g_strdup_printf ("%s/%d", - inet_ntop (AF_INET, &addrbytes, buf, sizeof (buf)), - (int) nm_ip4_route_get_prefix (route)); - g_value_take_string (target_value, string); - } else - g_value_set_string (target_value, ""); - return TRUE; -} - -static gboolean -ip4_route_transform_to_next_hop_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP4Route *route; - char buf[INET_ADDRSTRLEN]; - guint32 addrbytes; - - route = g_value_get_boxed (source_value); - if (route) - addrbytes = nm_ip4_route_get_next_hop (route); - else - addrbytes = 0; - - if (addrbytes) - inet_ntop (AF_INET, &addrbytes, buf, sizeof (buf)); - else - buf[0] = '\0'; - g_value_set_string (target_value, buf); - return TRUE; -} - -static gboolean -ip4_route_transform_to_metric_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP4Route *route; - char *string; - - route = g_value_get_boxed (source_value); - if (route && nm_ip4_route_get_dest (route)) { - string = g_strdup_printf ("%lu", (gulong) nm_ip4_route_get_metric (route)); - g_value_take_string (target_value, string); - } else - g_value_set_string (target_value, ""); - return TRUE; -} - -static gboolean -ip4_route_transform_from_dest_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP4Route *route; - const char *text; - guint32 addrbytes, prefix; - - text = g_value_get_string (source_value); - if (!ip_string_parse (text, AF_INET, &addrbytes, &prefix)) - return FALSE; - - /* Fetch the original property value */ - g_object_get (g_binding_get_source (binding), - g_binding_get_source_property (binding), &route, - NULL); - - nm_ip4_route_set_dest (route, addrbytes); - nm_ip4_route_set_prefix (route, prefix); - - g_value_take_boxed (target_value, route); - return TRUE; -} - -static gboolean -ip4_route_transform_from_next_hop_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP4Route *route; - const char *text; - guint32 addrbytes; - - text = g_value_get_string (source_value); - if (*text) { - if (!ip_string_parse (text, AF_INET, &addrbytes, NULL)) - return FALSE; - } else - addrbytes = 0; - - /* Fetch the original property value */ - g_object_get (g_binding_get_source (binding), - g_binding_get_source_property (binding), &route, - NULL); - - nm_ip4_route_set_next_hop (route, addrbytes); - - g_value_take_boxed (target_value, route); - return TRUE; -} - -static gboolean -ip4_route_transform_from_metric_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP4Route *route; - const char *text; - guint32 metric; - - text = g_value_get_string (source_value); - metric = strtoul (text, NULL, 10); - - /* Fetch the original property value */ - g_object_get (g_binding_get_source (binding), - g_binding_get_source_property (binding), &route, - NULL); - - nm_ip4_route_set_metric (route, metric); - - g_value_take_boxed (target_value, route); - return TRUE; -} - -/** - * nm_editor_bind_ip4_route_to_strings: - * @source: the source object - * @source_property: the source property - * @dest_target: the target object for the route's destionation - * @dest_target_property: the property on @dest_target - * @next_hop_target: the target object for the route's next hop - * @next_hop_target_property: the property on @next_hop_target - * @metric_target: the target object for the route's metric - * @metric_target_property: the property on @metric_target - * @flags: %GBindingFlags - * - * Binds the #NMIP4Route-valued property @source_property on @source to the - * three indicated string-valued target properties (and vice versa if - * %G_BINDING_BIDIRECTIONAL is specified). - * - * @dest_target_property should be an "address/prefix" string, as with - * nm_editor_bind_ip4_addresses_with_prefix_to_strv(). @next_hop_target_property - * is a plain IP address, and @metric_target_property is a number. - */ -void -nm_editor_bind_ip4_route_to_strings (gpointer source, +nm_editor_bind_ip_gateway_to_string (int family, + gpointer source, const gchar *source_property, - gpointer dest_target, - const gchar *dest_target_property, - gpointer next_hop_target, - const gchar *next_hop_target_property, - gpointer metric_target, - const gchar *metric_target_property, + gpointer target, + const gchar *target_property, GBindingFlags flags) { g_object_bind_property_full (source, source_property, - dest_target, dest_target_property, - flags, - ip4_route_transform_to_dest_string, - ip4_route_transform_from_dest_string, - NULL, NULL); - g_object_bind_property_full (source, source_property, - next_hop_target, next_hop_target_property, - flags, - ip4_route_transform_to_next_hop_string, - ip4_route_transform_from_next_hop_string, - NULL, NULL); - g_object_bind_property_full (source, source_property, - metric_target, metric_target_property, + target, target_property, flags, - ip4_route_transform_to_metric_string, - ip4_route_transform_from_metric_string, - NULL, NULL); + ip_gateway_to_string, + ip_gateway_from_string, + GINT_TO_POINTER (family), NULL); } -#define IP6_ADDRESS_SET(addr) ( addr \ - && memcmp (addr, &in6addr_any, sizeof (struct in6_addr)) != 0) - static gboolean -ip6_addresses_with_prefix_to_strv (GBinding *binding, +ip_route_transform_to_dest_string (GBinding *binding, const GValue *source_value, GValue *target_value, gpointer user_data) { - GPtrArray *addrs; - NMIP6Address *addr; - const struct in6_addr *addrbytes; - guint prefix; - char **strings, buf[INET6_ADDRSTRLEN]; - int i; - - addrs = g_value_get_boxed (source_value); - strings = g_new0 (char *, addrs->len + 1); - - for (i = 0; i < addrs->len; i++) { - addr = addrs->pdata[i]; - addrbytes = nm_ip6_address_get_address (addr); - prefix = nm_ip6_address_get_prefix (addr); - - if (IP6_ADDRESS_SET (addrbytes)) { - strings[i] = g_strdup_printf ("%s/%d", - inet_ntop (AF_INET6, addrbytes, buf, sizeof (buf)), - prefix); - } else - strings[i] = g_strdup (""); - } - - g_value_take_boxed (target_value, strings); - return TRUE; -} - -static gboolean -ip6_addresses_with_prefix_from_strv (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - char **strings; - GPtrArray *addrs; - NMIP6Address *addr; - struct in6_addr addrbytes; - guint32 prefix; - int i; - - strings = g_value_get_boxed (source_value); - - /* Fetch the original property value, so as to preserve the gateway elements */ - g_object_get (g_binding_get_source (binding), - g_binding_get_source_property (binding), &addrs, - NULL); - - for (i = 0; strings[i]; i++) { - if (i >= addrs->len) { - addr = nm_ip6_address_new (); - nm_ip6_address_set_prefix (addr, 128); - g_ptr_array_add (addrs, addr); - } else - addr = addrs->pdata[i]; - - if (!ip_string_parse (strings[i], AF_INET6, &addrbytes, &prefix)) { - g_ptr_array_unref (addrs); - return FALSE; - } - - nm_ip6_address_set_address (addr, &addrbytes); - nm_ip6_address_set_prefix (addr, prefix); - } - - g_ptr_array_set_size (addrs, i); - g_value_set_boxed (target_value, addrs); - return TRUE; -} - -/** - * nm_editor_bind_ip6_addresses_with_prefix_to_strv: - * @source: the source object (eg, an #NMSettingIP6Config) - * @source_property: the property on @source to bind (eg, - * %NM_SETTING_IP6_CONFIG_ADDRESSES) - * @target: the target object (eg, an #NmtAddressList) - * @target_property: the property on @target to bind - * (eg, "strings") - * @flags: %GBindingFlags - * - * Binds the #GPtrArray-of-#NMIP6Address property @source_property on @source to - * the %G_TYPE_STRV property @target_property on @target. - * - * Each #NMIP6Address in triplet in @source_property will be converted to a - * string of the form "ip::ad:dr:ess/prefix" in @target_property (and vice versa - * if %G_BINDING_BIDIRECTIONAL) is specified. The "gateway" fields in - * @source_property are ignored when converting to strings, and unmodified when - * converting from strings. - */ -void -nm_editor_bind_ip6_addresses_with_prefix_to_strv (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) -{ - g_object_bind_property_full (source, source_property, - target, target_property, - flags, - ip6_addresses_with_prefix_to_strv, - ip6_addresses_with_prefix_from_strv, - NULL, NULL); -} - -static gboolean -ip6_addresses_check_and_copy (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - char **strings; - struct in6_addr addr; - int i; - - strings = g_value_get_boxed (source_value); - - for (i = 0; strings[i]; i++) { - if (!ip_string_parse (strings[i], AF_INET6, &addr, NULL)) - return FALSE; - } - - g_value_set_boxed (target_value, strings); - return TRUE; -} - -/** - * nm_editor_bind_ip6_addresses_to_strv: - * @source: the source object (eg, an #NMSettingIP6Config) - * @source_property: the property on @source to bind (eg, - * %NM_SETTING_IP6_CONFIG_DNS) - * @target: the target object (eg, an #NmtAddressList) - * @target_property: the property on @target to bind - * (eg, "strings") - * @flags: %GBindingFlags - * - * Binds the %G_TYPE_STRV property @source_property on @source to the - * %G_TYPE_STRV property @target_property on @target, verifying that - * each string is a valid IPv6 address when copying. - */ -void -nm_editor_bind_ip6_addresses_to_strv (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) -{ - g_object_bind_property_full (source, source_property, - target, target_property, - flags, - ip6_addresses_check_and_copy, - ip6_addresses_check_and_copy, - NULL, NULL); -} - -static gboolean -ip6_gateway_to_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - GPtrArray *addrs; - NMIP6Address *addr; - const struct in6_addr *gateway; - char buf[INET6_ADDRSTRLEN]; - const char *str; - - addrs = g_value_get_boxed (source_value); - if (addrs->len == 0) - return FALSE; - - addr = addrs->pdata[0]; - gateway = nm_ip6_address_get_gateway (addr); - - if (IP6_ADDRESS_SET (gateway)) - str = inet_ntop (AF_INET6, gateway, buf, sizeof (buf)); - else - str = ""; - g_value_set_string (target_value, str); - return TRUE; -} - -static gboolean -ip6_gateway_from_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - GPtrArray *addrs; - const char *text; - NMIP6Address *addr; - struct in6_addr gateway; - int i; - - text = g_value_get_string (source_value); - if (!ip_string_parse (text, AF_INET6, &gateway, NULL)) - return FALSE; - - /* Fetch the original property value, so as to preserve the IP address elements */ - g_object_get (g_binding_get_source (binding), - g_binding_get_source_property (binding), &addrs, - NULL); - if (!addrs->len) { - g_ptr_array_unref (addrs); - return FALSE; - } - - addr = addrs->pdata[0]; - nm_ip6_address_set_gateway (addr, &gateway); - - for (i = 1; i < addrs->len; i++) { - addr = addrs->pdata[i]; - nm_ip6_address_set_gateway (addr, &in6addr_any); - } - - g_value_take_boxed (target_value, addrs); - return TRUE; -} - -/** - * nm_editor_bind_ip6_gateway_to_string: - * @source: the source object (eg, an #NMSettingIP6Config) - * @source_property: the property on @source to bind (eg, - * %NM_SETTING_IP6_CONFIG_ADDRESSES) - * @target: the target object (eg, an #NmtNewtEntry) - * @target_property: the property on @target to bind - * (eg, "text") - * @flags: %GBindingFlags - * - * Binds the #GPtrArray-of-#NMIP6Address property @source_property on @source to - * the %G_TYPE_STRING property @target_property on @target. - * - * Specifically, this binds the "gateway" field of the first address in - * @source_property; all other addresses in @source_property are ignored, and - * its "address" and "prefix" fields are unmodified. - */ -void -nm_editor_bind_ip6_gateway_to_string (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) -{ - g_object_bind_property_full (source, source_property, - target, target_property, - flags, - ip6_gateway_to_string, - ip6_gateway_from_string, - NULL, NULL); -} - -static gboolean -ip6_route_transform_to_dest_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - NMIP6Route *route; - char buf[INET6_ADDRSTRLEN], *string; - const struct in6_addr *addrbytes; + NMIPRoute *route; + const char *addrstr; + char *string; route = g_value_get_boxed (source_value); if (route) - addrbytes = nm_ip6_route_get_dest (route); + addrstr = nm_ip_route_get_dest (route); else - addrbytes = &in6addr_any; + addrstr = NULL; - if (IP6_ADDRESS_SET (addrbytes)) { - string = g_strdup_printf ("%s/%d", - inet_ntop (AF_INET6, addrbytes, buf, sizeof (buf)), - (int) nm_ip6_route_get_prefix (route)); + if (addrstr) { + string = g_strdup_printf ("%s/%d", addrstr, (int) nm_ip_route_get_prefix (route)); g_value_take_string (target_value, string); } else g_value_set_string (target_value, ""); @@ -851,41 +401,38 @@ ip6_route_transform_to_dest_string (GBinding *binding, } static gboolean -ip6_route_transform_to_next_hop_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_route_transform_to_next_hop_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { - NMIP6Route *route; - char buf[INET6_ADDRSTRLEN]; - const struct in6_addr *addrbytes; + NMIPRoute *route; + const char *addrstr; route = g_value_get_boxed (source_value); - if (route) - addrbytes = nm_ip6_route_get_next_hop (route); - else - addrbytes = &in6addr_any; + if (route) { + addrstr = nm_ip_route_get_next_hop (route); + if (!addrstr) + addrstr = ""; + } else + addrstr = ""; - if (IP6_ADDRESS_SET (addrbytes)) - inet_ntop (AF_INET6, addrbytes, buf, sizeof (buf)); - else - buf[0] = '\0'; - g_value_set_string (target_value, buf); + g_value_set_string (target_value, addrstr); return TRUE; } static gboolean -ip6_route_transform_to_metric_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_route_transform_to_metric_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { - NMIP6Route *route; + NMIPRoute *route; char *string; route = g_value_get_boxed (source_value); - if (route && IP6_ADDRESS_SET (nm_ip6_route_get_dest (route))) { - string = g_strdup_printf ("%lu", (gulong) nm_ip6_route_get_metric (route)); + if (route && nm_ip_route_get_dest (route)) { + string = g_strdup_printf ("%lu", (gulong) nm_ip_route_get_metric (route)); g_value_take_string (target_value, string); } else g_value_set_string (target_value, ""); @@ -893,18 +440,19 @@ ip6_route_transform_to_metric_string (GBinding *binding, } static gboolean -ip6_route_transform_from_dest_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_route_transform_from_dest_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { - NMIP6Route *route; + int family = GPOINTER_TO_INT (user_data); + NMIPRoute *route; const char *text; - struct in6_addr addrbytes; + char *addrstr; guint32 prefix; text = g_value_get_string (source_value); - if (!ip_string_parse (text, AF_INET6, &addrbytes, &prefix)) + if (!parse_addr_prefix (text, family, &addrstr, &prefix)) return FALSE; /* Fetch the original property value */ @@ -912,48 +460,49 @@ ip6_route_transform_from_dest_string (GBinding *binding, g_binding_get_source_property (binding), &route, NULL); - nm_ip6_route_set_dest (route, &addrbytes); - nm_ip6_route_set_prefix (route, prefix); + nm_ip_route_set_dest (route, addrstr); + nm_ip_route_set_prefix (route, prefix); + g_free (addrstr); g_value_take_boxed (target_value, route); return TRUE; } static gboolean -ip6_route_transform_from_next_hop_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_route_transform_from_next_hop_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { - NMIP6Route *route; + int family = GPOINTER_TO_INT (user_data); + NMIPRoute *route; const char *text; - struct in6_addr addrbytes; text = g_value_get_string (source_value); if (*text) { - if (!ip_string_parse (text, AF_INET6, &addrbytes, NULL)) + if (!nm_utils_ipaddr_valid (family, text)) return FALSE; } else - addrbytes = in6addr_any; + text = NULL; /* Fetch the original property value */ g_object_get (g_binding_get_source (binding), g_binding_get_source_property (binding), &route, NULL); - nm_ip6_route_set_next_hop (route, &addrbytes); + nm_ip_route_set_next_hop (route, text); g_value_take_boxed (target_value, route); return TRUE; } static gboolean -ip6_route_transform_from_metric_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) +ip_route_transform_from_metric_string (GBinding *binding, + const GValue *source_value, + GValue *target_value, + gpointer user_data) { - NMIP6Route *route; + NMIPRoute *route; const char *text; guint32 metric; @@ -965,14 +514,15 @@ ip6_route_transform_from_metric_string (GBinding *binding, g_binding_get_source_property (binding), &route, NULL); - nm_ip6_route_set_metric (route, metric); + nm_ip_route_set_metric (route, metric); g_value_take_boxed (target_value, route); return TRUE; } /** - * nm_editor_bind_ip6_route_to_strings: + * nm_editor_bind_ip_route_to_strings: + * @family: the IP address family * @source: the source object * @source_property: the source property * @dest_target: the target object for the route's destionation @@ -983,43 +533,44 @@ ip6_route_transform_from_metric_string (GBinding *binding, * @metric_target_property: the property on @metric_target * @flags: %GBindingFlags * - * Binds the #NMIP6Route-valued property @source_property on @source - * to the three indicated string-valued target properties (and vice - * versa if %G_BINDING_BIDIRECTIONAL is specified). + * Binds the #NMIPRoute-valued property @source_property on @source to the + * three indicated string-valued target properties (and vice versa if + * %G_BINDING_BIDIRECTIONAL is specified). * * @dest_target_property should be an "address/prefix" string, as with - * nm_editor_bind_ip6_addresses_with_prefix_to_strv(). @next_hop_target - * is a plain IP address, and @metric_target is a number. + * nm_editor_bind_ip4_addresses_with_prefix_to_strv(). @next_hop_target_property + * is a plain IP address, and @metric_target_property is a number. */ void -nm_editor_bind_ip6_route_to_strings (gpointer source, - const gchar *source_property, - gpointer dest_target, - const gchar *dest_target_property, - gpointer next_hop_target, - const gchar *next_hop_target_property, - gpointer metric_target, - const gchar *metric_target_property, - GBindingFlags flags) +nm_editor_bind_ip_route_to_strings (int family, + gpointer source, + const gchar *source_property, + gpointer dest_target, + const gchar *dest_target_property, + gpointer next_hop_target, + const gchar *next_hop_target_property, + gpointer metric_target, + const gchar *metric_target_property, + GBindingFlags flags) { g_object_bind_property_full (source, source_property, dest_target, dest_target_property, flags, - ip6_route_transform_to_dest_string, - ip6_route_transform_from_dest_string, - NULL, NULL); + ip_route_transform_to_dest_string, + ip_route_transform_from_dest_string, + GINT_TO_POINTER (family), NULL); g_object_bind_property_full (source, source_property, next_hop_target, next_hop_target_property, flags, - ip6_route_transform_to_next_hop_string, - ip6_route_transform_from_next_hop_string, - NULL, NULL); + ip_route_transform_to_next_hop_string, + ip_route_transform_from_next_hop_string, + GINT_TO_POINTER (family), NULL); g_object_bind_property_full (source, source_property, metric_target, metric_target_property, flags, - ip6_route_transform_to_metric_string, - ip6_route_transform_from_metric_string, - NULL, NULL); + ip_route_transform_to_metric_string, + ip_route_transform_from_metric_string, + GINT_TO_POINTER (family), NULL); } /* Wireless security method binding */ diff --git a/clients/tui/nm-editor-bindings.h b/clients/tui/nm-editor-bindings.h index b595ef03c1..5e34c42dec 100644 --- a/clients/tui/nm-editor-bindings.h +++ b/clients/tui/nm-editor-bindings.h @@ -25,49 +25,27 @@ G_BEGIN_DECLS void nm_editor_bindings_init (void); -void nm_editor_bind_ip4_addresses_with_prefix_to_strv (gpointer source, +void nm_editor_bind_ip_addresses_with_prefix_to_strv (int family, + gpointer source, const gchar *source_property, gpointer target, const gchar *target_property, GBindingFlags flags); -void nm_editor_bind_ip4_addresses_to_strv (gpointer source, +void nm_editor_bind_ip_addresses_to_strv (int family, + gpointer source, const gchar *source_property, gpointer target, const gchar *target_property, GBindingFlags flags); -void nm_editor_bind_ip4_gateway_to_string (gpointer source, +void nm_editor_bind_ip_gateway_to_string (int family, + gpointer source, const gchar *source_property, gpointer target, const gchar *target_property, GBindingFlags flags); -void nm_editor_bind_ip4_route_to_strings (gpointer source, - const gchar *source_property, - gpointer dest_target, - const gchar *dest_target_property, - gpointer next_hop_target, - const gchar *next_hop_target_property, - gpointer metric_target, - const gchar *metric_target_property, - GBindingFlags flags); - -void nm_editor_bind_ip6_addresses_with_prefix_to_strv (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags); -void nm_editor_bind_ip6_addresses_to_strv (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags); -void nm_editor_bind_ip6_gateway_to_string (gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags); - -void nm_editor_bind_ip6_route_to_strings (gpointer source, +void nm_editor_bind_ip_route_to_strings (int family, + gpointer source, const gchar *source_property, gpointer dest_target, const gchar *dest_target_property, diff --git a/clients/tui/nmt-page-ip4.c b/clients/tui/nmt-page-ip4.c index 8d10b4aa7f..686c2ce923 100644 --- a/clients/tui/nmt-page-ip4.c +++ b/clients/tui/nmt-page-ip4.c @@ -138,21 +138,24 @@ nmt_page_ip4_constructed (GObject *object) grid = NMT_PAGE_GRID (ip4); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4_WITH_PREFIX); - nm_editor_bind_ip4_addresses_with_prefix_to_strv (s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES, - widget, "strings", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + nm_editor_bind_ip_addresses_with_prefix_to_strv (AF_INET, + s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES, + widget, "strings", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Addresses"), widget, NULL); widget = nmt_ip_entry_new (25, AF_INET, FALSE, TRUE); - nm_editor_bind_ip4_gateway_to_string (s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES, - widget, "text", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + nm_editor_bind_ip_gateway_to_string (AF_INET, + s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES, + widget, "text", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Gateway"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4); - nm_editor_bind_ip4_addresses_to_strv (s_ip4, NM_SETTING_IP4_CONFIG_DNS, - widget, "strings", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + nm_editor_bind_ip_addresses_to_strv (AF_INET, + s_ip4, NM_SETTING_IP4_CONFIG_DNS, + widget, "strings", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("DNS servers"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_HOSTNAME); diff --git a/clients/tui/nmt-page-ip6.c b/clients/tui/nmt-page-ip6.c index 7d3ef548fd..05917fa0c8 100644 --- a/clients/tui/nmt-page-ip6.c +++ b/clients/tui/nmt-page-ip6.c @@ -138,21 +138,24 @@ nmt_page_ip6_constructed (GObject *object) grid = NMT_PAGE_GRID (ip6); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP6_WITH_PREFIX); - nm_editor_bind_ip6_addresses_with_prefix_to_strv (s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES, - widget, "strings", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + nm_editor_bind_ip_addresses_with_prefix_to_strv (AF_INET6, + s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES, + widget, "strings", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Addresses"), widget, NULL); widget = nmt_ip_entry_new (25, AF_INET6, FALSE, TRUE); - nm_editor_bind_ip6_gateway_to_string (s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES, - widget, "text", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + nm_editor_bind_ip_gateway_to_string (AF_INET6, + s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES, + widget, "text", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Gateway"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP6); - nm_editor_bind_ip6_addresses_to_strv (s_ip6, NM_SETTING_IP6_CONFIG_DNS, - widget, "strings", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + nm_editor_bind_ip_addresses_to_strv (AF_INET6, + s_ip6, NM_SETTING_IP6_CONFIG_DNS, + widget, "strings", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("DNS servers"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_HOSTNAME); diff --git a/clients/tui/nmt-route-editor.c b/clients/tui/nmt-route-editor.c index 98fd9ccbb6..3871b86302 100644 --- a/clients/tui/nmt-route-editor.c +++ b/clients/tui/nmt-route-editor.c @@ -109,12 +109,12 @@ nmt_route_editor_constructed (GObject *object) if (NM_IS_SETTING_IP4_CONFIG (priv->edit_setting)) { routes = nmt_route_table_new (AF_INET); g_object_bind_property (priv->edit_setting, NM_SETTING_IP4_CONFIG_ROUTES, - routes, "ip4-routes", + routes, "routes", G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL); } else { routes = nmt_route_table_new (AF_INET6); g_object_bind_property (priv->edit_setting, NM_SETTING_IP6_CONFIG_ROUTES, - routes, "ip6-routes", + routes, "routes", G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL); } diff --git a/clients/tui/nmt-route-entry.c b/clients/tui/nmt-route-entry.c index 9b22f389dc..3a87552089 100644 --- a/clients/tui/nmt-route-entry.c +++ b/clients/tui/nmt-route-entry.c @@ -49,8 +49,7 @@ typedef struct { int family; int ip_entry_width, metric_entry_width; - NMIP4Route *ip4_route; - NMIP6Route *ip6_route; + NMIPRoute *route; } NmtRouteEntryPrivate; enum { @@ -58,8 +57,7 @@ enum { PROP_FAMILY, PROP_IP_ENTRY_WIDTH, PROP_METRIC_ENTRY_WIDTH, - PROP_IP4_ROUTE, - PROP_IP6_ROUTE, + PROP_ROUTE, LAST_PROP }; @@ -143,20 +141,12 @@ nmt_route_entry_constructed (GObject *object) nmt_newt_grid_add (grid, priv->metric, 4, 0); nmt_newt_widget_set_padding (priv->metric, 1, 0, 0, 0); - if (priv->family == AF_INET) { - nm_editor_bind_ip4_route_to_strings (object, "ip4-route", - priv->dest, "text", - priv->next_hop, "text", - priv->metric, "text", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); - } else if (priv->family == AF_INET6) { - nm_editor_bind_ip6_route_to_strings (object, "ip6-route", - priv->dest, "text", - priv->next_hop, "text", - priv->metric, "text", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); - } else - g_assert_not_reached (); + nm_editor_bind_ip_route_to_strings (priv->family, + object, "route", + priv->dest, "text", + priv->next_hop, "text", + priv->metric, "text", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); G_OBJECT_CLASS (nmt_route_entry_parent_class)->constructed (object); } @@ -174,8 +164,7 @@ nmt_route_entry_finalize (GObject *object) { NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE (object); - g_clear_pointer (&priv->ip4_route, nm_ip4_route_unref); - g_clear_pointer (&priv->ip6_route, nm_ip6_route_unref); + g_clear_pointer (&priv->route, nm_ip_route_unref); G_OBJECT_CLASS (nmt_route_entry_parent_class)->finalize (object); } @@ -198,17 +187,10 @@ nmt_route_entry_set_property (GObject *object, case PROP_METRIC_ENTRY_WIDTH: priv->metric_entry_width = g_value_get_int (value); break; - case PROP_IP4_ROUTE: - g_return_if_fail (priv->family == AF_INET); - if (priv->ip4_route) - nm_ip4_route_unref (priv->ip4_route); - priv->ip4_route = g_value_dup_boxed (value); - break; - case PROP_IP6_ROUTE: - g_return_if_fail (priv->family == AF_INET6); - if (priv->ip6_route) - nm_ip6_route_unref (priv->ip6_route); - priv->ip6_route = g_value_dup_boxed (value); + case PROP_ROUTE: + if (priv->route) + nm_ip_route_unref (priv->route); + priv->route = g_value_dup_boxed (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -234,13 +216,8 @@ nmt_route_entry_get_property (GObject *object, case PROP_METRIC_ENTRY_WIDTH: g_value_set_int (value, priv->metric_entry_width); break; - case PROP_IP4_ROUTE: - g_return_if_fail (priv->family == AF_INET); - g_value_set_boxed (value, priv->ip4_route); - break; - case PROP_IP6_ROUTE: - g_return_if_fail (priv->family == AF_INET6); - g_value_set_boxed (value, priv->ip6_route); + case PROP_ROUTE: + g_value_set_boxed (value, priv->route); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -301,27 +278,14 @@ nmt_route_entry_class_init (NmtRouteEntryClass *entry_class) G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); /** - * NmtRouteEntry:ip4-route: - * - * The contents of the entries, as an #NMIP4Route. Only valid - * if #NmtRouteEntry:family is %AF_INET. - */ - g_object_class_install_property - (object_class, PROP_IP4_ROUTE, - g_param_spec_boxed ("ip4-route", "", "", - nm_ip4_route_get_type (), - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); - /** - * NmtRouteEntry:ip6-route: + * NmtRouteEntry:route: * - * The contents of the entries, as an #NMIP6Route. Only valid - * if #NmtRouteEntry:family is %AF_INET6. + * The contents of the entries, as an #NMIPRoute. */ g_object_class_install_property - (object_class, PROP_IP6_ROUTE, - g_param_spec_boxed ("ip6-route", "", "", - nm_ip6_route_get_type (), + (object_class, PROP_ROUTE, + g_param_spec_boxed ("route", "", "", + nm_ip_route_get_type (), G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); } diff --git a/clients/tui/nmt-route-table.c b/clients/tui/nmt-route-table.c index 023928c34f..523d8ee32a 100644 --- a/clients/tui/nmt-route-table.c +++ b/clients/tui/nmt-route-table.c @@ -54,8 +54,7 @@ typedef struct { enum { PROP_0, PROP_FAMILY, - PROP_IP4_ROUTES, - PROP_IP6_ROUTES, + PROP_ROUTES, LAST_PROP }; @@ -85,7 +84,7 @@ route_list_transform_to_route (GBinding *binding, NmtRouteTable *table = NMT_ROUTE_TABLE (g_binding_get_source (binding)); NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); int n = GPOINTER_TO_INT (user_data); - gpointer route; + NMIPRoute *route; if (n >= priv->routes->len) return FALSE; @@ -105,24 +104,17 @@ route_list_transform_from_route (GBinding *binding, NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); int n = GPOINTER_TO_INT (user_data); GPtrArray *routes; - gpointer route; + NMIPRoute *route; if (n >= priv->routes->len) return FALSE; route = priv->routes->pdata[n]; routes = priv->routes; - if (priv->family == AF_INET) - priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_route_unref); - else - priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip6_route_unref); + priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref); - if (route) { - if (priv->family == AF_INET) - nm_ip4_route_unref (route); - else if (priv->family == AF_INET6) - nm_ip6_route_unref (route); - } + if (route) + nm_ip_route_unref (route); routes->pdata[n] = g_value_dup_boxed (source_value); g_value_take_boxed (target_value, routes); @@ -141,21 +133,12 @@ create_route_entry (NmtWidgetList *list, priv->ip_entry_width, priv->metric_entry_width); - if (priv->family == AF_INET) { - g_object_bind_property_full (table, "ip4-routes", - entry, "ip4-route", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE, - route_list_transform_to_route, - route_list_transform_from_route, - GINT_TO_POINTER (num), NULL); - } else { - g_object_bind_property_full (table, "ip6-routes", - entry, "ip6-route", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE, - route_list_transform_to_route, - route_list_transform_from_route, - GINT_TO_POINTER (num), NULL); - } + g_object_bind_property_full (table, "routes", + entry, "route", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE, + route_list_transform_to_route, + route_list_transform_from_route, + GINT_TO_POINTER (num), NULL); return entry; } @@ -164,24 +147,15 @@ add_route (NmtWidgetList *list, gpointer table) { NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); + NMIPRoute *route; - if (priv->family == AF_INET) { - NMIP4Route *route; - - route = nm_ip4_route_new (); - nm_ip4_route_set_prefix (route, 32); - g_ptr_array_add (priv->routes, route); - nmt_widget_list_set_length (list, priv->routes->len); - g_object_notify (table, "ip4-routes"); - } else { - NMIP6Route *route; - - route = nm_ip6_route_new (); - nm_ip6_route_set_prefix (route, 128); - g_ptr_array_add (priv->routes, route); - nmt_widget_list_set_length (list, priv->routes->len); - g_object_notify (table, "ip6-routes"); - } + if (priv->family == AF_INET) + route = nm_ip_route_new (AF_INET, "0.0.0.0", 32, NULL, 0, NULL); + else + route = nm_ip_route_new (AF_INET6, "::", 128, NULL, 0, NULL); + g_ptr_array_add (priv->routes, route); + nmt_widget_list_set_length (list, priv->routes->len); + g_object_notify (table, "routes"); } static void @@ -190,7 +164,7 @@ remove_route (NmtWidgetList *list, gpointer table) { NmtRouteTablePrivate *priv = NMT_ROUTE_TABLE_GET_PRIVATE (table); - gpointer route; + NMIPRoute *route; if (num >= priv->routes->len) return; @@ -199,10 +173,7 @@ remove_route (NmtWidgetList *list, g_ptr_array_remove_index (priv->routes, num); nmt_widget_list_set_length (list, priv->routes->len); - if (priv->family == AF_INET) - g_object_notify (table, "ip4-routes"); - else - g_object_notify (table, "ip6-routes"); + g_object_notify (table, "routes"); } static void @@ -214,6 +185,8 @@ nmt_route_table_init (NmtRouteTable *table) int dest_prefix_width, next_hop_width, metric_width; char *text; + priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref); + header = nmt_newt_grid_new (); text = g_strdup_printf ("%s/%s", _("Destination"), _("Prefix")); @@ -283,27 +256,12 @@ nmt_route_table_set_property (GObject *object, switch (prop_id) { case PROP_FAMILY: priv->family = g_value_get_int (value); - if (priv->family == AF_INET) - priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_route_unref); - else - priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip6_route_unref); - break; - case PROP_IP4_ROUTES: - g_return_if_fail (priv->family == AF_INET); - array = g_value_get_boxed (value); - g_ptr_array_set_size (priv->routes, 0); - for (i = 0; i < array->len; i++) { - nm_ip4_route_ref (array->pdata[i]); - g_ptr_array_add (priv->routes, array->pdata[i]); - } - nmt_widget_list_set_length (NMT_WIDGET_LIST (priv->list), priv->routes->len); break; - case PROP_IP6_ROUTES: - g_return_if_fail (priv->family == AF_INET6); + case PROP_ROUTES: array = g_value_get_boxed (value); g_ptr_array_set_size (priv->routes, 0); for (i = 0; i < array->len; i++) { - nm_ip6_route_ref (array->pdata[i]); + nm_ip_route_ref (array->pdata[i]); g_ptr_array_add (priv->routes, array->pdata[i]); } nmt_widget_list_set_length (NMT_WIDGET_LIST (priv->list), priv->routes->len); @@ -326,12 +284,7 @@ nmt_route_table_get_property (GObject *object, case PROP_FAMILY: g_value_set_int (value, priv->family); break; - case PROP_IP4_ROUTES: - g_return_if_fail (priv->family == AF_INET); - g_value_set_boxed (value, priv->routes); - break; - case PROP_IP6_ROUTES: - g_return_if_fail (priv->family == AF_INET6); + case PROP_ROUTES: g_value_set_boxed (value, priv->routes); break; default: @@ -365,34 +318,16 @@ nmt_route_table_class_init (NmtRouteTableClass *table_class) G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); /** - * NmtRouteTable:ip4-routes: - * - * The array of routes, suitable for binding to - * #NMSettingIP4Config:routes. - * - * Only valid if #NmtRouteTable:family is %AF_INET - * - * Element-type: NMIP4Route - */ - g_object_class_install_property - (object_class, PROP_IP4_ROUTES, - g_param_spec_boxed ("ip4-routes", "", "", - G_TYPE_PTR_ARRAY, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); - /** - * NmtRouteTable:ip6-routes: - * - * The array of routes, suitable for binding to - * #NMSettingIP6Config:routes. + * NmtRouteTable:routes: * - * Only valid if #NmtRouteTable:family is %AF_INET6 + * The array of routes, suitable for binding to #NMSettingIP4Config:routes + * or #NMSettingIP6Config:routes. * - * Element-type: NMIP6Route + * Element-type: NMIPRoute */ g_object_class_install_property - (object_class, PROP_IP6_ROUTES, - g_param_spec_boxed ("ip6-routes", "", "", + (object_class, PROP_ROUTES, + g_param_spec_boxed ("routes", "", "", G_TYPE_PTR_ARRAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); diff --git a/docs/libnm/libnm-docs.xml b/docs/libnm/libnm-docs.xml index 007cbe4108..130c690429 100644 --- a/docs/libnm/libnm-docs.xml +++ b/docs/libnm/libnm-docs.xml @@ -87,6 +87,7 @@ + diff --git a/include/nm-test-utils.h b/include/nm-test-utils.h index 11ffd33033..ca6d97d598 100644 --- a/include/nm-test-utils.h +++ b/include/nm-test-utils.h @@ -952,18 +952,6 @@ nmtst_assert_connection_unnormalizable (NMConnection *con, #endif -static inline void -nmtst_assert_ip4_address_equals (guint32 addr, const char *expected, const char *loc) -{ - guint32 addr2 = nmtst_inet4_from_string (expected); - - if (addr != addr2) - g_error ("assert: %s: ip4 address '%s' expected, but got %s", - loc, expected ? expected : "any", nm_utils_inet4_ntop (addr, NULL)); -} -#define nmtst_assert_ip4_address_equals(addr, expected) \ - nmtst_assert_ip4_address_equals (addr, expected, G_STRLOC) - #ifdef __NM_UTILS_H__ static inline void nmtst_assert_hwaddr_equals (gconstpointer hwaddr1, gssize hwaddr1_len, const char *expected, const char *loc) diff --git a/libnm-core/Makefile.libnm-core b/libnm-core/Makefile.libnm-core index 481fc806ac..fc6d779b5e 100644 --- a/libnm-core/Makefile.libnm-core +++ b/libnm-core/Makefile.libnm-core @@ -24,6 +24,7 @@ libnm_core_headers = \ $(core)/nm-setting-generic.h \ $(core)/nm-setting-gsm.h \ $(core)/nm-setting-infiniband.h \ + $(core)/nm-setting-ip-config.h \ $(core)/nm-setting-ip4-config.h \ $(core)/nm-setting-ip6-config.h \ $(core)/nm-setting-olpc-mesh.h \ @@ -69,6 +70,7 @@ libnm_core_sources = \ $(core)/nm-setting-generic.c \ $(core)/nm-setting-gsm.c \ $(core)/nm-setting-infiniband.c \ + $(core)/nm-setting-ip-config.c \ $(core)/nm-setting-ip4-config.c \ $(core)/nm-setting-ip6-config.c \ $(core)/nm-setting-olpc-mesh.c \ diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index fced4ffe3d..ff44eb043f 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -77,7 +77,7 @@ const char *_nm_setting_ip4_config_get_address_label (NMSettingIP4Config *setting, guint32 i); gboolean _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, - NMIP4Address *address, + NMIPAddress *address, const char *label); /* NM_SETTING_COMPARE_FLAG_INFERRABLE: check whether a device-generated diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c new file mode 100644 index 0000000000..12080bd05f --- /dev/null +++ b/libnm-core/nm-setting-ip-config.c @@ -0,0 +1,874 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2007 - 2014 Red Hat, Inc. + * Copyright 2007 - 2008 Novell, Inc. + */ + +#include +#include + +#include "nm-setting-ip-config.h" +#include "nm-utils.h" +#include "nm-glib-compat.h" +#include "nm-setting-private.h" +#include "nm-utils-private.h" + +static char * +canonicalize_ip (int family, const char *ip, gboolean null_any) +{ + guint8 addr_bytes[sizeof (struct in6_addr)]; + char addr_str[NM_UTILS_INET_ADDRSTRLEN]; + int ret; + + if (!ip) { + g_return_val_if_fail (null_any == TRUE, NULL); + return NULL; + } + + ret = inet_pton (family, ip, addr_bytes); + g_return_val_if_fail (ret == 1, NULL); + + if (null_any) { + int addrlen = (family == AF_INET ? sizeof (struct in_addr) : sizeof (struct in6_addr)); + + if (!memcmp (addr_bytes, &in6addr_any, addrlen)) + return NULL; + } + + return g_strdup (inet_ntop (family, addr_bytes, addr_str, sizeof (addr_str))); +} + +static gboolean +valid_ip (int family, const char *ip, GError **error) +{ + if (!nm_utils_ipaddr_valid (family, ip)) { + g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_FAILED, + family == AF_INET ? _("Invalid IPv4 address '%s'") : _("Invalid IPv6 address '%s"), + ip); + return FALSE; + } else + return TRUE; +} + +static gboolean +valid_prefix (int family, guint prefix, GError **error) +{ + if ( (family == AF_INET && prefix > 32) + || (family == AF_INET6 && prefix > 128) + || prefix == 0) { + g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_FAILED, + family == AF_INET ? _("Invalid IPv4 address prefix '%u'") : _("Invalid IPv6 address prefix '%u"), + prefix); + return FALSE; + } + + return TRUE; +} + + +G_DEFINE_BOXED_TYPE (NMIPAddress, nm_ip_address, nm_ip_address_dup, nm_ip_address_unref) + +struct NMIPAddress { + guint refcount; + + char *address, *gateway; + int prefix, family; +}; + +/** + * nm_ip_address_new: + * @family: the IP address family (%AF_INET or %AF_INET6) + * @addr: the IP address + * @prefix: the address prefix length + * @gateway: (allow-none): the gateway + * @error: location to store error, or %NULL + * + * Creates a new #NMIPAddress object. + * + * Returns: (transfer full): the new #NMIPAddress object, or %NULL on error + **/ +NMIPAddress * +nm_ip_address_new (int family, + const char *addr, guint prefix, const char *gateway, + GError **error) +{ + NMIPAddress *address; + + g_return_val_if_fail (family == AF_INET || family == AF_INET6, NULL); + g_return_val_if_fail (addr != NULL, NULL); + + if (!valid_ip (family, addr, error)) + return NULL; + if (!valid_prefix (family, prefix, error)) + return NULL; + if (gateway && !valid_ip (family, gateway, error)) + return NULL; + + address = g_slice_new0 (NMIPAddress); + address->refcount = 1; + + address->family = family; + address->address = canonicalize_ip (family, addr, FALSE); + address->prefix = prefix; + address->gateway = canonicalize_ip (family, gateway, TRUE); + + return address; +} + +/** + * nm_ip_address_new_binary: + * @family: the IP address family (%AF_INET or %AF_INET6) + * @addr: the IP address + * @prefix: the address prefix length + * @gateway: (allow-none): the gateway + * @error: location to store error, or %NULL + * + * Creates a new #NMIPAddress object. @addr and @gateway (if non-%NULL) must + * point to buffers of the correct size for @family. + * + * Returns: (transfer full): the new #NMIPAddress object, or %NULL on error + **/ +NMIPAddress * +nm_ip_address_new_binary (int family, + gconstpointer addr, guint prefix, gconstpointer gateway, + GError **error) +{ + NMIPAddress *address; + char string[NM_UTILS_INET_ADDRSTRLEN]; + + g_return_val_if_fail (family == AF_INET || family == AF_INET6, NULL); + g_return_val_if_fail (addr != NULL, NULL); + + if (!valid_prefix (family, prefix, error)) + return NULL; + + address = g_slice_new0 (NMIPAddress); + address->refcount = 1; + + address->family = family; + address->address = g_strdup (inet_ntop (family, addr, string, sizeof (string))); + address->prefix = prefix; + if (gateway) + address->gateway = g_strdup (inet_ntop (family, gateway, string, sizeof (string))); + + return address; +} + +/** + * nm_ip_address_ref: + * @address: the #NMIPAddress + * + * Increases the reference count of the object. + **/ +void +nm_ip_address_ref (NMIPAddress *address) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (address->refcount > 0); + + address->refcount++; +} + +/** + * nm_ip_address_unref: + * @address: the #NMIPAddress + * + * Decreases the reference count of the object. If the reference count + * reaches zero, the object will be destroyed. + **/ +void +nm_ip_address_unref (NMIPAddress *address) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (address->refcount > 0); + + address->refcount--; + if (address->refcount == 0) { + g_free (address->address); + g_free (address->gateway); + g_slice_free (NMIPAddress, address); + } +} + +/** + * nm_ip_address_equal: + * @address: the #NMIPAddress + * @other: the #NMIPAddress to compare @address to. + * + * Determines if two #NMIPAddress objects contain the same values. + * + * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. + **/ +gboolean +nm_ip_address_equal (NMIPAddress *address, NMIPAddress *other) +{ + g_return_val_if_fail (address != NULL, FALSE); + g_return_val_if_fail (address->refcount > 0, FALSE); + + g_return_val_if_fail (other != NULL, FALSE); + g_return_val_if_fail (other->refcount > 0, FALSE); + + if ( address->family != other->family + || address->prefix != other->prefix + || strcmp (address->address, other->address) != 0 + || g_strcmp0 (address->gateway, other->gateway) != 0) + return FALSE; + return TRUE; +} + +/** + * nm_ip_address_dup: + * @address: the #NMIPAddress + * + * Creates a copy of @address + * + * Returns: (transfer full): a copy of @address + **/ +NMIPAddress * +nm_ip_address_dup (NMIPAddress *address) +{ + NMIPAddress *copy; + + g_return_val_if_fail (address != NULL, NULL); + g_return_val_if_fail (address->refcount > 0, NULL); + + copy = nm_ip_address_new (address->family, + address->address, address->prefix, address->gateway, + NULL); + return copy; +} + +/** + * nm_ip_address_get_family: + * @address: the #NMIPAddress + * + * Gets the IP address family (eg, AF_INET) property of this address + * object. + * + * Returns: the IP address family + **/ +int +nm_ip_address_get_family (NMIPAddress *address) +{ + g_return_val_if_fail (address != NULL, 0); + g_return_val_if_fail (address->refcount > 0, 0); + + return address->family; +} + +/** + * nm_ip_address_get_address: + * @address: the #NMIPAddress + * + * Gets the IP address property of this address object. + * + * Returns: the IP address + **/ +const char * +nm_ip_address_get_address (NMIPAddress *address) +{ + g_return_val_if_fail (address != NULL, NULL); + g_return_val_if_fail (address->refcount > 0, NULL); + + return address->address; +} + +/** + * nm_ip_address_set_address: + * @address: the #NMIPAddress + * @addr: the IP address, as a string + * + * Sets the IP address property of this address object. + * + * @addr must be a valid address of @address's family. If you aren't sure you + * have a valid address, use nm_utils_ipaddr_valid() to check it. + **/ +void +nm_ip_address_set_address (NMIPAddress *address, + const char *addr) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (addr != NULL); + g_return_if_fail (nm_utils_ipaddr_valid (address->family, addr)); + + g_free (address->address); + address->address = canonicalize_ip (address->family, addr, FALSE); +} + +/** + * nm_ip_address_get_address_binary: (skip) + * @address: the #NMIPAddress + * @addr: a buffer in which to store the address in binary format. + * + * Gets the IP address property of this address object. + * + * @addr must point to a buffer that is the correct size for @address's family. + **/ +void +nm_ip_address_get_address_binary (NMIPAddress *address, + gpointer addr) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (addr != NULL); + + inet_pton (address->family, address->address, addr); +} + +/** + * nm_ip_address_set_address_binary: (skip) + * @address: the #NMIPAddress + * @addr: the address, in binary format + * + * Sets the IP address property of this address object. + * + * @addr must point to a buffer that is the correct size for @address's family. + **/ +void +nm_ip_address_set_address_binary (NMIPAddress *address, + gconstpointer addr) +{ + char string[NM_UTILS_INET_ADDRSTRLEN]; + + g_return_if_fail (address != NULL); + g_return_if_fail (addr != NULL); + + g_free (address->address); + address->address = g_strdup (inet_ntop (address->family, addr, string, sizeof (string))); +} + +/** + * nm_ip_address_get_prefix: + * @address: the #NMIPAddress + * + * Gets the IP address prefix (ie "24" or "30" etc) property of this address + * object. + * + * Returns: the IP address prefix + **/ +guint +nm_ip_address_get_prefix (NMIPAddress *address) +{ + g_return_val_if_fail (address != NULL, 0); + g_return_val_if_fail (address->refcount > 0, 0); + + return address->prefix; +} + +/** + * nm_ip_address_set_prefix: + * @address: the #NMIPAddress + * @prefix: the IP address prefix + * + * Sets the IP address prefix property of this address object. + **/ +void +nm_ip_address_set_prefix (NMIPAddress *address, + guint prefix) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (valid_prefix (address->family, prefix, NULL)); + + address->prefix = prefix; +} + +/** + * nm_ip_address_get_gateway: + * @address: the #NMIPAddress + * + * Gets the gateway property of this address object; this will be %NULL if the + * address has no associated gateway. + * + * Returns: the gateway + **/ +const char * +nm_ip_address_get_gateway (NMIPAddress *address) +{ + g_return_val_if_fail (address != NULL, NULL); + g_return_val_if_fail (address->refcount > 0, NULL); + + return address->gateway; +} + +/** + * nm_ip_address_set_gateway: + * @address: the #NMIPAddress + * @gateway: (allow-none): the gateway, as a string + * + * Sets the gateway property of this address object. + * + * @gateway (if non-%NULL) must be a valid address of @address's family. If you + * aren't sure you have a valid address, use nm_utils_ipaddr_valid() to check + * it. + **/ +void +nm_ip_address_set_gateway (NMIPAddress *address, + const char *gateway) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (!gateway || nm_utils_ipaddr_valid (address->family, gateway)); + + g_free (address->gateway); + address->gateway = canonicalize_ip (address->family, gateway, TRUE); +} + + +G_DEFINE_BOXED_TYPE (NMIPRoute, nm_ip_route, nm_ip_route_dup, nm_ip_route_unref) + +struct NMIPRoute { + guint refcount; + + int family; + char *dest; + guint prefix; + char *next_hop; + guint32 metric; +}; + +/** + * nm_ip_route_new: + * @family: the IP address family (%AF_INET or %AF_INET6) + * @dest: the IP address of the route's destination + * @prefix: the address prefix length + * @next_hop: (allow-none): the IP address of the next hop (or %NULL) + * @metric: the route metric (or 0 for "default") + * @error: location to store error, or %NULL + * + * Creates a new #NMIPRoute object. + * + * Returns: (transfer full): the new #NMIPRoute object, or %NULL on error + **/ +NMIPRoute * +nm_ip_route_new (int family, + const char *dest, + guint prefix, + const char *next_hop, + guint metric, + GError **error) +{ + NMIPRoute *route; + + g_return_val_if_fail (family == AF_INET || family == AF_INET6, NULL); + + if (!valid_ip (family, dest, error)) + return NULL; + if (!valid_prefix (family, prefix, error)) + return NULL; + if (next_hop && !valid_ip (family, next_hop, error)) + return NULL; + + route = g_slice_new0 (NMIPRoute); + route->refcount = 1; + + route->family = family; + route->dest = canonicalize_ip (family, dest, FALSE); + route->prefix = prefix; + route->next_hop = canonicalize_ip (family, next_hop, TRUE); + route->metric = metric; + + return route; +} + +/** + * nm_ip_route_new_binary: + * @family: the IP address family (%AF_INET or %AF_INET6) + * @dest: the IP address of the route's destination + * @prefix: the address prefix length + * @next_hop: (allow-none): the IP address of the next hop (or %NULL) + * @metric: the route metric (or 0 for "default") + * @error: location to store error, or %NULL + * + * Creates a new #NMIPRoute object. @dest and @next_hop (if non-%NULL) must + * point to buffers of the correct size for @family. + * + * Returns: (transfer full): the new #NMIPRoute object, or %NULL on error + **/ +NMIPRoute * +nm_ip_route_new_binary (int family, + gconstpointer dest, + guint prefix, + gconstpointer next_hop, + guint metric, + GError **error) +{ + NMIPRoute *route; + char string[NM_UTILS_INET_ADDRSTRLEN]; + + g_return_val_if_fail (family == AF_INET || family == AF_INET6, NULL); + + if (!valid_prefix (family, prefix, error)) + return NULL; + + route = g_slice_new0 (NMIPRoute); + route->refcount = 1; + + route->family = family; + route->dest = g_strdup (inet_ntop (family, dest, string, sizeof (string))); + route->prefix = prefix; + if (next_hop) + route->next_hop = g_strdup (inet_ntop (family, next_hop, string, sizeof (string))); + route->metric = metric; + + return route; +} + +/** + * nm_ip_route_ref: + * @route: the #NMIPRoute + * + * Increases the reference count of the object. + **/ +void +nm_ip_route_ref (NMIPRoute *route) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (route->refcount > 0); + + route->refcount++; +} + +/** + * nm_ip_route_unref: + * @route: the #NMIPRoute + * + * Decreases the reference count of the object. If the reference count + * reaches zero, the object will be destroyed. + **/ +void +nm_ip_route_unref (NMIPRoute *route) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (route->refcount > 0); + + route->refcount--; + if (route->refcount == 0) { + g_free (route->dest); + g_free (route->next_hop); + g_slice_free (NMIPRoute, route); + } +} + +/** + * nm_ip_route_equal: + * @route: the #NMIPRoute + * @other: the #NMIPRoute to compare @route to. + * + * Determines if two #NMIPRoute objects contain the same values. + * + * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. + **/ +gboolean +nm_ip_route_equal (NMIPRoute *route, NMIPRoute *other) +{ + g_return_val_if_fail (route != NULL, FALSE); + g_return_val_if_fail (route->refcount > 0, FALSE); + + g_return_val_if_fail (other != NULL, FALSE); + g_return_val_if_fail (other->refcount > 0, FALSE); + + if ( route->prefix != other->prefix + || route->metric != other->metric + || strcmp (route->dest, other->dest) != 0 + || g_strcmp0 (route->next_hop, other->next_hop) != 0) + return FALSE; + return TRUE; +} + +/** + * nm_ip_route_dup: + * @route: the #NMIPRoute + * + * Creates a copy of @route + * + * Returns: (transfer full): a copy of @route + **/ +NMIPRoute * +nm_ip_route_dup (NMIPRoute *route) +{ + NMIPRoute *copy; + + g_return_val_if_fail (route != NULL, NULL); + g_return_val_if_fail (route->refcount > 0, NULL); + + copy = nm_ip_route_new (route->family, + route->dest, route->prefix, + route->next_hop, route->metric, + NULL); + return copy; +} + +/** + * nm_ip_route_get_family: + * @route: the #NMIPRoute + * + * Gets the IP address family (eg, AF_INET) property of this route + * object. + * + * Returns: the IP address family + **/ +int +nm_ip_route_get_family (NMIPRoute *route) +{ + g_return_val_if_fail (route != NULL, 0); + g_return_val_if_fail (route->refcount > 0, 0); + + return route->family; +} + +/** + * nm_ip_route_get_dest: + * @route: the #NMIPRoute + * + * Gets the IP destination address property of this route object. + * + * Returns: the IP address of the route's destination + **/ +const char * +nm_ip_route_get_dest (NMIPRoute *route) +{ + g_return_val_if_fail (route != NULL, NULL); + g_return_val_if_fail (route->refcount > 0, NULL); + + return route->dest; +} + +/** + * nm_ip_route_set_dest: + * @route: the #NMIPRoute + * @dest: the route's destination, as a string + * + * Sets the destination property of this route object. + * + * @dest must be a valid address of @route's family. If you aren't sure you + * have a valid address, use nm_utils_ipaddr_valid() to check it. + **/ +void +nm_ip_route_set_dest (NMIPRoute *route, + const char *dest) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (dest != NULL); + g_return_if_fail (nm_utils_ipaddr_valid (route->family, dest)); + + g_free (route->dest); + route->dest = canonicalize_ip (route->family, dest, FALSE); +} + +/** + * nm_ip_route_get_dest_binary: (skip) + * @route: the #NMIPRoute + * @dest: a buffer in which to store the destination in binary format. + * + * Gets the destination property of this route object. + * + * @dest must point to a buffer that is the correct size for @route's family. + **/ +void +nm_ip_route_get_dest_binary (NMIPRoute *route, + gpointer dest) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (dest != NULL); + + inet_pton (route->family, route->dest, dest); +} + +/** + * nm_ip_route_set_dest_binary: (skip) + * @route: the #NMIPRoute + * @dest: the route's destination, in binary format + * + * Sets the destination property of this route object. + * + * @dest must point to a buffer that is the correct size for @route's family. + **/ +void +nm_ip_route_set_dest_binary (NMIPRoute *route, + gconstpointer dest) +{ + char string[NM_UTILS_INET_ADDRSTRLEN]; + + g_return_if_fail (route != NULL); + g_return_if_fail (dest != NULL); + + g_free (route->dest); + route->dest = g_strdup (inet_ntop (route->family, dest, string, sizeof (string))); +} + +/** + * nm_ip_route_get_prefix: + * @route: the #NMIPRoute + * + * Gets the IP prefix (ie "24" or "30" etc) of this route. + * + * Returns: the IP prefix + **/ +guint +nm_ip_route_get_prefix (NMIPRoute *route) +{ + g_return_val_if_fail (route != NULL, 0); + g_return_val_if_fail (route->refcount > 0, 0); + + return route->prefix; +} + +/** + * nm_ip_route_set_prefix: + * @route: the #NMIPRoute + * @prefix: the route prefix + * + * Sets the prefix property of this route object. + **/ +void +nm_ip_route_set_prefix (NMIPRoute *route, + guint prefix) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (valid_prefix (route->family, prefix, NULL)); + + route->prefix = prefix; +} + +/** + * nm_ip_route_get_next_hop: + * @route: the #NMIPRoute + * + * Gets the IP address of the next hop of this route; this will be %NULL if the + * route has no next hop. + * + * Returns: the IP address of the next hop, or %NULL if this is a device route. + **/ +const char * +nm_ip_route_get_next_hop (NMIPRoute *route) +{ + g_return_val_if_fail (route != NULL, NULL); + g_return_val_if_fail (route->refcount > 0, NULL); + + return route->next_hop; +} + +/** + * nm_ip_route_set_next_hop: + * @route: the #NMIPRoute + * @next_hop: (allow-none): the route's next hop, as a string + * + * Sets the next-hop property of this route object. + * + * @next_hop (if non-%NULL) must be a valid address of @route's family. If you + * aren't sure you have a valid address, use nm_utils_ipaddr_valid() to check + * it. + **/ +void +nm_ip_route_set_next_hop (NMIPRoute *route, + const char *next_hop) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (!next_hop || nm_utils_ipaddr_valid (route->family, next_hop)); + + g_free (route->next_hop); + route->next_hop = canonicalize_ip (route->family, next_hop, TRUE); +} + +/** + * nm_ip_route_get_next_hop_binary: (skip) + * @route: the #NMIPRoute + * @next_hop: a buffer in which to store the next hop in binary format. + * + * Gets the next hop property of this route object. + * + * @next_hop must point to a buffer that is the correct size for @route's family. + * + * Returns: %TRUE if @route has a next hop, %FALSE if not (in which case + * @next_hop will be zeroed out) + **/ +gboolean +nm_ip_route_get_next_hop_binary (NMIPRoute *route, + gpointer next_hop) +{ + g_return_val_if_fail (route != NULL, FALSE); + g_return_val_if_fail (next_hop != NULL, FALSE); + + if (route->next_hop) { + inet_pton (route->family, route->next_hop, next_hop); + return TRUE; + } else { + memset (next_hop, 0, + route->family == AF_INET ? sizeof (struct in_addr) : sizeof (struct in6_addr)); + return FALSE; + } +} + +/** + * nm_ip_route_set_next_hop_binary: (skip) + * @route: the #NMIPRoute + * @next_hop: the route's next hop, in binary format + * + * Sets the destination property of this route object. + * + * @next_hop (if non-%NULL) must point to a buffer that is the correct size for + * @route's family. + **/ +void +nm_ip_route_set_next_hop_binary (NMIPRoute *route, + gconstpointer next_hop) +{ + char string[NM_UTILS_INET_ADDRSTRLEN]; + + g_return_if_fail (route != NULL); + + g_free (route->next_hop); + if (next_hop) + route->next_hop = g_strdup (inet_ntop (route->family, next_hop, string, sizeof (string))); + else + route->next_hop = NULL; +} + +/** + * nm_ip_route_get_metric: + * @route: the #NMIPRoute + * + * Gets the route metric property of this route object; lower values + * indicate "better" or more preferred routes; 0 indicates "default" + * (meaning NetworkManager will set it appropriately). + * + * Returns: the route metric + **/ +guint32 +nm_ip_route_get_metric (NMIPRoute *route) +{ + g_return_val_if_fail (route != NULL, 0); + g_return_val_if_fail (route->refcount > 0, 0); + + return route->metric; +} + +/** + * nm_ip_route_set_metric: + * @route: the #NMIPRoute + * @metric: the route metric + * + * Sets the metric property of this route object. + **/ +void +nm_ip_route_set_metric (NMIPRoute *route, + guint32 metric) +{ + g_return_if_fail (route != NULL); + + route->metric = metric; +} diff --git a/libnm-core/nm-setting-ip-config.h b/libnm-core/nm-setting-ip-config.h new file mode 100644 index 0000000000..d282935083 --- /dev/null +++ b/libnm-core/nm-setting-ip-config.h @@ -0,0 +1,117 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2007 - 2014 Red Hat, Inc. + * Copyright 2007 - 2008 Novell, Inc. + */ + +#ifndef NM_SETTING_IP_CONFIG_H +#define NM_SETTING_IP_CONFIG_H + +#if !defined (__NETWORKMANAGER_H_INSIDE__) && !defined (NETWORKMANAGER_COMPILATION) +#error "Only can be included directly." +#endif + +#include "nm-setting.h" + +G_BEGIN_DECLS + +typedef struct NMIPAddress NMIPAddress; + +GType nm_ip_address_get_type (void); + +NMIPAddress *nm_ip_address_new (int family, + const char *addr, + guint prefix, + const char *gateway, + GError **error); +NMIPAddress *nm_ip_address_new_binary (int family, + gconstpointer addr, + guint prefix, + gconstpointer gateway, + GError **error); + +void nm_ip_address_ref (NMIPAddress *address); +void nm_ip_address_unref (NMIPAddress *address); +gboolean nm_ip_address_equal (NMIPAddress *address, + NMIPAddress *other); +NMIPAddress *nm_ip_address_dup (NMIPAddress *address); + +int nm_ip_address_get_family (NMIPAddress *address); +const char *nm_ip_address_get_address (NMIPAddress *address); +void nm_ip_address_set_address (NMIPAddress *address, + const char *addr); +void nm_ip_address_get_address_binary (NMIPAddress *address, + gpointer addr); +void nm_ip_address_set_address_binary (NMIPAddress *address, + gconstpointer addr); +guint nm_ip_address_get_prefix (NMIPAddress *address); +void nm_ip_address_set_prefix (NMIPAddress *address, + guint prefix); +const char *nm_ip_address_get_gateway (NMIPAddress *address); +void nm_ip_address_set_gateway (NMIPAddress *address, + const char *gateway); + +typedef struct NMIPRoute NMIPRoute; + +GType nm_ip_route_get_type (void); + +NMIPRoute *nm_ip_route_new (int family, + const char *dest, + guint prefix, + const char *next_hop, + guint metric, + GError **error); +NMIPRoute *nm_ip_route_new_binary (int family, + gconstpointer dest, + guint prefix, + gconstpointer next_hop, + guint metric, + GError **error); + +void nm_ip_route_ref (NMIPRoute *route); +void nm_ip_route_unref (NMIPRoute *route); +gboolean nm_ip_route_equal (NMIPRoute *route, + NMIPRoute *other); +NMIPRoute *nm_ip_route_dup (NMIPRoute *route); + +int nm_ip_route_get_family (NMIPRoute *route); +const char *nm_ip_route_get_dest (NMIPRoute *route); +void nm_ip_route_set_dest (NMIPRoute *route, + const char *dest); +void nm_ip_route_get_dest_binary (NMIPRoute *route, + gpointer dest); +void nm_ip_route_set_dest_binary (NMIPRoute *route, + gconstpointer dest); +guint nm_ip_route_get_prefix (NMIPRoute *route); +void nm_ip_route_set_prefix (NMIPRoute *route, + guint prefix); +const char *nm_ip_route_get_next_hop (NMIPRoute *route); +void nm_ip_route_set_next_hop (NMIPRoute *route, + const char *next_hop); +gboolean nm_ip_route_get_next_hop_binary (NMIPRoute *route, + gpointer next_hop); +void nm_ip_route_set_next_hop_binary (NMIPRoute *route, + gconstpointer next_hop); +guint32 nm_ip_route_get_metric (NMIPRoute *route); +void nm_ip_route_set_metric (NMIPRoute *route, + guint32 metric); + +G_END_DECLS + +#endif /* NM_SETTING_IP_CONFIG_H */ diff --git a/libnm-core/nm-setting-ip4-config.c b/libnm-core/nm-setting-ip4-config.c index 00f2df502e..2cc00093f9 100644 --- a/libnm-core/nm-setting-ip4-config.c +++ b/libnm-core/nm-setting-ip4-config.c @@ -39,9 +39,6 @@ * properties related to IPv4 addressing, routing, and Domain Name Service **/ -G_DEFINE_BOXED_TYPE (NMIP4Address, nm_ip4_address, nm_ip4_address_dup, nm_ip4_address_unref) -G_DEFINE_BOXED_TYPE (NMIP4Route, nm_ip4_route, nm_ip4_route_dup, nm_ip4_route_unref) - G_DEFINE_TYPE_WITH_CODE (NMSettingIP4Config, nm_setting_ip4_config, NM_TYPE_SETTING, _nm_register_setting (IP4_CONFIG, 4)) NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP4_CONFIG) @@ -52,9 +49,9 @@ typedef struct { char *method; GSList *dns; /* list of IP address strings */ GSList *dns_search; /* list of strings */ - GSList *addresses; /* array of NMIP4Address */ + GSList *addresses; /* array of NMIPAddress */ GSList *address_labels; /* list of strings */ - GSList *routes; /* array of NMIP4Route */ + GSList *routes; /* array of NMIPRoute */ gboolean ignore_auto_routes; gboolean ignore_auto_dns; char *dhcp_client_id; @@ -424,7 +421,7 @@ nm_setting_ip4_config_get_num_addresses (NMSettingIP4Config *setting) * * Returns: the address at index @i **/ -NMIP4Address * +NMIPAddress * nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i) { NMSettingIP4ConfigPrivate *priv; @@ -434,7 +431,7 @@ nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i) priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); g_return_val_if_fail (i < g_slist_length (priv->addresses), NULL); - return (NMIP4Address *) g_slist_nth_data (priv->addresses, i); + return (NMIPAddress *) g_slist_nth_data (priv->addresses, i); } const char * @@ -463,18 +460,18 @@ _nm_setting_ip4_config_get_address_label (NMSettingIP4Config *setting, guint32 i **/ gboolean nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, - NMIP4Address *address) + NMIPAddress *address) { return _nm_setting_ip4_config_add_address_with_label (setting, address, ""); } gboolean _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, - NMIP4Address *address, + NMIPAddress *address, const char *label) { NMSettingIP4ConfigPrivate *priv; - NMIP4Address *copy; + NMIPAddress *copy; GSList *iter; g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); @@ -483,11 +480,11 @@ _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { - if (nm_ip4_address_compare ((NMIP4Address *) iter->data, address)) + if (nm_ip_address_equal ((NMIPAddress *) iter->data, address)) return FALSE; } - copy = nm_ip4_address_dup (address); + copy = nm_ip_address_dup (address); priv->addresses = g_slist_append (priv->addresses, copy); priv->address_labels = g_slist_append (priv->address_labels, g_strdup (label)); @@ -515,7 +512,7 @@ nm_setting_ip4_config_remove_address (NMSettingIP4Config *setting, guint32 i) label = g_slist_nth (priv->address_labels, i); g_return_if_fail (addr != NULL && label != NULL); - nm_ip4_address_unref ((NMIP4Address *) addr->data); + nm_ip_address_unref ((NMIPAddress *) addr->data); priv->addresses = g_slist_delete_link (priv->addresses, addr); g_free (label->data); priv->address_labels = g_slist_delete_link (priv->address_labels, label); @@ -534,7 +531,7 @@ nm_setting_ip4_config_remove_address (NMSettingIP4Config *setting, guint32 i) **/ gboolean nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting, - NMIP4Address *address) + NMIPAddress *address) { NMSettingIP4ConfigPrivate *priv; GSList *iter; @@ -544,8 +541,8 @@ nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting, priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { - if (nm_ip4_address_compare ((NMIP4Address *) iter->data, address)) { - nm_ip4_address_unref ((NMIP4Address *) iter->data); + if (nm_ip_address_equal ((NMIPAddress *) iter->data, address)) { + nm_ip_address_unref ((NMIPAddress *) iter->data); priv->addresses = g_slist_delete_link (priv->addresses, iter); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES); return TRUE; @@ -567,7 +564,7 @@ nm_setting_ip4_config_clear_addresses (NMSettingIP4Config *setting) g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref); + g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); priv->addresses = NULL; g_slist_free_full (priv->address_labels, g_free); priv->address_labels = NULL; @@ -595,7 +592,7 @@ nm_setting_ip4_config_get_num_routes (NMSettingIP4Config *setting) * * Returns: the route at index @i **/ -NMIP4Route * +NMIPRoute * nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i) { NMSettingIP4ConfigPrivate *priv; @@ -605,7 +602,7 @@ nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i) priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); g_return_val_if_fail (i < g_slist_length (priv->routes), NULL); - return (NMIP4Route *) g_slist_nth_data (priv->routes, i); + return (NMIPRoute *) g_slist_nth_data (priv->routes, i); } /** @@ -620,10 +617,10 @@ nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i) **/ gboolean nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, - NMIP4Route *route) + NMIPRoute *route) { NMSettingIP4ConfigPrivate *priv; - NMIP4Route *copy; + NMIPRoute *copy; GSList *iter; g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); @@ -631,11 +628,11 @@ nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); for (iter = priv->routes; iter; iter = g_slist_next (iter)) { - if (nm_ip4_route_compare ((NMIP4Route *) iter->data, route)) + if (nm_ip_route_equal (iter->data, route)) return FALSE; } - copy = nm_ip4_route_dup (route); + copy = nm_ip_route_dup (route); priv->routes = g_slist_append (priv->routes, copy); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); return TRUE; @@ -660,7 +657,7 @@ nm_setting_ip4_config_remove_route (NMSettingIP4Config *setting, guint32 i) elt = g_slist_nth (priv->routes, i); g_return_if_fail (elt != NULL); - nm_ip4_route_unref ((NMIP4Route *) elt->data); + nm_ip_route_unref ((NMIPRoute *) elt->data); priv->routes = g_slist_delete_link (priv->routes, elt); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); } @@ -676,7 +673,7 @@ nm_setting_ip4_config_remove_route (NMSettingIP4Config *setting, guint32 i) **/ gboolean nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting, - NMIP4Route *route) + NMIPRoute *route) { NMSettingIP4ConfigPrivate *priv; GSList *iter; @@ -686,8 +683,8 @@ nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting, priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); for (iter = priv->routes; iter; iter = g_slist_next (iter)) { - if (nm_ip4_route_compare ((NMIP4Route *) iter->data, route)) { - nm_ip4_route_unref ((NMIP4Route *) iter->data); + if (nm_ip_route_equal ((NMIPRoute *) iter->data, route)) { + nm_ip_route_unref ((NMIPRoute *) iter->data); priv->routes = g_slist_delete_link (priv->routes, iter); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); return TRUE; @@ -709,7 +706,7 @@ nm_setting_ip4_config_clear_routes (NMSettingIP4Config *setting) g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); priv->routes = NULL; g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); } @@ -871,7 +868,7 @@ static gboolean verify (NMSetting *setting, NMConnection *connection, GError **error) { NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - GSList *iter, *l_iter; + GSList *iter; int i; if (!priv->method) { @@ -957,33 +954,9 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) return FALSE; } - /* Validate addresses */ - for (iter = priv->addresses, l_iter = priv->address_labels, i = 0; - iter && l_iter; - iter = g_slist_next (iter), l_iter = g_slist_next (l_iter), i++) { - NMIP4Address *addr = (NMIP4Address *) iter->data; - const char *label = (const char *) l_iter->data; - guint32 prefix = nm_ip4_address_get_prefix (addr); - - if (!nm_ip4_address_get_address (addr)) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("%d. IPv4 address is invalid"), - i+1); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES); - return FALSE; - } - - if (!prefix || prefix > 32) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("%d. IPv4 address has invalid prefix"), - i+1); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES); - return FALSE; - } + /* Validate address labels */ + for (iter = priv->address_labels, i = 0; iter; iter = g_slist_next (iter), i++) { + const char *label = (const char *) iter->data; if (!verify_label (label)) { g_set_error (error, @@ -996,7 +969,7 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) } } - if (iter || l_iter) { + if (g_slist_length (priv->addresses) != g_slist_length (priv->address_labels)) { g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, @@ -1007,32 +980,6 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) return FALSE; } - /* Validate routes */ - for (iter = priv->routes, i = 0; iter; iter = g_slist_next (iter), i++) { - NMIP4Route *route = (NMIP4Route *) iter->data; - guint32 prefix = nm_ip4_route_get_prefix (route); - - if (!nm_ip4_route_get_dest (route)) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("%d. route is invalid"), - i+1); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ROUTES); - return FALSE; - } - - if (!prefix || prefix > 32) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("%d. route has invalid prefix"), - i+1); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ROUTES); - return FALSE; - } - } - /* Validate DNS */ for (iter = priv->dns, i = 0; iter; iter = g_slist_next (iter), i++) { const char *dns = (const char *) iter->data; @@ -1070,9 +1017,9 @@ finalize (GObject *object) g_slist_free_full (priv->dns, g_free); g_slist_free_full (priv->dns_search, g_free); - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref); + g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); g_slist_free_full (priv->address_labels, g_free); - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); G_OBJECT_CLASS (nm_setting_ip4_config_parent_class)->finalize (object); } @@ -1138,9 +1085,9 @@ set_property (GObject *object, guint prop_id, priv->dns_search = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_ADDRESSES: - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip4_address_unref); + g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); priv->addresses = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), - (NMUtilsCopyFunc) nm_ip4_address_dup); + (NMUtilsCopyFunc) nm_ip_address_dup); if (g_slist_length (priv->addresses) != g_slist_length (priv->address_labels)) { g_slist_free_full (priv->address_labels, g_free); @@ -1154,9 +1101,9 @@ set_property (GObject *object, guint prop_id, priv->address_labels = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_ROUTES: - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); priv->routes = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), - (NMUtilsCopyFunc) nm_ip4_route_dup); + (NMUtilsCopyFunc) nm_ip_route_dup); break; case PROP_IGNORE_AUTO_ROUTES: priv->ignore_auto_routes = g_value_get_boolean (value); @@ -1205,13 +1152,13 @@ get_property (GObject *object, guint prop_id, g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns_search)); break; case PROP_ADDRESSES: - g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip4_address_dup, (GDestroyNotify) nm_ip4_address_unref)); + g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip_address_dup, (GDestroyNotify) nm_ip_address_unref)); break; case PROP_ADDRESS_LABELS: g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->address_labels)); break; case PROP_ROUTES: - g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip4_route_dup, (GDestroyNotify) nm_ip4_route_unref)); + g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip_route_dup, (GDestroyNotify) nm_ip_route_unref)); break; case PROP_IGNORE_AUTO_ROUTES: g_value_set_boolean (value, nm_setting_ip4_config_get_ignore_auto_routes (setting)); @@ -1325,7 +1272,7 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) * with the "shared", "link-local", or "disabled" methods as addressing is * either automatic or disabled with these methods. * - * Element-Type: NMIP4Address + * Element-Type: NMIPAddress **/ g_object_class_install_property (object_class, PROP_ADDRESSES, @@ -1360,7 +1307,7 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) * the 'shared', 'link-local', or 'disabled' methods because there is no * upstream network. * - * Element-Type: NMIP4Route + * Element-Type: NMIPRoute **/ g_object_class_install_property (object_class, PROP_ROUTES, @@ -1482,463 +1429,3 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS)); } - - -struct NMIP4Address { - guint32 refcount; - guint32 address; /* network byte order */ - guint32 prefix; - guint32 gateway; /* network byte order */ -}; - -/** - * nm_ip4_address_new: - * - * Creates and returns a new #NMIP4Address object. - * - * Returns: (transfer full): the new empty #NMIP4Address object - **/ -NMIP4Address * -nm_ip4_address_new (void) -{ - NMIP4Address *address; - - address = g_malloc0 (sizeof (NMIP4Address)); - address->refcount = 1; - return address; -} - -/** - * nm_ip4_address_dup: - * @source: the #NMIP4Address object to copy - * - * Copies a given #NMIP4Address object and returns the copy. - * - * Returns: (transfer full): the copy of the given #NMIP4Address copy - **/ -NMIP4Address * -nm_ip4_address_dup (NMIP4Address *source) -{ - NMIP4Address *address; - - g_return_val_if_fail (source != NULL, NULL); - g_return_val_if_fail (source->refcount > 0, NULL); - - address = nm_ip4_address_new (); - address->address = source->address; - address->prefix = source->prefix; - address->gateway = source->gateway; - - return address; -} - -/** - * nm_ip4_address_ref: - * @address: the #NMIP4Address - * - * Increases the reference count of the object. - **/ -void -nm_ip4_address_ref (NMIP4Address *address) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - - address->refcount++; -} - -/** - * nm_ip4_address_unref: - * @address: the #NMIP4Address - * - * Decreases the reference count of the object. If the reference count - * reaches zero, the object will be destroyed. - **/ -void -nm_ip4_address_unref (NMIP4Address *address) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - - address->refcount--; - if (address->refcount == 0) { - memset (address, 0, sizeof (NMIP4Address)); - g_free (address); - } -} - -/** - * nm_ip4_address_compare: - * @address: the #NMIP4Address - * @other: the #NMIP4Address to compare @address to. - * - * Determines if two #NMIP4Address objects contain the same values. - * - * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. - **/ -gboolean -nm_ip4_address_compare (NMIP4Address *address, NMIP4Address *other) -{ - g_return_val_if_fail (address != NULL, FALSE); - g_return_val_if_fail (address->refcount > 0, FALSE); - - g_return_val_if_fail (other != NULL, FALSE); - g_return_val_if_fail (other->refcount > 0, FALSE); - - if ( address->address != other->address - || address->prefix != other->prefix - || address->gateway != other->gateway) - return FALSE; - return TRUE; -} - -/** - * nm_ip4_address_get_address: - * @address: the #NMIP4Address - * - * Gets the IPv4 address property of this address object. - * - * Returns: the IPv4 address in network byte order - **/ -guint32 -nm_ip4_address_get_address (NMIP4Address *address) -{ - g_return_val_if_fail (address != NULL, 0); - g_return_val_if_fail (address->refcount > 0, 0); - - return address->address; -} - -/** - * nm_ip4_address_set_address: - * @address: the #NMIP4Address - * @addr: the IPv4 address in network byte order - * - * Sets the IPv4 address property of this object. - **/ -void -nm_ip4_address_set_address (NMIP4Address *address, guint32 addr) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - - address->address = addr; -} - -/** - * nm_ip4_address_get_prefix: - * @address: the #NMIP4Address - * - * Gets the IPv4 address prefix (ie "24" or "30" etc) property of this address - * object. - * - * Returns: the IPv4 address prefix - **/ -guint32 -nm_ip4_address_get_prefix (NMIP4Address *address) -{ - g_return_val_if_fail (address != NULL, 0); - g_return_val_if_fail (address->refcount > 0, 0); - - return address->prefix; -} - -/** - * nm_ip4_address_set_prefix: - * @address: the #NMIP4Address - * @prefix: the address prefix, a number between 1 and 32 inclusive - * - * Sets the IPv4 address prefix. - **/ -void -nm_ip4_address_set_prefix (NMIP4Address *address, guint32 prefix) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - g_return_if_fail (prefix <= 32); - g_return_if_fail (prefix > 0); - - address->prefix = prefix; -} - -/** - * nm_ip4_address_get_gateway: - * @address: the #NMIP4Address - * - * Gets the IPv4 default gateway property of this address object. - * - * Returns: the IPv4 gateway address in network byte order - **/ -guint32 -nm_ip4_address_get_gateway (NMIP4Address *address) -{ - g_return_val_if_fail (address != NULL, 0); - g_return_val_if_fail (address->refcount > 0, 0); - - return address->gateway; -} - -/** - * nm_ip4_address_set_gateway: - * @address: the #NMIP4Address - * @gateway: the IPv4 default gateway in network byte order - * - * Sets the IPv4 default gateway property of this address object. - **/ -void -nm_ip4_address_set_gateway (NMIP4Address *address, guint32 gateway) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - - address->gateway = gateway; -} - - -struct NMIP4Route { - guint32 refcount; - - guint32 dest; /* network byte order */ - guint32 prefix; - guint32 next_hop; /* network byte order */ - guint32 metric; /* lower metric == more preferred */ -}; - -/** - * nm_ip4_route_new: - * - * Creates and returns a new #NMIP4Route object. - * - * Returns: (transfer full): the new empty #NMIP4Route object - **/ -NMIP4Route * -nm_ip4_route_new (void) -{ - NMIP4Route *route; - - route = g_malloc0 (sizeof (NMIP4Route)); - route->refcount = 1; - return route; -} - -/** - * nm_ip4_route_dup: - * @source: the #NMIP4Route object to copy - * - * Copies a given #NMIP4Route object and returns the copy. - * - * Returns: (transfer full): the copy of the given #NMIP4Route copy - **/ -NMIP4Route * -nm_ip4_route_dup (NMIP4Route *source) -{ - NMIP4Route *route; - - g_return_val_if_fail (source != NULL, NULL); - g_return_val_if_fail (source->refcount > 0, NULL); - - route = nm_ip4_route_new (); - route->dest = source->dest; - route->prefix = source->prefix; - route->next_hop = source->next_hop; - route->metric = source->metric; - - return route; -} - -/** - * nm_ip4_route_ref: - * @route: the #NMIP4Route - * - * Increases the reference count of the object. - **/ -void -nm_ip4_route_ref (NMIP4Route *route) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->refcount++; -} - -/** - * nm_ip4_route_unref: - * @route: the #NMIP4Route - * - * Decreases the reference count of the object. If the reference count - * reaches zero, the object will be destroyed. - **/ -void -nm_ip4_route_unref (NMIP4Route *route) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->refcount--; - if (route->refcount == 0) { - memset (route, 0, sizeof (NMIP4Route)); - g_free (route); - } -} - -/** - * nm_ip4_route_compare: - * @route: the #NMIP4Route - * @other: the #NMIP4Route to compare @route to. - * - * Determines if two #NMIP4Route objects contain the same values. - * - * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. - **/ -gboolean -nm_ip4_route_compare (NMIP4Route *route, NMIP4Route *other) -{ - g_return_val_if_fail (route != NULL, FALSE); - g_return_val_if_fail (route->refcount > 0, FALSE); - - g_return_val_if_fail (other != NULL, FALSE); - g_return_val_if_fail (other->refcount > 0, FALSE); - - if ( route->dest != other->dest - || route->prefix != other->prefix - || route->next_hop != other->next_hop - || route->metric != other->metric) - return FALSE; - return TRUE; -} - -/** - * nm_ip4_route_get_dest: - * @route: the #NMIP4Route - * - * Gets the IPv4 destination address property of this route object. - * - * Returns: the IPv4 address in network byte order - **/ -guint32 -nm_ip4_route_get_dest (NMIP4Route *route) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->refcount > 0, 0); - - return route->dest; -} - -/** - * nm_ip4_route_set_dest: - * @route: the #NMIP4Route - * @dest: the destination address in network byte order - * - * Sets the IPv4 destination address property of this route object. - **/ -void -nm_ip4_route_set_dest (NMIP4Route *route, guint32 dest) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->dest = dest; -} - -/** - * nm_ip4_route_get_prefix: - * @route: the #NMIP4Route - * - * Gets the IPv4 prefix (ie "24" or "30" etc) of this route. - * - * Returns: the IPv4 prefix - **/ -guint32 -nm_ip4_route_get_prefix (NMIP4Route *route) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->refcount > 0, 0); - - return route->prefix; -} - -/** - * nm_ip4_route_set_prefix: - * @route: the #NMIP4Route - * @prefix: the prefix, a number between 1 and 32 inclusive - * - * Sets the IPv4 prefix of this route. - **/ -void -nm_ip4_route_set_prefix (NMIP4Route *route, guint32 prefix) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - g_return_if_fail (prefix <= 32); - g_return_if_fail (prefix > 0); - - route->prefix = prefix; -} - -/** - * nm_ip4_route_get_next_hop: - * @route: the #NMIP4Route - * - * Gets the IPv4 address of the next hop of this route. - * - * Returns: the IPv4 address in network byte order - **/ -guint32 -nm_ip4_route_get_next_hop (NMIP4Route *route) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->refcount > 0, 0); - - return route->next_hop; -} - -/** - * nm_ip4_route_set_next_hop: - * @route: the #NMIP4Route - * @next_hop: the IPv4 address of the next hop in network byte order - * - * Sets the IPv4 address of the next hop of this route. - **/ -void -nm_ip4_route_set_next_hop (NMIP4Route *route, guint32 next_hop) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->next_hop = next_hop; -} - -/** - * nm_ip4_route_get_metric: - * @route: the #NMIP4Route - * - * Gets the route metric property of this route object; lower values indicate - * "better" or more preferred routes. - * - * Returns: the route metric - **/ -guint32 -nm_ip4_route_get_metric (NMIP4Route *route) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->refcount > 0, 0); - - return route->metric; -} - -/** - * nm_ip4_route_set_metric: - * @route: the #NMIP4Route - * @metric: the route metric - * - * Sets the route metric property of this route object; lower values indicate - * "better" or more preferred routes. - **/ -void -nm_ip4_route_set_metric (NMIP4Route *route, guint32 metric) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->metric = metric; -} diff --git a/libnm-core/nm-setting-ip4-config.h b/libnm-core/nm-setting-ip4-config.h index 369e1d46d2..f683becda5 100644 --- a/libnm-core/nm-setting-ip4-config.h +++ b/libnm-core/nm-setting-ip4-config.h @@ -28,6 +28,7 @@ #endif #include "nm-setting.h" +#include "nm-setting-ip-config.h" G_BEGIN_DECLS @@ -97,57 +98,6 @@ G_BEGIN_DECLS */ #define NM_SETTING_IP4_CONFIG_METHOD_DISABLED "disabled" -typedef struct NMIP4Address NMIP4Address; - -GType nm_ip4_address_get_type (void); - -NMIP4Address * nm_ip4_address_new (void); -NMIP4Address * nm_ip4_address_dup (NMIP4Address *source); -void nm_ip4_address_ref (NMIP4Address *address); -void nm_ip4_address_unref (NMIP4Address *address); -/* Return TRUE if addresses are identical */ -gboolean nm_ip4_address_compare (NMIP4Address *address, NMIP4Address *other); - -guint32 nm_ip4_address_get_address (NMIP4Address *address); -void nm_ip4_address_set_address (NMIP4Address *address, - guint32 addr); /* network byte order */ - -guint32 nm_ip4_address_get_prefix (NMIP4Address *address); -void nm_ip4_address_set_prefix (NMIP4Address *address, - guint32 prefix); - -guint32 nm_ip4_address_get_gateway (NMIP4Address *address); -void nm_ip4_address_set_gateway (NMIP4Address *address, - guint32 gateway); /* network byte order */ - -typedef struct NMIP4Route NMIP4Route; - -GType nm_ip4_route_get_type (void); - -NMIP4Route * nm_ip4_route_new (void); -NMIP4Route * nm_ip4_route_dup (NMIP4Route *source); -void nm_ip4_route_ref (NMIP4Route *route); -void nm_ip4_route_unref (NMIP4Route *route); -/* Return TRUE if routes are identical */ -gboolean nm_ip4_route_compare (NMIP4Route *route, NMIP4Route *other); - -guint32 nm_ip4_route_get_dest (NMIP4Route *route); -void nm_ip4_route_set_dest (NMIP4Route *route, - guint32 dest); /* network byte order */ - -guint32 nm_ip4_route_get_prefix (NMIP4Route *route); -void nm_ip4_route_set_prefix (NMIP4Route *route, - guint32 prefix); - -guint32 nm_ip4_route_get_next_hop (NMIP4Route *route); -void nm_ip4_route_set_next_hop (NMIP4Route *route, - guint32 next_hop); /* network byte order */ - -guint32 nm_ip4_route_get_metric (NMIP4Route *route); -void nm_ip4_route_set_metric (NMIP4Route *route, - guint32 metric); - - struct _NMSettingIP4Config { NMSetting parent; }; @@ -179,17 +129,17 @@ gboolean nm_setting_ip4_config_remove_dns_search_by_value (NMSettingIP4Conf void nm_setting_ip4_config_clear_dns_searches (NMSettingIP4Config *setting); guint32 nm_setting_ip4_config_get_num_addresses (NMSettingIP4Config *setting); -NMIP4Address *nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, NMIP4Address *address); +NMIPAddress * nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i); +gboolean nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, NMIPAddress *address); void nm_setting_ip4_config_remove_address (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting, NMIP4Address *address); +gboolean nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting, NMIPAddress *address); void nm_setting_ip4_config_clear_addresses (NMSettingIP4Config *setting); guint32 nm_setting_ip4_config_get_num_routes (NMSettingIP4Config *setting); -NMIP4Route * nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, NMIP4Route *route); +NMIPRoute * nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i); +gboolean nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, NMIPRoute *route); void nm_setting_ip4_config_remove_route (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting, NMIP4Route *route); +gboolean nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting, NMIPRoute *route); void nm_setting_ip4_config_clear_routes (NMSettingIP4Config *setting); gboolean nm_setting_ip4_config_get_ignore_auto_routes (NMSettingIP4Config *setting); diff --git a/libnm-core/nm-setting-ip6-config.c b/libnm-core/nm-setting-ip6-config.c index cbc5e2c8be..3ac82eb4e1 100644 --- a/libnm-core/nm-setting-ip6-config.c +++ b/libnm-core/nm-setting-ip6-config.c @@ -37,9 +37,6 @@ * properties related to IPv6 addressing, routing, and Domain Name Service **/ -G_DEFINE_BOXED_TYPE (NMIP6Address, nm_ip6_address, nm_ip6_address_dup, nm_ip6_address_unref) -G_DEFINE_BOXED_TYPE (NMIP6Route, nm_ip6_route, nm_ip6_route_dup, nm_ip6_route_unref) - G_DEFINE_TYPE_WITH_CODE (NMSettingIP6Config, nm_setting_ip6_config, NM_TYPE_SETTING, _nm_register_setting (IP6_CONFIG, 4)) NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP6_CONFIG) @@ -51,8 +48,8 @@ typedef struct { char *dhcp_hostname; GSList *dns; /* array of struct in6_addr */ GSList *dns_search; /* list of strings */ - GSList *addresses; /* array of NMIP6Address */ - GSList *routes; /* array of NMIP6Route */ + GSList *addresses; /* array of NMIPAddress */ + GSList *routes; /* array of NMIPRoute */ gboolean ignore_auto_routes; gboolean ignore_auto_dns; gboolean never_default; @@ -437,7 +434,7 @@ nm_setting_ip6_config_get_num_addresses (NMSettingIP6Config *setting) * * Returns: the address at index @i **/ -NMIP6Address * +NMIPAddress * nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i) { NMSettingIP6ConfigPrivate *priv; @@ -447,7 +444,7 @@ nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i) priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); g_return_val_if_fail (i < g_slist_length (priv->addresses), NULL); - return (NMIP6Address *) g_slist_nth_data (priv->addresses, i); + return (NMIPAddress *) g_slist_nth_data (priv->addresses, i); } /** @@ -463,10 +460,10 @@ nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i) **/ gboolean nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, - NMIP6Address *address) + NMIPAddress *address) { NMSettingIP6ConfigPrivate *priv; - NMIP6Address *copy; + NMIPAddress *copy; GSList *iter; g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); @@ -474,11 +471,11 @@ nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { - if (nm_ip6_address_compare ((NMIP6Address *) iter->data, address)) + if (nm_ip_address_equal ((NMIPAddress *) iter->data, address)) return FALSE; } - copy = nm_ip6_address_dup (address); + copy = nm_ip_address_dup (address); priv->addresses = g_slist_append (priv->addresses, copy); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); return TRUE; @@ -503,7 +500,7 @@ nm_setting_ip6_config_remove_address (NMSettingIP6Config *setting, guint32 i) elt = g_slist_nth (priv->addresses, i); g_return_if_fail (elt != NULL); - nm_ip6_address_unref ((NMIP6Address *) elt->data); + nm_ip_address_unref ((NMIPAddress *) elt->data); priv->addresses = g_slist_delete_link (priv->addresses, elt); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); } @@ -519,7 +516,7 @@ nm_setting_ip6_config_remove_address (NMSettingIP6Config *setting, guint32 i) **/ gboolean nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting, - NMIP6Address *address) + NMIPAddress *address) { NMSettingIP6ConfigPrivate *priv; GSList *iter; @@ -529,7 +526,7 @@ nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting, priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { - if (nm_ip6_address_compare ((NMIP6Address *) iter->data, address)) { + if (nm_ip_address_equal ((NMIPAddress *) iter->data, address)) { priv->addresses = g_slist_delete_link (priv->addresses, iter); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); return TRUE; @@ -551,7 +548,7 @@ nm_setting_ip6_config_clear_addresses (NMSettingIP6Config *setting) g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip6_address_unref); + g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); priv->addresses = NULL; g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); } @@ -577,7 +574,7 @@ nm_setting_ip6_config_get_num_routes (NMSettingIP6Config *setting) * * Returns: the route at index @i **/ -NMIP6Route * +NMIPRoute * nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i) { NMSettingIP6ConfigPrivate *priv; @@ -587,7 +584,7 @@ nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i) priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); g_return_val_if_fail (i < g_slist_length (priv->routes), NULL); - return (NMIP6Route *) g_slist_nth_data (priv->routes, i); + return (NMIPRoute *) g_slist_nth_data (priv->routes, i); } /** @@ -602,10 +599,10 @@ nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i) **/ gboolean nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, - NMIP6Route *route) + NMIPRoute *route) { NMSettingIP6ConfigPrivate *priv; - NMIP6Route *copy; + NMIPRoute *copy; GSList *iter; g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); @@ -613,11 +610,11 @@ nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); for (iter = priv->routes; iter; iter = g_slist_next (iter)) { - if (nm_ip6_route_compare ((NMIP6Route *) iter->data, route)) + if (nm_ip_route_equal ((NMIPRoute *) iter->data, route)) return FALSE; } - copy = nm_ip6_route_dup (route); + copy = nm_ip_route_dup (route); priv->routes = g_slist_append (priv->routes, copy); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); return TRUE; @@ -642,7 +639,7 @@ nm_setting_ip6_config_remove_route (NMSettingIP6Config *setting, guint32 i) elt = g_slist_nth (priv->routes, i); g_return_if_fail (elt != NULL); - nm_ip6_route_unref ((NMIP6Route *) elt->data); + nm_ip_route_unref ((NMIPRoute *) elt->data); priv->routes = g_slist_delete_link (priv->routes, elt); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); } @@ -658,7 +655,7 @@ nm_setting_ip6_config_remove_route (NMSettingIP6Config *setting, guint32 i) **/ gboolean nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting, - NMIP6Route *route) + NMIPRoute *route) { NMSettingIP6ConfigPrivate *priv; GSList *iter; @@ -668,8 +665,8 @@ nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting, priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); for (iter = priv->routes; iter; iter = g_slist_next (iter)) { - if (nm_ip6_route_compare ((NMIP6Route *) iter->data, route)) { - nm_ip6_route_unref ((NMIP6Route *) iter->data); + if (nm_ip_route_equal ((NMIPRoute *) iter->data, route)) { + nm_ip_route_unref ((NMIPRoute *) iter->data); priv->routes = g_slist_delete_link (priv->routes, iter); g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); return TRUE; @@ -691,7 +688,7 @@ nm_setting_ip6_config_clear_routes (NMSettingIP6Config *setting) g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_route_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); priv->routes = NULL; g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); } @@ -899,8 +896,8 @@ finalize (GObject *object) g_slist_free_full (priv->dns, g_free); g_slist_free_full (priv->dns_search, g_free); - g_slist_free_full (priv->addresses, g_free); - g_slist_free_full (priv->routes, g_free); + g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); G_OBJECT_CLASS (nm_setting_ip6_config_parent_class)->finalize (object); } @@ -964,14 +961,14 @@ set_property (GObject *object, guint prop_id, priv->dns_search = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_ADDRESSES: - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_address_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_address_unref); priv->addresses = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), - (NMUtilsCopyFunc) nm_ip6_address_dup); + (NMUtilsCopyFunc) nm_ip_address_dup); break; case PROP_ROUTES: - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip6_route_unref); + g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); priv->routes = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), - (NMUtilsCopyFunc) nm_ip6_route_dup); + (NMUtilsCopyFunc) nm_ip_route_dup); break; case PROP_IGNORE_AUTO_ROUTES: priv->ignore_auto_routes = g_value_get_boolean (value); @@ -1015,10 +1012,10 @@ get_property (GObject *object, guint prop_id, g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns_search)); break; case PROP_ADDRESSES: - g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip6_address_dup, (GDestroyNotify) nm_ip6_address_unref)); + g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip_address_dup, (GDestroyNotify) nm_ip_address_unref)); break; case PROP_ROUTES: - g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip6_route_dup, (GDestroyNotify) nm_ip6_route_unref)); + g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip_route_dup, (GDestroyNotify) nm_ip_route_unref)); break; case PROP_IGNORE_AUTO_ROUTES: g_value_set_boolean (value, priv->ignore_auto_routes); @@ -1139,7 +1136,7 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class) * be used with the 'shared' or 'link-local' methods as the interface is * automatically assigned an address with these methods. * - * Element-Type: NMIP6Address + * Element-Type: NMIPAddress **/ g_object_class_install_property (object_class, PROP_ADDRESSES, @@ -1160,7 +1157,7 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class) * to those returned by automatic configuration. Routes cannot be used with * the 'shared' or 'link-local' methods because there is no upstream network. * - * Element-Type: NMIP6Route + * Element-Type: NMIPRoute **/ g_object_class_install_property (object_class, PROP_ROUTES, @@ -1259,472 +1256,3 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class) G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS)); } - -/********************************************************************/ - -struct NMIP6Address { - guint32 refcount; - struct in6_addr address; - guint32 prefix; - struct in6_addr gateway; -}; - -/** - * nm_ip6_address_new: - * - * Creates and returns a new #NMIP6Address object. - * - * Returns: (transfer full): the new empty #NMIP6Address object - **/ -NMIP6Address * -nm_ip6_address_new (void) -{ - NMIP6Address *address; - - address = g_malloc0 (sizeof (NMIP6Address)); - address->refcount = 1; - return address; -} - -/** - * nm_ip6_address_dup: - * @source: the #NMIP6Address object to copy - * - * Copies a given #NMIP6Address object and returns the copy. - * - * Returns: (transfer full): the copy of the given #NMIP6Address copy - **/ -NMIP6Address * -nm_ip6_address_dup (NMIP6Address *source) -{ - NMIP6Address *address; - - g_return_val_if_fail (source != NULL, NULL); - g_return_val_if_fail (source->refcount > 0, NULL); - - address = nm_ip6_address_new (); - address->prefix = source->prefix; - memcpy (&address->address, &source->address, sizeof (struct in6_addr)); - memcpy (&address->gateway, &source->gateway, sizeof (struct in6_addr)); - - return address; -} - -/** - * nm_ip6_address_ref: - * @address: the #NMIP6Address - * - * Increases the reference count of the object. - **/ -void -nm_ip6_address_ref (NMIP6Address *address) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - - address->refcount++; -} - -/** - * nm_ip6_address_unref: - * @address: the #NMIP6Address - * - * Decreases the reference count of the object. If the reference count - * reaches zero, the object will be destroyed. - **/ -void -nm_ip6_address_unref (NMIP6Address *address) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - - address->refcount--; - if (address->refcount == 0) { - memset (address, 0, sizeof (NMIP6Address)); - g_free (address); - } -} - -/** - * nm_ip6_address_compare: - * @address: the #NMIP6Address - * @other: the #NMIP6Address to compare @address to. - * - * Determines if two #NMIP6Address objects contain the same values. - * - * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. - **/ -gboolean -nm_ip6_address_compare (NMIP6Address *address, NMIP6Address *other) -{ - g_return_val_if_fail (address != NULL, FALSE); - g_return_val_if_fail (address->refcount > 0, FALSE); - - g_return_val_if_fail (other != NULL, FALSE); - g_return_val_if_fail (other->refcount > 0, FALSE); - - if ( memcmp (&address->address, &other->address, sizeof (struct in6_addr)) - || address->prefix != other->prefix - || memcmp (&address->gateway, &other->gateway, sizeof (struct in6_addr))) - return FALSE; - return TRUE; -} - -/** - * nm_ip6_address_get_address: - * @address: the #NMIP6Address - * - * Gets the IPv6 address property of this address object. - * - * Returns: (array fixed-size=16) (element-type guint8) (transfer none): - * the IPv6 address - **/ -const struct in6_addr * -nm_ip6_address_get_address (NMIP6Address *address) -{ - g_return_val_if_fail (address != NULL, NULL); - g_return_val_if_fail (address->refcount > 0, NULL); - - return &address->address; -} - -/** - * nm_ip6_address_set_address: - * @address: the #NMIP6Address - * @addr: the IPv6 address - * - * Sets the IPv6 address property of this object. - **/ -void -nm_ip6_address_set_address (NMIP6Address *address, const struct in6_addr *addr) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - g_return_if_fail (addr != NULL); - - memcpy (&address->address, addr, sizeof (struct in6_addr)); -} - -/** - * nm_ip6_address_get_prefix: - * @address: the #NMIP6Address - * - * Gets the IPv6 address prefix property of this address object. - * - * Returns: the IPv6 address prefix - **/ -guint32 -nm_ip6_address_get_prefix (NMIP6Address *address) -{ - g_return_val_if_fail (address != NULL, 0); - g_return_val_if_fail (address->refcount > 0, 0); - - return address->prefix; -} - -/** - * nm_ip6_address_set_prefix: - * @address: the #NMIP6Address - * @prefix: the address prefix, a number between 0 and 128 inclusive - * - * Sets the IPv6 address prefix. - **/ -void -nm_ip6_address_set_prefix (NMIP6Address *address, guint32 prefix) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - g_return_if_fail (prefix <= 128); - g_return_if_fail (prefix > 0); - - address->prefix = prefix; -} - -/** - * nm_ip6_address_get_gateway: - * @address: the #NMIP6Address - * - * Gets the IPv6 default gateway property of this address object. - * - * Returns: (array fixed-size=16) (element-type guint8) (transfer none): - * the IPv6 gateway address - **/ -const struct in6_addr * -nm_ip6_address_get_gateway (NMIP6Address *address) -{ - g_return_val_if_fail (address != NULL, NULL); - g_return_val_if_fail (address->refcount > 0, NULL); - - return &address->gateway; -} - -/** - * nm_ip6_address_set_gateway: - * @address: the #NMIP6Address - * @gateway: the IPv6 default gateway - * - * Sets the IPv6 default gateway property of this address object. - **/ -void -nm_ip6_address_set_gateway (NMIP6Address *address, const struct in6_addr *gateway) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (address->refcount > 0); - g_return_if_fail (gateway != NULL); - - memcpy (&address->gateway, gateway, sizeof (struct in6_addr)); -} - -/********************************************************************/ - -struct NMIP6Route { - guint32 refcount; - - struct in6_addr dest; - guint32 prefix; - struct in6_addr next_hop; - guint32 metric; /* lower metric == more preferred */ -}; - -/** - * nm_ip6_route_new: - * - * Creates and returns a new #NMIP6Route object. - * - * Returns: (transfer full): the new empty #NMIP6Route object - **/ -NMIP6Route * -nm_ip6_route_new (void) -{ - NMIP6Route *route; - - route = g_malloc0 (sizeof (NMIP6Route)); - route->refcount = 1; - return route; -} - -/** - * nm_ip6_route_dup: - * @source: the #NMIP6Route object to copy - * - * Copies a given #NMIP6Route object and returns the copy. - * - * Returns: (transfer full): the copy of the given #NMIP6Route copy - **/ -NMIP6Route * -nm_ip6_route_dup (NMIP6Route *source) -{ - NMIP6Route *route; - - g_return_val_if_fail (source != NULL, NULL); - g_return_val_if_fail (source->refcount > 0, NULL); - - route = nm_ip6_route_new (); - route->prefix = source->prefix; - route->metric = source->metric; - memcpy (&route->dest, &source->dest, sizeof (struct in6_addr)); - memcpy (&route->next_hop, &source->next_hop, sizeof (struct in6_addr)); - - return route; -} - -/** - * nm_ip6_route_ref: - * @route: the #NMIP6Route - * - * Increases the reference count of the object. - **/ -void -nm_ip6_route_ref (NMIP6Route *route) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->refcount++; -} - -/** - * nm_ip6_route_unref: - * @route: the #NMIP6Route - * - * Decreases the reference count of the object. If the reference count - * reaches zero, the object will be destroyed. - **/ -void -nm_ip6_route_unref (NMIP6Route *route) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->refcount--; - if (route->refcount == 0) { - memset (route, 0, sizeof (NMIP6Route)); - g_free (route); - } -} - -/** - * nm_ip6_route_compare: - * @route: the #NMIP6Route - * @other: the #NMIP6Route to compare @route to. - * - * Determines if two #NMIP6Route objects contain the same values. - * - * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. - **/ -gboolean -nm_ip6_route_compare (NMIP6Route *route, NMIP6Route *other) -{ - g_return_val_if_fail (route != NULL, FALSE); - g_return_val_if_fail (route->refcount > 0, FALSE); - - g_return_val_if_fail (other != NULL, FALSE); - g_return_val_if_fail (other->refcount > 0, FALSE); - - if ( memcmp (&route->dest, &other->dest, sizeof (struct in6_addr)) - || route->prefix != other->prefix - || memcmp (&route->next_hop, &other->next_hop, sizeof (struct in6_addr)) - || route->metric != other->metric) - return FALSE; - return TRUE; -} - -/** - * nm_ip6_route_get_dest: - * @route: the #NMIP6Route - * - * Gets the IPv6 destination address property of this route object. - * - * Returns: (array fixed-size=16) (element-type guint8) (transfer none): - * the IPv6 address of destination - **/ -const struct in6_addr * -nm_ip6_route_get_dest (NMIP6Route *route) -{ - g_return_val_if_fail (route != NULL, NULL); - g_return_val_if_fail (route->refcount > 0, NULL); - - return &route->dest; -} - -/** - * nm_ip6_route_set_dest: - * @route: the #NMIP6Route - * @dest: the destination address - * - * Sets the IPv6 destination address property of this route object. - **/ -void -nm_ip6_route_set_dest (NMIP6Route *route, const struct in6_addr *dest) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - g_return_if_fail (dest != NULL); - - memcpy (&route->dest, dest, sizeof (struct in6_addr)); -} - -/** - * nm_ip6_route_get_prefix: - * @route: the #NMIP6Route - * - * Gets the IPv6 prefix (ie "32" or "64" etc) of this route. - * - * Returns: the IPv6 prefix - **/ -guint32 -nm_ip6_route_get_prefix (NMIP6Route *route) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->refcount > 0, 0); - - return route->prefix; -} - -/** - * nm_ip6_route_set_prefix: - * @route: the #NMIP6Route - * @prefix: the prefix, a number between 1 and 128 inclusive - * - * Sets the IPv6 prefix of this route. - **/ -void -nm_ip6_route_set_prefix (NMIP6Route *route, guint32 prefix) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - g_return_if_fail (prefix <= 128); - g_return_if_fail (prefix > 0); - - route->prefix = prefix; -} - -/** - * nm_ip6_route_get_next_hop: - * @route: the #NMIP6Route - * - * Gets the IPv6 address of the next hop of this route. - * - * Returns: (array fixed-size=16) (element-type guint8) (transfer none): - * the IPv6 address of next hop - **/ -const struct in6_addr * -nm_ip6_route_get_next_hop (NMIP6Route *route) -{ - g_return_val_if_fail (route != NULL, NULL); - g_return_val_if_fail (route->refcount > 0, NULL); - - return &route->next_hop; -} - -/** - * nm_ip6_route_set_next_hop: - * @route: the #NMIP6Route - * @next_hop: the IPv6 address of the next hop - * - * Sets the IPv6 address of the next hop of this route. - **/ -void -nm_ip6_route_set_next_hop (NMIP6Route *route, const struct in6_addr *next_hop) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - g_return_if_fail (next_hop != NULL); - - memcpy (&route->next_hop, next_hop, sizeof (struct in6_addr)); -} - -/** - * nm_ip6_route_get_metric: - * @route: the #NMIP6Route - * - * Gets the route metric property of this route object; lower values indicate - * "better" or more preferred routes. - * - * Returns: the route metric - **/ -guint32 -nm_ip6_route_get_metric (NMIP6Route *route) -{ - g_return_val_if_fail (route != NULL, 0); - g_return_val_if_fail (route->refcount > 0, 0); - - return route->metric; -} - -/** - * nm_ip6_route_set_metric: - * @route: the #NMIP6Route - * @metric: the route metric - * - * Sets the route metric property of this route object; lower values indicate - * "better" or more preferred routes. - **/ -void -nm_ip6_route_set_metric (NMIP6Route *route, guint32 metric) -{ - g_return_if_fail (route != NULL); - g_return_if_fail (route->refcount > 0); - - route->metric = metric; -} diff --git a/libnm-core/nm-setting-ip6-config.h b/libnm-core/nm-setting-ip6-config.h index 831f212bb6..73b93eeae8 100644 --- a/libnm-core/nm-setting-ip6-config.h +++ b/libnm-core/nm-setting-ip6-config.h @@ -29,6 +29,7 @@ #include #include "nm-setting.h" +#include "nm-setting-ip-config.h" G_BEGIN_DECLS @@ -127,57 +128,6 @@ typedef enum { NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR = 2 } NMSettingIP6ConfigPrivacy; - -typedef struct NMIP6Address NMIP6Address; - -GType nm_ip6_address_get_type (void); - -NMIP6Address * nm_ip6_address_new (void); -NMIP6Address * nm_ip6_address_dup (NMIP6Address *source); -void nm_ip6_address_ref (NMIP6Address *address); -void nm_ip6_address_unref (NMIP6Address *address); -/* Return TRUE if addresses are identical */ -gboolean nm_ip6_address_compare (NMIP6Address *address, NMIP6Address *other); - -const struct in6_addr *nm_ip6_address_get_address (NMIP6Address *address); -void nm_ip6_address_set_address (NMIP6Address *address, - const struct in6_addr *addr); - -guint32 nm_ip6_address_get_prefix (NMIP6Address *address); -void nm_ip6_address_set_prefix (NMIP6Address *address, - guint32 prefix); - -const struct in6_addr *nm_ip6_address_get_gateway (NMIP6Address *address); -void nm_ip6_address_set_gateway (NMIP6Address *address, - const struct in6_addr *gateway); - -typedef struct NMIP6Route NMIP6Route; - -GType nm_ip6_route_get_type (void); - -NMIP6Route * nm_ip6_route_new (void); -NMIP6Route * nm_ip6_route_dup (NMIP6Route *source); -void nm_ip6_route_ref (NMIP6Route *route); -void nm_ip6_route_unref (NMIP6Route *route); -/* Return TRUE if routes are identical */ -gboolean nm_ip6_route_compare (NMIP6Route *route, NMIP6Route *other); - -const struct in6_addr *nm_ip6_route_get_dest (NMIP6Route *route); -void nm_ip6_route_set_dest (NMIP6Route *route, - const struct in6_addr *dest); - -guint32 nm_ip6_route_get_prefix (NMIP6Route *route); -void nm_ip6_route_set_prefix (NMIP6Route *route, - guint32 prefix); - -const struct in6_addr *nm_ip6_route_get_next_hop (NMIP6Route *route); -void nm_ip6_route_set_next_hop (NMIP6Route *route, - const struct in6_addr *next_hop); - -guint32 nm_ip6_route_get_metric (NMIP6Route *route); -void nm_ip6_route_set_metric (NMIP6Route *route, - guint32 metric); - struct _NMSettingIP6Config { NMSetting parent; }; @@ -209,17 +159,17 @@ gboolean nm_setting_ip6_config_remove_dns_search_by_value (NMSetti void nm_setting_ip6_config_clear_dns_searches (NMSettingIP6Config *setting); guint32 nm_setting_ip6_config_get_num_addresses (NMSettingIP6Config *setting); -NMIP6Address * nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, NMIP6Address *address); +NMIPAddress * nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i); +gboolean nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, NMIPAddress *address); void nm_setting_ip6_config_remove_address (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting, NMIP6Address *address); +gboolean nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting, NMIPAddress *address); void nm_setting_ip6_config_clear_addresses (NMSettingIP6Config *setting); guint32 nm_setting_ip6_config_get_num_routes (NMSettingIP6Config *setting); -NMIP6Route * nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, NMIP6Route *route); +NMIPRoute * nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i); +gboolean nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, NMIPRoute *route); void nm_setting_ip6_config_remove_route (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting, NMIP6Route *route); +gboolean nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting, NMIPRoute *route); void nm_setting_ip6_config_clear_routes (NMSettingIP6Config *setting); gboolean nm_setting_ip6_config_get_ignore_auto_routes (NMSettingIP6Config *setting); diff --git a/libnm-core/nm-utils-private.h b/libnm-core/nm-utils-private.h index bc42921514..4a4e82e0d9 100644 --- a/libnm-core/nm-utils-private.h +++ b/libnm-core/nm-utils-private.h @@ -40,29 +40,12 @@ GVariant * _nm_utils_bytes_to_dbus (const GValue *prop_value); void _nm_utils_bytes_from_dbus (GVariant *dbus_value, GValue *prop_value); -GVariant * _nm_utils_ip4_dns_to_dbus (const GValue *prop_value); -void _nm_utils_ip4_dns_from_dbus (GVariant *dbus_value, - GValue *prop_value); -GVariant * _nm_utils_ip4_addresses_to_dbus (const GValue *prop_value); -void _nm_utils_ip4_addresses_from_dbus (GVariant *dbus_value, - GValue *prop_value); -GVariant * _nm_utils_ip4_routes_to_dbus (const GValue *prop_value); -void _nm_utils_ip4_routes_from_dbus (GVariant *dbus_value, - GValue *prop_value); - -GVariant * _nm_utils_ip6_dns_to_dbus (const GValue *prop_value); -void _nm_utils_ip6_dns_from_dbus (GVariant *dbus_value, - GValue *prop_value); -GVariant * _nm_utils_ip6_addresses_to_dbus (const GValue *prop_value); -void _nm_utils_ip6_addresses_from_dbus (GVariant *dbus_value, - GValue *prop_value); -GVariant * _nm_utils_ip6_routes_to_dbus (const GValue *prop_value); -void _nm_utils_ip6_routes_from_dbus (GVariant *dbus_value, - GValue *prop_value); - GSList * _nm_utils_strv_to_slist (char **strv); char ** _nm_utils_slist_to_strv (GSList *slist); +GPtrArray * _nm_utils_strv_to_ptrarray (char **strv); +char ** _nm_utils_ptrarray_to_strv (GPtrArray *ptrarray); + char ** _nm_utils_strsplit_set (const char *str, const char *delimiters, int max_tokens); diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index dfa0d7cf81..a028f84d4d 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -662,7 +662,7 @@ _nm_utils_slist_to_strv (GSList *slist) { GSList *iter; char **strv; - int len, i = 0; + int len, i; len = g_slist_length (slist); strv = g_new (char *, len + 1); @@ -674,6 +674,40 @@ _nm_utils_slist_to_strv (GSList *slist) return strv; } +GPtrArray * +_nm_utils_strv_to_ptrarray (char **strv) +{ + GPtrArray *ptrarray; + int i; + + ptrarray = g_ptr_array_new_with_free_func (g_free); + + if (strv) { + for (i = 0; strv[i]; i++) + g_ptr_array_add (ptrarray, g_strdup (strv[i])); + } + + return ptrarray; +} + +char ** +_nm_utils_ptrarray_to_strv (GPtrArray *ptrarray) +{ + char **strv; + int i; + + if (!ptrarray) + return g_new0 (char *, 1); + + strv = g_new (char *, ptrarray->len + 1); + + for (i = 0; i < ptrarray->len; i++) + strv[i] = g_strdup (ptrarray->pdata[i]); + strv[i] = NULL; + + return strv; +} + /** * _nm_utils_strsplit_set: * @str: string to split @@ -1100,11 +1134,12 @@ nm_utils_ip4_dns_from_variant (GVariant *value) /** * nm_utils_ip4_addresses_to_variant: - * @addresses: (element-type NMIP4Address): an array of #NMIP4Address objects + * @addresses: (element-type NMIPAddress): an array of #NMIPAddress objects * - * Utility function to convert a #GPtrArray of #NMIP4Address objects into a - * #GVariant of type 'aau' representing an array of NetworkManager IPv4 - * addresses (which are tuples of address, prefix, and gateway). + * Utility function to convert a #GPtrArray of #NMIPAddress objects representing + * IPv4 addresses into a #GVariant of type 'aau' representing an array of + * NetworkManager IPv4 addresses (which are tuples of address, prefix, and + * gateway). * * Returns: (transfer none): a new floating #GVariant representing @addresses. **/ @@ -1118,12 +1153,18 @@ nm_utils_ip4_addresses_to_variant (GPtrArray *addresses) if (addresses) { for (i = 0; i < addresses->len; i++) { - NMIP4Address *addr = addresses->pdata[i]; + NMIPAddress *addr = addresses->pdata[i]; guint32 array[3]; - array[0] = nm_ip4_address_get_address (addr); - array[1] = nm_ip4_address_get_prefix (addr); - array[2] = nm_ip4_address_get_gateway (addr); + if (nm_ip_address_get_family (addr) != AF_INET) + continue; + + nm_ip_address_get_address_binary (addr, &array[0]); + array[1] = nm_ip_address_get_prefix (addr); + if (nm_ip_address_get_gateway (addr)) + inet_pton (AF_INET, nm_ip_address_get_gateway (addr), &array[2]); + else + array[2] = 0; g_variant_builder_add (&builder, "@au", g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, @@ -1140,10 +1181,10 @@ nm_utils_ip4_addresses_to_variant (GPtrArray *addresses) * * Utility function to convert a #GVariant of type 'aau' representing a list of * NetworkManager IPv4 addresses (which are tuples of address, prefix, and - * gateway) into a #GPtrArray of #NMIP4Address objects. + * gateway) into a #GPtrArray of #NMIPAddress objects. * - * Returns: (transfer full) (element-type NMIP4Address): a newly allocated - * #GPtrArray of #NMIP4Address objects + * Returns: (transfer full) (element-type NMIPAddress): a newly allocated + * #GPtrArray of #NMIPAddress objects **/ GPtrArray * nm_utils_ip4_addresses_from_variant (GVariant *value) @@ -1155,12 +1196,13 @@ nm_utils_ip4_addresses_from_variant (GVariant *value) g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("aau")), NULL); g_variant_iter_init (&iter, value); - addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_address_unref); + addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_address_unref); while (g_variant_iter_next (&iter, "@au", &addr_var)) { const guint32 *addr_array; gsize length; - NMIP4Address *addr; + NMIPAddress *addr; + GError *error = NULL; addr_array = g_variant_get_fixed_array (addr_var, &length, sizeof (guint32)); if (length < 3) { @@ -1169,12 +1211,15 @@ nm_utils_ip4_addresses_from_variant (GVariant *value) continue; } - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, addr_array[0]); - nm_ip4_address_set_prefix (addr, addr_array[1]); - nm_ip4_address_set_gateway (addr, addr_array[2]); - - g_ptr_array_add (addresses, addr); + addr = nm_ip_address_new_binary (AF_INET, + &addr_array[0], addr_array[1], &addr_array[2], + &error); + if (addr) + g_ptr_array_add (addresses, addr); + else { + g_warning ("Ignoring invalid IP4 address: %s", error->message); + g_clear_error (&error); + } g_variant_unref (addr_var); } @@ -1183,11 +1228,12 @@ nm_utils_ip4_addresses_from_variant (GVariant *value) /** * nm_utils_ip4_routes_to_variant: - * @routes: (element-type NMIP4Route): an array of #NMIP4Route objects + * @routes: (element-type NMIPRoute): an array of #NMIP4Route objects * - * Utility function to convert a #GPtrArray of #NMIP4Route objects into a - * #GVariant of type 'aau' representing an array of NetworkManager IPv4 routes - * (which are tuples of route, prefix, next hop, and metric). + * Utility function to convert a #GPtrArray of #NMIPRoute objects representing + * IPv4 routes into a #GVariant of type 'aau' representing an array of + * NetworkManager IPv4 routes (which are tuples of route, prefix, next hop, and + * metric). * * Returns: (transfer none): a new floating #GVariant representing @routes. **/ @@ -1201,13 +1247,16 @@ nm_utils_ip4_routes_to_variant (GPtrArray *routes) if (routes) { for (i = 0; i < routes->len; i++) { - NMIP4Route *route = routes->pdata[i]; + NMIPRoute *route = routes->pdata[i]; guint32 array[4]; - array[0] = nm_ip4_route_get_dest (route); - array[1] = nm_ip4_route_get_prefix (route); - array[2] = nm_ip4_route_get_next_hop (route); - array[3] = nm_ip4_route_get_metric (route); + if (nm_ip_route_get_family (route) != AF_INET) + continue; + + nm_ip_route_get_dest_binary (route, &array[0]); + array[1] = nm_ip_route_get_prefix (route); + nm_ip_route_get_next_hop_binary (route, &array[2]); + array[3] = nm_ip_route_get_metric (route); g_variant_builder_add (&builder, "@au", g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, @@ -1224,10 +1273,10 @@ nm_utils_ip4_routes_to_variant (GPtrArray *routes) * * Utility function to convert a #GVariant of type 'aau' representing an array * of NetworkManager IPv4 routes (which are tuples of route, prefix, next hop, - * and metric) into a #GPtrArray of #NMIP4Route objects. + * and metric) into a #GPtrArray of #NMIPRoute objects. * - * Returns: (transfer full) (element-type NMIP4Route): a newly allocated - * #GPtrArray of #NMIP4Route objects + * Returns: (transfer full) (element-type NMIPRoute): a newly allocated + * #GPtrArray of #NMIPRoute objects **/ GPtrArray * nm_utils_ip4_routes_from_variant (GVariant *value) @@ -1239,12 +1288,13 @@ nm_utils_ip4_routes_from_variant (GVariant *value) g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("aau")), NULL); g_variant_iter_init (&iter, value); - routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip4_route_unref); + routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref); while (g_variant_iter_next (&iter, "@au", &route_var)) { const guint32 *route_array; gsize length; - NMIP4Route *route; + NMIPRoute *route; + GError *error = NULL; route_array = g_variant_get_fixed_array (route_var, &length, sizeof (guint32)); if (length < 4) { @@ -1253,13 +1303,16 @@ nm_utils_ip4_routes_from_variant (GVariant *value) continue; } - route = nm_ip4_route_new (); - nm_ip4_route_set_dest (route, route_array[0]); - nm_ip4_route_set_prefix (route, route_array[1]); - nm_ip4_route_set_next_hop (route, route_array[2]); - nm_ip4_route_set_metric (route, route_array[3]); - - g_ptr_array_add (routes, route); + route = nm_ip_route_new_binary (AF_INET, + &route_array[0], route_array[1], + &route_array[2], route_array[3], + &error); + if (route) + g_ptr_array_add (routes, route); + else { + g_warning ("Ignoring invalid IP4 route: %s", error->message); + g_clear_error (&error); + } g_variant_unref (route_var); } @@ -1412,11 +1465,12 @@ nm_utils_ip6_dns_from_variant (GVariant *value) /** * nm_utils_ip6_addresses_to_variant: - * @addresses: (element-type NMIP6Address): an array of #NMIP6Address objects + * @addresses: (element-type NMIPAddress): an array of #NMIPAddress objects * - * Utility function to convert a #GPtrArray of #NMIP6Address objects into a - * #GVariant of type 'a(ayuay)' representing an array of NetworkManager IPv6 - * addresses (which are tuples of address, prefix, and gateway). + * Utility function to convert a #GPtrArray of #NMIPAddress objects representing + * IPv6 addresses into a #GVariant of type 'a(ayuay)' representing an array of + * NetworkManager IPv6 addresses (which are tuples of address, prefix, and + * gateway). * * Returns: (transfer none): a new floating #GVariant representing @addresses. **/ @@ -1430,17 +1484,24 @@ nm_utils_ip6_addresses_to_variant (GPtrArray *addresses) if (addresses) { for (i = 0; i < addresses->len; i++) { - NMIP6Address *addr = addresses->pdata[i]; + NMIPAddress *addr = addresses->pdata[i]; + struct in6_addr ip_bytes, gateway_bytes; GVariant *ip, *gateway; guint32 prefix; - ip = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, - nm_ip6_address_get_address (addr), - 16, 1); - prefix = nm_ip6_address_get_prefix (addr); - gateway = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, - nm_ip6_address_get_gateway (addr), - 16, 1); + if (nm_ip_address_get_family (addr) != AF_INET6) + continue; + + nm_ip_address_get_address_binary (addr, &ip_bytes); + ip = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &ip_bytes, 16, 1); + + prefix = nm_ip_address_get_prefix (addr); + + if (nm_ip_address_get_gateway (addr)) + inet_pton (AF_INET6, nm_ip_address_get_gateway (addr), &gateway_bytes); + else + memset (&gateway_bytes, 0, sizeof (gateway_bytes)); + gateway = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &gateway_bytes, 16, 1); g_variant_builder_add (&builder, "(@ayu@ay)", ip, prefix, gateway); } @@ -1455,10 +1516,10 @@ nm_utils_ip6_addresses_to_variant (GPtrArray *addresses) * * Utility function to convert a #GVariant of type 'a(ayuay)' representing a * list of NetworkManager IPv6 addresses (which are tuples of address, prefix, - * and gateway) into a #GPtrArray of #NMIP6Address objects. + * and gateway) into a #GPtrArray of #NMIPAddress objects. * - * Returns: (transfer full) (element-type NMIP6Address): a newly allocated - * #GPtrArray of #NMIP6Address objects + * Returns: (transfer full) (element-type NMIPAddress): a newly allocated + * #GPtrArray of #NMIPAddress objects **/ GPtrArray * nm_utils_ip6_addresses_from_variant (GVariant *value) @@ -1471,12 +1532,13 @@ nm_utils_ip6_addresses_from_variant (GVariant *value) g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("a(ayuay)")), NULL); g_variant_iter_init (&iter, value); - addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip6_address_unref); + addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_address_unref); while (g_variant_iter_next (&iter, "(@ayu@ay)", &addr_var, &prefix, &gateway_var)) { - NMIP6Address *addr; + NMIPAddress *addr; const struct in6_addr *addr_bytes, *gateway_bytes; gsize addr_len, gateway_len; + GError *error = NULL; if ( !g_variant_is_of_type (addr_var, G_VARIANT_TYPE_BYTESTRING) || !g_variant_is_of_type (gateway_var, G_VARIANT_TYPE_BYTESTRING)) { @@ -1490,11 +1552,6 @@ nm_utils_ip6_addresses_from_variant (GVariant *value) __func__, (int) addr_len); goto next; } - if (prefix > 128) { - g_warning ("%s: ignoring invalid IP6 prefix %d", - __func__, prefix); - goto next; - } gateway_bytes = g_variant_get_fixed_array (gateway_var, &gateway_len, 1); if (gateway_len != 16) { g_warning ("%s: ignoring invalid IP6 address of length %d", @@ -1502,11 +1559,13 @@ nm_utils_ip6_addresses_from_variant (GVariant *value) goto next; } - addr = nm_ip6_address_new (); - nm_ip6_address_set_address (addr, addr_bytes); - nm_ip6_address_set_prefix (addr, prefix); - nm_ip6_address_set_gateway (addr, gateway_bytes); - g_ptr_array_add (addresses, addr); + addr = nm_ip_address_new_binary (AF_INET6, addr_bytes, prefix, gateway_bytes, &error); + if (addr) + g_ptr_array_add (addresses, addr); + else { + g_warning ("Ignoring invalid IP4 address: %s", error->message); + g_clear_error (&error); + } next: g_variant_unref (addr_var); @@ -1518,11 +1577,12 @@ nm_utils_ip6_addresses_from_variant (GVariant *value) /** * nm_utils_ip6_routes_to_variant: - * @routes: (element-type NMIP6Route): an array of #NMIP6Route objects + * @routes: (element-type NMIPRoute): an array of #NMIPRoute objects * - * Utility function to convert a #GPtrArray of #NMIP6Route objects into a - * #GVariant of type 'a(ayuayu)' representing an array of NetworkManager IPv6 - * routes (which are tuples of route, prefix, next hop, and metric). + * Utility function to convert a #GPtrArray of #NMIPRoute objects representing + * IPv6 routes into a #GVariant of type 'a(ayuayu)' representing an array of + * NetworkManager IPv6 routes (which are tuples of route, prefix, next hop, and + * metric). * * Returns: (transfer none): a new floating #GVariant representing @routes. **/ @@ -1536,18 +1596,20 @@ nm_utils_ip6_routes_to_variant (GPtrArray *routes) if (routes) { for (i = 0; i < routes->len; i++) { - NMIP6Route *route = routes->pdata[i]; + NMIPRoute *route = routes->pdata[i]; + struct in6_addr dest_bytes, next_hop_bytes; GVariant *dest, *next_hop; guint32 prefix, metric; - dest = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, - nm_ip6_route_get_dest (route), - 16, 1); - prefix = nm_ip6_route_get_prefix (route); - next_hop = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, - nm_ip6_route_get_next_hop (route), - 16, 1); - metric = nm_ip6_route_get_metric (route); + if (nm_ip_route_get_family (route) != AF_INET6) + continue; + + nm_ip_route_get_dest_binary (route, &dest_bytes); + dest = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &dest_bytes, 16, 1); + prefix = nm_ip_route_get_prefix (route); + nm_ip_route_get_next_hop_binary (route, &next_hop_bytes); + next_hop = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &next_hop_bytes, 16, 1); + metric = nm_ip_route_get_metric (route); g_variant_builder_add (&builder, "(@ayu@ayu)", dest, prefix, next_hop, metric); } @@ -1562,10 +1624,10 @@ nm_utils_ip6_routes_to_variant (GPtrArray *routes) * * Utility function to convert a #GVariant of type 'a(ayuayu)' representing an * array of NetworkManager IPv6 routes (which are tuples of route, prefix, next - * hop, and metric) into a #GPtrArray of #NMIP6Route objects. + * hop, and metric) into a #GPtrArray of #NMIPRoute objects. * - * Returns: (transfer full) (element-type NMIP6Route): a newly allocated - * #GPtrArray of #NMIP6Route objects + * Returns: (transfer full) (element-type NMIPRoute): a newly allocated + * #GPtrArray of #NMIPRoute objects **/ GPtrArray * nm_utils_ip6_routes_from_variant (GVariant *value) @@ -1579,11 +1641,12 @@ nm_utils_ip6_routes_from_variant (GVariant *value) g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("a(ayuayu)")), NULL); - routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip6_route_unref); + routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref); g_variant_iter_init (&iter, value); while (g_variant_iter_next (&iter, "(@ayu@ayu)", &dest_var, &prefix, &next_hop_var, &metric)) { - NMIP6Route *route; + NMIPRoute *route; + GError *error = NULL; if ( !g_variant_is_of_type (dest_var, G_VARIANT_TYPE_BYTESTRING) || !g_variant_is_of_type (next_hop_var, G_VARIANT_TYPE_BYTESTRING)) { @@ -1597,6 +1660,7 @@ nm_utils_ip6_routes_from_variant (GVariant *value) __func__, (int) dest_len); goto next; } + next_hop = g_variant_get_fixed_array (next_hop_var, &next_hop_len, 1); if (next_hop_len != 16) { g_warning ("%s: ignoring invalid IP6 address of length %d", @@ -1604,12 +1668,13 @@ nm_utils_ip6_routes_from_variant (GVariant *value) goto next; } - route = nm_ip6_route_new (); - nm_ip6_route_set_dest (route, dest); - nm_ip6_route_set_prefix (route, prefix); - nm_ip6_route_set_next_hop (route, next_hop); - nm_ip6_route_set_metric (route, metric); - g_ptr_array_add (routes, route); + route = nm_ip_route_new_binary (AF_INET6, dest, prefix, next_hop, metric, &error); + if (route) + g_ptr_array_add (routes, route); + else { + g_warning ("Ignoring invalid IP6 route: %s", error->message); + g_clear_error (&error); + } next: g_variant_unref (dest_var); @@ -2750,6 +2815,28 @@ nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst) INET6_ADDRSTRLEN); } +/** + * nm_utils_ipaddr_valid: + * @family: %AF_INET or %AF_INET6, or %AF_UNSPEC to accept either + * @ip: an IP address + * + * Checks if @ip contains a valid IP address of the given family. + * + * Return value: %TRUE or %FALSE + */ +gboolean +nm_utils_ipaddr_valid (int family, const char *ip) +{ + guint8 buf[sizeof (struct in6_addr)]; + + g_return_val_if_fail (family == AF_INET || family == AF_INET6 || family == AF_UNSPEC, FALSE); + + if (family == AF_UNSPEC) + family = strchr (ip, ':') ? AF_INET6 : AF_INET; + + return inet_pton (family, ip, buf) == 1; +} + /** * nm_utils_check_virtual_device_compatibility: * @virtual_type: a virtual connection type @@ -2809,5 +2896,3 @@ nm_utils_check_virtual_device_compatibility (GType virtual_type, GType other_typ return FALSE; } } - - diff --git a/libnm-core/nm-utils.h b/libnm-core/nm-utils.h index 5dd3a84342..ccf15f7b84 100644 --- a/libnm-core/nm-utils.h +++ b/libnm-core/nm-utils.h @@ -174,6 +174,8 @@ gboolean nm_utils_is_uuid (const char *str); const char *nm_utils_inet4_ntop (in_addr_t inaddr, char *dst); const char *nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst); +gboolean nm_utils_ipaddr_valid (int family, const char *ip); + gboolean nm_utils_check_virtual_device_compatibility (GType virtual_type, GType other_type); G_END_DECLS diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index a71238929f..41189481f2 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -324,7 +324,7 @@ static void test_setting_ip4_config_labels (void) { NMSettingIP4Config *s_ip4; - NMIP4Address *addr; + NMIPAddress *addr; const char *label; GPtrArray *addrs; char **labels; @@ -336,12 +336,11 @@ test_setting_ip4_config_labels (void) NULL); /* addr 1 */ - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, 0x01010101); - nm_ip4_address_set_prefix (addr, 24); + addr = nm_ip_address_new (AF_INET, "1.1.1.1", 24, NULL, &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); @@ -349,12 +348,11 @@ test_setting_ip4_config_labels (void) g_assert_cmpstr (label, ==, ""); /* addr 2 */ - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, 0x02020202); - nm_ip4_address_set_prefix (addr, 24); + addr = nm_ip_address_new (AF_INET, "2.2.2.2", 24, NULL, &error); + g_assert_no_error (error); _nm_setting_ip4_config_add_address_with_label (s_ip4, addr, "eth0:1"); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); @@ -362,12 +360,11 @@ test_setting_ip4_config_labels (void) g_assert_cmpstr (label, ==, "eth0:1"); /* addr 3 */ - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, 0x03030303); - nm_ip4_address_set_prefix (addr, 24); + addr = nm_ip_address_new (AF_INET, "3.3.3.3", 24, NULL, &error); + g_assert_no_error (error); _nm_setting_ip4_config_add_address_with_label (s_ip4, addr, ""); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); @@ -380,12 +377,12 @@ test_setting_ip4_config_labels (void) g_assert_no_error (error); addr = nm_setting_ip4_config_get_address (s_ip4, 0); - g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x02020202); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); label = _nm_setting_ip4_config_get_address_label (s_ip4, 0); g_assert_cmpstr (label, ==, "eth0:1"); addr = nm_setting_ip4_config_get_address (s_ip4, 1); - g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x03030303); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); g_assert_cmpstr (label, ==, ""); @@ -409,12 +406,12 @@ test_setting_ip4_config_labels (void) g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); addr = nm_setting_ip4_config_get_address (s_ip4, 0); - g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x02020202); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); label = _nm_setting_ip4_config_get_address_label (s_ip4, 0); g_assert_cmpstr (label, ==, ""); addr = nm_setting_ip4_config_get_address (s_ip4, 1); - g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x03030303); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); g_assert_cmpstr (label, ==, ""); @@ -428,12 +425,12 @@ test_setting_ip4_config_labels (void) g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); addr = nm_setting_ip4_config_get_address (s_ip4, 0); - g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x02020202); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); label = _nm_setting_ip4_config_get_address_label (s_ip4, 0); g_assert_cmpstr (label, ==, "eth0:1"); addr = nm_setting_ip4_config_get_address (s_ip4, 1); - g_assert_cmpint (nm_ip4_address_get_address (addr), ==, 0x03030303); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); g_assert_cmpstr (label, ==, ""); @@ -2457,8 +2454,9 @@ test_setting_ip4_changed_signal (void) NMConnection *connection; gboolean changed = FALSE; NMSettingIP4Config *s_ip4; - NMIP4Address *addr; - NMIP4Route *route; + NMIPAddress *addr; + NMIPRoute *route; + GError *error = NULL; connection = nm_simple_connection_new (); g_signal_connect (connection, @@ -2489,22 +2487,20 @@ test_setting_ip4_changed_signal (void) ASSERT_CHANGED (nm_setting_ip4_config_add_dns_search (s_ip4, "foobar.com")); ASSERT_CHANGED (nm_setting_ip4_config_clear_dns_searches (s_ip4)); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, 0x2233); - nm_ip4_address_set_prefix (addr, 24); + addr = nm_ip_address_new (AF_INET, "22.33.0.0", 24, NULL, &error); + g_assert_no_error (error); ASSERT_CHANGED (nm_setting_ip4_config_add_address (s_ip4, addr)); ASSERT_CHANGED (nm_setting_ip4_config_remove_address (s_ip4, 0)); - g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*addr != NULL && label != NULL*"); + g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*addr != NULL*"); ASSERT_UNCHANGED (nm_setting_ip4_config_remove_address (s_ip4, 1)); g_test_assert_expected_messages (); nm_setting_ip4_config_add_address (s_ip4, addr); ASSERT_CHANGED (nm_setting_ip4_config_clear_addresses (s_ip4)); - route = nm_ip4_route_new (); - nm_ip4_route_set_dest (route, 0x2233); - nm_ip4_route_set_prefix (route, 24); + route = nm_ip_route_new (AF_INET, "22.33.0.0", 24, NULL, 0, &error); + g_assert_no_error (error); ASSERT_CHANGED (nm_setting_ip4_config_add_route (s_ip4, route)); ASSERT_CHANGED (nm_setting_ip4_config_remove_route (s_ip4, 0)); @@ -2516,8 +2512,8 @@ test_setting_ip4_changed_signal (void) nm_setting_ip4_config_add_route (s_ip4, route); ASSERT_CHANGED (nm_setting_ip4_config_clear_routes (s_ip4)); - nm_ip4_address_unref (addr); - nm_ip4_route_unref (route); + nm_ip_address_unref (addr); + nm_ip_route_unref (route); g_object_unref (connection); } @@ -2527,9 +2523,9 @@ test_setting_ip6_changed_signal (void) NMConnection *connection; gboolean changed = FALSE; NMSettingIP6Config *s_ip6; - NMIP6Address *addr; - NMIP6Route *route; - const struct in6_addr t = { { { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 } } }; + NMIPAddress *addr; + NMIPRoute *route; + GError *error = NULL; connection = nm_simple_connection_new (); g_signal_connect (connection, @@ -2560,9 +2556,8 @@ test_setting_ip6_changed_signal (void) nm_setting_ip6_config_add_dns_search (s_ip6, "foobar.com"); ASSERT_CHANGED (nm_setting_ip6_config_clear_dns_searches (s_ip6)); - addr = nm_ip6_address_new (); - nm_ip6_address_set_address (addr, &t); - nm_ip6_address_set_prefix (addr, 64); + addr = nm_ip_address_new (AF_INET6, "1:2:3::4:5:6", 64, NULL, &error); + g_assert_no_error (error); ASSERT_CHANGED (nm_setting_ip6_config_add_address (s_ip6, addr)); ASSERT_CHANGED (nm_setting_ip6_config_remove_address (s_ip6, 0)); @@ -2574,9 +2569,8 @@ test_setting_ip6_changed_signal (void) nm_setting_ip6_config_add_address (s_ip6, addr); ASSERT_CHANGED (nm_setting_ip6_config_clear_addresses (s_ip6)); - route = nm_ip6_route_new (); - nm_ip6_route_set_dest (route, &t); - nm_ip6_route_set_prefix (route, 128); + route = nm_ip_route_new (AF_INET6, "1:2:3::4:5:6", 128, NULL, 0, &error); + g_assert_no_error (error); ASSERT_CHANGED (nm_setting_ip6_config_add_route (s_ip6, route)); ASSERT_CHANGED (nm_setting_ip6_config_remove_route (s_ip6, 0)); @@ -2588,8 +2582,8 @@ test_setting_ip6_changed_signal (void) nm_setting_ip6_config_add_route (s_ip6, route); ASSERT_CHANGED (nm_setting_ip6_config_clear_routes (s_ip6)); - nm_ip6_address_unref (addr); - nm_ip6_route_unref (route); + nm_ip_address_unref (addr); + nm_ip_route_unref (route); g_object_unref (connection); } diff --git a/libnm/NetworkManager.h b/libnm/NetworkManager.h index ce7bec74c2..d112e33480 100644 --- a/libnm/NetworkManager.h +++ b/libnm/NetworkManager.h @@ -63,6 +63,7 @@ #include #include #include +#include #include #include #include diff --git a/libnm/libnm.ver b/libnm/libnm.ver index d735a30e38..106beaa08f 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -268,18 +268,6 @@ global: nm_dhcp6_config_get_one_option; nm_dhcp6_config_get_options; nm_dhcp6_config_get_type; - nm_ip4_address_compare; - nm_ip4_address_dup; - nm_ip4_address_get_address; - nm_ip4_address_get_gateway; - nm_ip4_address_get_prefix; - nm_ip4_address_get_type; - nm_ip4_address_new; - nm_ip4_address_ref; - nm_ip4_address_set_address; - nm_ip4_address_set_gateway; - nm_ip4_address_set_prefix; - nm_ip4_address_unref; nm_ip4_config_get_addresses; nm_ip4_config_get_domains; nm_ip4_config_get_gateway; @@ -288,32 +276,6 @@ global: nm_ip4_config_get_searches; nm_ip4_config_get_type; nm_ip4_config_get_wins_servers; - nm_ip4_route_compare; - nm_ip4_route_dup; - nm_ip4_route_get_dest; - nm_ip4_route_get_metric; - nm_ip4_route_get_next_hop; - nm_ip4_route_get_prefix; - nm_ip4_route_get_type; - nm_ip4_route_new; - nm_ip4_route_ref; - nm_ip4_route_set_dest; - nm_ip4_route_set_metric; - nm_ip4_route_set_next_hop; - nm_ip4_route_set_prefix; - nm_ip4_route_unref; - nm_ip6_address_compare; - nm_ip6_address_dup; - nm_ip6_address_get_address; - nm_ip6_address_get_gateway; - nm_ip6_address_get_prefix; - nm_ip6_address_get_type; - nm_ip6_address_new; - nm_ip6_address_ref; - nm_ip6_address_set_address; - nm_ip6_address_set_gateway; - nm_ip6_address_set_prefix; - nm_ip6_address_unref; nm_ip6_config_get_addresses; nm_ip6_config_get_domains; nm_ip6_config_get_gateway; @@ -321,20 +283,40 @@ global: nm_ip6_config_get_routes; nm_ip6_config_get_searches; nm_ip6_config_get_type; - nm_ip6_route_compare; - nm_ip6_route_dup; - nm_ip6_route_get_dest; - nm_ip6_route_get_metric; - nm_ip6_route_get_next_hop; - nm_ip6_route_get_prefix; - nm_ip6_route_get_type; - nm_ip6_route_new; - nm_ip6_route_ref; - nm_ip6_route_set_dest; - nm_ip6_route_set_metric; - nm_ip6_route_set_next_hop; - nm_ip6_route_set_prefix; - nm_ip6_route_unref; + nm_ip_address_equal; + nm_ip_address_get_address; + nm_ip_address_get_address_binary; + nm_ip_address_get_family; + nm_ip_address_get_gateway; + nm_ip_address_get_prefix; + nm_ip_address_get_type; + nm_ip_address_new; + nm_ip_address_new_binary; + nm_ip_address_ref; + nm_ip_address_set_address; + nm_ip_address_set_address_binary; + nm_ip_address_set_gateway; + nm_ip_address_set_prefix; + nm_ip_address_unref; + nm_ip_route_equal; + nm_ip_route_get_dest; + nm_ip_route_get_dest_binary; + nm_ip_route_get_family; + nm_ip_route_get_metric; + nm_ip_route_get_next_hop; + nm_ip_route_get_next_hop_binary; + nm_ip_route_get_prefix; + nm_ip_route_get_type; + nm_ip_route_new; + nm_ip_route_new_binary; + nm_ip_route_ref; + nm_ip_route_set_dest; + nm_ip_route_set_dest_binary; + nm_ip_route_set_metric; + nm_ip_route_set_next_hop; + nm_ip_route_set_next_hop_binary; + nm_ip_route_set_prefix; + nm_ip_route_unref; nm_manager_error_get_type; nm_manager_error_quark; nm_object_get_path; @@ -833,6 +815,7 @@ global: nm_utils_ip6_dns_to_variant; nm_utils_ip6_routes_from_variant; nm_utils_ip6_routes_to_variant; + nm_utils_ipaddr_valid; nm_utils_is_empty_ssid; nm_utils_is_uuid; nm_utils_rsa_key_encrypt; diff --git a/libnm/nm-ip4-config.c b/libnm/nm-ip4-config.c index ac3e5b36e9..bc86821043 100644 --- a/libnm/nm-ip4-config.c +++ b/libnm/nm-ip4-config.c @@ -161,13 +161,13 @@ get_property (GObject *object, break; case PROP_ADDRESSES: g_value_take_boxed (value, _nm_utils_copy_array (nm_ip4_config_get_addresses (self), - (NMUtilsCopyFunc) nm_ip4_address_dup, - (GDestroyNotify) nm_ip4_address_unref)); + (NMUtilsCopyFunc) nm_ip_address_dup, + (GDestroyNotify) nm_ip_address_unref)); break; case PROP_ROUTES: g_value_take_boxed (value, _nm_utils_copy_array (nm_ip4_config_get_routes (self), - (NMUtilsCopyFunc) nm_ip4_route_dup, - (GDestroyNotify) nm_ip4_route_unref)); + (NMUtilsCopyFunc) nm_ip_route_dup, + (GDestroyNotify) nm_ip_route_unref)); break; case PROP_NAMESERVERS: g_value_set_boxed (value, (char **) nm_ip4_config_get_nameservers (self)); @@ -220,7 +220,7 @@ nm_ip4_config_class_init (NMIP4ConfigClass *config_class) /** * NMIP4Config:addresses: * - * A #GPtrArray containing the addresses (#NMIP4Address) of the configuration. + * A #GPtrArray containing the addresses (#NMIPAddress) of the configuration. **/ g_object_class_install_property (object_class, PROP_ADDRESSES, @@ -232,7 +232,7 @@ nm_ip4_config_class_init (NMIP4ConfigClass *config_class) /** * NMIP4Config:routes: * - * A #GPtrArray containing the routes (#NMIP4Route) of the configuration. + * A #GPtrArray containing the routes (#NMIPRoute) of the configuration. **/ g_object_class_install_property (object_class, PROP_ROUTES, @@ -312,8 +312,8 @@ nm_ip4_config_get_gateway (NMIP4Config *config) * * Gets the IP4 addresses (containing the address, prefix, and gateway). * - * Returns: (element-type NMIP4Address) (transfer none): the #GPtrArray - * containing #NMIP4Addresses. This is the internal copy used by the + * Returns: (element-type NMIPAddress) (transfer none): the #GPtrArray + * containing #NMIPAddresses. This is the internal copy used by the * configuration and must not be modified. **/ GPtrArray * @@ -398,8 +398,8 @@ nm_ip4_config_get_wins_servers (NMIP4Config *config) * * Gets the routes. * - * Returns: (element-type NMIP4Route) (transfer none): the #GPtrArray containing - * #NMIP4Routes. This is the internal copy used by the configuration, and must + * Returns: (element-type NMIPRoute) (transfer none): the #GPtrArray containing + * #NMIPRoutes. This is the internal copy used by the configuration, and must * not be modified. **/ GPtrArray * diff --git a/libnm/nm-ip6-config.c b/libnm/nm-ip6-config.c index 8b48c15992..589b79ce1c 100644 --- a/libnm/nm-ip6-config.c +++ b/libnm/nm-ip6-config.c @@ -135,8 +135,8 @@ nm_ip6_config_get_gateway (NMIP6Config *config) * * Gets the IP6 addresses (containing the address, prefix, and gateway). * - * Returns: (element-type NMIP6Address) (transfer none): the #GPtrArray - * containing #NMIP6Addresses. This is the internal copy used by the + * Returns: (element-type NMIPAddress) (transfer none): the #GPtrArray + * containing #NMIPAddresses. This is the internal copy used by the * configuration and must not be modified. **/ GPtrArray * @@ -203,8 +203,8 @@ nm_ip6_config_get_searches (NMIP6Config *config) * * Gets the routes. * - * Returns: (element-type NMIP6Route) (transfer none): the #GPtrArray containing - * #NMIP6Routes. This is the internal copy used by the configuration, and must + * Returns: (element-type NMIPRoute) (transfer none): the #GPtrArray containing + * #NMIPRoutes. This is the internal copy used by the configuration, and must * not be modified. **/ GPtrArray * @@ -246,13 +246,13 @@ get_property (GObject *object, break; case PROP_ADDRESSES: g_value_take_boxed (value, _nm_utils_copy_array (nm_ip6_config_get_addresses (self), - (NMUtilsCopyFunc) nm_ip6_address_dup, - (GDestroyNotify) nm_ip6_address_unref)); + (NMUtilsCopyFunc) nm_ip_address_dup, + (GDestroyNotify) nm_ip_address_unref)); break; case PROP_ROUTES: g_value_take_boxed (value, _nm_utils_copy_array (nm_ip6_config_get_routes (self), - (NMUtilsCopyFunc) nm_ip6_route_dup, - (GDestroyNotify) nm_ip6_route_unref)); + (NMUtilsCopyFunc) nm_ip_route_dup, + (GDestroyNotify) nm_ip_route_unref)); break; case PROP_NAMESERVERS: g_value_set_boxed (value, (char **) nm_ip6_config_get_nameservers (self)); @@ -314,7 +314,7 @@ nm_ip6_config_class_init (NMIP6ConfigClass *config_class) /** * NMIP6Config:addresses: * - * The #GPtrArray containing the IPv6 addresses (#NMIP6Address). + * The #GPtrArray containing the IPv6 addresses (#NMIPAddress). **/ g_object_class_install_property (object_class, PROP_ADDRESSES, @@ -326,7 +326,7 @@ nm_ip6_config_class_init (NMIP6ConfigClass *config_class) /** * NMIP6Config:routes: * - * The #GPtrArray containing the IPv6 routes (#NMIP6Route). + * The #GPtrArray containing the IPv6 routes (#NMIPRoute). **/ g_object_class_install_property (object_class, PROP_ROUTES, diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 526aba570c..9b624a2f6b 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -2391,19 +2391,25 @@ nm_utils_ip4_routes_from_gvalue (const GValue *value) routes = (GPtrArray *) g_value_get_boxed (value); for (i = 0; routes && (i < routes->len); i++) { GArray *array = (GArray *) g_ptr_array_index (routes, i); - NMIP4Route *route; + guint32 *array_val = (guint32 *) array->data; + NMIPRoute *route; + GError *error = NULL; if (array->len < 4) { g_warning ("Ignoring invalid IP4 route"); continue; } - route = nm_ip4_route_new (); - nm_ip4_route_set_dest (route, g_array_index (array, guint32, 0)); - nm_ip4_route_set_prefix (route, g_array_index (array, guint32, 1)); - nm_ip4_route_set_next_hop (route, g_array_index (array, guint32, 2)); - nm_ip4_route_set_metric (route, g_array_index (array, guint32, 3)); - list = g_slist_prepend (list, route); + route = nm_ip_route_new_binary (AF_INET, + &array_val[0], array_val[1], + &array_val[2], array_val[3], + &error); + if (route) + list = g_slist_prepend (list, route); + else { + g_warning ("Ignoring invalid IP4 route: %s", error->message); + g_clear_error (&error); + } } return g_slist_reverse (list); @@ -2445,7 +2451,8 @@ nm_utils_ip6_routes_from_gvalue (const GValue *value) GValueArray *route_values = (GValueArray *) g_ptr_array_index (routes, i); GByteArray *dest, *next_hop; guint prefix, metric; - NMIP6Route *route; + NMIPRoute *route; + GError *error = NULL; if (!_nm_utils_gvalue_array_validate (route_values, 4, DBUS_TYPE_G_UCHAR_ARRAY, @@ -2474,12 +2481,16 @@ nm_utils_ip6_routes_from_gvalue (const GValue *value) metric = g_value_get_uint (g_value_array_get_nth (route_values, 3)); - route = nm_ip6_route_new (); - nm_ip6_route_set_dest (route, (struct in6_addr *)dest->data); - nm_ip6_route_set_prefix (route, prefix); - nm_ip6_route_set_next_hop (route, (struct in6_addr *)next_hop->data); - nm_ip6_route_set_metric (route, metric); - list = g_slist_prepend (list, route); + route = nm_ip_route_new_binary (AF_INET6, + dest->data, prefix, + next_hop->data, metric, + &error); + if (route) + list = g_slist_prepend (list, route); + else { + g_warning ("Ignoring invalid IP6 route: %s", error->message); + g_clear_error (&error); + } } return g_slist_reverse (list); diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index af5e52750c..6071b9527d 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -2873,11 +2873,11 @@ reserve_shared_ip (NMDevice *self, NMSettingIP4Config *s_ip4, NMPlatformIP4Addre if (s_ip4 && nm_setting_ip4_config_get_num_addresses (s_ip4)) { /* Use the first user-supplied address */ - NMIP4Address *user = nm_setting_ip4_config_get_address (s_ip4, 0); + NMIPAddress *user = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (user); - address->address = nm_ip4_address_get_address (user); - address->plen = nm_ip4_address_get_prefix (user); + nm_ip_address_get_address_binary (user, &address->address); + address->plen = nm_ip_address_get_prefix (user); } else { /* Find an unused address in the 10.42.x.x range */ guint32 start = (guint32) ntohl (0x0a2a0001); /* 10.42.0.1 */ @@ -4650,8 +4650,7 @@ send_arps (NMDevice *self, const char *mode_arg) NMConnection *connection; NMSettingIP4Config *s_ip4; int i, num; - NMIP4Address *addr; - guint32 ipaddr; + NMIPAddress *addr; GError *error = NULL; connection = nm_device_get_connection (self); @@ -4673,8 +4672,7 @@ send_arps (NMDevice *self, const char *mode_arg) for (i = 0; i < num; i++) { gs_free char *tmp_str = NULL; addr = nm_setting_ip4_config_get_address (s_ip4, i); - ipaddr = nm_ip4_address_get_address (addr); - argv[ip_arg] = (char *) nm_utils_inet4_ntop (ipaddr, NULL); + argv[ip_arg] = nm_ip_address_get_address (addr); _LOGD (LOGD_DEVICE | LOGD_IP4, "arping: run %s", (tmp_str = g_strjoinv (" ", (char **) argv))); diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 343aabcd0b..46b8fb1983 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -317,9 +317,11 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i else if (nm_setting_ip4_config_get_ignore_auto_routes (setting)) nm_ip4_config_set_never_default (config, FALSE); for (i = 0; i < naddresses; i++) { - guint32 gateway = nm_ip4_address_get_gateway (nm_setting_ip4_config_get_address (setting, i)); + const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip4_config_get_address (setting, i)); + guint32 gateway; - if (gateway) { + if (gateway_str) { + inet_pton (AF_INET, gateway_str, &gateway); nm_ip4_config_set_gateway (config, gateway); break; } @@ -327,13 +329,13 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i /* Addresses */ for (i = 0; i < naddresses; i++) { - NMIP4Address *s_addr = nm_setting_ip4_config_get_address (setting, i); + NMIPAddress *s_addr = nm_setting_ip4_config_get_address (setting, i); const char *label = _nm_setting_ip4_config_get_address_label (setting, i); NMPlatformIP4Address address; memset (&address, 0, sizeof (address)); - address.address = nm_ip4_address_get_address (s_addr); - address.plen = nm_ip4_address_get_prefix (s_addr); + nm_ip_address_get_address_binary (s_addr, &address.address); + address.plen = nm_ip_address_get_prefix (s_addr); address.lifetime = NM_PLATFORM_LIFETIME_PERMANENT; address.preferred = NM_PLATFORM_LIFETIME_PERMANENT; address.source = NM_IP_CONFIG_SOURCE_USER; @@ -346,14 +348,14 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i if (nm_setting_ip4_config_get_ignore_auto_routes (setting)) nm_ip4_config_reset_routes (config); for (i = 0; i < nroutes; i++) { - NMIP4Route *s_route = nm_setting_ip4_config_get_route (setting, i); + NMIPRoute *s_route = nm_setting_ip4_config_get_route (setting, i); NMPlatformIP4Route route; memset (&route, 0, sizeof (route)); - route.network = nm_ip4_route_get_dest (s_route); - route.plen = nm_ip4_route_get_prefix (s_route); - route.gateway = nm_ip4_route_get_next_hop (s_route); - route.metric = nm_ip4_route_get_metric (s_route); + nm_ip_route_get_dest_binary (s_route, &route.network); + route.plen = nm_ip_route_get_prefix (s_route); + nm_ip_route_get_next_hop_binary (s_route, &route.gateway); + route.metric = nm_ip_route_get_metric (s_route); if (!route.metric) route.metric = default_route_metric; route.source = NM_IP_CONFIG_SOURCE_USER; @@ -406,7 +408,7 @@ nm_ip4_config_create_setting (const NMIP4Config *config) /* Addresses */ for (i = 0; i < naddresses; i++) { const NMPlatformIP4Address *address = nm_ip4_config_get_address (config, i); - NMIP4Address *s_addr; + NMIPAddress *s_addr; /* Detect dynamic address */ if (address->lifetime != NM_PLATFORM_LIFETIME_PERMANENT) { @@ -418,18 +420,15 @@ nm_ip4_config_create_setting (const NMIP4Config *config) if (!method) method = NM_SETTING_IP4_CONFIG_METHOD_MANUAL; - s_addr = nm_ip4_address_new (); - - nm_ip4_address_set_address (s_addr, address->address); - nm_ip4_address_set_prefix (s_addr, address->plen); + s_addr = nm_ip_address_new_binary (AF_INET, &address->address, address->plen, NULL, NULL); /* For backwards compatibility, attach the gateway to an address if it's * in the same subnet. */ if (same_prefix (address->address, gateway, address->plen)) - nm_ip4_address_set_gateway (s_addr, gateway); + nm_ip_address_set_gateway (s_addr, nm_utils_inet4_ntop (gateway, NULL)); _nm_setting_ip4_config_add_address_with_label (s_ip4, s_addr, address->label); - nm_ip4_address_unref (s_addr); + nm_ip_address_unref (s_addr); } /* Use 'disabled' if the method wasn't previously set */ @@ -440,7 +439,7 @@ nm_ip4_config_create_setting (const NMIP4Config *config) /* Routes */ for (i = 0; i < nroutes; i++) { const NMPlatformIP4Route *route = nm_ip4_config_get_route (config, i); - NMIP4Route *s_route; + NMIPRoute *s_route; /* Ignore default route. */ if (!route->plen) @@ -450,14 +449,12 @@ nm_ip4_config_create_setting (const NMIP4Config *config) if (route->source != NM_IP_CONFIG_SOURCE_USER) continue; - s_route = nm_ip4_route_new (); - nm_ip4_route_set_dest (s_route, route->network); - nm_ip4_route_set_prefix (s_route, route->plen); - nm_ip4_route_set_next_hop (s_route, route->gateway); - nm_ip4_route_set_metric (s_route, route->metric); - + s_route = nm_ip_route_new_binary (AF_INET, + &route->network, route->plen, + &route->gateway, route->metric, + NULL); nm_setting_ip4_config_add_route (s_ip4, s_route); - nm_ip4_route_unref (s_route); + nm_ip_route_unref (s_route); } /* DNS */ diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index d18b35c6a3..ed7d41b46b 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -421,22 +421,24 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, i else if (nm_setting_ip6_config_get_ignore_auto_routes (setting)) nm_ip6_config_set_never_default (config, FALSE); for (i = 0; i < naddresses; i++) { - const struct in6_addr *gateway = nm_ip6_address_get_gateway (nm_setting_ip6_config_get_address (setting, i)); + const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip6_config_get_address (setting, i)); + struct in6_addr gateway; - if (gateway && !IN6_IS_ADDR_UNSPECIFIED (gateway)) { - nm_ip6_config_set_gateway (config, gateway); + if (gateway_str) { + inet_pton (AF_INET6, gateway_str, &gateway); + nm_ip6_config_set_gateway (config, &gateway); break; } } /* Addresses */ for (i = 0; i < naddresses; i++) { - NMIP6Address *s_addr = nm_setting_ip6_config_get_address (setting, i); + NMIPAddress *s_addr = nm_setting_ip6_config_get_address (setting, i); NMPlatformIP6Address address; memset (&address, 0, sizeof (address)); - address.address = *nm_ip6_address_get_address (s_addr); - address.plen = nm_ip6_address_get_prefix (s_addr); + nm_ip_address_get_address_binary (s_addr, &address.address); + address.plen = nm_ip_address_get_prefix (s_addr); address.lifetime = NM_PLATFORM_LIFETIME_PERMANENT; address.preferred = NM_PLATFORM_LIFETIME_PERMANENT; address.source = NM_IP_CONFIG_SOURCE_USER; @@ -448,14 +450,14 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, i if (nm_setting_ip6_config_get_ignore_auto_routes (setting)) nm_ip6_config_reset_routes (config); for (i = 0; i < nroutes; i++) { - NMIP6Route *s_route = nm_setting_ip6_config_get_route (setting, i); + NMIPRoute *s_route = nm_setting_ip6_config_get_route (setting, i); NMPlatformIP6Route route; memset (&route, 0, sizeof (route)); - route.network = *nm_ip6_route_get_dest (s_route); - route.plen = nm_ip6_route_get_prefix (s_route); - route.gateway = *nm_ip6_route_get_next_hop (s_route); - route.metric = nm_ip6_route_get_metric (s_route); + nm_ip_route_get_dest_binary (s_route, &route.network); + route.plen = nm_ip_route_get_prefix (s_route); + nm_ip_route_get_next_hop_binary (s_route, &route.gateway); + route.metric = nm_ip_route_get_metric (s_route); if (!route.metric) route.metric = default_route_metric; route.source = NM_IP_CONFIG_SOURCE_USER; @@ -508,7 +510,7 @@ nm_ip6_config_create_setting (const NMIP6Config *config) /* Addresses */ for (i = 0; i < naddresses; i++) { const NMPlatformIP6Address *address = nm_ip6_config_get_address (config, i); - NMIP6Address *s_addr; + NMIPAddress *s_addr; /* Ignore link-local address. */ if (IN6_IS_ADDR_LINKLOCAL (&address->address)) { @@ -527,15 +529,9 @@ nm_ip6_config_create_setting (const NMIP6Config *config) if (!method || strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0) method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL; - s_addr = nm_ip6_address_new (); - - nm_ip6_address_set_address (s_addr, &address->address); - nm_ip6_address_set_prefix (s_addr, address->plen); - if (gateway) - nm_ip6_address_set_gateway (s_addr, gateway); - + s_addr = nm_ip_address_new_binary (AF_INET6, &address->address, address->plen, gateway, NULL); nm_setting_ip6_config_add_address (s_ip6, s_addr); - nm_ip6_address_unref (s_addr); + nm_ip_address_unref (s_addr); } /* Use 'ignore' if the method wasn't previously set */ @@ -546,7 +542,7 @@ nm_ip6_config_create_setting (const NMIP6Config *config) /* Routes */ for (i = 0; i < nroutes; i++) { const NMPlatformIP6Route *route = nm_ip6_config_get_route (config, i); - NMIP6Route *s_route; + NMIPRoute *s_route; /* Ignore link-local route. */ if (IN6_IS_ADDR_LINKLOCAL (&route->network)) @@ -560,14 +556,12 @@ nm_ip6_config_create_setting (const NMIP6Config *config) if (route->source != NM_IP_CONFIG_SOURCE_USER) continue; - s_route = nm_ip6_route_new (); - nm_ip6_route_set_dest (s_route, &route->network); - nm_ip6_route_set_prefix (s_route, route->plen); - nm_ip6_route_set_next_hop (s_route, &route->gateway); - nm_ip6_route_set_metric (s_route, route->metric); - + s_route = nm_ip_route_new_binary (AF_INET6, + &route->network, route->plen, + &route->gateway, route->metric, + NULL); nm_setting_ip6_config_add_route (s_ip6, s_route); - nm_ip6_route_unref (s_route); + nm_ip_route_unref (s_route); } /* DNS */ diff --git a/src/settings/plugins/ibft/reader.c b/src/settings/plugins/ibft/reader.c index aac7715a7a..be9e249678 100644 --- a/src/settings/plugins/ibft/reader.c +++ b/src/settings/plugins/ibft/reader.c @@ -268,18 +268,14 @@ ip4_setting_add_from_block (const GPtrArray *block, GError **error) { NMSettingIP4Config *s_ip4 = NULL; - NMIP4Address *addr; + NMIPAddress *addr; const char *s_method = NULL; const char *s_ipaddr = NULL; const char *s_gateway = NULL; const char *s_dns1 = NULL; const char *s_dns2 = NULL; const char *s_netmask = NULL; - guint32 ipaddr = 0; guint32 netmask = 0; - guint32 gateway = 0; - guint32 dns1 = 0; - guint32 dns2 = 0; guint32 prefix; g_assert (block); @@ -316,7 +312,7 @@ ip4_setting_add_from_block (const GPtrArray *block, g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); /* IP address */ - if (!s_ipaddr || inet_pton (AF_INET, s_ipaddr, &ipaddr) != 1) { + if (!s_ipaddr || !nm_utils_ipaddr_valid (AF_INET, s_ipaddr)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "iBFT: malformed iscsiadm record: invalid IP address '%s'.", s_ipaddr); @@ -332,33 +328,35 @@ ip4_setting_add_from_block (const GPtrArray *block, } prefix = nm_utils_ip4_netmask_to_prefix (netmask); - if (s_gateway && inet_pton (AF_INET, s_gateway, &gateway) != 1) { + if (s_gateway && !nm_utils_ipaddr_valid (AF_INET, s_gateway)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "iBFT: malformed iscsiadm record: invalid IP gateway '%s'.", s_gateway); goto error; } - if (s_dns1 && inet_pton (AF_INET, s_dns1, &dns1) != 1) { + if (s_dns1 && !nm_utils_ipaddr_valid (AF_INET, s_dns1)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "iBFT: malformed iscsiadm record: invalid DNS1 address '%s'.", s_dns1); goto error; } - if (s_dns2 && inet_pton (AF_INET, s_dns2, &dns2) != 1) { + if (s_dns2 && !nm_utils_ipaddr_valid (AF_INET, s_dns2)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "iBFT: malformed iscsiadm record: invalid DNS2 address '%s'.", s_dns2); goto error; } - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ipaddr); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gateway); + addr = nm_ip_address_new (AF_INET, s_ipaddr, prefix, s_gateway, error); + if (!addr) { + g_prefix_error (error, "iBFT: malformed iscsiadm record: "); + goto error; + } + nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); if (s_dns1) nm_setting_ip4_config_add_dns (s_ip4, s_dns1); diff --git a/src/settings/plugins/ibft/tests/test-ibft.c b/src/settings/plugins/ibft/tests/test-ibft.c index 3fb64ac4bd..32e1eb6a8b 100644 --- a/src/settings/plugins/ibft/tests/test-ibft.c +++ b/src/settings/plugins/ibft/tests/test-ibft.c @@ -120,7 +120,7 @@ test_read_ibft_static (void) GError *error = NULL; const char *mac_address; const char *expected_mac_address = "00:33:21:98:b9:f0"; - NMIP4Address *ip4_addr; + NMIPAddress *ip4_addr; GPtrArray *block; block = read_block (TEST_IBFT_DIR "/iscsiadm-test-static", expected_mac_address); @@ -160,9 +160,9 @@ test_read_ibft_static (void) g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (ip4_addr); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_address (ip4_addr), "192.168.32.72"); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 22); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_gateway (ip4_addr), "192.168.35.254"); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.32.72"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 22); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.35.254"); g_object_unref (connection); g_ptr_array_unref (block); @@ -220,7 +220,7 @@ test_read_ibft_vlan (void) NMSettingIP4Config *s_ip4; const char *mac_address; const char *expected_mac_address = "00:33:21:98:b9:f0"; - NMIP4Address *ip4_addr; + NMIPAddress *ip4_addr; GError *error = NULL; GPtrArray *block; @@ -257,9 +257,9 @@ test_read_ibft_vlan (void) g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (ip4_addr); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_address (ip4_addr), "192.168.6.200"); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 24); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_gateway (ip4_addr), "0.0.0.0"); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.6.200"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, NULL); g_object_unref (connection); g_ptr_array_ref (block); diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index 6994f6af93..4dc64b8cfd 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -255,12 +255,10 @@ make_connection_setting (const char *file, static gboolean read_ip4_address (shvarFile *ifcfg, const char *tag, - guint32 *out_addr, + char **out_addr, GError **error) { char *value = NULL; - guint32 ip4_addr; - gboolean success = FALSE; g_return_val_if_fail (ifcfg != NULL, FALSE); g_return_val_if_fail (tag != NULL, FALSE); @@ -268,45 +266,21 @@ read_ip4_address (shvarFile *ifcfg, if (error) g_return_val_if_fail (*error == NULL, FALSE); - *out_addr = 0; + *out_addr = NULL; value = svGetValue (ifcfg, tag, FALSE); if (!value) return TRUE; - if (inet_pton (AF_INET, value, &ip4_addr) > 0) { - *out_addr = ip4_addr; - success = TRUE; + if (nm_utils_ipaddr_valid (AF_INET, value)) { + *out_addr = value; + return TRUE; } else { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid %s IP4 address '%s'", tag, value); - } - g_free (value); - return success; -} - -/* Returns TRUE on valid address, including unspecified (::) */ -static gboolean -parse_ip6_address (const char *value, - struct in6_addr *out_addr, - GError **error) -{ - struct in6_addr ip6_addr; - - g_return_val_if_fail (value != NULL, FALSE); - g_return_val_if_fail (out_addr != NULL, FALSE); - if (error) - g_return_val_if_fail (*error == NULL, FALSE); - - *out_addr = in6addr_any; - if (inet_pton (AF_INET6, value, &ip6_addr) <= 0) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid IP6 address '%s'", value); + g_free (value); return FALSE; } - - *out_addr = ip6_addr; - return TRUE; } static char * @@ -358,19 +332,23 @@ static gboolean read_full_ip4_address (shvarFile *ifcfg, const char *network_file, gint32 which, - NMIP4Address *addr, + NMIPAddress *base_addr, + NMIPAddress **out_address, GError **error) { char *ip_tag, *prefix_tag, *netmask_tag, *gw_tag; - guint32 tmp; + char *ip = NULL, *gw = NULL; + long prefix = 0; gboolean success = FALSE; shvarFile *network_ifcfg; char *value; + guint32 tmp; g_return_val_if_fail (which >= -1, FALSE); g_return_val_if_fail (ifcfg != NULL, FALSE); g_return_val_if_fail (network_file != NULL, FALSE); - g_return_val_if_fail (addr != NULL, FALSE); + g_return_val_if_fail (out_address != NULL, FALSE); + g_return_val_if_fail (*out_address == NULL, FALSE); if (error) g_return_val_if_fail (*error == NULL, FALSE); @@ -380,82 +358,86 @@ read_full_ip4_address (shvarFile *ifcfg, gw_tag = get_numbered_tag ("GATEWAY", which); /* IP address */ - if (!read_ip4_address (ifcfg, ip_tag, &tmp, error)) - goto done; - if (tmp) - nm_ip4_address_set_address (addr, tmp); - else if (!nm_ip4_address_get_address (addr)) { - success = TRUE; + if (!read_ip4_address (ifcfg, ip_tag, &ip, error)) goto done; + if (!ip) { + if (base_addr) + ip = g_strdup (nm_ip_address_get_address (base_addr)); + else { + success = TRUE; + goto done; + } } /* Gateway */ - if (!read_ip4_address (ifcfg, gw_tag, &tmp, error)) + if (!read_ip4_address (ifcfg, gw_tag, &gw, error)) goto done; - if (tmp) - nm_ip4_address_set_gateway (addr, tmp); - else { + if (!gw && base_addr) + gw = g_strdup (nm_ip_address_get_gateway (base_addr)); + if (!gw) { gboolean read_success; /* If no gateway in the ifcfg, try /etc/sysconfig/network instead */ network_ifcfg = svOpenFile (network_file, NULL); if (network_ifcfg) { - read_success = read_ip4_address (network_ifcfg, "GATEWAY", &tmp, error); + read_success = read_ip4_address (network_ifcfg, "GATEWAY", &gw, error); svCloseFile (network_ifcfg); if (!read_success) goto done; - nm_ip4_address_set_gateway (addr, tmp); } } /* Prefix */ value = svGetValue (ifcfg, prefix_tag, FALSE); if (value) { - long int prefix; - errno = 0; prefix = strtol (value, NULL, 10); - if (errno || prefix <= 0 || prefix > 32) { + if (errno || prefix < 0) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP4 prefix '%s'", value); g_free (value); goto done; } - nm_ip4_address_set_prefix (addr, (guint32) prefix); g_free (value); } /* Fall back to NETMASK if no PREFIX was specified */ - if (!nm_ip4_address_get_prefix (addr)) { - if (!read_ip4_address (ifcfg, netmask_tag, &tmp, error)) + if (prefix == 0) { + if (!read_ip4_address (ifcfg, netmask_tag, &value, error)) goto done; - if (tmp) - nm_ip4_address_set_prefix (addr, nm_utils_ip4_netmask_to_prefix (tmp)); + if (value) { + inet_pton (AF_INET, value, &tmp); + prefix = nm_utils_ip4_netmask_to_prefix (tmp); + g_free (value); + } } - /* Try to autodetermine the prefix for the address' class */ - if (!nm_ip4_address_get_prefix (addr)) { - guint32 prefix = 0; + if (prefix == 0 && base_addr) + prefix = nm_ip_address_get_prefix (base_addr); - prefix = nm_utils_ip4_get_default_prefix (nm_ip4_address_get_address (addr)); - nm_ip4_address_set_prefix (addr, prefix); + /* Try to autodetermine the prefix for the address' class */ + if (prefix == 0) { + if (inet_pton (AF_INET, ip, &tmp) == 1) { + prefix = nm_utils_ip4_get_default_prefix (tmp); - value = svGetValue (ifcfg, ip_tag, FALSE); - PARSE_WARNING ("missing %s, assuming %s/%u", prefix_tag, value, prefix); - g_free (value); + PARSE_WARNING ("missing %s, assuming %s/%ld", prefix_tag, ip, prefix); + } } /* Validate the prefix */ - if (nm_ip4_address_get_prefix (addr) > 32) { + if (prefix == 0) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Missing or invalid IP4 prefix '%d'", - nm_ip4_address_get_prefix (addr)); + "Missing IP4 prefix"); goto done; } - success = TRUE; + *out_address = nm_ip_address_new (AF_INET, ip, prefix, gw, error); + if (*out_address) + success = TRUE; done: + g_free (ip); + g_free (gw); g_free (ip_tag); g_free (prefix_tag); g_free (netmask_tag); @@ -469,12 +451,12 @@ static gboolean read_one_ip4_route (shvarFile *ifcfg, const char *network_file, guint32 which, - NMIP4Route **out_route, + NMIPRoute **out_route, GError **error) { - NMIP4Route *route; char *ip_tag, *netmask_tag, *gw_tag, *metric_tag, *value; - guint32 tmp; + char *dest = NULL, *next_hop = NULL; + long prefix, metric; gboolean success = FALSE; g_return_val_if_fail (ifcfg != NULL, FALSE); @@ -484,75 +466,72 @@ read_one_ip4_route (shvarFile *ifcfg, if (error) g_return_val_if_fail (*error == NULL, FALSE); - route = nm_ip4_route_new (); - ip_tag = g_strdup_printf ("ADDRESS%u", which); netmask_tag = g_strdup_printf ("NETMASK%u", which); gw_tag = g_strdup_printf ("GATEWAY%u", which); metric_tag = g_strdup_printf ("METRIC%u", which); /* Destination */ - if (!read_ip4_address (ifcfg, ip_tag, &tmp, error)) + if (!read_ip4_address (ifcfg, ip_tag, &dest, error)) goto out; - if (!tmp) { + if (!dest) { /* Check whether IP is missing or 0.0.0.0 */ char *val; val = svGetValue (ifcfg, ip_tag, FALSE); if (!val) { - nm_ip4_route_unref (route); - route = NULL; + *out_route = NULL; success = TRUE; /* missing route = success */ goto out; } g_free (val); } - nm_ip4_route_set_dest (route, tmp); /* Next hop */ - if (!read_ip4_address (ifcfg, gw_tag, &tmp, error)) + if (!read_ip4_address (ifcfg, gw_tag, &next_hop, error)) goto out; - /* No need to check tmp, because we don't make distinction between missing GATEWAY IP and 0.0.0.0 */ - nm_ip4_route_set_next_hop (route, tmp); + /* We don't make distinction between missing GATEWAY IP and 0.0.0.0 */ /* Prefix */ - if (!read_ip4_address (ifcfg, netmask_tag, &tmp, error)) + if (!read_ip4_address (ifcfg, netmask_tag, &value, error)) goto out; - if (tmp) - nm_ip4_route_set_prefix (route, nm_utils_ip4_netmask_to_prefix (tmp)); + if (value) { + guint32 netmask; - /* Validate the prefix */ - if ( !nm_ip4_route_get_prefix (route) - || nm_ip4_route_get_prefix (route) > 32) { + inet_pton (AF_INET, value, &netmask); + prefix = nm_utils_ip4_netmask_to_prefix (netmask); + g_free (value); + if (netmask != nm_utils_ip4_prefix_to_netmask (prefix)) { + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, + "Invalid IP4 netmask '%s' \"%s\"", netmask_tag, nm_utils_inet4_ntop (netmask, NULL)); + goto out; + } + } else { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Missing or invalid IP4 prefix '%d'", - nm_ip4_route_get_prefix (route)); + "Missing IP4 route element '%s'", netmask_tag); goto out; } /* Metric */ value = svGetValue (ifcfg, metric_tag, FALSE); if (value) { - long int metric; - - errno = 0; - metric = strtol (value, NULL, 10); - if (errno || metric < 0) { + metric = nm_utils_ascii_str_to_int64 (value, 10, 0, G_MAXUINT32, -1); + if (metric < 0) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP4 route metric '%s'", value); g_free (value); goto out; } - nm_ip4_route_set_metric (route, (guint32) metric); g_free (value); - } + } else + metric = 0; - *out_route = route; - success = TRUE; + *out_route = nm_ip_route_new (AF_INET, dest, prefix, next_hop, metric, error); + if (*out_route) + success = TRUE; out: - if (!success && route) - nm_ip4_route_unref (route); - + g_free (dest); + g_free (next_hop); g_free (ip_tag); g_free (netmask_tag); g_free (gw_tag); @@ -568,9 +547,8 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError char **lines = NULL, **iter; GRegex *regex_to1, *regex_to2, *regex_via, *regex_metric; GMatchInfo *match_info; - NMIP4Route *route; - guint32 ip4_addr; - char *dest = NULL, *prefix = NULL, *metric = NULL; + NMIPRoute *route = NULL; + char *dest = NULL, *prefix = NULL, *next_hop = NULL, *metric = NULL; long int prefix_int, metric_int; gboolean success = FALSE; @@ -599,9 +577,6 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError regex_via = g_regex_new (pattern_via, 0, 0, NULL); regex_metric = g_regex_new (pattern_metric, 0, 0, NULL); - /* New NMIP4Route structure */ - route = nm_ip4_route_new (); - /* Iterate through file lines */ lines = g_strsplit_set (contents, "\n\r", -1); for (iter = lines; iter && *iter; iter++) { @@ -625,14 +600,13 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError dest = g_match_info_fetch (match_info, 1); if (!strcmp (dest, "default")) strcpy (dest, "0.0.0.0"); - if (inet_pton (AF_INET, dest, &ip4_addr) != 1) { + if (!nm_utils_ipaddr_valid (AF_INET, dest)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP4 route destination address '%s'", dest); g_free (dest); + g_match_info_free (match_info); goto error; } - nm_ip4_route_set_dest (route, ip4_addr); - g_free (dest); /* Prefix - is optional; 32 if missing */ prefix = g_match_info_fetch (match_info, 2); @@ -644,31 +618,30 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError if (errno || prefix_int <= 0 || prefix_int > 32) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP4 route destination prefix '%s'", prefix); + g_free (dest); g_free (prefix); goto error; } } - nm_ip4_route_set_prefix (route, (guint32) prefix_int); g_free (prefix); /* Next hop */ g_regex_match (regex_via, *iter, 0, &match_info); if (g_match_info_matches (match_info)) { - char *next_hop = g_match_info_fetch (match_info, 1); - if (inet_pton (AF_INET, next_hop, &ip4_addr) != 1) { + next_hop = g_match_info_fetch (match_info, 1); + if (!nm_utils_ipaddr_valid (AF_INET, next_hop)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP4 route gateway address '%s'", next_hop); g_match_info_free (match_info); + g_free (dest); g_free (next_hop); goto error; } - g_free (next_hop); } else { /* we don't make distinction between missing GATEWAY IP and 0.0.0.0 */ - ip4_addr = 0; + next_hop = NULL; } - nm_ip4_route_set_next_hop (route, ip4_addr); g_match_info_free (match_info); /* Metric */ @@ -682,18 +655,23 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError g_match_info_free (match_info); g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP4 route metric '%s'", metric); + g_free (dest); + g_free (next_hop); g_free (metric); goto error; } g_free (metric); } - - nm_ip4_route_set_metric (route, (guint32) metric_int); g_match_info_free (match_info); + route = nm_ip_route_new (AF_INET, dest, prefix_int, next_hop, metric_int, error); + if (!route) { + g_free (dest); + g_free (next_hop); + goto error; + } if (!nm_setting_ip4_config_add_route (s_ip4, route)) PARSE_WARNING ("duplicate IP4 route"); - } success = TRUE; @@ -701,7 +679,8 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError error: g_free (contents); g_strfreev (lines); - nm_ip4_route_unref (route); + if (route) + nm_ip_route_unref (route); g_regex_unref (regex_to1); g_regex_unref (regex_to2); g_regex_unref (regex_via); @@ -715,15 +694,13 @@ parse_full_ip6_address (shvarFile *ifcfg, const char *network_file, const char *addr_str, int i, - NMIP6Address **out_address, + NMIPAddress **out_address, GError **error) { - NMIP6Address *addr = NULL; char **list; - char *ip_val, *prefix_val; + char *ip_val, *prefix_val, *gateway_val = NULL; + long prefix; shvarFile *network_ifcfg; - char *value = NULL; - struct in6_addr tmp = IN6ADDR_ANY_INIT; gboolean success = FALSE; g_return_val_if_fail (addr_str != NULL, FALSE); @@ -741,23 +718,9 @@ parse_full_ip6_address (shvarFile *ifcfg, } ip_val = list[0]; - prefix_val = list[1]; - - addr = nm_ip6_address_new (); - /* IP address */ - if (!parse_ip6_address (ip_val, &tmp, error)) - goto error; - if (IN6_IS_ADDR_UNSPECIFIED (&tmp)) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid IP6 address '%s'", ip_val); - goto error; - } - nm_ip6_address_set_address (addr, &tmp); - /* Prefix */ + prefix_val = list[1]; if (prefix_val) { - long int prefix; - errno = 0; prefix = strtol (prefix_val, NULL, 10); if (errno || prefix <= 0 || prefix > 128) { @@ -765,47 +728,38 @@ parse_full_ip6_address (shvarFile *ifcfg, "Invalid IP6 prefix '%s'", prefix_val); goto error; } - nm_ip6_address_set_prefix (addr, (guint32) prefix); } else { /* Missing prefix is treated as prefix of 64 */ - nm_ip6_address_set_prefix (addr, 64); + prefix = 64; } /* Gateway */ - tmp = in6addr_any; - value = svGetValue (ifcfg, "IPV6_DEFAULTGW", FALSE); - if (i != 0) { - /* We don't support gateways for IPV6ADDR_SECONDARIES yet */ - g_free (value); - value = NULL; - } - if (!value) { - /* If no gateway in the ifcfg, try global /etc/sysconfig/network instead */ - network_ifcfg = svOpenFile (network_file, NULL); - if (network_ifcfg) { - value = svGetValue (network_ifcfg, "IPV6_DEFAULTGW", FALSE); - svCloseFile (network_ifcfg); - } - } - if (value) { + if (i == 0) { char *ptr; - if ((ptr = strchr (value, '%')) != NULL) - *ptr = '\0'; /* remove %interface prefix if present */ - if (!parse_ip6_address (value, &tmp, error)) - goto error; - nm_ip6_address_set_gateway (addr, &tmp); - } + gateway_val = svGetValue (ifcfg, "IPV6_DEFAULTGW", FALSE); + if (!gateway_val) { + /* If no gateway in the ifcfg, try global /etc/sysconfig/network instead */ + network_ifcfg = svOpenFile (network_file, NULL); + if (network_ifcfg) { + gateway_val = svGetValue (network_ifcfg, "IPV6_DEFAULTGW", FALSE); + svCloseFile (network_ifcfg); + } + } - *out_address = addr; - success = TRUE; + if ( gateway_val + && (ptr = strchr (gateway_val, '%')) != NULL) + *ptr = '\0'; /* remove %interface suffix if present */ + } else + gateway_val = NULL; -error: - if (!success && addr) - nm_ip6_address_unref (addr); + *out_address = nm_ip_address_new (AF_INET6, ip_val, prefix, gateway_val, error); + if (*out_address) + success = TRUE; +error: g_strfreev (list); - g_free (value); + g_free (gateway_val); return success; } @@ -824,9 +778,8 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro char **lines = NULL, **iter; GRegex *regex_to1, *regex_to2, *regex_via, *regex_metric; GMatchInfo *match_info; - NMIP6Route *route; - struct in6_addr ip6_addr; - char *dest = NULL, *prefix = NULL, *metric = NULL; + NMIPRoute *route = NULL; + char *dest = NULL, *prefix = NULL, *next_hop = NULL, *metric = NULL; long int prefix_int, metric_int; gboolean success = FALSE; @@ -855,9 +808,6 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro regex_via = g_regex_new (pattern_via, 0, 0, NULL); regex_metric = g_regex_new (pattern_metric, 0, 0, NULL); - /* New NMIP6Route structure */ - route = nm_ip6_route_new (); - /* Iterate through file lines */ lines = g_strsplit_set (contents, "\n\r", -1); for (iter = lines; iter && *iter; iter++) { @@ -881,19 +831,11 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro dest = g_match_info_fetch (match_info, 1); if (!g_strcmp0 (dest, "default")) { /* Ignore default route - NM handles it internally */ - g_free (dest); + g_clear_pointer (&dest, g_free); g_match_info_free (match_info); PARSE_WARNING ("ignoring manual default route: '%s' (%s)", *iter, filename); continue; } - if (inet_pton (AF_INET6, dest, &ip6_addr) != 1) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid IP6 route destination address '%s'", dest); - g_free (dest); - goto error; - } - nm_ip6_route_set_dest (route, &ip6_addr); - g_free (dest); /* Prefix - is optional; 128 if missing */ prefix = g_match_info_fetch (match_info, 2); @@ -905,31 +847,30 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro if (errno || prefix_int <= 0 || prefix_int > 128) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP6 route destination prefix '%s'", prefix); + g_free (dest); g_free (prefix); goto error; } } - nm_ip6_route_set_prefix (route, (guint32) prefix_int); g_free (prefix); /* Next hop */ g_regex_match (regex_via, *iter, 0, &match_info); if (g_match_info_matches (match_info)) { - char *next_hop = g_match_info_fetch (match_info, 1); - if (inet_pton (AF_INET6, next_hop, &ip6_addr) != 1) { + next_hop = g_match_info_fetch (match_info, 1); + if (!nm_utils_ipaddr_valid (AF_INET6, next_hop)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IPv6 route nexthop address '%s'", next_hop); g_match_info_free (match_info); + g_free (dest); g_free (next_hop); goto error; } - g_free (next_hop); } else { /* Missing "via" is taken as :: */ - ip6_addr = in6addr_any; + next_hop = NULL; } - nm_ip6_route_set_next_hop (route, &ip6_addr); g_match_info_free (match_info); /* Metric */ @@ -943,15 +884,20 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro g_match_info_free (match_info); g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IP6 route metric '%s'", metric); + g_free (dest); + g_free (next_hop); g_free (metric); goto error; } g_free (metric); } - - nm_ip6_route_set_metric (route, (guint32) metric_int); g_match_info_free (match_info); + route = nm_ip_route_new (AF_INET6, dest, prefix_int, next_hop, metric_int, error); + g_free (dest); + g_free (next_hop); + if (!route) + goto error; if (!nm_setting_ip6_config_add_route (s_ip6, route)) PARSE_WARNING ("duplicate IP6 route"); } @@ -961,7 +907,8 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro error: g_free (contents); g_strfreev (lines); - nm_ip6_route_unref (route); + if (route) + nm_ip_route_unref (route); g_regex_unref (regex_to1); g_regex_unref (regex_to2); g_regex_unref (regex_via); @@ -1082,16 +1029,12 @@ make_ip4_setting (shvarFile *ifcfg, * the legacy 'network' service (ifup-eth). */ for (i = -1; i < 256; i++) { - NMIP4Address *addr = NULL; + NMIPAddress *addr = NULL; - addr = nm_ip4_address_new (); - if (!read_full_ip4_address (ifcfg, network_file, i, addr, error)) { - nm_ip4_address_unref (addr); + if (!read_full_ip4_address (ifcfg, network_file, i, NULL, &addr, error)) goto done; - } - if (!nm_ip4_address_get_address (addr)) { - nm_ip4_address_unref (addr); + if (!addr) { /* The first mandatory variable is 2-indexed (IPADDR2) * Variables IPADDR, IPADDR0 and IPADDR1 are optional */ if (i > 1) @@ -1101,7 +1044,7 @@ make_ip4_setting (shvarFile *ifcfg, if (!nm_setting_ip4_config_add_address (s_ip4, addr)) PARSE_WARNING ("duplicate IP4 address"); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); } /* DNS servers @@ -1109,27 +1052,22 @@ make_ip4_setting (shvarFile *ifcfg, */ for (i = 1; i <= 10; i++) { char *tag; - guint32 dns; - struct in6_addr ip6_dns; tag = g_strdup_printf ("DNS%u", i); value = svGetValue (ifcfg, tag, FALSE); if (value) { - if (!read_ip4_address (ifcfg, tag, &dns, error)) { - gboolean valid = TRUE; - + if (nm_utils_ipaddr_valid (AF_INET, value)) { + if (!nm_setting_ip4_config_add_dns (s_ip4, value)) + PARSE_WARNING ("duplicate DNS server %s", tag); + } else if (nm_utils_ipaddr_valid (AF_INET6, value)) { /* Ignore IPv6 addresses */ - valid = parse_ip6_address (value, &ip6_dns, NULL); - if (!valid) { - g_free (tag); - goto done; - } - g_clear_error (error); - dns = 0; + } else { + PARSE_WARNING ("invalid DNS server address %s", value); + g_free (tag); + g_free (value); + goto done; } - if (dns && !nm_setting_ip4_config_add_dns (s_ip4, value)) - PARSE_WARNING ("duplicate DNS server %s", tag); g_free (value); } @@ -1169,7 +1107,7 @@ make_ip4_setting (shvarFile *ifcfg, route_ifcfg = utils_get_route_ifcfg (ifcfg->fileName, FALSE); if (route_ifcfg) { for (i = 0; i < 256; i++) { - NMIP4Route *route = NULL; + NMIPRoute *route = NULL; if (!read_one_ip4_route (route_ifcfg, network_file, i, &route, error)) { svCloseFile (route_ifcfg); @@ -1181,7 +1119,7 @@ make_ip4_setting (shvarFile *ifcfg, if (!nm_setting_ip4_config_add_route (s_ip4, route)) PARSE_WARNING ("duplicate IP4 route"); - nm_ip4_route_unref (route); + nm_ip_route_unref (route); } svCloseFile (route_ifcfg); } @@ -1225,7 +1163,7 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo GDir *dir; char *dirname, *base; shvarFile *parsed; - NMIP4Address *base_addr; + NMIPAddress *base_addr; GError *err = NULL; g_return_if_fail (s_ip4 != NULL); @@ -1244,7 +1182,7 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo dir = g_dir_open (dirname, 0, &err); if (dir) { const char *item; - NMIP4Address *addr; + NMIPAddress *addr; gboolean ok; while ((item = g_dir_read_name (dir))) { @@ -1293,8 +1231,8 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo continue; } - addr = nm_ip4_address_dup (base_addr); - ok = read_full_ip4_address (parsed, network_file, -1, addr, &err); + addr = NULL; + ok = read_full_ip4_address (parsed, network_file, -1, base_addr, &addr, &err); svCloseFile (parsed); if (ok) { if (!_nm_setting_ip4_config_add_address_with_label (s_ip4, addr, device)) @@ -1304,7 +1242,7 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo full_path, err ? err->message : "no address"); g_clear_error (&err); } - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); g_free (device); g_free (full_path); @@ -1476,7 +1414,7 @@ make_ip6_setting (shvarFile *ifcfg, list = g_strsplit_set (value, " ", 0); g_free (value); for (iter = list, i = 0; iter && *iter; iter++, i++) { - NMIP6Address *addr = NULL; + NMIPAddress *addr = NULL; if (!parse_full_ip6_address (ifcfg, network_file, *iter, i, &addr, error)) { g_strfreev (list); @@ -1485,7 +1423,7 @@ make_ip6_setting (shvarFile *ifcfg, if (!nm_setting_ip6_config_add_address (s_ip6, addr)) PARSE_WARNING ("duplicate IP6 address"); - nm_ip6_address_unref (addr); + nm_ip_address_unref (addr); } g_strfreev (list); @@ -1494,8 +1432,6 @@ make_ip6_setting (shvarFile *ifcfg, */ for (i = 1; i <= 10; i++) { char *tag; - struct in6_addr ip6_dns; - guint32 ip4_addr; tag = g_strdup_printf ("DNS%u", i); value = svGetValue (ifcfg, tag, FALSE); @@ -1504,18 +1440,16 @@ make_ip6_setting (shvarFile *ifcfg, break; /* all done */ } - ip6_dns = in6addr_any; - if (parse_ip6_address (value, &ip6_dns, NULL)) { - if (!IN6_IS_ADDR_UNSPECIFIED (&ip6_dns) && !nm_setting_ip6_config_add_dns (s_ip6, value)) + if (nm_utils_ipaddr_valid (AF_INET6, value)) { + if (!nm_setting_ip6_config_add_dns (s_ip6, value)) PARSE_WARNING ("duplicate DNS server %s", tag); + } else if (nm_utils_ipaddr_valid (AF_INET, value)) { + /* Ignore IPv4 addresses */ } else { - /* Maybe an IPv4 address? If so ignore it */ - if (inet_pton (AF_INET, value, &ip4_addr) != 1) { - g_free (tag); - g_free (value); - PARSE_WARNING ("duplicate IP6 address"); - goto error; - } + PARSE_WARNING ("invalid DNS server address %s", value); + g_free (tag); + g_free (value); + goto error; } g_free (tag); diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index b2d91e46f6..0162cb130e 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -446,11 +446,8 @@ test_read_wired_static (const char *file, GError *error = NULL; const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0xee }; - struct in6_addr addr6; - const char *expected6_address1 = "dead:beaf::1"; - const char *expected6_address2 = "dead:beaf::2"; - NMIP4Address *ip4_addr; - NMIP6Address *ip6_addr; + NMIPAddress *ip4_addr; + NMIPAddress *ip6_addr; gboolean success; connection = connection_from_file (file, NULL, TYPE_ETHERNET, @@ -494,9 +491,9 @@ test_read_wired_static (const char *file, g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (ip4_addr); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 24); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_address (ip4_addr), "192.168.1.5"); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_gateway (ip4_addr), "192.168.1.1"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.1.1"); /* ===== IPv6 SETTING ===== */ s_ip6 = nm_connection_get_setting_ip6_config (connection); @@ -515,15 +512,13 @@ test_read_wired_static (const char *file, ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); g_assert (ip6_addr); - g_assert_cmpint (nm_ip6_address_get_prefix (ip6_addr), ==, 64); - g_assert_cmpint (inet_pton (AF_INET6, expected6_address1, &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr6)); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "dead:beaf::1"); ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 1); g_assert (ip6_addr); - g_assert_cmpint (nm_ip6_address_get_prefix (ip6_addr), ==, 56); - g_assert_cmpint (inet_pton (AF_INET6, expected6_address2, &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr6)); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "dead:beaf::2"); } else { g_assert_cmpstr (nm_setting_ip6_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE); } @@ -540,7 +535,7 @@ test_read_wired_static_no_prefix (gconstpointer user_data) NMSettingConnection *s_con; NMSettingIP4Config *s_ip4; GError *error = NULL; - NMIP4Address *ip4_addr; + NMIPAddress *ip4_addr; char *file, *expected_id; file = g_strdup_printf (TEST_IFCFG_DIR "/network-scripts/ifcfg-test-wired-static-no-prefix-%u", expected_prefix); @@ -568,7 +563,7 @@ test_read_wired_static_no_prefix (gconstpointer user_data) g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (ip4_addr); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, expected_prefix); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, expected_prefix); g_free (file); g_free (expected_id); @@ -737,10 +732,8 @@ test_read_wired_dhcp_plus_ip (void) NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; GError *error = NULL; - guint32 addr4; - struct in6_addr addr6; - NMIP4Address *ip4_addr; - NMIP6Address *ip6_addr; + NMIPAddress *ip4_addr; + NMIPAddress *ip6_addr; gboolean success; connection = connection_from_file (TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wired-dhcp-plus-ip", @@ -767,17 +760,14 @@ test_read_wired_dhcp_plus_ip (void) g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (ip4_addr); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 24); - g_assert_cmpint (inet_pton (AF_INET, "1.2.3.4", &addr4), >, 0); - g_assert_cmpint (nm_ip4_address_get_address (ip4_addr), ==, addr4); - g_assert_cmpint (inet_pton (AF_INET, "1.1.1.1", &addr4), >, 0); - g_assert_cmpint (nm_ip4_address_get_gateway (ip4_addr), ==, addr4); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "1.2.3.4"); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "1.1.1.1"); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 1); g_assert (ip4_addr); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 16); - g_assert_cmpint (inet_pton (AF_INET, "9.8.7.6", &addr4), >, 0); - g_assert_cmpint (nm_ip4_address_get_address (ip4_addr), ==, addr4); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 16); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "9.8.7.6"); /* ===== IPv6 SETTING ===== */ s_ip6 = nm_connection_get_setting_ip6_config (connection); @@ -794,21 +784,18 @@ test_read_wired_dhcp_plus_ip (void) g_assert_cmpint (nm_setting_ip6_config_get_num_addresses (s_ip6), ==, 3); ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); g_assert (ip6_addr); - g_assert_cmpint (nm_ip6_address_get_prefix (ip6_addr), ==, 56); - g_assert_cmpint (inet_pton (AF_INET6, "1001:abba::1234", &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr6)); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "1001:abba::1234"); ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 1); g_assert (ip6_addr); - g_assert_cmpint (nm_ip6_address_get_prefix (ip6_addr), ==, 64); - g_assert_cmpint (inet_pton (AF_INET6, "2001:abba::2234", &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr6)); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "2001:abba::2234"); ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 2); g_assert (ip6_addr); - g_assert_cmpint (nm_ip6_address_get_prefix (ip6_addr), ==, 96); - g_assert_cmpint (inet_pton (AF_INET6, "3001:abba::3234", &addr6), >, 0); - g_assert (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr6)); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 96); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "3001:abba::3234"); g_object_unref (connection); } @@ -821,7 +808,7 @@ test_read_wired_global_gateway (void) NMSettingWired *s_wired; NMSettingIP4Config *s_ip4; GError *error = NULL; - NMIP4Address *ip4_addr; + NMIPAddress *ip4_addr; char *unmanaged = NULL; connection = connection_from_file (TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wired-global-gateway", @@ -847,9 +834,9 @@ test_read_wired_global_gateway (void) /* Address #1 */ ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert (ip4_addr); - g_assert_cmpint (nm_ip4_address_get_prefix (ip4_addr), ==, 24); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_address (ip4_addr), "192.168.1.5"); - nmtst_assert_ip4_address_equals (nm_ip4_address_get_gateway (ip4_addr), "192.168.1.2"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.1.2"); g_object_unref (connection); } @@ -1137,7 +1124,7 @@ test_read_wired_static_routes (void) NMSettingWired *s_wired; NMSettingIP4Config *s_ip4; GError *error = NULL; - NMIP4Route *ip4_route; + NMIPRoute *ip4_route; connection = connection_from_file (TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wired-static-routes", NULL, TYPE_ETHERNET, NULL, NULL, NULL, NULL, &error, NULL); @@ -1162,17 +1149,17 @@ test_read_wired_static_routes (void) ip4_route = nm_setting_ip4_config_get_route (s_ip4, 0); g_assert (ip4_route); - nmtst_assert_ip4_address_equals (nm_ip4_route_get_dest (ip4_route), "11.22.33.0"); - g_assert_cmpint (nm_ip4_route_get_prefix (ip4_route), ==, 24); - nmtst_assert_ip4_address_equals (nm_ip4_route_get_next_hop (ip4_route), "192.168.1.5"); - g_assert_cmpint (nm_ip4_route_get_metric (ip4_route), ==, 0); + g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "11.22.33.0"); + g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 24); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "192.168.1.5"); + g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 0); ip4_route = nm_setting_ip4_config_get_route (s_ip4, 1); g_assert (ip4_route); - nmtst_assert_ip4_address_equals (nm_ip4_route_get_dest (ip4_route), "44.55.66.77"); - g_assert_cmpint (nm_ip4_route_get_prefix (ip4_route), ==, 32); - nmtst_assert_ip4_address_equals (nm_ip4_route_get_next_hop (ip4_route), "192.168.1.7"); - g_assert_cmpint (nm_ip4_route_get_metric (ip4_route), ==, 3); + g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "44.55.66.77"); + g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 32); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "192.168.1.7"); + g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 3); g_object_unref (connection); } @@ -1193,15 +1180,8 @@ test_read_wired_static_routes_legacy (void) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - NMIP4Route *ip4_route; - guint32 addr; + NMIPRoute *ip4_route; const char *expected_id = "System test-wired-static-routes-legacy"; - const char *expected_dst1 = "21.31.41.0"; - const char *expected_dst2 = "32.42.52.62"; - const char *expected_dst3 = "43.53.0.0"; - const char *expected_gw1 = "9.9.9.9"; - const char *expected_gw2 = "8.8.8.8"; - const char *expected_gw3 = "7.7.7.7"; connection = connection_from_file (TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, NULL, @@ -1281,99 +1261,27 @@ test_read_wired_static_routes_legacy (void) /* Route #1 */ ip4_route = nm_setting_ip4_config_get_route (s_ip4, 0); - ASSERT (ip4_route, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: missing IP4 route #1", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - - ASSERT (inet_pton (AF_INET, expected_dst1, &addr) > 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: couldn't convert destination IP address #1", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - ASSERT (nm_ip4_route_get_dest (ip4_route) == addr, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value #1", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); - - ASSERT (nm_ip4_route_get_prefix (ip4_route) == 24, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected destination route #1 prefix", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - - ASSERT (inet_pton (AF_INET, expected_gw1, &addr) > 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: couldn't convert next hop IP address #1", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - ASSERT (nm_ip4_route_get_next_hop (ip4_route) == addr, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value #1", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); - - ASSERT (nm_ip4_route_get_metric (ip4_route) == 1, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected destination route #1 metric", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); + g_assert (ip4_route != NULL); + g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "21.31.41.0"); + g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 24); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "9.9.9.9"); + g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 1); /* Route #2 */ ip4_route = nm_setting_ip4_config_get_route (s_ip4, 1); - ASSERT (ip4_route, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: missing IP4 route #2", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - - ASSERT (inet_pton (AF_INET, expected_dst2, &addr) > 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: couldn't convert destination IP address #2", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - ASSERT (nm_ip4_route_get_dest (ip4_route) == addr, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value #2", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); - - ASSERT (nm_ip4_route_get_prefix (ip4_route) == 32, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected destination route #2 prefix", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - - ASSERT (inet_pton (AF_INET, expected_gw2, &addr) > 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: couldn't convert next hop IP address #2", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - ASSERT (nm_ip4_route_get_next_hop (ip4_route) == addr, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value #2", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); - - ASSERT (nm_ip4_route_get_metric (ip4_route) == 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected destination route #2 metric", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); + g_assert (ip4_route != NULL); + g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "32.42.52.62"); + g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 32); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "8.8.8.8"); + g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 0); /* Route #3 */ ip4_route = nm_setting_ip4_config_get_route (s_ip4, 2); - ASSERT (ip4_route, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: missing IP4 route #3", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - - ASSERT (inet_pton (AF_INET, expected_dst3, &addr) > 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: couldn't convert destination IP address #3", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - ASSERT (nm_ip4_route_get_dest (ip4_route) == addr, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value #3", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); - - ASSERT (nm_ip4_route_get_prefix (ip4_route) == 16, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected destination route #3 prefix", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - - ASSERT (inet_pton (AF_INET, expected_gw3, &addr) > 0, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: couldn't convert next hop IP address #3", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); - ASSERT (nm_ip4_route_get_next_hop (ip4_route) == addr, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value #3", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, - NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); - - ASSERT (nm_ip4_route_get_metric (ip4_route) == 3, - "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected destination route #3 metric", - TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY); + g_assert (ip4_route != NULL); + g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "43.53.0.0"); + g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 16); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "7.7.7.7"); + g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 3); g_free (unmanaged); g_free (keyfile); @@ -1396,14 +1304,7 @@ test_read_wired_ipv4_manual (const char *file, const char *expected_id) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - const char *expected_address1 = "1.2.3.4"; - const char *expected_address2 = "9.8.7.6"; - const char *expected_address3 = "3.3.3.3"; - guint32 expected_prefix1 = 24; - guint32 expected_prefix2 = 16; - guint32 expected_prefix3 = 8; - NMIP4Address *ip4_addr; - guint32 addr; + NMIPAddress *ip4_addr; connection = connection_from_file (file, NULL, @@ -1477,54 +1378,21 @@ test_read_wired_ipv4_manual (const char *file, const char *expected_id) /* Address #1 */ ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); - ASSERT (ip4_addr, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: missing IP4 address #1", - file); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix1, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected IP4 address #1 prefix", - file); - - ASSERT (inet_pton (AF_INET, expected_address1, &addr) > 0, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: couldn't convert IP address #1", - file); - ASSERT (nm_ip4_address_get_address (ip4_addr) == addr, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected IP4 address #1", - file); + g_assert (ip4_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "1.2.3.4"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); /* Address #2 */ ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 1); - ASSERT (ip4_addr, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: missing IP4 address #2", - file); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix2, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected IP4 address #2 prefix", - file); - - ASSERT (inet_pton (AF_INET, expected_address2, &addr) > 0, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: couldn't convert IP address #2", - file); - ASSERT (nm_ip4_address_get_address (ip4_addr) == addr, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected IP4 address #2", - file); + g_assert (ip4_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "9.8.7.6"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 16); /* Address #3 */ ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 2); - ASSERT (ip4_addr, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: missing IP4 address #3", - file); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix3, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected IP4 address #3 prefix", - file); - - ASSERT (inet_pton (AF_INET, expected_address3, &addr) > 0, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: couldn't convert IP address #3", - file); - ASSERT (nm_ip4_address_get_address (ip4_addr) == addr, - "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected IP4 address #3", - file); + g_assert (ip4_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "3.3.3.3"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 8); g_free (unmanaged); g_free (keyfile); @@ -1551,15 +1419,8 @@ test_read_wired_ipv6_manual (void) GError *error = NULL; const char *tmp; const char *expected_id = "System test-wired-ipv6-manual"; - const char *expected_address1 = "1001:abba::1234"; - const char *expected_address2 = "2001:abba::2234"; - const char *expected_address3 = "3001:abba::3234"; - guint32 expected_prefix1 = 56; - guint32 expected_prefix2 = 64; - guint32 expected_prefix3 = 96; - NMIP6Address *ip6_addr; - NMIP6Route *ip6_route; - struct in6_addr addr; + NMIPAddress *ip6_addr; + NMIPRoute *ip6_route; g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, "*ignoring manual default route*"); @@ -1707,75 +1568,38 @@ test_read_wired_ipv6_manual (void) /* Address #1 */ ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); - ASSERT (ip6_addr, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: missing IP6 address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (nm_ip6_address_get_prefix (ip6_addr) == expected_prefix1, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected IP6 address #1 prefix", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (inet_pton (AF_INET6, expected_address1, &addr) > 0, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: couldn't convert IP address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr), - "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected IP6 address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); + g_assert (ip6_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "1001:abba::1234"); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56); /* Address #2 */ ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 1); - ASSERT (ip6_addr, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: missing IP6 address #2", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (nm_ip6_address_get_prefix (ip6_addr) == expected_prefix2, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected IP6 address #2 prefix", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (inet_pton (AF_INET6, expected_address2, &addr) > 0, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: couldn't convert IP address #2", - TEST_IFCFG_WIRED_IPV6_MANUAL); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr), - "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected IP6 address #2", - TEST_IFCFG_WIRED_IPV6_MANUAL); + g_assert (ip6_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "2001:abba::2234"); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64); /* Address #3 */ ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 2); - ASSERT (ip6_addr, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: missing IP6 address #3", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (nm_ip6_address_get_prefix (ip6_addr) == expected_prefix3, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected IP6 address #3 prefix", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (inet_pton (AF_INET6, expected_address3, &addr) > 0, - "wired-ipv6-manual-verify-ip6", "failed to verify %s: couldn't convert IP address #3", - TEST_IFCFG_WIRED_IPV6_MANUAL); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr), - "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected IP6 address #3", - TEST_IFCFG_WIRED_IPV6_MANUAL); + g_assert (ip6_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "3001:abba::3234"); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 96); /* Routes */ g_assert_cmpint (nm_setting_ip6_config_get_num_routes (s_ip6), ==, 2); /* Route #1 */ ip6_route = nm_setting_ip6_config_get_route (s_ip6, 0); g_assert (ip6_route); - g_assert_cmpint (inet_pton (AF_INET6, "9876::1234", &addr), >, 0); - g_assert_cmpint (memcmp (nm_ip6_route_get_dest (ip6_route), &addr, sizeof (struct in6_addr)), ==, 0); - g_assert_cmpint (nm_ip6_route_get_prefix (ip6_route), ==, 96); - g_assert_cmpint (inet_pton (AF_INET6, "9876::7777", &addr), >, 0); - g_assert_cmpint (memcmp (nm_ip6_route_get_next_hop (ip6_route), &addr, sizeof (struct in6_addr)), ==, 0); - g_assert_cmpint (nm_ip6_route_get_metric (ip6_route), ==, 2); + g_assert_cmpstr (nm_ip_route_get_dest (ip6_route), ==, "9876::1234"); + g_assert_cmpint (nm_ip_route_get_prefix (ip6_route), ==, 96); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip6_route), ==, "9876::7777"); + g_assert_cmpint (nm_ip_route_get_metric (ip6_route), ==, 2); /* Route #2 */ ip6_route = nm_setting_ip6_config_get_route (s_ip6, 1); g_assert (ip6_route); - g_assert_cmpint (inet_pton (AF_INET6, "abbe::cafe", &addr), >, 0); - g_assert_cmpint (memcmp (nm_ip6_route_get_dest (ip6_route), &addr, sizeof (struct in6_addr)), ==, 0); - g_assert_cmpint (nm_ip6_route_get_prefix (ip6_route), ==, 64); - g_assert_cmpint (inet_pton (AF_INET6, "::", &addr), >, 0); - g_assert_cmpint (memcmp (nm_ip6_route_get_next_hop (ip6_route), &addr, sizeof (struct in6_addr)), ==, 0); - g_assert_cmpint (nm_ip6_route_get_metric (ip6_route), ==, 777); + g_assert_cmpstr (nm_ip_route_get_dest (ip6_route), ==, "abbe::cafe"); + g_assert_cmpint (nm_ip_route_get_prefix (ip6_route), ==, 64); + g_assert_cmpstr (nm_ip_route_get_next_hop (ip6_route), ==, NULL); + g_assert_cmpint (nm_ip_route_get_metric (ip6_route), ==, 777); /* DNS Addresses */ ASSERT (nm_setting_ip6_config_get_num_dns (s_ip6) == 2, @@ -1828,10 +1652,7 @@ test_read_wired_ipv6_only (void) GError *error = NULL; const char *tmp; const char *expected_id = "System test-wired-ipv6-only"; - const char *expected_address1 = "1001:abba::1234"; - guint32 expected_prefix1 = 56; - NMIP6Address *ip6_addr; - struct in6_addr addr; + NMIPAddress *ip6_addr; const char *method; connection = connection_from_file (TEST_IFCFG_WIRED_IPV6_ONLY, @@ -1921,20 +1742,9 @@ test_read_wired_ipv6_only (void) /* Address #1 */ ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); - ASSERT (ip6_addr, - "wired-ipv6-only-verify-ip6", "failed to verify %s: missing IP6 address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (nm_ip6_address_get_prefix (ip6_addr) == expected_prefix1, - "wired-ipv6-only-verify-ip6", "failed to verify %s: unexpected IP6 address #1 prefix", - TEST_IFCFG_WIRED_IPV6_MANUAL); - - ASSERT (inet_pton (AF_INET6, expected_address1, &addr) > 0, - "wired-ipv6-only-verify-ip6", "failed to verify %s: couldn't convert IP address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); - ASSERT (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), &addr), - "wired-ipv6-only-verify-ip6", "failed to verify %s: unexpected IP6 address #1", - TEST_IFCFG_WIRED_IPV6_MANUAL); + g_assert (ip6_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "1001:abba::1234"); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56); /* DNS Addresses */ ASSERT (nm_setting_ip6_config_get_num_dns (s_ip6) == 1, @@ -2564,7 +2374,7 @@ test_read_wired_aliases_good (void) GError *error = NULL; const char *tmp; const char *expected_id = "System aliasem0"; - int expected_num_addresses = 4, expected_prefix = 24; + int expected_num_addresses = 4; const char *expected_address[4] = { "192.168.1.5", "192.168.1.6", "192.168.1.9", "192.168.1.99" }; const char *expected_label[4] = { "", "aliasem0:1", "aliasem0:2", "aliasem0:99" }; const char *expected_gateway[4] = { "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1" }; @@ -2630,53 +2440,24 @@ test_read_wired_aliases_good (void) /* Addresses */ for (i = 0; i < expected_num_addresses; i++) { - NMIP4Address *ip4_addr; - char buf[INET_ADDRSTRLEN]; - struct in_addr addr; + NMIPAddress *ip4_addr; + const char *addr; ip4_addr = nm_setting_ip4_config_get_address (s_ip4, i); - ASSERT (ip4_addr, - "aliases-good-verify-ip4", "failed to verify %s: missing IP4 address #%d", - TEST_IFCFG_ALIASES_GOOD, - i); + g_assert (ip4_addr != NULL); - addr.s_addr = nm_ip4_address_get_address (ip4_addr); - ASSERT (inet_ntop (AF_INET, &addr, buf, sizeof (buf)) > 0, - "aliases-good-verify-ip4", "failed to verify %s: couldn't convert IP address #%d", - TEST_IFCFG_ALIASES_GOOD, - i); + addr = nm_ip_address_get_address (ip4_addr); + g_assert (nm_utils_ipaddr_valid (AF_INET, addr)); for (j = 0; j < expected_num_addresses; j++) { - if (!g_strcmp0 (buf, expected_address[j])) + if (!g_strcmp0 (addr, expected_address[j])) break; } + g_assert (j < expected_num_addresses); - ASSERT (j < expected_num_addresses, - "aliases-good-verify-ip4", "failed to verify %s: unexpected IP4 address #%d", - TEST_IFCFG_ALIASES_GOOD, - i); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix, - "aliases-good-verify-ip4", "failed to verify %s: unexpected IP4 address prefix #%d", - TEST_IFCFG_ALIASES_GOOD, - i); - - if (expected_gateway[j]) { - ASSERT (inet_pton (AF_INET, expected_gateway[j], &addr) > 0, - "aliases-good-verify-ip4", "failed to verify %s: couldn't convert IP address gateway #%d", - TEST_IFCFG_ALIASES_GOOD, - i); - } else - addr.s_addr = 0; - ASSERT (nm_ip4_address_get_gateway (ip4_addr) == addr.s_addr, - "aliases-good-verify-ip4", "failed to verify %s: unexpected IP4 address gateway #%d", - TEST_IFCFG_ALIASES_GOOD, - i); - - ASSERT (g_strcmp0 (_nm_setting_ip4_config_get_address_label (s_ip4, i), expected_label[j]) == 0, - "aliases-good-verify-ip4", "failed to verify %s: unexpected IP4 address label #%d", - TEST_IFCFG_ALIASES_GOOD, - i); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, expected_gateway[j]); + g_assert_cmpstr (_nm_setting_ip4_config_get_address_label (s_ip4, i), ==, expected_label[j]); expected_address[j] = NULL; expected_gateway[j] = NULL; @@ -2709,12 +2490,7 @@ test_read_wired_aliases_bad (const char *base, const char *expected_id) gboolean ignore_error = FALSE; GError *error = NULL; const char *tmp; - int expected_num_addresses = 1, expected_prefix = 24; - const char *expected_address = "192.168.1.5"; - const char *expected_label = ""; - const char *expected_gateway = "192.168.1.1"; - NMIP4Address *ip4_addr; - struct in_addr addr; + NMIPAddress *ip4_addr; connection = connection_from_file (base, NULL, @@ -2769,7 +2545,7 @@ test_read_wired_aliases_bad (const char *base, const char *expected_id) NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_METHOD); - ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == expected_num_addresses, + ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == 1, "aliases-bad-verify-ip4", "failed to verify %s: unexpected %s / %s key value", base, NM_SETTING_IP4_CONFIG_SETTING_NAME, @@ -2777,31 +2553,11 @@ test_read_wired_aliases_bad (const char *base, const char *expected_id) /* Addresses */ ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); - ASSERT (ip4_addr, - "aliases-bad-verify-ip4", "failed to verify %s: missing IP4 address", - base); - - ASSERT (inet_pton (AF_INET, expected_address, &addr) > 0, - "aliases-bad-verify-ip4", "failed to verify %s: couldn't convert IP address", - base); - ASSERT (nm_ip4_address_get_address (ip4_addr) == addr.s_addr, - "aliases-bad-verify-ip4", "failed to verify %s: unexpected IP4 address", - base); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix, - "aliases-bad-verify-ip4", "failed to verify %s: unexpected IP4 address prefix", - base); - - ASSERT (inet_pton (AF_INET, expected_gateway, &addr) > 0, - "aliases-bad-verify-ip4", "failed to verify %s: couldn't convert IP address gateway", - base); - ASSERT (nm_ip4_address_get_gateway (ip4_addr) == addr.s_addr, - "aliases-bad-verify-ip4", "failed to verify %s: unexpected IP4 address gateway", - base); - - ASSERT (g_strcmp0 (_nm_setting_ip4_config_get_address_label (s_ip4, 0), expected_label) == 0, - "aliases-bad-verify-ip4", "failed to verify %s: unexpected IP4 address label", - base); + g_assert (ip4_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.1.1"); + g_assert_cmpstr (_nm_setting_ip4_config_get_address_label (s_ip4, 0), ==, ""); g_free (keyfile); g_free (routefile); @@ -6470,25 +6226,17 @@ test_write_wired_static (void) static const char *mac = "31:33:33:37:be:cd"; guint32 mtu = 1492; char *uuid; - const guint32 ip1 = htonl (0x01010103); - const guint32 ip2 = htonl (0x01010105); - const guint32 gw = htonl (0x01010101); const char *dns1 = "4.2.2.1"; const char *dns2 = "4.2.2.2"; - const guint32 prefix = 24; const char *dns_search1 = "foobar.com"; const char *dns_search2 = "lab.foobar.com"; const char *dns_search3 = "foobar6.com"; const char *dns_search4 = "lab6.foobar.com"; - struct in6_addr ip6, ip6_1, ip6_2; - struct in6_addr route1_dest, route2_dest, route1_nexthop, route2_nexthop; const char *dns6_1 = "fade:0102:0103::face"; const char *dns6_2 = "cafe:ffff:eeee:dddd:cccc:bbbb:aaaa:feed"; - const guint32 route1_prefix = 64, route2_prefix = 128; - const guint32 route1_metric = 99, route2_metric = 1; - NMIP4Address *addr; - NMIP6Address *addr6; - NMIP6Route *route6; + NMIPAddress *addr; + NMIPAddress *addr6; + NMIPRoute *route6; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -6498,14 +6246,6 @@ test_write_wired_static (void) char *route6file = NULL; gboolean ignore_error = FALSE; - inet_pton (AF_INET6, "1003:1234:abcd::1", &ip6); - inet_pton (AF_INET6, "2003:1234:abcd::2", &ip6_1); - inet_pton (AF_INET6, "3003:1234:abcd::3", &ip6_2); - inet_pton (AF_INET6, "2222:aaaa:bbbb:cccc::", &route1_dest); - inet_pton (AF_INET6, "2222:aaaa:bbbb:cccc:dddd:eeee:5555:6666", &route1_nexthop); - inet_pton (AF_INET6, "::", &route2_dest); - inet_pton (AF_INET6, "2222:aaaa::9999", &route2_nexthop); - connection = nm_simple_connection_new (); /* Connection setting */ @@ -6539,19 +6279,15 @@ test_write_wired_static (void) NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, NULL); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip2); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.5", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nm_setting_ip4_config_add_dns (s_ip4, dns1); nm_setting_ip4_config_add_dns (s_ip4, dns2); @@ -6569,40 +6305,33 @@ test_write_wired_static (void) NULL); /* Add addresses */ - addr6 = nm_ip6_address_new (); - nm_ip6_address_set_address (addr6, &ip6); - nm_ip6_address_set_prefix (addr6, 11); + addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, NULL, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_address (s_ip6, addr6); - nm_ip6_address_unref (addr6); + nm_ip_address_unref (addr6); - addr6 = nm_ip6_address_new (); - nm_ip6_address_set_address (addr6, &ip6_1); - nm_ip6_address_set_prefix (addr6, 22); + addr6 = nm_ip_address_new (AF_INET6, "2003:1234:abcd::2", 22, NULL, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_address (s_ip6, addr6); - nm_ip6_address_unref (addr6); + nm_ip_address_unref (addr6); - addr6 = nm_ip6_address_new (); - nm_ip6_address_set_address (addr6, &ip6_2); - nm_ip6_address_set_prefix (addr6, 33); + addr6 = nm_ip_address_new (AF_INET6, "3003:1234:abcd::3", 33, NULL, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_address (s_ip6, addr6); - nm_ip6_address_unref (addr6); + nm_ip_address_unref (addr6); /* Add routes */ - route6 = nm_ip6_route_new (); - nm_ip6_route_set_dest (route6, &route1_dest); - nm_ip6_route_set_prefix (route6, route1_prefix); - nm_ip6_route_set_next_hop (route6, &route1_nexthop); - nm_ip6_route_set_metric (route6, route1_metric); + route6 = nm_ip_route_new (AF_INET6, + "2222:aaaa:bbbb:cccc::", 64, + "2222:aaaa:bbbb:cccc:dddd:eeee:5555:6666", 99, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_route (s_ip6, route6); - nm_ip6_route_unref (route6); + nm_ip_route_unref (route6); - route6 = nm_ip6_route_new (); - nm_ip6_route_set_dest (route6, &route2_dest); - nm_ip6_route_set_prefix (route6, route2_prefix); - nm_ip6_route_set_next_hop (route6, &route2_nexthop); - nm_ip6_route_set_metric (route6, route2_metric); + route6 = nm_ip_route_new (AF_INET6, "::", 128, "2222:aaaa::9999", 1, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_route (s_ip6, route6); - nm_ip6_route_unref (route6); + nm_ip_route_unref (route6); /* DNS servers */ nm_setting_ip6_config_add_dns (s_ip6, dns6_1); @@ -6906,9 +6635,8 @@ test_write_wired_static_ip6_only (void) NMSettingIP6Config *s_ip6; static const char *mac = "31:33:33:37:be:cd"; char *uuid; - struct in6_addr ip6; const char *dns6 = "fade:0102:0103::face"; - NMIP6Address *addr6; + NMIPAddress *addr6; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -6918,8 +6646,6 @@ test_write_wired_static_ip6_only (void) char *route6file = NULL; gboolean ignore_error = FALSE; - inet_pton (AF_INET6, "1003:1234:abcd::1", &ip6); - connection = nm_simple_connection_new (); /* Connection setting */ @@ -6958,11 +6684,10 @@ test_write_wired_static_ip6_only (void) NULL); /* Add addresses */ - addr6 = nm_ip6_address_new (); - nm_ip6_address_set_address (addr6, &ip6); - nm_ip6_address_set_prefix (addr6, 11); + addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, NULL, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_address (s_ip6, addr6); - nm_ip6_address_unref (addr6); + nm_ip_address_unref (addr6); /* DNS server */ nm_setting_ip6_config_add_dns (s_ip6, dns6); @@ -7038,29 +6763,15 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) NMSettingIP6Config *s_ip6; static const char *mac = "31:33:33:37:be:cd"; char *uuid; - struct in6_addr ip6; const char *dns6 = "fade:0102:0103::face"; - NMIP6Address *addr6; + NMIPAddress *addr6; gboolean success; GError *error = NULL; char *testfile = NULL; char *id = NULL; gboolean ignore_error = FALSE; char *written_ifcfg_gateway; - char s_gateway6[INET6_ADDRSTRLEN] = { 0 }; - struct in6_addr gateway6_autovar; - const struct in6_addr *gateway6 = NULL; - - /* parsing the input argument and set the struct in6_addr "gateway6" to - * the gateway address. NULL means "do not set the gateway explicitly". */ - if (user_data) { - g_assert_cmpint (inet_pton (AF_INET6, user_data, &gateway6_autovar), ==, 1); - gateway6 = &gateway6_autovar; - } - - inet_pton (AF_INET6, "1003:1234:abcd::1", &ip6); - if (gateway6) - inet_ntop (AF_INET6, gateway6, s_gateway6, sizeof (s_gateway6)); + const char *gateway6 = user_data; connection = nm_simple_connection_new (); @@ -7069,7 +6780,7 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) nm_connection_add_setting (connection, NM_SETTING (s_con)); uuid = nm_utils_uuid_generate (); - id = g_strdup_printf ("Test Write Wired Static IP6 Only With Gateway %s", gateway6 ? s_gateway6 : "NULL"); + id = g_strdup_printf ("Test Write Wired Static IP6 Only With Gateway %s", gateway6 ? gateway6 : "NULL"); g_object_set (s_con, NM_SETTING_CONNECTION_ID, id, NM_SETTING_CONNECTION_UUID, uuid, @@ -7102,13 +6813,10 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) NULL); /* Add addresses */ - addr6 = nm_ip6_address_new (); - nm_ip6_address_set_address (addr6, &ip6); - nm_ip6_address_set_prefix (addr6, 11); - if (gateway6) - nm_ip6_address_set_gateway (addr6, gateway6); + addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, gateway6, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_address (s_ip6, addr6); - nm_ip6_address_unref (addr6); + nm_ip_address_unref (addr6); /* DNS server */ nm_setting_ip6_config_add_dns (s_ip6, dns6); @@ -7159,13 +6867,13 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) g_assert (addr6); /* assert that the gateway was written and reloaded as expected */ - if (!gateway6 || IN6_IS_ADDR_UNSPECIFIED (gateway6)) { - g_assert (IN6_IS_ADDR_UNSPECIFIED (nm_ip6_address_get_gateway (addr6))); - g_assert (written_ifcfg_gateway==NULL); + if (!gateway6 || !strcmp (gateway6, "::")) { + g_assert (nm_ip_address_get_gateway (addr6) == NULL); + g_assert (written_ifcfg_gateway == NULL); } else { - g_assert (!IN6_IS_ADDR_UNSPECIFIED (nm_ip6_address_get_gateway (addr6))); - g_assert_cmpint (memcmp (nm_ip6_address_get_gateway (addr6), gateway6, sizeof (struct in6_addr)), ==, 0); - g_assert_cmpstr (written_ifcfg_gateway, ==, s_gateway6); + g_assert (nm_ip_address_get_gateway (addr6) != NULL); + g_assert_cmpstr (nm_ip_address_get_gateway (addr6), ==, gateway6); + g_assert_cmpstr (written_ifcfg_gateway, ==, gateway6); } g_free (testfile); @@ -7334,20 +7042,12 @@ test_write_wired_static_routes (void) static const char *mac = "31:33:33:37:be:cd"; guint32 mtu = 1492; char *uuid; - const guint32 ip1 = htonl (0x01010103); - const guint32 ip2 = htonl (0x01010105); - const guint32 gw = htonl (0x01010101); const char *dns1 = "4.2.2.1"; const char *dns2 = "4.2.2.2"; - const guint32 route_dst1 = htonl (0x01020300); - const guint32 route_dst2= htonl (0x03020100); - const guint32 route_gw1 = htonl (0xdeadbeef); - const guint32 route_gw2 = htonl (0xcafeabbe); - const guint32 prefix = 24; const char *dns_search1 = "foobar.com"; const char *dns_search2 = "lab.foobar.com"; - NMIP4Address *addr; - NMIP4Route *route; + NMIPAddress *addr; + NMIPRoute *route; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -7389,35 +7089,26 @@ test_write_wired_static_routes (void) NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip2); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.5", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); /* Write out routes */ - route = nm_ip4_route_new (); - nm_ip4_route_set_dest (route, route_dst1); - nm_ip4_route_set_prefix (route, prefix); - nm_ip4_route_set_next_hop (route, route_gw1); + route = nm_ip_route_new (AF_INET, "1.2.3.0", 24, "222.173.190.239", 0, &error); + g_assert_no_error (error); nm_setting_ip4_config_add_route (s_ip4, route); - nm_ip4_route_unref (route); + nm_ip_route_unref (route); - route = nm_ip4_route_new (); - nm_ip4_route_set_dest (route, route_dst2); - nm_ip4_route_set_prefix (route, prefix); - nm_ip4_route_set_next_hop (route, route_gw2); - nm_ip4_route_set_metric (route, 77); + route = nm_ip_route_new (AF_INET, "3.2.1.0", 24, "202.254.186.190", 77, &error); + g_assert_no_error (error); nm_setting_ip4_config_add_route (s_ip4, route); - nm_ip4_route_unref (route); + nm_ip_route_unref (route); nm_setting_ip4_config_add_dns (s_ip4, dns1); nm_setting_ip4_config_add_dns (s_ip4, dns2); @@ -7865,11 +7556,9 @@ test_write_wired_aliases (void) NMSettingIP4Config *s_ip4; char *uuid; int num_addresses = 4; - guint32 ip[] = { 0x01010101, 0x01010102, 0x01010103, 0x01010104 }; + const char *ip[] = { "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" }; const char *label[] = { "", "alias0:2", "", "alias0:3" }; - const guint32 gw = htonl (0x01010101); - const guint32 prefix = 24; - NMIP4Address *addr; + NMIPAddress *addr; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -7920,12 +7609,10 @@ test_write_wired_aliases (void) NULL); for (i = 0; i < num_addresses; i++) { - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip[i]); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, ip[i], 24, "1.1.1.1", &error); + g_assert_no_error (error); _nm_setting_ip4_config_add_address_with_label (s_ip4, addr, label[i]); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); } ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -8002,41 +7689,23 @@ test_write_wired_aliases (void) /* Addresses */ for (i = 0; i < num_addresses; i++) { - guint32 addrbytes; + const char *addrstr; addr = nm_setting_ip4_config_get_address (s_ip4, i); - ASSERT (addr, - "wired-aliases-write-verify-ip4", "failed to verify %s: missing IP4 address #%d", - testfile, - i); + g_assert (addr != NULL); - addrbytes = nm_ip4_address_get_address (addr); + addrstr = nm_ip_address_get_address (addr); for (j = 0; j < num_addresses; j++) { - if (addrbytes == ip[j]) + if (!g_strcmp0 (addrstr, ip[j])) break; } + g_assert (j < num_addresses); - ASSERT (j < num_addresses, - "wired-aliases-write-verify-ip4", "failed to verify %s: unexpected IP4 address #%d", - testfile, - i); - - ASSERT (nm_ip4_address_get_prefix (addr) == prefix, - "wired-aliases-write-verify-ip4", "failed to verify %s: unexpected IP4 address prefix #%d", - testfile, - i); + g_assert_cmpint (nm_ip_address_get_prefix (addr), ==, 24); + g_assert_cmpstr (nm_ip_address_get_gateway (addr), ==, "1.1.1.1"); + g_assert_cmpstr (_nm_setting_ip4_config_get_address_label (s_ip4, i), ==, label[j]); - ASSERT (nm_ip4_address_get_gateway (addr) == gw, - "wired-aliases-write-verify-ip4", "failed to verify %s: unexpected IP4 address gateway #%d", - testfile, - i); - - ASSERT (g_strcmp0 (_nm_setting_ip4_config_get_address_label (s_ip4, i), label[j]) == 0, - "wired-aliases-write-verify-ip4", "failed to verify %s: unexpected IP4 address label #%d", - testfile, - i); - - ip[j] = 0; + ip[j] = NULL; } for (i = 0; i < num_addresses; i++) { @@ -8065,13 +7734,7 @@ test_write_gateway (void) gboolean success; GError *error = NULL; shvarFile *f; - NMIP4Address *addr; - const char *ip1_str = "1.1.1.3"; - const char *ip2_str = "2.2.2.5"; - const char *gw1_str = "1.1.1.254"; - const char *gw2_str = "2.2.2.254"; - struct in_addr ip1, ip2, gw1, gw2; - const guint32 prefix = 24; + NMIPAddress *addr; connection = nm_simple_connection_new (); @@ -8100,24 +7763,15 @@ test_write_gateway (void) NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, NULL); - inet_pton (AF_INET, ip1_str, &ip1); - inet_pton (AF_INET, ip2_str, &ip2); - inet_pton (AF_INET, gw1_str, &gw1); - inet_pton (AF_INET, gw2_str, &gw2); - - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1.s_addr); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw1.s_addr); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.254", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip2.s_addr); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw2.s_addr); + addr = nm_ip_address_new (AF_INET, "2.2.2.5", 24, "2.2.2.254", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); success = nm_connection_verify (connection, &error); g_assert_no_error (error); @@ -8138,12 +7792,12 @@ test_write_gateway (void) /* re-read the file to check that the keys was written as IPADDR, GATEWAY and IPADDR1, GATEWAY1 */ val = svGetValue (f, "IPADDR", FALSE); g_assert (val); - g_assert_cmpstr (val, ==, ip1_str); + g_assert_cmpstr (val, ==, "1.1.1.3"); g_free (val); val = svGetValue (f, "IPADDR1", FALSE); g_assert (val); - g_assert_cmpstr (val, ==, ip2_str); + g_assert_cmpstr (val, ==, "2.2.2.5"); g_free (val); val = svGetValue (f, "IPADDR0", FALSE); @@ -8164,12 +7818,12 @@ test_write_gateway (void) val = svGetValue (f, "GATEWAY", FALSE); g_assert (val); - g_assert_cmpstr (val, ==, gw1_str); + g_assert_cmpstr (val, ==, "1.1.1.254"); g_free (val); val = svGetValue (f, "GATEWAY1", FALSE); g_assert (val); - g_assert_cmpstr (val, ==, gw2_str); + g_assert_cmpstr (val, ==, "2.2.2.254"); g_free (val); val = svGetValue (f, "GATEWAY0", FALSE); @@ -8616,11 +8270,8 @@ test_write_wifi_wep_adhoc (void) GBytes *ssid; const char *ssid_data = "blahblah"; struct stat statbuf; - NMIP4Address *addr; - const guint32 ip1 = htonl (0x01010103); - const guint32 gw = htonl (0x01010101); + NMIPAddress *addr; const char *dns1 = "4.2.2.1"; - const guint32 prefix = 24; connection = nm_simple_connection_new (); @@ -8664,12 +8315,10 @@ test_write_wifi_wep_adhoc (void) g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); /* IP Address */ - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nm_setting_ip4_config_add_dns (s_ip4, dns1); @@ -9608,11 +9257,8 @@ test_write_wifi_wpa_psk_adhoc (void) gboolean ignore_error = FALSE; GBytes *ssid; const char *ssid_data = "blahblah"; - NMIP4Address *addr; - const guint32 ip1 = htonl (0x01010103); - const guint32 gw = htonl (0x01010101); + NMIPAddress *addr; const char *dns1 = "4.2.2.1"; - const guint32 prefix = 24; connection = nm_simple_connection_new (); @@ -9663,12 +9309,10 @@ test_write_wifi_wpa_psk_adhoc (void) g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); /* IP Address */ - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 25, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nm_setting_ip4_config_add_dns (s_ip4, dns1); @@ -11566,10 +11210,7 @@ test_write_bridge_main (void) NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; char *uuid; - const guint32 ip1 = htonl (0x01010103); - const guint32 gw = htonl (0x01010101); - const guint32 prefix = 24; - NMIP4Address *addr; + NMIPAddress *addr; static const char *mac = "31:33:33:37:be:cd"; gboolean success; GError *error = NULL; @@ -11617,12 +11258,10 @@ test_write_bridge_main (void) NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, NULL); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); /* IP6 setting */ s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); @@ -12354,10 +11993,7 @@ test_write_bond_main (void) NMSettingIP6Config *s_ip6; NMSettingWired *s_wired; char *uuid; - const guint32 ip1 = htonl (0x01010103); - const guint32 gw = htonl (0x01010101); - const guint32 prefix = 24; - NMIP4Address *addr; + NMIPAddress *addr; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -12400,12 +12036,10 @@ test_write_bond_main (void) NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, NULL); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); /* IP6 setting */ s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); @@ -12698,10 +12332,7 @@ test_write_infiniband (void) const char *mac = "80:00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22"; guint32 mtu = 65520; char *uuid; - const guint32 ip1 = htonl (0x01010103); - const guint32 gw = htonl (0x01010101); - const guint32 prefix = 24; - NMIP4Address *addr; + NMIPAddress *addr; gboolean success; GError *error = NULL; char *testfile = NULL; @@ -12745,12 +12376,10 @@ test_write_infiniband (void) NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, NULL); - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, ip1); - nm_ip4_address_set_prefix (addr, prefix); - nm_ip4_address_set_gateway (addr, gw); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, addr); - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); /* IP6 setting */ s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); @@ -13829,7 +13458,7 @@ int main (int argc, char **argv) g_test_add_data_func (TPATH "static-ip6-only-gw/_NULL_", NULL, test_write_wired_static_ip6_only_gw); g_test_add_data_func (TPATH "static-ip6-only-gw/::", "::", test_write_wired_static_ip6_only_gw); g_test_add_data_func (TPATH "static-ip6-only-gw/2001:db8:8:4::2", "2001:db8:8:4::2", test_write_wired_static_ip6_only_gw); - g_test_add_data_func (TPATH "static-ip6-only-gw/ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255", "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255", test_write_wired_static_ip6_only_gw); + g_test_add_data_func (TPATH "static-ip6-only-gw/::ffff:255.255.255.255", "::ffff:255.255.255.255", test_write_wired_static_ip6_only_gw); test_read_wired_static (TEST_IFCFG_WIRED_STATIC, "System test-wired-static", TRUE); test_read_wired_static (TEST_IFCFG_WIRED_STATIC_BOOTPROTO, "System test-wired-static-bootproto", FALSE); diff --git a/src/settings/plugins/ifcfg-rh/writer.c b/src/settings/plugins/ifcfg-rh/writer.c index 55dca87c82..160f070cdc 100644 --- a/src/settings/plugins/ifcfg-rh/writer.c +++ b/src/settings/plugins/ifcfg-rh/writer.c @@ -1743,12 +1743,11 @@ write_connection_setting (NMSettingConnection *s_con, shvarFile *ifcfg) static gboolean write_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError **error) { - char dest[INET_ADDRSTRLEN]; - char next_hop[INET_ADDRSTRLEN]; + const char *dest, *next_hop; char **route_items; char *route_contents; - NMIP4Route *route; - guint32 ip, prefix, metric; + NMIPRoute *route; + guint32 prefix, metric; guint32 i, num; gboolean success = FALSE; @@ -1767,17 +1766,10 @@ write_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError for (i = 0; i < num; i++) { route = nm_setting_ip4_config_get_route (s_ip4, i); - memset (dest, 0, sizeof (dest)); - ip = nm_ip4_route_get_dest (route); - inet_ntop (AF_INET, (const void *) &ip, &dest[0], sizeof (dest)); - - prefix = nm_ip4_route_get_prefix (route); - - memset (next_hop, 0, sizeof (next_hop)); - ip = nm_ip4_route_get_next_hop (route); - inet_ntop (AF_INET, (const void *) &ip, &next_hop[0], sizeof (next_hop)); - - metric = nm_ip4_route_get_metric (route); + dest = nm_ip_route_get_dest (route); + prefix = nm_ip_route_get_prefix (route); + next_hop = nm_ip_route_get_next_hop (route); + metric = nm_ip_route_get_metric (route); route_items[i] = g_strdup_printf ("%s/%u via %s metric %u\n", dest, prefix, next_hop, metric); } @@ -1887,9 +1879,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) */ num = nm_setting_ip4_config_get_num_addresses (s_ip4); for (i = n = 0; i < num; i++) { - char buf[INET_ADDRSTRLEN]; - NMIP4Address *addr; - guint32 ip; + NMIPAddress *addr; if (i > 0) { const char *label; @@ -1918,24 +1908,15 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) addr = nm_setting_ip4_config_get_address (s_ip4, i); - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_address_get_address (addr); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (ifcfg, addr_key, &buf[0], FALSE); + svSetValue (ifcfg, addr_key, nm_ip_address_get_address (addr), FALSE); - tmp = g_strdup_printf ("%u", nm_ip4_address_get_prefix (addr)); + tmp = g_strdup_printf ("%u", nm_ip_address_get_prefix (addr)); svSetValue (ifcfg, prefix_key, tmp, FALSE); g_free (tmp); svSetValue (ifcfg, netmask_key, NULL, FALSE); - if (nm_ip4_address_get_gateway (addr)) { - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_address_get_gateway (addr); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (ifcfg, gw_key, &buf[0], FALSE); - } else - svSetValue (ifcfg, gw_key, NULL, FALSE); + svSetValue (ifcfg, gw_key, nm_ip_address_get_gateway (addr), FALSE); g_free (addr_key); g_free (prefix_key); @@ -2050,8 +2031,8 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) num = nm_setting_ip4_config_get_num_routes (s_ip4); for (i = 0; i < 256; i++) { char buf[INET_ADDRSTRLEN]; - NMIP4Route *route; - guint32 ip, metric; + NMIPRoute *route; + guint32 netmask, metric; addr_key = g_strdup_printf ("ADDRESS%d", i); netmask_key = g_strdup_printf ("NETMASK%d", i); @@ -2066,23 +2047,17 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) } else { route = nm_setting_ip4_config_get_route (s_ip4, i); - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_route_get_dest (route); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (routefile, addr_key, &buf[0], FALSE); + svSetValue (routefile, addr_key, nm_ip_route_get_dest (route), FALSE); memset (buf, 0, sizeof (buf)); - ip = nm_utils_ip4_prefix_to_netmask (nm_ip4_route_get_prefix (route)); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); + netmask = nm_utils_ip4_prefix_to_netmask (nm_ip_route_get_prefix (route)); + inet_ntop (AF_INET, (const void *) &netmask, &buf[0], sizeof (buf)); svSetValue (routefile, netmask_key, &buf[0], FALSE); - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_route_get_next_hop (route); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (routefile, gw_key, &buf[0], FALSE); + svSetValue (routefile, gw_key, nm_ip_route_get_next_hop (route), FALSE); memset (buf, 0, sizeof (buf)); - metric = nm_ip4_route_get_metric (route); + metric = nm_ip_route_get_metric (route); if (metric == 0) svSetValue (routefile, metric_key, NULL, FALSE); else { @@ -2162,9 +2137,8 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) num = nm_setting_ip4_config_get_num_addresses (s_ip4); for (i = 0; i < num; i++) { const char *label, *p; - char buf[INET_ADDRSTRLEN], *path, *tmp; - NMIP4Address *addr; - guint32 ip; + char *path, *tmp; + NMIPAddress *addr; shvarFile *ifcfg; label = _nm_setting_ip4_config_get_address_label (s_ip4, i); @@ -2186,22 +2160,13 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) svSetValue (ifcfg, "DEVICE", label, FALSE); addr = nm_setting_ip4_config_get_address (s_ip4, i); + svSetValue (ifcfg, "IPADDR", nm_ip_address_get_address (addr), FALSE); - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_address_get_address (addr); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (ifcfg, "IPADDR", &buf[0], FALSE); - - tmp = g_strdup_printf ("%u", nm_ip4_address_get_prefix (addr)); + tmp = g_strdup_printf ("%u", nm_ip_address_get_prefix (addr)); svSetValue (ifcfg, "PREFIX", tmp, FALSE); g_free (tmp); - if (nm_ip4_address_get_gateway (addr)) { - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_address_get_gateway (addr); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], sizeof (buf)); - svSetValue (ifcfg, "GATEWAY", &buf[0], FALSE); - } + svSetValue (ifcfg, "GATEWAY", nm_ip_address_get_gateway (addr), FALSE); svWriteFile (ifcfg, 0644, NULL); svCloseFile (ifcfg); @@ -2214,13 +2179,9 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) static gboolean write_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **error) { - char dest[INET6_ADDRSTRLEN]; - char next_hop[INET6_ADDRSTRLEN]; char **route_items; char *route_contents; - NMIP6Route *route; - const struct in6_addr *ip; - guint32 prefix, metric; + NMIPRoute *route; guint32 i, num; gboolean success = FALSE; @@ -2238,20 +2199,11 @@ write_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **err route_items = g_malloc0 (sizeof (char*) * (num + 1)); for (i = 0; i < num; i++) { route = nm_setting_ip6_config_get_route (s_ip6, i); - - memset (dest, 0, sizeof (dest)); - ip = nm_ip6_route_get_dest (route); - inet_ntop (AF_INET6, (const void *) ip, &dest[0], sizeof (dest)); - - prefix = nm_ip6_route_get_prefix (route); - - memset (next_hop, 0, sizeof (next_hop)); - ip = nm_ip6_route_get_next_hop (route); - inet_ntop (AF_INET6, (const void *) ip, &next_hop[0], sizeof (next_hop)); - - metric = nm_ip6_route_get_metric (route); - - route_items[i] = g_strdup_printf ("%s/%u via %s metric %u\n", dest, prefix, next_hop, metric); + route_items[i] = g_strdup_printf ("%s/%u via %s metric %u\n", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + nm_ip_route_get_next_hop (route), + nm_ip_route_get_metric (route)); } route_items[num] = NULL; route_contents = g_strjoinv (NULL, route_items); @@ -2276,13 +2228,11 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) NMSettingIP6Config *s_ip6; NMSettingIP4Config *s_ip4; const char *value; - char *addr_key, *prefix; + char *addr_key; guint32 i, num, num4; GString *searches; - char buf[INET6_ADDRSTRLEN]; - char ipv6_defaultgw[INET6_ADDRSTRLEN]; - NMIP6Address *addr; - const struct in6_addr *ip; + const char *ipv6_defaultgw; + NMIPAddress *addr; const char *dns; GString *ip_str1, *ip_str2, *ip_ptr; char *route6_path; @@ -2336,7 +2286,7 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) num = nm_setting_ip6_config_get_num_addresses (s_ip6); ip_str1 = g_string_new (NULL); ip_str2 = g_string_new (NULL); - ipv6_defaultgw[0] = 0; + ipv6_defaultgw = NULL; for (i = 0; i < num; i++) { if (i == 0) ip_ptr = ip_str1; @@ -2344,23 +2294,16 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) ip_ptr = ip_str2; addr = nm_setting_ip6_config_get_address (s_ip6, i); - ip = nm_ip6_address_get_address (addr); - prefix = g_strdup_printf ("%u", nm_ip6_address_get_prefix (addr)); - memset (buf, 0, sizeof (buf)); - inet_ntop (AF_INET6, (const void *) ip, buf, sizeof (buf)); + if (i > 1) g_string_append_c (ip_ptr, ' '); /* separate addresses in IPV6ADDR_SECONDARIES */ - g_string_append (ip_ptr, buf); - g_string_append_c (ip_ptr, '/'); - g_string_append (ip_ptr, prefix); - g_free (prefix); - - /* We only support gateway for the first IP address for now */ - if (i == 0) { - ip = nm_ip6_address_get_gateway (addr); - if (!IN6_IS_ADDR_UNSPECIFIED (ip)) - inet_ntop (AF_INET6, ip, ipv6_defaultgw, sizeof (ipv6_defaultgw)); - } + g_string_append_printf (ip_ptr, "%s/%u", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr)); + + /* We only support gateway for the first IP address */ + if (i == 0) + ipv6_defaultgw = nm_ip_address_get_gateway (addr); } svSetValue (ifcfg, "IPV6ADDR", ip_str1->str, FALSE); svSetValue (ifcfg, "IPV6ADDR_SECONDARIES", ip_str2->str, FALSE); diff --git a/src/settings/plugins/ifnet/connection_parser.c b/src/settings/plugins/ifnet/connection_parser.c index f76876f11f..64dcd75a77 100644 --- a/src/settings/plugins/ifnet/connection_parser.c +++ b/src/settings/plugins/ifnet/connection_parser.c @@ -611,21 +611,25 @@ make_ip4_setting (NMConnection *connection, /************** add all ip settings to the connection**********/ while (iblock) { ip_block *current_iblock; - NMIP4Address *ip4_addr = nm_ip4_address_new (); + NMIPAddress *ip4_addr; + GError *local = NULL; - nm_ip4_address_set_address (ip4_addr, iblock->ip); - nm_ip4_address_set_prefix (ip4_addr, - nm_utils_ip4_netmask_to_prefix - (iblock->netmask)); /* currently all the IPs has the same gateway */ - nm_ip4_address_set_gateway (ip4_addr, iblock->gateway); - if (iblock->gateway) + ip4_addr = nm_ip_address_new (AF_INET, iblock->ip, iblock->prefix, iblock->next_hop, &local); + if (iblock->next_hop) g_object_set (ip4_setting, NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, TRUE, NULL); - if (!nm_setting_ip4_config_add_address (ip4_setting, ip4_addr)) - nm_log_warn (LOGD_SETTINGS, "ignoring duplicate IP4 address"); - nm_ip4_address_unref (ip4_addr); + + if (ip4_addr) { + if (!nm_setting_ip4_config_add_address (ip4_setting, ip4_addr)) + nm_log_warn (LOGD_SETTINGS, "ignoring duplicate IP4 address"); + nm_ip_address_unref (ip4_addr); + } else { + nm_log_warn (LOGD_SETTINGS, " ignoring invalid address entry: %s", local->message); + g_clear_error (&local); + } + current_iblock = iblock; iblock = iblock->next; destroy_ip_block (current_iblock); @@ -690,33 +694,34 @@ make_ip4_setting (NMConnection *connection, const char *metric_str; char *stripped; long int metric; - NMIP4Route *route = nm_ip4_route_new (); + NMIPRoute *route; + GError *local = NULL; - nm_ip4_route_set_dest (route, iblock->ip); - nm_ip4_route_set_next_hop (route, iblock->gateway); - nm_ip4_route_set_prefix (route, - nm_utils_ip4_netmask_to_prefix - (iblock->netmask)); if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) { metric = strtol (metric_str, NULL, 10); - nm_ip4_route_set_metric (route, (guint32) metric); } else { metric_str = ifnet_get_global_data ("metric"); if (metric_str) { stripped = g_strdup (metric_str); strip_string (stripped, '"'); metric = strtol (metric_str, NULL, 10); - nm_ip4_route_set_metric (route, - (guint32) metric); g_free (stripped); - } + } else + metric = 0; } - if (!nm_setting_ip4_config_add_route (ip4_setting, route)) - nm_log_warn (LOGD_SETTINGS, "duplicate IP4 route"); - nm_log_info (LOGD_SETTINGS, "new IP4 route:%d\n", iblock->ip); + route = nm_ip_route_new (AF_INET, iblock->ip, iblock->prefix, iblock->next_hop, metric, &local); - nm_ip4_route_unref (route); + if (route) { + if (nm_setting_ip4_config_add_route (ip4_setting, route)) + nm_log_info (LOGD_SETTINGS, "new IP4 route:%s\n", iblock->ip); + else + nm_log_warn (LOGD_SETTINGS, "duplicate IP4 route"); + nm_ip_route_unref (route); + } else { + nm_log_warn (LOGD_SETTINGS, " ignoring invalid route entry: %s", local->message); + g_clear_error (&local); + } current_iblock = iblock; iblock = iblock->next; @@ -739,7 +744,7 @@ make_ip6_setting (NMConnection *connection, gboolean ipv6_enabled = FALSE; gchar *method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL; const char *value; - ip6_block *iblock; + ip_block *iblock; gboolean never_default = !has_default_ip6_route (conn_name); s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); @@ -776,7 +781,7 @@ make_ip6_setting (NMConnection *connection, /* Make manual settings */ if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) { - ip6_block *current_iblock; + ip_block *current_iblock; iblock = convert_ip6_config_block (conn_name); if (!iblock) { @@ -787,20 +792,26 @@ make_ip6_setting (NMConnection *connection, } /* add all IPv6 addresses */ while (iblock) { - NMIP6Address *ip6_addr = nm_ip6_address_new (); - - nm_ip6_address_set_address (ip6_addr, iblock->ip); - nm_ip6_address_set_prefix (ip6_addr, iblock->prefix); - if (nm_setting_ip6_config_add_address (s_ip6, ip6_addr)) { - nm_log_info (LOGD_SETTINGS, "ipv6 addresses count: %d", - nm_setting_ip6_config_get_num_addresses (s_ip6)); + NMIPAddress *ip6_addr; + GError *local = NULL; + + ip6_addr = nm_ip_address_new (AF_INET6, iblock->ip, iblock->prefix, NULL, &local); + if (ip6_addr) { + if (nm_setting_ip6_config_add_address (s_ip6, ip6_addr)) { + nm_log_info (LOGD_SETTINGS, "ipv6 addresses count: %d", + nm_setting_ip6_config_get_num_addresses (s_ip6)); + } else { + nm_log_warn (LOGD_SETTINGS, "ignoring duplicate IP6 address"); + } + nm_ip_address_unref (ip6_addr); } else { - nm_log_warn (LOGD_SETTINGS, "ignoring duplicate IP4 address"); + nm_log_warn (LOGD_SETTINGS, " ignoring invalid address entry: %s", local->message); + g_clear_error (&local); } - nm_ip6_address_unref (ip6_addr); + current_iblock = iblock; iblock = iblock->next; - destroy_ip6_block (current_iblock); + destroy_ip_block (current_iblock); } } else if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)) { @@ -818,42 +829,45 @@ make_ip6_setting (NMConnection *connection, TRUE, NULL); /* Add all IPv6 routes */ while (iblock) { - ip6_block *current_iblock = iblock; + ip_block *current_iblock = iblock; const char *metric_str; char *stripped; - long int metric = 1; - NMIP6Route *route = nm_ip6_route_new (); + long int metric; + NMIPRoute *route; + GError *local = NULL; - nm_ip6_route_set_dest (route, iblock->ip); - nm_ip6_route_set_next_hop (route, iblock->next_hop); - nm_ip6_route_set_prefix (route, iblock->prefix); /* metric is not per routes configuration right now * global metric is also supported (metric="x") */ if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) { metric = strtol (metric_str, NULL, 10); - nm_ip6_route_set_metric (route, (guint32) metric); + nm_ip_route_set_metric (route, (guint32) metric); } else { metric_str = ifnet_get_global_data ("metric"); if (metric_str) { stripped = g_strdup (metric_str); strip_string (stripped, '"'); metric = strtol (metric_str, NULL, 10); - nm_ip6_route_set_metric (route, - (guint32) metric); + nm_ip_route_set_metric (route, (guint32) metric); g_free (stripped); } else - nm_ip6_route_set_metric (route, (guint32) 1); + metric = 1; } - if (nm_setting_ip6_config_add_route (s_ip6, route)) - nm_log_info (LOGD_SETTINGS, " new IP6 route"); - else - nm_log_warn (LOGD_SETTINGS, " duplicate IP6 route"); - nm_ip6_route_unref (route); + route = nm_ip_route_new (AF_INET6, iblock->ip, iblock->prefix, iblock->next_hop, metric, &local); + if (route) { + if (nm_setting_ip6_config_add_route (s_ip6, route)) + nm_log_info (LOGD_SETTINGS, " new IP6 route"); + else + nm_log_warn (LOGD_SETTINGS, " duplicate IP6 route"); + nm_ip_route_unref (route); + } else { + nm_log_warn (LOGD_SETTINGS, " ignoring invalid route entry: %s", local->message); + g_clear_error (&local); + } current_iblock = iblock; iblock = iblock->next; - destroy_ip6_block (current_iblock); + destroy_ip_block (current_iblock); } done: @@ -2361,7 +2375,6 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err { NMSettingIP4Config *s_ip4; const char *value; - char *tmp; guint32 i, num; GString *searches; GString *ips; @@ -2387,33 +2400,19 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err ips = g_string_new (NULL); /* IPv4 addresses */ for (i = 0; i < num; i++) { - char buf[INET_ADDRSTRLEN + 1]; - NMIP4Address *addr; - guint32 ip; + NMIPAddress *addr; addr = nm_setting_ip4_config_get_address (s_ip4, i); - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_address_get_address (addr); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], - sizeof (buf)); - g_string_append_printf (ips, "\"%s", &buf[0]); - - tmp = - g_strdup_printf ("%u", - nm_ip4_address_get_prefix (addr)); - g_string_append_printf (ips, "/%s\" ", tmp); - g_free (tmp); + g_string_append_printf (ips, "\"%s/%u", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr)); /* only the first gateway will be written */ - if (!has_def_route && nm_ip4_address_get_gateway (addr)) { - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_address_get_gateway (addr); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], - sizeof (buf)); + if (!has_def_route && nm_ip_address_get_gateway (addr)) { g_string_append_printf (routes, - "\"default via %s\" ", - &buf[0]); + "\"default via %s\" ", + nm_ip_address_get_gateway (addr)); has_def_route = TRUE; } } @@ -2474,29 +2473,19 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err num = nm_setting_ip4_config_get_num_routes (s_ip4); if (num > 0) { for (i = 0; i < num; i++) { - char buf[INET_ADDRSTRLEN + 1]; - NMIP4Route *route; - guint32 ip; + NMIPRoute *route; + const char *next_hop; route = nm_setting_ip4_config_get_route (s_ip4, i); - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_route_get_dest (route); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], - sizeof (buf)); - g_string_append_printf (routes, "\"%s", buf); - - tmp = - g_strdup_printf ("%u", - nm_ip4_route_get_prefix (route)); - g_string_append_printf (routes, "/%s via ", tmp); - g_free (tmp); + next_hop = nm_ip_route_get_next_hop (route); + if (!next_hop) + next_hop = "0.0.0.0"; - memset (buf, 0, sizeof (buf)); - ip = nm_ip4_route_get_next_hop (route); - inet_ntop (AF_INET, (const void *) &ip, &buf[0], - sizeof (buf)); - g_string_append_printf (routes, "%s\" ", buf); + g_string_append_printf (routes, "\"%s/%u via %s\" ", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + next_hop); } } if (routes->len > 0) @@ -2513,11 +2502,8 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err static gboolean write_route6_file (NMSettingIP6Config *s_ip6, const char *conn_name, GError **error) { - char dest[INET6_ADDRSTRLEN + 1]; - char next_hop[INET6_ADDRSTRLEN + 1]; - NMIP6Route *route; - const struct in6_addr *ip; - guint32 prefix; + NMIPRoute *route; + const char *next_hop; guint32 i, num; GString *routes_string; const char *old_routes; @@ -2535,20 +2521,14 @@ write_route6_file (NMSettingIP6Config *s_ip6, const char *conn_name, GError **er for (i = 0; i < num; i++) { route = nm_setting_ip6_config_get_route (s_ip6, i); - memset (dest, 0, sizeof (dest)); - ip = nm_ip6_route_get_dest (route); - inet_ntop (AF_INET6, (const void *) ip, &dest[0], - sizeof (dest)); - - prefix = nm_ip6_route_get_prefix (route); - - memset (next_hop, 0, sizeof (next_hop)); - ip = nm_ip6_route_get_next_hop (route); - inet_ntop (AF_INET6, (const void *) ip, &next_hop[0], - sizeof (next_hop)); + next_hop = nm_ip_route_get_next_hop (route); + if (!next_hop) + next_hop = "::"; g_string_append_printf (routes_string, "\"%s/%u via %s\" ", - dest, prefix, next_hop); + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + next_hop); } if (num > 0) ifnet_set_data (conn_name, "routes", routes_string->str); @@ -2562,12 +2542,9 @@ write_ip6_setting (NMConnection *connection, const char *conn_name, GError **err { NMSettingIP6Config *s_ip6; const char *value; - char *prefix; guint32 i, num; GString *searches; - char buf[INET6_ADDRSTRLEN + 1]; - NMIP6Address *addr; - const struct in6_addr *ip; + NMIPAddress *addr; s_ip6 = nm_connection_get_setting_ip6_config (connection); if (!s_ip6) { @@ -2617,16 +2594,10 @@ write_ip6_setting (NMConnection *connection, const char *conn_name, GError **err ip_str = g_string_new (NULL); for (i = 0; i < num; i++) { addr = nm_setting_ip6_config_get_address (s_ip6, i); - ip = nm_ip6_address_get_address (addr); - prefix = - g_strdup_printf ("%u", - nm_ip6_address_get_prefix (addr)); - memset (buf, 0, sizeof (buf)); - inet_ntop (AF_INET6, (const void *) ip, buf, - sizeof (buf)); - g_string_append_printf (ip_str, "\"%s/", buf); - g_string_append_printf (ip_str, "%s\" ", prefix); - g_free (prefix); + + g_string_append_printf (ip_str, "\"%s/%u\"", + nm_ip_address_get_address (addr), + nm_ip_address_get_prefix (addr)); } tmp = g_strdup_printf ("%s\" %s", config, ip_str->str); ifnet_set_data (conn_name, "config", tmp); diff --git a/src/settings/plugins/ifnet/net_utils.c b/src/settings/plugins/ifnet/net_utils.c index be19134e96..9ead29f6e2 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,18 +645,11 @@ 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) { diff --git a/src/settings/plugins/ifnet/net_utils.h b/src/settings/plugins/ifnet/net_utils.h index cee71d57e5..83272e8956 100644 --- a/src/settings/plugins/ifnet/net_utils.h +++ b/src/settings/plugins/ifnet/net_utils.h @@ -31,19 +31,12 @@ #define has_default_ip6_route(conn_name) has_default_route((conn_name), &is_ip6_address) typedef struct _ip_block { - guint32 ip; - guint32 netmask; - guint32 gateway; + char *ip; + guint32 prefix; + char *next_hop; struct _ip_block *next; } ip_block; -typedef struct _ip6_block { - struct in6_addr *ip; - long int prefix; - struct in6_addr *next_hop; - struct _ip6_block *next; -} ip6_block; - gchar *read_hostname (const char *path); gboolean write_hostname (const char *path, const char *hostname); gboolean is_static_ip4 (const char *conn_name); @@ -55,11 +48,10 @@ gboolean has_default_route (const char *conn_name, gboolean (*check_fn) (const c gboolean reload_parsers (void); ip_block *convert_ip4_config_block (const char *conn_name); -ip6_block *convert_ip6_config_block (const char *conn_name); +ip_block *convert_ip6_config_block (const char *conn_name); ip_block *convert_ip4_routes_block (const char *conn_name); -ip6_block *convert_ip6_routes_block (const char *conn_name); +ip_block *convert_ip6_routes_block (const char *conn_name); void destroy_ip_block (ip_block * iblock); -void destroy_ip6_block (ip6_block * iblock); void set_ip4_dns_servers (NMSettingIP4Config * s_ip4, const char *conn_name); void set_ip6_dns_servers (NMSettingIP6Config * s_ip6, const char *conn_name); diff --git a/src/settings/plugins/ifnet/tests/test_all.c b/src/settings/plugins/ifnet/tests/test_all.c index 163cf6ede3..ed44a01cf8 100644 --- a/src/settings/plugins/ifnet/tests/test_all.c +++ b/src/settings/plugins/ifnet/tests/test_all.c @@ -160,25 +160,14 @@ test_is_ip6_address (void) } static void -check_ip_block (ip_block * iblock, gchar * ip, gchar * netmask, gchar * gateway) +check_ip_block (ip_block * iblock, gchar * ip, guint32 prefix, gchar * gateway) { - char *str; - guint32 tmp_ip4_addr; - - str = malloc (INET_ADDRSTRLEN); - tmp_ip4_addr = iblock->ip; - inet_ntop (AF_INET, &tmp_ip4_addr, str, INET_ADDRSTRLEN); - ASSERT (strcmp (ip, str) == 0, "check ip", - "ip expected:%s, find:%s", ip, str); - tmp_ip4_addr = iblock->netmask; - inet_ntop (AF_INET, &tmp_ip4_addr, str, INET_ADDRSTRLEN); - ASSERT (strcmp (netmask, str) == 0, "check netmask", - "netmask expected:%s, find:%s", netmask, str); - tmp_ip4_addr = iblock->gateway; - inet_ntop (AF_INET, &tmp_ip4_addr, str, INET_ADDRSTRLEN); - ASSERT (strcmp (gateway, str) == 0, "check gateway", - "gateway expected:%s, find:%s", gateway, str); - free (str); + ASSERT (strcmp (ip, iblock->ip) == 0, "check ip", + "ip expected:%s, find:%s", ip, iblock->ip); + ASSERT (prefix == iblock->prefix, "check netmask", + "prefix expected:%d, find:%d", prefix, iblock->prefix); + ASSERT (g_strcmp0 (gateway, iblock->next_hop) == 0, "check gateway", + "gateway expected:%s, find:%s", gateway, iblock->next_hop); } static void @@ -189,14 +178,12 @@ test_convert_ipv4_config_block (void) ASSERT (iblock != NULL, "convert ipv4 block", "block eth0 should not be NULL"); - check_ip_block (iblock, "202.117.16.121", "255.255.255.0", - "202.117.16.1"); + check_ip_block (iblock, "202.117.16.121", 24, "202.117.16.1"); iblock = iblock->next; destroy_ip_block (tmp); ASSERT (iblock != NULL, "convert ipv4 block", "block eth0 should have a second IP address"); - check_ip_block (iblock, "192.168.4.121", "255.255.255.0", - "202.117.16.1"); + check_ip_block (iblock, "192.168.4.121", 24, "202.117.16.1"); destroy_ip_block (iblock); g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, @@ -206,7 +193,7 @@ test_convert_ipv4_config_block (void) ASSERT (iblock != NULL && iblock->next == NULL, "convert error IPv4 address", "should only get one address"); - check_ip_block (iblock, "192.168.4.121", "255.255.255.0", "0.0.0.0"); + check_ip_block (iblock, "192.168.4.121", 24, NULL); destroy_ip_block (iblock); g_test_expect_message ("NetworkManager", G_LOG_LEVEL_WARNING, @@ -214,7 +201,6 @@ test_convert_ipv4_config_block (void) iblock = convert_ip4_config_block ("eth3"); ASSERT (iblock == NULL, "convert config_block", "convert error configuration"); - destroy_ip_block (iblock); } static void @@ -224,7 +210,7 @@ test_convert_ipv4_routes_block (void) ip_block *tmp = iblock; ASSERT (iblock != NULL, "convert ip4 routes", "should get one route"); - check_ip_block (iblock, "192.168.4.0", "255.255.255.0", "192.168.4.1"); + check_ip_block (iblock, "192.168.4.0", 24, "192.168.4.1"); iblock = iblock->next; destroy_ip_block (tmp); ASSERT (iblock == NULL, "convert ip4 routes", @@ -234,7 +220,7 @@ test_convert_ipv4_routes_block (void) tmp = iblock; ASSERT (iblock != NULL, "convert ip4 routes", "should get one route"); - check_ip_block (iblock, "10.0.0.0", "255.0.0.0", "192.168.0.1"); + check_ip_block (iblock, "10.0.0.0", 8, "192.168.0.1"); iblock = iblock->next; destroy_ip_block (tmp); ASSERT (iblock == NULL, "convert ip4 routes", diff --git a/src/settings/plugins/ifupdown/parser.c b/src/settings/plugins/ifupdown/parser.c index 415cfcc755..72340a90eb 100644 --- a/src/settings/plugins/ifupdown/parser.c +++ b/src/settings/plugins/ifupdown/parser.c @@ -447,8 +447,8 @@ update_ip4_setting_from_if_block(NMConnection *connection, if (!is_static) { g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); } else { - guint32 tmp_addr, tmp_mask, tmp_gw; - NMIP4Address *addr; + guint32 tmp_mask; + NMIPAddress *addr; const char *address_v; const char *netmask_v; const char *gateway_v; @@ -460,10 +460,9 @@ update_ip4_setting_from_if_block(NMConnection *connection, /* Address */ address_v = ifparser_getkey (block, "address"); - if (!address_v || !inet_pton (AF_INET, address_v, &tmp_addr)) { + if (!address_v) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Missing IPv4 address '%s'", - address_v ? address_v : "(none)"); + "Missing IPv4 address"); goto error; } @@ -472,11 +471,6 @@ update_ip4_setting_from_if_block(NMConnection *connection, if (netmask_v) { if (strlen (netmask_v) < 7) { netmask_int = atoi (netmask_v); - if (netmask_int > 32) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid IPv4 netmask '%s'", netmask_v); - goto error; - } } else if (!inet_pton (AF_INET, netmask_v, &tmp_mask)) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid IPv4 netmask '%s'", netmask_v); @@ -490,17 +484,11 @@ update_ip4_setting_from_if_block(NMConnection *connection, gateway_v = ifparser_getkey (block, "gateway"); if (!gateway_v) gateway_v = address_v; /* dcbw: whaaa?? */ - if (!inet_pton (AF_INET, gateway_v, &tmp_gw)) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid IPv4 gateway '%s'", gateway_v); - goto error; - } /* Add the new address to the setting */ - addr = nm_ip4_address_new (); - nm_ip4_address_set_address (addr, tmp_addr); - nm_ip4_address_set_prefix (addr, netmask_int); - nm_ip4_address_set_gateway (addr, tmp_gw); + addr = nm_ip_address_new (AF_INET, address_v, netmask_int, gateway_v, error); + if (!addr) + goto error; if (nm_setting_ip4_config_add_address (s_ip4, addr)) { nm_log_info (LOGD_SETTINGS, "addresses count: %d", @@ -508,7 +496,7 @@ update_ip4_setting_from_if_block(NMConnection *connection, } else { nm_log_info (LOGD_SETTINGS, "ignoring duplicate IP4 address"); } - nm_ip4_address_unref (addr); + nm_ip_address_unref (addr); nameserver_v = ifparser_getkey (block, "dns-nameserver"); ifupdown_ip4_add_dns (s_ip4, nameserver_v); @@ -582,8 +570,7 @@ update_ip6_setting_from_if_block(NMConnection *connection, if (!is_static) { g_object_set(s_ip6, NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); } else { - struct in6_addr tmp_addr, tmp_gw; - NMIP6Address *addr; + NMIPAddress *addr; const char *address_v; const char *prefix_v; const char *gateway_v; @@ -595,10 +582,9 @@ update_ip6_setting_from_if_block(NMConnection *connection, /* Address */ address_v = ifparser_getkey(block, "address"); - if (!address_v || !inet_pton (AF_INET6, address_v, &tmp_addr)) { + if (!address_v) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Missing IPv6 address '%s'", - address_v ? address_v : "(none)"); + "Missing IPv6 address"); goto error; } @@ -611,17 +597,11 @@ update_ip6_setting_from_if_block(NMConnection *connection, gateway_v = ifparser_getkey (block, "gateway"); if (!gateway_v) gateway_v = address_v; /* dcbw: whaaa?? */ - if (!inet_pton (AF_INET6, gateway_v, &tmp_gw)) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid IPv6 gateway '%s'", gateway_v); - goto error; - } /* Add the new address to the setting */ - addr = nm_ip6_address_new (); - nm_ip6_address_set_address (addr, &tmp_addr); - nm_ip6_address_set_prefix (addr, prefix_int); - nm_ip6_address_set_gateway (addr, &tmp_gw); + addr = nm_ip_address_new (AF_INET6, address_v, prefix_int, gateway_v, error); + if (!addr) + goto error; if (nm_setting_ip6_config_add_address (s_ip6, addr)) { nm_log_info (LOGD_SETTINGS, "addresses count: %d", @@ -629,7 +609,7 @@ update_ip6_setting_from_if_block(NMConnection *connection, } else { nm_log_info (LOGD_SETTINGS, "ignoring duplicate IP6 address"); } - nm_ip6_address_unref (addr); + nm_ip_address_unref (addr); nameserver_v = ifparser_getkey(block, "dns-nameserver"); ifupdown_ip6_add_dns (s_ip6, nameserver_v); diff --git a/src/settings/plugins/ifupdown/tests/test-ifupdown.c b/src/settings/plugins/ifupdown/tests/test-ifupdown.c index 8100676d79..643fa7d08a 100644 --- a/src/settings/plugins/ifupdown/tests/test-ifupdown.c +++ b/src/settings/plugins/ifupdown/tests/test-ifupdown.c @@ -464,13 +464,10 @@ test17_read_static_ipv4 (const char *path) char *unmanaged = NULL; GError *error = NULL; const char* tmp; - const char *expected_address = "10.0.0.3"; const char *expected_id = "Ifupdown (eth0)"; const char *expected_search1 = "example.com"; const char *expected_search2 = "foo.example.com"; - guint32 expected_prefix = 8; - NMIP4Address *ip4_addr; - guint32 addr; + NMIPAddress *ip4_addr; #define TEST17_NAME "wired-static-verify-ip4" if_block *block = NULL; @@ -521,10 +518,6 @@ test17_read_static_ipv4 (const char *path) /* ===== IPv4 SETTING ===== */ - ASSERT (inet_pton (AF_INET, expected_address, &addr) > 0, - TEST17_NAME, "failed to verify %s: couldn't convert IP address #1", - file); - s_ip4 = nm_connection_get_setting_ip4_config (connection); ASSERT (s_ip4 != NULL, TEST17_NAME, "failed to verify %s: missing %s setting", @@ -547,17 +540,9 @@ test17_read_static_ipv4 (const char *path) NM_SETTING_IP4_CONFIG_ADDRESSES); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); - ASSERT (ip4_addr, - TEST17_NAME, "failed to verify %s: missing IP4 address #1", - file); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix, - TEST17_NAME, "failed to verify %s: unexpected IP4 address prefix", - file); - - ASSERT (nm_ip4_address_get_address (ip4_addr) == addr, - TEST17_NAME, "failed to verify %s: unexpected IP4 address: %s", - file, addr); + g_assert (ip4_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "10.0.0.3"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 8); /* DNS Addresses */ ASSERT (nm_setting_ip4_config_get_num_dns (s_ip4) == 2, @@ -629,13 +614,10 @@ test18_read_static_ipv6 (const char *path) char *unmanaged = NULL; GError *error = NULL; const char* tmp; - const char *expected_address = "fc00::1"; const char *expected_id = "Ifupdown (myip6tunnel)"; const char *expected_search1 = "example.com"; const char *expected_search2 = "foo.example.com"; - guint32 expected_prefix = 64; - NMIP6Address *ip6_addr; - struct in6_addr addr; + NMIPAddress *ip6_addr; if_block *block = NULL; #define TEST18_NAME "wired-static-verify-ip6" const char* file = "test18-" TEST18_NAME; @@ -691,11 +673,6 @@ test18_read_static_ipv6 (const char *path) /* ===== IPv6 SETTING ===== */ - ASSERT (inet_pton (AF_INET6, expected_address, &addr) > 0, - TEST18_NAME, - "failed to verify %s: couldn't convert IP address #1", - file); - s_ip6 = nm_connection_get_setting_ip6_config (connection); ASSERT (s_ip6 != NULL, TEST18_NAME, @@ -721,27 +698,9 @@ test18_read_static_ipv6 (const char *path) NM_SETTING_IP6_CONFIG_ADDRESSES); ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); - ASSERT (ip6_addr, - TEST18_NAME, - "failed to verify %s: missing %s / %s #1", - file, - NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ADDRESSES); - - ASSERT (nm_ip6_address_get_prefix (ip6_addr) == expected_prefix, - TEST18_NAME - "failed to verify %s: unexpected %s / %s prefix", - file, - NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ADDRESSES); - - ASSERT (IN6_ARE_ADDR_EQUAL (nm_ip6_address_get_address (ip6_addr), - &addr), - TEST18_NAME, - "failed to verify %s: unexpected %s / %s", - file, - NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ADDRESSES); + g_assert (ip6_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "fc00::1"); + g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64); /* DNS Addresses */ ASSERT (nm_setting_ip6_config_get_num_dns (s_ip6) == 2, @@ -813,10 +772,7 @@ test19_read_static_ipv4_plen (const char *path) NMSettingIP4Config *s_ip4; char *unmanaged = NULL; GError *error = NULL; - const char *expected_address = "10.0.0.3"; - guint32 expected_prefix = 8; - NMIP4Address *ip4_addr; - guint32 addr; + NMIPAddress *ip4_addr; #define TEST19_NAME "wired-static-verify-ip4-plen" if_block *block = NULL; @@ -838,10 +794,6 @@ test19_read_static_ipv4_plen (const char *path) /* ===== IPv4 SETTING ===== */ - ASSERT (inet_pton (AF_INET, expected_address, &addr) > 0, - TEST19_NAME, "failed to verify %s: couldn't convert IP address #1", - file); - s_ip4 = nm_connection_get_setting_ip4_config (connection); ASSERT (s_ip4 != NULL, TEST19_NAME, "failed to verify %s: missing %s setting", @@ -856,17 +808,9 @@ test19_read_static_ipv4_plen (const char *path) NM_SETTING_IP4_CONFIG_ADDRESSES); ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); - ASSERT (ip4_addr, - TEST19_NAME, "failed to verify %s: missing IP4 address #1", - file); - - ASSERT (nm_ip4_address_get_prefix (ip4_addr) == expected_prefix, - TEST19_NAME, "failed to verify %s: unexpected IP4 address prefix", - file); - - ASSERT (nm_ip4_address_get_address (ip4_addr) == addr, - TEST19_NAME, "failed to verify %s: unexpected IP4 address: %s", - file, addr); + g_assert (ip4_addr != NULL); + g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "10.0.0.3"); + g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 8); g_object_unref (connection); } diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c index cbd6e49ab1..97ccf7d89b 100644 --- a/src/settings/plugins/keyfile/reader.c +++ b/src/settings/plugins/keyfile/reader.c @@ -108,130 +108,57 @@ get_one_int (const char *str, guint32 max_val, const char *key_name, guint32 *ou } static gpointer -build_ip4_address_or_route (const char *key_name, const char *address_str, guint32 plen, const char *gateway_str, const char *metric_str, gboolean route) +build_address_or_route (const char *key_name, const char *address_str, guint32 plen, const char *gateway_str, const char *metric_str, int family, gboolean route) { gpointer result; - guint32 addr; - guint32 address = 0; - guint32 gateway = 0; guint32 metric = 0; - int err; + GError *error = NULL; g_return_val_if_fail (address_str, NULL); - /* Address */ - err = inet_pton (AF_INET, address_str, &addr); - if (err <= 0) { - nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid IPv4 address '%s'", __func__, address_str); - return NULL; - } - address = addr; - /* Gateway */ if (gateway_str && gateway_str[0]) { - err = inet_pton (AF_INET, gateway_str, &addr); - if (err <= 0) { - nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid IPv4 gateway '%s'", __func__, gateway_str); - return NULL; - } - gateway = addr; - } - else - gateway = 0; - - /* parse metric, default to 0 */ - if (metric_str) { - if (!get_one_int (metric_str, G_MAXUINT32, key_name, &metric)) - return NULL; - } - - if (route) { - result = nm_ip4_route_new (); - nm_ip4_route_set_dest (result, address); - nm_ip4_route_set_prefix (result, plen); - nm_ip4_route_set_next_hop (result, gateway); - nm_ip4_route_set_metric (result, metric); - } else { - result = nm_ip4_address_new (); - nm_ip4_address_set_address (result, address); - nm_ip4_address_set_prefix (result, plen); - nm_ip4_address_set_gateway (result, gateway); - } - - return result; -} - -static gpointer -build_ip6_address_or_route (const char *key_name, const char *address_str, guint32 plen, const char *gateway_str, const char *metric_str, gboolean route) -{ - gpointer result; - struct in6_addr addr; - guint32 metric = 0; - int err; - - g_return_val_if_fail (address_str, NULL); - - if (route) - result = nm_ip6_route_new (); - else - result = nm_ip6_address_new (); - - /* add address and prefix length */ - err = inet_pton (AF_INET6, address_str, &addr); - if (err <= 0) { - nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid IPv6 address '%s'", __func__, address_str); - goto error_out; - } - if (route) { - nm_ip6_route_set_dest (result, &addr); - nm_ip6_route_set_prefix (result, plen); - } else { - nm_ip6_address_set_address (result, &addr); - nm_ip6_address_set_prefix (result, plen); - } - - /* add gateway */ - if (gateway_str && gateway_str[0]) { - err = inet_pton (AF_INET6, gateway_str, &addr); - if (err <= 0) { + if (!nm_utils_ipaddr_valid (family, gateway_str)) { /* Try workaround for routes written by broken keyfile writer. * Due to bug bgo#719851, an older version of writer would have * written "a:b:c:d::/plen,metric" if the gateway was ::, instead * of "a:b:c:d::/plen,,metric" or "a:b:c:d::/plen,::,metric" - * Try workaround by interepeting gateway_str as metric to accept such + * Try workaround by interpreting gateway_str as metric to accept such * invalid routes. This broken syntax should not be not officially * supported. **/ - if (route && !metric_str && get_one_int (gateway_str, G_MAXUINT32, NULL, &metric)) - addr = in6addr_any; + if ( family == AF_INET6 + && route + && !metric_str + && get_one_int (gateway_str, G_MAXUINT32, NULL, &metric)) + gateway_str = NULL; else { - nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid IPv6 gateway '%s'", __func__, gateway_str); - goto error_out; + nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid gateway '%s'", __func__, gateway_str); + return NULL; } } } else - addr = in6addr_any; - - if (route) { - nm_ip6_route_set_next_hop (result, &addr); + gateway_str = NULL; - /* parse metric, default to 0 */ - if (metric_str) { - if (!get_one_int (metric_str, G_MAXUINT32, key_name, &metric)) - goto error_out; - } - nm_ip6_route_set_metric (result, metric); - } else - nm_ip6_address_set_gateway (result, &addr); - - return result; + /* parse metric, default to 0 */ + if (metric_str) { + if (!get_one_int (metric_str, G_MAXUINT32, key_name, &metric)) + return NULL; + } -error_out: if (route) - nm_ip6_route_unref (result); + result = nm_ip_route_new (family, address_str, plen, gateway_str, metric, &error); else - nm_ip4_route_unref (result); - return NULL; + result = nm_ip_address_new (family, address_str, plen, gateway_str, &error); + if (!result) { + nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid %s %s: %s", __func__, + family == AF_INET ? "IPv4" : "IPv6", + route ? "route" : "address", + error->message); + g_error_free (error); + } + + return result; } /* On success, returns pointer to the zero-terminated field (original @current). @@ -390,8 +317,8 @@ read_one_ip_address_or_route (GKeyFile *file, } /* build the appropriate data structure for NetworkManager settings */ - result = (ipv6 ? build_ip6_address_or_route : build_ip4_address_or_route) ( - key_name, address_str, plen, gateway_str, metric_str, route); + result = build_address_or_route (key_name, address_str, plen, gateway_str, metric_str, + ipv6 ? AF_INET6 : AF_INET, route); g_free (value); return result; @@ -413,17 +340,10 @@ ip_address_or_route_parser (NMSetting *setting, const char *key, GKeyFile *keyfi GDestroyNotify free_func; int i; - if (ipv6) { - if (routes) - free_func = (GDestroyNotify) nm_ip6_route_unref; - else - free_func = (GDestroyNotify) nm_ip6_address_unref; - } else { - if (routes) - free_func = (GDestroyNotify) nm_ip4_route_unref; - else - free_func = (GDestroyNotify) nm_ip4_address_unref; - } + if (routes) + free_func = (GDestroyNotify) nm_ip_route_unref; + else + free_func = (GDestroyNotify) nm_ip_address_unref; list = g_ptr_array_new_with_free_func (free_func); for (i = -1; i < 1000; i++) { diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index e37f7c8a90..0e05fed45b 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -38,69 +38,51 @@ #define TEST_WIRELESS_FILE TEST_KEYFILES_DIR"/Test_Wireless_Connection" static void -check_ip4_address (NMSettingIP4Config *config, int idx, const char *address_str, int plen, const char *gateway_str) +check_ip4_address (NMSettingIP4Config *config, int idx, const char *address, int plen, const char *gateway) { - NMIP4Address *ip4 = nm_setting_ip4_config_get_address (config, idx); - guint32 address, gateway; - - g_assert (inet_pton (AF_INET, address_str, &address) == 1); - g_assert (inet_pton (AF_INET, gateway_str, &gateway) == 1); + NMIPAddress *ip4 = nm_setting_ip4_config_get_address (config, idx); g_assert (ip4); - g_assert (nm_ip4_address_get_address (ip4) == address); - g_assert (nm_ip4_address_get_prefix (ip4) == plen); - g_assert (nm_ip4_address_get_gateway (ip4) == gateway); + g_assert_cmpstr (nm_ip_address_get_address (ip4), ==, address); + g_assert_cmpint (nm_ip_address_get_prefix (ip4), ==, plen); + g_assert_cmpstr (nm_ip_address_get_gateway (ip4), ==, gateway); } static void -check_ip6_address (NMSettingIP6Config *config, int idx, const char *address_str, int plen, const char *gateway_str) +check_ip6_address (NMSettingIP6Config *config, int idx, const char *address, int plen, const char *gateway) { - NMIP6Address *ip6 = nm_setting_ip6_config_get_address (config, idx); - struct in6_addr address; - struct in6_addr gateway; - - g_assert (inet_pton (AF_INET6, address_str, &address) == 1); - g_assert (inet_pton (AF_INET6, gateway_str, &gateway) == 1); + NMIPAddress *ip6 = nm_setting_ip6_config_get_address (config, idx); g_assert (ip6); - g_assert (!memcmp (nm_ip6_address_get_address (ip6), &address, sizeof(address))); - g_assert (nm_ip6_address_get_prefix (ip6) == plen); - g_assert (!memcmp (nm_ip6_address_get_gateway (ip6), &gateway, sizeof(gateway))); + g_assert_cmpstr (nm_ip_address_get_address (ip6), ==, address); + g_assert_cmpint (nm_ip_address_get_prefix (ip6), ==, plen); + g_assert_cmpstr (nm_ip_address_get_gateway (ip6), ==, gateway); } static void -check_ip4_route (NMSettingIP4Config *config, int idx, const char *destination_str, int plen, - const char *nexthop_str, int metric) +check_ip4_route (NMSettingIP4Config *config, int idx, const char *destination, int plen, + const char *next_hop, int metric) { - NMIP4Route *route = nm_setting_ip4_config_get_route (config, idx); - guint32 destination, nexthop; - - g_assert (inet_pton (AF_INET, destination_str, &destination) == 1); - g_assert (inet_pton (AF_INET, nexthop_str, &nexthop) == 1); + NMIPRoute *route = nm_setting_ip4_config_get_route (config, idx); g_assert (route); - g_assert (nm_ip4_route_get_dest (route) == destination); - g_assert (nm_ip4_route_get_prefix (route) == plen); - g_assert (nm_ip4_route_get_next_hop (route) == nexthop); - g_assert (nm_ip4_route_get_metric (route) == metric); + g_assert_cmpstr (nm_ip_route_get_dest (route), ==, destination); + g_assert_cmpint (nm_ip_route_get_prefix (route), ==, plen); + g_assert_cmpstr (nm_ip_route_get_next_hop (route), ==, next_hop); + g_assert_cmpint (nm_ip_route_get_metric (route), ==, metric); } static void -check_ip6_route (NMSettingIP6Config *config, int idx, const char *destination_str, int plen, - const char *next_hop_str, int metric) +check_ip6_route (NMSettingIP6Config *config, int idx, const char *destination, int plen, + const char *next_hop, int metric) { - NMIP6Route *route = nm_setting_ip6_config_get_route (config, idx); - struct in6_addr destination; - struct in6_addr next_hop; - - g_assert (inet_pton (AF_INET6, destination_str, &destination) == 1); - g_assert (inet_pton (AF_INET6, next_hop_str, &next_hop) == 1); + NMIPRoute *route = nm_setting_ip6_config_get_route (config, idx); g_assert (route); - g_assert (!memcmp (nm_ip6_route_get_dest (route), &destination, sizeof(destination))); - g_assert (nm_ip6_route_get_prefix (route) == plen); - g_assert (!memcmp (nm_ip6_route_get_next_hop (route), &next_hop, sizeof(next_hop))); - g_assert (nm_ip6_route_get_metric (route) == metric); + g_assert_cmpstr (nm_ip_route_get_dest (route), ==, destination); + g_assert_cmpint (nm_ip_route_get_prefix (route), ==, plen); + g_assert_cmpstr (nm_ip_route_get_next_hop (route), ==, next_hop); + g_assert_cmpint (nm_ip_route_get_metric (route), ==, metric); } static NMConnection * @@ -291,24 +273,24 @@ test_read_valid_wired_connection (void) check_ip4_address (s_ip4, 0, "2.3.4.5", 24, "2.3.4.6"); check_ip4_address (s_ip4, 1, "192.168.0.5", 24, "192.168.0.1"); check_ip4_address (s_ip4, 2, "1.2.3.4", 16, "1.2.1.1"); - check_ip4_address (s_ip4, 3, "3.4.5.6", 16, "0.0.0.0"); + check_ip4_address (s_ip4, 3, "3.4.5.6", 16, NULL); check_ip4_address (s_ip4, 4, "4.5.6.7", 24, "1.2.3.4"); - check_ip4_address (s_ip4, 5, "5.6.7.8", 24, "0.0.0.0"); + check_ip4_address (s_ip4, 5, "5.6.7.8", 24, NULL); /* IPv4 routes */ g_assert (nm_setting_ip4_config_get_num_routes (s_ip4) == 12); - check_ip4_route (s_ip4, 0, "5.6.7.8", 32, "0.0.0.0", 0); + check_ip4_route (s_ip4, 0, "5.6.7.8", 32, NULL, 0); check_ip4_route (s_ip4, 1, "1.2.3.0", 24, "2.3.4.8", 99); - check_ip4_route (s_ip4, 2, "1.1.1.2", 12, "0.0.0.0", 0); - check_ip4_route (s_ip4, 3, "1.1.1.3", 13, "0.0.0.0", 0); + check_ip4_route (s_ip4, 2, "1.1.1.2", 12, NULL, 0); + check_ip4_route (s_ip4, 3, "1.1.1.3", 13, NULL, 0); check_ip4_route (s_ip4, 4, "1.1.1.4", 14, "2.2.2.4", 0); check_ip4_route (s_ip4, 5, "1.1.1.5", 15, "2.2.2.5", 0); check_ip4_route (s_ip4, 6, "1.1.1.6", 16, "2.2.2.6", 0); - check_ip4_route (s_ip4, 7, "1.1.1.7", 17, "0.0.0.0", 0); - check_ip4_route (s_ip4, 8, "1.1.1.8", 18, "0.0.0.0", 0); - check_ip4_route (s_ip4, 9, "1.1.1.9", 19, "0.0.0.0", 0); - check_ip4_route (s_ip4, 10, "1.1.1.10", 20, "0.0.0.0", 0); - check_ip4_route (s_ip4, 11, "1.1.1.11", 21, "0.0.0.0", 21); + check_ip4_route (s_ip4, 7, "1.1.1.7", 17, NULL, 0); + check_ip4_route (s_ip4, 8, "1.1.1.8", 18, NULL, 0); + check_ip4_route (s_ip4, 9, "1.1.1.9", 19, NULL, 0); + check_ip4_route (s_ip4, 10, "1.1.1.10", 20, NULL, 0); + check_ip4_route (s_ip4, 11, "1.1.1.11", 21, NULL, 21); /* ===== IPv6 SETTING ===== */ @@ -371,25 +353,25 @@ test_read_valid_wired_connection (void) /* IPv6 addresses */ g_assert (nm_setting_ip6_config_get_num_addresses (s_ip6) == 10); check_ip6_address (s_ip6, 0, "2:3:4:5:6:7:8:9", 64, "2:3:4:5:1:2:3:4"); - check_ip6_address (s_ip6, 1, "abcd:1234:ffff::cdde", 64, "::"); - check_ip6_address (s_ip6, 2, "1:2:3:4:5:6:7:8", 96, "::"); - check_ip6_address (s_ip6, 3, "3:4:5:6:7:8:9:0", 128, "::"); - check_ip6_address (s_ip6, 4, "3:4:5:6:7:8:9:14", 64, "::"); - check_ip6_address (s_ip6, 5, "3:4:5:6:7:8:9:15", 64, "::"); - check_ip6_address (s_ip6, 6, "3:4:5:6:7:8:9:16", 66, "::"); - check_ip6_address (s_ip6, 7, "3:4:5:6:7:8:9:17", 67, "::"); - check_ip6_address (s_ip6, 8, "3:4:5:6:7:8:9:18", 68, "::"); - check_ip6_address (s_ip6, 9, "3:4:5:6:7:8:9:19", 69, "1::09"); + check_ip6_address (s_ip6, 1, "abcd:1234:ffff::cdde", 64, NULL); + check_ip6_address (s_ip6, 2, "1:2:3:4:5:6:7:8", 96, NULL); + check_ip6_address (s_ip6, 3, "3:4:5:6:7:8:9:0", 128, NULL); + check_ip6_address (s_ip6, 4, "3:4:5:6:7:8:9:14", 64, NULL); + check_ip6_address (s_ip6, 5, "3:4:5:6:7:8:9:15", 64, NULL); + check_ip6_address (s_ip6, 6, "3:4:5:6:7:8:9:16", 66, NULL); + check_ip6_address (s_ip6, 7, "3:4:5:6:7:8:9:17", 67, NULL); + check_ip6_address (s_ip6, 8, "3:4:5:6:7:8:9:18", 68, NULL); + check_ip6_address (s_ip6, 9, "3:4:5:6:7:8:9:19", 69, "1::9"); /* Route #1 */ g_assert (nm_setting_ip6_config_get_num_routes (s_ip6) == 7); check_ip6_route (s_ip6, 0, "d:e:f:0:1:2:3:4", 64, "f:e:d:c:1:2:3:4", 0); check_ip6_route (s_ip6, 1, "a:b:c:d::", 64, "f:e:d:c:1:2:3:4", 99); - check_ip6_route (s_ip6, 2, "8:7:6:5:4:3:2:1", 128, "::", 0); - check_ip6_route (s_ip6, 3, "6:7:8:9:0:1:2:3", 126, "::", 1); - check_ip6_route (s_ip6, 4, "7:8:9:0:1:2:3:4", 125, "::", 5); - check_ip6_route (s_ip6, 5, "8:9:0:1:2:3:4:5", 124, "::", 6); - check_ip6_route (s_ip6, 6, "8:9:0:1:2:3:4:6", 123, "::", 0); + check_ip6_route (s_ip6, 2, "8:7:6:5:4:3:2:1", 128, NULL, 0); + check_ip6_route (s_ip6, 3, "6:7:8:9:0:1:2:3", 126, NULL, 1); + check_ip6_route (s_ip6, 4, "7:8:9:0:1:2:3:4", 125, NULL, 5); + check_ip6_route (s_ip6, 5, "8:9:0:1:2:3:4:5", 124, NULL, 6); + check_ip6_route (s_ip6, 6, "8:9:0:1:2:3:4:6", 123, NULL, 0); g_object_unref (connection); } @@ -399,20 +381,13 @@ add_one_ip4_address (NMSettingIP4Config *s_ip4, const char *gw, guint32 prefix) { - guint32 tmp; - NMIP4Address *ip4_addr; - - ip4_addr = nm_ip4_address_new (); - nm_ip4_address_set_prefix (ip4_addr, prefix); - - inet_pton (AF_INET, addr, &tmp); - nm_ip4_address_set_address (ip4_addr, tmp); - - inet_pton (AF_INET, gw, &tmp); - nm_ip4_address_set_gateway (ip4_addr, tmp); + NMIPAddress *ip4_addr; + GError *error = NULL; + ip4_addr = nm_ip_address_new (AF_INET, addr, prefix, gw, &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, ip4_addr); - nm_ip4_address_unref (ip4_addr); + nm_ip_address_unref (ip4_addr); } static void @@ -422,21 +397,13 @@ add_one_ip4_route (NMSettingIP4Config *s_ip4, guint32 prefix, guint32 metric) { - guint32 addr; - NMIP4Route *route; - - route = nm_ip4_route_new (); - nm_ip4_route_set_prefix (route, prefix); - nm_ip4_route_set_metric (route, metric); - - inet_pton (AF_INET, dest, &addr); - nm_ip4_route_set_dest (route, addr); - - inet_pton (AF_INET, nh, &addr); - nm_ip4_route_set_next_hop (route, addr); + NMIPRoute *route; + GError *error = NULL; + route = nm_ip_route_new (AF_INET, dest, prefix, nh, metric, &error); + g_assert_no_error (error); nm_setting_ip4_config_add_route (s_ip4, route); - nm_ip4_route_unref (route); + nm_ip_route_unref (route); } static void @@ -445,22 +412,13 @@ add_one_ip6_address (NMSettingIP6Config *s_ip6, guint32 prefix, const char *gw) { - struct in6_addr tmp; - NMIP6Address *ip6_addr; - - ip6_addr = nm_ip6_address_new (); - nm_ip6_address_set_prefix (ip6_addr, prefix); - - inet_pton (AF_INET6, addr, &tmp); - nm_ip6_address_set_address (ip6_addr, &tmp); - - if (gw) { - inet_pton (AF_INET6, gw, &tmp); - nm_ip6_address_set_gateway (ip6_addr, &tmp); - } + NMIPAddress *ip6_addr; + GError *error = NULL; + ip6_addr = nm_ip_address_new (AF_INET6, addr, prefix, gw, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_address (s_ip6, ip6_addr); - nm_ip6_address_unref (ip6_addr); + nm_ip_address_unref (ip6_addr); } static void @@ -470,21 +428,13 @@ add_one_ip6_route (NMSettingIP6Config *s_ip6, guint32 prefix, guint32 metric) { - struct in6_addr addr; - NMIP6Route *route; - - route = nm_ip6_route_new (); - nm_ip6_route_set_prefix (route, prefix); - nm_ip6_route_set_metric (route, metric); - - inet_pton (AF_INET6, dest, &addr); - nm_ip6_route_set_dest (route, &addr); - - inet_pton (AF_INET6, nh, &addr); - nm_ip6_route_set_next_hop (route, &addr); + NMIPRoute *route; + GError *error = NULL; + route = nm_ip_route_new (AF_INET6, dest, prefix, nh, metric, &error); + g_assert_no_error (error); nm_setting_ip6_config_add_route (s_ip6, route); - nm_ip6_route_unref (route); + nm_ip_route_unref (route); } @@ -515,9 +465,9 @@ test_write_wired_connection (void) const char *route2 = "1.1.1.1"; const char *route2_nh = "1.2.1.1"; const char *route3 = "2.2.2.2"; - const char *route3_nh = "0.0.0.0"; + const char *route3_nh = NULL; const char *route4 = "3.3.3.3"; - const char *route4_nh = "0.0.0.0"; + const char *route4_nh = NULL; const char *dns6_1 = "1::cafe"; const char *dns6_2 = "2::cafe"; const char *address6_1 = "abcd::beef"; diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c index 17d4d9581a..766eabf44f 100644 --- a/src/settings/plugins/keyfile/writer.c +++ b/src/settings/plugins/keyfile/writer.c @@ -97,12 +97,12 @@ write_array_of_uint (GKeyFile *file, } static void -ip4_dns_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) +dns_writer (GKeyFile *file, + const char *keyfile_dir, + const char *uuid, + NMSetting *setting, + const char *key, + const GValue *value) { char **list; @@ -114,51 +114,59 @@ ip4_dns_writer (GKeyFile *file, } static void -write_ip4_values (GKeyFile *file, - const char *setting_name, - GPtrArray *array, - gboolean is_route) +write_ip_values (GKeyFile *file, + const char *setting_name, + GPtrArray *array, + gboolean is_route) { GString *output; - int i; - guint32 addr, gw, plen, metric; + int family, i; + const char *addr, *gw; + guint32 plen, metric; char key_name[30], *key_name_idx; if (!array->len) return; + family = !strcmp (setting_name, NM_SETTING_IP4_CONFIG_SETTING_NAME) ? AF_INET : AF_INET6; + strcpy (key_name, is_route ? "route" : "address"); key_name_idx = key_name + strlen (key_name); output = g_string_sized_new (2*INET_ADDRSTRLEN + 10); for (i = 0; i < array->len; i++) { if (is_route) { - NMIP4Route *route = array->pdata[i]; + NMIPRoute *route = array->pdata[i]; - addr = nm_ip4_route_get_dest (route); - plen = nm_ip4_route_get_prefix (route); - gw = nm_ip4_route_get_next_hop (route); - metric = nm_ip4_route_get_metric (route); + addr = nm_ip_route_get_dest (route); + plen = nm_ip_route_get_prefix (route); + gw = nm_ip_route_get_next_hop (route); + metric = nm_ip_route_get_metric (route); } else { - NMIP4Address *address = array->pdata[i]; + NMIPAddress *address = array->pdata[i]; - addr = nm_ip4_address_get_address (address); - plen = nm_ip4_address_get_prefix (address); - gw = nm_ip4_address_get_gateway (address); + addr = nm_ip_address_get_address (address); + plen = nm_ip_address_get_prefix (address); + gw = nm_ip_address_get_gateway (address); metric = 0; } g_string_set_size (output, 0); - g_string_append_printf (output, "%s/%u", - nm_utils_inet4_ntop (addr, NULL), - (unsigned) plen); + g_string_append_printf (output, "%s/%u", addr, plen); if (metric || gw) { /* Older versions of the plugin do not support the form * "a.b.c.d/plen,,metric", so, we always have to write the - * gateway, even if it's 0.0.0.0. - * The current version support reading of the above form. */ - g_string_append_c (output, ','); - g_string_append (output, nm_utils_inet4_ntop (gw, NULL)); + * gateway, even if there isn't one. + * The current version supports reading of the above form. + */ + if (!gw) { + if (family == AF_INET) + gw = "0.0.0.0"; + else + gw = "::"; + } + + g_string_append_printf (output, ",%s", gw); if (metric) g_string_append_printf (output, ",%lu", (unsigned long) metric); } @@ -170,19 +178,19 @@ write_ip4_values (GKeyFile *file, } static void -ip4_addr_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) +addr_writer (GKeyFile *file, + const char *keyfile_dir, + const char *uuid, + NMSetting *setting, + const char *key, + const GValue *value) { GPtrArray *array; const char *setting_name = nm_setting_get_name (setting); array = (GPtrArray *) g_value_get_boxed (value); if (array && array->len) - write_ip4_values (file, setting_name, array, FALSE); + write_ip_values (file, setting_name, array, FALSE); } static void @@ -197,154 +205,21 @@ ip4_addr_label_writer (GKeyFile *file, } static void -ip4_route_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) +route_writer (GKeyFile *file, + const char *keyfile_dir, + const char *uuid, + NMSetting *setting, + const char *key, + const GValue *value) { GPtrArray *array; const char *setting_name = nm_setting_get_name (setting); array = (GPtrArray *) g_value_get_boxed (value); if (array && array->len) - write_ip4_values (file, setting_name, array, TRUE); -} - -static void -ip6_dns_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) -{ - char **list; - - list = g_value_get_boxed (value); - if (list && list[0]) { - nm_keyfile_plugin_kf_set_string_list (file, nm_setting_get_name (setting), key, - (const char **) list, g_strv_length (list)); - } -} - -static char * -ip6_values_to_addr_prefix (const struct in6_addr *addr, guint prefix, const struct in6_addr *gw, - gboolean force_write_gateway) -{ - GString *ip6_str; - char buf[INET6_ADDRSTRLEN]; - - /* address */ - nm_utils_inet6_ntop (addr, buf); - - /* Enough space for the address, '/', and the prefix */ - ip6_str = g_string_sized_new ((INET6_ADDRSTRLEN * 2) + 5); - - /* prefix */ - g_string_append (ip6_str, buf); - g_string_append_printf (ip6_str, "/%u", prefix); - - /* gateway */ - nm_utils_inet6_ntop (gw, buf); - if (force_write_gateway || !IN6_IS_ADDR_UNSPECIFIED (gw)) - g_string_append_printf (ip6_str, ",%s", buf); - - return g_string_free (ip6_str, FALSE); + write_ip_values (file, setting_name, array, TRUE); } -static void -ip6_addr_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) -{ - GPtrArray *array; - const char *setting_name = nm_setting_get_name (setting); - int i, j; - - array = (GPtrArray *) g_value_get_boxed (value); - if (!array || !array->len) - return; - - for (i = 0, j = 1; i < array->len; i++) { - NMIP6Address *addr = array->pdata[i]; - char *key_name, *ip6_addr; - - ip6_addr = ip6_values_to_addr_prefix (nm_ip6_address_get_address (addr), - nm_ip6_address_get_prefix (addr), - nm_ip6_address_get_gateway (addr), - /* we allow omitting the gateway if it's :: */ - FALSE); - /* Write it out */ - key_name = g_strdup_printf ("address%d", j++); - nm_keyfile_plugin_kf_set_string (file, setting_name, key_name, ip6_addr); - g_free (key_name); - g_free (ip6_addr); - } -} - -static void -ip6_route_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) -{ - GPtrArray *array; - const char *setting_name = nm_setting_get_name (setting); - GString *output; - int i, j; - - array = (GPtrArray *) g_value_get_boxed (value); - if (!array || !array->len) - return; - - for (i = 0, j = 1; i < array->len; i++) { - NMIP6Route *route = array->pdata[i]; - char *key_name; - char *addr_str; - guint metric; - - output = g_string_new (""); - - /* Metric */ - metric = nm_ip6_route_get_metric (route); - - /* Address, prefix and next hop - * We allow omitting the gateway ::, if we also omit the metric - * and force writing of the gateway, if we add a non zero metric. - * The current version of the reader also supports the syntax - * "a:b:c::/plen,,metric" for a gateway ::. - * As older versions of the plugin, cannot read this form, - * we always write the gateway, whenever we also write the metric. - * But if possible, we omit them both (",::,0") or only the metric - * (",0"). - **/ - addr_str = ip6_values_to_addr_prefix (nm_ip6_route_get_dest (route), - nm_ip6_route_get_prefix (route), - nm_ip6_route_get_next_hop (route), - metric != 0); - g_string_append (output, addr_str); - g_free (addr_str); - - if (metric != 0) - g_string_append_printf (output, ",%u", metric); - - /* Write it out */ - key_name = g_strdup_printf ("route%d", j++); - nm_keyfile_plugin_kf_set_string (file, setting_name, key_name, output->str); - g_free (key_name); - - g_string_free (output, TRUE); - } -} - - static void write_hash_of_string (GKeyFile *file, NMSetting *setting, @@ -704,25 +579,25 @@ static KeyWriter key_writers[] = { setting_alias_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES, - ip4_addr_writer }, + addr_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, "address-labels", ip4_addr_label_writer }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_ADDRESSES, - ip6_addr_writer }, + addr_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ROUTES, - ip4_route_writer }, + route_writer }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_ROUTES, - ip6_route_writer }, + route_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS, - ip4_dns_writer }, + dns_writer }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS, - ip6_dns_writer }, + dns_writer }, { NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID, ssid_writer }, diff --git a/src/tests/test-general.c b/src/tests/test-general.c index e6bc207bd7..e16d702ddb 100644 --- a/src/tests/test-general.c +++ b/src/tests/test-general.c @@ -576,8 +576,8 @@ test_connection_no_match_ip4_addr (void) GSList *connections = NULL; NMSettingIP4Config *s_ip4; NMSettingIP6Config *s_ip6; - NMIP4Address *nm_addr; - guint32 addr, gw; + NMIPAddress *nm_addr; + GError *error = NULL; orig = _match_connection_new (); copy = nm_simple_connection_new_clone (orig); @@ -604,28 +604,20 @@ test_connection_no_match_ip4_addr (void) g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); - nm_addr = nm_ip4_address_new (); - inet_pton (AF_INET, "1.1.1.4", &addr); - inet_pton (AF_INET, "1.1.1.254", &gw); - nm_ip4_address_set_address (nm_addr, addr); - nm_ip4_address_set_prefix (nm_addr, 24); - nm_ip4_address_set_gateway (nm_addr, gw); + nm_addr = nm_ip_address_new (AF_INET, "1.1.1.4", 24, "1.1.1.254", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, nm_addr); - nm_ip4_address_unref (nm_addr); + nm_ip_address_unref (nm_addr); s_ip4 = nm_connection_get_setting_ip4_config (copy); g_assert (s_ip4); g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); - nm_addr = nm_ip4_address_new (); - inet_pton (AF_INET, "2.2.2.4", &addr); - inet_pton (AF_INET, "2.2.2.254", &gw); - nm_ip4_address_set_address (nm_addr, addr); - nm_ip4_address_set_prefix (nm_addr, 24); - nm_ip4_address_set_gateway (nm_addr, gw); + nm_addr = nm_ip_address_new (AF_INET, "2.2.2.4", 24, "2.2.2.254", &error); + g_assert_no_error (error); nm_setting_ip4_config_add_address (s_ip4, nm_addr); - nm_ip4_address_unref (nm_addr); + nm_ip_address_unref (nm_addr); matched = nm_utils_match_connection (connections, orig, TRUE, NULL, NULL); g_assert (matched != copy); diff --git a/src/vpn-manager/nm-vpn-connection.c b/src/vpn-manager/nm-vpn-connection.c index 46e14ca153..2f02706b1f 100644 --- a/src/vpn-manager/nm-vpn-connection.c +++ b/src/vpn-manager/nm-vpn-connection.c @@ -1233,13 +1233,13 @@ nm_vpn_connection_ip4_config_get (DBusGProxy *proxy, routes = nm_utils_ip4_routes_from_gvalue (val); for (iter = routes; iter; iter = iter->next) { - NMIP4Route *item = iter->data; + NMIPRoute *item = iter->data; NMPlatformIP4Route route; memset (&route, 0, sizeof (route)); - route.network = nm_ip4_route_get_dest (item); - route.plen = nm_ip4_route_get_prefix (item); - route.gateway = nm_ip4_route_get_next_hop (item); + nm_ip_route_get_dest_binary (item, &route.network); + route.plen = nm_ip_route_get_prefix (item); + nm_ip_route_get_next_hop_binary (item, &route.gateway); route.source = NM_IP_CONFIG_SOURCE_VPN; route.metric = vpn_routing_metric (connection); @@ -1255,7 +1255,7 @@ nm_vpn_connection_ip4_config_get (DBusGProxy *proxy, nm_ip4_config_add_route (config, &route); } - g_slist_free_full (routes, (GDestroyNotify) nm_ip4_route_unref); + g_slist_free_full (routes, (GDestroyNotify) nm_ip_route_unref); } val = (GValue *) g_hash_table_lookup (config_hash, NM_VPN_PLUGIN_IP4_CONFIG_NEVER_DEFAULT); @@ -1380,13 +1380,13 @@ nm_vpn_connection_ip6_config_get (DBusGProxy *proxy, routes = nm_utils_ip6_routes_from_gvalue (val); for (iter = routes; iter; iter = iter->next) { - NMIP6Route *item = iter->data; + NMIPRoute *item = iter->data; NMPlatformIP6Route route; memset (&route, 0, sizeof (route)); - route.network = *nm_ip6_route_get_dest (item); - route.plen = nm_ip6_route_get_prefix (item); - route.gateway = *nm_ip6_route_get_next_hop (item); + nm_ip_route_get_dest_binary (item, &route.network); + route.plen = nm_ip_route_get_prefix (item); + nm_ip_route_get_next_hop_binary (item, &route.gateway); route.source = NM_IP_CONFIG_SOURCE_VPN; route.metric = vpn_routing_metric (connection); @@ -1402,7 +1402,7 @@ nm_vpn_connection_ip6_config_get (DBusGProxy *proxy, nm_ip6_config_add_route (config, &route); } - g_slist_free_full (routes, (GDestroyNotify) nm_ip6_route_unref); + g_slist_free_full (routes, (GDestroyNotify) nm_ip_route_unref); } val = (GValue *) g_hash_table_lookup (config_hash, NM_VPN_PLUGIN_IP6_CONFIG_NEVER_DEFAULT); -- cgit v1.2.1 From 39709fdc2ecaf42e431410f9ad425a9fb5c57812 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 16 Sep 2014 16:38:04 -0400 Subject: libnm-core: add NMIPAddress/NMIPRoute attributes, use for labels Add key-value attributes to NMIPAddress and NMIPRoute, and use them to store IPv4 address labels. Demote NMSettingIP4Config:address-labels to a D-Bus-only property, and arrange for :addresses setter to read the labels out of that property when creating the addresses. --- libnm-core/nm-core-internal.h | 6 - libnm-core/nm-setting-ip-config.c | 186 ++++++++++++++++++++- libnm-core/nm-setting-ip-config.h | 15 ++ libnm-core/nm-setting-ip4-config.c | 177 ++++++++++---------- libnm-core/tests/test-general.c | 137 +++++++-------- libnm/libnm.ver | 6 + src/nm-ip4-config.c | 12 +- src/settings/plugins/ifcfg-rh/reader.c | 3 +- .../plugins/ifcfg-rh/tests/test-ifcfg-rh.c | 22 ++- src/settings/plugins/ifcfg-rh/writer.c | 18 +- 10 files changed, 397 insertions(+), 185 deletions(-) diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index ff44eb043f..407d189e3c 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -74,12 +74,6 @@ g_clear_object (&c); \ } -const char *_nm_setting_ip4_config_get_address_label (NMSettingIP4Config *setting, - guint32 i); -gboolean _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, - NMIPAddress *address, - const char *label); - /* NM_SETTING_COMPARE_FLAG_INFERRABLE: check whether a device-generated * connection can be replaced by a already-defined connection. This flag only * takes into account properties marked with the %NM_SETTING_PARAM_INFERRABLE diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c index 12080bd05f..42857dca00 100644 --- a/libnm-core/nm-setting-ip-config.c +++ b/libnm-core/nm-setting-ip-config.c @@ -89,6 +89,8 @@ struct NMIPAddress { char *address, *gateway; int prefix, family; + + GHashTable *attributes; }; /** @@ -202,6 +204,8 @@ nm_ip_address_unref (NMIPAddress *address) if (address->refcount == 0) { g_free (address->address); g_free (address->gateway); + if (address->attributes) + g_hash_table_unref (address->attributes); g_slice_free (NMIPAddress, address); } } @@ -211,7 +215,8 @@ nm_ip_address_unref (NMIPAddress *address) * @address: the #NMIPAddress * @other: the #NMIPAddress to compare @address to. * - * Determines if two #NMIPAddress objects contain the same values. + * Determines if two #NMIPAddress objects contain the same address, prefix, and + * gateway (attributes are not compared). * * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. **/ @@ -251,6 +256,16 @@ nm_ip_address_dup (NMIPAddress *address) copy = nm_ip_address_new (address->family, address->address, address->prefix, address->gateway, NULL); + if (address->attributes) { + GHashTableIter iter; + const char *key; + GVariant *value; + + g_hash_table_iter_init (&iter, address->attributes); + while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) + nm_ip_address_set_attribute (copy, key, value); + } + return copy; } @@ -427,6 +442,82 @@ nm_ip_address_set_gateway (NMIPAddress *address, address->gateway = canonicalize_ip (address->family, gateway, TRUE); } +/** + * nm_ip_address_get_attribute_names: + * @address: the #NMIPAddress + * + * Gets an array of attribute names defined on @address. + * + * Returns: (transfer full): a %NULL-terminated array of attribute names, + **/ +char ** +nm_ip_address_get_attribute_names (NMIPAddress *address) +{ + GHashTableIter iter; + const char *key; + GPtrArray *names; + + g_return_val_if_fail (address != NULL, NULL); + + names = g_ptr_array_new (); + + if (address->attributes) { + g_hash_table_iter_init (&iter, address->attributes); + while (g_hash_table_iter_next (&iter, (gpointer *) &key, NULL)) + g_ptr_array_add (names, g_strdup (key)); + } + g_ptr_array_add (names, NULL); + + return (char **) g_ptr_array_free (names, FALSE); +} + +/** + * nm_ip_address_get_attribute: + * @address: the #NMIPAddress + * @name: the name of an address attribute + * + * Gets the value of the attribute with name @name on @address + * + * Returns: (transfer none): the value of the attribute with name @name on + * @address, or %NULL if @address has no such attribute. + **/ +GVariant * +nm_ip_address_get_attribute (NMIPAddress *address, const char *name) +{ + g_return_val_if_fail (address != NULL, NULL); + g_return_val_if_fail (name != NULL && *name != '\0', NULL); + + if (address->attributes) + return g_hash_table_lookup (address->attributes, name); + else + return NULL; +} + +/** + * nm_ip_address_set_attribute: + * @address: the #NMIPAddress + * @name: the name of an address attribute + * @value: (transfer none) (allow-none): the value + * + * Sets or clears the named attribute on @address to the given value. + **/ +void +nm_ip_address_set_attribute (NMIPAddress *address, const char *name, GVariant *value) +{ + g_return_if_fail (address != NULL); + g_return_if_fail (name != NULL && *name != '\0'); + + if (!address->attributes) { + address->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, (GDestroyNotify) g_variant_unref); + } + + if (value) + g_hash_table_insert (address->attributes, g_strdup (name), g_variant_ref_sink (value)); + else + g_hash_table_remove (address->attributes, name); +} + G_DEFINE_BOXED_TYPE (NMIPRoute, nm_ip_route, nm_ip_route_dup, nm_ip_route_unref) @@ -438,6 +529,8 @@ struct NMIPRoute { guint prefix; char *next_hop; guint32 metric; + + GHashTable *attributes; }; /** @@ -559,6 +652,8 @@ nm_ip_route_unref (NMIPRoute *route) if (route->refcount == 0) { g_free (route->dest); g_free (route->next_hop); + if (route->attributes) + g_hash_table_unref (route->attributes); g_slice_free (NMIPRoute, route); } } @@ -568,7 +663,8 @@ nm_ip_route_unref (NMIPRoute *route) * @route: the #NMIPRoute * @other: the #NMIPRoute to compare @route to. * - * Determines if two #NMIPRoute objects contain the same values. + * Determines if two #NMIPRoute objects contain the same destination, prefix, + * next hop, and metric. (Attributes are not compared.) * * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. **/ @@ -609,6 +705,16 @@ nm_ip_route_dup (NMIPRoute *route) route->dest, route->prefix, route->next_hop, route->metric, NULL); + if (route->attributes) { + GHashTableIter iter; + const char *key; + GVariant *value; + + g_hash_table_iter_init (&iter, route->attributes); + while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) + nm_ip_route_set_attribute (copy, key, value); + } + return copy; } @@ -872,3 +978,79 @@ nm_ip_route_set_metric (NMIPRoute *route, route->metric = metric; } + +/** + * nm_ip_route_get_attribute_names: + * @route: the #NMIPRoute + * + * Gets an array of attribute names defined on @route. + * + * Returns: (transfer full): a %NULL-terminated array of attribute names + **/ +char ** +nm_ip_route_get_attribute_names (NMIPRoute *route) +{ + GHashTableIter iter; + const char *key; + GPtrArray *names; + + g_return_val_if_fail (route != NULL, NULL); + + names = g_ptr_array_new (); + + if (route->attributes) { + g_hash_table_iter_init (&iter, route->attributes); + while (g_hash_table_iter_next (&iter, (gpointer *) &key, NULL)) + g_ptr_array_add (names, g_strdup (key)); + } + g_ptr_array_add (names, NULL); + + return (char **) g_ptr_array_free (names, FALSE); +} + +/** + * nm_ip_route_get_attribute: + * @route: the #NMIPRoute + * @name: the name of an route attribute + * + * Gets the value of the attribute with name @name on @route + * + * Returns: (transfer none): the value of the attribute with name @name on + * @route, or %NULL if @route has no such attribute. + **/ +GVariant * +nm_ip_route_get_attribute (NMIPRoute *route, const char *name) +{ + g_return_val_if_fail (route != NULL, NULL); + g_return_val_if_fail (name != NULL && *name != '\0', NULL); + + if (route->attributes) + return g_hash_table_lookup (route->attributes, name); + else + return NULL; +} + +/** + * nm_ip_route_set_attribute: + * @route: the #NMIPRoute + * @name: the name of a route attribute + * @value: (transfer none) (allow-none): the value + * + * Sets the named attribute on @route to the given value. + **/ +void +nm_ip_route_set_attribute (NMIPRoute *route, const char *name, GVariant *value) +{ + g_return_if_fail (route != NULL); + g_return_if_fail (name != NULL && *name != '\0'); + + if (!route->attributes) { + route->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, (GDestroyNotify) g_variant_unref); + } + + if (value) + g_hash_table_insert (route->attributes, g_strdup (name), g_variant_ref_sink (value)); + else + g_hash_table_remove (route->attributes, name); +} diff --git a/libnm-core/nm-setting-ip-config.h b/libnm-core/nm-setting-ip-config.h index d282935083..24c1cbda42 100644 --- a/libnm-core/nm-setting-ip-config.h +++ b/libnm-core/nm-setting-ip-config.h @@ -67,6 +67,14 @@ const char *nm_ip_address_get_gateway (NMIPAddress *address); void nm_ip_address_set_gateway (NMIPAddress *address, const char *gateway); +char **nm_ip_address_get_attribute_names (NMIPAddress *address); +GVariant *nm_ip_address_get_attribute (NMIPAddress *address, + const char *name); +void nm_ip_address_set_attribute (NMIPAddress *address, + const char *name, + GVariant *value); + + typedef struct NMIPRoute NMIPRoute; GType nm_ip_route_get_type (void); @@ -112,6 +120,13 @@ guint32 nm_ip_route_get_metric (NMIPRoute *route); void nm_ip_route_set_metric (NMIPRoute *route, guint32 metric); +char **nm_ip_route_get_attribute_names (NMIPRoute *route); +GVariant *nm_ip_route_get_attribute (NMIPRoute *route, + const char *name); +void nm_ip_route_set_attribute (NMIPRoute *route, + const char *name, + GVariant *value); + G_END_DECLS #endif /* NM_SETTING_IP_CONFIG_H */ diff --git a/libnm-core/nm-setting-ip4-config.c b/libnm-core/nm-setting-ip4-config.c index 2cc00093f9..e15dae0491 100644 --- a/libnm-core/nm-setting-ip4-config.c +++ b/libnm-core/nm-setting-ip4-config.c @@ -50,7 +50,6 @@ typedef struct { GSList *dns; /* list of IP address strings */ GSList *dns_search; /* list of strings */ GSList *addresses; /* array of NMIPAddress */ - GSList *address_labels; /* list of strings */ GSList *routes; /* array of NMIPRoute */ gboolean ignore_auto_routes; gboolean ignore_auto_dns; @@ -67,7 +66,6 @@ enum { PROP_DNS, PROP_DNS_SEARCH, PROP_ADDRESSES, - PROP_ADDRESS_LABELS, PROP_ROUTES, PROP_IGNORE_AUTO_ROUTES, PROP_IGNORE_AUTO_DNS, @@ -434,19 +432,6 @@ nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i) return (NMIPAddress *) g_slist_nth_data (priv->addresses, i); } -const char * -_nm_setting_ip4_config_get_address_label (NMSettingIP4Config *setting, guint32 i) -{ - NMSettingIP4ConfigPrivate *priv; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i < g_slist_length (priv->address_labels), NULL); - - return (const char *) g_slist_nth_data (priv->address_labels, i); -} - /** * nm_setting_ip4_config_add_address: * @setting: the #NMSettingIP4Config @@ -461,14 +446,6 @@ _nm_setting_ip4_config_get_address_label (NMSettingIP4Config *setting, guint32 i gboolean nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, NMIPAddress *address) -{ - return _nm_setting_ip4_config_add_address_with_label (setting, address, ""); -} - -gboolean -_nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, - NMIPAddress *address, - const char *label) { NMSettingIP4ConfigPrivate *priv; NMIPAddress *copy; @@ -476,7 +453,6 @@ _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); g_return_val_if_fail (address != NULL, FALSE); - g_return_val_if_fail (label != NULL, FALSE); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { @@ -486,7 +462,6 @@ _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *setting, copy = nm_ip_address_dup (address); priv->addresses = g_slist_append (priv->addresses, copy); - priv->address_labels = g_slist_append (priv->address_labels, g_strdup (label)); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES); return TRUE; @@ -503,19 +478,16 @@ void nm_setting_ip4_config_remove_address (NMSettingIP4Config *setting, guint32 i) { NMSettingIP4ConfigPrivate *priv; - GSList *addr, *label; + GSList *addr; g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); addr = g_slist_nth (priv->addresses, i); - label = g_slist_nth (priv->address_labels, i); - g_return_if_fail (addr != NULL && label != NULL); + g_return_if_fail (addr != NULL); nm_ip_address_unref ((NMIPAddress *) addr->data); priv->addresses = g_slist_delete_link (priv->addresses, addr); - g_free (label->data); - priv->address_labels = g_slist_delete_link (priv->address_labels, label); g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES); } @@ -566,8 +538,6 @@ nm_setting_ip4_config_clear_addresses (NMSettingIP4Config *setting) g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); priv->addresses = NULL; - g_slist_free_full (priv->address_labels, g_free); - priv->address_labels = NULL; g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES); } @@ -843,9 +813,6 @@ verify_label (const char *label) const char *p; char *iface; - if (!*label) - return TRUE; - p = strchr (label, ':'); if (!p) return FALSE; @@ -955,31 +922,33 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) } /* Validate address labels */ - for (iter = priv->address_labels, i = 0; iter; iter = g_slist_next (iter), i++) { - const char *label = (const char *) iter->data; - - if (!verify_label (label)) { + for (iter = priv->addresses, i = 0; iter; iter = g_slist_next (iter), i++) { + NMIPAddress *addr = (NMIPAddress *) iter->data; + GVariant *label; + + label = nm_ip_address_get_attribute (addr, "label"); + if (!label) + continue; + if (!g_variant_is_of_type (label, G_VARIANT_TYPE_STRING)) { + g_set_error (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("%d. IPv4 address has 'label' property with invalid type"), + i+1); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES); + return FALSE; + } + if (!verify_label (g_variant_get_string (label, NULL))) { g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, _("%d. IPv4 address has invalid label '%s'"), - i+1, label); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, "address-labels"); + i+1, g_variant_get_string (label, NULL)); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES); return FALSE; } } - if (g_slist_length (priv->addresses) != g_slist_length (priv->address_labels)) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("IPv4 address / label count mismatch (%d vs %d)"), - g_slist_length (priv->addresses), - g_slist_length (priv->address_labels)); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, "address-labels"); - return FALSE; - } - /* Validate DNS */ for (iter = priv->dns, i = 0; iter; iter = g_slist_next (iter), i++) { const char *dns = (const char *) iter->data; @@ -1018,7 +987,6 @@ finalize (GObject *object) g_slist_free_full (priv->dns, g_free); g_slist_free_full (priv->dns_search, g_free); g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); - g_slist_free_full (priv->address_labels, g_free); g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); G_OBJECT_CLASS (nm_setting_ip4_config_parent_class)->finalize (object); @@ -1038,16 +1006,67 @@ ip4_dns_from_dbus (GVariant *dbus_value, } static GVariant * -ip4_addresses_to_dbus (const GValue *prop_value) +ip4_addresses_get (NMSetting *setting, + const char *property) { - return nm_utils_ip4_addresses_to_variant (g_value_get_boxed (prop_value)); + GPtrArray *addrs; + GVariant *ret; + + g_object_get (setting, property, &addrs, NULL); + ret = nm_utils_ip4_addresses_to_variant (addrs); + g_ptr_array_unref (addrs); + + return ret; } static void -ip4_addresses_from_dbus (GVariant *dbus_value, - GValue *prop_value) +ip4_addresses_set (NMSetting *setting, + GVariant *connection_dict, + const char *property, + GVariant *value) { - g_value_take_boxed (prop_value, nm_utils_ip4_addresses_from_variant (dbus_value)); + GPtrArray *addrs; + GVariant *s_ip4; + char **labels; + int i; + + addrs = nm_utils_ip4_addresses_from_variant (value); + + s_ip4 = g_variant_lookup_value (connection_dict, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); + if (g_variant_lookup (s_ip4, "address-labels", "^as", &labels)) { + for (i = 0; i < addrs->len && labels[i]; i++) + if (*labels[i]) + nm_ip_address_set_attribute (addrs->pdata[i], "label", g_variant_new_string (labels[i])); + g_strfreev (labels); + } + g_variant_unref (s_ip4); + + g_object_set (setting, property, addrs, NULL); + g_ptr_array_unref (addrs); +} + +static GVariant * +ip4_address_labels_get (NMSetting *setting, + NMConnection *connection, + const char *property) +{ + NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); + GPtrArray *labels; + GSList *iter; + GVariant *ret; + + labels = g_ptr_array_new (); + for (iter = priv->addresses; iter; iter = iter->next) { + NMIPAddress *addr = iter->data; + GVariant *label = nm_ip_address_get_attribute (addr, "label"); + + g_ptr_array_add (labels, (char *) (label ? g_variant_get_string (label, NULL) : "")); + } + + ret = g_variant_new_strv ((const char * const *) labels->pdata, labels->len); + g_ptr_array_unref (labels); + + return ret; } static GVariant * @@ -1069,7 +1088,6 @@ set_property (GObject *object, guint prop_id, { NMSettingIP4Config *setting = NM_SETTING_IP4_CONFIG (object); NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - GSList *iter; switch (prop_id) { case PROP_METHOD: @@ -1089,16 +1107,6 @@ set_property (GObject *object, guint prop_id, priv->addresses = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), (NMUtilsCopyFunc) nm_ip_address_dup); - if (g_slist_length (priv->addresses) != g_slist_length (priv->address_labels)) { - g_slist_free_full (priv->address_labels, g_free); - priv->address_labels = NULL; - for (iter = priv->addresses; iter; iter = iter->next) - priv->address_labels = g_slist_prepend (priv->address_labels, g_strdup ("")); - } - break; - case PROP_ADDRESS_LABELS: - g_slist_free_full (priv->address_labels, g_free); - priv->address_labels = _nm_utils_strv_to_slist (g_value_get_boxed (value)); break; case PROP_ROUTES: g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); @@ -1154,9 +1162,6 @@ get_property (GObject *object, guint prop_id, case PROP_ADDRESSES: g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip_address_dup, (GDestroyNotify) nm_ip_address_unref)); break; - case PROP_ADDRESS_LABELS: - g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->address_labels)); - break; case PROP_ROUTES: g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip_route_dup, (GDestroyNotify) nm_ip_route_unref)); break; @@ -1281,23 +1286,17 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) G_PARAM_READWRITE | NM_SETTING_PARAM_INFERRABLE | G_PARAM_STATIC_STRINGS)); - _nm_setting_class_transform_property (parent_class, NM_SETTING_IP4_CONFIG_ADDRESSES, - G_VARIANT_TYPE ("aau"), - ip4_addresses_to_dbus, - ip4_addresses_from_dbus); - - /** - * NMSettingIP4Config:address-labels: - * - * Internal use only. - **/ - g_object_class_install_property - (object_class, PROP_ADDRESS_LABELS, - g_param_spec_boxed ("address-labels", "", "", - G_TYPE_STRV, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); + _nm_setting_class_override_property (parent_class, NM_SETTING_IP4_CONFIG_ADDRESSES, + G_VARIANT_TYPE ("aau"), + ip4_addresses_get, + ip4_addresses_set, + NULL); + + _nm_setting_class_add_dbus_only_property (parent_class, + "address-labels", + G_VARIANT_TYPE_STRING_ARRAY, + ip4_address_labels_get, + NULL); /** * NMSettingIP4Config:routes: diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 41189481f2..b103ce8922 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -325,9 +325,11 @@ test_setting_ip4_config_labels (void) { NMSettingIP4Config *s_ip4; NMIPAddress *addr; - const char *label; + GVariant *label; GPtrArray *addrs; char **labels; + NMConnection *conn; + GVariant *dict, *setting_dict, *value; GError *error = NULL; s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); @@ -344,32 +346,38 @@ test_setting_ip4_config_labels (void) nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); - label = _nm_setting_ip4_config_get_address_label (s_ip4, 0); - g_assert_cmpstr (label, ==, ""); + addr = nm_setting_ip4_config_get_address (s_ip4, 0); + label = nm_ip_address_get_attribute (addr, "label"); + g_assert (label == NULL); /* addr 2 */ addr = nm_ip_address_new (AF_INET, "2.2.2.2", 24, NULL, &error); g_assert_no_error (error); + nm_ip_address_set_attribute (addr, "label", g_variant_new_string ("eth0:1")); - _nm_setting_ip4_config_add_address_with_label (s_ip4, addr, "eth0:1"); + nm_setting_ip4_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); - label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); - g_assert_cmpstr (label, ==, "eth0:1"); + addr = nm_setting_ip4_config_get_address (s_ip4, 1); + label = nm_ip_address_get_attribute (addr, "label"); + g_assert (label != NULL); + g_assert_cmpstr (g_variant_get_string (label, NULL), ==, "eth0:1"); /* addr 3 */ addr = nm_ip_address_new (AF_INET, "3.3.3.3", 24, NULL, &error); g_assert_no_error (error); + nm_ip_address_set_attribute (addr, "label", NULL); - _nm_setting_ip4_config_add_address_with_label (s_ip4, addr, ""); + nm_setting_ip4_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); - label = _nm_setting_ip4_config_get_address_label (s_ip4, 2); - g_assert_cmpstr (label, ==, ""); + addr = nm_setting_ip4_config_get_address (s_ip4, 2); + label = nm_ip_address_get_attribute (addr, "label"); + g_assert (label == NULL); /* Remove addr 1 and re-verify remaining addresses */ nm_setting_ip4_config_remove_address (s_ip4, 0); @@ -378,95 +386,81 @@ test_setting_ip4_config_labels (void) addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); - label = _nm_setting_ip4_config_get_address_label (s_ip4, 0); - g_assert_cmpstr (label, ==, "eth0:1"); + label = nm_ip_address_get_attribute (addr, "label"); + g_assert (label != NULL); + g_assert_cmpstr (g_variant_get_string (label, NULL), ==, "eth0:1"); addr = nm_setting_ip4_config_get_address (s_ip4, 1); g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); - label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); - g_assert_cmpstr (label, ==, ""); + label = nm_ip_address_get_attribute (addr, "label"); + g_assert (label == NULL); + /* The labels should appear in the D-Bus serialization */ + conn = nmtst_create_minimal_connection ("label test", NULL, NM_SETTING_WIRED_SETTING_NAME, NULL); + nm_connection_add_setting (conn, NM_SETTING (s_ip4)); + dict = nm_connection_to_dbus (conn, NM_CONNECTION_SERIALIZE_ALL); + g_object_unref (conn); - /* Test explicit property assignment */ - g_object_get (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_ADDRESSES, &addrs, - "address-labels", &labels, - NULL); + setting_dict = g_variant_lookup_value (dict, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); + g_assert (setting_dict != NULL); + value = g_variant_lookup_value (setting_dict, "address-labels", G_VARIANT_TYPE_STRING_ARRAY); + g_assert (value != NULL); - nm_setting_ip4_config_clear_addresses (s_ip4); - g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 0); + g_variant_get (value, "^as", &labels); + g_assert_cmpint (g_strv_length (labels), ==, 2); + g_assert_cmpstr (labels[0], ==, "eth0:1"); + g_assert_cmpstr (labels[1], ==, ""); - /* Setting addrs but not labels will result in empty labels */ - g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_ADDRESSES, addrs, - NULL); - g_ptr_array_unref (addrs); - nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); + g_variant_unref (setting_dict); + g_variant_unref (value); + g_strfreev (labels); + + /* And should be deserialized */ + conn = nm_simple_connection_new_from_dbus (dict, &error); g_assert_no_error (error); - g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); + g_variant_unref (dict); + + s_ip4 = nm_connection_get_setting_ip4_config (conn); addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); - label = _nm_setting_ip4_config_get_address_label (s_ip4, 0); - g_assert_cmpstr (label, ==, ""); + label = nm_ip_address_get_attribute (addr, "label"); + g_assert (label != NULL); + g_assert_cmpstr (g_variant_get_string (label, NULL), ==, "eth0:1"); addr = nm_setting_ip4_config_get_address (s_ip4, 1); g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); - label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); - g_assert_cmpstr (label, ==, ""); + label = nm_ip_address_get_attribute (addr, "label"); + g_assert (label == NULL); + + /* Test explicit property assignment */ + g_object_get (G_OBJECT (s_ip4), + NM_SETTING_IP4_CONFIG_ADDRESSES, &addrs, + NULL); + + nm_setting_ip4_config_clear_addresses (s_ip4); + g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 0); - /* Setting labels now will leave addresses untouched */ g_object_set (G_OBJECT (s_ip4), - "address-labels", labels, + NM_SETTING_IP4_CONFIG_ADDRESSES, addrs, NULL); - g_strfreev (labels); + g_ptr_array_unref (addrs); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); addr = nm_setting_ip4_config_get_address (s_ip4, 0); g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); - label = _nm_setting_ip4_config_get_address_label (s_ip4, 0); - g_assert_cmpstr (label, ==, "eth0:1"); + label = nm_ip_address_get_attribute (addr, "label"); + g_assert (label != NULL); + g_assert_cmpstr (g_variant_get_string (label, NULL), ==, "eth0:1"); addr = nm_setting_ip4_config_get_address (s_ip4, 1); g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); - label = _nm_setting_ip4_config_get_address_label (s_ip4, 1); - g_assert_cmpstr (label, ==, ""); - - /* Setting labels to a value that's too short or too long will result in - * the setting not verifying. - */ - labels = g_strsplit ("eth0:2", ",", -1); - g_object_set (G_OBJECT (s_ip4), - "address-labels", labels, - NULL); - g_strfreev (labels); - - nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); - g_assert_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY); - g_assert (g_str_has_prefix (error->message, "ipv4.address-labels:")); - g_clear_error (&error); - - labels = g_strsplit ("eth0:2,eth0:3", ",", -1); - g_object_set (G_OBJECT (s_ip4), - "address-labels", labels, - NULL); - g_strfreev (labels); - nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); - g_assert_no_error (error); - - labels = g_strsplit ("eth0:2,eth0:3,eth0:4", ",", -1); - g_object_set (G_OBJECT (s_ip4), - "address-labels", labels, - NULL); - g_strfreev (labels); - nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); - g_assert_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY); - g_assert (g_str_has_prefix (error->message, "ipv4.address-labels:")); - g_clear_error (&error); + label = nm_ip_address_get_attribute (addr, "label"); + g_assert (label == NULL); - g_object_unref (s_ip4); + g_object_unref (conn); } static void @@ -1640,7 +1634,6 @@ test_connection_diff_a_only (void) { NM_SETTING_IP4_CONFIG_DNS, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP4_CONFIG_DNS_SEARCH, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP4_CONFIG_ADDRESSES, NM_SETTING_DIFF_RESULT_IN_A }, - { "address-labels", NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP4_CONFIG_ROUTES, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, NM_SETTING_DIFF_RESULT_IN_A }, diff --git a/libnm/libnm.ver b/libnm/libnm.ver index 106beaa08f..b40c232591 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -286,6 +286,8 @@ global: nm_ip_address_equal; nm_ip_address_get_address; nm_ip_address_get_address_binary; + nm_ip_address_get_attribute; + nm_ip_address_get_attribute_names; nm_ip_address_get_family; nm_ip_address_get_gateway; nm_ip_address_get_prefix; @@ -295,10 +297,13 @@ global: nm_ip_address_ref; nm_ip_address_set_address; nm_ip_address_set_address_binary; + nm_ip_address_set_attribute; nm_ip_address_set_gateway; nm_ip_address_set_prefix; nm_ip_address_unref; nm_ip_route_equal; + nm_ip_route_get_attribute; + nm_ip_route_get_attribute_names; nm_ip_route_get_dest; nm_ip_route_get_dest_binary; nm_ip_route_get_family; @@ -310,6 +315,7 @@ global: nm_ip_route_new; nm_ip_route_new_binary; nm_ip_route_ref; + nm_ip_route_set_attribute; nm_ip_route_set_dest; nm_ip_route_set_dest_binary; nm_ip_route_set_metric; diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 46b8fb1983..cebfe3bbe9 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -330,7 +330,7 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i /* Addresses */ for (i = 0; i < naddresses; i++) { NMIPAddress *s_addr = nm_setting_ip4_config_get_address (setting, i); - const char *label = _nm_setting_ip4_config_get_address_label (setting, i); + GVariant *label; NMPlatformIP4Address address; memset (&address, 0, sizeof (address)); @@ -339,7 +339,10 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i address.lifetime = NM_PLATFORM_LIFETIME_PERMANENT; address.preferred = NM_PLATFORM_LIFETIME_PERMANENT; address.source = NM_IP_CONFIG_SOURCE_USER; - g_strlcpy (address.label, label, sizeof (address.label)); + + label = nm_ip_address_get_attribute (s_addr, "label"); + if (label) + g_strlcpy (address.label, g_variant_get_string (label, NULL), sizeof (address.label)); nm_ip4_config_add_address (config, &address); } @@ -427,7 +430,10 @@ nm_ip4_config_create_setting (const NMIP4Config *config) if (same_prefix (address->address, gateway, address->plen)) nm_ip_address_set_gateway (s_addr, nm_utils_inet4_ntop (gateway, NULL)); - _nm_setting_ip4_config_add_address_with_label (s_ip4, s_addr, address->label); + if (*address->label) + nm_ip_address_set_attribute (s_addr, "label", g_variant_new_string (address->label)); + + nm_setting_ip4_config_add_address (s_ip4, s_addr); nm_ip_address_unref (s_addr); } diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index 4dc64b8cfd..62f52d41c0 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -1235,7 +1235,8 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo ok = read_full_ip4_address (parsed, network_file, -1, base_addr, &addr, &err); svCloseFile (parsed); if (ok) { - if (!_nm_setting_ip4_config_add_address_with_label (s_ip4, addr, device)) + nm_ip_address_set_attribute (addr, "label", g_variant_new_string (device)); + if (!nm_setting_ip4_config_add_address (s_ip4, addr)) PARSE_WARNING ("duplicate IP4 address in alias file %s", item); } else { PARSE_WARNING ("error reading IP4 address from alias file '%s': %s", diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index 0162cb130e..cafaf88be9 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -2376,7 +2376,7 @@ test_read_wired_aliases_good (void) const char *expected_id = "System aliasem0"; int expected_num_addresses = 4; const char *expected_address[4] = { "192.168.1.5", "192.168.1.6", "192.168.1.9", "192.168.1.99" }; - const char *expected_label[4] = { "", "aliasem0:1", "aliasem0:2", "aliasem0:99" }; + const char *expected_label[4] = { NULL, "aliasem0:1", "aliasem0:2", "aliasem0:99" }; const char *expected_gateway[4] = { "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1" }; int i, j; @@ -2442,6 +2442,7 @@ test_read_wired_aliases_good (void) for (i = 0; i < expected_num_addresses; i++) { NMIPAddress *ip4_addr; const char *addr; + GVariant *label; ip4_addr = nm_setting_ip4_config_get_address (s_ip4, i); g_assert (ip4_addr != NULL); @@ -2457,7 +2458,11 @@ test_read_wired_aliases_good (void) g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, expected_gateway[j]); - g_assert_cmpstr (_nm_setting_ip4_config_get_address_label (s_ip4, i), ==, expected_label[j]); + label = nm_ip_address_get_attribute (ip4_addr, "label"); + if (expected_label[j]) + g_assert_cmpstr (g_variant_get_string (label, NULL), ==, expected_label[j]); + else + g_assert (label == NULL); expected_address[j] = NULL; expected_gateway[j] = NULL; @@ -2557,7 +2562,7 @@ test_read_wired_aliases_bad (const char *base, const char *expected_id) g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.1.1"); - g_assert_cmpstr (_nm_setting_ip4_config_get_address_label (s_ip4, 0), ==, ""); + g_assert (nm_ip_address_get_attribute (ip4_addr, "label") == NULL); g_free (keyfile); g_free (routefile); @@ -7557,7 +7562,7 @@ test_write_wired_aliases (void) char *uuid; int num_addresses = 4; const char *ip[] = { "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" }; - const char *label[] = { "", "alias0:2", "", "alias0:3" }; + const char *label[] = { NULL, "alias0:2", NULL, "alias0:3" }; NMIPAddress *addr; gboolean success; GError *error = NULL; @@ -7611,7 +7616,9 @@ test_write_wired_aliases (void) for (i = 0; i < num_addresses; i++) { addr = nm_ip_address_new (AF_INET, ip[i], 24, "1.1.1.1", &error); g_assert_no_error (error); - _nm_setting_ip4_config_add_address_with_label (s_ip4, addr, label[i]); + if (label[i]) + nm_ip_address_set_attribute (addr, "label", g_variant_new_string (label[i])); + nm_setting_ip4_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); } @@ -7703,7 +7710,10 @@ test_write_wired_aliases (void) g_assert_cmpint (nm_ip_address_get_prefix (addr), ==, 24); g_assert_cmpstr (nm_ip_address_get_gateway (addr), ==, "1.1.1.1"); - g_assert_cmpstr (_nm_setting_ip4_config_get_address_label (s_ip4, i), ==, label[j]); + if (label[j]) + g_assert_cmpstr (g_variant_get_string (nm_ip_address_get_attribute (addr, "label"), NULL), ==, label[j]); + else + g_assert (nm_ip_address_get_attribute (addr, "label") == NULL); ip[j] = NULL; } diff --git a/src/settings/plugins/ifcfg-rh/writer.c b/src/settings/plugins/ifcfg-rh/writer.c index 160f070cdc..d22b30cdc6 100644 --- a/src/settings/plugins/ifcfg-rh/writer.c +++ b/src/settings/plugins/ifcfg-rh/writer.c @@ -1881,11 +1881,13 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) for (i = n = 0; i < num; i++) { NMIPAddress *addr; + addr = nm_setting_ip4_config_get_address (s_ip4, i); + if (i > 0) { - const char *label; + GVariant *label; - label = _nm_setting_ip4_config_get_address_label (s_ip4, i); - if (*label) + label = nm_ip_address_get_attribute (addr, "label"); + if (label) continue; } @@ -1906,8 +1908,6 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) gw_key = g_strdup_printf ("GATEWAY%d", n); } - addr = nm_setting_ip4_config_get_address (s_ip4, i); - svSetValue (ifcfg, addr_key, nm_ip_address_get_address (addr), FALSE); tmp = g_strdup_printf ("%u", nm_ip_address_get_prefix (addr)); @@ -2136,12 +2136,18 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) num = nm_setting_ip4_config_get_num_addresses (s_ip4); for (i = 0; i < num; i++) { + GVariant *label_var; const char *label, *p; char *path, *tmp; NMIPAddress *addr; shvarFile *ifcfg; - label = _nm_setting_ip4_config_get_address_label (s_ip4, i); + addr = nm_setting_ip4_config_get_address (s_ip4, i); + + label_var = nm_ip_address_get_attribute (addr, "label"); + if (!label_var) + continue; + label = g_variant_get_string (label_var, NULL); if ( strncmp (label, base_name, base_name_len) != 0 || label[base_name_len] != ':') continue; -- cgit v1.2.1 From 3f30c6f1c2f9ce76b24fada0e75fd3cf733ca4bc Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Sun, 19 Oct 2014 17:30:10 -0400 Subject: libnm-core: extract NMSettingIPConfig superclass out of IP4, IP6 classes Split a base NMSettingIPConfig class out of NMSettingIP4Config and NMSettingIP6Config, and update things accordingly. Further simplifications of now-redundant IPv4-vs-IPv6 code are possible, and should happen in the future. --- clients/cli/connections.c | 19 +- clients/cli/settings.c | 359 +++--- clients/cli/settings.h | 4 +- clients/tui/nmt-page-ip4.c | 28 +- clients/tui/nmt-page-ip6.c | 28 +- clients/tui/nmt-route-editor.c | 36 +- examples/C/glib/add-connection-gdbus.c | 2 +- examples/C/glib/add-connection-libnm.c | 2 +- libnm-core/nm-connection.c | 34 +- libnm-core/nm-connection.h | 4 +- libnm-core/nm-core-types.h | 1 + libnm-core/nm-setting-ip-config.c | 1176 ++++++++++++++++++ libnm-core/nm-setting-ip-config.h | 88 ++ libnm-core/nm-setting-ip4-config.c | 1278 ++------------------ libnm-core/nm-setting-ip4-config.h | 59 +- libnm-core/nm-setting-ip6-config.c | 1089 +---------------- libnm-core/nm-setting-ip6-config.h | 58 +- libnm-core/nm-utils.c | 1 + libnm-core/tests/test-general.c | 200 +-- libnm-core/tests/test-secrets.c | 4 +- libnm/libnm.ver | 93 +- po/POTFILES.in | 1 + src/NetworkManagerUtils.c | 23 +- src/devices/bluetooth/nm-bluez-device.c | 8 +- src/devices/nm-device-gre.c | 1 + src/devices/nm-device.c | 54 +- src/devices/wifi/nm-device-wifi.c | 16 +- src/devices/wwan/nm-modem-broadband.c | 1 + src/devices/wwan/nm-modem.c | 11 +- src/dhcp-manager/nm-dhcp-dhclient-utils.c | 1 + src/dhcp-manager/nm-dhcp-utils.c | 1 + src/dhcp-manager/tests/test-dhcp-dhclient.c | 1 + src/nm-ip4-config.c | 47 +- src/nm-ip4-config.h | 2 +- src/nm-ip6-config.c | 47 +- src/nm-ip6-config.h | 3 +- src/nm-policy.c | 16 +- src/settings/plugins/ibft/reader.c | 14 +- src/settings/plugins/ibft/tests/test-ibft.c | 28 +- src/settings/plugins/ifcfg-rh/reader.c | 82 +- .../plugins/ifcfg-rh/tests/test-ifcfg-rh.c | 1130 ++++++++--------- src/settings/plugins/ifcfg-rh/writer.c | 88 +- src/settings/plugins/ifnet/connection_parser.c | 113 +- src/settings/plugins/ifnet/net_utils.c | 12 +- src/settings/plugins/ifnet/net_utils.h | 4 +- src/settings/plugins/ifupdown/parser.c | 36 +- .../plugins/ifupdown/tests/test-ifupdown.c | 92 +- src/settings/plugins/keyfile/reader.c | 12 +- src/settings/plugins/keyfile/tests/test-keyfile.c | 413 +++---- src/settings/plugins/keyfile/writer.c | 12 +- .../tests/test-supplicant-config.c | 18 +- src/tests/test-general.c | 58 +- src/tests/test-resolvconf-capture.c | 1 + 53 files changed, 2995 insertions(+), 3914 deletions(-) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index d92b913cf8..c131054168 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -2889,7 +2889,7 @@ check_and_convert_vlan_prio_maps (const char *prio_map, static gboolean add_ip4_address_to_connection (NMIPAddress *ip4addr, NMConnection *connection) { - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; gboolean ret; if (!ip4addr) @@ -2897,13 +2897,13 @@ add_ip4_address_to_connection (NMIPAddress *ip4addr, NMConnection *connection) s_ip4 = nm_connection_get_setting_ip4_config (connection); if (!s_ip4) { - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); } - ret = nm_setting_ip4_config_add_address (s_ip4, ip4addr); + ret = nm_setting_ip_config_add_address (s_ip4, ip4addr); nm_ip_address_unref (ip4addr); return ret; @@ -2912,7 +2912,7 @@ add_ip4_address_to_connection (NMIPAddress *ip4addr, NMConnection *connection) static gboolean add_ip6_address_to_connection (NMIPAddress *ip6addr, NMConnection *connection) { - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; gboolean ret; if (!ip6addr) @@ -2920,13 +2920,13 @@ add_ip6_address_to_connection (NMIPAddress *ip6addr, NMConnection *connection) s_ip6 = nm_connection_get_setting_ip6_config (connection); if (!s_ip6) { - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, NULL); } - ret = nm_setting_ip6_config_add_address (s_ip6, ip6addr); + ret = nm_setting_ip_config_add_address (s_ip6, ip6addr); nm_ip_address_unref (ip6addr); return ret; @@ -8107,8 +8107,7 @@ editor_init_new_connection (NmCli *nmc, NMConnection *connection) static void editor_init_existing_connection (NMConnection *connection) { - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4, *s_ip6; NMSettingWireless *s_wireless; NMSettingConnection *s_con; diff --git a/clients/cli/settings.c b/clients/cli/settings.c index 19dc06d702..96932e5a16 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -20,6 +20,7 @@ #include "config.h" #include +#include #include #include @@ -251,64 +252,64 @@ NmcOutputField nmc_fields_setting_wireless_security[] = { /* Available fields for NM_SETTING_IP4_CONFIG_SETTING_NAME */ NmcOutputField nmc_fields_setting_ip4_config[] = { - SETTING_FIELD ("name", 8), /* 0 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_METHOD, 10), /* 1 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_DNS, 20), /* 2 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_DNS_SEARCH, 15), /* 3 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_ADDRESSES, 20), /* 4 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_ROUTES, 20), /* 5 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, 19), /* 6 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, 16), /* 7 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, 15), /* 8 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME, 19), /* 9 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, 14), /* 10 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, 15), /* 11 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_MAY_FAIL, 12), /* 12 */ + SETTING_FIELD ("name", 8), /* 0 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_METHOD, 10), /* 1 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DNS, 20), /* 2 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DNS_SEARCH, 15), /* 3 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_ADDRESSES, 20), /* 4 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_ROUTES, 20), /* 5 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, 19), /* 6 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, 16), /* 7 */ + SETTING_FIELD (NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, 15), /* 8 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, 19), /* 9 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, 14), /* 10 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_NEVER_DEFAULT, 15), /* 11 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_MAY_FAIL, 12), /* 12 */ {NULL, NULL, 0, NULL, FALSE, FALSE, 0} }; #define NMC_FIELDS_SETTING_IP4_CONFIG_ALL "name"","\ - NM_SETTING_IP4_CONFIG_METHOD","\ - NM_SETTING_IP4_CONFIG_DNS","\ - NM_SETTING_IP4_CONFIG_DNS_SEARCH","\ - NM_SETTING_IP4_CONFIG_ADDRESSES","\ - NM_SETTING_IP4_CONFIG_ROUTES","\ - NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES","\ - NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS","\ + NM_SETTING_IP_CONFIG_METHOD","\ + NM_SETTING_IP_CONFIG_DNS","\ + NM_SETTING_IP_CONFIG_DNS_SEARCH","\ + NM_SETTING_IP_CONFIG_ADDRESSES","\ + NM_SETTING_IP_CONFIG_ROUTES","\ + NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES","\ + NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS","\ NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID","\ - NM_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME","\ - NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME","\ - NM_SETTING_IP4_CONFIG_NEVER_DEFAULT","\ - NM_SETTING_IP4_CONFIG_MAY_FAIL + NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME","\ + NM_SETTING_IP_CONFIG_DHCP_HOSTNAME","\ + NM_SETTING_IP_CONFIG_NEVER_DEFAULT","\ + NM_SETTING_IP_CONFIG_MAY_FAIL #define NMC_FIELDS_SETTING_IP4_CONFIG_COMMON NMC_FIELDS_SETTING_IP4_CONFIG_ALL /* Available fields for NM_SETTING_IP6_CONFIG_SETTING_NAME */ NmcOutputField nmc_fields_setting_ip6_config[] = { - SETTING_FIELD ("name", 8), /* 0 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_METHOD, 10), /* 1 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_DNS, 20), /* 2 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_DNS_SEARCH, 15), /* 3 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_ADDRESSES, 20), /* 4 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_ROUTES, 20), /* 5 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES, 19), /* 6 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS, 16), /* 7 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_NEVER_DEFAULT, 15), /* 8 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_MAY_FAIL, 12), /* 9 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_IP6_PRIVACY, 15), /* 10 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_DHCP_HOSTNAME, 14), /* 11 */ + SETTING_FIELD ("name", 8), /* 0 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_METHOD, 10), /* 1 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DNS, 20), /* 2 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DNS_SEARCH, 15), /* 3 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_ADDRESSES, 20), /* 4 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_ROUTES, 20), /* 5 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, 19), /* 6 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, 16), /* 7 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_NEVER_DEFAULT, 15), /* 8 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_MAY_FAIL, 12), /* 9 */ + SETTING_FIELD (NM_SETTING_IP6_CONFIG_IP6_PRIVACY, 15), /* 10 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, 14), /* 11 */ {NULL, NULL, 0, NULL, FALSE, FALSE, 0} }; #define NMC_FIELDS_SETTING_IP6_CONFIG_ALL "name"","\ - NM_SETTING_IP6_CONFIG_METHOD","\ - NM_SETTING_IP6_CONFIG_DNS","\ - NM_SETTING_IP6_CONFIG_DNS_SEARCH","\ - NM_SETTING_IP6_CONFIG_ADDRESSES","\ - NM_SETTING_IP6_CONFIG_ROUTES","\ - NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES","\ - NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS","\ - NM_SETTING_IP6_CONFIG_NEVER_DEFAULT","\ - NM_SETTING_IP6_CONFIG_MAY_FAIL","\ + NM_SETTING_IP_CONFIG_METHOD","\ + NM_SETTING_IP_CONFIG_DNS","\ + NM_SETTING_IP_CONFIG_DNS_SEARCH","\ + NM_SETTING_IP_CONFIG_ADDRESSES","\ + NM_SETTING_IP_CONFIG_ROUTES","\ + NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES","\ + NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS","\ + NM_SETTING_IP_CONFIG_NEVER_DEFAULT","\ + NM_SETTING_IP_CONFIG_MAY_FAIL","\ NM_SETTING_IP6_CONFIG_IP6_PRIVACY","\ - NM_SETTING_IP6_CONFIG_DHCP_HOSTNAME + NM_SETTING_IP_CONFIG_DHCP_HOSTNAME #define NMC_FIELDS_SETTING_IP6_CONFIG_COMMON NMC_FIELDS_SETTING_IP4_CONFIG_ALL /* Available fields for NM_SETTING_SERIAL_SETTING_NAME */ @@ -1199,23 +1200,23 @@ nmc_property_ib_get_p_key (NMSetting *setting) DEFINE_GETTER (nmc_property_ib_get_parent, NM_SETTING_INFINIBAND_PARENT) /* --- NM_SETTING_IP4_CONFIG_SETTING_NAME property get functions --- */ -DEFINE_GETTER (nmc_property_ipv4_get_method, NM_SETTING_IP4_CONFIG_METHOD) -DEFINE_GETTER (nmc_property_ipv4_get_dns, NM_SETTING_IP4_CONFIG_DNS) -DEFINE_GETTER (nmc_property_ipv4_get_dns_search, NM_SETTING_IP4_CONFIG_DNS_SEARCH) +DEFINE_GETTER (nmc_property_ipv4_get_method, NM_SETTING_IP_CONFIG_METHOD) +DEFINE_GETTER (nmc_property_ipv4_get_dns, NM_SETTING_IP_CONFIG_DNS) +DEFINE_GETTER (nmc_property_ipv4_get_dns_search, NM_SETTING_IP_CONFIG_DNS_SEARCH) static char * nmc_property_ipv4_get_addresses (NMSetting *setting) { - NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting); + NMSettingIPConfig *s_ip4 = NM_SETTING_IP_CONFIG (setting); GString *printable; guint32 num_addresses, i; NMIPAddress *addr; printable = g_string_new (NULL); - num_addresses = nm_setting_ip4_config_get_num_addresses (s_ip4); + num_addresses = nm_setting_ip_config_get_num_addresses (s_ip4); for (i = 0; i < num_addresses; i++) { - addr = nm_setting_ip4_config_get_address (s_ip4, i); + addr = nm_setting_ip_config_get_address (s_ip4, i); if (printable->len > 0) g_string_append (printable, "; "); @@ -1240,16 +1241,16 @@ nmc_property_ipv4_get_addresses (NMSetting *setting) static char * nmc_property_ipv4_get_routes (NMSetting *setting) { - NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting); + NMSettingIPConfig *s_ip4 = NM_SETTING_IP_CONFIG (setting); GString *printable; guint32 num_routes, i; NMIPRoute *route; printable = g_string_new (NULL); - num_routes = nm_setting_ip4_config_get_num_routes (s_ip4); + num_routes = nm_setting_ip_config_get_num_routes (s_ip4); for (i = 0; i < num_routes; i++) { - route = nm_setting_ip4_config_get_route (s_ip4, i); + route = nm_setting_ip_config_get_route (s_ip4, i); if (printable->len > 0) g_string_append (printable, "; "); @@ -1274,32 +1275,32 @@ nmc_property_ipv4_get_routes (NMSetting *setting) return g_string_free (printable, FALSE); } -DEFINE_GETTER (nmc_property_ipv4_get_ignore_auto_routes, NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES) -DEFINE_GETTER (nmc_property_ipv4_get_ignore_auto_dns, NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS) +DEFINE_GETTER (nmc_property_ipv4_get_ignore_auto_routes, NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES) +DEFINE_GETTER (nmc_property_ipv4_get_ignore_auto_dns, NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS) DEFINE_GETTER (nmc_property_ipv4_get_dhcp_client_id, NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID) -DEFINE_GETTER (nmc_property_ipv4_get_dhcp_send_hostname, NM_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME) -DEFINE_GETTER (nmc_property_ipv4_get_dhcp_hostname, NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME) -DEFINE_GETTER (nmc_property_ipv4_get_never_default, NM_SETTING_IP4_CONFIG_NEVER_DEFAULT) -DEFINE_GETTER (nmc_property_ipv4_get_may_fail, NM_SETTING_IP4_CONFIG_MAY_FAIL) +DEFINE_GETTER (nmc_property_ipv4_get_dhcp_send_hostname, NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME) +DEFINE_GETTER (nmc_property_ipv4_get_dhcp_hostname, NM_SETTING_IP_CONFIG_DHCP_HOSTNAME) +DEFINE_GETTER (nmc_property_ipv4_get_never_default, NM_SETTING_IP_CONFIG_NEVER_DEFAULT) +DEFINE_GETTER (nmc_property_ipv4_get_may_fail, NM_SETTING_IP_CONFIG_MAY_FAIL) /* --- NM_SETTING_IP6_CONFIG_SETTING_NAME property get functions --- */ -DEFINE_GETTER (nmc_property_ipv6_get_method, NM_SETTING_IP6_CONFIG_METHOD) -DEFINE_GETTER (nmc_property_ipv6_get_dns, NM_SETTING_IP6_CONFIG_DNS) -DEFINE_GETTER (nmc_property_ipv6_get_dns_search, NM_SETTING_IP6_CONFIG_DNS_SEARCH) +DEFINE_GETTER (nmc_property_ipv6_get_method, NM_SETTING_IP_CONFIG_METHOD) +DEFINE_GETTER (nmc_property_ipv6_get_dns, NM_SETTING_IP_CONFIG_DNS) +DEFINE_GETTER (nmc_property_ipv6_get_dns_search, NM_SETTING_IP_CONFIG_DNS_SEARCH) static char * nmc_property_ipv6_get_addresses (NMSetting *setting) { - NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting); + NMSettingIPConfig *s_ip6 = NM_SETTING_IP_CONFIG (setting); GString *printable; guint32 num_addresses, i; NMIPAddress *addr; printable = g_string_new (NULL); - num_addresses = nm_setting_ip6_config_get_num_addresses (s_ip6); + num_addresses = nm_setting_ip_config_get_num_addresses (s_ip6); for (i = 0; i < num_addresses; i++) { - addr = nm_setting_ip6_config_get_address (s_ip6, i); + addr = nm_setting_ip_config_get_address (s_ip6, i); if (printable->len > 0) g_string_append (printable, "; "); @@ -1324,16 +1325,16 @@ nmc_property_ipv6_get_addresses (NMSetting *setting) static char * nmc_property_ipv6_get_routes (NMSetting *setting) { - NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting); + NMSettingIPConfig *s_ip6 = NM_SETTING_IP_CONFIG (setting); GString *printable; guint32 num_routes, i; NMIPRoute *route; printable = g_string_new (NULL); - num_routes = nm_setting_ip6_config_get_num_routes (s_ip6); + num_routes = nm_setting_ip_config_get_num_routes (s_ip6); for (i = 0; i < num_routes; i++) { - route = nm_setting_ip6_config_get_route (s_ip6, i); + route = nm_setting_ip_config_get_route (s_ip6, i); if (printable->len > 0) g_string_append (printable, "; "); @@ -1358,11 +1359,11 @@ nmc_property_ipv6_get_routes (NMSetting *setting) return g_string_free (printable, FALSE); } -DEFINE_GETTER (nmc_property_ipv6_get_ignore_auto_routes, NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES) -DEFINE_GETTER (nmc_property_ipv6_get_ignore_auto_dns, NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS) -DEFINE_GETTER (nmc_property_ipv6_get_never_default, NM_SETTING_IP6_CONFIG_NEVER_DEFAULT) -DEFINE_GETTER (nmc_property_ipv6_get_may_fail, NM_SETTING_IP6_CONFIG_MAY_FAIL) -DEFINE_GETTER (nmc_property_ipv6_get_dhcp_hostname, NM_SETTING_IP6_CONFIG_DHCP_HOSTNAME) +DEFINE_GETTER (nmc_property_ipv6_get_ignore_auto_routes, NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES) +DEFINE_GETTER (nmc_property_ipv6_get_ignore_auto_dns, NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS) +DEFINE_GETTER (nmc_property_ipv6_get_never_default, NM_SETTING_IP_CONFIG_NEVER_DEFAULT) +DEFINE_GETTER (nmc_property_ipv6_get_may_fail, NM_SETTING_IP_CONFIG_MAY_FAIL) +DEFINE_GETTER (nmc_property_ipv6_get_dhcp_hostname, NM_SETTING_IP_CONFIG_DHCP_HOSTNAME) static char * nmc_property_ipv6_get_ip6_privacy (NMSetting *setting) @@ -1696,19 +1697,19 @@ ipv4_addresses_changed_cb (GObject *object, GParamSpec *pspec, gpointer user_dat /* If we have some IP addresses set method to 'manual'. * Else if the method was 'manual', change it back to 'auto'. */ - if (nm_setting_ip4_config_get_num_addresses (NM_SETTING_IP4_CONFIG (object))) { - if (g_strcmp0 (nm_setting_ip4_config_get_method (NM_SETTING_IP4_CONFIG (object)), NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) { + if (nm_setting_ip_config_get_num_addresses (NM_SETTING_IP_CONFIG (object))) { + if (g_strcmp0 (nm_setting_ip_config_get_method (NM_SETTING_IP_CONFIG (object)), NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) { if (!answered) { answered = TRUE; answer = get_answer ("ipv4.method", "manual"); } if (answer) - g_object_set (object, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); + g_object_set (object, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); } } else { answered = FALSE; - if (!g_strcmp0 (nm_setting_ip4_config_get_method (NM_SETTING_IP4_CONFIG (object)), NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) - g_object_set (object, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + if (!g_strcmp0 (nm_setting_ip_config_get_method (NM_SETTING_IP_CONFIG (object)), NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) + g_object_set (object, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); } g_signal_handlers_unblock_by_func (object, G_CALLBACK (ipv4_method_changed_cb), NULL); @@ -1724,8 +1725,8 @@ ipv4_method_changed_cb (GObject *object, GParamSpec *pspec, gpointer user_data) g_signal_handlers_block_by_func (object, G_CALLBACK (ipv4_addresses_changed_cb), NULL); /* If method != manual, remove addresses (save them for restoring them later when method becomes 'manual' */ - if (g_strcmp0 (nm_setting_ip4_config_get_method (NM_SETTING_IP4_CONFIG (object)), NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) { - if (nm_setting_ip4_config_get_num_addresses (NM_SETTING_IP4_CONFIG (object))) { + if (g_strcmp0 (nm_setting_ip_config_get_method (NM_SETTING_IP_CONFIG (object)), NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) { + if (nm_setting_ip_config_get_num_addresses (NM_SETTING_IP_CONFIG (object))) { if (!answered) { answered = TRUE; answer = get_answer ("ipv4.addresses", NULL); @@ -1733,14 +1734,14 @@ ipv4_method_changed_cb (GObject *object, GParamSpec *pspec, gpointer user_data) if (answer) { if (G_IS_VALUE (&value)) g_value_unset (&value); - nmc_property_get_gvalue (NM_SETTING (object), NM_SETTING_IP4_CONFIG_ADDRESSES, &value); - g_object_set (object, NM_SETTING_IP4_CONFIG_ADDRESSES, NULL, NULL); + nmc_property_get_gvalue (NM_SETTING (object), NM_SETTING_IP_CONFIG_ADDRESSES, &value); + g_object_set (object, NM_SETTING_IP_CONFIG_ADDRESSES, NULL, NULL); } } } else { answered = FALSE; if (G_IS_VALUE (&value)) { - nmc_property_set_gvalue (NM_SETTING (object), NM_SETTING_IP4_CONFIG_ADDRESSES, &value); + nmc_property_set_gvalue (NM_SETTING (object), NM_SETTING_IP_CONFIG_ADDRESSES, &value); g_value_unset (&value); } } @@ -1759,19 +1760,19 @@ ipv6_addresses_changed_cb (GObject *object, GParamSpec *pspec, gpointer user_dat /* If we have some IP addresses set method to 'manual'. * Else if the method was 'manual', change it back to 'auto'. */ - if (nm_setting_ip6_config_get_num_addresses (NM_SETTING_IP6_CONFIG (object))) { - if (g_strcmp0 (nm_setting_ip6_config_get_method (NM_SETTING_IP6_CONFIG (object)), NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) { + if (nm_setting_ip_config_get_num_addresses (NM_SETTING_IP_CONFIG (object))) { + if (g_strcmp0 (nm_setting_ip_config_get_method (NM_SETTING_IP_CONFIG (object)), NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) { if (!answered) { answered = TRUE; answer = get_answer ("ipv6.method", "manual"); } if (answer) - g_object_set (object, NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, NULL); + g_object_set (object, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, NULL); } } else { answered = FALSE; - if (!g_strcmp0 (nm_setting_ip6_config_get_method (NM_SETTING_IP6_CONFIG (object)), NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) - g_object_set (object, NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); + if (!g_strcmp0 (nm_setting_ip_config_get_method (NM_SETTING_IP_CONFIG (object)), NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) + g_object_set (object, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); } g_signal_handlers_unblock_by_func (object, G_CALLBACK (ipv6_method_changed_cb), NULL); @@ -1787,8 +1788,8 @@ ipv6_method_changed_cb (GObject *object, GParamSpec *pspec, gpointer user_data) g_signal_handlers_block_by_func (object, G_CALLBACK (ipv6_addresses_changed_cb), NULL); /* If method != manual, remove addresses (save them for restoring them later when method becomes 'manual' */ - if (g_strcmp0 (nm_setting_ip6_config_get_method (NM_SETTING_IP6_CONFIG (object)), NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) { - if (nm_setting_ip6_config_get_num_addresses (NM_SETTING_IP6_CONFIG (object))) { + if (g_strcmp0 (nm_setting_ip_config_get_method (NM_SETTING_IP_CONFIG (object)), NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) { + if (nm_setting_ip_config_get_num_addresses (NM_SETTING_IP_CONFIG (object))) { if (!answered) { answered = TRUE; answer = get_answer ("ipv6.addresses", NULL); @@ -1796,14 +1797,14 @@ ipv6_method_changed_cb (GObject *object, GParamSpec *pspec, gpointer user_data) if (answer) { if (G_IS_VALUE (&value)) g_value_unset (&value); - nmc_property_get_gvalue (NM_SETTING (object), NM_SETTING_IP6_CONFIG_ADDRESSES, &value); - g_object_set (object, NM_SETTING_IP6_CONFIG_ADDRESSES, NULL, NULL); + nmc_property_get_gvalue (NM_SETTING (object), NM_SETTING_IP_CONFIG_ADDRESSES, &value); + g_object_set (object, NM_SETTING_IP_CONFIG_ADDRESSES, NULL, NULL); } } } else { answered = FALSE; if (G_IS_VALUE (&value)) { - nmc_property_set_gvalue (NM_SETTING (object), NM_SETTING_IP6_CONFIG_ADDRESSES, &value); + nmc_property_set_gvalue (NM_SETTING (object), NM_SETTING_IP_CONFIG_ADDRESSES, &value); g_value_unset (&value); } } @@ -1867,24 +1868,24 @@ connection_master_changed_cb (GObject *object, GParamSpec *pspec, gpointer user_ } void -nmc_setting_ip4_connect_handlers (NMSettingIP4Config *setting) +nmc_setting_ip4_connect_handlers (NMSettingIPConfig *setting) { g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - g_signal_connect (setting, "notify::" NM_SETTING_IP4_CONFIG_ADDRESSES, + g_signal_connect (setting, "notify::" NM_SETTING_IP_CONFIG_ADDRESSES, G_CALLBACK (ipv4_addresses_changed_cb), NULL); - g_signal_connect (setting, "notify::" NM_SETTING_IP4_CONFIG_METHOD, + g_signal_connect (setting, "notify::" NM_SETTING_IP_CONFIG_METHOD, G_CALLBACK (ipv4_method_changed_cb), NULL); } void -nmc_setting_ip6_connect_handlers (NMSettingIP6Config *setting) +nmc_setting_ip6_connect_handlers (NMSettingIPConfig *setting) { g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - g_signal_connect (setting, "notify::" NM_SETTING_IP6_CONFIG_ADDRESSES, + g_signal_connect (setting, "notify::" NM_SETTING_IP_CONFIG_ADDRESSES, G_CALLBACK (ipv6_addresses_changed_cb), NULL); - g_signal_connect (setting, "notify::" NM_SETTING_IP6_CONFIG_METHOD, + g_signal_connect (setting, "notify::" NM_SETTING_IP_CONFIG_METHOD, G_CALLBACK (ipv6_method_changed_cb), NULL); } @@ -1918,15 +1919,15 @@ nmc_setting_custom_init (NMSetting *setting) g_return_if_fail (NM_IS_SETTING (setting)); if (NM_IS_SETTING_IP4_CONFIG (setting)) { - g_object_set (NM_SETTING_IP4_CONFIG (setting), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + g_object_set (NM_SETTING_IP_CONFIG (setting), + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); - nmc_setting_ip4_connect_handlers (NM_SETTING_IP4_CONFIG (setting)); + nmc_setting_ip4_connect_handlers (NM_SETTING_IP_CONFIG (setting)); } else if (NM_IS_SETTING_IP6_CONFIG (setting)) { - g_object_set (NM_SETTING_IP6_CONFIG (setting), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + g_object_set (NM_SETTING_IP_CONFIG (setting), + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); - nmc_setting_ip6_connect_handlers (NM_SETTING_IP6_CONFIG (setting)); + nmc_setting_ip6_connect_handlers (NM_SETTING_IP_CONFIG (setting)); } else if (NM_IS_SETTING_WIRELESS (setting)) { g_object_set (NM_SETTING_WIRELESS (setting), NM_SETTING_WIRELESS_MODE, NM_SETTING_WIRELESS_MODE_INFRA, @@ -3151,14 +3152,14 @@ nmc_property_ipv4_set_dns (NMSetting *setting, const char *prop, const char *val g_strfreev (strv); return FALSE; } - nm_setting_ip4_config_add_dns (NM_SETTING_IP4_CONFIG (setting), addr); + nm_setting_ip_config_add_dns (NM_SETTING_IP_CONFIG (setting), addr); } g_strfreev (strv); return TRUE; } static gboolean -_validate_and_remove_ipv4_dns (NMSettingIP4Config *setting, +_validate_and_remove_ipv4_dns (NMSettingIPConfig *setting, const char *dns, GError **error) { @@ -3170,15 +3171,15 @@ _validate_and_remove_ipv4_dns (NMSettingIP4Config *setting, return FALSE; } - ret = nm_setting_ip4_config_remove_dns_by_value (setting, dns); + ret = nm_setting_ip_config_remove_dns_by_value (setting, dns); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain DNS server '%s'"), dns); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_dns, - NM_SETTING_IP4_CONFIG, - nm_setting_ip4_config_get_num_dns, - nm_setting_ip4_config_remove_dns, + NM_SETTING_IP_CONFIG, + nm_setting_ip_config_get_num_dns, + nm_setting_ip_config_remove_dns, _validate_and_remove_ipv4_dns) static const char * @@ -3204,20 +3205,20 @@ nmc_property_ipv4_set_dns_search (NMSetting *setting, const char *prop, const ch } while (strv && strv[i]) - nm_setting_ip4_config_add_dns_search (NM_SETTING_IP4_CONFIG (setting), strv[i++]); + nm_setting_ip_config_add_dns_search (NM_SETTING_IP_CONFIG (setting), strv[i++]); g_strfreev (strv); return TRUE; } static gboolean -_validate_and_remove_ipv4_dns_search (NMSettingIP4Config *setting, +_validate_and_remove_ipv4_dns_search (NMSettingIPConfig *setting, const char *dns_search, GError **error) { gboolean ret; - ret = nm_setting_ip4_config_remove_dns_search_by_value (setting, dns_search); + ret = nm_setting_ip_config_remove_dns_search_by_value (setting, dns_search); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain DNS search domain '%s'"), @@ -3225,9 +3226,9 @@ _validate_and_remove_ipv4_dns_search (NMSettingIP4Config *setting, return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_dns_search, - NM_SETTING_IP4_CONFIG, - nm_setting_ip4_config_get_num_dns_searches, - nm_setting_ip4_config_remove_dns_search, + NM_SETTING_IP_CONFIG, + nm_setting_ip_config_get_num_dns_searches, + nm_setting_ip_config_remove_dns_search, _validate_and_remove_ipv4_dns_search) /* 'addresses' */ @@ -3252,7 +3253,7 @@ nmc_property_ipv4_set_addresses (NMSetting *setting, const char *prop, const cha g_strfreev (strv); return FALSE; } - nm_setting_ip4_config_add_address (NM_SETTING_IP4_CONFIG (setting), ip4addr); + nm_setting_ip_config_add_address (NM_SETTING_IP_CONFIG (setting), ip4addr); nm_ip_address_unref (ip4addr); } g_strfreev (strv); @@ -3260,7 +3261,7 @@ nmc_property_ipv4_set_addresses (NMSetting *setting, const char *prop, const cha } static gboolean -_validate_and_remove_ipv4_address (NMSettingIP4Config *setting, +_validate_and_remove_ipv4_address (NMSettingIPConfig *setting, const char *address, GError **error) { @@ -3271,7 +3272,7 @@ _validate_and_remove_ipv4_address (NMSettingIP4Config *setting, if (!ip4addr) return FALSE; - ret = nm_setting_ip4_config_remove_address_by_value (setting, ip4addr); + ret = nm_setting_ip_config_remove_address_by_value (setting, ip4addr); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain IP address '%s'"), address); @@ -3279,9 +3280,9 @@ _validate_and_remove_ipv4_address (NMSettingIP4Config *setting, return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_addresses, - NM_SETTING_IP4_CONFIG, - nm_setting_ip4_config_get_num_addresses, - nm_setting_ip4_config_remove_address, + NM_SETTING_IP_CONFIG, + nm_setting_ip_config_get_num_addresses, + nm_setting_ip_config_remove_address, _validate_and_remove_ipv4_address) static const char * @@ -3350,7 +3351,7 @@ nmc_property_ipv4_set_routes (NMSetting *setting, const char *prop, const char * g_strfreev (strv); return FALSE; } - nm_setting_ip4_config_add_route (NM_SETTING_IP4_CONFIG (setting), ip4route); + nm_setting_ip_config_add_route (NM_SETTING_IP_CONFIG (setting), ip4route); nm_ip_route_unref (ip4route); } g_strfreev (strv); @@ -3358,7 +3359,7 @@ nmc_property_ipv4_set_routes (NMSetting *setting, const char *prop, const char * } static gboolean -_validate_and_remove_ipv4_route (NMSettingIP4Config *setting, +_validate_and_remove_ipv4_route (NMSettingIPConfig *setting, const char *route, GError **error) { @@ -3369,16 +3370,16 @@ _validate_and_remove_ipv4_route (NMSettingIP4Config *setting, if (!ip4route) return FALSE; - ret = nm_setting_ip4_config_remove_route_by_value (setting, ip4route); + ret = nm_setting_ip_config_remove_route_by_value (setting, ip4route); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain route '%s'"), route); nm_ip_route_unref (ip4route); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv4_remove_routes, - NM_SETTING_IP4_CONFIG, - nm_setting_ip4_config_get_num_routes, - nm_setting_ip4_config_remove_route, + NM_SETTING_IP_CONFIG, + nm_setting_ip_config_get_num_routes, + nm_setting_ip_config_remove_route, _validate_and_remove_ipv4_route) static const char * @@ -3465,14 +3466,14 @@ nmc_property_ipv6_set_dns (NMSetting *setting, const char *prop, const char *val g_strfreev (strv); return FALSE; } - nm_setting_ip6_config_add_dns (NM_SETTING_IP6_CONFIG (setting), addr); + nm_setting_ip_config_add_dns (NM_SETTING_IP_CONFIG (setting), addr); } g_strfreev (strv); return TRUE; } static gboolean -_validate_and_remove_ipv6_dns (NMSettingIP6Config *setting, +_validate_and_remove_ipv6_dns (NMSettingIPConfig *setting, const char *dns, GError **error) { @@ -3484,15 +3485,15 @@ _validate_and_remove_ipv6_dns (NMSettingIP6Config *setting, return FALSE; } - ret = nm_setting_ip6_config_remove_dns_by_value (setting, dns); + ret = nm_setting_ip_config_remove_dns_by_value (setting, dns); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain DNS server '%s'"), dns); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_dns, - NM_SETTING_IP6_CONFIG, - nm_setting_ip6_config_get_num_dns, - nm_setting_ip6_config_remove_dns, + NM_SETTING_IP_CONFIG, + nm_setting_ip_config_get_num_dns, + nm_setting_ip_config_remove_dns, _validate_and_remove_ipv6_dns) static const char * @@ -3524,20 +3525,20 @@ nmc_property_ipv6_set_dns_search (NMSetting *setting, const char *prop, const ch } while (strv && strv[i]) - nm_setting_ip6_config_add_dns_search (NM_SETTING_IP6_CONFIG (setting), strv[i++]); + nm_setting_ip_config_add_dns_search (NM_SETTING_IP_CONFIG (setting), strv[i++]); g_strfreev (strv); return TRUE; } static gboolean -_validate_and_remove_ipv6_dns_search (NMSettingIP6Config *setting, +_validate_and_remove_ipv6_dns_search (NMSettingIPConfig *setting, const char *dns_search, GError **error) { gboolean ret; - ret = nm_setting_ip6_config_remove_dns_search_by_value (setting, dns_search); + ret = nm_setting_ip_config_remove_dns_search_by_value (setting, dns_search); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain DNS search domain '%s'"), @@ -3545,9 +3546,9 @@ _validate_and_remove_ipv6_dns_search (NMSettingIP6Config *setting, return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_dns_search, - NM_SETTING_IP6_CONFIG, - nm_setting_ip6_config_get_num_dns_searches, - nm_setting_ip6_config_remove_dns_search, + NM_SETTING_IP_CONFIG, + nm_setting_ip_config_get_num_dns_searches, + nm_setting_ip_config_remove_dns_search, _validate_and_remove_ipv6_dns_search) /* 'addresses' */ @@ -3572,7 +3573,7 @@ nmc_property_ipv6_set_addresses (NMSetting *setting, const char *prop, const cha g_strfreev (strv); return FALSE; } - nm_setting_ip6_config_add_address (NM_SETTING_IP6_CONFIG (setting), ip6addr); + nm_setting_ip_config_add_address (NM_SETTING_IP_CONFIG (setting), ip6addr); nm_ip_address_unref (ip6addr); } g_strfreev (strv); @@ -3580,7 +3581,7 @@ nmc_property_ipv6_set_addresses (NMSetting *setting, const char *prop, const cha } static gboolean -_validate_and_remove_ipv6_address (NMSettingIP6Config *setting, +_validate_and_remove_ipv6_address (NMSettingIPConfig *setting, const char *address, GError **error) { @@ -3591,16 +3592,16 @@ _validate_and_remove_ipv6_address (NMSettingIP6Config *setting, if (!ip6addr) return FALSE; - ret = nm_setting_ip6_config_remove_address_by_value (setting, ip6addr); + ret = nm_setting_ip_config_remove_address_by_value (setting, ip6addr); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain IP address '%s'"), address); nm_ip_address_unref (ip6addr); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_addresses, - NM_SETTING_IP6_CONFIG, - nm_setting_ip6_config_get_num_addresses, - nm_setting_ip6_config_remove_address, + NM_SETTING_IP_CONFIG, + nm_setting_ip_config_get_num_addresses, + nm_setting_ip_config_remove_address, _validate_and_remove_ipv6_address) static const char * @@ -3634,7 +3635,7 @@ nmc_property_ipv6_set_routes (NMSetting *setting, const char *prop, const char * g_strfreev (strv); return FALSE; } - nm_setting_ip6_config_add_route (NM_SETTING_IP6_CONFIG (setting), ip6route); + nm_setting_ip_config_add_route (NM_SETTING_IP_CONFIG (setting), ip6route); nm_ip_route_unref (ip6route); } g_strfreev (strv); @@ -3642,7 +3643,7 @@ nmc_property_ipv6_set_routes (NMSetting *setting, const char *prop, const char * } static gboolean -_validate_and_remove_ipv6_route (NMSettingIP6Config *setting, +_validate_and_remove_ipv6_route (NMSettingIPConfig *setting, const char *route, GError **error) { @@ -3653,16 +3654,16 @@ _validate_and_remove_ipv6_route (NMSettingIP6Config *setting, if (!ip6route) return FALSE; - ret = nm_setting_ip6_config_remove_route_by_value (setting, ip6route); + ret = nm_setting_ip_config_remove_route_by_value (setting, ip6route); if (!ret) g_set_error (error, 1, 0, _("the property doesn't contain route '%s'"), route); nm_ip_route_unref (ip6route); return ret; } DEFINE_REMOVER_INDEX_OR_VALUE (nmc_property_ipv6_remove_routes, - NM_SETTING_IP6_CONFIG, - nm_setting_ip6_config_get_num_routes, - nm_setting_ip6_config_remove_route, + NM_SETTING_IP_CONFIG, + nm_setting_ip_config_get_num_routes, + nm_setting_ip_config_remove_route, _validate_and_remove_ipv6_route) static const char * @@ -4695,6 +4696,8 @@ nmc_add_prop_funcs (char *key, /* concatenate setting name and property name */ #define GLUE(A,B) (g_strconcat ((NM_SETTING_##A##_SETTING_NAME),(NM_SETTING_##A##_##B), NULL)) +#define GLUE_IP(A,B) (g_strconcat ((NM_SETTING_IP##A##_CONFIG_SETTING_NAME),(NM_SETTING_IP_CONFIG_##B), NULL)) + void nmc_properties_init (void) { @@ -5420,49 +5423,49 @@ nmc_properties_init (void) NULL); /* Add editable properties for NM_SETTING_IP4_CONFIG_SETTING_NAME */ - nmc_add_prop_funcs (GLUE (IP4_CONFIG, METHOD), + nmc_add_prop_funcs (GLUE_IP (4, METHOD), nmc_property_ipv4_get_method, nmc_property_ipv4_set_method, NULL, NULL, nmc_property_ipv4_allowed_method, NULL); - nmc_add_prop_funcs (GLUE (IP4_CONFIG, DNS), + nmc_add_prop_funcs (GLUE_IP (4, DNS), nmc_property_ipv4_get_dns, nmc_property_ipv4_set_dns, nmc_property_ipv4_remove_dns, nmc_property_ipv4_describe_dns, NULL, NULL); - nmc_add_prop_funcs (GLUE (IP4_CONFIG, DNS_SEARCH), + nmc_add_prop_funcs (GLUE_IP (4, DNS_SEARCH), nmc_property_ipv4_get_dns_search, nmc_property_ipv4_set_dns_search, nmc_property_ipv4_remove_dns_search, NULL, NULL, NULL); - nmc_add_prop_funcs (GLUE (IP4_CONFIG, ADDRESSES), + nmc_add_prop_funcs (GLUE_IP (4, ADDRESSES), nmc_property_ipv4_get_addresses, nmc_property_ipv4_set_addresses, nmc_property_ipv4_remove_addresses, nmc_property_ipv4_describe_addresses, NULL, nmc_property_out2in_addresses); - nmc_add_prop_funcs (GLUE (IP4_CONFIG, ROUTES), + nmc_add_prop_funcs (GLUE_IP (4, ROUTES), nmc_property_ipv4_get_routes, nmc_property_ipv4_set_routes, nmc_property_ipv4_remove_routes, nmc_property_ipv4_describe_routes, NULL, nmc_property_out2in_routes); - nmc_add_prop_funcs (GLUE (IP4_CONFIG, IGNORE_AUTO_ROUTES), + nmc_add_prop_funcs (GLUE_IP (4, IGNORE_AUTO_ROUTES), nmc_property_ipv4_get_ignore_auto_routes, nmc_property_set_bool, NULL, NULL, NULL, NULL); - nmc_add_prop_funcs (GLUE (IP4_CONFIG, IGNORE_AUTO_DNS), + nmc_add_prop_funcs (GLUE_IP (4, IGNORE_AUTO_DNS), nmc_property_ipv4_get_ignore_auto_dns, nmc_property_set_bool, NULL, @@ -5476,28 +5479,28 @@ nmc_properties_init (void) NULL, NULL, NULL); - nmc_add_prop_funcs (GLUE (IP4_CONFIG, DHCP_SEND_HOSTNAME), + nmc_add_prop_funcs (GLUE_IP (4, DHCP_SEND_HOSTNAME), nmc_property_ipv4_get_dhcp_send_hostname, nmc_property_set_bool, NULL, NULL, NULL, NULL); - nmc_add_prop_funcs (GLUE (IP4_CONFIG, DHCP_HOSTNAME), + nmc_add_prop_funcs (GLUE_IP (4, DHCP_HOSTNAME), nmc_property_ipv4_get_dhcp_hostname, nmc_property_set_string, NULL, NULL, NULL, NULL); - nmc_add_prop_funcs (GLUE (IP4_CONFIG, NEVER_DEFAULT), + nmc_add_prop_funcs (GLUE_IP (4, NEVER_DEFAULT), nmc_property_ipv4_get_never_default, nmc_property_set_bool, NULL, NULL, NULL, NULL); - nmc_add_prop_funcs (GLUE (IP4_CONFIG, MAY_FAIL), + nmc_add_prop_funcs (GLUE_IP (4, MAY_FAIL), nmc_property_ipv4_get_may_fail, nmc_property_set_bool, NULL, @@ -5506,63 +5509,63 @@ nmc_properties_init (void) NULL); /* Add editable properties for NM_SETTING_IP6_CONFIG_SETTING_NAME */ - nmc_add_prop_funcs (GLUE (IP6_CONFIG, METHOD), + nmc_add_prop_funcs (GLUE_IP (6, METHOD), nmc_property_ipv6_get_method, nmc_property_ipv6_set_method, NULL, NULL, nmc_property_ipv6_allowed_method, NULL); - nmc_add_prop_funcs (GLUE (IP6_CONFIG, DNS), + nmc_add_prop_funcs (GLUE_IP (6, DNS), nmc_property_ipv6_get_dns, nmc_property_ipv6_set_dns, nmc_property_ipv6_remove_dns, nmc_property_ipv6_describe_dns, NULL, NULL); - nmc_add_prop_funcs (GLUE (IP6_CONFIG, DNS_SEARCH), + nmc_add_prop_funcs (GLUE_IP (6, DNS_SEARCH), nmc_property_ipv6_get_dns_search, nmc_property_ipv6_set_dns_search, nmc_property_ipv6_remove_dns_search, NULL, NULL, NULL); - nmc_add_prop_funcs (GLUE (IP6_CONFIG, ADDRESSES), + nmc_add_prop_funcs (GLUE_IP (6, ADDRESSES), nmc_property_ipv6_get_addresses, nmc_property_ipv6_set_addresses, nmc_property_ipv6_remove_addresses, nmc_property_ipv6_describe_addresses, NULL, nmc_property_out2in_addresses); - nmc_add_prop_funcs (GLUE (IP6_CONFIG, ROUTES), + nmc_add_prop_funcs (GLUE_IP (6, ROUTES), nmc_property_ipv6_get_routes, nmc_property_ipv6_set_routes, nmc_property_ipv6_remove_routes, nmc_property_ipv6_describe_routes, NULL, nmc_property_out2in_routes); - nmc_add_prop_funcs (GLUE (IP6_CONFIG, IGNORE_AUTO_ROUTES), + nmc_add_prop_funcs (GLUE_IP (6, IGNORE_AUTO_ROUTES), nmc_property_ipv6_get_ignore_auto_routes, nmc_property_set_bool, NULL, NULL, NULL, NULL); - nmc_add_prop_funcs (GLUE (IP6_CONFIG, IGNORE_AUTO_DNS), + nmc_add_prop_funcs (GLUE_IP (6, IGNORE_AUTO_DNS), nmc_property_ipv6_get_ignore_auto_dns, nmc_property_set_bool, NULL, NULL, NULL, NULL); - nmc_add_prop_funcs (GLUE (IP6_CONFIG, NEVER_DEFAULT), + nmc_add_prop_funcs (GLUE_IP (6, NEVER_DEFAULT), nmc_property_ipv6_get_never_default, nmc_property_set_bool, NULL, NULL, NULL, NULL); - nmc_add_prop_funcs (GLUE (IP6_CONFIG, MAY_FAIL), + nmc_add_prop_funcs (GLUE_IP (6, MAY_FAIL), nmc_property_ipv6_get_may_fail, nmc_property_set_bool, NULL, @@ -5576,7 +5579,7 @@ nmc_properties_init (void) NULL, NULL, nmc_property_out2in_cut_paren); - nmc_add_prop_funcs (GLUE (IP6_CONFIG, DHCP_HOSTNAME), + nmc_add_prop_funcs (GLUE_IP (6, DHCP_HOSTNAME), nmc_property_ipv6_get_dhcp_hostname, nmc_property_set_string, NULL, @@ -6715,7 +6718,7 @@ setting_wireless_security_details (NMSetting *setting, NmCli *nmc, const char *o static gboolean setting_ip4_config_details (NMSetting *setting, NmCli *nmc, const char *one_prop, gboolean secrets) { - NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (setting); + NMSettingIPConfig *s_ip4 = NM_SETTING_IP_CONFIG (setting); NmcOutputField *tmpl, *arr; size_t tmpl_len; @@ -6752,7 +6755,7 @@ setting_ip4_config_details (NMSetting *setting, NmCli *nmc, const char *one_pro static gboolean setting_ip6_config_details (NMSetting *setting, NmCli *nmc, const char *one_prop, gboolean secrets) { - NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (setting); + NMSettingIPConfig *s_ip6 = NM_SETTING_IP_CONFIG (setting); NmcOutputField *tmpl, *arr; size_t tmpl_len; diff --git a/clients/cli/settings.h b/clients/cli/settings.h index ad74fc9053..3c4c814fd2 100644 --- a/clients/cli/settings.h +++ b/clients/cli/settings.h @@ -30,8 +30,8 @@ void nmc_properties_cleanup (void); NMSetting *nmc_setting_new_for_name (const char *name); void nmc_setting_custom_init (NMSetting *setting); -void nmc_setting_ip4_connect_handlers (NMSettingIP4Config *setting); -void nmc_setting_ip6_connect_handlers (NMSettingIP6Config *setting); +void nmc_setting_ip4_connect_handlers (NMSettingIPConfig *setting); +void nmc_setting_ip6_connect_handlers (NMSettingIPConfig *setting); void nmc_setting_wireless_connect_handlers (NMSettingWireless *setting); void nmc_setting_connection_connect_handlers (NMSettingConnection *setting, NMConnection *connection); diff --git a/clients/tui/nmt-page-ip4.c b/clients/tui/nmt-page-ip4.c index 686c2ce923..782a204fc9 100644 --- a/clients/tui/nmt-page-ip4.c +++ b/clients/tui/nmt-page-ip4.c @@ -59,12 +59,12 @@ static gboolean nmt_page_ip4_show_by_default (NmtEditorPage *page) { NMConnection *conn; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; conn = nmt_editor_page_get_connection (page); s_ip4 = nm_connection_get_setting_ip4_config (conn); - if ( !g_strcmp0 (nm_setting_ip4_config_get_method (s_ip4), NM_SETTING_IP4_CONFIG_METHOD_MANUAL) - || nm_setting_ip4_config_get_num_addresses (s_ip4)) + if ( !g_strcmp0 (nm_setting_ip_config_get_method (s_ip4), NM_SETTING_IP4_CONFIG_METHOD_MANUAL) + || nm_setting_ip_config_get_num_addresses (s_ip4)) return TRUE; return FALSE; } @@ -115,22 +115,22 @@ nmt_page_ip4_constructed (GObject *object) { NmtPageIP4 *ip4 = NMT_PAGE_IP4 (object); NmtPageGrid *grid; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NmtNewtWidget *widget, *button; NMConnection *conn; conn = nmt_editor_page_get_connection (NMT_EDITOR_PAGE (ip4)); s_ip4 = nm_connection_get_setting_ip4_config (conn); if (!s_ip4) { - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (conn, (NMSetting *) s_ip4); } widget = nmt_newt_popup_new (ip4methods); - g_object_bind_property (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, + g_object_bind_property (s_ip4, NM_SETTING_IP_CONFIG_METHOD, widget, "active-id", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_editor_page_set_header_widget (NMT_EDITOR_PAGE (ip4), widget); @@ -139,27 +139,27 @@ nmt_page_ip4_constructed (GObject *object) widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4_WITH_PREFIX); nm_editor_bind_ip_addresses_with_prefix_to_strv (AF_INET, - s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES, + s_ip4, NM_SETTING_IP_CONFIG_ADDRESSES, widget, "strings", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Addresses"), widget, NULL); widget = nmt_ip_entry_new (25, AF_INET, FALSE, TRUE); nm_editor_bind_ip_gateway_to_string (AF_INET, - s_ip4, NM_SETTING_IP4_CONFIG_ADDRESSES, + s_ip4, NM_SETTING_IP_CONFIG_ADDRESSES, widget, "text", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Gateway"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4); nm_editor_bind_ip_addresses_to_strv (AF_INET, - s_ip4, NM_SETTING_IP4_CONFIG_DNS, + s_ip4, NM_SETTING_IP_CONFIG_DNS, widget, "strings", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("DNS servers"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_HOSTNAME); - g_object_bind_property (s_ip4, NM_SETTING_IP4_CONFIG_DNS_SEARCH, + g_object_bind_property (s_ip4, NM_SETTING_IP_CONFIG_DNS_SEARCH, widget, "strings", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Search domains"), widget, NULL); @@ -170,7 +170,7 @@ nmt_page_ip4_constructed (GObject *object) "text", "", "style", NMT_NEWT_LABEL_PLAIN, NULL); - g_object_bind_property_full (s_ip4, NM_SETTING_IP4_CONFIG_ROUTES, + g_object_bind_property_full (s_ip4, NM_SETTING_IP_CONFIG_ROUTES, widget, "text", G_BINDING_SYNC_CREATE, ip4_routes_transform_to_description, @@ -180,7 +180,7 @@ nmt_page_ip4_constructed (GObject *object) nmt_page_grid_append (grid, _("Routing"), widget, button); widget = nmt_newt_checkbox_new (_("Never use this network for default route")); - g_object_bind_property (s_ip4, NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, + g_object_bind_property (s_ip4, NM_SETTING_IP_CONFIG_NEVER_DEFAULT, widget, "active", G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL); nmt_page_grid_append (grid, NULL, widget, NULL); @@ -188,7 +188,7 @@ nmt_page_ip4_constructed (GObject *object) nmt_page_grid_append (grid, NULL, nmt_newt_separator_new (), NULL); widget = nmt_newt_checkbox_new (_("Require IPv4 addressing for this connection")); - g_object_bind_property (s_ip4, NM_SETTING_IP4_CONFIG_MAY_FAIL, + g_object_bind_property (s_ip4, NM_SETTING_IP_CONFIG_MAY_FAIL, widget, "active", G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL | G_BINDING_INVERT_BOOLEAN); diff --git a/clients/tui/nmt-page-ip6.c b/clients/tui/nmt-page-ip6.c index 05917fa0c8..88b948cd14 100644 --- a/clients/tui/nmt-page-ip6.c +++ b/clients/tui/nmt-page-ip6.c @@ -59,12 +59,12 @@ static gboolean nmt_page_ip6_show_by_default (NmtEditorPage *page) { NMConnection *conn; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; conn = nmt_editor_page_get_connection (page); s_ip6 = nm_connection_get_setting_ip6_config (conn); - if ( !g_strcmp0 (nm_setting_ip6_config_get_method (s_ip6), NM_SETTING_IP6_CONFIG_METHOD_MANUAL) - || nm_setting_ip6_config_get_num_addresses (s_ip6)) + if ( !g_strcmp0 (nm_setting_ip_config_get_method (s_ip6), NM_SETTING_IP6_CONFIG_METHOD_MANUAL) + || nm_setting_ip_config_get_num_addresses (s_ip6)) return TRUE; return FALSE; } @@ -115,22 +115,22 @@ nmt_page_ip6_constructed (GObject *object) { NmtPageIP6 *ip6 = NMT_PAGE_IP6 (object); NmtPageGrid *grid; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; NmtNewtWidget *widget, *button; NMConnection *conn; conn = nmt_editor_page_get_connection (NMT_EDITOR_PAGE (ip6)); s_ip6 = nm_connection_get_setting_ip6_config (conn); if (!s_ip6) { - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_object_set (G_OBJECT (s_ip6), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (conn, (NMSetting *) s_ip6); } widget = nmt_newt_popup_new (ip6methods); - g_object_bind_property (s_ip6, NM_SETTING_IP6_CONFIG_METHOD, + g_object_bind_property (s_ip6, NM_SETTING_IP_CONFIG_METHOD, widget, "active-id", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_editor_page_set_header_widget (NMT_EDITOR_PAGE (ip6), widget); @@ -139,27 +139,27 @@ nmt_page_ip6_constructed (GObject *object) widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP6_WITH_PREFIX); nm_editor_bind_ip_addresses_with_prefix_to_strv (AF_INET6, - s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES, + s_ip6, NM_SETTING_IP_CONFIG_ADDRESSES, widget, "strings", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Addresses"), widget, NULL); widget = nmt_ip_entry_new (25, AF_INET6, FALSE, TRUE); nm_editor_bind_ip_gateway_to_string (AF_INET6, - s_ip6, NM_SETTING_IP6_CONFIG_ADDRESSES, + s_ip6, NM_SETTING_IP_CONFIG_ADDRESSES, widget, "text", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Gateway"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP6); nm_editor_bind_ip_addresses_to_strv (AF_INET6, - s_ip6, NM_SETTING_IP6_CONFIG_DNS, + s_ip6, NM_SETTING_IP_CONFIG_DNS, widget, "strings", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("DNS servers"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_HOSTNAME); - g_object_bind_property (s_ip6, NM_SETTING_IP6_CONFIG_DNS_SEARCH, + g_object_bind_property (s_ip6, NM_SETTING_IP_CONFIG_DNS_SEARCH, widget, "strings", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Search domains"), widget, NULL); @@ -168,7 +168,7 @@ nmt_page_ip6_constructed (GObject *object) "text", "", "style", NMT_NEWT_LABEL_PLAIN, NULL); - g_object_bind_property_full (s_ip6, NM_SETTING_IP6_CONFIG_ROUTES, + g_object_bind_property_full (s_ip6, NM_SETTING_IP_CONFIG_ROUTES, widget, "text", G_BINDING_SYNC_CREATE, ip6_routes_transform_to_description, @@ -178,7 +178,7 @@ nmt_page_ip6_constructed (GObject *object) nmt_page_grid_append (grid, _("Routing"), widget, button); widget = nmt_newt_checkbox_new (_("Never use this network for default route")); - g_object_bind_property (s_ip6, NM_SETTING_IP6_CONFIG_NEVER_DEFAULT, + g_object_bind_property (s_ip6, NM_SETTING_IP_CONFIG_NEVER_DEFAULT, widget, "active", G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL); nmt_page_grid_append (grid, NULL, widget, NULL); @@ -186,7 +186,7 @@ nmt_page_ip6_constructed (GObject *object) nmt_page_grid_append (grid, NULL, nmt_newt_separator_new (), NULL); widget = nmt_newt_checkbox_new (_("Require IPv6 addressing for this connection")); - g_object_bind_property (s_ip6, NM_SETTING_IP6_CONFIG_MAY_FAIL, + g_object_bind_property (s_ip6, NM_SETTING_IP_CONFIG_MAY_FAIL, widget, "active", G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL | G_BINDING_INVERT_BOOLEAN); diff --git a/clients/tui/nmt-route-editor.c b/clients/tui/nmt-route-editor.c index 3871b86302..77bbec76de 100644 --- a/clients/tui/nmt-route-editor.c +++ b/clients/tui/nmt-route-editor.c @@ -77,21 +77,15 @@ save_routes_and_exit (NmtNewtButton *button, { NmtRouteEditor *editor = user_data; NmtRouteEditorPrivate *priv = NMT_ROUTE_EDITOR_GET_PRIVATE (editor); - const char *property; - GBinding *binding; + GPtrArray *routes; - if (NM_IS_SETTING_IP4_CONFIG (priv->edit_setting)) - property = NM_SETTING_IP4_CONFIG_ROUTES; - else - property = NM_SETTING_IP6_CONFIG_ROUTES; - - /* Because of the complicated dbus-glib GTypes, it's easier to cheat - * and use GBinding to do this than it is to copy the value by hand. - */ - binding = g_object_bind_property (priv->edit_setting, property, - priv->orig_setting, property, - G_BINDING_SYNC_CREATE); - g_object_unref (binding); + g_object_get (priv->edit_setting, + NM_SETTING_IP_CONFIG_ROUTES, &routes, + NULL); + g_object_set (priv->orig_setting, + NM_SETTING_IP_CONFIG_ROUTES, routes, + NULL); + g_ptr_array_unref (routes); nmt_newt_form_quit (NMT_NEWT_FORM (editor)); } @@ -106,17 +100,13 @@ nmt_route_editor_constructed (GObject *object) if (G_OBJECT_CLASS (nmt_route_editor_parent_class)->constructed) G_OBJECT_CLASS (nmt_route_editor_parent_class)->constructed (object); - if (NM_IS_SETTING_IP4_CONFIG (priv->edit_setting)) { + if (NM_IS_SETTING_IP4_CONFIG (priv->edit_setting)) routes = nmt_route_table_new (AF_INET); - g_object_bind_property (priv->edit_setting, NM_SETTING_IP4_CONFIG_ROUTES, - routes, "routes", - G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL); - } else { + else routes = nmt_route_table_new (AF_INET6); - g_object_bind_property (priv->edit_setting, NM_SETTING_IP6_CONFIG_ROUTES, - routes, "routes", - G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL); - } + g_object_bind_property (priv->edit_setting, NM_SETTING_IP_CONFIG_ROUTES, + routes, "routes", + G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL); vbox = nmt_newt_grid_new (); nmt_newt_grid_add (NMT_NEWT_GRID (vbox), routes, 0, 0); diff --git a/examples/C/glib/add-connection-gdbus.c b/examples/C/glib/add-connection-gdbus.c index a7874fc51f..7a4d593945 100644 --- a/examples/C/glib/add-connection-gdbus.c +++ b/examples/C/glib/add-connection-gdbus.c @@ -86,7 +86,7 @@ add_connection (GDBusProxy *proxy, const char *con_name) /* Build up the 'ipv4' Setting */ g_variant_builder_init (&setting_builder, G_VARIANT_TYPE ("a{sv}")); g_variant_builder_add (&setting_builder, "{sv}", - NM_SETTING_IP4_CONFIG_METHOD, + NM_SETTING_IP_CONFIG_METHOD, g_variant_new_string (NM_SETTING_IP4_CONFIG_METHOD_AUTO)); g_variant_builder_add (&connection_builder, "{sa{sv}}", NM_SETTING_IP4_CONFIG_SETTING_NAME, diff --git a/examples/C/glib/add-connection-libnm.c b/examples/C/glib/add-connection-libnm.c index 1288e79c51..f206340e0c 100644 --- a/examples/C/glib/add-connection-libnm.c +++ b/examples/C/glib/add-connection-libnm.c @@ -86,7 +86,7 @@ add_connection (NMClient *client, GMainLoop *loop, const char *con_name) /* Build up the 'ipv4' Setting */ s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); diff --git a/libnm-core/nm-connection.c b/libnm-core/nm-connection.c index a6789b3195..e2c06c1765 100644 --- a/libnm-core/nm-connection.c +++ b/libnm-core/nm-connection.c @@ -631,8 +631,7 @@ _normalize_ip_config (NMConnection *self, GHashTable *parameters) NMSettingConnection *s_con = nm_connection_get_setting_connection (self); const char *default_ip4_method = NM_SETTING_IP4_CONFIG_METHOD_AUTO; const char *default_ip6_method = NULL; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4, *s_ip6; NMSetting *setting; if (parameters) @@ -663,7 +662,7 @@ _normalize_ip_config (NMConnection *self, GHashTable *parameters) setting = nm_setting_ip4_config_new (); g_object_set (setting, - NM_SETTING_IP4_CONFIG_METHOD, default_ip4_method, + NM_SETTING_IP_CONFIG_METHOD, default_ip4_method, NULL); nm_connection_add_setting (self, setting); } @@ -671,8 +670,8 @@ _normalize_ip_config (NMConnection *self, GHashTable *parameters) setting = nm_setting_ip6_config_new (); g_object_set (setting, - NM_SETTING_IP6_CONFIG_METHOD, default_ip6_method, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, default_ip6_method, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); nm_connection_add_setting (self, setting); } @@ -740,8 +739,7 @@ _nm_connection_verify (NMConnection *connection, GError **error) { NMConnectionPrivate *priv; NMSettingConnection *s_con; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4, *s_ip6; GHashTableIter iter; gpointer value; GSList *all_settings = NULL, *setting_i; @@ -1739,14 +1737,19 @@ nm_connection_get_setting_infiniband (NMConnection *connection) * * A shortcut to return any #NMSettingIP4Config the connection might contain. * - * Returns: (transfer none): an #NMSettingIP4Config if the connection contains one, otherwise %NULL + * Note that it returns the value as type #NMSettingIPConfig, since the vast + * majority of IPv4-setting-related methods are on that type, not + * #NMSettingIP4Config. + * + * Returns: (type NMSettingIP4Config) (transfer none): an #NMSettingIP4Config if the + * connection contains one, otherwise %NULL **/ -NMSettingIP4Config * +NMSettingIPConfig * nm_connection_get_setting_ip4_config (NMConnection *connection) { g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL); - return (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG); + return (NMSettingIPConfig *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG); } /** @@ -1755,14 +1758,19 @@ nm_connection_get_setting_ip4_config (NMConnection *connection) * * A shortcut to return any #NMSettingIP6Config the connection might contain. * - * Returns: (transfer none): an #NMSettingIP6Config if the connection contains one, otherwise %NULL + * Note that it returns the value as type #NMSettingIPConfig, since the vast + * majority of IPv6-setting-related methods are on that type, not + * #NMSettingIP6Config. + * + * Returns: (type NMSettingIP6Config) (transfer none): an #NMSettingIP6Config if the + * connection contains one, otherwise %NULL **/ -NMSettingIP6Config * +NMSettingIPConfig * nm_connection_get_setting_ip6_config (NMConnection *connection) { g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL); - return (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG); + return (NMSettingIPConfig *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG); } /** diff --git a/libnm-core/nm-connection.h b/libnm-core/nm-connection.h index ec5db7fa2b..2f9883c33d 100644 --- a/libnm-core/nm-connection.h +++ b/libnm-core/nm-connection.h @@ -193,8 +193,8 @@ NMSettingDcb * nm_connection_get_setting_dcb (NMConnec NMSettingGeneric * nm_connection_get_setting_generic (NMConnection *connection); NMSettingGsm * nm_connection_get_setting_gsm (NMConnection *connection); NMSettingInfiniband * nm_connection_get_setting_infiniband (NMConnection *connection); -NMSettingIP4Config * nm_connection_get_setting_ip4_config (NMConnection *connection); -NMSettingIP6Config * nm_connection_get_setting_ip6_config (NMConnection *connection); +NMSettingIPConfig * nm_connection_get_setting_ip4_config (NMConnection *connection); +NMSettingIPConfig * nm_connection_get_setting_ip6_config (NMConnection *connection); NMSettingOlpcMesh * nm_connection_get_setting_olpc_mesh (NMConnection *connection); NMSettingPpp * nm_connection_get_setting_ppp (NMConnection *connection); NMSettingPppoe * nm_connection_get_setting_pppoe (NMConnection *connection); diff --git a/libnm-core/nm-core-types.h b/libnm-core/nm-core-types.h index a6fc529fae..524d6396c5 100644 --- a/libnm-core/nm-core-types.h +++ b/libnm-core/nm-core-types.h @@ -40,6 +40,7 @@ typedef struct _NMSettingDcb NMSettingDcb; typedef struct _NMSettingGeneric NMSettingGeneric; typedef struct _NMSettingGsm NMSettingGsm; typedef struct _NMSettingInfiniband NMSettingInfiniband; +typedef struct _NMSettingIPConfig NMSettingIPConfig; typedef struct _NMSettingIP4Config NMSettingIP4Config; typedef struct _NMSettingIP6Config NMSettingIP6Config; typedef struct _NMSettingOlpcMesh NMSettingOlpcMesh; diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c index 42857dca00..ebe38712b2 100644 --- a/libnm-core/nm-setting-ip-config.c +++ b/libnm-core/nm-setting-ip-config.c @@ -21,14 +21,29 @@ */ #include +#include #include #include "nm-setting-ip-config.h" +#include "nm-setting-ip4-config.h" +#include "nm-setting-ip6-config.h" #include "nm-utils.h" #include "nm-glib-compat.h" #include "nm-setting-private.h" #include "nm-utils-private.h" +/** + * SECTION:nm-setting-ip-config + * @short_description: Abstract base class for IPv4 and IPv6 + * addressing, routing, and name service properties + * @include: nm-setting-ip-config.h + * @see_also: #NMSettingIP4Config, #NMSettingIP6Config + * + * #NMSettingIPConfig is the abstract base class of + * #NMSettingIP4Config and #NMSettingIP6Config, providing properties + * related to IP addressing, routing, and Domain Name Service. + **/ + static char * canonicalize_ip (int family, const char *ip, gboolean null_any) { @@ -1054,3 +1069,1164 @@ nm_ip_route_set_attribute (NMIPRoute *route, const char *name, GVariant *value) else g_hash_table_remove (route->attributes, name); } + + +G_DEFINE_ABSTRACT_TYPE (NMSettingIPConfig, nm_setting_ip_config, NM_TYPE_SETTING) + +#define NM_SETTING_IP_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_IP_CONFIG, NMSettingIPConfigPrivate)) + +typedef struct { + char *method; + GPtrArray *dns; /* array of IP address strings */ + GPtrArray *dns_search; /* array of domain name strings */ + GPtrArray *addresses; /* array of NMIPAddress */ + GPtrArray *routes; /* array of NMIPRoute */ + gboolean ignore_auto_routes; + gboolean ignore_auto_dns; + char *dhcp_hostname; + gboolean dhcp_send_hostname; + gboolean never_default; + gboolean may_fail; +} NMSettingIPConfigPrivate; + +enum { + PROP_0, + PROP_METHOD, + PROP_DNS, + PROP_DNS_SEARCH, + PROP_ADDRESSES, + PROP_ROUTES, + PROP_IGNORE_AUTO_ROUTES, + PROP_IGNORE_AUTO_DNS, + PROP_DHCP_HOSTNAME, + PROP_DHCP_SEND_HOSTNAME, + PROP_NEVER_DEFAULT, + PROP_MAY_FAIL, + + LAST_PROP +}; + +#define NM_SETTING_IP_CONFIG_GET_FAMILY(setting) (NM_IS_SETTING_IP4_CONFIG (setting) ? AF_INET : AF_INET6) + +/** + * nm_setting_ip_config_get_method: + * @setting: the #NMSettingIPConfig + * + * Returns: the #NMSettingIPConfig:method property of the setting; see + * #NMSettingIP4Config and #NMSettingIP6Config for details of the + * methods available with each type. + **/ +const char * +nm_setting_ip_config_get_method (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), NULL); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->method; +} + +/** + * nm_setting_ip_config_get_num_dns: + * @setting: the #NMSettingIPConfig + * + * Returns: the number of configured DNS servers + **/ +guint +nm_setting_ip_config_get_num_dns (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), 0); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->dns->len; +} + +/** + * nm_setting_ip_config_get_dns: + * @setting: the #NMSettingIPConfig + * @i: index number of the DNS server to return + * + * Returns: the IP address of the DNS server at index @i + **/ +const char * +nm_setting_ip_config_get_dns (NMSettingIPConfig *setting, int i) +{ + NMSettingIPConfigPrivate *priv; + + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), NULL); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + g_return_val_if_fail (i < priv->dns->len, NULL); + + return priv->dns->pdata[i]; +} + +/** + * nm_setting_ip_config_add_dns: + * @setting: the #NMSettingIPConfig + * @dns: the IP address of the DNS server to add + * + * Adds a new DNS server to the setting. + * + * Returns: %TRUE if the DNS server was added; %FALSE if the server was already + * known + **/ +gboolean +nm_setting_ip_config_add_dns (NMSettingIPConfig *setting, const char *dns) +{ + NMSettingIPConfigPrivate *priv; + char *dns_canonical; + int i; + + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + g_return_val_if_fail (dns != NULL, FALSE); + g_return_val_if_fail (nm_utils_ipaddr_valid (NM_SETTING_IP_CONFIG_GET_FAMILY (setting), dns), FALSE); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + + dns_canonical = canonicalize_ip (NM_SETTING_IP_CONFIG_GET_FAMILY (setting), dns, FALSE); + for (i = 0; i < priv->dns->len; i++) { + if (!strcmp (dns_canonical, priv->dns->pdata[i])) { + g_free (dns_canonical); + return FALSE; + } + } + + g_ptr_array_add (priv->dns, dns_canonical); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_DNS); + return TRUE; +} + +/** + * nm_setting_ip_config_remove_dns: + * @setting: the #NMSettingIPConfig + * @i: index number of the DNS server to remove + * + * Removes the DNS server at index @i. + **/ +void +nm_setting_ip_config_remove_dns (NMSettingIPConfig *setting, int i) +{ + NMSettingIPConfigPrivate *priv; + + g_return_if_fail (NM_IS_SETTING_IP_CONFIG (setting)); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + g_return_if_fail (i < priv->dns->len); + + g_ptr_array_remove_index (priv->dns, i); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_DNS); +} + +/** + * nm_setting_ip_config_remove_dns_by_value: + * @setting: the #NMSettingIPConfig + * @dns: the DNS server to remove + * + * Removes the DNS server @dns. + * + * Returns: %TRUE if the DNS server was found and removed; %FALSE if it was not. + **/ +gboolean +nm_setting_ip_config_remove_dns_by_value (NMSettingIPConfig *setting, const char *dns) +{ + NMSettingIPConfigPrivate *priv; + char *dns_canonical; + int i; + + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + g_return_val_if_fail (dns != NULL, FALSE); + g_return_val_if_fail (nm_utils_ipaddr_valid (NM_SETTING_IP_CONFIG_GET_FAMILY (setting), dns), FALSE); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + + dns_canonical = canonicalize_ip (NM_SETTING_IP_CONFIG_GET_FAMILY (setting), dns, FALSE); + for (i = 0; i < priv->dns->len; i++) { + if (!strcmp (dns_canonical, priv->dns->pdata[i])) { + g_ptr_array_remove_index (priv->dns, i); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_DNS); + g_free (dns_canonical); + return TRUE; + } + } + g_free (dns_canonical); + return FALSE; +} + +/** + * nm_setting_ip_config_clear_dns: + * @setting: the #NMSettingIPConfig + * + * Removes all configured DNS servers. + **/ +void +nm_setting_ip_config_clear_dns (NMSettingIPConfig *setting) +{ + NMSettingIPConfigPrivate *priv; + + g_return_if_fail (NM_IS_SETTING_IP_CONFIG (setting)); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + g_ptr_array_set_size (priv->dns, 0); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_DNS); +} + +/** + * nm_setting_ip_config_get_num_dns_searches: + * @setting: the #NMSettingIPConfig + * + * Returns: the number of configured DNS search domains + **/ +guint +nm_setting_ip_config_get_num_dns_searches (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), 0); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->dns_search->len; +} + +/** + * nm_setting_ip_config_get_dns_search: + * @setting: the #NMSettingIPConfig + * @i: index number of the DNS search domain to return + * + * Returns: the DNS search domain at index @i + **/ +const char * +nm_setting_ip_config_get_dns_search (NMSettingIPConfig *setting, int i) +{ + NMSettingIPConfigPrivate *priv; + + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), NULL); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + g_return_val_if_fail (i < priv->dns_search->len, NULL); + + return priv->dns_search->pdata[i]; +} + +/** + * nm_setting_ip_config_add_dns_search: + * @setting: the #NMSettingIPConfig + * @dns_search: the search domain to add + * + * Adds a new DNS search domain to the setting. + * + * Returns: %TRUE if the DNS search domain was added; %FALSE if the search + * domain was already known + **/ +gboolean +nm_setting_ip_config_add_dns_search (NMSettingIPConfig *setting, + const char *dns_search) +{ + NMSettingIPConfigPrivate *priv; + int i; + + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + g_return_val_if_fail (dns_search != NULL, FALSE); + g_return_val_if_fail (dns_search[0] != '\0', FALSE); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + for (i = 0; i < priv->dns_search->len; i++) { + if (!strcmp (dns_search, priv->dns_search->pdata[i])) + return FALSE; + } + + g_ptr_array_add (priv->dns_search, g_strdup (dns_search)); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_DNS_SEARCH); + return TRUE; +} + +/** + * nm_setting_ip_config_remove_dns_search: + * @setting: the #NMSettingIPConfig + * @i: index number of the DNS search domain + * + * Removes the DNS search domain at index @i. + **/ +void +nm_setting_ip_config_remove_dns_search (NMSettingIPConfig *setting, int i) +{ + NMSettingIPConfigPrivate *priv; + + g_return_if_fail (NM_IS_SETTING_IP_CONFIG (setting)); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + g_return_if_fail (i < priv->dns_search->len); + + g_ptr_array_remove_index (priv->dns_search, i); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_DNS_SEARCH); +} + +/** + * nm_setting_ip_config_remove_dns_search_by_value: + * @setting: the #NMSettingIPConfig + * @dns_search: the search domain to remove + * + * Removes the DNS search domain @dns_search. + * + * Returns: %TRUE if the DNS search domain was found and removed; %FALSE if it was not. + * + * Since 0.9.10 + **/ +gboolean +nm_setting_ip_config_remove_dns_search_by_value (NMSettingIPConfig *setting, + const char *dns_search) +{ + NMSettingIPConfigPrivate *priv; + int i; + + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + g_return_val_if_fail (dns_search != NULL, FALSE); + g_return_val_if_fail (dns_search[0] != '\0', FALSE); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + for (i = 0; i < priv->dns_search->len; i++) { + if (!strcmp (dns_search, priv->dns_search->pdata[i])) { + g_ptr_array_remove_index (priv->dns_search, i); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_DNS_SEARCH); + return TRUE; + } + } + return FALSE; +} + +/** + * nm_setting_ip_config_clear_dns_searches: + * @setting: the #NMSettingIPConfig + * + * Removes all configured DNS search domains. + **/ +void +nm_setting_ip_config_clear_dns_searches (NMSettingIPConfig *setting) +{ + NMSettingIPConfigPrivate *priv; + + g_return_if_fail (NM_IS_SETTING_IP_CONFIG (setting)); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + g_ptr_array_set_size (priv->dns_search, 0); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_DNS_SEARCH); +} + +/** + * nm_setting_ip_config_get_num_addresses: + * @setting: the #NMSettingIPConfig + * + * Returns: the number of configured addresses + **/ +guint +nm_setting_ip_config_get_num_addresses (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), 0); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->addresses->len; +} + +/** + * nm_setting_ip_config_get_address: + * @setting: the #NMSettingIPConfig + * @i: index number of the address to return + * + * Returns: the address at index @i + **/ +NMIPAddress * +nm_setting_ip_config_get_address (NMSettingIPConfig *setting, int i) +{ + NMSettingIPConfigPrivate *priv; + + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), NULL); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + g_return_val_if_fail (i < priv->addresses->len, NULL); + + return priv->addresses->pdata[i]; +} + +/** + * nm_setting_ip_config_add_address: + * @setting: the #NMSettingIPConfig + * @address: the new address to add + * + * Adds a new IP address and associated information to the setting. The + * given address is duplicated internally and is not changed by this function. + * + * Returns: %TRUE if the address was added; %FALSE if the address was already + * known. + **/ +gboolean +nm_setting_ip_config_add_address (NMSettingIPConfig *setting, + NMIPAddress *address) +{ + NMSettingIPConfigPrivate *priv; + int i; + + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + g_return_val_if_fail (address != NULL, FALSE); + g_return_val_if_fail (address->family == NM_SETTING_IP_CONFIG_GET_FAMILY (setting), FALSE); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + for (i = 0; i < priv->addresses->len; i++) { + if (nm_ip_address_equal (priv->addresses->pdata[i], address)) + return FALSE; + } + + g_ptr_array_add (priv->addresses, nm_ip_address_dup (address)); + + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_ADDRESSES); + return TRUE; +} + +/** + * nm_setting_ip_config_remove_address: + * @setting: the #NMSettingIPConfig + * @i: index number of the address to remove + * + * Removes the address at index @i. + **/ +void +nm_setting_ip_config_remove_address (NMSettingIPConfig *setting, int i) +{ + NMSettingIPConfigPrivate *priv; + + g_return_if_fail (NM_IS_SETTING_IP_CONFIG (setting)); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + g_return_if_fail (i < priv->addresses->len); + + g_ptr_array_remove_index (priv->addresses, i); + + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_ADDRESSES); +} + +/** + * nm_setting_ip_config_remove_address_by_value: + * @setting: the #NMSettingIPConfig + * @address: the IP address to remove + * + * Removes the address @address. + * + * Returns: %TRUE if the address was found and removed; %FALSE if it was not. + **/ +gboolean +nm_setting_ip_config_remove_address_by_value (NMSettingIPConfig *setting, + NMIPAddress *address) +{ + NMSettingIPConfigPrivate *priv; + int i; + + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + g_return_val_if_fail (address != NULL, FALSE); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + for (i = 0; i < priv->addresses->len; i++) { + if (nm_ip_address_equal (priv->addresses->pdata[i], address)) { + g_ptr_array_remove_index (priv->addresses, i); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_ADDRESSES); + return TRUE; + } + } + return FALSE; +} + +/** + * nm_setting_ip_config_clear_addresses: + * @setting: the #NMSettingIPConfig + * + * Removes all configured addresses. + **/ +void +nm_setting_ip_config_clear_addresses (NMSettingIPConfig *setting) +{ + NMSettingIPConfigPrivate *priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + + g_return_if_fail (NM_IS_SETTING_IP_CONFIG (setting)); + + g_ptr_array_set_size (priv->addresses, 0); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_ADDRESSES); +} + +/** + * nm_setting_ip_config_get_num_routes: + * @setting: the #NMSettingIPConfig + * + * Returns: the number of configured routes + **/ +guint +nm_setting_ip_config_get_num_routes (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), 0); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->routes->len; +} + +/** + * nm_setting_ip_config_get_route: + * @setting: the #NMSettingIPConfig + * @i: index number of the route to return + * + * Returns: the route at index @i + **/ +NMIPRoute * +nm_setting_ip_config_get_route (NMSettingIPConfig *setting, int i) +{ + NMSettingIPConfigPrivate *priv; + + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), NULL); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + g_return_val_if_fail (i < priv->routes->len, NULL); + + return priv->routes->pdata[i]; +} + +/** + * nm_setting_ip_config_add_route: + * @setting: the #NMSettingIPConfig + * @route: the route to add + * + * Adds a new route and associated information to the setting. The + * given route is duplicated internally and is not changed by this function. + * + * Returns: %TRUE if the route was added; %FALSE if the route was already known. + **/ +gboolean +nm_setting_ip_config_add_route (NMSettingIPConfig *setting, + NMIPRoute *route) +{ + NMSettingIPConfigPrivate *priv; + int i; + + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + g_return_val_if_fail (route != NULL, FALSE); + g_return_val_if_fail (route->family == NM_SETTING_IP_CONFIG_GET_FAMILY (setting), FALSE); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + for (i = 0; i < priv->routes->len; i++) { + if (nm_ip_route_equal (priv->routes->pdata[i], route)) + return FALSE; + } + + g_ptr_array_add (priv->routes, nm_ip_route_dup (route)); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_ROUTES); + return TRUE; +} + +/** + * nm_setting_ip_config_remove_route: + * @setting: the #NMSettingIPConfig + * @i: index number of the route + * + * Removes the route at index @i. + **/ +void +nm_setting_ip_config_remove_route (NMSettingIPConfig *setting, int i) +{ + NMSettingIPConfigPrivate *priv; + + g_return_if_fail (NM_IS_SETTING_IP_CONFIG (setting)); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + g_return_if_fail (i < priv->routes->len); + + g_ptr_array_remove_index (priv->routes, i); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_ROUTES); +} + +/** + * nm_setting_ip_config_remove_route_by_value: + * @setting: the #NMSettingIPConfig + * @route: the route to remove + * + * Removes the route @route. + * + * Returns: %TRUE if the route was found and removed; %FALSE if it was not. + **/ +gboolean +nm_setting_ip_config_remove_route_by_value (NMSettingIPConfig *setting, + NMIPRoute *route) +{ + NMSettingIPConfigPrivate *priv; + int i; + + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + g_return_val_if_fail (route != NULL, FALSE); + + priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + for (i = 0; i < priv->routes->len; i++) { + if (nm_ip_route_equal (priv->routes->pdata[i], route)) { + g_ptr_array_remove_index (priv->routes, i); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_ROUTES); + return TRUE; + } + } + return FALSE; +} + +/** + * nm_setting_ip_config_clear_routes: + * @setting: the #NMSettingIPConfig + * + * Removes all configured routes. + **/ +void +nm_setting_ip_config_clear_routes (NMSettingIPConfig *setting) +{ + NMSettingIPConfigPrivate *priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + + g_return_if_fail (NM_IS_SETTING_IP_CONFIG (setting)); + + g_ptr_array_set_size (priv->routes, 0); + g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_ROUTES); +} + +/** + * nm_setting_ip_config_get_ignore_auto_routes: + * @setting: the #NMSettingIPConfig + * + * Returns the value contained in the #NMSettingIPConfig:ignore-auto-routes + * property. + * + * Returns: %TRUE if automatically configured (ie via DHCP) routes should be + * ignored. + **/ +gboolean +nm_setting_ip_config_get_ignore_auto_routes (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->ignore_auto_routes; +} + +/** + * nm_setting_ip_config_get_ignore_auto_dns: + * @setting: the #NMSettingIPConfig + * + * Returns the value contained in the #NMSettingIPConfig:ignore-auto-dns + * property. + * + * Returns: %TRUE if automatically configured (ie via DHCP) DNS information + * should be ignored. + **/ +gboolean +nm_setting_ip_config_get_ignore_auto_dns (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->ignore_auto_dns; +} + +/** + * nm_setting_ip_config_get_dhcp_hostname: + * @setting: the #NMSettingIPConfig + * + * Returns the value contained in the #NMSettingIPConfig:dhcp-hostname + * property. + * + * Returns: the configured hostname to send to the DHCP server + **/ +const char * +nm_setting_ip_config_get_dhcp_hostname (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), NULL); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->dhcp_hostname; +} + +/** + * nm_setting_ip_config_get_dhcp_send_hostname: + * @setting: the #NMSettingIPConfig + * + * Returns the value contained in the #NMSettingIPConfig:dhcp-send-hostname + * property. + * + * Returns: %TRUE if NetworkManager should send the machine hostname to the + * DHCP server when requesting addresses to allow the server to automatically + * update DNS information for this machine. + **/ +gboolean +nm_setting_ip_config_get_dhcp_send_hostname (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->dhcp_send_hostname; +} + +/** + * nm_setting_ip_config_get_never_default: + * @setting: the #NMSettingIPConfig + * + * Returns the value contained in the #NMSettingIPConfig:never-default + * property. + * + * Returns: %TRUE if this connection should never be the default + * connection + **/ +gboolean +nm_setting_ip_config_get_never_default (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->never_default; +} + +/** + * nm_setting_ip_config_get_may_fail: + * @setting: the #NMSettingIPConfig + * + * Returns the value contained in the #NMSettingIPConfig:may-fail + * property. + * + * Returns: %TRUE if this connection doesn't require this type of IP + * addressing to complete for the connection to succeed. + **/ +gboolean +nm_setting_ip_config_get_may_fail (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), FALSE); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->may_fail; +} + +static gboolean +verify_label (const char *label) +{ + const char *p; + char *iface; + + p = strchr (label, ':'); + if (!p) + return FALSE; + iface = g_strndup (label, p - label); + if (!nm_utils_iface_valid_name (iface)) { + g_free (iface); + return FALSE; + } + g_free (iface); + + for (p++; *p; p++) { + if (!g_ascii_isalnum (*p) && *p != '_') + return FALSE; + } + + return TRUE; +} + +static gboolean +verify (NMSetting *setting, NMConnection *connection, GError **error) +{ + NMSettingIPConfigPrivate *priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + int i; + + if (!priv->method) { + g_set_error_literal (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_PROPERTY, + _("property is missing")); + g_prefix_error (error, "%s.%s: ", nm_setting_get_name (setting), NM_SETTING_IP_CONFIG_METHOD); + return FALSE; + } + + if (priv->dhcp_hostname && !*priv->dhcp_hostname) { + g_set_error_literal (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("property is empty")); + g_prefix_error (error, "%s.%s: ", nm_setting_get_name (setting), NM_SETTING_IP_CONFIG_DHCP_HOSTNAME); + return FALSE; + } + + /* Validate DNS */ + for (i = 0; i < priv->dns->len; i++) { + const char *dns = priv->dns->pdata[i]; + + if (!nm_utils_ipaddr_valid (NM_SETTING_IP_CONFIG_GET_FAMILY (setting), dns)) { + g_set_error (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("%d. DNS server address is invalid"), + i+1); + g_prefix_error (error, "%s.%s: ", nm_setting_get_name (setting), NM_SETTING_IP_CONFIG_DNS); + return FALSE; + } + } + + /* Validate addresses */ + for (i = 0; i < priv->addresses->len; i++) { + NMIPAddress *addr = (NMIPAddress *) priv->addresses->pdata[i]; + GVariant *label; + + if (nm_ip_address_get_family (addr) != NM_SETTING_IP_CONFIG_GET_FAMILY (setting)) { + g_set_error (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("%d. IP address is invalid"), + i+1); + g_prefix_error (error, "%s.%s: ", nm_setting_get_name (setting), NM_SETTING_IP_CONFIG_ADDRESSES); + return FALSE; + } + + label = nm_ip_address_get_attribute (addr, "label"); + if (label) { + if (!g_variant_is_of_type (label, G_VARIANT_TYPE_STRING)) { + g_set_error (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("%d. IP address has 'label' property with invalid type"), + i+1); + g_prefix_error (error, "%s.%s: ", nm_setting_get_name (setting), NM_SETTING_IP_CONFIG_ADDRESSES); + return FALSE; + } + if (!verify_label (g_variant_get_string (label, NULL))) { + g_set_error (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("%d. IP address has invalid label '%s'"), + i+1, g_variant_get_string (label, NULL)); + g_prefix_error (error, "%s.%s: ", nm_setting_get_name (setting), NM_SETTING_IP_CONFIG_ADDRESSES); + return FALSE; + } + } + } + + /* Validate routes */ + for (i = 0; i < priv->routes->len; i++) { + NMIPRoute *route = (NMIPRoute *) priv->routes->pdata[i]; + + if (nm_ip_route_get_family (route) != NM_SETTING_IP_CONFIG_GET_FAMILY (setting)) { + g_set_error (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("%d. route is invalid"), + i+1); + g_prefix_error (error, "%s.%s: ", nm_setting_get_name (setting), NM_SETTING_IP_CONFIG_ROUTES); + return FALSE; + } + } + + return TRUE; +} + + +static void +nm_setting_ip_config_init (NMSettingIPConfig *setting) +{ + NMSettingIPConfigPrivate *priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + + priv->dns = g_ptr_array_new_with_free_func (g_free); + priv->dns_search = g_ptr_array_new_with_free_func (g_free); + priv->addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_address_unref); + priv->routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref); +} + +static void +finalize (GObject *object) +{ + NMSettingIPConfig *self = NM_SETTING_IP_CONFIG (object); + NMSettingIPConfigPrivate *priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (self); + + g_free (priv->method); + g_free (priv->dhcp_hostname); + + g_ptr_array_unref (priv->dns); + g_ptr_array_unref (priv->dns_search); + g_ptr_array_unref (priv->addresses); + g_ptr_array_unref (priv->routes); + + G_OBJECT_CLASS (nm_setting_ip_config_parent_class)->finalize (object); +} + +static void +set_property (GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec) +{ + NMSettingIPConfig *setting = NM_SETTING_IP_CONFIG (object); + NMSettingIPConfigPrivate *priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + + switch (prop_id) { + case PROP_METHOD: + g_free (priv->method); + priv->method = g_value_dup_string (value); + break; + case PROP_DNS: + g_ptr_array_unref (priv->dns); + priv->dns = _nm_utils_strv_to_ptrarray (g_value_get_boxed (value)); + break; + case PROP_DNS_SEARCH: + g_ptr_array_unref (priv->dns_search); + priv->dns_search = _nm_utils_strv_to_ptrarray (g_value_get_boxed (value)); + break; + case PROP_ADDRESSES: + g_ptr_array_unref (priv->addresses); + priv->addresses = _nm_utils_copy_array (g_value_get_boxed (value), + (NMUtilsCopyFunc) nm_ip_address_dup, + (GDestroyNotify) nm_ip_address_unref); + break; + case PROP_ROUTES: + g_ptr_array_unref (priv->routes); + priv->routes = _nm_utils_copy_array (g_value_get_boxed (value), + (NMUtilsCopyFunc) nm_ip_route_dup, + (GDestroyNotify) nm_ip_route_unref); + break; + case PROP_IGNORE_AUTO_ROUTES: + priv->ignore_auto_routes = g_value_get_boolean (value); + break; + case PROP_IGNORE_AUTO_DNS: + priv->ignore_auto_dns = g_value_get_boolean (value); + break; + case PROP_DHCP_HOSTNAME: + g_free (priv->dhcp_hostname); + priv->dhcp_hostname = g_value_dup_string (value); + break; + case PROP_DHCP_SEND_HOSTNAME: + priv->dhcp_send_hostname = g_value_get_boolean (value); + break; + case PROP_NEVER_DEFAULT: + priv->never_default = g_value_get_boolean (value); + break; + case PROP_MAY_FAIL: + priv->may_fail = g_value_get_boolean (value); + 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) +{ + NMSettingIPConfig *setting = NM_SETTING_IP_CONFIG (object); + NMSettingIPConfigPrivate *priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + + switch (prop_id) { + case PROP_METHOD: + g_value_set_string (value, nm_setting_ip_config_get_method (setting)); + break; + case PROP_DNS: + g_value_take_boxed (value, _nm_utils_ptrarray_to_strv (priv->dns)); + break; + case PROP_DNS_SEARCH: + g_value_take_boxed (value, _nm_utils_ptrarray_to_strv (priv->dns_search)); + break; + case PROP_ADDRESSES: + g_value_take_boxed (value, _nm_utils_copy_array (priv->addresses, + (NMUtilsCopyFunc) nm_ip_address_dup, + (GDestroyNotify) nm_ip_address_unref)); + break; + case PROP_ROUTES: + g_value_take_boxed (value, _nm_utils_copy_array (priv->routes, + (NMUtilsCopyFunc) nm_ip_route_dup, + (GDestroyNotify) nm_ip_route_unref)); + break; + case PROP_IGNORE_AUTO_ROUTES: + g_value_set_boolean (value, nm_setting_ip_config_get_ignore_auto_routes (setting)); + break; + case PROP_IGNORE_AUTO_DNS: + g_value_set_boolean (value, nm_setting_ip_config_get_ignore_auto_dns (setting)); + break; + case PROP_DHCP_HOSTNAME: + g_value_set_string (value, nm_setting_ip_config_get_dhcp_hostname (setting)); + break; + case PROP_DHCP_SEND_HOSTNAME: + g_value_set_boolean (value, nm_setting_ip_config_get_dhcp_send_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 +nm_setting_ip_config_class_init (NMSettingIPConfigClass *setting_class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (setting_class); + NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class); + + g_type_class_add_private (setting_class, sizeof (NMSettingIPConfigPrivate)); + + /* virtual methods */ + object_class->set_property = set_property; + object_class->get_property = get_property; + object_class->finalize = finalize; + parent_class->verify = verify; + + /* Properties */ + + /** + * NMSettingIPConfig:method: + * + * IP configuration method. + * + * #NMSettingIP4Config and #NMSettingIP6Config both support "auto", + * "manual", and "link-local". See the subclass-specific documentation for + * other values. + * + * In general, for the "auto" method, properties such as + * #NMSettingIPConfig:dns and #NMSettingIPConfig:routes specify information + * that is added on to the information returned from automatic + * configuration. The #NMSettingIPConfig:ignore-auto-routes and + * #NMSettingIPConfig:ignore-auto-dns properties modify this behavior. + * + * For methods that imply no upstream network, such as "shared" or + * "link-local", these properties must be empty. + **/ + g_object_class_install_property + (object_class, PROP_METHOD, + g_param_spec_string (NM_SETTING_IP_CONFIG_METHOD, "", "", + NULL, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMSettingIPConfig:dns: + * + * Array of IP addresses of DNS servers. + **/ + g_object_class_install_property + (object_class, PROP_DNS, + g_param_spec_boxed (NM_SETTING_IP_CONFIG_DNS, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMSettingIPConfig:dns-search: + * + * Array of DNS search domains. + **/ + g_object_class_install_property + (object_class, PROP_DNS_SEARCH, + g_param_spec_boxed (NM_SETTING_IP_CONFIG_DNS_SEARCH, "", "", + G_TYPE_STRV, + G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMSettingIPConfig:addresses: + * + * Array of IP addresses. + * + * Element-Type: NMIPAddress + **/ + g_object_class_install_property + (object_class, PROP_ADDRESSES, + g_param_spec_boxed (NM_SETTING_IP_CONFIG_ADDRESSES, "", "", + G_TYPE_PTR_ARRAY, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMSettingIPConfig:routes: + * + * Array of IP routes. + * + * Element-Type: NMIPRoute + **/ + g_object_class_install_property + (object_class, PROP_ROUTES, + g_param_spec_boxed (NM_SETTING_IP_CONFIG_ROUTES, "", "", + G_TYPE_PTR_ARRAY, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMSettingIPConfig:ignore-auto-routes: + * + * When #NMSettingIPConfig:method is set to "auto" and this property to + * %TRUE, automatically configured routes are ignored and only routes + * specified in the #NMSettingIPConfig:routes property, if any, are used. + **/ + g_object_class_install_property + (object_class, PROP_IGNORE_AUTO_ROUTES, + g_param_spec_boolean (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, "", "", + FALSE, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); + + /** + * NMSettingIPConfig:ignore-auto-dns: + * + * When #NMSettingIPConfig:method is set to "auto" and this property to + * %TRUE, automatically configured nameservers and search domains are + * ignored and only nameservers and search domains specified in the + * #NMSettingIPConfig:dns and #NMSettingIPConfig:dns-search properties, if + * any, are used. + **/ + g_object_class_install_property + (object_class, PROP_IGNORE_AUTO_DNS, + g_param_spec_boolean (NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, "", "", + FALSE, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); + + /** + * NMSettingIPConfig:dhcp-hostname: + * + * If the #NMSettingIPConfig:dhcp-send-hostname property is %TRUE, then the + * specified name will be sent to the DHCP server when acquiring a lease. + **/ + g_object_class_install_property + (object_class, PROP_DHCP_HOSTNAME, + g_param_spec_string (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, "", "", + NULL, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMSettingIPConfig:dhcp-send-hostname: + * + * If %TRUE, a hostname is sent to the DHCP server when acquiring a lease. + * Some DHCP servers use this hostname to update DNS databases, essentially + * providing a static hostname for the computer. If the + * #NMSettingIPConfig:dhcp-hostname property is %NULL and this property is + * %TRUE, the current persistent hostname of the computer is sent. + **/ + g_object_class_install_property + (object_class, PROP_DHCP_SEND_HOSTNAME, + g_param_spec_boolean (NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, "", "", + TRUE, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); + + /** + * NMSettingIPConfig:never-default: + * + * If %TRUE, this connection will never be the default connection for this + * IP type, meaning it will never be assigned the default route by + * NetworkManager. + **/ + g_object_class_install_property + (object_class, PROP_NEVER_DEFAULT, + g_param_spec_boolean (NM_SETTING_IP_CONFIG_NEVER_DEFAULT, "", "", + FALSE, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); + + /** + * NMSettingIPConfig:may-fail: + * + * If %TRUE, allow overall network configuration to proceed even if the + * configuration specified by this property times out. Note that at least + * one IP configuration must succeed or overall network configuration will + * still fail. For example, in IPv6-only networks, setting this property to + * %TRUE on the #NMSettingIP4Config allows the overall network configuration + * to succeed if IPv4 configuration fails but IPv6 configuration completes + * successfully. + **/ + g_object_class_install_property + (object_class, PROP_MAY_FAIL, + g_param_spec_boolean (NM_SETTING_IP_CONFIG_MAY_FAIL, "", "", + TRUE, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); +} diff --git a/libnm-core/nm-setting-ip-config.h b/libnm-core/nm-setting-ip-config.h index 24c1cbda42..0f2d16367c 100644 --- a/libnm-core/nm-setting-ip-config.h +++ b/libnm-core/nm-setting-ip-config.h @@ -127,6 +127,94 @@ void nm_ip_route_set_attribute (NMIPRoute *route, const char *name, GVariant *value); + +#define NM_TYPE_SETTING_IP_CONFIG (nm_setting_ip_config_get_type ()) +#define NM_SETTING_IP_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_SETTING_IP_CONFIG, NMSettingIPConfig)) +#define NM_SETTING_IP_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_SETTING_IPCONFIG, NMSettingIPConfigClass)) +#define NM_IS_SETTING_IP_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_SETTING_IP_CONFIG)) +#define NM_IS_SETTING_IP_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_SETTING_IP_CONFIG)) +#define NM_SETTING_IP_CONFIG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_SETTING_IP_CONFIG, NMSettingIPConfigClass)) + +#define NM_SETTING_IP_CONFIG_METHOD "method" +#define NM_SETTING_IP_CONFIG_DNS "dns" +#define NM_SETTING_IP_CONFIG_DNS_SEARCH "dns-search" +#define NM_SETTING_IP_CONFIG_ADDRESSES "addresses" +#define NM_SETTING_IP_CONFIG_ROUTES "routes" +#define NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES "ignore-auto-routes" +#define NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS "ignore-auto-dns" +#define NM_SETTING_IP_CONFIG_DHCP_HOSTNAME "dhcp-hostname" +#define NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME "dhcp-send-hostname" +#define NM_SETTING_IP_CONFIG_NEVER_DEFAULT "never-default" +#define NM_SETTING_IP_CONFIG_MAY_FAIL "may-fail" + +struct _NMSettingIPConfig { + NMSetting parent; +}; + +typedef struct { + NMSettingClass parent; + + /* Padding for future expansion */ + gpointer padding[8]; +} NMSettingIPConfigClass; + +GType nm_setting_ip_config_get_type (void); + +const char *nm_setting_ip_config_get_method (NMSettingIPConfig *setting); + +guint nm_setting_ip_config_get_num_dns (NMSettingIPConfig *setting); +const char *nm_setting_ip_config_get_dns (NMSettingIPConfig *setting, + int i); +gboolean nm_setting_ip_config_add_dns (NMSettingIPConfig *setting, + const char *dns); +void nm_setting_ip_config_remove_dns (NMSettingIPConfig *setting, + int i); +gboolean nm_setting_ip_config_remove_dns_by_value (NMSettingIPConfig *setting, + const char *dns); +void nm_setting_ip_config_clear_dns (NMSettingIPConfig *setting); + +guint nm_setting_ip_config_get_num_dns_searches (NMSettingIPConfig *setting); +const char *nm_setting_ip_config_get_dns_search (NMSettingIPConfig *setting, + int i); +gboolean nm_setting_ip_config_add_dns_search (NMSettingIPConfig *setting, + const char *dns_search); +void nm_setting_ip_config_remove_dns_search (NMSettingIPConfig *setting, + int i); +gboolean nm_setting_ip_config_remove_dns_search_by_value (NMSettingIPConfig *setting, + const char *dns_search); +void nm_setting_ip_config_clear_dns_searches (NMSettingIPConfig *setting); + +guint nm_setting_ip_config_get_num_addresses (NMSettingIPConfig *setting); +NMIPAddress *nm_setting_ip_config_get_address (NMSettingIPConfig *setting, + int i); +gboolean nm_setting_ip_config_add_address (NMSettingIPConfig *setting, + NMIPAddress *address); +void nm_setting_ip_config_remove_address (NMSettingIPConfig *setting, + int i); +gboolean nm_setting_ip_config_remove_address_by_value (NMSettingIPConfig *setting, + NMIPAddress *address); +void nm_setting_ip_config_clear_addresses (NMSettingIPConfig *setting); + +guint nm_setting_ip_config_get_num_routes (NMSettingIPConfig *setting); +NMIPRoute *nm_setting_ip_config_get_route (NMSettingIPConfig *setting, + int i); +gboolean nm_setting_ip_config_add_route (NMSettingIPConfig *setting, + NMIPRoute *route); +void nm_setting_ip_config_remove_route (NMSettingIPConfig *setting, + int i); +gboolean nm_setting_ip_config_remove_route_by_value (NMSettingIPConfig *setting, + NMIPRoute *route); +void nm_setting_ip_config_clear_routes (NMSettingIPConfig *setting); + +gboolean nm_setting_ip_config_get_ignore_auto_routes (NMSettingIPConfig *setting); +gboolean nm_setting_ip_config_get_ignore_auto_dns (NMSettingIPConfig *setting); + +const char *nm_setting_ip_config_get_dhcp_hostname (NMSettingIPConfig *setting); +gboolean nm_setting_ip_config_get_dhcp_send_hostname (NMSettingIPConfig *setting); + +gboolean nm_setting_ip_config_get_never_default (NMSettingIPConfig *setting); +gboolean nm_setting_ip_config_get_may_fail (NMSettingIPConfig *setting); + G_END_DECLS #endif /* NM_SETTING_IP_CONFIG_H */ diff --git a/libnm-core/nm-setting-ip4-config.c b/libnm-core/nm-setting-ip4-config.c index e15dae0491..068ba0451a 100644 --- a/libnm-core/nm-setting-ip4-config.c +++ b/libnm-core/nm-setting-ip4-config.c @@ -16,705 +16,67 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * Copyright 2007 - 2014 Red Hat, Inc. - * Copyright 2007 - 2008 Novell, Inc. + * Copyright 2014 Red Hat, Inc. */ #include #include -#include #include "nm-setting-ip4-config.h" -#include "nm-utils.h" -#include "nm-glib-compat.h" #include "nm-setting-private.h" -#include "nm-core-internal.h" -#include "nm-utils-private.h" /** * SECTION:nm-setting-ip4-config * @short_description: Describes IPv4 addressing, routing, and name service properties * * The #NMSettingIP4Config object is a #NMSetting subclass that describes - * properties related to IPv4 addressing, routing, and Domain Name Service - **/ - -G_DEFINE_TYPE_WITH_CODE (NMSettingIP4Config, nm_setting_ip4_config, NM_TYPE_SETTING, + * properties related to IPv4 addressing, routing, and Domain Name Service. + * + * #NMSettingIP4Config has few properties or methods of its own; it inherits + * almost everything from #NMSettingIPConfig. + * + * NetworkManager supports 5 values for the #NMSettingIPConfig:method property + * for IPv4. If "auto" is specified then the appropriate automatic method + * (DHCP, PPP, etc) is used for the interface and most other properties can be + * left unset. If "link-local" is specified, then a link-local address in the + * 169.254/16 range will be assigned to the interface. If "manual" is + * specified, static IP addressing is used and at least one IP address must be + * given in the "addresses" property. If "shared" is specified (indicating that + * this connection will provide network access to other computers) then the + * interface is assigned an address in the 10.42.x.1/24 range and a DHCP and + * forwarding DNS server are started, and the interface is NAT-ed to the current + * default network connection. "disabled" means IPv4 will not be used on this + * connection. + **/ + +G_DEFINE_TYPE_WITH_CODE (NMSettingIP4Config, nm_setting_ip4_config, NM_TYPE_SETTING_IP_CONFIG, _nm_register_setting (IP4_CONFIG, 4)) NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP4_CONFIG) #define NM_SETTING_IP4_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_IP4_CONFIG, NMSettingIP4ConfigPrivate)) typedef struct { - char *method; - GSList *dns; /* list of IP address strings */ - GSList *dns_search; /* list of strings */ - GSList *addresses; /* array of NMIPAddress */ - GSList *routes; /* array of NMIPRoute */ - gboolean ignore_auto_routes; - gboolean ignore_auto_dns; char *dhcp_client_id; - gboolean dhcp_send_hostname; - char *dhcp_hostname; - gboolean never_default; - gboolean may_fail; } NMSettingIP4ConfigPrivate; enum { PROP_0, - PROP_METHOD, - PROP_DNS, - PROP_DNS_SEARCH, - PROP_ADDRESSES, - PROP_ROUTES, - PROP_IGNORE_AUTO_ROUTES, - PROP_IGNORE_AUTO_DNS, PROP_DHCP_CLIENT_ID, - PROP_DHCP_SEND_HOSTNAME, - PROP_DHCP_HOSTNAME, - PROP_NEVER_DEFAULT, - PROP_MAY_FAIL, LAST_PROP -}; - -/** - * nm_setting_ip4_config_new: - * - * Creates a new #NMSettingIP4Config object with default values. - * - * Returns: (transfer full): the new empty #NMSettingIP4Config object - **/ -NMSetting * -nm_setting_ip4_config_new (void) -{ - return (NMSetting *) g_object_new (NM_TYPE_SETTING_IP4_CONFIG, NULL); -} - -/** - * nm_setting_ip4_config_get_method: - * @setting: the #NMSettingIP4Config - * - * Returns: the #NMSettingIP4Config:method property of the setting - **/ -const char * -nm_setting_ip4_config_get_method (NMSettingIP4Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL); - - return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->method; -} - -/** - * nm_setting_ip4_config_get_num_dns: - * @setting: the #NMSettingIP4Config - * - * Returns: the number of configured DNS servers - **/ -guint32 -nm_setting_ip4_config_get_num_dns (NMSettingIP4Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), 0); - - return g_slist_length (NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns); -} - -/** - * nm_setting_ip4_config_get_dns: - * @setting: the #NMSettingIP4Config - * @i: index number of the DNS server to return - * - * Returns: the IPv4 address of the DNS server at index @i - **/ -const char * -nm_setting_ip4_config_get_dns (NMSettingIP4Config *setting, guint32 i) -{ - NMSettingIP4ConfigPrivate *priv; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i < g_slist_length (priv->dns), NULL); - - return (const char *) g_slist_nth_data (priv->dns, i); -} - -static const char * -canonicalize_ip (const char *ip) -{ - in_addr_t addr; - int ret; - - ret = inet_pton (AF_INET, ip, &addr); - g_return_val_if_fail (ret == 1, NULL); - return nm_utils_inet4_ntop (addr, NULL); -} - -/** - * nm_setting_ip4_config_add_dns: - * @setting: the #NMSettingIP4Config - * @dns: the IPv4 address of the DNS server to add - * - * Adds a new DNS server to the setting. - * - * Returns: %TRUE if the DNS server was added; %FALSE if the server was already - * known - **/ -gboolean -nm_setting_ip4_config_add_dns (NMSettingIP4Config *setting, const char *dns) -{ - NMSettingIP4ConfigPrivate *priv; - const char *dns_canonical; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - g_return_val_if_fail (dns != NULL, FALSE); - g_return_val_if_fail (dns[0] != '\0', FALSE); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - - dns_canonical = canonicalize_ip (dns); - g_return_val_if_fail (dns_canonical != NULL, FALSE); - - for (iter = priv->dns; iter; iter = g_slist_next (iter)) { - if (!strcmp (dns_canonical, (char *) iter->data)) - return FALSE; - } - - priv->dns = g_slist_append (priv->dns, g_strdup (dns_canonical)); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS); - return TRUE; -} - -/** - * nm_setting_ip4_config_remove_dns: - * @setting: the #NMSettingIP4Config - * @i: index number of the DNS server to remove - * - * Removes the DNS server at index @i. - **/ -void -nm_setting_ip4_config_remove_dns (NMSettingIP4Config *setting, guint32 i) -{ - NMSettingIP4ConfigPrivate *priv; - GSList *elt; - - g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - elt = g_slist_nth (priv->dns, i); - g_return_if_fail (elt != NULL); - - g_free (elt->data); - priv->dns = g_slist_delete_link (priv->dns, elt); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS); -} - -/** - * nm_setting_ip4_config_remove_dns_by_value: - * @setting: the #NMSettingIP4Config - * @dns: the DNS server to remove - * - * Removes the DNS server @dns. - * - * Returns: %TRUE if the DNS server was found and removed; %FALSE if it was not. - **/ -gboolean -nm_setting_ip4_config_remove_dns_by_value (NMSettingIP4Config *setting, const char *dns) -{ - NMSettingIP4ConfigPrivate *priv; - const char *dns_canonical; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - g_return_val_if_fail (dns != NULL, FALSE); - g_return_val_if_fail (dns[0] != '\0', FALSE); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - - dns_canonical = canonicalize_ip (dns); - g_return_val_if_fail (dns_canonical != NULL, FALSE); - - for (iter = priv->dns; iter; iter = g_slist_next (iter)) { - if (!strcmp (dns_canonical, (char *) iter->data)) { - priv->dns = g_slist_delete_link (priv->dns, iter); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS); - return TRUE; - } - } - return FALSE; -} - -/** - * nm_setting_ip4_config_clear_dns: - * @setting: the #NMSettingIP4Config - * - * Removes all configured DNS servers. - **/ -void -nm_setting_ip4_config_clear_dns (NMSettingIP4Config *setting) -{ - g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - - g_slist_free_full (NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns, g_free); - NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns = NULL; - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS); -} - -/** - * nm_setting_ip4_config_get_num_dns_searches: - * @setting: the #NMSettingIP4Config - * - * Returns: the number of configured DNS search domains - **/ -guint32 -nm_setting_ip4_config_get_num_dns_searches (NMSettingIP4Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), 0); - - return g_slist_length (NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns_search); -} - -/** - * nm_setting_ip4_config_get_dns_search: - * @setting: the #NMSettingIP4Config - * @i: index number of the DNS search domain to return - * - * Returns: the DNS search domain at index @i - **/ -const char * -nm_setting_ip4_config_get_dns_search (NMSettingIP4Config *setting, guint32 i) -{ - NMSettingIP4ConfigPrivate *priv; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i < g_slist_length (priv->dns_search), NULL); - - return (const char *) g_slist_nth_data (priv->dns_search, i); -} - -/** - * nm_setting_ip4_config_add_dns_search: - * @setting: the #NMSettingIP4Config - * @dns_search: the search domain to add - * - * Adds a new DNS search domain to the setting. - * - * Returns: %TRUE if the DNS search domain was added; %FALSE if the search - * domain was already known - **/ -gboolean -nm_setting_ip4_config_add_dns_search (NMSettingIP4Config *setting, - const char *dns_search) -{ - NMSettingIP4ConfigPrivate *priv; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - g_return_val_if_fail (dns_search != NULL, FALSE); - g_return_val_if_fail (dns_search[0] != '\0', FALSE); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - for (iter = priv->dns_search; iter; iter = g_slist_next (iter)) { - if (!strcmp (dns_search, (char *) iter->data)) - return FALSE; - } - - priv->dns_search = g_slist_append (priv->dns_search, g_strdup (dns_search)); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS_SEARCH); - return TRUE; -} - -/** - * nm_setting_ip4_config_remove_dns_search: - * @setting: the #NMSettingIP4Config - * @i: index number of the DNS search domain - * - * Removes the DNS search domain at index @i. - **/ -void -nm_setting_ip4_config_remove_dns_search (NMSettingIP4Config *setting, guint32 i) -{ - NMSettingIP4ConfigPrivate *priv; - GSList *elt; - - g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - elt = g_slist_nth (priv->dns_search, i); - g_return_if_fail (elt != NULL); - - g_free (elt->data); - priv->dns_search = g_slist_delete_link (priv->dns_search, elt); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS_SEARCH); -} - -/** - * nm_setting_ip4_config_remove_dns_search_by_value: - * @setting: the #NMSettingIP4Config - * @dns_search: the search domain to remove - * - * Removes the DNS search domain @dns_search. - * - * Returns: %TRUE if the DNS search domain was found and removed; %FALSE if it was not. - **/ -gboolean -nm_setting_ip4_config_remove_dns_search_by_value (NMSettingIP4Config *setting, - const char *dns_search) -{ - NMSettingIP4ConfigPrivate *priv; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - g_return_val_if_fail (dns_search != NULL, FALSE); - g_return_val_if_fail (dns_search[0] != '\0', FALSE); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - for (iter = priv->dns_search; iter; iter = g_slist_next (iter)) { - if (!strcmp (dns_search, (char *) iter->data)) { - priv->dns_search = g_slist_delete_link (priv->dns_search, iter); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS_SEARCH); - return TRUE; - } - } - return FALSE; -} - -/** - * nm_setting_ip4_config_clear_dns_searches: - * @setting: the #NMSettingIP4Config - * - * Removes all configured DNS search domains. - **/ -void -nm_setting_ip4_config_clear_dns_searches (NMSettingIP4Config *setting) -{ - g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - - g_slist_free_full (NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns_search, g_free); - NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dns_search = NULL; - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DNS_SEARCH); -} - -/** - * nm_setting_ip4_config_get_num_addresses: - * @setting: the #NMSettingIP4Config - * - * Returns: the number of configured addresses - **/ -guint32 -nm_setting_ip4_config_get_num_addresses (NMSettingIP4Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), 0); - - return g_slist_length (NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->addresses); -} - -/** - * nm_setting_ip4_config_get_address: - * @setting: the #NMSettingIP4Config - * @i: index number of the address to return - * - * Returns: the address at index @i - **/ -NMIPAddress * -nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i) -{ - NMSettingIP4ConfigPrivate *priv; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i < g_slist_length (priv->addresses), NULL); - - return (NMIPAddress *) g_slist_nth_data (priv->addresses, i); -} - -/** - * nm_setting_ip4_config_add_address: - * @setting: the #NMSettingIP4Config - * @address: the new address to add - * - * Adds a new IPv4 address and associated information to the setting. The - * given address is duplicated internally and is not changed by this function. - * - * Returns: %TRUE if the address was added; %FALSE if the address was already - * known. - **/ -gboolean -nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, - NMIPAddress *address) -{ - NMSettingIP4ConfigPrivate *priv; - NMIPAddress *copy; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - g_return_val_if_fail (address != NULL, FALSE); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { - if (nm_ip_address_equal ((NMIPAddress *) iter->data, address)) - return FALSE; - } - - copy = nm_ip_address_dup (address); - priv->addresses = g_slist_append (priv->addresses, copy); - - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES); - return TRUE; -} - -/** - * nm_setting_ip4_config_remove_address: - * @setting: the #NMSettingIP4Config - * @i: index number of the address to remove - * - * Removes the address at index @i. - **/ -void -nm_setting_ip4_config_remove_address (NMSettingIP4Config *setting, guint32 i) -{ - NMSettingIP4ConfigPrivate *priv; - GSList *addr; - - g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - addr = g_slist_nth (priv->addresses, i); - g_return_if_fail (addr != NULL); - - nm_ip_address_unref ((NMIPAddress *) addr->data); - priv->addresses = g_slist_delete_link (priv->addresses, addr); - - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES); -} - -/** - * nm_setting_ip4_config_remove_address_by_value: - * @setting: the #NMSettingIP4Config - * @address: the IP address to remove - * - * Removes the address @address. - * - * Returns: %TRUE if the address was found and removed; %FALSE if it was not. - **/ -gboolean -nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting, - NMIPAddress *address) -{ - NMSettingIP4ConfigPrivate *priv; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - g_return_val_if_fail (address != NULL, FALSE); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { - if (nm_ip_address_equal ((NMIPAddress *) iter->data, address)) { - nm_ip_address_unref ((NMIPAddress *) iter->data); - priv->addresses = g_slist_delete_link (priv->addresses, iter); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES); - return TRUE; - } - } - return FALSE; -} - -/** - * nm_setting_ip4_config_clear_addresses: - * @setting: the #NMSettingIP4Config - * - * Removes all configured addresses. - **/ -void -nm_setting_ip4_config_clear_addresses (NMSettingIP4Config *setting) -{ - NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - - g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); - priv->addresses = NULL; - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ADDRESSES); -} - -/** - * nm_setting_ip4_config_get_num_routes: - * @setting: the #NMSettingIP4Config - * - * Returns: the number of configured routes - **/ -guint32 -nm_setting_ip4_config_get_num_routes (NMSettingIP4Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), 0); - - return g_slist_length (NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->routes); -} - -/** - * nm_setting_ip4_config_get_route: - * @setting: the #NMSettingIP4Config - * @i: index number of the route to return - * - * Returns: the route at index @i - **/ -NMIPRoute * -nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i) -{ - NMSettingIP4ConfigPrivate *priv; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i < g_slist_length (priv->routes), NULL); - - return (NMIPRoute *) g_slist_nth_data (priv->routes, i); -} - -/** - * nm_setting_ip4_config_add_route: - * @setting: the #NMSettingIP4Config - * @route: the route to add - * - * Adds a new IPv4 route and associated information to the setting. The - * given route is duplicated internally and is not changed by this function. - * - * Returns: %TRUE if the route was added; %FALSE if the route was already known. - **/ -gboolean -nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, - NMIPRoute *route) -{ - NMSettingIP4ConfigPrivate *priv; - NMIPRoute *copy; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - g_return_val_if_fail (route != NULL, FALSE); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - for (iter = priv->routes; iter; iter = g_slist_next (iter)) { - if (nm_ip_route_equal (iter->data, route)) - return FALSE; - } - - copy = nm_ip_route_dup (route); - priv->routes = g_slist_append (priv->routes, copy); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); - return TRUE; -} - -/** - * nm_setting_ip4_config_remove_route: - * @setting: the #NMSettingIP4Config - * @i: index number of the route - * - * Removes the route at index @i. - **/ -void -nm_setting_ip4_config_remove_route (NMSettingIP4Config *setting, guint32 i) -{ - NMSettingIP4ConfigPrivate *priv; - GSList *elt; - - g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - elt = g_slist_nth (priv->routes, i); - g_return_if_fail (elt != NULL); - - nm_ip_route_unref ((NMIPRoute *) elt->data); - priv->routes = g_slist_delete_link (priv->routes, elt); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); -} - -/** - * nm_setting_ip4_config_remove_route_by_value: - * @setting: the #NMSettingIP4Config - * @route: the route to remove - * - * Removes the route @route. - * - * Returns: %TRUE if the route was found and removed; %FALSE if it was not. - **/ -gboolean -nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting, - NMIPRoute *route) -{ - NMSettingIP4ConfigPrivate *priv; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - g_return_val_if_fail (route != NULL, FALSE); - - priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - for (iter = priv->routes; iter; iter = g_slist_next (iter)) { - if (nm_ip_route_equal ((NMIPRoute *) iter->data, route)) { - nm_ip_route_unref ((NMIPRoute *) iter->data); - priv->routes = g_slist_delete_link (priv->routes, iter); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); - return TRUE; - } - } - return FALSE; -} - -/** - * nm_setting_ip4_config_clear_routes: - * @setting: the #NMSettingIP4Config - * - * Removes all configured routes. - **/ -void -nm_setting_ip4_config_clear_routes (NMSettingIP4Config *setting) -{ - NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - - g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); - - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); - priv->routes = NULL; - g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); -} - -/** - * nm_setting_ip4_config_get_ignore_auto_routes: - * @setting: the #NMSettingIP4Config - * - * Returns the value contained in the #NMSettingIP4Config:ignore-auto-routes - * property. - * - * Returns: %TRUE if automatically configured (ie via DHCP) routes should be - * ignored. - **/ -gboolean -nm_setting_ip4_config_get_ignore_auto_routes (NMSettingIP4Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - - return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->ignore_auto_routes; -} +}; /** - * nm_setting_ip4_config_get_ignore_auto_dns: - * @setting: the #NMSettingIP4Config + * nm_setting_ip4_config_new: * - * Returns the value contained in the #NMSettingIP4Config:ignore-auto-dns - * property. + * Creates a new #NMSettingIP4Config object with default values. * - * Returns: %TRUE if automatically configured (ie via DHCP) DNS information - * should be ignored. + * Returns: (transfer full): the new empty #NMSettingIP4Config object **/ -gboolean -nm_setting_ip4_config_get_ignore_auto_dns (NMSettingIP4Config *setting) +NMSetting * +nm_setting_ip4_config_new (void) { - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - - return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->ignore_auto_dns; + return (NMSetting *) g_object_new (NM_TYPE_SETTING_IP4_CONFIG, NULL); } /** @@ -735,171 +97,73 @@ nm_setting_ip4_config_get_dhcp_client_id (NMSettingIP4Config *setting) return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dhcp_client_id; } -/** - * nm_setting_ip4_config_get_dhcp_send_hostname: - * @setting: the #NMSettingIP4Config - * - * Returns the value contained in the #NMSettingIP4Config:dhcp-send-hostname - * property. - * - * Returns: %TRUE if NetworkManager should send the machine hostname to the - * DHCP server when requesting addresses to allow the server to automatically - * update DNS information for this machine. - **/ -gboolean -nm_setting_ip4_config_get_dhcp_send_hostname (NMSettingIP4Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - - return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dhcp_send_hostname; -} - -/** - * nm_setting_ip4_config_get_dhcp_hostname: - * @setting: the #NMSettingIP4Config - * - * Returns the value contained in the #NMSettingIP4Config:dhcp-hostname - * property. - * - * Returns: the configured hostname to send to the DHCP server - **/ -const char * -nm_setting_ip4_config_get_dhcp_hostname (NMSettingIP4Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL); - - return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dhcp_hostname; -} - -/** - * nm_setting_ip4_config_get_never_default: - * @setting: the #NMSettingIP4Config - * - * Returns the value contained in the #NMSettingIP4Config:never-default - * property. - * - * Returns: %TRUE if this connection should never be the default connection - * for IPv4 addressing - **/ -gboolean -nm_setting_ip4_config_get_never_default (NMSettingIP4Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - - return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->never_default; -} - -/** - * nm_setting_ip4_config_get_may_fail: - * @setting: the #NMSettingIP4Config - * - * Returns the value contained in the #NMSettingIP4Config:may-fail - * property. - * - * Returns: %TRUE if this connection doesn't require IPv4 addressing to complete - * for the connection to succeed. - **/ -gboolean -nm_setting_ip4_config_get_may_fail (NMSettingIP4Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), FALSE); - - return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->may_fail; -} - -static gboolean -verify_label (const char *label) -{ - const char *p; - char *iface; - - p = strchr (label, ':'); - if (!p) - return FALSE; - iface = g_strndup (label, p - label); - if (!nm_utils_iface_valid_name (iface)) { - g_free (iface); - return FALSE; - } - g_free (iface); - - for (p++; *p; p++) { - if (!g_ascii_isalnum (*p) && *p != '_') - return FALSE; - } - - return TRUE; -} - static gboolean verify (NMSetting *setting, NMConnection *connection, GError **error) { NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); - GSList *iter; - int i; + NMSettingIPConfig *s_ip = NM_SETTING_IP_CONFIG (setting); + const char *method; - if (!priv->method) { - g_set_error_literal (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_MISSING_PROPERTY, - _("property is missing")); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_METHOD); + if (!NM_SETTING_CLASS (nm_setting_ip4_config_parent_class)->verify (setting, connection, error)) return FALSE; - } - if (!strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) { - if (!priv->addresses) { + method = nm_setting_ip_config_get_method (s_ip); + /* Base class already checked that it exists */ + g_assert (method); + + if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) { + if (nm_setting_ip_config_get_num_addresses (s_ip) == 0) { g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_PROPERTY, _("this property cannot be empty for '%s=%s'"), - NM_SETTING_IP4_CONFIG_METHOD, priv->method); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_METHOD, method); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_ADDRESSES); return FALSE; } - } else if ( !strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL) - || !strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) - || !strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)) { - if (priv->dns) { + } else if ( !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL) + || !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) + || !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)) { + if (nm_setting_ip_config_get_num_dns (s_ip) > 0) { g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, _("this property is not allowed for '%s=%s'"), - NM_SETTING_IP4_CONFIG_METHOD, priv->method); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_METHOD, method); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_DNS); return FALSE; } - if (priv->dns_search) { + if (nm_setting_ip_config_get_num_dns_searches (s_ip) > 0) { g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, _("this property is not allowed for '%s=%s'"), - NM_SETTING_IP4_CONFIG_METHOD, priv->method); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_METHOD, method); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_DNS_SEARCH); return FALSE; } /* Shared allows IP addresses; link-local and disabled do not */ - if (strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) != 0) { - if (priv->addresses) { + if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) != 0) { + if (nm_setting_ip_config_get_num_addresses (s_ip) > 0) { g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, _("this property is not allowed for '%s=%s'"), - NM_SETTING_IP4_CONFIG_METHOD, priv->method); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_METHOD, method); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_ADDRESSES); return FALSE; } } - } else if (!strcmp (priv->method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) { + } else if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) { /* nothing to do */ } else { g_set_error_literal (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, _("property is invalid")); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_METHOD); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_METHOD); return FALSE; } @@ -912,63 +176,9 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) return FALSE; } - if (priv->dhcp_hostname && !strlen (priv->dhcp_hostname)) { - g_set_error_literal (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("property is empty")); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME); - return FALSE; - } - - /* Validate address labels */ - for (iter = priv->addresses, i = 0; iter; iter = g_slist_next (iter), i++) { - NMIPAddress *addr = (NMIPAddress *) iter->data; - GVariant *label; - - label = nm_ip_address_get_attribute (addr, "label"); - if (!label) - continue; - if (!g_variant_is_of_type (label, G_VARIANT_TYPE_STRING)) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("%d. IPv4 address has 'label' property with invalid type"), - i+1); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES); - return FALSE; - } - if (!verify_label (g_variant_get_string (label, NULL))) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("%d. IPv4 address has invalid label '%s'"), - i+1, g_variant_get_string (label, NULL)); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_ADDRESSES); - return FALSE; - } - } - - /* Validate DNS */ - for (iter = priv->dns, i = 0; iter; iter = g_slist_next (iter), i++) { - const char *dns = (const char *) iter->data; - in_addr_t addr; - - if (inet_pton (AF_INET, dns, &addr) != 1) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("%d. DNS server address is invalid"), - i+1); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DNS); - return FALSE; - } - } - return TRUE; } - static void nm_setting_ip4_config_init (NMSettingIP4Config *setting) { @@ -977,21 +187,46 @@ nm_setting_ip4_config_init (NMSettingIP4Config *setting) static void finalize (GObject *object) { - NMSettingIP4Config *self = NM_SETTING_IP4_CONFIG (object); - NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (self); + NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (object); - g_free (priv->method); - g_free (priv->dhcp_hostname); g_free (priv->dhcp_client_id); - g_slist_free_full (priv->dns, g_free); - g_slist_free_full (priv->dns_search, g_free); - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); - G_OBJECT_CLASS (nm_setting_ip4_config_parent_class)->finalize (object); } +static void +set_property (GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec) +{ + NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (object); + + switch (prop_id) { + case PROP_DHCP_CLIENT_ID: + g_free (priv->dhcp_client_id); + priv->dhcp_client_id = g_value_dup_string (value); + 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; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + static GVariant * ip4_dns_to_dbus (const GValue *prop_value) { @@ -1050,14 +285,15 @@ ip4_address_labels_get (NMSetting *setting, NMConnection *connection, const char *property) { - NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); + NMSettingIPConfig *s_ip = NM_SETTING_IP_CONFIG (setting); GPtrArray *labels; - GSList *iter; GVariant *ret; + int num_addrs, i; labels = g_ptr_array_new (); - for (iter = priv->addresses; iter; iter = iter->next) { - NMIPAddress *addr = iter->data; + num_addrs = nm_setting_ip_config_get_num_addresses (s_ip); + for (i = 0; i < num_addrs; i++) { + NMIPAddress *addr = nm_setting_ip_config_get_address (s_ip, i); GVariant *label = nm_ip_address_get_attribute (addr, "label"); g_ptr_array_add (labels, (char *) (label ? g_variant_get_string (label, NULL) : "")); @@ -1082,121 +318,12 @@ ip4_routes_from_dbus (GVariant *dbus_value, g_value_take_boxed (prop_value, nm_utils_ip4_routes_from_variant (dbus_value)); } -static void -set_property (GObject *object, guint prop_id, - const 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_free (priv->method); - priv->method = g_value_dup_string (value); - break; - case PROP_DNS: - g_slist_free_full (priv->dns, g_free); - priv->dns = _nm_utils_strv_to_slist (g_value_get_boxed (value)); - break; - case PROP_DNS_SEARCH: - g_slist_free_full (priv->dns_search, g_free); - priv->dns_search = _nm_utils_strv_to_slist (g_value_get_boxed (value)); - break; - case PROP_ADDRESSES: - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); - priv->addresses = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), - (NMUtilsCopyFunc) nm_ip_address_dup); - - break; - case PROP_ROUTES: - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); - priv->routes = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), - (NMUtilsCopyFunc) nm_ip_route_dup); - break; - case PROP_IGNORE_AUTO_ROUTES: - priv->ignore_auto_routes = g_value_get_boolean (value); - break; - case PROP_IGNORE_AUTO_DNS: - priv->ignore_auto_dns = g_value_get_boolean (value); - break; - case PROP_DHCP_CLIENT_ID: - g_free (priv->dhcp_client_id); - priv->dhcp_client_id = g_value_dup_string (value); - break; - case PROP_DHCP_SEND_HOSTNAME: - priv->dhcp_send_hostname = g_value_get_boolean (value); - break; - case PROP_DHCP_HOSTNAME: - g_free (priv->dhcp_hostname); - priv->dhcp_hostname = g_value_dup_string (value); - break; - case PROP_NEVER_DEFAULT: - priv->never_default = g_value_get_boolean (value); - break; - case PROP_MAY_FAIL: - priv->may_fail = g_value_get_boolean (value); - 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 *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_take_boxed (value, _nm_utils_slist_to_strv (priv->dns)); - break; - case PROP_DNS_SEARCH: - g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns_search)); - break; - case PROP_ADDRESSES: - g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip_address_dup, (GDestroyNotify) nm_ip_address_unref)); - break; - case PROP_ROUTES: - g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip_route_dup, (GDestroyNotify) nm_ip_route_unref)); - 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 -nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) +nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *ip4_class) { - GObjectClass *object_class = G_OBJECT_CLASS (setting_class); - NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class); + NMSettingClass *setting_class = NM_SETTING_CLASS (ip4_class); + GObjectClass *object_class = G_OBJECT_CLASS (ip4_class); g_type_class_add_private (setting_class, sizeof (NMSettingIP4ConfigPrivate)); @@ -1204,227 +331,46 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) object_class->set_property = set_property; object_class->get_property = get_property; object_class->finalize = finalize; - parent_class->verify = verify; + setting_class->verify = verify; + + /* properties */ - /* Properties */ /** - * NMSettingIP4Config:method: + * NMSettingIP4Config:dhcp-client-id: * - * IPv4 configuration method. If "auto" is specified then the appropriate - * automatic method (DHCP, PPP, etc) is used for the interface and most - * other properties can be left unset. If "link-local" is specified, then a - * link-local address in the 169.254/16 range will be assigned to the - * interface. If "manual" is specified, static IP addressing is used and at - * least one IP address must be given in the "addresses" property. If - * "shared" is specified (indicating that this connection will provide - * network access to other computers) then the interface is assigned an - * address in the 10.42.x.1/24 range and a DHCP and forwarding DNS server - * are started, and the interface is NAT-ed to the current default network - * connection. "disabled" means IPv4 will not be used on this connection. - * This property must be set. + * A string sent to the DHCP server to identify the local machine which the + * DHCP server may use to customize the DHCP lease and options. **/ g_object_class_install_property - (object_class, PROP_METHOD, - g_param_spec_string (NM_SETTING_IP4_CONFIG_METHOD, "", "", + (object_class, PROP_DHCP_CLIENT_ID, + g_param_spec_string (NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, "", "", NULL, G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | G_PARAM_STATIC_STRINGS)); - /** - * NMSettingIP4Config:dns: - * - * Array of IPv4 addresses of DNS servers. For the 'auto' method, these - * DNS servers are appended to those (if any) returned by automatic - * configuration. DNS servers cannot be used with the "shared", - * "link-local", or "disabled" methods as there is no upstream network. In - * all other methods, these DNS servers are used as the only DNS servers for - * this connection. - **/ - g_object_class_install_property - (object_class, PROP_DNS, - g_param_spec_boxed (NM_SETTING_IP4_CONFIG_DNS, "", "", - G_TYPE_STRV, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); - _nm_setting_class_transform_property (parent_class, NM_SETTING_IP4_CONFIG_DNS, + /* IP4-specific property overrides */ + _nm_setting_class_transform_property (setting_class, + NM_SETTING_IP_CONFIG_DNS, G_VARIANT_TYPE ("au"), ip4_dns_to_dbus, ip4_dns_from_dbus); - /** - * NMSettingIP4Config:dns-search: - * - * List of DNS search domains. For the "auto" method, these search domains - * are appended to those returned by automatic configuration. Search domains - * cannot be used with the "shared", "link-local", or "disabled" methods as - * there is no upstream network. In all other methods, these search domains - * are used as the only search domains for this connection. - **/ - g_object_class_install_property - (object_class, PROP_DNS_SEARCH, - g_param_spec_boxed (NM_SETTING_IP4_CONFIG_DNS_SEARCH, "", "", - G_TYPE_STRV, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP4Config:addresses: - * - * Array of IPv4 addresses. The gateway may be left as 0 if no gateway exists - * for that subnet. For the 'auto' method, given IP addresses are appended - * to those returned by automatic configuration. Addresses cannot be used - * with the "shared", "link-local", or "disabled" methods as addressing is - * either automatic or disabled with these methods. - * - * Element-Type: NMIPAddress - **/ - g_object_class_install_property - (object_class, PROP_ADDRESSES, - g_param_spec_boxed (NM_SETTING_IP4_CONFIG_ADDRESSES, "", "", - G_TYPE_PTR_ARRAY, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); - _nm_setting_class_override_property (parent_class, NM_SETTING_IP4_CONFIG_ADDRESSES, + _nm_setting_class_override_property (setting_class, + NM_SETTING_IP_CONFIG_ADDRESSES, G_VARIANT_TYPE ("aau"), ip4_addresses_get, ip4_addresses_set, NULL); - _nm_setting_class_add_dbus_only_property (parent_class, + _nm_setting_class_add_dbus_only_property (setting_class, "address-labels", G_VARIANT_TYPE_STRING_ARRAY, ip4_address_labels_get, NULL); - /** - * NMSettingIP4Config:routes: - * - * Array of IPv4 routes. For the 'auto' method, given IP routes are appended - * to those returned by automatic configuration. Routes cannot be used with - * the 'shared', 'link-local', or 'disabled' methods because there is no - * upstream network. - * - * Element-Type: NMIPRoute - **/ - g_object_class_install_property - (object_class, PROP_ROUTES, - g_param_spec_boxed (NM_SETTING_IP4_CONFIG_ROUTES, "", "", - G_TYPE_PTR_ARRAY, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); - _nm_setting_class_transform_property (parent_class, NM_SETTING_IP4_CONFIG_ROUTES, + _nm_setting_class_transform_property (setting_class, + NM_SETTING_IP_CONFIG_ROUTES, G_VARIANT_TYPE ("aau"), ip4_routes_to_dbus, ip4_routes_from_dbus); - - /** - * NMSettingIP4Config:ignore-auto-routes: - * - * When the method is set to "auto" and this property to %TRUE, - * automatically configured routes are ignored and only routes specified in - * the #NMSettingIP4Config:routes property, if any, are used. - **/ - g_object_class_install_property - (object_class, PROP_IGNORE_AUTO_ROUTES, - g_param_spec_boolean (NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, "", "", - FALSE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP4Config:ignore-auto-dns: - * - * When the method is set to "auto" and this property to %TRUE, - * automatically configured nameservers and search domains are ignored and - * only nameservers and search domains specified in the - * #NMSettingIP4Config:dns and #NMSettingIP4Config:dns-search properties, if - * any, are used. - **/ - g_object_class_install_property - (object_class, PROP_IGNORE_AUTO_DNS, - g_param_spec_boolean (NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, "", "", - FALSE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP4Config:dhcp-client-id: - * - * A string sent to the DHCP server to identify the local machine which the - * DHCP server may use to customize the DHCP lease and options. - **/ - g_object_class_install_property - (object_class, PROP_DHCP_CLIENT_ID, - g_param_spec_string (NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, "", "", - NULL, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP4Config:dhcp-send-hostname: - * - * If %TRUE, a hostname is sent to the DHCP server when acquiring a lease. - * Some DHCP servers use this hostname to update DNS databases, essentially - * providing a static hostname for the computer. If the - * #NMSettingIP4Config:dhcp-hostname property is empty and this property is - * %TRUE, the current persistent hostname of the computer is sent. - **/ - g_object_class_install_property - (object_class, PROP_DHCP_SEND_HOSTNAME, - g_param_spec_boolean (NM_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME, "", "", - TRUE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP4Config:dhcp-hostname: - * - * If the #NMSettingIP4Config:dhcp-send-hostname property is %TRUE, then the - * specified name will be sent to the DHCP server when acquiring a lease. - **/ - g_object_class_install_property - (object_class, PROP_DHCP_HOSTNAME, - g_param_spec_string (NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, "", "", - NULL, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP4Config:never-default: - * - * If %TRUE, this connection will never be the default IPv4 connection, - * meaning it will never be assigned the default route by NetworkManager. - **/ - g_object_class_install_property - (object_class, PROP_NEVER_DEFAULT, - g_param_spec_boolean (NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, "", "", - FALSE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP4Config:may-fail: - * - * If %TRUE, allow overall network configuration to proceed even if IPv4 - * configuration times out. Note that at least one IP configuration must - * succeed or overall network configuration will still fail. For example, - * in IPv6-only networks, setting this property to %TRUE allows the overall - * network configuration to succeed if IPv4 configuration fails but IPv6 - * configuration completes successfully. - **/ - g_object_class_install_property - (object_class, PROP_MAY_FAIL, - g_param_spec_boolean (NM_SETTING_IP4_CONFIG_MAY_FAIL, "", "", - TRUE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT | - G_PARAM_STATIC_STRINGS)); } diff --git a/libnm-core/nm-setting-ip4-config.h b/libnm-core/nm-setting-ip4-config.h index f683becda5..e944dfe391 100644 --- a/libnm-core/nm-setting-ip4-config.h +++ b/libnm-core/nm-setting-ip4-config.h @@ -27,7 +27,6 @@ #error "Only can be included directly." #endif -#include "nm-setting.h" #include "nm-setting-ip-config.h" G_BEGIN_DECLS @@ -41,18 +40,7 @@ G_BEGIN_DECLS #define NM_SETTING_IP4_CONFIG_SETTING_NAME "ipv4" -#define NM_SETTING_IP4_CONFIG_METHOD "method" -#define NM_SETTING_IP4_CONFIG_DNS "dns" -#define NM_SETTING_IP4_CONFIG_DNS_SEARCH "dns-search" -#define NM_SETTING_IP4_CONFIG_ADDRESSES "addresses" -#define NM_SETTING_IP4_CONFIG_ROUTES "routes" -#define NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES "ignore-auto-routes" -#define NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS "ignore-auto-dns" #define NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID "dhcp-client-id" -#define NM_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME "dhcp-send-hostname" -#define NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME "dhcp-hostname" -#define NM_SETTING_IP4_CONFIG_NEVER_DEFAULT "never-default" -#define NM_SETTING_IP4_CONFIG_MAY_FAIL "may-fail" /** * NM_SETTING_IP4_CONFIG_METHOD_AUTO: @@ -99,11 +87,11 @@ G_BEGIN_DECLS #define NM_SETTING_IP4_CONFIG_METHOD_DISABLED "disabled" struct _NMSettingIP4Config { - NMSetting parent; + NMSettingIPConfig parent; }; typedef struct { - NMSettingClass parent; + NMSettingIPConfigClass parent; /*< private >*/ gpointer padding[4]; @@ -111,46 +99,9 @@ typedef struct { GType nm_setting_ip4_config_get_type (void); -NMSetting * nm_setting_ip4_config_new (void); -const char * nm_setting_ip4_config_get_method (NMSettingIP4Config *setting); - -guint32 nm_setting_ip4_config_get_num_dns (NMSettingIP4Config *setting); -const char * nm_setting_ip4_config_get_dns (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_add_dns (NMSettingIP4Config *setting, const char *dns); -void nm_setting_ip4_config_remove_dns (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_remove_dns_by_value (NMSettingIP4Config *setting, const char *dns); -void nm_setting_ip4_config_clear_dns (NMSettingIP4Config *setting); - -guint32 nm_setting_ip4_config_get_num_dns_searches (NMSettingIP4Config *setting); -const char * nm_setting_ip4_config_get_dns_search (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_add_dns_search (NMSettingIP4Config *setting, const char *dns_search); -void nm_setting_ip4_config_remove_dns_search (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_remove_dns_search_by_value (NMSettingIP4Config *setting, const char *dns_search); -void nm_setting_ip4_config_clear_dns_searches (NMSettingIP4Config *setting); - -guint32 nm_setting_ip4_config_get_num_addresses (NMSettingIP4Config *setting); -NMIPAddress * nm_setting_ip4_config_get_address (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_add_address (NMSettingIP4Config *setting, NMIPAddress *address); -void nm_setting_ip4_config_remove_address (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_remove_address_by_value (NMSettingIP4Config *setting, NMIPAddress *address); -void nm_setting_ip4_config_clear_addresses (NMSettingIP4Config *setting); - -guint32 nm_setting_ip4_config_get_num_routes (NMSettingIP4Config *setting); -NMIPRoute * nm_setting_ip4_config_get_route (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_add_route (NMSettingIP4Config *setting, NMIPRoute *route); -void nm_setting_ip4_config_remove_route (NMSettingIP4Config *setting, guint32 i); -gboolean nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting, NMIPRoute *route); -void nm_setting_ip4_config_clear_routes (NMSettingIP4Config *setting); - -gboolean nm_setting_ip4_config_get_ignore_auto_routes (NMSettingIP4Config *setting); -gboolean nm_setting_ip4_config_get_ignore_auto_dns (NMSettingIP4Config *setting); -const char * nm_setting_ip4_config_get_dhcp_client_id (NMSettingIP4Config *setting); -gboolean nm_setting_ip4_config_get_dhcp_send_hostname (NMSettingIP4Config *setting); -const char * nm_setting_ip4_config_get_dhcp_hostname (NMSettingIP4Config *setting); - -gboolean nm_setting_ip4_config_get_never_default (NMSettingIP4Config *setting); - -gboolean nm_setting_ip4_config_get_may_fail (NMSettingIP4Config *setting); +NMSetting *nm_setting_ip4_config_new (void); + +const char *nm_setting_ip4_config_get_dhcp_client_id (NMSettingIP4Config *setting); G_END_DECLS diff --git a/libnm-core/nm-setting-ip6-config.c b/libnm-core/nm-setting-ip6-config.c index 3ac82eb4e1..dc05e541c3 100644 --- a/libnm-core/nm-setting-ip6-config.c +++ b/libnm-core/nm-setting-ip6-config.c @@ -23,9 +23,6 @@ #include #include "nm-setting-ip6-config.h" -#include "nm-utils.h" -#include "nm-utils-private.h" -#include "nm-glib-compat.h" #include "nm-setting-private.h" #include "nm-core-enum-types.h" @@ -35,41 +32,35 @@ * * The #NMSettingIP6Config object is a #NMSetting subclass that describes * properties related to IPv6 addressing, routing, and Domain Name Service + * + * #NMSettingIP6Config has few properties or methods of its own; it inherits + * almost everything from #NMSettingIPConfig. + * + * NetworkManager supports 6 values for the #NMSettingIPConfig:method property + * for IPv6. If "auto" is specified then the appropriate automatic method (PPP, + * router advertisement, etc) is used for the device and most other properties + * can be left unset. To force the use of DHCP only, specify "dhcp"; this + * method is only valid for Ethernet- based hardware. If "link-local" is + * specified, then an IPv6 link-local address will be assigned to the interface. + * If "manual" is specified, static IP addressing is used and at least one IP + * address must be given in the "addresses" property. If "ignore" is specified, + * IPv6 configuration is not done. Note: the "shared" method is not yet + * supported. **/ -G_DEFINE_TYPE_WITH_CODE (NMSettingIP6Config, nm_setting_ip6_config, NM_TYPE_SETTING, +G_DEFINE_TYPE_WITH_CODE (NMSettingIP6Config, nm_setting_ip6_config, NM_TYPE_SETTING_IP_CONFIG, _nm_register_setting (IP6_CONFIG, 4)) NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP6_CONFIG) #define NM_SETTING_IP6_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_IP6_CONFIG, NMSettingIP6ConfigPrivate)) typedef struct { - char *method; - char *dhcp_hostname; - GSList *dns; /* array of struct in6_addr */ - GSList *dns_search; /* list of strings */ - GSList *addresses; /* array of NMIPAddress */ - GSList *routes; /* array of NMIPRoute */ - gboolean ignore_auto_routes; - gboolean ignore_auto_dns; - gboolean never_default; - gboolean may_fail; NMSettingIP6ConfigPrivacy ip6_privacy; } NMSettingIP6ConfigPrivate; enum { PROP_0, - PROP_METHOD, - PROP_DHCP_HOSTNAME, - PROP_DNS, - PROP_DNS_SEARCH, - PROP_ADDRESSES, - PROP_ROUTES, - PROP_IGNORE_AUTO_ROUTES, - PROP_IGNORE_AUTO_DNS, - PROP_NEVER_DEFAULT, - PROP_MAY_FAIL, PROP_IP6_PRIVACY, LAST_PROP @@ -88,683 +79,6 @@ nm_setting_ip6_config_new (void) return (NMSetting *) g_object_new (NM_TYPE_SETTING_IP6_CONFIG, NULL); } -/** - * nm_setting_ip6_config_get_method: - * @setting: the #NMSettingIP6Config - * - * Returns: the #NMSettingIP6Config:method property of the setting - **/ -const char * -nm_setting_ip6_config_get_method (NMSettingIP6Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL); - - return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->method; -} - -/** - * nm_setting_ip6_config_get_dhcp_hostname: - * @setting: the #NMSettingIP6Config - * - * Returns the value contained in the #NMSettingIP6Config:dhcp-hostname - * property. - * - * Returns: the configured hostname to send to the DHCP server - **/ -const char * -nm_setting_ip6_config_get_dhcp_hostname (NMSettingIP6Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL); - - return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dhcp_hostname; -} - -/** - * nm_setting_ip6_config_get_num_dns: - * @setting: the #NMSettingIP6Config - * - * Returns: the number of configured DNS servers - **/ -guint32 -nm_setting_ip6_config_get_num_dns (NMSettingIP6Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), 0); - - return g_slist_length (NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dns); -} - -/** - * nm_setting_ip6_config_get_dns: - * @setting: the #NMSettingIP6Config - * @i: index number of the DNS server to return - * - * Returns: (transfer none): the IPv6 address of the DNS server at index @i - **/ -const char * -nm_setting_ip6_config_get_dns (NMSettingIP6Config *setting, guint32 i) -{ - NMSettingIP6ConfigPrivate *priv; - - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i < g_slist_length (priv->dns), NULL); - - return (const char *) g_slist_nth_data (priv->dns, i); -} - -static const char * -canonicalize_ip (const char *ip) -{ - struct in6_addr addr; - int ret; - - ret = inet_pton (AF_INET6, ip, &addr); - g_return_val_if_fail (ret == 1, NULL); - return nm_utils_inet6_ntop (&addr, NULL); -} - -/** - * nm_setting_ip6_config_add_dns: - * @setting: the #NMSettingIP6Config - * @dns: the IPv6 address of the DNS server to add - * - * Adds a new DNS server to the setting. - * - * Returns: %TRUE if the DNS server was added; %FALSE if the server was already - * known - **/ -gboolean -nm_setting_ip6_config_add_dns (NMSettingIP6Config *setting, const char *dns) -{ - NMSettingIP6ConfigPrivate *priv; - const char *dns_canonical; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); - g_return_val_if_fail (dns != NULL, FALSE); - g_return_val_if_fail (dns[0] != '\0', FALSE); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - - dns_canonical = canonicalize_ip (dns); - g_return_val_if_fail (dns_canonical != NULL, FALSE); - - for (iter = priv->dns; iter; iter = g_slist_next (iter)) { - if (!strcmp (dns_canonical, (char *) iter->data)) - return FALSE; - } - - priv->dns = g_slist_append (priv->dns, g_strdup (dns_canonical)); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS); - return TRUE; -} - -/** - * nm_setting_ip6_config_remove_dns: - * @setting: the #NMSettingIP6Config - * @i: index number of the DNS server to remove - * - * Removes the DNS server at index @i. - **/ -void -nm_setting_ip6_config_remove_dns (NMSettingIP6Config *setting, guint32 i) -{ - NMSettingIP6ConfigPrivate *priv; - GSList *elt; - - g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - elt = g_slist_nth (priv->dns, i); - g_return_if_fail (elt != NULL); - - g_free (elt->data); - priv->dns = g_slist_delete_link (priv->dns, elt); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS); -} - -/** - * nm_setting_ip6_config_remove_dns_by_value: - * @setting: the #NMSettingIP6Config - * @dns: the IPv6 address of the DNS server to remove - * - * Removes the DNS server at index @i. - * - * Returns: %TRUE if the DNS server was found and removed; %FALSE if it was not. - **/ -gboolean -nm_setting_ip6_config_remove_dns_by_value (NMSettingIP6Config *setting, - const char *dns) -{ - NMSettingIP6ConfigPrivate *priv; - const char *dns_canonical; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); - g_return_val_if_fail (dns != NULL, FALSE); - g_return_val_if_fail (dns[0] != '\0', FALSE); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - - dns_canonical = canonicalize_ip (dns); - g_return_val_if_fail (dns_canonical != NULL, FALSE); - - for (iter = priv->dns; iter; iter = g_slist_next (iter)) { - if (!strcmp (dns_canonical, (char *) iter->data)) { - priv->dns = g_slist_delete_link (priv->dns, iter); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS); - return TRUE; - } - } - return FALSE; -} - -/** - * nm_setting_ip6_config_clear_dns: - * @setting: the #NMSettingIP6Config - * - * Removes all configured DNS servers. - **/ -void -nm_setting_ip6_config_clear_dns (NMSettingIP6Config *setting) -{ - g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - - g_slist_free_full (NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dns, g_free); - NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dns = NULL; - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS); -} - -/** - * nm_setting_ip6_config_get_num_dns_searches: - * @setting: the #NMSettingIP6Config - * - * Returns: the number of configured DNS search domains - **/ -guint32 -nm_setting_ip6_config_get_num_dns_searches (NMSettingIP6Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), 0); - - return g_slist_length (NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dns_search); -} - -/** - * nm_setting_ip6_config_get_dns_search: - * @setting: the #NMSettingIP6Config - * @i: index number of the DNS search domain to return - * - * Returns: the DNS search domain at index @i - **/ -const char * -nm_setting_ip6_config_get_dns_search (NMSettingIP6Config *setting, guint32 i) -{ - NMSettingIP6ConfigPrivate *priv; - - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i < g_slist_length (priv->dns_search), NULL); - - return (const char *) g_slist_nth_data (priv->dns_search, i); -} - -/** - * nm_setting_ip6_config_add_dns_search: - * @setting: the #NMSettingIP6Config - * @dns_search: the search domain to add - * - * Adds a new DNS search domain to the setting. - * - * Returns: %TRUE if the DNS search domain was added; %FALSE if the search - * domain was already known - **/ -gboolean -nm_setting_ip6_config_add_dns_search (NMSettingIP6Config *setting, - const char *dns_search) -{ - NMSettingIP6ConfigPrivate *priv; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); - g_return_val_if_fail (dns_search != NULL, FALSE); - g_return_val_if_fail (dns_search[0] != '\0', FALSE); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - for (iter = priv->dns_search; iter; iter = g_slist_next (iter)) { - if (!strcmp (dns_search, (char *) iter->data)) - return FALSE; - } - - priv->dns_search = g_slist_append (priv->dns_search, g_strdup (dns_search)); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS_SEARCH); - return TRUE; -} - -/** - * nm_setting_ip6_config_remove_dns_search: - * @setting: the #NMSettingIP6Config - * @i: index number of the DNS search domain - * - * Removes the DNS search domain at index @i. - **/ -void -nm_setting_ip6_config_remove_dns_search (NMSettingIP6Config *setting, guint32 i) -{ - NMSettingIP6ConfigPrivate *priv; - GSList *elt; - - g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - elt = g_slist_nth (priv->dns_search, i); - g_return_if_fail (elt != NULL); - - g_free (elt->data); - priv->dns_search = g_slist_delete_link (priv->dns_search, elt); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS_SEARCH); -} - -/** - * nm_setting_ip6_config_remove_dns_search_by_value: - * @setting: the #NMSettingIP6Config - * @dns_search: the search domain to remove - * - * Removes the DNS search domain @dns_search. - * - * Returns: %TRUE if the DNS search domain was found and removed; %FALSE if it was not. - **/ -gboolean -nm_setting_ip6_config_remove_dns_search_by_value (NMSettingIP6Config *setting, - const char *dns_search) -{ - NMSettingIP6ConfigPrivate *priv; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); - g_return_val_if_fail (dns_search != NULL, FALSE); - g_return_val_if_fail (dns_search[0] != '\0', FALSE); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - for (iter = priv->dns_search; iter; iter = g_slist_next (iter)) { - if (!strcmp (dns_search, (char *) iter->data)) { - priv->dns_search = g_slist_delete_link (priv->dns_search, iter); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS_SEARCH); - return TRUE; - } - } - return FALSE; -} - -/** - * nm_setting_ip6_config_clear_dns_searches: - * @setting: the #NMSettingIP6Config - * - * Removes all configured DNS search domains. - **/ -void -nm_setting_ip6_config_clear_dns_searches (NMSettingIP6Config *setting) -{ - g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - - g_slist_free_full (NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dns_search, g_free); - NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->dns_search = NULL; - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_DNS_SEARCH); -} - -/** - * nm_setting_ip6_config_get_num_addresses: - * @setting: the #NMSettingIP6Config - * - * Returns: the number of configured addresses - **/ -guint32 -nm_setting_ip6_config_get_num_addresses (NMSettingIP6Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), 0); - - return g_slist_length (NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->addresses); -} - -/** - * nm_setting_ip6_config_get_address: - * @setting: the #NMSettingIP6Config - * @i: index number of the address to return - * - * Returns: the address at index @i - **/ -NMIPAddress * -nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i) -{ - NMSettingIP6ConfigPrivate *priv; - - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i < g_slist_length (priv->addresses), NULL); - - return (NMIPAddress *) g_slist_nth_data (priv->addresses, i); -} - -/** - * nm_setting_ip6_config_add_address: - * @setting: the #NMSettingIP6Config - * @address: the new address to add - * - * Adds a new IPv6 address and associated information to the setting. The - * given address is duplicated internally and is not changed by this function. - * - * Returns: %TRUE if the address was added; %FALSE if the address was already - * known. - **/ -gboolean -nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, - NMIPAddress *address) -{ - NMSettingIP6ConfigPrivate *priv; - NMIPAddress *copy; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); - g_return_val_if_fail (address != NULL, FALSE); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { - if (nm_ip_address_equal ((NMIPAddress *) iter->data, address)) - return FALSE; - } - - copy = nm_ip_address_dup (address); - priv->addresses = g_slist_append (priv->addresses, copy); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); - return TRUE; -} - -/** - * nm_setting_ip6_config_remove_address: - * @setting: the #NMSettingIP6Config - * @i: index number of the address to remove - * - * Removes the address at index @i. - **/ -void -nm_setting_ip6_config_remove_address (NMSettingIP6Config *setting, guint32 i) -{ - NMSettingIP6ConfigPrivate *priv; - GSList *elt; - - g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - elt = g_slist_nth (priv->addresses, i); - g_return_if_fail (elt != NULL); - - nm_ip_address_unref ((NMIPAddress *) elt->data); - priv->addresses = g_slist_delete_link (priv->addresses, elt); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); -} - -/** - * nm_setting_ip6_config_remove_address_by_value: - * @setting: the #NMSettingIP6Config - * @address: the address to remove - * - * Removes the address @address. - * - * Returns: %TRUE if the address was found and removed; %FALSE if it was not. - **/ -gboolean -nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting, - NMIPAddress *address) -{ - NMSettingIP6ConfigPrivate *priv; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); - g_return_val_if_fail (address != NULL, FALSE); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - for (iter = priv->addresses; iter; iter = g_slist_next (iter)) { - if (nm_ip_address_equal ((NMIPAddress *) iter->data, address)) { - priv->addresses = g_slist_delete_link (priv->addresses, iter); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); - return TRUE; - } - } - return FALSE; -} - -/** - * nm_setting_ip6_config_clear_addresses: - * @setting: the #NMSettingIP6Config - * - * Removes all configured addresses. - **/ -void -nm_setting_ip6_config_clear_addresses (NMSettingIP6Config *setting) -{ - NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - - g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); - priv->addresses = NULL; - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ADDRESSES); -} - -/** - * nm_setting_ip6_config_get_num_routes: - * @setting: the #NMSettingIP6Config - * - * Returns: the number of configured routes - **/ -guint32 -nm_setting_ip6_config_get_num_routes (NMSettingIP6Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), 0); - - return g_slist_length (NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->routes); -} - -/** - * nm_setting_ip6_config_get_route: - * @setting: the #NMSettingIP6Config - * @i: index number of the route to return - * - * Returns: the route at index @i - **/ -NMIPRoute * -nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i) -{ - NMSettingIP6ConfigPrivate *priv; - - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), NULL); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - g_return_val_if_fail (i < g_slist_length (priv->routes), NULL); - - return (NMIPRoute *) g_slist_nth_data (priv->routes, i); -} - -/** - * nm_setting_ip6_config_add_route: - * @setting: the #NMSettingIP6Config - * @route: the route to add - * - * Adds a new IPv6 route and associated information to the setting. The - * given route is duplicated internally and is not changed by this function. - * - * Returns: %TRUE if the route was added; %FALSE if the route was already known. - **/ -gboolean -nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, - NMIPRoute *route) -{ - NMSettingIP6ConfigPrivate *priv; - NMIPRoute *copy; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); - g_return_val_if_fail (route != NULL, FALSE); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - for (iter = priv->routes; iter; iter = g_slist_next (iter)) { - if (nm_ip_route_equal ((NMIPRoute *) iter->data, route)) - return FALSE; - } - - copy = nm_ip_route_dup (route); - priv->routes = g_slist_append (priv->routes, copy); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); - return TRUE; -} - -/** - * nm_setting_ip6_config_remove_route: - * @setting: the #NMSettingIP6Config - * @i: index number of the route - * - * Removes the route at index @i. - **/ -void -nm_setting_ip6_config_remove_route (NMSettingIP6Config *setting, guint32 i) -{ - NMSettingIP6ConfigPrivate *priv; - GSList *elt; - - g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - elt = g_slist_nth (priv->routes, i); - g_return_if_fail (elt != NULL); - - nm_ip_route_unref ((NMIPRoute *) elt->data); - priv->routes = g_slist_delete_link (priv->routes, elt); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); -} - -/** - * nm_setting_ip6_config_remove_route_by_value: - * @setting: the #NMSettingIP6Config - * @route: the route to remove - * - * Removes the route @route. - * - * Returns: %TRUE if the route was found and removed; %FALSE if it was not. - **/ -gboolean -nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting, - NMIPRoute *route) -{ - NMSettingIP6ConfigPrivate *priv; - GSList *iter; - - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); - g_return_val_if_fail (route != NULL, FALSE); - - priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - for (iter = priv->routes; iter; iter = g_slist_next (iter)) { - if (nm_ip_route_equal ((NMIPRoute *) iter->data, route)) { - nm_ip_route_unref ((NMIPRoute *) iter->data); - priv->routes = g_slist_delete_link (priv->routes, iter); - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); - return TRUE; - } - } - return FALSE; -} - -/** - * nm_setting_ip6_config_clear_routes: - * @setting: the #NMSettingIP6Config - * - * Removes all configured routes. - **/ -void -nm_setting_ip6_config_clear_routes (NMSettingIP6Config *setting) -{ - NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - - g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); - - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); - priv->routes = NULL; - g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); -} - -/** - * nm_setting_ip6_config_get_ignore_auto_routes: - * @setting: the #NMSettingIP6Config - * - * Returns the value contained in the #NMSettingIP6Config:ignore-auto-routes - * property. - * - * Returns: %TRUE if automatically configured (ie via DHCP) routes should be - * ignored. - **/ -gboolean -nm_setting_ip6_config_get_ignore_auto_routes (NMSettingIP6Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); - - return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->ignore_auto_routes; -} - -/** - * nm_setting_ip6_config_get_ignore_auto_dns: - * @setting: the #NMSettingIP6Config - * - * Returns the value contained in the #NMSettingIP6Config:ignore-auto-dns - * property. - * - * Returns: %TRUE if automatically configured (ie via DHCP or router - * advertisements) DNS information should be ignored. - **/ -gboolean -nm_setting_ip6_config_get_ignore_auto_dns (NMSettingIP6Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); - - return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->ignore_auto_dns; -} - -/** - * nm_setting_ip6_config_get_never_default: - * @setting: the #NMSettingIP6Config - * - * Returns the value contained in the #NMSettingIP6Config:never-default - * property. - * - * Returns: %TRUE if this connection should never be the default connection - * for IPv6 addressing - **/ -gboolean -nm_setting_ip6_config_get_never_default (NMSettingIP6Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); - - return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->never_default; -} - -/** - * nm_setting_ip6_config_get_may_fail: - * @setting: the #NMSettingIP6Config - * - * Returns the value contained in the #NMSettingIP6Config:may-fail - * property. - * - * Returns: %TRUE if this connection doesn't require IPv6 addressing to complete - * for the connection to succeed. - **/ -gboolean -nm_setting_ip6_config_get_may_fail (NMSettingIP6Config *setting) -{ - g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), FALSE); - - return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->may_fail; -} - /** * nm_setting_ip6_config_get_ip6_privacy: * @setting: the #NMSettingIP6Config @@ -785,98 +99,71 @@ nm_setting_ip6_config_get_ip6_privacy (NMSettingIP6Config *setting) static gboolean verify (NMSetting *setting, NMConnection *connection, GError **error) { - NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting); - GSList *iter; - int i; + NMSettingIPConfig *s_ip = NM_SETTING_IP_CONFIG (setting); + const char *method; - if (!priv->method) { - g_set_error_literal (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_MISSING_PROPERTY, - _("property is missing")); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_METHOD); + if (!NM_SETTING_CLASS (nm_setting_ip6_config_parent_class)->verify (setting, connection, error)) return FALSE; - } - if (!strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) { - if (!priv->addresses) { + method = nm_setting_ip_config_get_method (s_ip); + /* Base class already checked that it exists */ + g_assert (method); + + if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) { + if (nm_setting_ip_config_get_num_addresses (s_ip) == 0) { g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_PROPERTY, _("this property cannot be empty for '%s=%s'"), - NM_SETTING_IP6_CONFIG_METHOD, priv->method); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_METHOD, method); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_ADDRESSES); return FALSE; } - } else if ( !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) - || !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) - || !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_SHARED)) { - if (priv->dns) { + } else if ( !strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) + || !strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) + || !strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_SHARED)) { + if (nm_setting_ip_config_get_num_dns (s_ip) > 0) { g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, _("'%s' not allowed for %s=%s"), _("this property is not allowed for '%s=%s'"), - NM_SETTING_IP6_CONFIG_METHOD, priv->method); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS); + NM_SETTING_IP_CONFIG_METHOD, method); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_DNS); return FALSE; } - if (priv->dns_search) { + if (nm_setting_ip_config_get_num_dns_searches (s_ip) > 0) { g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, _("this property is not allowed for '%s=%s'"), - NM_SETTING_IP6_CONFIG_METHOD, priv->method); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_METHOD, method); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_DNS_SEARCH); return FALSE; } - if (priv->addresses) { + if (nm_setting_ip_config_get_num_addresses (s_ip) > 0) { g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, _("this property is not allowed for '%s=%s'"), - NM_SETTING_IP6_CONFIG_METHOD, priv->method); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_METHOD, method); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_ADDRESSES); return FALSE; } - } else if ( !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) - || !strcmp (priv->method, NM_SETTING_IP6_CONFIG_METHOD_DHCP)) { + } else if ( !strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) + || !strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_DHCP)) { /* nothing to do */ } else { g_set_error_literal (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, _("property is invalid")); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_METHOD); - return FALSE; - } - - if (priv->dhcp_hostname && !strlen (priv->dhcp_hostname)) { - g_set_error_literal (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("property is missing")); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DHCP_HOSTNAME); + g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_METHOD); return FALSE; } - for (iter = priv->dns, i = 0; iter; iter = g_slist_next (iter), i++) { - const char *dns = (const char *) iter->data; - struct in6_addr addr; - - if (inet_pton (AF_INET6, dns, &addr) != 1) { - g_set_error (error, - NM_CONNECTION_ERROR, - NM_CONNECTION_ERROR_INVALID_PROPERTY, - _("%d. DNS server address is invalid"), - i+1); - g_prefix_error (error, "%s.%s: ", NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP6_CONFIG_DNS); - return FALSE; - } - } - return TRUE; } @@ -886,22 +173,6 @@ nm_setting_ip6_config_init (NMSettingIP6Config *setting) { } -static void -finalize (GObject *object) -{ - NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (object); - - g_free (priv->method); - g_free (priv->dhcp_hostname); - - g_slist_free_full (priv->dns, g_free); - g_slist_free_full (priv->dns_search, g_free); - g_slist_free_full (priv->addresses, (GDestroyNotify) nm_ip_address_unref); - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); - - G_OBJECT_CLASS (nm_setting_ip6_config_parent_class)->finalize (object); -} - static GVariant * ip6_dns_to_dbus (const GValue *prop_value) { @@ -948,44 +219,6 @@ set_property (GObject *object, guint prop_id, NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (object); switch (prop_id) { - case PROP_METHOD: - g_free (priv->method); - priv->method = g_value_dup_string (value); - break; - case PROP_DNS: - g_slist_free_full (priv->dns, g_free); - priv->dns = _nm_utils_strv_to_slist (g_value_get_boxed (value)); - break; - case PROP_DNS_SEARCH: - g_slist_free_full (priv->dns_search, g_free); - priv->dns_search = _nm_utils_strv_to_slist (g_value_get_boxed (value)); - break; - case PROP_ADDRESSES: - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_address_unref); - priv->addresses = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), - (NMUtilsCopyFunc) nm_ip_address_dup); - break; - case PROP_ROUTES: - g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip_route_unref); - priv->routes = _nm_utils_copy_array_to_slist (g_value_get_boxed (value), - (NMUtilsCopyFunc) nm_ip_route_dup); - break; - case PROP_IGNORE_AUTO_ROUTES: - priv->ignore_auto_routes = g_value_get_boolean (value); - break; - case PROP_IGNORE_AUTO_DNS: - priv->ignore_auto_dns = g_value_get_boolean (value); - break; - case PROP_DHCP_HOSTNAME: - g_free (priv->dhcp_hostname); - priv->dhcp_hostname = g_value_dup_string (value); - break; - case PROP_NEVER_DEFAULT: - priv->never_default = g_value_get_boolean (value); - break; - case PROP_MAY_FAIL: - priv->may_fail = g_value_get_boolean (value); - break; case PROP_IP6_PRIVACY: priv->ip6_privacy = g_value_get_enum (value); break; @@ -1002,36 +235,6 @@ get_property (GObject *object, guint prop_id, NMSettingIP6ConfigPrivate *priv = NM_SETTING_IP6_CONFIG_GET_PRIVATE (object); switch (prop_id) { - case PROP_METHOD: - g_value_set_string (value, priv->method); - break; - case PROP_DNS: - g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns)); - break; - case PROP_DNS_SEARCH: - g_value_take_boxed (value, _nm_utils_slist_to_strv (priv->dns_search)); - break; - case PROP_ADDRESSES: - g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->addresses, (NMUtilsCopyFunc) nm_ip_address_dup, (GDestroyNotify) nm_ip_address_unref)); - break; - case PROP_ROUTES: - g_value_take_boxed (value, _nm_utils_copy_slist_to_array (priv->routes, (NMUtilsCopyFunc) nm_ip_route_dup, (GDestroyNotify) nm_ip_route_unref)); - break; - case PROP_IGNORE_AUTO_ROUTES: - g_value_set_boolean (value, priv->ignore_auto_routes); - break; - case PROP_IGNORE_AUTO_DNS: - g_value_set_boolean (value, priv->ignore_auto_dns); - break; - case PROP_DHCP_HOSTNAME: - g_value_set_string (value, priv->dhcp_hostname); - 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; case PROP_IP6_PRIVACY: g_value_set_enum (value, priv->ip6_privacy); break; @@ -1042,200 +245,19 @@ get_property (GObject *object, guint prop_id, } static void -nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class) +nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *ip6_class) { - GObjectClass *object_class = G_OBJECT_CLASS (setting_class); - NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class); + GObjectClass *object_class = G_OBJECT_CLASS (ip6_class); + NMSettingClass *setting_class = NM_SETTING_CLASS (ip6_class); - g_type_class_add_private (setting_class, sizeof (NMSettingIP6ConfigPrivate)); + g_type_class_add_private (ip6_class, sizeof (NMSettingIP6ConfigPrivate)); /* virtual methods */ object_class->set_property = set_property; object_class->get_property = get_property; - object_class->finalize = finalize; - parent_class->verify = verify; + setting_class->verify = verify; /* Properties */ - /** - * NMSettingIP6Config:method: - * - * IPv6 configuration method. If "auto" is specified then the appropriate - * automatic method (PPP, router advertisement, etc) is used for the device - * and most other properties can be left unset. To force the use of DHCP - * only, specify "dhcp"; this method is only valid for Ethernet- based - * hardware. If "link-local" is specified, then an IPv6 link-local address - * will be assigned to the interface. If "manual" is specified, static IP - * addressing is used and at least one IP address must be given in the - * "addresses" property. If "ignore" is specified, IPv6 configuration is - * not done. This property must be set. Note: the "shared" method is not - * yet supported. - **/ - g_object_class_install_property - (object_class, PROP_METHOD, - g_param_spec_string (NM_SETTING_IP6_CONFIG_METHOD, "", "", - NULL, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP6Config:dhcp-hostname: - * - * The specified name will be sent to the DHCP server when acquiring a - * lease. - **/ - g_object_class_install_property - (object_class, PROP_DHCP_HOSTNAME, - g_param_spec_string (NM_SETTING_IP6_CONFIG_DHCP_HOSTNAME, "", "", - NULL, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP6Config:dns: - * - * Array of IPv6 addresses of DNS servers. For the "auto" method, these DNS - * servers are appended to those (if any) returned by automatic - * configuration. DNS servers cannot be used with the "shared" or - * "link-local" methods as there is no usptream network. In all other - * methods, these DNS servers are used as the only DNS servers for this - * connection. - **/ - g_object_class_install_property - (object_class, PROP_DNS, - g_param_spec_boxed (NM_SETTING_IP6_CONFIG_DNS, "", "", - G_TYPE_STRV, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); - _nm_setting_class_transform_property (parent_class, NM_SETTING_IP6_CONFIG_DNS, - G_VARIANT_TYPE ("aay"), - ip6_dns_to_dbus, - ip6_dns_from_dbus); - - /** - * NMSettingIP6Config:dns-search: - * - * List of DNS search domains. For the "auto" method, these search domains - * are appended to those returned by automatic configuration. Search domains - * cannot be used with the "shared" or "link-local" methods as there is no - * upstream network. In all other methods, these search domains are used as - * the only search domains for this connection. - **/ - g_object_class_install_property - (object_class, PROP_DNS_SEARCH, - g_param_spec_boxed (NM_SETTING_IP6_CONFIG_DNS_SEARCH, "", "", - G_TYPE_STRV, - G_PARAM_READWRITE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP6Config:addresses: - * - * Array of IPv6 addresses. For the 'auto' method, given IP addresses are - * appended to those returned by automatic configuration. Addresses cannot - * be used with the 'shared' or 'link-local' methods as the interface is - * automatically assigned an address with these methods. - * - * Element-Type: NMIPAddress - **/ - g_object_class_install_property - (object_class, PROP_ADDRESSES, - g_param_spec_boxed (NM_SETTING_IP6_CONFIG_ADDRESSES, "", "", - G_TYPE_PTR_ARRAY, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); - _nm_setting_class_transform_property (parent_class, NM_SETTING_IP6_CONFIG_ADDRESSES, - G_VARIANT_TYPE ("a(ayuay)"), - ip6_addresses_to_dbus, - ip6_addresses_from_dbus); - - /** - * NMSettingIP6Config:routes: - * - * Array of IPv6 routes. For the 'auto' method, given IP routes are appended - * to those returned by automatic configuration. Routes cannot be used with - * the 'shared' or 'link-local' methods because there is no upstream network. - * - * Element-Type: NMIPRoute - **/ - g_object_class_install_property - (object_class, PROP_ROUTES, - g_param_spec_boxed (NM_SETTING_IP6_CONFIG_ROUTES, "", "", - G_TYPE_PTR_ARRAY, - G_PARAM_READWRITE | - NM_SETTING_PARAM_INFERRABLE | - G_PARAM_STATIC_STRINGS)); - _nm_setting_class_transform_property (parent_class, NM_SETTING_IP6_CONFIG_ROUTES, - G_VARIANT_TYPE ("a(ayuayu)"), - ip6_routes_to_dbus, - ip6_routes_from_dbus); - - /** - * NMSettingIP6Config:ignore-auto-routes: - * - * When the method is set to "auto" or "dhcp" and this property is set to - * %TRUE, automatically configured routes are ignored and only routes - * specified in the #NMSettingIP6Config:routes property, if any, are used. - **/ - g_object_class_install_property - (object_class, PROP_IGNORE_AUTO_ROUTES, - g_param_spec_boolean (NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES, "", "", - FALSE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP6Config:ignore-auto-dns: - * - * When the method is set to "auto" or "dhcp" and this property is set to - * %TRUE, automatically configured nameservers and search domains are - * ignored and only nameservers and search domains specified in the - * #NMSettingIP6Config:dns and #NMSettingIP6Config:dns-search properties, if - * any, are used. - **/ - g_object_class_install_property - (object_class, PROP_IGNORE_AUTO_DNS, - g_param_spec_boolean (NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS, "", "", - FALSE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP6Config:never-default: - * - * If %TRUE, this connection will never be the default IPv6 connection, - * meaning it will never be assigned the default IPv6 route by - * NetworkManager. - **/ - g_object_class_install_property - (object_class, PROP_NEVER_DEFAULT, - g_param_spec_boolean (NM_SETTING_IP6_CONFIG_NEVER_DEFAULT, "", "", - FALSE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT | - G_PARAM_STATIC_STRINGS)); - - /** - * NMSettingIP6Config:may-fail: - * - * If %TRUE, allow overall network configuration to proceed even if IPv6 - * configuration times out. Note that at least one IP configuration must - * succeed or overall network configuration will still fail. For example, - * in IPv4-only networks, setting this property to %TRUE allows the overall - * network configuration to succeed if IPv6 configuration fails but IPv4 - * configuration completes successfully. - **/ - g_object_class_install_property - (object_class, PROP_MAY_FAIL, - g_param_spec_boolean (NM_SETTING_IP6_CONFIG_MAY_FAIL, "", "", - TRUE, - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT | - G_PARAM_STATIC_STRINGS)); - /** * NMSettingIP6Config:ip6-privacy: * @@ -1255,4 +277,23 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class) G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS)); + + /* IP6-specific property overrides */ + _nm_setting_class_transform_property (setting_class, + NM_SETTING_IP_CONFIG_DNS, + G_VARIANT_TYPE ("aay"), + ip6_dns_to_dbus, + ip6_dns_from_dbus); + + _nm_setting_class_transform_property (setting_class, + NM_SETTING_IP_CONFIG_ADDRESSES, + G_VARIANT_TYPE ("a(ayuay)"), + ip6_addresses_to_dbus, + ip6_addresses_from_dbus); + + _nm_setting_class_transform_property (setting_class, + NM_SETTING_IP_CONFIG_ROUTES, + G_VARIANT_TYPE ("a(ayuayu)"), + ip6_routes_to_dbus, + ip6_routes_from_dbus); } diff --git a/libnm-core/nm-setting-ip6-config.h b/libnm-core/nm-setting-ip6-config.h index 73b93eeae8..b791e937b4 100644 --- a/libnm-core/nm-setting-ip6-config.h +++ b/libnm-core/nm-setting-ip6-config.h @@ -26,9 +26,6 @@ #error "Only can be included directly." #endif -#include - -#include "nm-setting.h" #include "nm-setting-ip-config.h" G_BEGIN_DECLS @@ -42,18 +39,7 @@ G_BEGIN_DECLS #define NM_SETTING_IP6_CONFIG_SETTING_NAME "ipv6" -#define NM_SETTING_IP6_CONFIG_METHOD "method" -#define NM_SETTING_IP6_CONFIG_DNS "dns" -#define NM_SETTING_IP6_CONFIG_DNS_SEARCH "dns-search" -#define NM_SETTING_IP6_CONFIG_ADDRESSES "addresses" -#define NM_SETTING_IP6_CONFIG_ROUTES "routes" -#define NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES "ignore-auto-routes" -#define NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS "ignore-auto-dns" -#define NM_SETTING_IP6_CONFIG_NEVER_DEFAULT "never-default" -#define NM_SETTING_IP6_CONFIG_MAY_FAIL "may-fail" -#define NM_SETTING_IP6_CONFIG_IP6_PRIVACY "ip6-privacy" -#define NM_SETTING_IP6_CONFIG_DHCP_HOSTNAME "dhcp-hostname" - +#define NM_SETTING_IP6_CONFIG_IP6_PRIVACY "ip6-privacy" /** * NM_SETTING_IP6_CONFIG_METHOD_IGNORE: @@ -129,11 +115,11 @@ typedef enum { } NMSettingIP6ConfigPrivacy; struct _NMSettingIP6Config { - NMSetting parent; + NMSettingIPConfig parent; }; typedef struct { - NMSettingClass parent; + NMSettingIPConfigClass parent; /*< private >*/ gpointer padding[4]; @@ -141,42 +127,8 @@ typedef struct { GType nm_setting_ip6_config_get_type (void); -NMSetting * nm_setting_ip6_config_new (void); -const char * nm_setting_ip6_config_get_method (NMSettingIP6Config *setting); - -guint32 nm_setting_ip6_config_get_num_dns (NMSettingIP6Config *setting); -const char * nm_setting_ip6_config_get_dns (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_add_dns (NMSettingIP6Config *setting, const char *dns); -void nm_setting_ip6_config_remove_dns (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_remove_dns_by_value (NMSettingIP6Config *setting, const char *dns); -void nm_setting_ip6_config_clear_dns (NMSettingIP6Config *setting); - -guint32 nm_setting_ip6_config_get_num_dns_searches (NMSettingIP6Config *setting); -const char * nm_setting_ip6_config_get_dns_search (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_add_dns_search (NMSettingIP6Config *setting, const char *dns_search); -void nm_setting_ip6_config_remove_dns_search (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_remove_dns_search_by_value (NMSettingIP6Config *setting, const char *dns_search); -void nm_setting_ip6_config_clear_dns_searches (NMSettingIP6Config *setting); - -guint32 nm_setting_ip6_config_get_num_addresses (NMSettingIP6Config *setting); -NMIPAddress * nm_setting_ip6_config_get_address (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_add_address (NMSettingIP6Config *setting, NMIPAddress *address); -void nm_setting_ip6_config_remove_address (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_remove_address_by_value (NMSettingIP6Config *setting, NMIPAddress *address); -void nm_setting_ip6_config_clear_addresses (NMSettingIP6Config *setting); - -guint32 nm_setting_ip6_config_get_num_routes (NMSettingIP6Config *setting); -NMIPRoute * nm_setting_ip6_config_get_route (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_add_route (NMSettingIP6Config *setting, NMIPRoute *route); -void nm_setting_ip6_config_remove_route (NMSettingIP6Config *setting, guint32 i); -gboolean nm_setting_ip6_config_remove_route_by_value (NMSettingIP6Config *setting, NMIPRoute *route); -void nm_setting_ip6_config_clear_routes (NMSettingIP6Config *setting); -gboolean nm_setting_ip6_config_get_ignore_auto_routes (NMSettingIP6Config *setting); - -gboolean nm_setting_ip6_config_get_ignore_auto_dns (NMSettingIP6Config *setting); -const char * nm_setting_ip6_config_get_dhcp_hostname (NMSettingIP6Config *setting); -gboolean nm_setting_ip6_config_get_never_default (NMSettingIP6Config *setting); -gboolean nm_setting_ip6_config_get_may_fail (NMSettingIP6Config *setting); +NMSetting *nm_setting_ip6_config_new (void); + NMSettingIP6ConfigPrivacy nm_setting_ip6_config_get_ip6_privacy (NMSettingIP6Config *setting); G_END_DECLS diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index a028f84d4d..0f67da3733 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index b103ce8922..041c4263f4 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -323,7 +323,7 @@ test_setting_vpn_modify_during_foreach (void) static void test_setting_ip4_config_labels (void) { - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMIPAddress *addr; GVariant *label; GPtrArray *addrs; @@ -332,21 +332,21 @@ test_setting_ip4_config_labels (void) GVariant *dict, *setting_dict, *value; GError *error = NULL; - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); /* addr 1 */ addr = nm_ip_address_new (AF_INET, "1.1.1.1", 24, NULL, &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); - addr = nm_setting_ip4_config_get_address (s_ip4, 0); + addr = nm_setting_ip_config_get_address (s_ip4, 0); label = nm_ip_address_get_attribute (addr, "label"); g_assert (label == NULL); @@ -355,12 +355,12 @@ test_setting_ip4_config_labels (void) g_assert_no_error (error); nm_ip_address_set_attribute (addr, "label", g_variant_new_string ("eth0:1")); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); - addr = nm_setting_ip4_config_get_address (s_ip4, 1); + addr = nm_setting_ip_config_get_address (s_ip4, 1); label = nm_ip_address_get_attribute (addr, "label"); g_assert (label != NULL); g_assert_cmpstr (g_variant_get_string (label, NULL), ==, "eth0:1"); @@ -370,27 +370,27 @@ test_setting_ip4_config_labels (void) g_assert_no_error (error); nm_ip_address_set_attribute (addr, "label", NULL); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); - addr = nm_setting_ip4_config_get_address (s_ip4, 2); + addr = nm_setting_ip_config_get_address (s_ip4, 2); label = nm_ip_address_get_attribute (addr, "label"); g_assert (label == NULL); /* Remove addr 1 and re-verify remaining addresses */ - nm_setting_ip4_config_remove_address (s_ip4, 0); + nm_setting_ip_config_remove_address (s_ip4, 0); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); - addr = nm_setting_ip4_config_get_address (s_ip4, 0); + addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); label = nm_ip_address_get_attribute (addr, "label"); g_assert (label != NULL); g_assert_cmpstr (g_variant_get_string (label, NULL), ==, "eth0:1"); - addr = nm_setting_ip4_config_get_address (s_ip4, 1); + addr = nm_setting_ip_config_get_address (s_ip4, 1); g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); label = nm_ip_address_get_attribute (addr, "label"); g_assert (label == NULL); @@ -422,40 +422,40 @@ test_setting_ip4_config_labels (void) s_ip4 = nm_connection_get_setting_ip4_config (conn); - addr = nm_setting_ip4_config_get_address (s_ip4, 0); + addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); label = nm_ip_address_get_attribute (addr, "label"); g_assert (label != NULL); g_assert_cmpstr (g_variant_get_string (label, NULL), ==, "eth0:1"); - addr = nm_setting_ip4_config_get_address (s_ip4, 1); + addr = nm_setting_ip_config_get_address (s_ip4, 1); g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); label = nm_ip_address_get_attribute (addr, "label"); g_assert (label == NULL); /* Test explicit property assignment */ g_object_get (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_ADDRESSES, &addrs, + NM_SETTING_IP_CONFIG_ADDRESSES, &addrs, NULL); - nm_setting_ip4_config_clear_addresses (s_ip4); - g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 0); + nm_setting_ip_config_clear_addresses (s_ip4); + g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 0); g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_ADDRESSES, addrs, + NM_SETTING_IP_CONFIG_ADDRESSES, addrs, NULL); g_ptr_array_unref (addrs); nm_setting_verify (NM_SETTING (s_ip4), NULL, &error); g_assert_no_error (error); - g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); + g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 2); - addr = nm_setting_ip4_config_get_address (s_ip4, 0); + addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); label = nm_ip_address_get_attribute (addr, "label"); g_assert (label != NULL); g_assert_cmpstr (g_variant_get_string (label, NULL), ==, "eth0:1"); - addr = nm_setting_ip4_config_get_address (s_ip4, 1); + addr = nm_setting_ip_config_get_address (s_ip4, 1); g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); label = nm_ip_address_get_attribute (addr, "label"); g_assert (label == NULL); @@ -996,8 +996,8 @@ new_test_connection (void) setting = nm_setting_ip4_config_new (); g_object_set (G_OBJECT (setting), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, - NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, "eyeofthetiger", + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, "eyeofthetiger", NULL); nm_connection_add_setting (connection, setting); @@ -1045,7 +1045,7 @@ new_connection_dict (char **out_uuid, /* IP6 */ g_variant_builder_init (&setting_builder, NM_VARIANT_TYPE_SETTING); g_variant_builder_add (&setting_builder, "{sv}", - NM_SETTING_IP6_CONFIG_METHOD, + NM_SETTING_IP_CONFIG_METHOD, g_variant_new_string (*out_expected_ip6_method)); g_variant_builder_add (&conn_builder, "{sa{sv}}", NM_SETTING_IP6_CONFIG_SETTING_NAME, @@ -1062,7 +1062,7 @@ test_connection_replace_settings (void) GError *error = NULL; gboolean success; NMSettingConnection *s_con; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; char *uuid = NULL; const char *expected_id = NULL, *expected_method = NULL; @@ -1086,7 +1086,7 @@ test_connection_replace_settings (void) s_ip6 = nm_connection_get_setting_ip6_config (connection); g_assert (s_ip6); - g_assert_cmpstr (nm_setting_ip6_config_get_method (s_ip6), ==, expected_method); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, expected_method); g_free (uuid); g_variant_unref (new_settings); @@ -1217,7 +1217,7 @@ test_connection_new_from_dbus (void) GVariant *new_settings; GError *error = NULL; NMSettingConnection *s_con; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; char *uuid = NULL; const char *expected_id = NULL, *expected_method = NULL; @@ -1239,7 +1239,7 @@ test_connection_new_from_dbus (void) s_ip6 = nm_connection_get_setting_ip6_config (connection); g_assert (s_ip6); - g_assert_cmpstr (nm_setting_ip6_config_get_method (s_ip6), ==, expected_method); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, expected_method); g_free (uuid); g_variant_unref (new_settings); @@ -1630,18 +1630,18 @@ test_connection_diff_a_only (void) { NULL, NM_SETTING_DIFF_RESULT_UNKNOWN }, } }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, { - { NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_DIFF_RESULT_IN_A }, - { NM_SETTING_IP4_CONFIG_DNS, NM_SETTING_DIFF_RESULT_IN_A }, - { NM_SETTING_IP4_CONFIG_DNS_SEARCH, NM_SETTING_DIFF_RESULT_IN_A }, - { NM_SETTING_IP4_CONFIG_ADDRESSES, NM_SETTING_DIFF_RESULT_IN_A }, - { NM_SETTING_IP4_CONFIG_ROUTES, NM_SETTING_DIFF_RESULT_IN_A }, - { NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, NM_SETTING_DIFF_RESULT_IN_A }, - { NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_DNS, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_DNS_SEARCH, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_ADDRESSES, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_ROUTES, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, NM_SETTING_DIFF_RESULT_IN_A }, - { NM_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME, NM_SETTING_DIFF_RESULT_IN_A }, - { NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, NM_SETTING_DIFF_RESULT_IN_A }, - { NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, NM_SETTING_DIFF_RESULT_IN_A }, - { NM_SETTING_IP4_CONFIG_MAY_FAIL, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_NEVER_DEFAULT, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_MAY_FAIL, NM_SETTING_DIFF_RESULT_IN_A }, { NULL, NM_SETTING_DIFF_RESULT_UNKNOWN }, } }, }; @@ -1681,11 +1681,11 @@ test_connection_diff_different (void) { NMConnection *a, *b; GHashTable *out_diffs = NULL; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; gboolean same; const DiffSetting settings[] = { { NM_SETTING_IP4_CONFIG_SETTING_NAME, { - { NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_DIFF_RESULT_IN_A | NM_SETTING_DIFF_RESULT_IN_B }, + { NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_DIFF_RESULT_IN_A | NM_SETTING_DIFF_RESULT_IN_B }, { NULL, NM_SETTING_DIFF_RESULT_UNKNOWN }, } }, }; @@ -1695,7 +1695,7 @@ test_connection_diff_different (void) s_ip4 = nm_connection_get_setting_ip4_config (a); g_assert (s_ip4); g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); same = nm_connection_diff (a, b, NM_SETTING_COMPARE_FLAG_EXACT, &out_diffs); @@ -1766,7 +1766,7 @@ test_connection_diff_inferrable (void) gboolean same; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *uuid; const DiffSetting settings[] = { { NM_SETTING_CONNECTION_SETTING_NAME, { @@ -1794,7 +1794,7 @@ test_connection_diff_inferrable (void) s_ip4 = nm_connection_get_setting_ip4_config (a); g_assert (s_ip4); - g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, TRUE, NULL); + g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, TRUE, NULL); /* Make sure the diff returns no results as secrets are ignored */ same = nm_connection_diff (a, b, NM_SETTING_COMPARE_FLAG_INFERRABLE, &out_diffs); @@ -1836,11 +1836,11 @@ add_generic_settings (NMConnection *connection, const char *ctype) g_free (uuid); setting = nm_setting_ip4_config_new (); - g_object_set (setting, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (setting, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (connection, setting); setting = nm_setting_ip6_config_new (); - g_object_set (setting, NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); + g_object_set (setting, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (connection, setting); } @@ -2446,7 +2446,7 @@ test_setting_ip4_changed_signal (void) { NMConnection *connection; gboolean changed = FALSE; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMIPAddress *addr; NMIPRoute *route; GError *error = NULL; @@ -2457,53 +2457,53 @@ test_setting_ip4_changed_signal (void) (GCallback) test_connection_changed_cb, &changed); - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - ASSERT_CHANGED (nm_setting_ip4_config_add_dns (s_ip4, "11.22.0.0")); - ASSERT_CHANGED (nm_setting_ip4_config_remove_dns (s_ip4, 0)); + ASSERT_CHANGED (nm_setting_ip_config_add_dns (s_ip4, "11.22.0.0")); + ASSERT_CHANGED (nm_setting_ip_config_remove_dns (s_ip4, 0)); - g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*elt != NULL*"); - ASSERT_UNCHANGED (nm_setting_ip4_config_remove_dns (s_ip4, 1)); + g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*i < priv->dns->len*"); + ASSERT_UNCHANGED (nm_setting_ip_config_remove_dns (s_ip4, 1)); g_test_assert_expected_messages (); - nm_setting_ip4_config_add_dns (s_ip4, "33.44.0.0"); - ASSERT_CHANGED (nm_setting_ip4_config_clear_dns (s_ip4)); + nm_setting_ip_config_add_dns (s_ip4, "33.44.0.0"); + ASSERT_CHANGED (nm_setting_ip_config_clear_dns (s_ip4)); - ASSERT_CHANGED (nm_setting_ip4_config_add_dns_search (s_ip4, "foobar.com")); - ASSERT_CHANGED (nm_setting_ip4_config_remove_dns_search (s_ip4, 0)); + ASSERT_CHANGED (nm_setting_ip_config_add_dns_search (s_ip4, "foobar.com")); + ASSERT_CHANGED (nm_setting_ip_config_remove_dns_search (s_ip4, 0)); - g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*elt != NULL*"); - ASSERT_UNCHANGED (nm_setting_ip4_config_remove_dns_search (s_ip4, 1)); + g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*i < priv->dns_search->len*"); + ASSERT_UNCHANGED (nm_setting_ip_config_remove_dns_search (s_ip4, 1)); g_test_assert_expected_messages (); - ASSERT_CHANGED (nm_setting_ip4_config_add_dns_search (s_ip4, "foobar.com")); - ASSERT_CHANGED (nm_setting_ip4_config_clear_dns_searches (s_ip4)); + ASSERT_CHANGED (nm_setting_ip_config_add_dns_search (s_ip4, "foobar.com")); + ASSERT_CHANGED (nm_setting_ip_config_clear_dns_searches (s_ip4)); addr = nm_ip_address_new (AF_INET, "22.33.0.0", 24, NULL, &error); g_assert_no_error (error); - ASSERT_CHANGED (nm_setting_ip4_config_add_address (s_ip4, addr)); - ASSERT_CHANGED (nm_setting_ip4_config_remove_address (s_ip4, 0)); + ASSERT_CHANGED (nm_setting_ip_config_add_address (s_ip4, addr)); + ASSERT_CHANGED (nm_setting_ip_config_remove_address (s_ip4, 0)); - g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*addr != NULL*"); - ASSERT_UNCHANGED (nm_setting_ip4_config_remove_address (s_ip4, 1)); + g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*i < priv->addresses->len*"); + ASSERT_UNCHANGED (nm_setting_ip_config_remove_address (s_ip4, 1)); g_test_assert_expected_messages (); - nm_setting_ip4_config_add_address (s_ip4, addr); - ASSERT_CHANGED (nm_setting_ip4_config_clear_addresses (s_ip4)); + nm_setting_ip_config_add_address (s_ip4, addr); + ASSERT_CHANGED (nm_setting_ip_config_clear_addresses (s_ip4)); route = nm_ip_route_new (AF_INET, "22.33.0.0", 24, NULL, 0, &error); g_assert_no_error (error); - ASSERT_CHANGED (nm_setting_ip4_config_add_route (s_ip4, route)); - ASSERT_CHANGED (nm_setting_ip4_config_remove_route (s_ip4, 0)); + ASSERT_CHANGED (nm_setting_ip_config_add_route (s_ip4, route)); + ASSERT_CHANGED (nm_setting_ip_config_remove_route (s_ip4, 0)); - g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*elt != NULL*"); - ASSERT_UNCHANGED (nm_setting_ip4_config_remove_route (s_ip4, 1)); + g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*i < priv->routes->len*"); + ASSERT_UNCHANGED (nm_setting_ip_config_remove_route (s_ip4, 1)); g_test_assert_expected_messages (); - nm_setting_ip4_config_add_route (s_ip4, route); - ASSERT_CHANGED (nm_setting_ip4_config_clear_routes (s_ip4)); + nm_setting_ip_config_add_route (s_ip4, route); + ASSERT_CHANGED (nm_setting_ip_config_clear_routes (s_ip4)); nm_ip_address_unref (addr); nm_ip_route_unref (route); @@ -2515,7 +2515,7 @@ test_setting_ip6_changed_signal (void) { NMConnection *connection; gboolean changed = FALSE; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; NMIPAddress *addr; NMIPRoute *route; GError *error = NULL; @@ -2526,54 +2526,54 @@ test_setting_ip6_changed_signal (void) (GCallback) test_connection_changed_cb, &changed); - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); - ASSERT_CHANGED (nm_setting_ip6_config_add_dns (s_ip6, "1:2:3::4:5:6")); - ASSERT_CHANGED (nm_setting_ip6_config_remove_dns (s_ip6, 0)); + ASSERT_CHANGED (nm_setting_ip_config_add_dns (s_ip6, "1:2:3::4:5:6")); + ASSERT_CHANGED (nm_setting_ip_config_remove_dns (s_ip6, 0)); - g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*elt != NULL*"); - ASSERT_UNCHANGED (nm_setting_ip6_config_remove_dns (s_ip6, 1)); + g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*i < priv->dns->len*"); + ASSERT_UNCHANGED (nm_setting_ip_config_remove_dns (s_ip6, 1)); g_test_assert_expected_messages (); - nm_setting_ip6_config_add_dns (s_ip6, "1:2:3::4:5:6"); - ASSERT_CHANGED (nm_setting_ip6_config_clear_dns (s_ip6)); + nm_setting_ip_config_add_dns (s_ip6, "1:2:3::4:5:6"); + ASSERT_CHANGED (nm_setting_ip_config_clear_dns (s_ip6)); - ASSERT_CHANGED (nm_setting_ip6_config_add_dns_search (s_ip6, "foobar.com")); - ASSERT_CHANGED (nm_setting_ip6_config_remove_dns_search (s_ip6, 0)); + ASSERT_CHANGED (nm_setting_ip_config_add_dns_search (s_ip6, "foobar.com")); + ASSERT_CHANGED (nm_setting_ip_config_remove_dns_search (s_ip6, 0)); - g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*elt != NULL*"); - ASSERT_UNCHANGED (nm_setting_ip6_config_remove_dns_search (s_ip6, 1)); + g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*i < priv->dns_search->len*"); + ASSERT_UNCHANGED (nm_setting_ip_config_remove_dns_search (s_ip6, 1)); g_test_assert_expected_messages (); - nm_setting_ip6_config_add_dns_search (s_ip6, "foobar.com"); - ASSERT_CHANGED (nm_setting_ip6_config_clear_dns_searches (s_ip6)); + nm_setting_ip_config_add_dns_search (s_ip6, "foobar.com"); + ASSERT_CHANGED (nm_setting_ip_config_clear_dns_searches (s_ip6)); addr = nm_ip_address_new (AF_INET6, "1:2:3::4:5:6", 64, NULL, &error); g_assert_no_error (error); - ASSERT_CHANGED (nm_setting_ip6_config_add_address (s_ip6, addr)); - ASSERT_CHANGED (nm_setting_ip6_config_remove_address (s_ip6, 0)); + ASSERT_CHANGED (nm_setting_ip_config_add_address (s_ip6, addr)); + ASSERT_CHANGED (nm_setting_ip_config_remove_address (s_ip6, 0)); - g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*elt != NULL*"); - ASSERT_UNCHANGED (nm_setting_ip6_config_remove_address (s_ip6, 1)); + g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*i < priv->addresses->len*"); + ASSERT_UNCHANGED (nm_setting_ip_config_remove_address (s_ip6, 1)); g_test_assert_expected_messages (); - nm_setting_ip6_config_add_address (s_ip6, addr); - ASSERT_CHANGED (nm_setting_ip6_config_clear_addresses (s_ip6)); + nm_setting_ip_config_add_address (s_ip6, addr); + ASSERT_CHANGED (nm_setting_ip_config_clear_addresses (s_ip6)); route = nm_ip_route_new (AF_INET6, "1:2:3::4:5:6", 128, NULL, 0, &error); g_assert_no_error (error); - ASSERT_CHANGED (nm_setting_ip6_config_add_route (s_ip6, route)); - ASSERT_CHANGED (nm_setting_ip6_config_remove_route (s_ip6, 0)); + ASSERT_CHANGED (nm_setting_ip_config_add_route (s_ip6, route)); + ASSERT_CHANGED (nm_setting_ip_config_remove_route (s_ip6, 0)); - g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*elt != NULL*"); - ASSERT_UNCHANGED (nm_setting_ip6_config_remove_route (s_ip6, 1)); + g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*i < priv->routes->len*"); + ASSERT_UNCHANGED (nm_setting_ip_config_remove_route (s_ip6, 1)); g_test_assert_expected_messages (); - nm_setting_ip6_config_add_route (s_ip6, route); - ASSERT_CHANGED (nm_setting_ip6_config_clear_routes (s_ip6)); + nm_setting_ip_config_add_route (s_ip6, route); + ASSERT_CHANGED (nm_setting_ip_config_clear_routes (s_ip6)); nm_ip_address_unref (addr); nm_ip_route_unref (route); @@ -2834,12 +2834,12 @@ test_connection_normalize_virtual_iface_name (void) nm_connection_add_setting (con, g_object_new (NM_TYPE_SETTING_IP4_CONFIG, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL)); nm_connection_add_setting (con, g_object_new (NM_TYPE_SETTING_IP6_CONFIG, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL)); s_vlan = nm_connection_get_setting_vlan (con); diff --git a/libnm-core/tests/test-secrets.c b/libnm-core/tests/test-secrets.c index b93a1ba155..bb4b4daea5 100644 --- a/libnm-core/tests/test-secrets.c +++ b/libnm-core/tests/test-secrets.c @@ -125,7 +125,7 @@ make_tls_connection (const char *detail, NMSetting8021xCKScheme scheme) s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, detail, "failed to verify connection: %s", @@ -293,7 +293,7 @@ make_tls_phase2_connection (const char *detail, NMSetting8021xCKScheme scheme) s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, detail, "failed to verify connection: %s", diff --git a/libnm/libnm.ver b/libnm/libnm.ver index b40c232591..5a5d534bfc 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -551,74 +551,45 @@ global: nm_setting_infiniband_get_type; nm_setting_infiniband_get_virtual_interface_name; nm_setting_infiniband_new; - nm_setting_ip4_config_add_address; - nm_setting_ip4_config_add_dns; - nm_setting_ip4_config_add_dns_search; - nm_setting_ip4_config_add_route; - nm_setting_ip4_config_clear_addresses; - nm_setting_ip4_config_clear_dns; - nm_setting_ip4_config_clear_dns_searches; - nm_setting_ip4_config_clear_routes; - nm_setting_ip4_config_get_address; nm_setting_ip4_config_get_dhcp_client_id; - nm_setting_ip4_config_get_dhcp_hostname; - nm_setting_ip4_config_get_dhcp_send_hostname; - nm_setting_ip4_config_get_dns; - nm_setting_ip4_config_get_dns_search; - nm_setting_ip4_config_get_ignore_auto_dns; - nm_setting_ip4_config_get_ignore_auto_routes; - nm_setting_ip4_config_get_may_fail; - nm_setting_ip4_config_get_method; - nm_setting_ip4_config_get_never_default; - nm_setting_ip4_config_get_num_addresses; - nm_setting_ip4_config_get_num_dns; - nm_setting_ip4_config_get_num_dns_searches; - nm_setting_ip4_config_get_num_routes; - nm_setting_ip4_config_get_route; nm_setting_ip4_config_get_type; nm_setting_ip4_config_new; - nm_setting_ip4_config_remove_address; - nm_setting_ip4_config_remove_address_by_value; - nm_setting_ip4_config_remove_dns; - nm_setting_ip4_config_remove_dns_by_value; - nm_setting_ip4_config_remove_dns_search; - nm_setting_ip4_config_remove_dns_search_by_value; - nm_setting_ip4_config_remove_route; - nm_setting_ip4_config_remove_route_by_value; - nm_setting_ip6_config_add_address; - nm_setting_ip6_config_add_dns; - nm_setting_ip6_config_add_dns_search; - nm_setting_ip6_config_add_route; - nm_setting_ip6_config_clear_addresses; - nm_setting_ip6_config_clear_dns; - nm_setting_ip6_config_clear_dns_searches; - nm_setting_ip6_config_clear_routes; - nm_setting_ip6_config_get_address; - nm_setting_ip6_config_get_dhcp_hostname; - nm_setting_ip6_config_get_dns; - nm_setting_ip6_config_get_dns_search; - nm_setting_ip6_config_get_ignore_auto_dns; - nm_setting_ip6_config_get_ignore_auto_routes; nm_setting_ip6_config_get_ip6_privacy; - nm_setting_ip6_config_get_may_fail; - nm_setting_ip6_config_get_method; - nm_setting_ip6_config_get_never_default; - nm_setting_ip6_config_get_num_addresses; - nm_setting_ip6_config_get_num_dns; - nm_setting_ip6_config_get_num_dns_searches; - nm_setting_ip6_config_get_num_routes; - nm_setting_ip6_config_get_route; nm_setting_ip6_config_get_type; nm_setting_ip6_config_new; nm_setting_ip6_config_privacy_get_type; - nm_setting_ip6_config_remove_address; - nm_setting_ip6_config_remove_address_by_value; - nm_setting_ip6_config_remove_dns; - nm_setting_ip6_config_remove_dns_by_value; - nm_setting_ip6_config_remove_dns_search; - nm_setting_ip6_config_remove_dns_search_by_value; - nm_setting_ip6_config_remove_route; - nm_setting_ip6_config_remove_route_by_value; + nm_setting_ip_config_add_address; + nm_setting_ip_config_add_dns; + nm_setting_ip_config_add_dns_search; + nm_setting_ip_config_add_route; + nm_setting_ip_config_clear_addresses; + nm_setting_ip_config_clear_dns; + nm_setting_ip_config_clear_dns_searches; + nm_setting_ip_config_clear_routes; + nm_setting_ip_config_get_address; + nm_setting_ip_config_get_dhcp_hostname; + nm_setting_ip_config_get_dhcp_send_hostname; + nm_setting_ip_config_get_dns; + nm_setting_ip_config_get_dns_search; + nm_setting_ip_config_get_ignore_auto_dns; + nm_setting_ip_config_get_ignore_auto_routes; + nm_setting_ip_config_get_may_fail; + nm_setting_ip_config_get_method; + nm_setting_ip_config_get_never_default; + nm_setting_ip_config_get_num_addresses; + nm_setting_ip_config_get_num_dns; + nm_setting_ip_config_get_num_dns_searches; + nm_setting_ip_config_get_num_routes; + nm_setting_ip_config_get_route; + nm_setting_ip_config_get_type; + nm_setting_ip_config_remove_address; + nm_setting_ip_config_remove_address_by_value; + nm_setting_ip_config_remove_dns; + nm_setting_ip_config_remove_dns_by_value; + nm_setting_ip_config_remove_dns_search; + nm_setting_ip_config_remove_dns_search_by_value; + nm_setting_ip_config_remove_route; + nm_setting_ip_config_remove_route_by_value; nm_setting_lookup_type; nm_setting_olpc_mesh_get_channel; nm_setting_olpc_mesh_get_dhcp_anycast_address; diff --git a/po/POTFILES.in b/po/POTFILES.in index 6d23a67b77..5c44103240 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -59,6 +59,7 @@ libnm-core/nm-setting-connection.c libnm-core/nm-setting-dcb.c libnm-core/nm-setting-gsm.c libnm-core/nm-setting-infiniband.c +libnm-core/nm-setting-ip-config.c libnm-core/nm-setting-ip4-config.c libnm-core/nm-setting-ip6-config.c libnm-core/nm-setting-olpc-mesh.c diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 9b624a2f6b..19896696bb 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -1212,8 +1212,7 @@ nm_utils_get_ip_config_method (NMConnection *connection, GType ip_setting_type) { NMSettingConnection *s_con; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4, *s_ip6; const char *method; s_con = nm_connection_get_setting_connection (connection); @@ -1226,7 +1225,7 @@ nm_utils_get_ip_config_method (NMConnection *connection, else { s_ip4 = nm_connection_get_setting_ip4_config (connection); g_return_val_if_fail (s_ip4 != NULL, NM_SETTING_IP4_CONFIG_METHOD_AUTO); - method = nm_setting_ip4_config_get_method (s_ip4); + method = nm_setting_ip_config_get_method (s_ip4); g_return_val_if_fail (method != NULL, NM_SETTING_IP4_CONFIG_METHOD_AUTO); return method; @@ -1240,7 +1239,7 @@ nm_utils_get_ip_config_method (NMConnection *connection, else { s_ip6 = nm_connection_get_setting_ip6_config (connection); g_return_val_if_fail (s_ip6 != NULL, NM_SETTING_IP6_CONFIG_METHOD_AUTO); - method = nm_setting_ip6_config_get_method (s_ip6); + method = nm_setting_ip_config_get_method (s_ip6); g_return_val_if_fail (method != NULL, NM_SETTING_IP6_CONFIG_METHOD_AUTO); return method; @@ -1387,12 +1386,12 @@ check_ip6_method (NMConnection *orig, { GHashTable *props; const char *orig_ip6_method, *candidate_ip6_method; - NMSettingIP6Config *candidate_ip6; + NMSettingIPConfig *candidate_ip6; gboolean allow = FALSE; props = check_property_in_hash (settings, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); if (!props) return TRUE; @@ -1408,7 +1407,7 @@ check_ip6_method (NMConnection *orig, if ( strcmp (orig_ip6_method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0 && strcmp (candidate_ip6_method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0 - && (!candidate_ip6 || nm_setting_ip6_config_get_may_fail (candidate_ip6))) { + && (!candidate_ip6 || nm_setting_ip_config_get_may_fail (candidate_ip6))) { allow = TRUE; } @@ -1425,7 +1424,7 @@ check_ip6_method (NMConnection *orig, if (allow) { remove_from_hash (settings, props, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); } return allow; } @@ -1438,11 +1437,11 @@ check_ip4_method (NMConnection *orig, { GHashTable *props; const char *orig_ip4_method, *candidate_ip4_method; - NMSettingIP4Config *candidate_ip4; + NMSettingIPConfig *candidate_ip4; props = check_property_in_hash (settings, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); if (!props) return TRUE; @@ -1457,11 +1456,11 @@ check_ip4_method (NMConnection *orig, if ( strcmp (orig_ip4_method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0 && strcmp (candidate_ip4_method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0 - && (!candidate_ip4 || nm_setting_ip4_config_get_may_fail (candidate_ip4)) + && (!candidate_ip4 || nm_setting_ip_config_get_may_fail (candidate_ip4)) && (device_has_carrier == FALSE)) { remove_from_hash (settings, props, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); return TRUE; } return FALSE; diff --git a/src/devices/bluetooth/nm-bluez-device.c b/src/devices/bluetooth/nm-bluez-device.c index 74a89a5f64..40fb31819b 100644 --- a/src/devices/bluetooth/nm-bluez-device.c +++ b/src/devices/bluetooth/nm-bluez-device.c @@ -211,16 +211,16 @@ pan_connection_check_create (NMBluezDevice *self) /* Setting: IPv4 */ setting = nm_setting_ip4_config_new (); g_object_set (G_OBJECT (setting), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, - NM_SETTING_IP4_CONFIG_MAY_FAIL, FALSE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_MAY_FAIL, FALSE, NULL); nm_connection_add_setting (connection, setting); /* Setting: IPv6 */ setting = nm_setting_ip6_config_new (); g_object_set (G_OBJECT (setting), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); nm_connection_add_setting (connection, setting); diff --git a/src/devices/nm-device-gre.c b/src/devices/nm-device-gre.c index e7df12e3f8..9c25b398ce 100644 --- a/src/devices/nm-device-gre.c +++ b/src/devices/nm-device-gre.c @@ -21,6 +21,7 @@ #include "config.h" #include +#include #include "nm-device-gre.h" #include "nm-device-private.h" diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 6071b9527d..a9ea2cb61c 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -2106,8 +2106,7 @@ gboolean nm_device_ip_config_should_fail (NMDevice *self, gboolean ip6) { NMConnection *connection; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4, *s_ip6; g_return_val_if_fail (self != NULL, TRUE); @@ -2117,11 +2116,11 @@ nm_device_ip_config_should_fail (NMDevice *self, gboolean ip6) /* Fail the connection if the failed IP method is required to complete */ if (ip6) { s_ip6 = nm_connection_get_setting_ip6_config (connection); - if (!nm_setting_ip6_config_get_may_fail (s_ip6)) + if (!nm_setting_ip_config_get_may_fail (s_ip6)) return TRUE; } else { s_ip4 = nm_connection_get_setting_ip4_config (connection); - if (!nm_setting_ip4_config_get_may_fail (s_ip4)) + if (!nm_setting_ip_config_get_may_fail (s_ip4)) return TRUE; } @@ -2778,7 +2777,7 @@ dhcp4_start (NMDevice *self, NMDeviceStateReason *reason) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; const guint8 *hw_addr; size_t hw_addr_len = 0; GByteArray *tmp = NULL; @@ -2804,9 +2803,9 @@ dhcp4_start (NMDevice *self, tmp, nm_connection_get_uuid (connection), nm_device_get_priority (self), - nm_setting_ip4_config_get_dhcp_send_hostname (s_ip4), - nm_setting_ip4_config_get_dhcp_hostname (s_ip4), - nm_setting_ip4_config_get_dhcp_client_id (s_ip4), + nm_setting_ip_config_get_dhcp_send_hostname (s_ip4), + nm_setting_ip_config_get_dhcp_hostname (s_ip4), + nm_setting_ip4_config_get_dhcp_client_id (NM_SETTING_IP4_CONFIG (s_ip4)), priv->dhcp_timeout, priv->dhcp_anycast_address); @@ -2864,16 +2863,16 @@ release_shared_ip (gpointer data) } static gboolean -reserve_shared_ip (NMDevice *self, NMSettingIP4Config *s_ip4, NMPlatformIP4Address *address) +reserve_shared_ip (NMDevice *self, NMSettingIPConfig *s_ip4, NMPlatformIP4Address *address) { if (G_UNLIKELY (shared_ips == NULL)) shared_ips = g_hash_table_new (g_direct_hash, g_direct_equal); memset (address, 0, sizeof (*address)); - if (s_ip4 && nm_setting_ip4_config_get_num_addresses (s_ip4)) { + if (s_ip4 && nm_setting_ip_config_get_num_addresses (s_ip4)) { /* Use the first user-supplied address */ - NMIPAddress *user = nm_setting_ip4_config_get_address (s_ip4, 0); + NMIPAddress *user = nm_setting_ip_config_get_address (s_ip4, 0); g_assert (user); nm_ip_address_get_address_binary (user, &address->address); @@ -2964,8 +2963,7 @@ connection_ip6_method_requires_carrier (NMConnection *connection, static gboolean connection_requires_carrier (NMConnection *connection) { - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4, *s_ip6; gboolean ip4_carrier_wanted, ip6_carrier_wanted; gboolean ip4_used = FALSE, ip6_used = FALSE; @@ -2975,7 +2973,7 @@ connection_requires_carrier (NMConnection *connection) * requires a carrier regardless of the IPv6 method. */ s_ip4 = nm_connection_get_setting_ip4_config (connection); - if (s_ip4 && !nm_setting_ip4_config_get_may_fail (s_ip4)) + if (s_ip4 && !nm_setting_ip_config_get_may_fail (s_ip4)) return TRUE; } @@ -2985,7 +2983,7 @@ connection_requires_carrier (NMConnection *connection) * requires a carrier regardless of the IPv4 method. */ s_ip6 = nm_connection_get_setting_ip6_config (connection); - if (s_ip6 && !nm_setting_ip6_config_get_may_fail (s_ip6)) + if (s_ip6 && !nm_setting_ip_config_get_may_fail (s_ip6)) return TRUE; } @@ -3320,7 +3318,7 @@ dhcp6_start (NMDevice *self, guint32 dhcp_opt, NMDeviceStateReason *reason) { - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); NMActStageReturn ret = NM_ACT_STAGE_RETURN_FAILURE; GByteArray *tmp = NULL; @@ -3358,11 +3356,11 @@ dhcp6_start (NMDevice *self, tmp, nm_connection_get_uuid (connection), nm_device_get_priority (self), - nm_setting_ip6_config_get_dhcp_hostname (s_ip6), + nm_setting_ip_config_get_dhcp_hostname (s_ip6), priv->dhcp_timeout, priv->dhcp_anycast_address, (dhcp_opt == NM_RDISC_DHCP_LEVEL_OTHERCONF) ? TRUE : FALSE, - nm_setting_ip6_config_get_ip6_privacy (s_ip6)); + nm_setting_ip6_config_get_ip6_privacy (NM_SETTING_IP6_CONFIG (s_ip6))); if (tmp) g_byte_array_free (tmp, TRUE); @@ -3373,8 +3371,8 @@ dhcp6_start (NMDevice *self, self); s_ip6 = nm_connection_get_setting_ip6_config (connection); - if (!nm_setting_ip6_config_get_may_fail (s_ip6) || - !strcmp (nm_setting_ip6_config_get_method (s_ip6), NM_SETTING_IP6_CONFIG_METHOD_DHCP)) + if (!nm_setting_ip_config_get_may_fail (s_ip6) || + !strcmp (nm_setting_ip_config_get_method (s_ip6), NM_SETTING_IP6_CONFIG_METHOD_DHCP)) nm_device_add_pending_action (self, PENDING_ACTION_DHCP6, TRUE); /* DHCP devices will be notified by the DHCP manager when stuff happens */ @@ -3858,7 +3856,7 @@ addrconf6_start (NMDevice *self, NMSettingIP6ConfigPrivacy use_tempaddr) priv->rdisc_use_tempaddr = use_tempaddr; print_support_extended_ifa_flags (use_tempaddr); - if (!nm_setting_ip6_config_get_may_fail (nm_connection_get_setting_ip6_config (connection))) + if (!nm_setting_ip_config_get_may_fail (nm_connection_get_setting_ip6_config (connection))) nm_device_add_pending_action (self, PENDING_ACTION_AUTOCONF6, TRUE); /* ensure link local is ready... */ @@ -4116,10 +4114,10 @@ act_stage3_ip6_config_start (NMDevice *self, */ ip6_privacy = ip6_use_tempaddr (); if (ip6_privacy == NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN) { - NMSettingIP6Config *s_ip6 = nm_connection_get_setting_ip6_config (connection); + NMSettingIPConfig *s_ip6 = nm_connection_get_setting_ip6_config (connection); if (s_ip6) - ip6_privacy = nm_setting_ip6_config_get_ip6_privacy (s_ip6); + ip6_privacy = nm_setting_ip6_config_get_ip6_privacy (NM_SETTING_IP6_CONFIG (s_ip6)); } ip6_privacy = use_tempaddr_clamp (ip6_privacy); @@ -4648,7 +4646,7 @@ send_arps (NMDevice *self, const char *mode_arg) const char *argv[] = { NULL, mode_arg, "-q", "-I", nm_device_get_ip_iface (self), "-c", "1", NULL, NULL }; int ip_arg = G_N_ELEMENTS (argv) - 2; NMConnection *connection; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; int i, num; NMIPAddress *addr; GError *error = NULL; @@ -4659,7 +4657,7 @@ send_arps (NMDevice *self, const char *mode_arg) s_ip4 = nm_connection_get_setting_ip4_config (connection); if (!s_ip4) return; - num = nm_setting_ip4_config_get_num_addresses (s_ip4); + num = nm_setting_ip_config_get_num_addresses (s_ip4); if (num == 0) return; @@ -4671,7 +4669,7 @@ send_arps (NMDevice *self, const char *mode_arg) for (i = 0; i < num; i++) { gs_free char *tmp_str = NULL; - addr = nm_setting_ip4_config_get_address (s_ip4, i); + addr = nm_setting_ip_config_get_address (s_ip4, i); argv[ip_arg] = nm_ip_address_get_address (addr); _LOGD (LOGD_DEVICE | LOGD_IP4, @@ -4719,7 +4717,7 @@ arp_announce (NMDevice *self) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); NMConnection *connection; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; int num; arp_cleanup (self); @@ -4733,7 +4731,7 @@ arp_announce (NMDevice *self) s_ip4 = nm_connection_get_setting_ip4_config (connection); if (!s_ip4) return; - num = nm_setting_ip4_config_get_num_addresses (s_ip4); + num = nm_setting_ip_config_get_num_addresses (s_ip4); if (num == 0) return; diff --git a/src/devices/wifi/nm-device-wifi.c b/src/devices/wifi/nm-device-wifi.c index 1210106a0e..f8b4c2a3d6 100644 --- a/src/devices/wifi/nm-device-wifi.c +++ b/src/devices/wifi/nm-device-wifi.c @@ -2809,14 +2809,14 @@ act_stage3_ip4_config_start (NMDevice *device, NMDeviceStateReason *reason) { NMConnection *connection; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; const char *method = NM_SETTING_IP4_CONFIG_METHOD_AUTO; connection = nm_device_get_connection (device); g_assert (connection); s_ip4 = nm_connection_get_setting_ip4_config (connection); if (s_ip4) - method = nm_setting_ip4_config_get_method (s_ip4); + method = nm_setting_ip_config_get_method (s_ip4); /* Indicate that a critical protocol is about to start */ if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0) @@ -2831,14 +2831,14 @@ act_stage3_ip6_config_start (NMDevice *device, NMDeviceStateReason *reason) { NMConnection *connection; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; const char *method = NM_SETTING_IP6_CONFIG_METHOD_AUTO; connection = nm_device_get_connection (device); g_assert (connection); s_ip6 = nm_connection_get_setting_ip6_config (connection); if (s_ip6) - method = nm_setting_ip6_config_get_method (s_ip6); + method = nm_setting_ip_config_get_method (s_ip6); /* Indicate that a critical protocol is about to start */ if (strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0 || @@ -2938,7 +2938,7 @@ static NMActStageReturn act_stage4_ip4_config_timeout (NMDevice *device, NMDeviceStateReason *reason) { NMConnection *connection; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; gboolean may_fail = FALSE, chain_up = FALSE; NMActStageReturn ret; @@ -2946,7 +2946,7 @@ act_stage4_ip4_config_timeout (NMDevice *device, NMDeviceStateReason *reason) g_assert (connection); s_ip4 = nm_connection_get_setting_ip4_config (connection); - may_fail = nm_setting_ip4_config_get_may_fail (s_ip4); + may_fail = nm_setting_ip_config_get_may_fail (s_ip4); ret = handle_ip_config_timeout (NM_DEVICE_WIFI (device), connection, may_fail, &chain_up, reason); if (chain_up) @@ -2959,7 +2959,7 @@ static NMActStageReturn act_stage4_ip6_config_timeout (NMDevice *device, NMDeviceStateReason *reason) { NMConnection *connection; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; gboolean may_fail = FALSE, chain_up = FALSE; NMActStageReturn ret; @@ -2967,7 +2967,7 @@ act_stage4_ip6_config_timeout (NMDevice *device, NMDeviceStateReason *reason) g_assert (connection); s_ip6 = nm_connection_get_setting_ip6_config (connection); - may_fail = nm_setting_ip6_config_get_may_fail (s_ip6); + may_fail = nm_setting_ip_config_get_may_fail (s_ip6); ret = handle_ip_config_timeout (NM_DEVICE_WIFI (device), connection, may_fail, &chain_up, reason); if (chain_up) diff --git a/src/devices/wwan/nm-modem-broadband.c b/src/devices/wwan/nm-modem-broadband.c index e896cbc10a..cf61a077f4 100644 --- a/src/devices/wwan/nm-modem-broadband.c +++ b/src/devices/wwan/nm-modem-broadband.c @@ -20,6 +20,7 @@ #include #include +#include #include #include "nm-modem-broadband.h" diff --git a/src/devices/wwan/nm-modem.c b/src/devices/wwan/nm-modem.c index 431dc05c0b..ba01d5ca46 100644 --- a/src/devices/wwan/nm-modem.c +++ b/src/devices/wwan/nm-modem.c @@ -235,26 +235,25 @@ nm_modem_get_connection_ip_type (NMModem *self, GError **error) { NMModemPrivate *priv = NM_MODEM_GET_PRIVATE (self); - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4, *s_ip6; const char *method; gboolean ip4 = TRUE, ip6 = TRUE; gboolean ip4_may_fail = TRUE, ip6_may_fail = TRUE; s_ip4 = nm_connection_get_setting_ip4_config (connection); if (s_ip4) { - method = nm_setting_ip4_config_get_method (s_ip4); + method = nm_setting_ip_config_get_method (s_ip4); if (g_strcmp0 (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0) ip4 = FALSE; - ip4_may_fail = nm_setting_ip4_config_get_may_fail (s_ip4); + ip4_may_fail = nm_setting_ip_config_get_may_fail (s_ip4); } s_ip6 = nm_connection_get_setting_ip6_config (connection); if (s_ip6) { - method = nm_setting_ip6_config_get_method (s_ip6); + method = nm_setting_ip_config_get_method (s_ip6); if (g_strcmp0 (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) == 0) ip6 = FALSE; - ip6_may_fail = nm_setting_ip6_config_get_may_fail (s_ip6); + ip6_may_fail = nm_setting_ip_config_get_may_fail (s_ip6); } if (ip4 && !ip6) { diff --git a/src/dhcp-manager/nm-dhcp-dhclient-utils.c b/src/dhcp-manager/nm-dhcp-dhclient-utils.c index 2469076719..a53e95b009 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient-utils.c +++ b/src/dhcp-manager/nm-dhcp-dhclient-utils.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "nm-dhcp-dhclient-utils.h" #include "nm-ip4-config.h" diff --git a/src/dhcp-manager/nm-dhcp-utils.c b/src/dhcp-manager/nm-dhcp-utils.c index 02fc4fe513..424d93db11 100644 --- a/src/dhcp-manager/nm-dhcp-utils.c +++ b/src/dhcp-manager/nm-dhcp-utils.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "nm-logging.h" #include "nm-dhcp-utils.h" diff --git a/src/dhcp-manager/tests/test-dhcp-dhclient.c b/src/dhcp-manager/tests/test-dhcp-dhclient.c index ab496b38bd..2ddf6b5078 100644 --- a/src/dhcp-manager/tests/test-dhcp-dhclient.c +++ b/src/dhcp-manager/tests/test-dhcp-dhclient.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "nm-dhcp-dhclient-utils.h" #include "nm-utils.h" diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index cebfe3bbe9..c51b449920 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -20,6 +20,7 @@ */ #include +#include #include "nm-ip4-config.h" @@ -296,7 +297,7 @@ nm_ip4_config_commit (const NMIP4Config *config, int ifindex) } void -nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, int default_route_metric) +nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIPConfig *setting, int default_route_metric) { guint naddresses, nroutes, nnameservers, nsearches; int i; @@ -304,20 +305,22 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i if (!setting) return; + g_return_if_fail (NM_IS_SETTING_IP4_CONFIG (setting)); + g_object_freeze_notify (G_OBJECT (config)); - naddresses = nm_setting_ip4_config_get_num_addresses (setting); - nroutes = nm_setting_ip4_config_get_num_routes (setting); - nnameservers = nm_setting_ip4_config_get_num_dns (setting); - nsearches = nm_setting_ip4_config_get_num_dns_searches (setting); + naddresses = nm_setting_ip_config_get_num_addresses (setting); + nroutes = nm_setting_ip_config_get_num_routes (setting); + nnameservers = nm_setting_ip_config_get_num_dns (setting); + nsearches = nm_setting_ip_config_get_num_dns_searches (setting); /* Gateway */ - if (nm_setting_ip4_config_get_never_default (setting)) + if (nm_setting_ip_config_get_never_default (setting)) nm_ip4_config_set_never_default (config, TRUE); - else if (nm_setting_ip4_config_get_ignore_auto_routes (setting)) + else if (nm_setting_ip_config_get_ignore_auto_routes (setting)) nm_ip4_config_set_never_default (config, FALSE); for (i = 0; i < naddresses; i++) { - const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip4_config_get_address (setting, i)); + const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip_config_get_address (setting, i)); guint32 gateway; if (gateway_str) { @@ -329,7 +332,7 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i /* Addresses */ for (i = 0; i < naddresses; i++) { - NMIPAddress *s_addr = nm_setting_ip4_config_get_address (setting, i); + NMIPAddress *s_addr = nm_setting_ip_config_get_address (setting, i); GVariant *label; NMPlatformIP4Address address; @@ -348,10 +351,10 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i } /* Routes */ - if (nm_setting_ip4_config_get_ignore_auto_routes (setting)) + if (nm_setting_ip_config_get_ignore_auto_routes (setting)) nm_ip4_config_reset_routes (config); for (i = 0; i < nroutes; i++) { - NMIPRoute *s_route = nm_setting_ip4_config_get_route (setting, i); + NMIPRoute *s_route = nm_setting_ip_config_get_route (setting, i); NMPlatformIP4Route route; memset (&route, 0, sizeof (route)); @@ -367,7 +370,7 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i } /* DNS */ - if (nm_setting_ip4_config_get_ignore_auto_dns (setting)) { + if (nm_setting_ip_config_get_ignore_auto_dns (setting)) { nm_ip4_config_reset_nameservers (config); nm_ip4_config_reset_domains (config); nm_ip4_config_reset_searches (config); @@ -375,11 +378,11 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i for (i = 0; i < nnameservers; i++) { guint32 ip; - if (inet_pton (AF_INET, nm_setting_ip4_config_get_dns (setting, i), &ip) == 1) + if (inet_pton (AF_INET, nm_setting_ip_config_get_dns (setting, i), &ip) == 1) nm_ip4_config_add_nameserver (config, ip); } for (i = 0; i < nsearches; i++) - nm_ip4_config_add_search (config, nm_setting_ip4_config_get_dns_search (setting, i)); + nm_ip4_config_add_search (config, nm_setting_ip_config_get_dns_search (setting, i)); g_object_thaw_notify (G_OBJECT (config)); } @@ -387,17 +390,17 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, i NMSetting * nm_ip4_config_create_setting (const NMIP4Config *config) { - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; guint32 gateway; guint naddresses, nroutes, nnameservers, nsearches; const char *method = NULL; int i; - s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); + s_ip4 = NM_SETTING_IP_CONFIG (nm_setting_ip4_config_new ()); if (!config) { g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED, NULL); return NM_SETTING (s_ip4); } @@ -433,14 +436,14 @@ nm_ip4_config_create_setting (const NMIP4Config *config) if (*address->label) nm_ip_address_set_attribute (s_addr, "label", g_variant_new_string (address->label)); - nm_setting_ip4_config_add_address (s_ip4, s_addr); + nm_setting_ip_config_add_address (s_ip4, s_addr); nm_ip_address_unref (s_addr); } /* Use 'disabled' if the method wasn't previously set */ if (!method) method = NM_SETTING_IP4_CONFIG_METHOD_DISABLED; - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, method, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, method, NULL); /* Routes */ for (i = 0; i < nroutes; i++) { @@ -459,7 +462,7 @@ nm_ip4_config_create_setting (const NMIP4Config *config) &route->network, route->plen, &route->gateway, route->metric, NULL); - nm_setting_ip4_config_add_route (s_ip4, s_route); + nm_setting_ip_config_add_route (s_ip4, s_route); nm_ip_route_unref (s_route); } @@ -467,12 +470,12 @@ nm_ip4_config_create_setting (const NMIP4Config *config) for (i = 0; i < nnameservers; i++) { guint32 nameserver = nm_ip4_config_get_nameserver (config, i); - nm_setting_ip4_config_add_dns (s_ip4, nm_utils_inet4_ntop (nameserver, NULL)); + nm_setting_ip_config_add_dns (s_ip4, nm_utils_inet4_ntop (nameserver, NULL)); } for (i = 0; i < nsearches; i++) { const char *search = nm_ip4_config_get_search (config, i); - nm_setting_ip4_config_add_dns_search (s_ip4, search); + nm_setting_ip_config_add_dns_search (s_ip4, search); } return NM_SETTING (s_ip4); diff --git a/src/nm-ip4-config.h b/src/nm-ip4-config.h index 16372be5cd..e9f2642af7 100644 --- a/src/nm-ip4-config.h +++ b/src/nm-ip4-config.h @@ -61,7 +61,7 @@ const char * nm_ip4_config_get_dbus_path (const NMIP4Config *config); /* Integration with nm-platform and nm-setting */ NMIP4Config *nm_ip4_config_capture (int ifindex, gboolean capture_resolv_conf); gboolean nm_ip4_config_commit (const NMIP4Config *config, int ifindex); -void nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIP4Config *setting, int default_route_metric); +void nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIPConfig *setting, int default_route_metric); NMSetting *nm_ip4_config_create_setting (const NMIP4Config *config); /* Utility functions */ diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index ed7d41b46b..e022e7178c 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -20,6 +20,7 @@ */ #include +#include #include "nm-ip6-config.h" @@ -400,7 +401,7 @@ nm_ip6_config_commit (const NMIP6Config *config, int ifindex) } void -nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, int default_route_metric) +nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIPConfig *setting, int default_route_metric) { guint naddresses, nroutes, nnameservers, nsearches; int i; @@ -408,20 +409,22 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, i if (!setting) return; - naddresses = nm_setting_ip6_config_get_num_addresses (setting); - nroutes = nm_setting_ip6_config_get_num_routes (setting); - nnameservers = nm_setting_ip6_config_get_num_dns (setting); - nsearches = nm_setting_ip6_config_get_num_dns_searches (setting); + g_return_if_fail (NM_IS_SETTING_IP6_CONFIG (setting)); + + naddresses = nm_setting_ip_config_get_num_addresses (setting); + nroutes = nm_setting_ip_config_get_num_routes (setting); + nnameservers = nm_setting_ip_config_get_num_dns (setting); + nsearches = nm_setting_ip_config_get_num_dns_searches (setting); g_object_freeze_notify (G_OBJECT (config)); /* Gateway */ - if (nm_setting_ip6_config_get_never_default (setting)) + if (nm_setting_ip_config_get_never_default (setting)) nm_ip6_config_set_never_default (config, TRUE); - else if (nm_setting_ip6_config_get_ignore_auto_routes (setting)) + else if (nm_setting_ip_config_get_ignore_auto_routes (setting)) nm_ip6_config_set_never_default (config, FALSE); for (i = 0; i < naddresses; i++) { - const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip6_config_get_address (setting, i)); + const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip_config_get_address (setting, i)); struct in6_addr gateway; if (gateway_str) { @@ -433,7 +436,7 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, i /* Addresses */ for (i = 0; i < naddresses; i++) { - NMIPAddress *s_addr = nm_setting_ip6_config_get_address (setting, i); + NMIPAddress *s_addr = nm_setting_ip_config_get_address (setting, i); NMPlatformIP6Address address; memset (&address, 0, sizeof (address)); @@ -447,10 +450,10 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, i } /* Routes */ - if (nm_setting_ip6_config_get_ignore_auto_routes (setting)) + if (nm_setting_ip_config_get_ignore_auto_routes (setting)) nm_ip6_config_reset_routes (config); for (i = 0; i < nroutes; i++) { - NMIPRoute *s_route = nm_setting_ip6_config_get_route (setting, i); + NMIPRoute *s_route = nm_setting_ip_config_get_route (setting, i); NMPlatformIP6Route route; memset (&route, 0, sizeof (route)); @@ -466,7 +469,7 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, i } /* DNS */ - if (nm_setting_ip6_config_get_ignore_auto_dns (setting)) { + if (nm_setting_ip_config_get_ignore_auto_dns (setting)) { nm_ip6_config_reset_nameservers (config); nm_ip6_config_reset_domains (config); nm_ip6_config_reset_searches (config); @@ -474,11 +477,11 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, i for (i = 0; i < nnameservers; i++) { struct in6_addr ip; - if (inet_pton (AF_INET6, nm_setting_ip6_config_get_dns (setting, i), &ip) == 1) + if (inet_pton (AF_INET6, nm_setting_ip_config_get_dns (setting, i), &ip) == 1) nm_ip6_config_add_nameserver (config, &ip); } for (i = 0; i < nsearches; i++) - nm_ip6_config_add_search (config, nm_setting_ip6_config_get_dns_search (setting, i)); + nm_ip6_config_add_search (config, nm_setting_ip_config_get_dns_search (setting, i)); g_object_thaw_notify (G_OBJECT (config)); } @@ -486,17 +489,17 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, i NMSetting * nm_ip6_config_create_setting (const NMIP6Config *config) { - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; const struct in6_addr *gateway; guint naddresses, nroutes, nnameservers, nsearches; const char *method = NULL; int i; - s_ip6 = NM_SETTING_IP6_CONFIG (nm_setting_ip6_config_new ()); + s_ip6 = NM_SETTING_IP_CONFIG (nm_setting_ip6_config_new ()); if (!config) { g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL); return NM_SETTING (s_ip6); } @@ -530,14 +533,14 @@ nm_ip6_config_create_setting (const NMIP6Config *config) method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL; s_addr = nm_ip_address_new_binary (AF_INET6, &address->address, address->plen, gateway, NULL); - nm_setting_ip6_config_add_address (s_ip6, s_addr); + nm_setting_ip_config_add_address (s_ip6, s_addr); nm_ip_address_unref (s_addr); } /* Use 'ignore' if the method wasn't previously set */ if (!method) method = NM_SETTING_IP6_CONFIG_METHOD_IGNORE; - g_object_set (s_ip6, NM_SETTING_IP6_CONFIG_METHOD, method, NULL); + g_object_set (s_ip6, NM_SETTING_IP_CONFIG_METHOD, method, NULL); /* Routes */ for (i = 0; i < nroutes; i++) { @@ -560,7 +563,7 @@ nm_ip6_config_create_setting (const NMIP6Config *config) &route->network, route->plen, &route->gateway, route->metric, NULL); - nm_setting_ip6_config_add_route (s_ip6, s_route); + nm_setting_ip_config_add_route (s_ip6, s_route); nm_ip_route_unref (s_route); } @@ -568,12 +571,12 @@ nm_ip6_config_create_setting (const NMIP6Config *config) for (i = 0; i < nnameservers; i++) { const struct in6_addr *nameserver = nm_ip6_config_get_nameserver (config, i); - nm_setting_ip6_config_add_dns (s_ip6, nm_utils_inet6_ntop (nameserver, NULL)); + nm_setting_ip_config_add_dns (s_ip6, nm_utils_inet6_ntop (nameserver, NULL)); } for (i = 0; i < nsearches; i++) { const char *search = nm_ip6_config_get_search (config, i); - nm_setting_ip6_config_add_dns_search (s_ip6, search); + nm_setting_ip_config_add_dns_search (s_ip6, search); } return NM_SETTING (s_ip6); diff --git a/src/nm-ip6-config.h b/src/nm-ip6-config.h index 60a82b0f72..19eef01372 100644 --- a/src/nm-ip6-config.h +++ b/src/nm-ip6-config.h @@ -22,6 +22,7 @@ #define __NETWORKMANAGER_IP6_CONFIG_H__ #include +#include #include "nm-types.h" #include "nm-setting-ip6-config.h" @@ -60,7 +61,7 @@ const char * nm_ip6_config_get_dbus_path (const NMIP6Config *config); /* Integration with nm-platform and nm-setting */ NMIP6Config *nm_ip6_config_capture (int ifindex, gboolean capture_resolv_conf, NMSettingIP6ConfigPrivacy use_temporary); gboolean nm_ip6_config_commit (const NMIP6Config *config, int ifindex); -void nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIP6Config *setting, int default_route_metric); +void nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIPConfig *setting, int default_route_metric); NMSetting *nm_ip6_config_create_setting (const NMIP6Config *config); /* Utility functions */ diff --git a/src/nm-policy.c b/src/nm-policy.c index e21df28a9c..e5a295bfb7 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -110,7 +110,7 @@ get_best_ip4_device (NMPolicy *self, gboolean fully_activated) NMDeviceState state = nm_device_get_state (dev); NMActRequest *req; NMConnection *connection; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; int prio; const char *method = NULL; @@ -151,7 +151,7 @@ get_best_ip4_device (NMPolicy *self, gboolean fully_activated) /* 'never-default' devices can't ever be the default */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - if (nm_setting_ip4_config_get_never_default (s_ip4)) + if (nm_setting_ip_config_get_never_default (s_ip4)) continue; prio = nm_device_get_priority (dev); @@ -194,7 +194,7 @@ get_best_ip6_device (NMPolicy *self, gboolean fully_activated) NMDeviceState state = nm_device_get_state (dev); NMActRequest *req; NMConnection *connection; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; int prio; const char *method = NULL; @@ -231,7 +231,7 @@ get_best_ip6_device (NMPolicy *self, gboolean fully_activated) s_ip6 = nm_connection_get_setting_ip6_config (connection); g_assert (s_ip6); - if (nm_setting_ip6_config_get_never_default (s_ip6)) + if (nm_setting_ip_config_get_never_default (s_ip6)) continue; prio = nm_device_get_priority (dev); @@ -539,7 +539,7 @@ get_best_ip4_config (NMPolicy *policy, NMVpnConnection *candidate; NMIP4Config *vpn_ip4; NMConnection *tmp; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMVpnConnectionState vpn_state; if (!NM_IS_VPN_CONNECTION (active)) @@ -565,7 +565,7 @@ get_best_ip4_config (NMPolicy *policy, /* Check the user's preference from the NMConnection */ s_ip4 = nm_connection_get_setting_ip4_config (tmp); - if (nm_setting_ip4_config_get_never_default (s_ip4)) + if (nm_setting_ip_config_get_never_default (s_ip4)) continue; } @@ -751,7 +751,7 @@ get_best_ip6_config (NMPolicy *policy, NMVpnConnection *candidate; NMIP6Config *vpn_ip6; NMConnection *tmp; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; NMVpnConnectionState vpn_state; if (!NM_IS_VPN_CONNECTION (active)) @@ -777,7 +777,7 @@ get_best_ip6_config (NMPolicy *policy, /* Check the user's preference from the NMConnection */ s_ip6 = nm_connection_get_setting_ip6_config (tmp); - if (nm_setting_ip6_config_get_never_default (s_ip6)) + if (nm_setting_ip_config_get_never_default (s_ip6)) continue; } diff --git a/src/settings/plugins/ibft/reader.c b/src/settings/plugins/ibft/reader.c index be9e249678..487e3f8b05 100644 --- a/src/settings/plugins/ibft/reader.c +++ b/src/settings/plugins/ibft/reader.c @@ -267,7 +267,7 @@ ip4_setting_add_from_block (const GPtrArray *block, NMConnection *connection, GError **error) { - NMSettingIP4Config *s_ip4 = NULL; + NMSettingIPConfig *s_ip4 = NULL; NMIPAddress *addr; const char *s_method = NULL; const char *s_ipaddr = NULL; @@ -296,10 +296,10 @@ ip4_setting_add_from_block (const GPtrArray *block, goto error; } - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); if (!g_ascii_strcasecmp (s_method, "dhcp")) { - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); goto success; } else if (g_ascii_strcasecmp (s_method, "static") != 0) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, @@ -309,7 +309,7 @@ ip4_setting_add_from_block (const GPtrArray *block, } /* Static configuration stuff */ - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); /* IP address */ if (!s_ipaddr || !nm_utils_ipaddr_valid (AF_INET, s_ipaddr)) { @@ -355,13 +355,13 @@ ip4_setting_add_from_block (const GPtrArray *block, goto error; } - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); if (s_dns1) - nm_setting_ip4_config_add_dns (s_ip4, s_dns1); + nm_setting_ip_config_add_dns (s_ip4, s_dns1); if (s_dns2) - nm_setting_ip4_config_add_dns (s_ip4, s_dns2); + nm_setting_ip_config_add_dns (s_ip4, s_dns2); success: nm_connection_add_setting (connection, NM_SETTING (s_ip4)); diff --git a/src/settings/plugins/ibft/tests/test-ibft.c b/src/settings/plugins/ibft/tests/test-ibft.c index 32e1eb6a8b..28a05bafe1 100644 --- a/src/settings/plugins/ibft/tests/test-ibft.c +++ b/src/settings/plugins/ibft/tests/test-ibft.c @@ -71,7 +71,7 @@ test_read_ibft_dhcp (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; GError *error = NULL; const char *mac_address; const char *expected_mac_address = "00:33:21:98:b9:f1"; @@ -105,7 +105,7 @@ test_read_ibft_dhcp (void) /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); g_object_unref (connection); } @@ -116,7 +116,7 @@ test_read_ibft_static (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; GError *error = NULL; const char *mac_address; const char *expected_mac_address = "00:33:21:98:b9:f0"; @@ -151,14 +151,14 @@ test_read_ibft_static (void) /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); - g_assert_cmpint (nm_setting_ip4_config_get_num_dns (s_ip4), ==, 2); - g_assert_cmpstr (nm_setting_ip4_config_get_dns (s_ip4, 0), ==, "10.16.255.2"); - g_assert_cmpstr (nm_setting_ip4_config_get_dns (s_ip4, 1), ==, "10.16.255.3"); + g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 2); + g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 0), ==, "10.16.255.2"); + g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 1), ==, "10.16.255.3"); - g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); + g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 1); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert (ip4_addr); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.32.72"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 22); @@ -217,7 +217,7 @@ test_read_ibft_vlan (void) NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingVlan *s_vlan; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; const char *mac_address; const char *expected_mac_address = "00:33:21:98:b9:f0"; NMIPAddress *ip4_addr; @@ -250,12 +250,12 @@ test_read_ibft_vlan (void) /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); - g_assert_cmpint (nm_setting_ip4_config_get_num_dns (s_ip4), ==, 0); + g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 0); - g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); + g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 1); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert (ip4_addr); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.6.200"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index 62f52d41c0..b16302c0ba 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -540,7 +540,7 @@ out: } static gboolean -read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError **error) +read_route_file_legacy (const char *filename, NMSettingIPConfig *s_ip4, GError **error) { char *contents = NULL; gsize len = 0; @@ -670,7 +670,7 @@ read_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError g_free (next_hop); goto error; } - if (!nm_setting_ip4_config_add_route (s_ip4, route)) + if (!nm_setting_ip_config_add_route (s_ip4, route)) PARSE_WARNING ("duplicate IP4 route"); } @@ -771,7 +771,7 @@ error: #define IPV6_ADDR_REGEX "[0-9A-Fa-f:.]+" static gboolean -read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **error) +read_route6_file (const char *filename, NMSettingIPConfig *s_ip6, GError **error) { char *contents = NULL; gsize len = 0; @@ -898,7 +898,7 @@ read_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **erro g_free (next_hop); if (!route) goto error; - if (!nm_setting_ip6_config_add_route (s_ip6, route)) + if (!nm_setting_ip_config_add_route (s_ip6, route)) PARSE_WARNING ("duplicate IP6 route"); } @@ -923,7 +923,7 @@ make_ip4_setting (shvarFile *ifcfg, const char *network_file, GError **error) { - NMSettingIP4Config *s_ip4 = NULL; + NMSettingIPConfig *s_ip4 = NULL; char *value = NULL; char *route_path = NULL; char *method; @@ -932,7 +932,7 @@ make_ip4_setting (shvarFile *ifcfg, shvarFile *route_ifcfg; gboolean never_default = FALSE; - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); /* First check if DEFROUTE is set for this device; DEFROUTE has the * opposite meaning from never-default. The default if DEFROUTE is not @@ -975,15 +975,15 @@ make_ip4_setting (shvarFile *ifcfg, } else if (!g_ascii_strcasecmp (value, "autoip")) { g_free (value); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL, - NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, never_default, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL, + NM_SETTING_IP_CONFIG_NEVER_DEFAULT, never_default, NULL); return NM_SETTING (s_ip4); } else if (!g_ascii_strcasecmp (value, "shared")) { g_free (value); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_SHARED, - NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, never_default, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_SHARED, + NM_SETTING_IP_CONFIG_NEVER_DEFAULT, never_default, NULL); return NM_SETTING (s_ip4); } else { @@ -995,11 +995,11 @@ make_ip4_setting (shvarFile *ifcfg, g_free (value); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, method, - NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, !svTrueValue (ifcfg, "PEERDNS", TRUE), - NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, !svTrueValue (ifcfg, "PEERROUTES", TRUE), - NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, never_default, - NM_SETTING_IP4_CONFIG_MAY_FAIL, !svTrueValue (ifcfg, "IPV4_FAILURE_FATAL", FALSE), + NM_SETTING_IP_CONFIG_METHOD, method, + NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, !svTrueValue (ifcfg, "PEERDNS", TRUE), + NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, !svTrueValue (ifcfg, "PEERROUTES", TRUE), + NM_SETTING_IP_CONFIG_NEVER_DEFAULT, never_default, + NM_SETTING_IP_CONFIG_MAY_FAIL, !svTrueValue (ifcfg, "IPV4_FAILURE_FATAL", FALSE), NULL); if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0) @@ -1009,11 +1009,11 @@ make_ip4_setting (shvarFile *ifcfg, if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) { value = svGetValue (ifcfg, "DHCP_HOSTNAME", FALSE); if (value && strlen (value)) - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, value, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, value, NULL); g_free (value); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME, + NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, svTrueValue (ifcfg, "DHCP_SEND_HOSTNAME", TRUE), NULL); @@ -1042,7 +1042,7 @@ make_ip4_setting (shvarFile *ifcfg, continue; } - if (!nm_setting_ip4_config_add_address (s_ip4, addr)) + if (!nm_setting_ip_config_add_address (s_ip4, addr)) PARSE_WARNING ("duplicate IP4 address"); nm_ip_address_unref (addr); } @@ -1057,7 +1057,7 @@ make_ip4_setting (shvarFile *ifcfg, value = svGetValue (ifcfg, tag, FALSE); if (value) { if (nm_utils_ipaddr_valid (AF_INET, value)) { - if (!nm_setting_ip4_config_add_dns (s_ip4, value)) + if (!nm_setting_ip_config_add_dns (s_ip4, value)) PARSE_WARNING ("duplicate DNS server %s", tag); } else if (nm_utils_ipaddr_valid (AF_INET6, value)) { /* Ignore IPv6 addresses */ @@ -1084,7 +1084,7 @@ make_ip4_setting (shvarFile *ifcfg, char **item; for (item = searches; *item; item++) { if (strlen (*item)) { - if (!nm_setting_ip4_config_add_dns_search (s_ip4, *item)) + if (!nm_setting_ip_config_add_dns_search (s_ip4, *item)) PARSE_WARNING ("duplicate DNS domain '%s'", *item); } } @@ -1117,7 +1117,7 @@ make_ip4_setting (shvarFile *ifcfg, if (!route) break; - if (!nm_setting_ip4_config_add_route (s_ip4, route)) + if (!nm_setting_ip_config_add_route (s_ip4, route)) PARSE_WARNING ("duplicate IP4 route"); nm_ip_route_unref (route); } @@ -1129,7 +1129,7 @@ make_ip4_setting (shvarFile *ifcfg, } /* Legacy value NM used for a while but is incorrect (rh #459370) */ - if (!nm_setting_ip4_config_get_num_dns_searches (s_ip4)) { + if (!nm_setting_ip_config_get_num_dns_searches (s_ip4)) { value = svGetValue (ifcfg, "SEARCH", FALSE); if (value) { char **searches = NULL; @@ -1139,7 +1139,7 @@ make_ip4_setting (shvarFile *ifcfg, char **item; for (item = searches; *item; item++) { if (strlen (*item)) { - if (!nm_setting_ip4_config_add_dns_search (s_ip4, *item)) + if (!nm_setting_ip_config_add_dns_search (s_ip4, *item)) PARSE_WARNING ("duplicate DNS search '%s'", *item); } } @@ -1158,7 +1158,7 @@ done: } static void -read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *network_file) +read_aliases (NMSettingIPConfig *s_ip4, const char *filename, const char *network_file) { GDir *dir; char *dirname, *base; @@ -1169,10 +1169,10 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo g_return_if_fail (s_ip4 != NULL); g_return_if_fail (filename != NULL); - if (nm_setting_ip4_config_get_num_addresses (s_ip4) == 0) + if (nm_setting_ip_config_get_num_addresses (s_ip4) == 0) return; - base_addr = nm_setting_ip4_config_get_address (s_ip4, 0); + base_addr = nm_setting_ip_config_get_address (s_ip4, 0); dirname = g_path_get_dirname (filename); g_return_if_fail (dirname != NULL); @@ -1236,7 +1236,7 @@ read_aliases (NMSettingIP4Config *s_ip4, const char *filename, const char *netwo svCloseFile (parsed); if (ok) { nm_ip_address_set_attribute (addr, "label", g_variant_new_string (device)); - if (!nm_setting_ip4_config_add_address (s_ip4, addr)) + if (!nm_setting_ip_config_add_address (s_ip4, addr)) PARSE_WARNING ("duplicate IP4 address in alias file %s", item); } else { PARSE_WARNING ("error reading IP4 address from alias file '%s': %s", @@ -1264,7 +1264,7 @@ make_ip6_setting (shvarFile *ifcfg, const char *network_file, GError **error) { - NMSettingIP6Config *s_ip6 = NULL; + NMSettingIPConfig *s_ip6 = NULL; char *value = NULL; char *str_value; char *route6_path = NULL; @@ -1279,7 +1279,7 @@ make_ip6_setting (shvarFile *ifcfg, char *ip6_privacy_str; NMSettingIP6ConfigPrivacy ip6_privacy_val; - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); /* First check if IPV6_DEFROUTE is set for this device; IPV6_DEFROUTE has the * opposite meaning from never-default. The default if IPV6_DEFROUTE is not @@ -1376,11 +1376,11 @@ make_ip6_setting (shvarFile *ifcfg, g_free (ip6_privacy_str); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, method, - NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS, !svTrueValue (ifcfg, "IPV6_PEERDNS", TRUE), - NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES, !svTrueValue (ifcfg, "IPV6_PEERROUTES", TRUE), - NM_SETTING_IP6_CONFIG_NEVER_DEFAULT, never_default, - NM_SETTING_IP6_CONFIG_MAY_FAIL, !svTrueValue (ifcfg, "IPV6_FAILURE_FATAL", FALSE), + NM_SETTING_IP_CONFIG_METHOD, method, + NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, !svTrueValue (ifcfg, "IPV6_PEERDNS", TRUE), + NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, !svTrueValue (ifcfg, "IPV6_PEERROUTES", TRUE), + NM_SETTING_IP_CONFIG_NEVER_DEFAULT, never_default, + NM_SETTING_IP_CONFIG_MAY_FAIL, !svTrueValue (ifcfg, "IPV6_FAILURE_FATAL", FALSE), NM_SETTING_IP6_CONFIG_IP6_PRIVACY, ip6_privacy_val, NULL); @@ -1393,7 +1393,7 @@ make_ip6_setting (shvarFile *ifcfg, /* METHOD_AUTO may trigger DHCPv6, so save the hostname to send to DHCP */ value = svGetValue (ifcfg, "DHCP_HOSTNAME", FALSE); if (value && value[0]) - g_object_set (s_ip6, NM_SETTING_IP6_CONFIG_DHCP_HOSTNAME, value, NULL); + g_object_set (s_ip6, NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, value, NULL); g_free (value); } @@ -1422,7 +1422,7 @@ make_ip6_setting (shvarFile *ifcfg, goto error; } - if (!nm_setting_ip6_config_add_address (s_ip6, addr)) + if (!nm_setting_ip_config_add_address (s_ip6, addr)) PARSE_WARNING ("duplicate IP6 address"); nm_ip_address_unref (addr); } @@ -1442,7 +1442,7 @@ make_ip6_setting (shvarFile *ifcfg, } if (nm_utils_ipaddr_valid (AF_INET6, value)) { - if (!nm_setting_ip6_config_add_dns (s_ip6, value)) + if (!nm_setting_ip_config_add_dns (s_ip6, value)) PARSE_WARNING ("duplicate DNS server %s", tag); } else if (nm_utils_ipaddr_valid (AF_INET, value)) { /* Ignore IPv4 addresses */ @@ -1457,7 +1457,7 @@ make_ip6_setting (shvarFile *ifcfg, g_free (value); } - /* DNS searches ('DOMAIN' key) are read by make_ip4_setting() and included in NMSettingIP4Config */ + /* DNS searches ('DOMAIN' key) are read by make_ip4_setting() and included in NMSettingIPConfig */ /* Read static routes from route6- file */ route6_path = utils_get_route6_path (ifcfg->fileName); @@ -4604,7 +4604,7 @@ check_dns_search_domains (shvarFile *ifcfg, NMSetting *s_ip4, NMSetting *s_ip6) /* If there is no IPv4 config or it doesn't contain DNS searches, * read DOMAIN and put the domains into IPv6. */ - if (!s_ip4 || nm_setting_ip4_config_get_num_dns_searches (NM_SETTING_IP4_CONFIG (s_ip4)) == 0) { + if (!s_ip4 || nm_setting_ip_config_get_num_dns_searches (NM_SETTING_IP_CONFIG (s_ip4)) == 0) { /* DNS searches */ char *value = svGetValue (ifcfg, "DOMAIN", FALSE); if (value) { @@ -4613,7 +4613,7 @@ check_dns_search_domains (shvarFile *ifcfg, NMSetting *s_ip4, NMSetting *s_ip6) char **item; for (item = searches; *item; item++) { if (strlen (*item)) { - if (!nm_setting_ip6_config_add_dns_search (NM_SETTING_IP6_CONFIG (s_ip6), *item)) + if (!nm_setting_ip_config_add_dns_search (NM_SETTING_IP_CONFIG (s_ip6), *item)) PARSE_WARNING ("duplicate DNS domain '%s'", *item); } } @@ -4794,7 +4794,7 @@ connection_from_file (const char *filename, connection = NULL; goto done; } else { - read_aliases (NM_SETTING_IP4_CONFIG (s_ip4), filename, network_file); + read_aliases (NM_SETTING_IP_CONFIG (s_ip4), filename, network_file); nm_connection_add_setting (connection, s_ip4); } diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index cafaf88be9..3c43a2f5df 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -183,8 +183,8 @@ test_read_basic (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; GError *error = NULL; const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x16, 0x41, 0x11, 0x22, 0x33 }; @@ -225,14 +225,14 @@ test_read_basic (void) /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_DISABLED); - g_assert (nm_setting_ip4_config_get_never_default (s_ip4) == FALSE); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_DISABLED); + g_assert (nm_setting_ip_config_get_never_default (s_ip4) == FALSE); /* ===== IPv6 SETTING ===== */ s_ip6 = nm_connection_get_setting_ip6_config (connection); g_assert (s_ip6); - g_assert_cmpstr (nm_setting_ip6_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE); - g_assert (nm_setting_ip6_config_get_never_default (s_ip6) == FALSE); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE); + g_assert (nm_setting_ip_config_get_never_default (s_ip6) == FALSE); g_object_unref (connection); } @@ -243,7 +243,7 @@ test_read_miscellaneous_variables (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; GError *error = NULL; char *expected_mac_blacklist[3] = { "00:16:41:11:22:88", "00:16:41:11:22:99", "6a:5d:5a:fa:dd:f0" }; int mac_blacklist_num, i; @@ -281,8 +281,8 @@ test_read_miscellaneous_variables (void) /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_DISABLED); - g_assert (nm_setting_ip4_config_get_never_default (s_ip4) == FALSE); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_DISABLED); + g_assert (nm_setting_ip_config_get_never_default (s_ip4) == FALSE); g_object_unref (connection); } @@ -293,7 +293,7 @@ test_read_variables_corner_cases (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; GError *error = NULL; const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x16, 0x41, 0x11, 0x22, 0x33 }; @@ -331,8 +331,8 @@ test_read_variables_corner_cases (void) /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_DISABLED); - g_assert (nm_setting_ip4_config_get_never_default (s_ip4) == FALSE); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_DISABLED); + g_assert (nm_setting_ip_config_get_never_default (s_ip4) == FALSE); g_object_unref (connection); } @@ -440,8 +440,8 @@ test_read_wired_static (const char *file, NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *unmanaged = NULL; GError *error = NULL; const char *mac; @@ -479,17 +479,17 @@ test_read_wired_static (const char *file, /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); - g_assert (nm_setting_ip4_config_get_may_fail (s_ip4)); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); + g_assert (nm_setting_ip_config_get_may_fail (s_ip4)); /* DNS Addresses */ - g_assert_cmpint (nm_setting_ip4_config_get_num_dns (s_ip4), ==, 2); - g_assert_cmpstr (nm_setting_ip4_config_get_dns (s_ip4, 0), ==, "4.2.2.1"); - g_assert_cmpstr (nm_setting_ip4_config_get_dns (s_ip4, 1), ==, "4.2.2.2"); + g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 2); + g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 0), ==, "4.2.2.1"); + g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 1), ==, "4.2.2.2"); /* IP addresses */ - g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); + g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 1); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert (ip4_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); @@ -499,28 +499,28 @@ test_read_wired_static (const char *file, s_ip6 = nm_connection_get_setting_ip6_config (connection); g_assert (s_ip6); if (expect_ip6) { - g_assert_cmpstr (nm_setting_ip6_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_MANUAL); - g_assert (nm_setting_ip6_config_get_may_fail (s_ip6)); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_MANUAL); + g_assert (nm_setting_ip_config_get_may_fail (s_ip6)); /* DNS Addresses */ - g_assert_cmpint (nm_setting_ip6_config_get_num_dns (s_ip6), ==, 2); - g_assert_cmpstr (nm_setting_ip6_config_get_dns (s_ip6, 0), ==, "1:2:3:4::a"); - g_assert_cmpstr (nm_setting_ip6_config_get_dns (s_ip6, 1), ==, "1:2:3:4::b"); + g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip6), ==, 2); + g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip6, 0), ==, "1:2:3:4::a"); + g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip6, 1), ==, "1:2:3:4::b"); /* IP addresses */ - g_assert_cmpint (nm_setting_ip6_config_get_num_addresses (s_ip6), ==, 2); + g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip6), ==, 2); - ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); + ip6_addr = nm_setting_ip_config_get_address (s_ip6, 0); g_assert (ip6_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64); g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "dead:beaf::1"); - ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 1); + ip6_addr = nm_setting_ip_config_get_address (s_ip6, 1); g_assert (ip6_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56); g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "dead:beaf::2"); } else { - g_assert_cmpstr (nm_setting_ip6_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE); } g_free (unmanaged); @@ -533,7 +533,7 @@ test_read_wired_static_no_prefix (gconstpointer user_data) guint32 expected_prefix = GPOINTER_TO_UINT (user_data); NMConnection *connection; NMSettingConnection *s_con; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; GError *error = NULL; NMIPAddress *ip4_addr; char *file, *expected_id; @@ -558,10 +558,10 @@ test_read_wired_static_no_prefix (gconstpointer user_data) /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); - g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 1); - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); + g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 1); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert (ip4_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, expected_prefix); @@ -578,7 +578,7 @@ test_read_wired_dhcp (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -674,49 +674,49 @@ test_read_wired_dhcp (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wired-dhcp-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DHCP, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); - tmp = nm_setting_ip4_config_get_dhcp_hostname (s_ip4); + tmp = nm_setting_ip_config_get_dhcp_hostname (s_ip4); ASSERT (tmp != NULL, "wired-dhcp-verify-ip4", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIRED_DHCP, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME); + NM_SETTING_IP_CONFIG_DHCP_HOSTNAME); ASSERT (strcmp (tmp, expected_dhcp_hostname) == 0, "wired-dhcp-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DHCP, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME); + NM_SETTING_IP_CONFIG_DHCP_HOSTNAME); - ASSERT (nm_setting_ip4_config_get_ignore_auto_dns (s_ip4) == TRUE, + ASSERT (nm_setting_ip_config_get_ignore_auto_dns (s_ip4) == TRUE, "wired-dhcp-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DHCP, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS); + NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS); /* DNS Addresses */ - ASSERT (nm_setting_ip4_config_get_num_dns (s_ip4) == 2, + ASSERT (nm_setting_ip_config_get_num_dns (s_ip4) == 2, "wired-dhcp-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DHCP, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (strcmp (nm_setting_ip4_config_get_dns (s_ip4, 0), "4.2.2.1") == 0, + ASSERT (strcmp (nm_setting_ip_config_get_dns (s_ip4, 0), "4.2.2.1") == 0, "wired-dhcp-verify-ip4", "failed to verify %s: unexpected %s / %s key value #1", TEST_IFCFG_WIRED_DHCP, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (strcmp (nm_setting_ip4_config_get_dns (s_ip4, 1), "4.2.2.2") == 0, + ASSERT (strcmp (nm_setting_ip_config_get_dns (s_ip4, 1), "4.2.2.2") == 0, "wired-dhcp-verify-ip4", "failed to verify %s: unexpected %s / %s key value #2", TEST_IFCFG_WIRED_DHCP, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); g_free (unmanaged); g_free (keyfile); @@ -729,8 +729,8 @@ static void test_read_wired_dhcp_plus_ip (void) { NMConnection *connection; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; GError *error = NULL; NMIPAddress *ip4_addr; NMIPAddress *ip6_addr; @@ -748,23 +748,23 @@ test_read_wired_dhcp_plus_ip (void) /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); - g_assert (nm_setting_ip4_config_get_may_fail (s_ip4)); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); + g_assert (nm_setting_ip_config_get_may_fail (s_ip4)); /* DNS Addresses */ - g_assert_cmpint (nm_setting_ip4_config_get_num_dns (s_ip4), ==, 2); - g_assert_cmpstr (nm_setting_ip4_config_get_dns (s_ip4, 0), ==, "4.2.2.1"); - g_assert_cmpstr (nm_setting_ip4_config_get_dns (s_ip4, 1), ==, "4.2.2.2"); + g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 2); + g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 0), ==, "4.2.2.1"); + g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip4, 1), ==, "4.2.2.2"); /* IP addresses */ - g_assert_cmpint (nm_setting_ip4_config_get_num_addresses (s_ip4), ==, 2); - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); + g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip4), ==, 2); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert (ip4_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "1.2.3.4"); g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "1.1.1.1"); - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 1); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 1); g_assert (ip4_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 16); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "9.8.7.6"); @@ -772,27 +772,27 @@ test_read_wired_dhcp_plus_ip (void) /* ===== IPv6 SETTING ===== */ s_ip6 = nm_connection_get_setting_ip6_config (connection); g_assert (s_ip6); - g_assert_cmpstr (nm_setting_ip6_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_AUTO); - g_assert (nm_setting_ip6_config_get_may_fail (s_ip6)); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_AUTO); + g_assert (nm_setting_ip_config_get_may_fail (s_ip6)); /* DNS Addresses */ - g_assert_cmpint (nm_setting_ip6_config_get_num_dns (s_ip6), ==, 2); - g_assert_cmpstr (nm_setting_ip6_config_get_dns (s_ip6, 0), ==, "1:2:3:4::a"); - g_assert_cmpstr (nm_setting_ip6_config_get_dns (s_ip6, 1), ==, "1:2:3:4::b"); + g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip6), ==, 2); + g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip6, 0), ==, "1:2:3:4::a"); + g_assert_cmpstr (nm_setting_ip_config_get_dns (s_ip6, 1), ==, "1:2:3:4::b"); /* IP addresses */ - g_assert_cmpint (nm_setting_ip6_config_get_num_addresses (s_ip6), ==, 3); - ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); + g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip6), ==, 3); + ip6_addr = nm_setting_ip_config_get_address (s_ip6, 0); g_assert (ip6_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56); g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "1001:abba::1234"); - ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 1); + ip6_addr = nm_setting_ip_config_get_address (s_ip6, 1); g_assert (ip6_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64); g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "2001:abba::2234"); - ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 2); + ip6_addr = nm_setting_ip_config_get_address (s_ip6, 2); g_assert (ip6_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 96); g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "3001:abba::3234"); @@ -806,7 +806,7 @@ test_read_wired_global_gateway (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; GError *error = NULL; NMIPAddress *ip4_addr; char *unmanaged = NULL; @@ -829,10 +829,10 @@ test_read_wired_global_gateway (void) /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); /* Address #1 */ - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert (ip4_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); @@ -845,8 +845,8 @@ static void test_read_wired_never_default (void) { NMConnection *connection; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; GError *error = NULL; connection = connection_from_file (TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wired-never-default", @@ -860,15 +860,15 @@ test_read_wired_never_default (void) /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); - g_assert (nm_setting_ip4_config_get_never_default (s_ip4)); - g_assert_cmpint (nm_setting_ip4_config_get_num_dns (s_ip4), ==, 0); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); + g_assert (nm_setting_ip_config_get_never_default (s_ip4)); + g_assert_cmpint (nm_setting_ip_config_get_num_dns (s_ip4), ==, 0); /* ===== IPv6 SETTING ===== */ s_ip6 = nm_connection_get_setting_ip6_config (connection); g_assert (s_ip6); - g_assert_cmpstr (nm_setting_ip6_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_AUTO); - g_assert (nm_setting_ip6_config_get_never_default (s_ip6)); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_AUTO); + g_assert (nm_setting_ip_config_get_never_default (s_ip6)); g_object_unref (connection); } @@ -881,8 +881,8 @@ test_read_wired_defroute_no (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -948,18 +948,18 @@ test_read_wired_defroute_no (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wired-defroute-no-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DEFROUTE_NO, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); - ASSERT (nm_setting_ip4_config_get_never_default (s_ip4) == TRUE, + ASSERT (nm_setting_ip_config_get_never_default (s_ip4) == TRUE, "wired-defroute-no-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DEFROUTE_NO, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_NEVER_DEFAULT); + NM_SETTING_IP_CONFIG_NEVER_DEFAULT); /* ===== IPv6 SETTING ===== */ @@ -970,18 +970,18 @@ test_read_wired_defroute_no (void) NM_SETTING_IP6_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip6_config_get_method (s_ip6); + tmp = nm_setting_ip_config_get_method (s_ip6); ASSERT (strcmp (tmp, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0, "wired-defroute-no-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DEFROUTE_NO, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); - ASSERT (nm_setting_ip6_config_get_never_default (s_ip6) == TRUE, + ASSERT (nm_setting_ip_config_get_never_default (s_ip6) == TRUE, "wired-defroute-no-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DEFROUTE_NO, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_NEVER_DEFAULT); + NM_SETTING_IP_CONFIG_NEVER_DEFAULT); g_free (unmanaged); g_free (keyfile); @@ -999,8 +999,8 @@ test_read_wired_defroute_no_gatewaydev_yes (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -1074,18 +1074,18 @@ test_read_wired_defroute_no_gatewaydev_yes (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wired-defroute-no-gatewaydev-yes-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DEFROUTE_NO_GATEWAYDEV_YES, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); - ASSERT (nm_setting_ip4_config_get_never_default (s_ip4) == FALSE, + ASSERT (nm_setting_ip_config_get_never_default (s_ip4) == FALSE, "wired-defroute-no-gatewaydev-yes-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DEFROUTE_NO_GATEWAYDEV_YES, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_NEVER_DEFAULT); + NM_SETTING_IP_CONFIG_NEVER_DEFAULT); /* ===== IPv6 SETTING ===== */ @@ -1096,18 +1096,18 @@ test_read_wired_defroute_no_gatewaydev_yes (void) NM_SETTING_IP6_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip6_config_get_method (s_ip6); - ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, - "wired-defroute-no-gatewaydev-yes-verify-ip4", "failed to verify %s: unexpected %s / %s key value", + tmp = nm_setting_ip_config_get_method (s_ip6); + ASSERT (strcmp (tmp, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0, + "wired-defroute-no-gatewaydev-yes-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DEFROUTE_NO_GATEWAYDEV_YES, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); - ASSERT (nm_setting_ip6_config_get_never_default (s_ip6) == FALSE, - "wired-defroute-no-gatewaydev-yes-verify-ip4", "failed to verify %s: unexpected %s / %s key value", + ASSERT (nm_setting_ip_config_get_never_default (s_ip6) == FALSE, + "wired-defroute-no-gatewaydev-yes-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DEFROUTE_NO_GATEWAYDEV_YES, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_NEVER_DEFAULT); + NM_SETTING_IP_CONFIG_NEVER_DEFAULT); g_free (unmanaged); g_free (keyfile); @@ -1122,7 +1122,7 @@ test_read_wired_static_routes (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; GError *error = NULL; NMIPRoute *ip4_route; @@ -1142,19 +1142,19 @@ test_read_wired_static_routes (void) /* ===== IPv4 SETTING ===== */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_MANUAL); /* Routes */ - g_assert_cmpint (nm_setting_ip4_config_get_num_routes (s_ip4), ==, 2); + g_assert_cmpint (nm_setting_ip_config_get_num_routes (s_ip4), ==, 2); - ip4_route = nm_setting_ip4_config_get_route (s_ip4, 0); + ip4_route = nm_setting_ip_config_get_route (s_ip4, 0); g_assert (ip4_route); g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "11.22.33.0"); g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 24); g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "192.168.1.5"); g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 0); - ip4_route = nm_setting_ip4_config_get_route (s_ip4, 1); + ip4_route = nm_setting_ip_config_get_route (s_ip4, 1); g_assert (ip4_route); g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "44.55.66.77"); g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 32); @@ -1172,7 +1172,7 @@ test_read_wired_static_routes_legacy (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -1245,22 +1245,22 @@ test_read_wired_static_routes_legacy (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_MANUAL) == 0, "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* Routes */ - ASSERT (nm_setting_ip4_config_get_num_routes (s_ip4) == 3, + ASSERT (nm_setting_ip_config_get_num_routes (s_ip4) == 3, "wired-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_STATIC_ROUTES_LEGACY, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES); + NM_SETTING_IP_CONFIG_ROUTES); /* Route #1 */ - ip4_route = nm_setting_ip4_config_get_route (s_ip4, 0); + ip4_route = nm_setting_ip_config_get_route (s_ip4, 0); g_assert (ip4_route != NULL); g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "21.31.41.0"); g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 24); @@ -1268,7 +1268,7 @@ test_read_wired_static_routes_legacy (void) g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 1); /* Route #2 */ - ip4_route = nm_setting_ip4_config_get_route (s_ip4, 1); + ip4_route = nm_setting_ip_config_get_route (s_ip4, 1); g_assert (ip4_route != NULL); g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "32.42.52.62"); g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 32); @@ -1276,7 +1276,7 @@ test_read_wired_static_routes_legacy (void) g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 0); /* Route #3 */ - ip4_route = nm_setting_ip4_config_get_route (s_ip4, 2); + ip4_route = nm_setting_ip_config_get_route (s_ip4, 2); g_assert (ip4_route != NULL); g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "43.53.0.0"); g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 16); @@ -1296,7 +1296,7 @@ test_read_wired_ipv4_manual (const char *file, const char *expected_id) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -1362,34 +1362,34 @@ test_read_wired_ipv4_manual (const char *file, const char *expected_id) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_MANUAL) == 0, "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected %s / %s key value", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* IP addresses */ - ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == 3, + ASSERT (nm_setting_ip_config_get_num_addresses (s_ip4) == 3, "wired-ipv4-manual-verify-ip4", "failed to verify %s: unexpected %s / %s key value", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_ADDRESSES); /* Address #1 */ - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert (ip4_addr != NULL); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "1.2.3.4"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); /* Address #2 */ - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 1); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 1); g_assert (ip4_addr != NULL); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "9.8.7.6"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 16); /* Address #3 */ - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 2); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 2); g_assert (ip4_addr != NULL); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "3.3.3.3"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 8); @@ -1409,8 +1409,8 @@ test_read_wired_ipv6_manual (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -1482,54 +1482,54 @@ test_read_wired_ipv6_manual (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* DNS Addresses */ - ASSERT (nm_setting_ip4_config_get_num_dns (s_ip4) == 2, + ASSERT (nm_setting_ip_config_get_num_dns (s_ip4) == 2, "wired-ipv6-manual-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); /* DNS search domains */ - ASSERT (nm_setting_ip4_config_get_num_dns_searches (s_ip4) == 3, + ASSERT (nm_setting_ip_config_get_num_dns_searches (s_ip4) == 3, "wired-ipv6-manual-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - tmp = nm_setting_ip4_config_get_dns_search (s_ip4, 0); + tmp = nm_setting_ip_config_get_dns_search (s_ip4, 0); ASSERT (tmp != NULL, "wired-ipv6-manual-verify-ip4", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); ASSERT (strcmp (tmp, "lorem.com") == 0, "wired-ipv6-manual-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); - tmp = nm_setting_ip4_config_get_dns_search (s_ip4, 1); + tmp = nm_setting_ip_config_get_dns_search (s_ip4, 1); ASSERT (tmp != NULL, "wired-ipv6-manual-verify-ip4", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); ASSERT (strcmp (tmp, "ipsum.org") == 0, "wired-ipv6-manual-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); - tmp = nm_setting_ip4_config_get_dns_search (s_ip4, 2); + tmp = nm_setting_ip_config_get_dns_search (s_ip4, 2); ASSERT (tmp != NULL, "wired-ipv6-manual-verify-ip4", "failed to verify %s: missing %s / %s key", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); ASSERT (strcmp (tmp, "dolor.edu") == 0, "wired-ipv6-manual-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); /* ===== IPv6 SETTING ===== */ @@ -1540,61 +1540,61 @@ test_read_wired_ipv6_manual (void) NM_SETTING_IP6_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip6_config_get_method (s_ip6); + tmp = nm_setting_ip_config_get_method (s_ip6); ASSERT (strcmp (tmp, NM_SETTING_IP6_CONFIG_METHOD_MANUAL) == 0, "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); - ASSERT (nm_setting_ip6_config_get_never_default (s_ip6) == FALSE, + ASSERT (nm_setting_ip_config_get_never_default (s_ip6) == FALSE, "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_NEVER_DEFAULT); + NM_SETTING_IP_CONFIG_NEVER_DEFAULT); - ASSERT (nm_setting_ip6_config_get_may_fail (s_ip6) == TRUE, + ASSERT (nm_setting_ip_config_get_may_fail (s_ip6) == TRUE, "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_MAY_FAIL); + NM_SETTING_IP_CONFIG_MAY_FAIL); /* IP addresses */ - ASSERT (nm_setting_ip6_config_get_num_addresses (s_ip6) == 3, + ASSERT (nm_setting_ip_config_get_num_addresses (s_ip6) == 3, "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_ADDRESSES); /* Address #1 */ - ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); + ip6_addr = nm_setting_ip_config_get_address (s_ip6, 0); g_assert (ip6_addr != NULL); g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "1001:abba::1234"); g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56); /* Address #2 */ - ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 1); + ip6_addr = nm_setting_ip_config_get_address (s_ip6, 1); g_assert (ip6_addr != NULL); g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "2001:abba::2234"); g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64); /* Address #3 */ - ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 2); + ip6_addr = nm_setting_ip_config_get_address (s_ip6, 2); g_assert (ip6_addr != NULL); g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "3001:abba::3234"); g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 96); /* Routes */ - g_assert_cmpint (nm_setting_ip6_config_get_num_routes (s_ip6), ==, 2); + g_assert_cmpint (nm_setting_ip_config_get_num_routes (s_ip6), ==, 2); /* Route #1 */ - ip6_route = nm_setting_ip6_config_get_route (s_ip6, 0); + ip6_route = nm_setting_ip_config_get_route (s_ip6, 0); g_assert (ip6_route); g_assert_cmpstr (nm_ip_route_get_dest (ip6_route), ==, "9876::1234"); g_assert_cmpint (nm_ip_route_get_prefix (ip6_route), ==, 96); g_assert_cmpstr (nm_ip_route_get_next_hop (ip6_route), ==, "9876::7777"); g_assert_cmpint (nm_ip_route_get_metric (ip6_route), ==, 2); /* Route #2 */ - ip6_route = nm_setting_ip6_config_get_route (s_ip6, 1); + ip6_route = nm_setting_ip_config_get_route (s_ip6, 1); g_assert (ip6_route); g_assert_cmpstr (nm_ip_route_get_dest (ip6_route), ==, "abbe::cafe"); g_assert_cmpint (nm_ip_route_get_prefix (ip6_route), ==, 64); @@ -1602,30 +1602,30 @@ test_read_wired_ipv6_manual (void) g_assert_cmpint (nm_ip_route_get_metric (ip6_route), ==, 777); /* DNS Addresses */ - ASSERT (nm_setting_ip6_config_get_num_dns (s_ip6) == 2, + ASSERT (nm_setting_ip_config_get_num_dns (s_ip6) == 2, "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (strcmp (nm_setting_ip6_config_get_dns (s_ip6, 0), "1:2:3:4::a") == 0, + ASSERT (strcmp (nm_setting_ip_config_get_dns (s_ip6, 0), "1:2:3:4::a") == 0, "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected %s / %s key value #1", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (strcmp (nm_setting_ip6_config_get_dns (s_ip6, 1), "1:2:3:4::b") == 0, + ASSERT (strcmp (nm_setting_ip_config_get_dns (s_ip6, 1), "1:2:3:4::b") == 0, "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected %s / %s key value #2", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); /* DNS domains - none as domains are stuffed to 'ipv4' setting */ - ASSERT (nm_setting_ip6_config_get_num_dns_searches (s_ip6) == 0, + ASSERT (nm_setting_ip_config_get_num_dns_searches (s_ip6) == 0, "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); g_free (unmanaged); g_free (keyfile); @@ -1642,8 +1642,8 @@ test_read_wired_ipv6_only (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -1710,12 +1710,12 @@ test_read_wired_ipv6_only (void) TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP4_CONFIG_SETTING_NAME); - method = nm_setting_ip4_config_get_method (s_ip4); + method = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0, "wired-ipv6-only-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* ===== IPv6 SETTING ===== */ @@ -1726,44 +1726,44 @@ test_read_wired_ipv6_only (void) NM_SETTING_IP6_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip6_config_get_method (s_ip6); + tmp = nm_setting_ip_config_get_method (s_ip6); ASSERT (strcmp (tmp, NM_SETTING_IP6_CONFIG_METHOD_MANUAL) == 0, "wired-ipv6-only-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* IP addresses */ - ASSERT (nm_setting_ip6_config_get_num_addresses (s_ip6) == 1, + ASSERT (nm_setting_ip_config_get_num_addresses (s_ip6) == 1, "wired-ipv6-only-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_ADDRESSES); /* Address #1 */ - ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); + ip6_addr = nm_setting_ip_config_get_address (s_ip6, 0); g_assert (ip6_addr != NULL); g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "1001:abba::1234"); g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 56); /* DNS Addresses */ - ASSERT (nm_setting_ip6_config_get_num_dns (s_ip6) == 1, + ASSERT (nm_setting_ip_config_get_num_dns (s_ip6) == 1, "wired-ipv6-only-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (strcmp (nm_setting_ip6_config_get_dns (s_ip6, 0), "1:2:3:4::a") == 0, + ASSERT (strcmp (nm_setting_ip_config_get_dns (s_ip6, 0), "1:2:3:4::a") == 0, "wired-ipv6-only-verify-ip6", "failed to verify %s: unexpected %s / %s key value #1", TEST_IFCFG_WIRED_IPV6_MANUAL, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); /* DNS domains should be in IPv6, because IPv4 is disabled */ - g_assert_cmpint (nm_setting_ip6_config_get_num_dns_searches (s_ip6), ==, 3); - g_assert_cmpstr (nm_setting_ip6_config_get_dns_search (s_ip6, 0), ==, "lorem.com"); - g_assert_cmpstr (nm_setting_ip6_config_get_dns_search (s_ip6, 1), ==, "ipsum.org"); - g_assert_cmpstr (nm_setting_ip6_config_get_dns_search (s_ip6, 2), ==, "dolor.edu"); + g_assert_cmpint (nm_setting_ip_config_get_num_dns_searches (s_ip6), ==, 3); + g_assert_cmpstr (nm_setting_ip_config_get_dns_search (s_ip6, 0), ==, "lorem.com"); + g_assert_cmpstr (nm_setting_ip_config_get_dns_search (s_ip6, 1), ==, "ipsum.org"); + g_assert_cmpstr (nm_setting_ip_config_get_dns_search (s_ip6, 2), ==, "dolor.edu"); g_free (unmanaged); g_free (keyfile); @@ -1780,8 +1780,8 @@ test_read_wired_dhcp6_only (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -1847,12 +1847,12 @@ test_read_wired_dhcp6_only (void) TEST_IFCFG_WIRED_DHCP6_ONLY, NM_SETTING_IP4_CONFIG_SETTING_NAME); - method = nm_setting_ip4_config_get_method (s_ip4); + method = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0, "wired-dhcp6-only-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DHCP6_ONLY, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* ===== IPv6 SETTING ===== */ @@ -1863,12 +1863,12 @@ test_read_wired_dhcp6_only (void) NM_SETTING_IP6_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip6_config_get_method (s_ip6); + tmp = nm_setting_ip_config_get_method (s_ip6); ASSERT (strcmp (tmp, NM_SETTING_IP6_CONFIG_METHOD_DHCP) == 0, "wired-dhcp6-only-verify-ip6", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_DHCP6_ONLY, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); g_free (unmanaged); g_free (keyfile); @@ -1937,8 +1937,8 @@ static void test_read_noip (void) { NMConnection *connection; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -1961,13 +1961,13 @@ test_read_noip (void) s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_DISABLED); - g_assert (nm_setting_ip4_config_get_never_default (s_ip4) == FALSE); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_DISABLED); + g_assert (nm_setting_ip_config_get_never_default (s_ip4) == FALSE); s_ip6 = nm_connection_get_setting_ip6_config (connection); g_assert (s_ip6); - g_assert_cmpstr (nm_setting_ip6_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE); - g_assert (nm_setting_ip6_config_get_never_default (s_ip6) == FALSE); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip6), ==, NM_SETTING_IP6_CONFIG_METHOD_IGNORE); + g_assert (nm_setting_ip_config_get_never_default (s_ip6) == FALSE); g_free (unmanaged); g_free (keyfile); @@ -1984,7 +1984,7 @@ test_read_wired_8021x_peap_mschapv2 (void) { NMConnection *connection; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSetting8021x *s_8021x; NMSetting8021x *tmp_8021x; char *unmanaged = NULL; @@ -2036,12 +2036,12 @@ test_read_wired_8021x_peap_mschapv2 (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wired-8021x-peap-mschapv2-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_8021x_PEAP_MSCHAPV2, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* ===== 802.1x SETTING ===== */ s_8021x = nm_connection_get_setting_802_1x (connection); @@ -2365,7 +2365,7 @@ test_read_wired_aliases_good (void) { NMConnection *connection; NMSettingConnection *s_con; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -2425,18 +2425,18 @@ test_read_wired_aliases_good (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_MANUAL) == 0, "aliases-good-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_ALIASES_GOOD, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); - ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == expected_num_addresses, + ASSERT (nm_setting_ip_config_get_num_addresses (s_ip4) == expected_num_addresses, "aliases-good-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_ALIASES_GOOD, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_ADDRESSES); /* Addresses */ for (i = 0; i < expected_num_addresses; i++) { @@ -2444,7 +2444,7 @@ test_read_wired_aliases_good (void) const char *addr; GVariant *label; - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, i); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, i); g_assert (ip4_addr != NULL); addr = nm_ip_address_get_address (ip4_addr); @@ -2487,7 +2487,7 @@ test_read_wired_aliases_bad (const char *base, const char *expected_id) { NMConnection *connection; NMSettingConnection *s_con; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -2543,21 +2543,21 @@ test_read_wired_aliases_bad (const char *base, const char *expected_id) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_MANUAL) == 0, "aliases-bad-verify-ip4", "failed to verify %s: unexpected %s / %s key value", base, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); - ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == 1, + ASSERT (nm_setting_ip_config_get_num_addresses (s_ip4) == 1, "aliases-bad-verify-ip4", "failed to verify %s: unexpected %s / %s key value", base, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_ADDRESSES); /* Addresses */ - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert (ip4_addr != NULL); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); @@ -2599,7 +2599,7 @@ test_read_wifi_open (void) NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -2754,12 +2754,12 @@ test_read_wifi_open (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wifi-open-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_OPEN, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); g_free (unmanaged); g_free (keyfile); @@ -3063,7 +3063,7 @@ test_read_wifi_wep (void) NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -3298,12 +3298,12 @@ test_read_wifi_wep (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wifi-wep-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WEP, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); g_free (unmanaged); g_free (keyfile); @@ -3321,7 +3321,7 @@ test_read_wifi_wep_adhoc (void) NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -3512,38 +3512,38 @@ test_read_wifi_wep_adhoc (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wifi-wep-adhoc-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WEP_ADHOC, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* Ignore auto DNS */ - ASSERT (nm_setting_ip4_config_get_ignore_auto_dns (s_ip4) == TRUE, + ASSERT (nm_setting_ip_config_get_ignore_auto_dns (s_ip4) == TRUE, "wifi-wep-adhoc-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WEP_ADHOC, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS); + NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS); /* DNS Addresses */ - ASSERT (nm_setting_ip4_config_get_num_dns (s_ip4) == 2, + ASSERT (nm_setting_ip_config_get_num_dns (s_ip4) == 2, "wifi-wep-adhoc-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WEP_ADHOC, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (strcmp (nm_setting_ip4_config_get_dns (s_ip4, 0), "4.2.2.1") == 0, + ASSERT (strcmp (nm_setting_ip_config_get_dns (s_ip4, 0), "4.2.2.1") == 0, "wifi-wep-adhoc-verify-ip4", "failed to verify %s: unexpected %s / %s key value #1", TEST_IFCFG_WIFI_WEP_ADHOC, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (strcmp (nm_setting_ip4_config_get_dns (s_ip4, 1), "4.2.2.2") == 0, + ASSERT (strcmp (nm_setting_ip_config_get_dns (s_ip4, 1), "4.2.2.2") == 0, "wifi-wep-adhoc-verify-ip4", "failed to verify %s: unexpected %s / %s key value #2", TEST_IFCFG_WIFI_WEP_ADHOC, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); g_free (unmanaged); g_free (keyfile); @@ -4113,7 +4113,7 @@ test_read_wifi_wpa_psk (void) NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -4391,12 +4391,12 @@ test_read_wifi_wpa_psk (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wifi-wpa-psk-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WPA_PSK, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); g_free (unmanaged); g_free (keyfile); @@ -4632,7 +4632,7 @@ test_read_wifi_wpa_psk_adhoc (void) NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -4787,12 +4787,12 @@ test_read_wifi_wpa_psk_adhoc (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wifi-wpa-psk-adhoc-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WPA_PSK_ADHOC, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); g_free (unmanaged); g_free (keyfile); @@ -4810,7 +4810,7 @@ test_read_wifi_wpa_psk_hex (void) NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -4929,12 +4929,12 @@ test_read_wifi_wpa_psk_hex (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wifi-wpa-psk-hex-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WPA_PSK_HEX, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); g_free (unmanaged); g_free (keyfile); @@ -4953,7 +4953,7 @@ test_read_wifi_wpa_eap_tls (void) { NMConnection *connection; NMSettingWireless *s_wireless; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSetting8021x *s_8021x; char *unmanaged = NULL; char *keyfile = NULL; @@ -5000,12 +5000,12 @@ test_read_wifi_wpa_eap_tls (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wifi-wpa-eap-tls-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WPA_EAP_TLS, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* ===== 802.1x SETTING ===== */ s_8021x = nm_connection_get_setting_802_1x (connection); @@ -5093,7 +5093,7 @@ test_read_wifi_wpa_eap_ttls_tls (void) { NMConnection *connection; NMSettingWireless *s_wireless; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSetting8021x *s_8021x; char *unmanaged = NULL; char *keyfile = NULL; @@ -5140,12 +5140,12 @@ test_read_wifi_wpa_eap_ttls_tls (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wifi-wpa-eap-ttls-tls-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WPA_EAP_TTLS_TLS, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* ===== 802.1x SETTING ===== */ s_8021x = nm_connection_get_setting_802_1x (connection); @@ -5324,7 +5324,7 @@ test_read_wifi_wep_eap_ttls_chap (void) NMConnection *connection; NMSettingWireless *s_wireless; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSetting8021x *s_8021x; char *unmanaged = NULL; char *keyfile = NULL; @@ -5372,12 +5372,12 @@ test_read_wifi_wep_eap_ttls_chap (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "wifi-wep-eap-ttls-chap-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIFI_WEP_EAP_TTLS_CHAP, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* ===== 802.1x SETTING ===== */ s_wsec = nm_connection_get_setting_wireless_security (connection); @@ -5736,7 +5736,7 @@ test_read_wired_qeth_static (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *keyfile = NULL; char *routefile = NULL; @@ -5884,12 +5884,12 @@ test_read_wired_qeth_static (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_MANUAL) == 0, "wired-qeth-static-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_WIRED_QETH_STATIC, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); g_free (unmanaged); g_free (keyfile); @@ -6226,8 +6226,8 @@ test_write_wired_static (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4, *reread_s_ip4; - NMSettingIP6Config *s_ip6, *reread_s_ip6; + NMSettingIPConfig *s_ip4, *reread_s_ip4; + NMSettingIPConfig *s_ip6, *reread_s_ip6; static const char *mac = "31:33:33:37:be:cd"; guint32 mtu = 1492; char *uuid; @@ -6276,53 +6276,53 @@ test_write_wired_static (void) NULL); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, - NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); addr = nm_ip_address_new (AF_INET, "1.1.1.5", 24, "1.1.1.1", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); - nm_setting_ip4_config_add_dns (s_ip4, dns1); - nm_setting_ip4_config_add_dns (s_ip4, dns2); + nm_setting_ip_config_add_dns (s_ip4, dns1); + nm_setting_ip_config_add_dns (s_ip4, dns2); - nm_setting_ip4_config_add_dns_search (s_ip4, dns_search1); - nm_setting_ip4_config_add_dns_search (s_ip4, dns_search2); + nm_setting_ip_config_add_dns_search (s_ip4, dns_search1); + nm_setting_ip_config_add_dns_search (s_ip4, dns_search2); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); /* Add addresses */ addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, NULL, &error); g_assert_no_error (error); - nm_setting_ip6_config_add_address (s_ip6, addr6); + nm_setting_ip_config_add_address (s_ip6, addr6); nm_ip_address_unref (addr6); addr6 = nm_ip_address_new (AF_INET6, "2003:1234:abcd::2", 22, NULL, &error); g_assert_no_error (error); - nm_setting_ip6_config_add_address (s_ip6, addr6); + nm_setting_ip_config_add_address (s_ip6, addr6); nm_ip_address_unref (addr6); addr6 = nm_ip_address_new (AF_INET6, "3003:1234:abcd::3", 33, NULL, &error); g_assert_no_error (error); - nm_setting_ip6_config_add_address (s_ip6, addr6); + nm_setting_ip_config_add_address (s_ip6, addr6); nm_ip_address_unref (addr6); /* Add routes */ @@ -6330,21 +6330,21 @@ test_write_wired_static (void) "2222:aaaa:bbbb:cccc::", 64, "2222:aaaa:bbbb:cccc:dddd:eeee:5555:6666", 99, &error); g_assert_no_error (error); - nm_setting_ip6_config_add_route (s_ip6, route6); + nm_setting_ip_config_add_route (s_ip6, route6); nm_ip_route_unref (route6); route6 = nm_ip_route_new (AF_INET6, "::", 128, "2222:aaaa::9999", 1, &error); g_assert_no_error (error); - nm_setting_ip6_config_add_route (s_ip6, route6); + nm_setting_ip_config_add_route (s_ip6, route6); nm_ip_route_unref (route6); /* DNS servers */ - nm_setting_ip6_config_add_dns (s_ip6, dns6_1); - nm_setting_ip6_config_add_dns (s_ip6, dns6_2); + nm_setting_ip_config_add_dns (s_ip6, dns6_1); + nm_setting_ip_config_add_dns (s_ip6, dns6_2); /* DNS domains */ - nm_setting_ip6_config_add_dns_search (s_ip6, dns_search3); - nm_setting_ip6_config_add_dns_search (s_ip6, dns_search4); + nm_setting_ip_config_add_dns_search (s_ip6, dns_search3); + nm_setting_ip_config_add_dns_search (s_ip6, dns_search4); ASSERT (nm_connection_verify (connection, &error) == TRUE, "wired-static-write", "failed to verify connection: %s", @@ -6389,10 +6389,10 @@ test_write_wired_static (void) */ reread_s_ip4 = nm_connection_get_setting_ip4_config (reread); reread_s_ip6 = nm_connection_get_setting_ip6_config (reread); - nm_setting_ip6_config_add_dns_search (reread_s_ip6, nm_setting_ip4_config_get_dns_search (reread_s_ip4, 2)); - nm_setting_ip6_config_add_dns_search (reread_s_ip6, nm_setting_ip4_config_get_dns_search (reread_s_ip4, 3)); - nm_setting_ip4_config_remove_dns_search (reread_s_ip4, 3); - nm_setting_ip4_config_remove_dns_search (reread_s_ip4, 2); + nm_setting_ip_config_add_dns_search (reread_s_ip6, nm_setting_ip_config_get_dns_search (reread_s_ip4, 2)); + nm_setting_ip_config_add_dns_search (reread_s_ip6, nm_setting_ip_config_get_dns_search (reread_s_ip4, 3)); + nm_setting_ip_config_remove_dns_search (reread_s_ip4, 3); + nm_setting_ip_config_remove_dns_search (reread_s_ip4, 2); ASSERT (nm_connection_compare (connection, reread, NM_SETTING_COMPARE_FLAG_EXACT) == TRUE, "wired-static-write", "written and re-read connection weren't the same."); @@ -6416,8 +6416,8 @@ test_write_wired_dhcp (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -6448,15 +6448,15 @@ test_write_wired_dhcp (void) nm_connection_add_setting (connection, NM_SETTING (s_wired)); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, "random-client-id-00:22:33", - NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, "awesome-hostname", - NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, TRUE, - NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, TRUE, + NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, "awesome-hostname", + NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, TRUE, + NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -6464,12 +6464,12 @@ test_write_wired_dhcp (void) (error && error->message) ? error->message : "(unknown)"); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); /* Save the ifcfg */ @@ -6564,8 +6564,8 @@ static void test_read_write_wired_dhcp_send_hostname (void) { NMConnection *connection, *reread; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; const char * dhcp_hostname = "kamil-patka"; char *written = NULL; GError *error = NULL; @@ -6582,14 +6582,14 @@ test_read_write_wired_dhcp_send_hostname (void) s_ip6 = nm_connection_get_setting_ip6_config (connection); g_assert (s_ip4); g_assert (s_ip6); - g_assert (nm_setting_ip4_config_get_dhcp_send_hostname (s_ip4) == TRUE); - g_assert_cmpstr (nm_setting_ip4_config_get_dhcp_hostname (s_ip4), ==, "svata-pulec"); - g_assert_cmpstr (nm_setting_ip6_config_get_dhcp_hostname (s_ip6), ==, "svata-pulec"); + g_assert (nm_setting_ip_config_get_dhcp_send_hostname (s_ip4) == TRUE); + g_assert_cmpstr (nm_setting_ip_config_get_dhcp_hostname (s_ip4), ==, "svata-pulec"); + g_assert_cmpstr (nm_setting_ip_config_get_dhcp_hostname (s_ip6), ==, "svata-pulec"); /* Set dhcp-send-hostname=false dhcp-hostname="kamil-patka" and write the connection. */ - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME, FALSE, NULL); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, dhcp_hostname, NULL); - g_object_set (s_ip6, NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, dhcp_hostname, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, FALSE, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, dhcp_hostname, NULL); + g_object_set (s_ip6, NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, dhcp_hostname, NULL); success = writer_new_connection (connection, TEST_SCRATCH_DIR "/network-scripts/", @@ -6621,9 +6621,9 @@ test_read_write_wired_dhcp_send_hostname (void) s_ip6 = nm_connection_get_setting_ip6_config (reread); g_assert (s_ip4); g_assert (s_ip6); - g_assert (nm_setting_ip4_config_get_dhcp_send_hostname (s_ip4) == FALSE); - g_assert_cmpstr (nm_setting_ip4_config_get_dhcp_hostname (s_ip4), ==, dhcp_hostname); - g_assert_cmpstr (nm_setting_ip6_config_get_dhcp_hostname (s_ip6), ==, dhcp_hostname); + g_assert (nm_setting_ip_config_get_dhcp_send_hostname (s_ip4) == FALSE); + g_assert_cmpstr (nm_setting_ip_config_get_dhcp_hostname (s_ip4), ==, dhcp_hostname); + g_assert_cmpstr (nm_setting_ip_config_get_dhcp_hostname (s_ip6), ==, dhcp_hostname); g_object_unref (connection); g_object_unref (reread); @@ -6636,8 +6636,8 @@ test_write_wired_static_ip6_only (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; static const char *mac = "31:33:33:37:be:cd"; char *uuid; const char *dns6 = "fade:0102:0103::face"; @@ -6673,29 +6673,29 @@ test_write_wired_static_ip6_only (void) g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, NULL); /* Add addresses */ addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, NULL, &error); g_assert_no_error (error); - nm_setting_ip6_config_add_address (s_ip6, addr6); + nm_setting_ip_config_add_address (s_ip6, addr6); nm_ip_address_unref (addr6); /* DNS server */ - nm_setting_ip6_config_add_dns (s_ip6, dns6); + nm_setting_ip_config_add_dns (s_ip6, dns6); ASSERT (nm_connection_verify (connection, &error) == TRUE, "wired-static-ip6-only-write", "failed to verify connection: %s", @@ -6764,8 +6764,8 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; static const char *mac = "31:33:33:37:be:cd"; char *uuid; const char *dns6 = "fade:0102:0103::face"; @@ -6802,29 +6802,29 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, NULL); /* Add addresses */ addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, gateway6, &error); g_assert_no_error (error); - nm_setting_ip6_config_add_address (s_ip6, addr6); + nm_setting_ip_config_add_address (s_ip6, addr6); nm_ip_address_unref (addr6); /* DNS server */ - nm_setting_ip6_config_add_dns (s_ip6, dns6); + nm_setting_ip_config_add_dns (s_ip6, dns6); g_assert (nm_connection_verify (connection, &error)); @@ -6867,8 +6867,8 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) /* access the gateway from the loaded connection. */ s_ip6 = nm_connection_get_setting_ip6_config (reread); - g_assert (s_ip6 && nm_setting_ip6_config_get_num_addresses (s_ip6)==1); - addr6 = nm_setting_ip6_config_get_address (s_ip6, 0); + g_assert (s_ip6 && nm_setting_ip_config_get_num_addresses (s_ip6)==1); + addr6 = nm_setting_ip_config_get_address (s_ip6, 0); g_assert (addr6); /* assert that the gateway was written and reloaded as expected */ @@ -6895,7 +6895,7 @@ test_read_write_static_routes_legacy (void) NMConnection *connection, *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; char *testfile = NULL; char *keyfile = NULL; @@ -6966,18 +6966,18 @@ test_read_write_static_routes_legacy (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "read-write-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_READ_WRITE_STATIC_ROUTES_LEGACY, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); - ASSERT (nm_setting_ip4_config_get_never_default (s_ip4) == FALSE, + ASSERT (nm_setting_ip_config_get_never_default (s_ip4) == FALSE, "read-write-static-routes-legacy-verify-ip4", "failed to verify %s: unexpected %s / %s key value", TEST_IFCFG_READ_WRITE_STATIC_ROUTES_LEGACY, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_NEVER_DEFAULT); + NM_SETTING_IP_CONFIG_NEVER_DEFAULT); /* Save the ifcfg; use a special different scratch dir to ensure that * we can clean up after the written connection in both the original @@ -7042,8 +7042,8 @@ test_write_wired_static_routes (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; static const char *mac = "31:33:33:37:be:cd"; guint32 mtu = 1492; char *uuid; @@ -7087,47 +7087,47 @@ test_write_wired_static_routes (void) NULL); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); addr = nm_ip_address_new (AF_INET, "1.1.1.5", 24, "1.1.1.1", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); /* Write out routes */ route = nm_ip_route_new (AF_INET, "1.2.3.0", 24, "222.173.190.239", 0, &error); g_assert_no_error (error); - nm_setting_ip4_config_add_route (s_ip4, route); + nm_setting_ip_config_add_route (s_ip4, route); nm_ip_route_unref (route); route = nm_ip_route_new (AF_INET, "3.2.1.0", 24, "202.254.186.190", 77, &error); g_assert_no_error (error); - nm_setting_ip4_config_add_route (s_ip4, route); + nm_setting_ip_config_add_route (s_ip4, route); nm_ip_route_unref (route); - nm_setting_ip4_config_add_dns (s_ip4, dns1); - nm_setting_ip4_config_add_dns (s_ip4, dns2); + nm_setting_ip_config_add_dns (s_ip4, dns1); + nm_setting_ip_config_add_dns (s_ip4, dns2); - nm_setting_ip4_config_add_dns_search (s_ip4, dns_search1); - nm_setting_ip4_config_add_dns_search (s_ip4, dns_search2); + nm_setting_ip_config_add_dns_search (s_ip4, dns_search1); + nm_setting_ip_config_add_dns_search (s_ip4, dns_search2); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -7190,8 +7190,8 @@ test_write_wired_dhcp_8021x_peap_mschapv2 (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; NMSetting8021x *s_8021x; char *uuid; gboolean success; @@ -7223,18 +7223,18 @@ test_write_wired_dhcp_8021x_peap_mschapv2 (void) nm_connection_add_setting (connection, NM_SETTING (s_wired)); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); /* 802.1x setting */ @@ -7344,8 +7344,8 @@ test_write_wired_8021x_tls (NMSetting8021xCKScheme scheme, NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; NMSetting8021x *s_8021x; char *uuid; gboolean success; @@ -7383,17 +7383,17 @@ test_write_wired_8021x_tls (NMSetting8021xCKScheme scheme, nm_connection_add_setting (connection, NM_SETTING (s_wired)); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_assert (s_ip6); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); @@ -7558,7 +7558,7 @@ test_write_wired_aliases (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *uuid; int num_addresses = 4; const char *ip[] = { "1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4" }; @@ -7602,15 +7602,15 @@ test_write_wired_aliases (void) nm_connection_add_setting (connection, NM_SETTING (s_wired)); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); ASSERT (s_ip4 != NULL, "wired-aliases-write", "failed to allocate new %s setting", NM_SETTING_IP4_CONFIG_SETTING_NAME); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, - NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); for (i = 0; i < num_addresses; i++) { @@ -7618,7 +7618,7 @@ test_write_wired_aliases (void) g_assert_no_error (error); if (label[i]) nm_ip_address_set_attribute (addr, "label", g_variant_new_string (label[i])); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); } @@ -7688,17 +7688,17 @@ test_write_wired_aliases (void) * verify the aliases manually. */ s_ip4 = nm_connection_get_setting_ip4_config (connection); - ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == num_addresses, + ASSERT (nm_setting_ip_config_get_num_addresses (s_ip4) == num_addresses, "wired-aliases-write-verify-ip4", "failed to verify %s: unexpected %s / %s key value", testfile, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_ADDRESSES); /* Addresses */ for (i = 0; i < num_addresses; i++) { const char *addrstr; - addr = nm_setting_ip4_config_get_address (s_ip4, i); + addr = nm_setting_ip_config_get_address (s_ip4, i); g_assert (addr != NULL); addrstr = nm_ip_address_get_address (addr); @@ -7739,7 +7739,7 @@ test_write_gateway (void) NMConnection *connection, *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *uuid, *testfile = NULL, *val; gboolean success; GError *error = NULL; @@ -7765,22 +7765,22 @@ test_write_gateway (void) nm_connection_add_setting (connection, NM_SETTING (s_wired)); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, - NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.254", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); addr = nm_ip_address_new (AF_INET, "2.2.2.5", 24, "2.2.2.254", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); success = nm_connection_verify (connection, &error); @@ -7871,8 +7871,8 @@ test_write_wifi_open (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWireless *s_wifi; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -7924,18 +7924,18 @@ test_write_wifi_open (void) g_bytes_unref (ssid); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -8011,8 +8011,8 @@ test_write_wifi_open_hex_ssid (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWireless *s_wifi; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -8054,18 +8054,18 @@ test_write_wifi_open_hex_ssid (void) g_bytes_unref (ssid); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -8125,8 +8125,8 @@ test_write_wifi_wep (void) NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -8183,18 +8183,18 @@ test_write_wifi_wep (void) nm_setting_wireless_security_set_wep_key (s_wsec, 3, "BBBBBBBBBBBBBBBBBBBBBBBBBB"); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -8266,8 +8266,8 @@ test_write_wifi_wep_adhoc (void) NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -8319,26 +8319,26 @@ test_write_wifi_wep_adhoc (void) nm_setting_wireless_security_set_wep_key (s_wsec, 0, "0123456789abcdef0123456789"); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); /* IP Address */ addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); - nm_setting_ip4_config_add_dns (s_ip4, dns1); + nm_setting_ip_config_add_dns (s_ip4, dns1); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -8410,8 +8410,8 @@ test_write_wifi_wep_passphrase (void) NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -8466,18 +8466,18 @@ test_write_wifi_wep_passphrase (void) nm_setting_wireless_security_set_wep_key (s_wsec, 0, "asdfdjaslfjasd;flasjdfl;aksdf"); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -8549,8 +8549,8 @@ test_write_wifi_wep_40_ascii (void) NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -8607,18 +8607,18 @@ test_write_wifi_wep_40_ascii (void) nm_setting_wireless_security_set_wep_key (s_wsec, 3, "donec"); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -8690,8 +8690,8 @@ test_write_wifi_wep_104_ascii (void) NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -8748,18 +8748,18 @@ test_write_wifi_wep_104_ascii (void) nm_setting_wireless_security_set_wep_key (s_wsec, 3, "thisismyascii"); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -8831,8 +8831,8 @@ test_write_wifi_leap (void) NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -8886,18 +8886,18 @@ test_write_wifi_leap (void) NULL); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -8969,8 +8969,8 @@ test_write_wifi_leap_secret_flags (NMSettingSecretFlags flags) NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -9025,19 +9025,19 @@ test_write_wifi_leap_secret_flags (NMSettingSecretFlags flags) NULL); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_assert (s_ip6); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); success = nm_connection_verify (connection, &error); @@ -9108,8 +9108,8 @@ test_write_wifi_wpa_psk (const char *name, NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid, *tmp; gboolean success; GError *error = NULL; @@ -9177,18 +9177,18 @@ test_write_wifi_wpa_psk (const char *name, } /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -9254,8 +9254,8 @@ test_write_wifi_wpa_psk_adhoc (void) NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -9313,26 +9313,26 @@ test_write_wifi_wpa_psk_adhoc (void) nm_setting_wireless_security_add_group (s_wsec, "tkip"); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); /* IP Address */ addr = nm_ip_address_new (AF_INET, "1.1.1.3", 25, "1.1.1.1", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); - nm_setting_ip4_config_add_dns (s_ip4, dns1); + nm_setting_ip_config_add_dns (s_ip4, dns1); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -9397,8 +9397,8 @@ test_write_wifi_wpa_eap_tls (void) NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; NMSetting8021x *s_8021x; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -9485,18 +9485,18 @@ test_write_wifi_wpa_eap_tls (void) TEST_IFCFG_WIFI_WPA_EAP_TLS_PRIVATE_KEY, error->message); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -9561,8 +9561,8 @@ test_write_wifi_wpa_eap_ttls_tls (void) NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; NMSetting8021x *s_8021x; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -9667,18 +9667,18 @@ test_write_wifi_wpa_eap_ttls_tls (void) TEST_IFCFG_WIFI_WPA_EAP_TLS_PRIVATE_KEY, error->message); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -9743,8 +9743,8 @@ test_write_wifi_wpa_eap_ttls_mschapv2 (void) NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; NMSetting8021x *s_8021x; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -9821,18 +9821,18 @@ test_write_wifi_wpa_eap_ttls_mschapv2 (void) /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -9896,8 +9896,8 @@ test_write_wifi_wpa_then_open (void) NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -9964,20 +9964,20 @@ test_write_wifi_wpa_then_open (void) nm_setting_wireless_security_add_group (s_wsec, "ccmp"); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_assert (s_ip6); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); success = nm_connection_verify (connection, &error); @@ -10086,8 +10086,8 @@ test_write_wifi_wpa_then_wep_with_perms (void) NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -10160,20 +10160,20 @@ test_write_wifi_wpa_then_wep_with_perms (void) nm_setting_wireless_security_add_group (s_wsec, "ccmp"); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_assert (s_ip6); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); success = nm_connection_verify (connection, &error); @@ -10290,8 +10290,8 @@ test_write_wifi_dynamic_wep_leap (void) NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; NMSetting8021x *s_8021x; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -10356,19 +10356,19 @@ test_write_wifi_dynamic_wep_leap (void) NULL); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_assert (s_ip6); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); success = nm_connection_verify (connection, &error); @@ -10443,8 +10443,8 @@ test_write_wired_qeth_dhcp (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; char **subchans; gboolean success; @@ -10488,20 +10488,20 @@ test_write_wired_qeth_dhcp (void) nm_setting_wired_add_s390_option (s_wired, "protocol", "blahbalh"); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); /* Verify */ @@ -10564,8 +10564,8 @@ test_write_wired_ctc_dhcp (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; char **subchans; gboolean success; @@ -10609,19 +10609,19 @@ test_write_wired_ctc_dhcp (void) nm_setting_wired_add_s390_option (s_wired, "ctcprot", "0"); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_assert (s_ip6); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); /* Verify */ @@ -10697,8 +10697,8 @@ test_write_permissions (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -10733,20 +10733,20 @@ test_write_permissions (void) nm_connection_add_setting (connection, NM_SETTING (s_wired)); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); /* Verify */ @@ -10810,8 +10810,8 @@ test_write_wifi_wep_agent_keys (void) NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; const char *str_ssid = "foobarbaz"; GBytes *ssid; @@ -10841,19 +10841,19 @@ test_write_wifi_wep_agent_keys (void) g_free (uuid); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_assert (s_ip6); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); /* Wifi setting */ @@ -10948,7 +10948,7 @@ test_write_wired_pppoe (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSettingPppoe *s_pppoe; NMSettingPpp *s_ppp; char *uuid; @@ -10976,11 +10976,11 @@ test_write_wired_pppoe (void) nm_connection_add_setting (connection, NM_SETTING (s_wired)); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* PPPoE setting */ @@ -11017,7 +11017,7 @@ test_write_vpn (void) { NMConnection *connection; NMSettingConnection *s_con; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSettingVpn *s_vpn; char *uuid; gboolean success; @@ -11052,11 +11052,11 @@ test_write_vpn (void) nm_setting_vpn_add_secret (s_vpn, "password", "sup3rs3cr3t"); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -11079,7 +11079,7 @@ test_write_mobile_broadband (gboolean gsm) { NMConnection *connection; NMSettingConnection *s_con; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSettingGsm *s_gsm; NMSettingCdma *s_cdma; NMSettingPpp *s_ppp; @@ -11130,11 +11130,11 @@ test_write_mobile_broadband (gboolean gsm) NULL); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* PPP setting */ @@ -11217,8 +11217,8 @@ test_write_bridge_main (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingBridge *s_bridge; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; NMIPAddress *addr; static const char *mac = "31:33:33:37:be:cd"; @@ -11259,27 +11259,27 @@ test_write_bridge_main (void) NULL); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, - NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_assert (s_ip6); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL); nmtst_assert_connection_verifies_without_normalization (connection); @@ -11814,8 +11814,8 @@ test_write_ethernet_missing_ipv6 (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; GError *error = NULL; @@ -11849,14 +11849,14 @@ test_write_ethernet_missing_ipv6 (void) nm_connection_add_setting (connection, NM_SETTING (s_wired)); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, "random-client-id-00:22:33", - NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, TRUE, - NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, TRUE, + NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, TRUE, + NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, TRUE, NULL); /* IP6 setting */ @@ -11907,12 +11907,12 @@ test_write_ethernet_missing_ipv6 (void) * the comparison can succeed. Missing IPv6 setting should have been * written out (and re-read) as Automatic IPv6. */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_assert (s_ip6); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); ASSERT (nm_connection_compare (connection, reread, NM_SETTING_COMPARE_FLAG_EXACT) == TRUE, @@ -11999,8 +11999,8 @@ test_write_bond_main (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingBond *s_bond; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; NMSettingWired *s_wired; char *uuid; NMIPAddress *addr; @@ -12038,25 +12038,25 @@ test_write_bond_main (void) nm_connection_add_setting (connection, NM_SETTING (s_bond)); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, - NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL); nmtst_assert_connection_verifies_without_normalization (connection); @@ -12337,8 +12337,8 @@ test_write_infiniband (void) NMConnection *reread; NMSettingConnection *s_con; NMSettingInfiniband *s_infiniband; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; const char *mac = "80:00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22"; guint32 mtu = 65520; char *uuid; @@ -12378,25 +12378,25 @@ test_write_infiniband (void) NULL); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, - NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, addr); + nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, @@ -12672,8 +12672,8 @@ test_write_dcb_basic (void) NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingDcb *s_dcb; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; gboolean success, ignore_error; guint i; char *uuid, *testfile; @@ -12702,12 +12702,12 @@ test_write_dcb_basic (void) nm_connection_add_setting (connection, NM_SETTING (s_wired)); /* IP stuff */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); - g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); + g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); - g_object_set (G_OBJECT (s_ip6), NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); + g_object_set (G_OBJECT (s_ip6), NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); /* DCB */ @@ -12952,8 +12952,8 @@ test_write_fcoe_mode (gconstpointer user_data) NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingDcb *s_dcb; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; gboolean success, ignore_error; char *uuid, *testfile; @@ -12975,12 +12975,12 @@ test_write_fcoe_mode (gconstpointer user_data) nm_connection_add_setting (connection, NM_SETTING (s_wired)); /* IP stuff */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); - g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); + g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); - g_object_set (G_OBJECT (s_ip6), NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); + g_object_set (G_OBJECT (s_ip6), NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); /* DCB */ @@ -13074,8 +13074,8 @@ test_write_team_master (void) NMSettingConnection *s_con; NMSettingTeam *s_team; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid, *testfile = NULL, *val; gboolean success; GError *error = NULL; @@ -13111,21 +13111,21 @@ test_write_team_master (void) nm_connection_add_setting (connection, NM_SETTING (s_wired)); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_assert (s_ip6); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); nmtst_assert_connection_verifies_without_normalization (connection); diff --git a/src/settings/plugins/ifcfg-rh/writer.c b/src/settings/plugins/ifcfg-rh/writer.c index d22b30cdc6..d12ff03cf4 100644 --- a/src/settings/plugins/ifcfg-rh/writer.c +++ b/src/settings/plugins/ifcfg-rh/writer.c @@ -1741,7 +1741,7 @@ write_connection_setting (NMSettingConnection *s_con, shvarFile *ifcfg) } static gboolean -write_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError **error) +write_route_file_legacy (const char *filename, NMSettingIPConfig *s_ip4, GError **error) { const char *dest, *next_hop; char **route_items; @@ -1756,7 +1756,7 @@ write_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError g_return_val_if_fail (error != NULL, FALSE); g_return_val_if_fail (*error == NULL, FALSE); - num = nm_setting_ip4_config_get_num_routes (s_ip4); + num = nm_setting_ip_config_get_num_routes (s_ip4); if (num == 0) { unlink (filename); return TRUE; @@ -1764,7 +1764,7 @@ write_route_file_legacy (const char *filename, NMSettingIP4Config *s_ip4, GError route_items = g_malloc0 (sizeof (char*) * (num + 1)); for (i = 0; i < num; i++) { - route = nm_setting_ip4_config_get_route (s_ip4, i); + route = nm_setting_ip_config_get_route (s_ip4, i); dest = nm_ip_route_get_dest (route); prefix = nm_ip_route_get_prefix (route); @@ -1794,7 +1794,7 @@ error: static gboolean write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) { - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; const char *value; char *addr_key, *prefix_key, *netmask_key, *gw_key, *metric_key, *tmp; char *route_path = NULL; @@ -1807,7 +1807,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) s_ip4 = nm_connection_get_setting_ip4_config (connection); if (s_ip4) - method = nm_setting_ip4_config_get_method (s_ip4); + method = nm_setting_ip_config_get_method (s_ip4); /* Missing IP4 setting is assumed to be DHCP */ if (!method) @@ -1850,7 +1850,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) /* Temporarily create fake IP4 setting if missing; method set to DHCP above */ if (!s_ip4) { - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); fake_ip4 = TRUE; } @@ -1877,11 +1877,11 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) /* Write out IPADDR, PREFIX, GATEWAY for current IP addresses * without labels. Unset obsolete NETMASK. */ - num = nm_setting_ip4_config_get_num_addresses (s_ip4); + num = nm_setting_ip_config_get_num_addresses (s_ip4); for (i = n = 0; i < num; i++) { NMIPAddress *addr; - addr = nm_setting_ip4_config_get_address (s_ip4, i); + addr = nm_setting_ip_config_get_address (s_ip4, i); if (i > 0) { GVariant *label; @@ -1943,7 +1943,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) g_free (gw_key); } - num = nm_setting_ip4_config_get_num_dns (s_ip4); + num = nm_setting_ip_config_get_num_dns (s_ip4); for (i = 0; i < 254; i++) { const char *dns; @@ -1952,19 +1952,19 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) if (i >= num) svSetValue (ifcfg, addr_key, NULL, FALSE); else { - dns = nm_setting_ip4_config_get_dns (s_ip4, i); + dns = nm_setting_ip_config_get_dns (s_ip4, i); svSetValue (ifcfg, addr_key, dns, FALSE); } g_free (addr_key); } - num = nm_setting_ip4_config_get_num_dns_searches (s_ip4); + num = nm_setting_ip_config_get_num_dns_searches (s_ip4); if (num > 0) { searches = g_string_new (NULL); for (i = 0; i < num; i++) { if (i > 0) g_string_append_c (searches, ' '); - g_string_append (searches, nm_setting_ip4_config_get_dns_search (s_ip4, i)); + g_string_append (searches, nm_setting_ip_config_get_dns_search (s_ip4, i)); } svSetValue (ifcfg, "DOMAIN", searches->str, FALSE); g_string_free (searches, TRUE); @@ -1973,7 +1973,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) /* DEFROUTE; remember that it has the opposite meaning from never-default */ svSetValue (ifcfg, "DEFROUTE", - nm_setting_ip4_config_get_never_default (s_ip4) ? "no" : "yes", + nm_setting_ip_config_get_never_default (s_ip4) ? "no" : "yes", FALSE); svSetValue (ifcfg, "PEERDNS", NULL, FALSE); @@ -1981,14 +1981,14 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) svSetValue (ifcfg, "DHCP_CLIENT_ID", NULL, FALSE); if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) { svSetValue (ifcfg, "PEERDNS", - nm_setting_ip4_config_get_ignore_auto_dns (s_ip4) ? "no" : "yes", + nm_setting_ip_config_get_ignore_auto_dns (s_ip4) ? "no" : "yes", FALSE); svSetValue (ifcfg, "PEERROUTES", - nm_setting_ip4_config_get_ignore_auto_routes (s_ip4) ? "no" : "yes", + nm_setting_ip_config_get_ignore_auto_routes (s_ip4) ? "no" : "yes", FALSE); - value = nm_setting_ip4_config_get_dhcp_hostname (s_ip4); + value = nm_setting_ip_config_get_dhcp_hostname (s_ip4); if (value) svSetValue (ifcfg, "DHCP_HOSTNAME", value, FALSE); @@ -1996,16 +1996,16 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) * in that case, because it is NM-specific variable */ svSetValue (ifcfg, "DHCP_SEND_HOSTNAME", - nm_setting_ip4_config_get_dhcp_send_hostname (s_ip4) ? NULL : "no", + nm_setting_ip_config_get_dhcp_send_hostname (s_ip4) ? NULL : "no", FALSE); - value = nm_setting_ip4_config_get_dhcp_client_id (s_ip4); + value = nm_setting_ip4_config_get_dhcp_client_id (NM_SETTING_IP4_CONFIG (s_ip4)); if (value) svSetValue (ifcfg, "DHCP_CLIENT_ID", value, FALSE); } svSetValue (ifcfg, "IPV4_FAILURE_FATAL", - nm_setting_ip4_config_get_may_fail (s_ip4) ? "no" : "yes", + nm_setting_ip_config_get_may_fail (s_ip4) ? "no" : "yes", FALSE); /* Static routes - route- file */ @@ -2028,7 +2028,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) } g_free (route_path); - num = nm_setting_ip4_config_get_num_routes (s_ip4); + num = nm_setting_ip_config_get_num_routes (s_ip4); for (i = 0; i < 256; i++) { char buf[INET_ADDRSTRLEN]; NMIPRoute *route; @@ -2045,7 +2045,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) svSetValue (routefile, gw_key, NULL, FALSE); svSetValue (routefile, metric_key, NULL, FALSE); } else { - route = nm_setting_ip4_config_get_route (s_ip4, i); + route = nm_setting_ip_config_get_route (s_ip4, i); svSetValue (routefile, addr_key, nm_ip_route_get_dest (route), FALSE); @@ -2096,7 +2096,7 @@ out: static void write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) { - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *base_ifcfg_dir, *base_ifcfg_name, *base_name; int i, num, base_ifcfg_name_len, base_name_len; GDir *dir; @@ -2134,7 +2134,7 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) if (!s_ip4) return; - num = nm_setting_ip4_config_get_num_addresses (s_ip4); + num = nm_setting_ip_config_get_num_addresses (s_ip4); for (i = 0; i < num; i++) { GVariant *label_var; const char *label, *p; @@ -2142,7 +2142,7 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) NMIPAddress *addr; shvarFile *ifcfg; - addr = nm_setting_ip4_config_get_address (s_ip4, i); + addr = nm_setting_ip_config_get_address (s_ip4, i); label_var = nm_ip_address_get_attribute (addr, "label"); if (!label_var) @@ -2165,7 +2165,7 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) svSetValue (ifcfg, "DEVICE", label, FALSE); - addr = nm_setting_ip4_config_get_address (s_ip4, i); + addr = nm_setting_ip_config_get_address (s_ip4, i); svSetValue (ifcfg, "IPADDR", nm_ip_address_get_address (addr), FALSE); tmp = g_strdup_printf ("%u", nm_ip_address_get_prefix (addr)); @@ -2183,7 +2183,7 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) } static gboolean -write_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **error) +write_route6_file (const char *filename, NMSettingIPConfig *s_ip6, GError **error) { char **route_items; char *route_contents; @@ -2196,7 +2196,7 @@ write_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **err g_return_val_if_fail (error != NULL, FALSE); g_return_val_if_fail (*error == NULL, FALSE); - num = nm_setting_ip6_config_get_num_routes (s_ip6); + num = nm_setting_ip_config_get_num_routes (s_ip6); if (num == 0) { unlink (filename); return TRUE; @@ -2204,7 +2204,7 @@ write_route6_file (const char *filename, NMSettingIP6Config *s_ip6, GError **err route_items = g_malloc0 (sizeof (char*) * (num + 1)); for (i = 0; i < num; i++) { - route = nm_setting_ip6_config_get_route (s_ip6, i); + route = nm_setting_ip_config_get_route (s_ip6, i); route_items[i] = g_strdup_printf ("%s/%u via %s metric %u\n", nm_ip_route_get_dest (route), nm_ip_route_get_prefix (route), @@ -2231,8 +2231,8 @@ error: static gboolean write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) { - NMSettingIP6Config *s_ip6; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip6; + NMSettingIPConfig *s_ip4; const char *value; char *addr_key; guint32 i, num, num4; @@ -2256,7 +2256,7 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) return TRUE; } - value = nm_setting_ip6_config_get_method (s_ip6); + value = nm_setting_ip_config_get_method (s_ip6); g_assert (value); if (!strcmp (value, NM_SETTING_IP6_CONFIG_METHOD_IGNORE)) { svSetValue (ifcfg, "IPV6INIT", "no", FALSE); @@ -2271,7 +2271,7 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) svSetValue (ifcfg, "IPV6INIT", "yes", FALSE); svSetValue (ifcfg, "IPV6_AUTOCONF", "no", FALSE); svSetValue (ifcfg, "DHCPV6C", "yes", FALSE); - hostname = nm_setting_ip6_config_get_dhcp_hostname (s_ip6); + hostname = nm_setting_ip_config_get_dhcp_hostname (s_ip6); if (hostname) svSetValue (ifcfg, "DHCP_HOSTNAME", hostname, FALSE); } else if (!strcmp (value, NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) { @@ -2289,7 +2289,7 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) } /* Write out IP addresses */ - num = nm_setting_ip6_config_get_num_addresses (s_ip6); + num = nm_setting_ip_config_get_num_addresses (s_ip6); ip_str1 = g_string_new (NULL); ip_str2 = g_string_new (NULL); ipv6_defaultgw = NULL; @@ -2299,7 +2299,7 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) else ip_ptr = ip_str2; - addr = nm_setting_ip6_config_get_address (s_ip6, i); + addr = nm_setting_ip_config_get_address (s_ip6, i); if (i > 1) g_string_append_c (ip_ptr, ' '); /* separate addresses in IPV6ADDR_SECONDARIES */ @@ -2319,22 +2319,22 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) /* Write out DNS - 'DNS' key is used both for IPv4 and IPv6 */ s_ip4 = nm_connection_get_setting_ip4_config (connection); - num4 = s_ip4 ? nm_setting_ip4_config_get_num_dns (s_ip4) : 0; /* from where to start with IPv6 entries */ - num = nm_setting_ip6_config_get_num_dns (s_ip6); + num4 = s_ip4 ? nm_setting_ip_config_get_num_dns (s_ip4) : 0; /* from where to start with IPv6 entries */ + num = nm_setting_ip_config_get_num_dns (s_ip6); for (i = 0; i < 254; i++) { addr_key = g_strdup_printf ("DNS%d", i + num4 + 1); if (i >= num) svSetValue (ifcfg, addr_key, NULL, FALSE); else { - dns = nm_setting_ip6_config_get_dns (s_ip6, i); + dns = nm_setting_ip_config_get_dns (s_ip6, i); svSetValue (ifcfg, addr_key, dns, FALSE); } g_free (addr_key); } /* Write out DNS domains - 'DOMAIN' key is shared for both IPv4 and IPv6 domains */ - num = nm_setting_ip6_config_get_num_dns_searches (s_ip6); + num = nm_setting_ip_config_get_num_dns_searches (s_ip6); if (num > 0) { char *ip4_domains; ip4_domains = svGetValue (ifcfg, "DOMAIN", FALSE); @@ -2342,7 +2342,7 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) for (i = 0; i < num; i++) { if (searches->len > 0) g_string_append_c (searches, ' '); - g_string_append (searches, nm_setting_ip6_config_get_dns_search (s_ip6, i)); + g_string_append (searches, nm_setting_ip_config_get_dns_search (s_ip6, i)); } svSetValue (ifcfg, "DOMAIN", searches->str, FALSE); g_string_free (searches, TRUE); @@ -2351,7 +2351,7 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) /* handle IPV6_DEFROUTE */ /* IPV6_DEFROUTE has the opposite meaning from 'never-default' */ - if (nm_setting_ip6_config_get_never_default(s_ip6)) + if (nm_setting_ip_config_get_never_default(s_ip6)) svSetValue (ifcfg, "IPV6_DEFROUTE", "no", FALSE); else svSetValue (ifcfg, "IPV6_DEFROUTE", "yes", FALSE); @@ -2360,22 +2360,22 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) svSetValue (ifcfg, "IPV6_PEERROUTES", NULL, FALSE); if (!strcmp (value, NM_SETTING_IP6_CONFIG_METHOD_AUTO)) { svSetValue (ifcfg, "IPV6_PEERDNS", - nm_setting_ip6_config_get_ignore_auto_dns (s_ip6) ? "no" : "yes", + nm_setting_ip_config_get_ignore_auto_dns (s_ip6) ? "no" : "yes", FALSE); svSetValue (ifcfg, "IPV6_PEERROUTES", - nm_setting_ip6_config_get_ignore_auto_routes (s_ip6) ? "no" : "yes", + nm_setting_ip_config_get_ignore_auto_routes (s_ip6) ? "no" : "yes", FALSE); } svSetValue (ifcfg, "IPV6_FAILURE_FATAL", - nm_setting_ip6_config_get_may_fail (s_ip6) ? "no" : "yes", + nm_setting_ip_config_get_may_fail (s_ip6) ? "no" : "yes", FALSE); /* IPv6 Privacy Extensions */ svSetValue (ifcfg, "IPV6_PRIVACY", NULL, FALSE); svSetValue (ifcfg, "IPV6_PRIVACY_PREFER_PUBLIC_IP", NULL, FALSE); - switch (nm_setting_ip6_config_get_ip6_privacy (s_ip6)){ + switch (nm_setting_ip6_config_get_ip6_privacy (NM_SETTING_IP6_CONFIG (s_ip6))){ case NM_SETTING_IP6_CONFIG_PRIVACY_DISABLED: svSetValue (ifcfg, "IPV6_PRIVACY", "no", FALSE); break; diff --git a/src/settings/plugins/ifnet/connection_parser.c b/src/settings/plugins/ifnet/connection_parser.c index 64dcd75a77..4233c75f2f 100644 --- a/src/settings/plugins/ifnet/connection_parser.c +++ b/src/settings/plugins/ifnet/connection_parser.c @@ -544,25 +544,25 @@ make_wired_connection_setting (NMConnection *connection, nm_connection_add_setting (connection, NM_SETTING (s_wired)); } -/* add NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, - * NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID in future*/ +/* add NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, + * NM_SETTING_IP_CONFIG_DHCP_CLIENT_ID in future*/ static void make_ip4_setting (NMConnection *connection, const char *conn_name, GError **error) { - NMSettingIP4Config *ip4_setting = - NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); + NMSettingIPConfig *ip4_setting = + NM_SETTING_IP_CONFIG (nm_setting_ip4_config_new ()); const char *value, *method = NULL; gboolean is_static_block = is_static_ip4 (conn_name); ip_block *iblock = NULL; /* set dhcp options (dhcp_xxx) */ value = ifnet_get_data (conn_name, "dhcp"); - g_object_set (ip4_setting, NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, value + g_object_set (ip4_setting, NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, value && strstr (value, "nodns") ? TRUE : FALSE, - NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, value + NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, value && strstr (value, "nogateway") ? TRUE : FALSE, NULL); if (!is_static_block) { @@ -575,21 +575,21 @@ make_ip4_setting (NMConnection *connection, } if (strstr (method, "dhcp")) g_object_set (ip4_setting, - NM_SETTING_IP4_CONFIG_METHOD, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, - NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL); + NM_SETTING_IP_CONFIG_NEVER_DEFAULT, FALSE, NULL); else if (strstr (method, "autoip")) { g_object_set (ip4_setting, - NM_SETTING_IP4_CONFIG_METHOD, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL, - NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL); + NM_SETTING_IP_CONFIG_NEVER_DEFAULT, FALSE, NULL); nm_connection_add_setting (connection, NM_SETTING (ip4_setting)); return; } else if (strstr (method, "shared")) { g_object_set (ip4_setting, - NM_SETTING_IP4_CONFIG_METHOD, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_SHARED, - NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, FALSE, NULL); + NM_SETTING_IP_CONFIG_NEVER_DEFAULT, FALSE, NULL); nm_connection_add_setting (connection, NM_SETTING (ip4_setting)); return; } else { @@ -618,11 +618,11 @@ make_ip4_setting (NMConnection *connection, ip4_addr = nm_ip_address_new (AF_INET, iblock->ip, iblock->prefix, iblock->next_hop, &local); if (iblock->next_hop) g_object_set (ip4_setting, - NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, + NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, TRUE, NULL); if (ip4_addr) { - if (!nm_setting_ip4_config_add_address (ip4_setting, ip4_addr)) + if (!nm_setting_ip_config_add_address (ip4_setting, ip4_addr)) nm_log_warn (LOGD_SETTINGS, "ignoring duplicate IP4 address"); nm_ip_address_unref (ip4_addr); } else { @@ -636,8 +636,8 @@ make_ip4_setting (NMConnection *connection, } g_object_set (ip4_setting, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, - NM_SETTING_IP4_CONFIG_NEVER_DEFAULT, !has_default_ip4_route (conn_name), + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_NEVER_DEFAULT, !has_default_ip4_route (conn_name), NULL); } @@ -648,7 +648,7 @@ make_ip4_setting (NMConnection *connection, get_dhcp_hostname_and_client_id (&dhcp_hostname, &client_id); if (dhcp_hostname) { g_object_set (ip4_setting, - NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, + NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, dhcp_hostname, NULL); nm_log_info (LOGD_SETTINGS, "DHCP hostname: %s", dhcp_hostname); g_free (dhcp_hostname); @@ -679,7 +679,7 @@ make_ip4_setting (NMConnection *connection, for (item = searches; *item; item++) { if (strlen (*item)) { - if (!nm_setting_ip4_config_add_dns_search (ip4_setting, *item)) + if (!nm_setting_ip_config_add_dns_search (ip4_setting, *item)) nm_log_warn (LOGD_SETTINGS, " duplicate DNS domain '%s'", *item); } } @@ -711,9 +711,8 @@ make_ip4_setting (NMConnection *connection, } route = nm_ip_route_new (AF_INET, iblock->ip, iblock->prefix, iblock->next_hop, metric, &local); - if (route) { - if (nm_setting_ip4_config_add_route (ip4_setting, route)) + if (nm_setting_ip_config_add_route (ip4_setting, route)) nm_log_info (LOGD_SETTINGS, "new IP4 route:%s\n", iblock->ip); else nm_log_warn (LOGD_SETTINGS, "duplicate IP4 route"); @@ -737,7 +736,7 @@ make_ip6_setting (NMConnection *connection, const char *conn_name, GError **error) { - NMSettingIP6Config *s_ip6 = NULL; + NMSettingIPConfig *s_ip6 = NULL; gboolean is_static_block = is_static_ip6 (conn_name); // used to disable IPv6 @@ -747,7 +746,7 @@ make_ip6_setting (NMConnection *connection, ip_block *iblock; gboolean never_default = !has_default_ip6_route (conn_name); - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); value = ifnet_get_data (conn_name, "enable_ipv6"); if (value && is_true (value)) @@ -757,7 +756,7 @@ make_ip6_setting (NMConnection *connection, // Currently only Manual and DHCP are supported if (!ipv6_enabled) { g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL); goto done; } else if (!is_static_block) { @@ -774,10 +773,10 @@ make_ip6_setting (NMConnection *connection, nm_log_info (LOGD_SETTINGS, "IPv6 for %s enabled, using %s", conn_name, method); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, method, - NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS, FALSE, - NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES, FALSE, - NM_SETTING_IP6_CONFIG_NEVER_DEFAULT, never_default, NULL); + NM_SETTING_IP_CONFIG_METHOD, method, + NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, FALSE, + NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, FALSE, + NM_SETTING_IP_CONFIG_NEVER_DEFAULT, never_default, NULL); /* Make manual settings */ if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_MANUAL)) { @@ -797,9 +796,9 @@ make_ip6_setting (NMConnection *connection, ip6_addr = nm_ip_address_new (AF_INET6, iblock->ip, iblock->prefix, NULL, &local); if (ip6_addr) { - if (nm_setting_ip6_config_add_address (s_ip6, ip6_addr)) { + if (nm_setting_ip_config_add_address (s_ip6, ip6_addr)) { nm_log_info (LOGD_SETTINGS, "ipv6 addresses count: %d", - nm_setting_ip6_config_get_num_addresses (s_ip6)); + nm_setting_ip_config_get_num_addresses (s_ip6)); } else { nm_log_warn (LOGD_SETTINGS, "ignoring duplicate IP6 address"); } @@ -817,15 +816,15 @@ make_ip6_setting (NMConnection *connection, } else if (!strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO)) { /* - autoconf or DHCPv6 stuff goes here */ } - // DNS Servers, set NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS TRUE here + // DNS Servers, set NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS TRUE here set_ip6_dns_servers (s_ip6, conn_name); - /* DNS searches ('DOMAIN' key) are read by make_ip4_setting() and included in NMSettingIP4Config */ + /* DNS searches ('DOMAIN' key) are read by make_ip4_setting() and included in NMSettingIPConfig */ // Add routes iblock = convert_ip6_routes_block (conn_name); if (iblock) - g_object_set (s_ip6, NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES, + g_object_set (s_ip6, NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, TRUE, NULL); /* Add all IPv6 routes */ while (iblock) { @@ -855,7 +854,7 @@ make_ip6_setting (NMConnection *connection, route = nm_ip_route_new (AF_INET6, iblock->ip, iblock->prefix, iblock->next_hop, metric, &local); if (route) { - if (nm_setting_ip6_config_add_route (s_ip6, route)) + if (nm_setting_ip_config_add_route (s_ip6, route)) nm_log_info (LOGD_SETTINGS, " new IP6 route"); else nm_log_warn (LOGD_SETTINGS, " duplicate IP6 route"); @@ -2373,7 +2372,7 @@ write_wired_setting (NMConnection *connection, static gboolean write_ip4_setting (NMConnection *connection, const char *conn_name, GError **error) { - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; const char *value; guint32 i, num; GString *searches; @@ -2392,17 +2391,17 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err } routes = g_string_new (NULL); - value = nm_setting_ip4_config_get_method (s_ip4); + value = nm_setting_ip_config_get_method (s_ip4); g_assert (value); if (!strcmp (value, NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) { - num = nm_setting_ip4_config_get_num_addresses (s_ip4); + num = nm_setting_ip_config_get_num_addresses (s_ip4); ips = g_string_new (NULL); /* IPv4 addresses */ for (i = 0; i < num; i++) { NMIPAddress *addr; - addr = nm_setting_ip4_config_get_address (s_ip4, i); + addr = nm_setting_ip_config_get_address (s_ip4, i); g_string_append_printf (ips, "\"%s/%u", nm_ip_address_get_address (addr), @@ -2426,13 +2425,13 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err ifnet_set_data (conn_name, "config", "dhcp"); /* DNS Servers */ - num = nm_setting_ip4_config_get_num_dns (s_ip4); + num = nm_setting_ip_config_get_num_dns (s_ip4); if (num > 0) { dns = g_string_new (NULL); for (i = 0; i < num; i++) { const char *ip; - ip = nm_setting_ip4_config_get_dns (s_ip4, i); + ip = nm_setting_ip_config_get_dns (s_ip4, i); g_string_append_printf (dns, " %s", ip); } ifnet_set_data (conn_name, "dns_servers", dns->str); @@ -2441,14 +2440,14 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err ifnet_set_data (conn_name, "dns_servers", NULL); /* DNS Searches */ - num = nm_setting_ip4_config_get_num_dns_searches (s_ip4); + num = nm_setting_ip_config_get_num_dns_searches (s_ip4); if (num > 0) { searches = g_string_new (NULL); for (i = 0; i < num; i++) { if (i > 0) g_string_append_c (searches, ' '); g_string_append (searches, - nm_setting_ip4_config_get_dns_search + nm_setting_ip_config_get_dns_search (s_ip4, i)); } ifnet_set_data (conn_name, "dns_search", searches->str); @@ -2457,12 +2456,12 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err ifnet_set_data (conn_name, "dns_search", NULL); /* FIXME Will be implemented when configuration supports it if (!strcmp(value, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) { - value = nm_setting_ip4_config_get_dhcp_hostname(s_ip4); + value = nm_setting_ip_config_get_dhcp_hostname(s_ip4); if (value) ifnet_set_data(conn_name, "DHCP_HOSTNAME", value, FALSE); - value = nm_setting_ip4_config_get_dhcp_client_id(s_ip4); + value = nm_setting_ip_config_get_dhcp_client_id(s_ip4); if (value) ifnet_set_data(conn_name, "DHCP_CLIENT_ID", value, FALSE); @@ -2470,13 +2469,13 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err */ /* Static routes */ - num = nm_setting_ip4_config_get_num_routes (s_ip4); + num = nm_setting_ip_config_get_num_routes (s_ip4); if (num > 0) { for (i = 0; i < num; i++) { NMIPRoute *route; const char *next_hop; - route = nm_setting_ip4_config_get_route (s_ip4, i); + route = nm_setting_ip_config_get_route (s_ip4, i); next_hop = nm_ip_route_get_next_hop (route); if (!next_hop) @@ -2500,7 +2499,7 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err } static gboolean -write_route6_file (NMSettingIP6Config *s_ip6, const char *conn_name, GError **error) +write_route6_file (NMSettingIPConfig *s_ip6, const char *conn_name, GError **error) { NMIPRoute *route; const char *next_hop; @@ -2509,7 +2508,7 @@ write_route6_file (NMSettingIP6Config *s_ip6, const char *conn_name, GError **er const char *old_routes; g_return_val_if_fail (s_ip6 != NULL, FALSE); - num = nm_setting_ip6_config_get_num_routes (s_ip6); + num = nm_setting_ip_config_get_num_routes (s_ip6); if (num == 0) { return TRUE; } @@ -2519,7 +2518,7 @@ write_route6_file (NMSettingIP6Config *s_ip6, const char *conn_name, GError **er if (old_routes) g_string_append (routes_string, "\" "); for (i = 0; i < num; i++) { - route = nm_setting_ip6_config_get_route (s_ip6, i); + route = nm_setting_ip_config_get_route (s_ip6, i); next_hop = nm_ip_route_get_next_hop (route); if (!next_hop) @@ -2540,7 +2539,7 @@ write_route6_file (NMSettingIP6Config *s_ip6, const char *conn_name, GError **er static gboolean write_ip6_setting (NMConnection *connection, const char *conn_name, GError **error) { - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; const char *value; guint32 i, num; GString *searches; @@ -2554,7 +2553,7 @@ write_ip6_setting (NMConnection *connection, const char *conn_name, GError **err return FALSE; } - value = nm_setting_ip6_config_get_method (s_ip6); + value = nm_setting_ip_config_get_method (s_ip6); g_assert (value); if (!strcmp (value, NM_SETTING_IP6_CONFIG_METHOD_IGNORE)) { ifnet_set_data (conn_name, "enable_ipv6", "false"); @@ -2588,12 +2587,12 @@ write_ip6_setting (NMConnection *connection, const char *conn_name, GError **err if (!config) config = ""; - num = nm_setting_ip6_config_get_num_addresses (s_ip6); + num = nm_setting_ip_config_get_num_addresses (s_ip6); /* IPv6 addresses */ ip_str = g_string_new (NULL); for (i = 0; i < num; i++) { - addr = nm_setting_ip6_config_get_address (s_ip6, i); + addr = nm_setting_ip_config_get_address (s_ip6, i); g_string_append_printf (ip_str, "\"%s/%u\"", nm_ip_address_get_address (addr), @@ -2606,7 +2605,7 @@ write_ip6_setting (NMConnection *connection, const char *conn_name, GError **err } /* DNS Servers */ - num = nm_setting_ip6_config_get_num_dns (s_ip6); + num = nm_setting_ip_config_get_num_dns (s_ip6); if (num > 0) { const char *dns_servers = ifnet_get_data (conn_name, "dns_servers"); gchar *tmp; @@ -2616,7 +2615,7 @@ write_ip6_setting (NMConnection *connection, const char *conn_name, GError **err if (!dns_servers) dns_servers = ""; for (i = 0; i < num; i++) { - dns = nm_setting_ip6_config_get_dns (s_ip6, i); + dns = nm_setting_ip_config_get_dns (s_ip6, i); if (!strstr (dns_servers, dns)) g_string_append_printf (dns_string, "%s ", dns); @@ -2628,7 +2627,7 @@ write_ip6_setting (NMConnection *connection, const char *conn_name, GError **err } else /* DNS Searches */ - num = nm_setting_ip6_config_get_num_dns_searches (s_ip6); + num = nm_setting_ip_config_get_num_dns_searches (s_ip6); if (num > 0) { const char *ip4_domains; @@ -2640,7 +2639,7 @@ write_ip6_setting (NMConnection *connection, const char *conn_name, GError **err const gchar *search = NULL; search = - nm_setting_ip6_config_get_dns_search (s_ip6, i); + nm_setting_ip_config_get_dns_search (s_ip6, i); if (search && !strstr (searches->str, search)) { if (searches->len > 0) g_string_append_c (searches, ' '); @@ -2686,7 +2685,7 @@ ifnet_update_parsers_by_connection (NMConnection *connection, GError **error) { NMSettingConnection *s_con; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; gboolean success = FALSE; const char *type; gboolean no_8021x = FALSE; diff --git a/src/settings/plugins/ifnet/net_utils.c b/src/settings/plugins/ifnet/net_utils.c index 9ead29f6e2..c4302418ec 100644 --- a/src/settings/plugins/ifnet/net_utils.c +++ b/src/settings/plugins/ifnet/net_utils.c @@ -651,7 +651,7 @@ destroy_ip_block (ip_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; @@ -668,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]); @@ -679,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; @@ -704,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]); @@ -715,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); diff --git a/src/settings/plugins/ifnet/net_utils.h b/src/settings/plugins/ifnet/net_utils.h index 83272e8956..d58e7ec616 100644 --- a/src/settings/plugins/ifnet/net_utils.h +++ b/src/settings/plugins/ifnet/net_utils.h @@ -53,8 +53,8 @@ ip_block *convert_ip4_routes_block (const char *conn_name); ip_block *convert_ip6_routes_block (const char *conn_name); void destroy_ip_block (ip_block * iblock); -void set_ip4_dns_servers (NMSettingIP4Config * s_ip4, const char *conn_name); -void set_ip6_dns_servers (NMSettingIP6Config * s_ip6, const char *conn_name); +void set_ip4_dns_servers (NMSettingIPConfig * s_ip4, const char *conn_name); +void set_ip6_dns_servers (NMSettingIPConfig * s_ip6, const char *conn_name); gchar *strip_string (gchar *str, gchar t); gboolean is_managed (const char *conn_name); diff --git a/src/settings/plugins/ifupdown/parser.c b/src/settings/plugins/ifupdown/parser.c index 72340a90eb..f916fa0e78 100644 --- a/src/settings/plugins/ifupdown/parser.c +++ b/src/settings/plugins/ifupdown/parser.c @@ -410,7 +410,7 @@ update_wired_setting_from_if_block(NMConnection *connection, } static void -ifupdown_ip4_add_dns (NMSettingIP4Config *s_ip4, const char *dns) +ifupdown_ip4_add_dns (NMSettingIPConfig *s_ip4, const char *dns) { guint32 addr; char **list, **iter; @@ -428,7 +428,7 @@ ifupdown_ip4_add_dns (NMSettingIP4Config *s_ip4, const char *dns) continue; } - if (!nm_setting_ip4_config_add_dns (s_ip4, *iter)) + if (!nm_setting_ip_config_add_dns (s_ip4, *iter)) nm_log_warn (LOGD_SETTINGS, " duplicate DNS domain '%s'", *iter); } g_strfreev (list); @@ -440,12 +440,12 @@ update_ip4_setting_from_if_block(NMConnection *connection, GError **error) { - NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new()); + NMSettingIPConfig *s_ip4 = NM_SETTING_IP_CONFIG (nm_setting_ip4_config_new()); const char *type = ifparser_getkey(block, "inet"); gboolean is_static = type && !strcmp("static", type); if (!is_static) { - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); } else { guint32 tmp_mask; NMIPAddress *addr; @@ -490,9 +490,9 @@ update_ip4_setting_from_if_block(NMConnection *connection, if (!addr) goto error; - if (nm_setting_ip4_config_add_address (s_ip4, addr)) { + if (nm_setting_ip_config_add_address (s_ip4, addr)) { nm_log_info (LOGD_SETTINGS, "addresses count: %d", - nm_setting_ip4_config_get_num_addresses (s_ip4)); + nm_setting_ip_config_get_num_addresses (s_ip4)); } else { nm_log_info (LOGD_SETTINGS, "ignoring duplicate IP4 address"); } @@ -504,7 +504,7 @@ update_ip4_setting_from_if_block(NMConnection *connection, nameservers_v = ifparser_getkey (block, "dns-nameservers"); ifupdown_ip4_add_dns (s_ip4, nameservers_v); - if (!nm_setting_ip4_config_get_num_dns (s_ip4)) + if (!nm_setting_ip_config_get_num_dns (s_ip4)) nm_log_info (LOGD_SETTINGS, "No dns-nameserver configured in /etc/network/interfaces"); /* DNS searches */ @@ -515,13 +515,13 @@ update_ip4_setting_from_if_block(NMConnection *connection, g_strstrip (*iter); if (g_ascii_isspace (*iter[0])) continue; - if (!nm_setting_ip4_config_add_dns_search (s_ip4, *iter)) + if (!nm_setting_ip_config_add_dns_search (s_ip4, *iter)) nm_log_warn (LOGD_SETTINGS, " duplicate DNS domain '%s'", *iter); } g_strfreev (list); } - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); } nm_connection_add_setting (connection, NM_SETTING (s_ip4)); @@ -533,7 +533,7 @@ error: } static void -ifupdown_ip6_add_dns (NMSettingIP6Config *s_ip6, const char *dns) +ifupdown_ip6_add_dns (NMSettingIPConfig *s_ip6, const char *dns) { struct in6_addr addr; char **list, **iter; @@ -551,7 +551,7 @@ ifupdown_ip6_add_dns (NMSettingIP6Config *s_ip6, const char *dns) continue; } - if (!nm_setting_ip6_config_add_dns (s_ip6, *iter)) + if (!nm_setting_ip_config_add_dns (s_ip6, *iter)) nm_log_warn (LOGD_SETTINGS, " duplicate DNS domain '%s'", *iter); } g_strfreev (list); @@ -562,13 +562,13 @@ update_ip6_setting_from_if_block(NMConnection *connection, if_block *block, GError **error) { - NMSettingIP6Config *s_ip6 = NM_SETTING_IP6_CONFIG (nm_setting_ip6_config_new()); + NMSettingIPConfig *s_ip6 = NM_SETTING_IP_CONFIG (nm_setting_ip6_config_new()); const char *type = ifparser_getkey(block, "inet6"); gboolean is_static = type && (!strcmp("static", type) || !strcmp("v4tunnel", type)); if (!is_static) { - g_object_set(s_ip6, NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); + g_object_set(s_ip6, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); } else { NMIPAddress *addr; const char *address_v; @@ -603,9 +603,9 @@ update_ip6_setting_from_if_block(NMConnection *connection, if (!addr) goto error; - if (nm_setting_ip6_config_add_address (s_ip6, addr)) { + if (nm_setting_ip_config_add_address (s_ip6, addr)) { nm_log_info (LOGD_SETTINGS, "addresses count: %d", - nm_setting_ip6_config_get_num_addresses (s_ip6)); + nm_setting_ip_config_get_num_addresses (s_ip6)); } else { nm_log_info (LOGD_SETTINGS, "ignoring duplicate IP6 address"); } @@ -617,7 +617,7 @@ update_ip6_setting_from_if_block(NMConnection *connection, nameservers_v = ifparser_getkey(block, "dns-nameservers"); ifupdown_ip6_add_dns (s_ip6, nameservers_v); - if (!nm_setting_ip6_config_get_num_dns (s_ip6)) + if (!nm_setting_ip_config_get_num_dns (s_ip6)) nm_log_info (LOGD_SETTINGS, "No dns-nameserver configured in /etc/network/interfaces"); /* DNS searches */ @@ -628,14 +628,14 @@ update_ip6_setting_from_if_block(NMConnection *connection, g_strstrip (*iter); if (isblank (*iter[0])) continue; - if (!nm_setting_ip6_config_add_dns_search (s_ip6, *iter)) + if (!nm_setting_ip_config_add_dns_search (s_ip6, *iter)) nm_log_warn (LOGD_SETTINGS, " duplicate DNS domain '%s'", *iter); } g_strfreev (list); } g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, NULL); } diff --git a/src/settings/plugins/ifupdown/tests/test-ifupdown.c b/src/settings/plugins/ifupdown/tests/test-ifupdown.c index 643fa7d08a..0752ec4bbc 100644 --- a/src/settings/plugins/ifupdown/tests/test-ifupdown.c +++ b/src/settings/plugins/ifupdown/tests/test-ifupdown.c @@ -459,7 +459,7 @@ test17_read_static_ipv4 (const char *path) { NMConnection *connection; NMSettingConnection *s_con; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSettingWired *s_wired; char *unmanaged = NULL; GError *error = NULL; @@ -525,81 +525,81 @@ test17_read_static_ipv4 (const char *path) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_MANUAL) == 0, TEST17_NAME, "failed to verify %s: unexpected %s / %s key value", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* IP addresses */ - ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == 1, + ASSERT (nm_setting_ip_config_get_num_addresses (s_ip4) == 1, TEST17_NAME, "failed to verify %s: unexpected %s / %s key value", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_ADDRESSES); - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert (ip4_addr != NULL); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "10.0.0.3"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 8); /* DNS Addresses */ - ASSERT (nm_setting_ip4_config_get_num_dns (s_ip4) == 2, + ASSERT (nm_setting_ip_config_get_num_dns (s_ip4) == 2, TEST17_NAME, "failed to verify %s: unexpected %s / %s key value", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (!strcmp (nm_setting_ip4_config_get_dns (s_ip4, 0), "10.0.0.1"), + ASSERT (!strcmp (nm_setting_ip_config_get_dns (s_ip4, 0), "10.0.0.1"), TEST17_NAME, "failed to verify %s: unexpected %s / %s key value #1", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (!strcmp (nm_setting_ip4_config_get_dns (s_ip4, 1), "10.0.0.2"), + ASSERT (!strcmp (nm_setting_ip_config_get_dns (s_ip4, 1), "10.0.0.2"), TEST17_NAME, "failed to verify %s: unexpected %s / %s key value #2", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == 1, + ASSERT (nm_setting_ip_config_get_num_addresses (s_ip4) == 1, TEST17_NAME, "failed to verify %s: unexpected %s / %s key value", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); /* DNS search domains */ - ASSERT (nm_setting_ip4_config_get_num_dns_searches (s_ip4) == 2, + ASSERT (nm_setting_ip_config_get_num_dns_searches (s_ip4) == 2, TEST17_NAME, "failed to verify %s: unexpected %s / %s key value", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - tmp = nm_setting_ip4_config_get_dns_search (s_ip4, 0); + tmp = nm_setting_ip_config_get_dns_search (s_ip4, 0); ASSERT (tmp != NULL, TEST17_NAME, "failed to verify %s: missing %s / %s key", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); ASSERT (strcmp (tmp, expected_search1) == 0, TEST17_NAME, "failed to verify %s: unexpected %s / %s key value", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); - tmp = nm_setting_ip4_config_get_dns_search (s_ip4, 1); + tmp = nm_setting_ip_config_get_dns_search (s_ip4, 1); ASSERT (tmp != NULL, TEST17_NAME, "failed to verify %s: missing %s / %s key", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); ASSERT (strcmp (tmp, expected_search2) == 0, TEST17_NAME, "failed to verify %s: unexpected %s / %s key value", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); g_object_unref (connection); } @@ -609,7 +609,7 @@ test18_read_static_ipv6 (const char *path) { NMConnection *connection; NMSettingConnection *s_con; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; NMSettingWired *s_wired; char *unmanaged = NULL; GError *error = NULL; @@ -681,85 +681,85 @@ test18_read_static_ipv6 (const char *path) NM_SETTING_IP6_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip6_config_get_method (s_ip6); + tmp = nm_setting_ip_config_get_method (s_ip6); ASSERT (strcmp (tmp, NM_SETTING_IP6_CONFIG_METHOD_MANUAL) == 0, TEST18_NAME, "failed to verify %s: unexpected %s / %s key value", file, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* IP addresses */ - ASSERT (nm_setting_ip6_config_get_num_addresses (s_ip6) == 1, + ASSERT (nm_setting_ip_config_get_num_addresses (s_ip6) == 1, TEST18_NAME, "failed to verify %s: unexpected number of %s / %s", file, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_ADDRESSES); - ip6_addr = nm_setting_ip6_config_get_address (s_ip6, 0); + ip6_addr = nm_setting_ip_config_get_address (s_ip6, 0); g_assert (ip6_addr != NULL); g_assert_cmpstr (nm_ip_address_get_address (ip6_addr), ==, "fc00::1"); g_assert_cmpint (nm_ip_address_get_prefix (ip6_addr), ==, 64); /* DNS Addresses */ - ASSERT (nm_setting_ip6_config_get_num_dns (s_ip6) == 2, + ASSERT (nm_setting_ip_config_get_num_dns (s_ip6) == 2, TEST18_NAME, "failed to verify %s: unexpected number of %s / %s values", file, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (!strcmp (nm_setting_ip6_config_get_dns (s_ip6, 0), "fc00::2"), + ASSERT (!strcmp (nm_setting_ip_config_get_dns (s_ip6, 0), "fc00::2"), TEST18_NAME, "failed to verify %s: unexpected %s / %s #1", file, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (!strcmp (nm_setting_ip6_config_get_dns (s_ip6, 1), "fc00::3"), + ASSERT (!strcmp (nm_setting_ip_config_get_dns (s_ip6, 1), "fc00::3"), TEST18_NAME, "failed to verify %s: unexpected %s / %s #2", file, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); /* DNS search domains */ - ASSERT (nm_setting_ip6_config_get_num_dns_searches (s_ip6) == 2, + ASSERT (nm_setting_ip_config_get_num_dns_searches (s_ip6) == 2, TEST18_NAME, "failed to verify %s: unexpected number of %s / %s values", file, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); - tmp = nm_setting_ip6_config_get_dns_search (s_ip6, 0); + tmp = nm_setting_ip_config_get_dns_search (s_ip6, 0); ASSERT (tmp != NULL, "wired-ipv6-manual-verify-ip6", "failed to verify %s: missing %s / %s #1", file, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); ASSERT (strcmp (tmp, expected_search1) == 0, "wired-ipv6-manual-verify-ip6", "failed to verify %s: unexpected %s / %s #1", file, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); - tmp = nm_setting_ip6_config_get_dns_search (s_ip6, 1); + tmp = nm_setting_ip_config_get_dns_search (s_ip6, 1); ASSERT (tmp != NULL, TEST18_NAME, "failed to verify %s: missing %s / %s #2", file, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); ASSERT (strcmp (tmp, expected_search2) == 0, TEST18_NAME, "failed to verify %s: unexpected %s / %s #2", file, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); g_free (unmanaged); g_object_unref (connection); @@ -769,7 +769,7 @@ static void test19_read_static_ipv4_plen (const char *path) { NMConnection *connection; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *unmanaged = NULL; GError *error = NULL; NMIPAddress *ip4_addr; @@ -801,13 +801,13 @@ test19_read_static_ipv4_plen (const char *path) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* IP addresses */ - ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == 1, + ASSERT (nm_setting_ip_config_get_num_addresses (s_ip4) == 1, TEST19_NAME, "failed to verify %s: unexpected %s / %s key value", file, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ADDRESSES); + NM_SETTING_IP_CONFIG_ADDRESSES); - ip4_addr = nm_setting_ip4_config_get_address (s_ip4, 0); + ip4_addr = nm_setting_ip_config_get_address (s_ip4, 0); g_assert (ip4_addr != NULL); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "10.0.0.3"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 8); diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c index 97ccf7d89b..526831a32e 100644 --- a/src/settings/plugins/keyfile/reader.c +++ b/src/settings/plugins/keyfile/reader.c @@ -879,27 +879,27 @@ static KeyParser key_parsers[] = { TRUE, mac_address_parser_ETHER }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ADDRESSES, + NM_SETTING_IP_CONFIG_ADDRESSES, FALSE, ip_address_or_route_parser }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ADDRESSES, + NM_SETTING_IP_CONFIG_ADDRESSES, FALSE, ip_address_or_route_parser }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES, + NM_SETTING_IP_CONFIG_ROUTES, FALSE, ip_address_or_route_parser }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ROUTES, + NM_SETTING_IP_CONFIG_ROUTES, FALSE, ip_address_or_route_parser }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS, + NM_SETTING_IP_CONFIG_DNS, FALSE, ip4_dns_parser }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS, + NM_SETTING_IP_CONFIG_DNS, FALSE, ip6_dns_parser }, { NM_SETTING_WIRED_SETTING_NAME, diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index 0e05fed45b..89504b38a6 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -38,9 +38,9 @@ #define TEST_WIRELESS_FILE TEST_KEYFILES_DIR"/Test_Wireless_Connection" static void -check_ip4_address (NMSettingIP4Config *config, int idx, const char *address, int plen, const char *gateway) +check_ip_address (NMSettingIPConfig *config, int idx, const char *address, int plen, const char *gateway) { - NMIPAddress *ip4 = nm_setting_ip4_config_get_address (config, idx); + NMIPAddress *ip4 = nm_setting_ip_config_get_address (config, idx); g_assert (ip4); g_assert_cmpstr (nm_ip_address_get_address (ip4), ==, address); @@ -49,34 +49,10 @@ check_ip4_address (NMSettingIP4Config *config, int idx, const char *address, int } static void -check_ip6_address (NMSettingIP6Config *config, int idx, const char *address, int plen, const char *gateway) +check_ip_route (NMSettingIPConfig *config, int idx, const char *destination, int plen, + const char *next_hop, int metric) { - NMIPAddress *ip6 = nm_setting_ip6_config_get_address (config, idx); - - g_assert (ip6); - g_assert_cmpstr (nm_ip_address_get_address (ip6), ==, address); - g_assert_cmpint (nm_ip_address_get_prefix (ip6), ==, plen); - g_assert_cmpstr (nm_ip_address_get_gateway (ip6), ==, gateway); -} - -static void -check_ip4_route (NMSettingIP4Config *config, int idx, const char *destination, int plen, - const char *next_hop, int metric) -{ - NMIPRoute *route = nm_setting_ip4_config_get_route (config, idx); - - g_assert (route); - g_assert_cmpstr (nm_ip_route_get_dest (route), ==, destination); - g_assert_cmpint (nm_ip_route_get_prefix (route), ==, plen); - g_assert_cmpstr (nm_ip_route_get_next_hop (route), ==, next_hop); - g_assert_cmpint (nm_ip_route_get_metric (route), ==, metric); -} - -static void -check_ip6_route (NMSettingIP6Config *config, int idx, const char *destination, int plen, - const char *next_hop, int metric) -{ - NMIPRoute *route = nm_setting_ip6_config_get_route (config, idx); + NMIPRoute *route = nm_setting_ip_config_get_route (config, idx); g_assert (route); g_assert_cmpstr (nm_ip_route_get_dest (route), ==, destination); @@ -107,8 +83,8 @@ test_read_valid_wired_connection (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; GError *error = NULL; const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; @@ -242,55 +218,55 @@ test_read_valid_wired_connection (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_MANUAL) == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", TEST_WIRED_FILE, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* DNS Addresses */ - ASSERT (nm_setting_ip4_config_get_num_dns (s_ip4) == 2, + ASSERT (nm_setting_ip_config_get_num_dns (s_ip4) == 2, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", TEST_WIRED_FILE, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (strcmp (nm_setting_ip4_config_get_dns (s_ip4, 0), "4.2.2.1") == 0, + ASSERT (strcmp (nm_setting_ip_config_get_dns (s_ip4, 0), "4.2.2.1") == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value #1", TEST_WIRED_FILE, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (strcmp (nm_setting_ip4_config_get_dns (s_ip4, 1), "4.2.2.2") == 0, + ASSERT (strcmp (nm_setting_ip_config_get_dns (s_ip4, 1), "4.2.2.2") == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value #2", TEST_WIRED_FILE, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); /* IPv4 addresses */ - g_assert (nm_setting_ip4_config_get_num_addresses (s_ip4) == 6); - check_ip4_address (s_ip4, 0, "2.3.4.5", 24, "2.3.4.6"); - check_ip4_address (s_ip4, 1, "192.168.0.5", 24, "192.168.0.1"); - check_ip4_address (s_ip4, 2, "1.2.3.4", 16, "1.2.1.1"); - check_ip4_address (s_ip4, 3, "3.4.5.6", 16, NULL); - check_ip4_address (s_ip4, 4, "4.5.6.7", 24, "1.2.3.4"); - check_ip4_address (s_ip4, 5, "5.6.7.8", 24, NULL); + g_assert (nm_setting_ip_config_get_num_addresses (s_ip4) == 6); + check_ip_address (s_ip4, 0, "2.3.4.5", 24, "2.3.4.6"); + check_ip_address (s_ip4, 1, "192.168.0.5", 24, "192.168.0.1"); + check_ip_address (s_ip4, 2, "1.2.3.4", 16, "1.2.1.1"); + check_ip_address (s_ip4, 3, "3.4.5.6", 16, NULL); + check_ip_address (s_ip4, 4, "4.5.6.7", 24, "1.2.3.4"); + check_ip_address (s_ip4, 5, "5.6.7.8", 24, NULL); /* IPv4 routes */ - g_assert (nm_setting_ip4_config_get_num_routes (s_ip4) == 12); - check_ip4_route (s_ip4, 0, "5.6.7.8", 32, NULL, 0); - check_ip4_route (s_ip4, 1, "1.2.3.0", 24, "2.3.4.8", 99); - check_ip4_route (s_ip4, 2, "1.1.1.2", 12, NULL, 0); - check_ip4_route (s_ip4, 3, "1.1.1.3", 13, NULL, 0); - check_ip4_route (s_ip4, 4, "1.1.1.4", 14, "2.2.2.4", 0); - check_ip4_route (s_ip4, 5, "1.1.1.5", 15, "2.2.2.5", 0); - check_ip4_route (s_ip4, 6, "1.1.1.6", 16, "2.2.2.6", 0); - check_ip4_route (s_ip4, 7, "1.1.1.7", 17, NULL, 0); - check_ip4_route (s_ip4, 8, "1.1.1.8", 18, NULL, 0); - check_ip4_route (s_ip4, 9, "1.1.1.9", 19, NULL, 0); - check_ip4_route (s_ip4, 10, "1.1.1.10", 20, NULL, 0); - check_ip4_route (s_ip4, 11, "1.1.1.11", 21, NULL, 21); + g_assert (nm_setting_ip_config_get_num_routes (s_ip4) == 12); + check_ip_route (s_ip4, 0, "5.6.7.8", 32, NULL, 0); + check_ip_route (s_ip4, 1, "1.2.3.0", 24, "2.3.4.8", 99); + check_ip_route (s_ip4, 2, "1.1.1.2", 12, NULL, 0); + check_ip_route (s_ip4, 3, "1.1.1.3", 13, NULL, 0); + check_ip_route (s_ip4, 4, "1.1.1.4", 14, "2.2.2.4", 0); + check_ip_route (s_ip4, 5, "1.1.1.5", 15, "2.2.2.5", 0); + check_ip_route (s_ip4, 6, "1.1.1.6", 16, "2.2.2.6", 0); + check_ip_route (s_ip4, 7, "1.1.1.7", 17, NULL, 0); + check_ip_route (s_ip4, 8, "1.1.1.8", 18, NULL, 0); + check_ip_route (s_ip4, 9, "1.1.1.9", 19, NULL, 0); + check_ip_route (s_ip4, 10, "1.1.1.10", 20, NULL, 0); + check_ip_route (s_ip4, 11, "1.1.1.11", 21, NULL, 21); /* ===== IPv6 SETTING ===== */ @@ -301,139 +277,110 @@ test_read_valid_wired_connection (void) NM_SETTING_IP6_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip6_config_get_method (s_ip6); + tmp = nm_setting_ip_config_get_method (s_ip6); ASSERT (strcmp (tmp, NM_SETTING_IP6_CONFIG_METHOD_MANUAL) == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", TEST_WIRED_FILE, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* DNS Addresses */ - ASSERT (nm_setting_ip6_config_get_num_dns (s_ip6) == 2, + ASSERT (nm_setting_ip_config_get_num_dns (s_ip6) == 2, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", TEST_WIRED_FILE, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (strcmp (nm_setting_ip6_config_get_dns (s_ip6, 0), "1111:dddd::aaaa") == 0, + ASSERT (strcmp (nm_setting_ip_config_get_dns (s_ip6, 0), "1111:dddd::aaaa") == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value #1", TEST_WIRED_FILE, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); - ASSERT (strcmp (nm_setting_ip6_config_get_dns (s_ip6, 1), "1::cafe") == 0, + ASSERT (strcmp (nm_setting_ip_config_get_dns (s_ip6, 1), "1::cafe") == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value #2", TEST_WIRED_FILE, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); /* DNS Searches */ - ASSERT (nm_setting_ip6_config_get_num_dns_searches (s_ip6) == 3, + ASSERT (nm_setting_ip_config_get_num_dns_searches (s_ip6) == 3, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", TEST_WIRED_FILE, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); - ASSERT (!strcmp (nm_setting_ip6_config_get_dns_search (s_ip6, 0), expected6_dnssearch1), + ASSERT (!strcmp (nm_setting_ip_config_get_dns_search (s_ip6, 0), expected6_dnssearch1), "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value #1", TEST_WIRED_FILE, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS_SEARCH); - ASSERT (!strcmp (nm_setting_ip6_config_get_dns_search (s_ip6, 1), expected6_dnssearch2), + NM_SETTING_IP_CONFIG_DNS_SEARCH); + ASSERT (!strcmp (nm_setting_ip_config_get_dns_search (s_ip6, 1), expected6_dnssearch2), "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value #2", TEST_WIRED_FILE, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS_SEARCH); - ASSERT (!strcmp (nm_setting_ip6_config_get_dns_search (s_ip6, 2), expected6_dnssearch3), + NM_SETTING_IP_CONFIG_DNS_SEARCH); + ASSERT (!strcmp (nm_setting_ip_config_get_dns_search (s_ip6, 2), expected6_dnssearch3), "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value #3", TEST_WIRED_FILE, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS_SEARCH); + NM_SETTING_IP_CONFIG_DNS_SEARCH); /* IPv6 addresses */ - g_assert (nm_setting_ip6_config_get_num_addresses (s_ip6) == 10); - check_ip6_address (s_ip6, 0, "2:3:4:5:6:7:8:9", 64, "2:3:4:5:1:2:3:4"); - check_ip6_address (s_ip6, 1, "abcd:1234:ffff::cdde", 64, NULL); - check_ip6_address (s_ip6, 2, "1:2:3:4:5:6:7:8", 96, NULL); - check_ip6_address (s_ip6, 3, "3:4:5:6:7:8:9:0", 128, NULL); - check_ip6_address (s_ip6, 4, "3:4:5:6:7:8:9:14", 64, NULL); - check_ip6_address (s_ip6, 5, "3:4:5:6:7:8:9:15", 64, NULL); - check_ip6_address (s_ip6, 6, "3:4:5:6:7:8:9:16", 66, NULL); - check_ip6_address (s_ip6, 7, "3:4:5:6:7:8:9:17", 67, NULL); - check_ip6_address (s_ip6, 8, "3:4:5:6:7:8:9:18", 68, NULL); - check_ip6_address (s_ip6, 9, "3:4:5:6:7:8:9:19", 69, "1::9"); + g_assert (nm_setting_ip_config_get_num_addresses (s_ip6) == 10); + check_ip_address (s_ip6, 0, "2:3:4:5:6:7:8:9", 64, "2:3:4:5:1:2:3:4"); + check_ip_address (s_ip6, 1, "abcd:1234:ffff::cdde", 64, NULL); + check_ip_address (s_ip6, 2, "1:2:3:4:5:6:7:8", 96, NULL); + check_ip_address (s_ip6, 3, "3:4:5:6:7:8:9:0", 128, NULL); + check_ip_address (s_ip6, 4, "3:4:5:6:7:8:9:14", 64, NULL); + check_ip_address (s_ip6, 5, "3:4:5:6:7:8:9:15", 64, NULL); + check_ip_address (s_ip6, 6, "3:4:5:6:7:8:9:16", 66, NULL); + check_ip_address (s_ip6, 7, "3:4:5:6:7:8:9:17", 67, NULL); + check_ip_address (s_ip6, 8, "3:4:5:6:7:8:9:18", 68, NULL); + check_ip_address (s_ip6, 9, "3:4:5:6:7:8:9:19", 69, "1::9"); /* Route #1 */ - g_assert (nm_setting_ip6_config_get_num_routes (s_ip6) == 7); - check_ip6_route (s_ip6, 0, "d:e:f:0:1:2:3:4", 64, "f:e:d:c:1:2:3:4", 0); - check_ip6_route (s_ip6, 1, "a:b:c:d::", 64, "f:e:d:c:1:2:3:4", 99); - check_ip6_route (s_ip6, 2, "8:7:6:5:4:3:2:1", 128, NULL, 0); - check_ip6_route (s_ip6, 3, "6:7:8:9:0:1:2:3", 126, NULL, 1); - check_ip6_route (s_ip6, 4, "7:8:9:0:1:2:3:4", 125, NULL, 5); - check_ip6_route (s_ip6, 5, "8:9:0:1:2:3:4:5", 124, NULL, 6); - check_ip6_route (s_ip6, 6, "8:9:0:1:2:3:4:6", 123, NULL, 0); + g_assert (nm_setting_ip_config_get_num_routes (s_ip6) == 7); + check_ip_route (s_ip6, 0, "d:e:f:0:1:2:3:4", 64, "f:e:d:c:1:2:3:4", 0); + check_ip_route (s_ip6, 1, "a:b:c:d::", 64, "f:e:d:c:1:2:3:4", 99); + check_ip_route (s_ip6, 2, "8:7:6:5:4:3:2:1", 128, NULL, 0); + check_ip_route (s_ip6, 3, "6:7:8:9:0:1:2:3", 126, NULL, 1); + check_ip_route (s_ip6, 4, "7:8:9:0:1:2:3:4", 125, NULL, 5); + check_ip_route (s_ip6, 5, "8:9:0:1:2:3:4:5", 124, NULL, 6); + check_ip_route (s_ip6, 6, "8:9:0:1:2:3:4:6", 123, NULL, 0); g_object_unref (connection); } static void -add_one_ip4_address (NMSettingIP4Config *s_ip4, - const char *addr, - const char *gw, - guint32 prefix) -{ - NMIPAddress *ip4_addr; - GError *error = NULL; - - ip4_addr = nm_ip_address_new (AF_INET, addr, prefix, gw, &error); - g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, ip4_addr); - nm_ip_address_unref (ip4_addr); -} - -static void -add_one_ip4_route (NMSettingIP4Config *s_ip4, - const char *dest, - const char *nh, - guint32 prefix, - guint32 metric) -{ - NMIPRoute *route; - GError *error = NULL; - - route = nm_ip_route_new (AF_INET, dest, prefix, nh, metric, &error); - g_assert_no_error (error); - nm_setting_ip4_config_add_route (s_ip4, route); - nm_ip_route_unref (route); -} - -static void -add_one_ip6_address (NMSettingIP6Config *s_ip6, - const char *addr, - guint32 prefix, - const char *gw) +add_one_ip_address (NMSettingIPConfig *s_ip, + const char *addr, + guint32 prefix, + const char *gw) { - NMIPAddress *ip6_addr; + NMIPAddress *ip_addr; GError *error = NULL; - ip6_addr = nm_ip_address_new (AF_INET6, addr, prefix, gw, &error); + ip_addr = nm_ip_address_new (NM_IS_SETTING_IP4_CONFIG (s_ip) ? AF_INET : AF_INET6, + addr, prefix, gw, &error); g_assert_no_error (error); - nm_setting_ip6_config_add_address (s_ip6, ip6_addr); - nm_ip_address_unref (ip6_addr); + nm_setting_ip_config_add_address (s_ip, ip_addr); + nm_ip_address_unref (ip_addr); } static void -add_one_ip6_route (NMSettingIP6Config *s_ip6, - const char *dest, - const char *nh, - guint32 prefix, - guint32 metric) +add_one_ip_route (NMSettingIPConfig *s_ip, + const char *dest, + const char *nh, + guint32 prefix, + guint32 metric) { NMIPRoute *route; GError *error = NULL; - route = nm_ip_route_new (AF_INET6, dest, prefix, nh, metric, &error); + route = nm_ip_route_new (NM_IS_SETTING_IP4_CONFIG (s_ip) ? AF_INET : AF_INET6, + dest, prefix, nh, metric, &error); g_assert_no_error (error); - nm_setting_ip6_config_add_route (s_ip6, route); + nm_setting_ip_config_add_route (s_ip, route); nm_ip_route_unref (route); } @@ -444,8 +391,8 @@ test_write_wired_connection (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; const char *mac = "99:88:77:66:55:44"; gboolean success; @@ -511,52 +458,52 @@ test_write_wired_connection (void) /* IP4 setting */ - s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); + s_ip4 = NM_SETTING_IP_CONFIG (nm_setting_ip4_config_new ()); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); /* Addresses */ - add_one_ip4_address (s_ip4, address1, address1_gw, 24); - add_one_ip4_address (s_ip4, address2, address2_gw, 8); + add_one_ip_address (s_ip4, address1, 24, address1_gw); + add_one_ip_address (s_ip4, address2, 8, address2_gw); /* Routes */ - add_one_ip4_route (s_ip4, route1, route1_nh, 24, 3); - add_one_ip4_route (s_ip4, route2, route2_nh, 8, 1); - add_one_ip4_route (s_ip4, route3, route3_nh, 7, 0); - add_one_ip4_route (s_ip4, route4, route4_nh, 6, 4); + add_one_ip_route (s_ip4, route1, route1_nh, 24, 3); + add_one_ip_route (s_ip4, route2, route2_nh, 8, 1); + add_one_ip_route (s_ip4, route3, route3_nh, 7, 0); + add_one_ip_route (s_ip4, route4, route4_nh, 6, 4); /* DNS servers */ - nm_setting_ip4_config_add_dns (s_ip4, dns1); - nm_setting_ip4_config_add_dns (s_ip4, dns2); + nm_setting_ip_config_add_dns (s_ip4, dns1); + nm_setting_ip_config_add_dns (s_ip4, dns2); /* IP6 setting */ - s_ip6 = NM_SETTING_IP6_CONFIG (nm_setting_ip6_config_new ()); + s_ip6 = NM_SETTING_IP_CONFIG (nm_setting_ip6_config_new ()); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, NULL); /* Addresses */ - add_one_ip6_address (s_ip6, address6_1, 64, NULL); - add_one_ip6_address (s_ip6, address6_2, 56, NULL); + add_one_ip_address (s_ip6, address6_1, 64, NULL); + add_one_ip_address (s_ip6, address6_2, 56, NULL); /* Routes */ - add_one_ip6_route (s_ip6, route6_1, route6_1_nh, 64, 3); - add_one_ip6_route (s_ip6, route6_2, route6_2_nh, 56, 1); - add_one_ip6_route (s_ip6, route6_3, route6_3_nh, 63, 5); - add_one_ip6_route (s_ip6, route6_4, route6_4_nh, 62, 0); + add_one_ip_route (s_ip6, route6_1, route6_1_nh, 64, 3); + add_one_ip_route (s_ip6, route6_2, route6_2_nh, 56, 1); + add_one_ip_route (s_ip6, route6_3, route6_3_nh, 63, 5); + add_one_ip_route (s_ip6, route6_4, route6_4_nh, 62, 0); /* DNS servers */ - nm_setting_ip6_config_add_dns (s_ip6, dns6_1); - nm_setting_ip6_config_add_dns (s_ip6, dns6_2); + nm_setting_ip_config_add_dns (s_ip6, dns6_1); + nm_setting_ip_config_add_dns (s_ip6, dns6_2); /* DNS searches */ - nm_setting_ip6_config_add_dns_search (s_ip6, "wallaceandgromit.com"); + nm_setting_ip_config_add_dns_search (s_ip6, "wallaceandgromit.com"); /* Write out the connection */ owner_uid = geteuid (); @@ -592,8 +539,8 @@ test_read_ip6_wired_connection (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; GError *error = NULL; const char *tmp; const char *expected_id = "Test Wired Connection IP6"; @@ -657,18 +604,18 @@ test_read_ip6_wired_connection (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", TEST_WIRED_IP6_FILE, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); - ASSERT (nm_setting_ip4_config_get_num_addresses (s_ip4) == 0, + ASSERT (nm_setting_ip_config_get_num_addresses (s_ip4) == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", TEST_WIRED_IP6_FILE, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS); + NM_SETTING_IP_CONFIG_DNS); /* ===== IPv6 SETTING ===== */ @@ -679,16 +626,16 @@ test_read_ip6_wired_connection (void) NM_SETTING_IP6_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip6_config_get_method (s_ip6); + tmp = nm_setting_ip_config_get_method (s_ip6); ASSERT (strcmp (tmp, NM_SETTING_IP6_CONFIG_METHOD_MANUAL) == 0, "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", TEST_WIRED_IP6_FILE, NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); /* IPv6 address */ - g_assert (nm_setting_ip6_config_get_num_addresses (s_ip6) == 1); - check_ip6_address (s_ip6, 0, "abcd:1234:ffff::cdde", 64, "abcd:1234:ffff::cdd1"); + g_assert (nm_setting_ip_config_get_num_addresses (s_ip6) == 1); + check_ip_address (s_ip6, 0, "abcd:1234:ffff::cdde", 64, "abcd:1234:ffff::cdd1"); g_object_unref (connection); } @@ -699,8 +646,8 @@ test_write_ip6_wired_connection (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; NMConnection *reread; @@ -735,30 +682,30 @@ test_write_ip6_wired_connection (void) /* IP4 setting */ - s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); + s_ip4 = NM_SETTING_IP_CONFIG (nm_setting_ip4_config_new ()); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED, NULL); /* IP6 setting */ - s_ip6 = NM_SETTING_IP6_CONFIG (nm_setting_ip6_config_new ()); + s_ip6 = NM_SETTING_IP_CONFIG (nm_setting_ip6_config_new ()); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, NULL); /* Addresses */ - add_one_ip6_address (s_ip6, address, 64, gw); + add_one_ip_address (s_ip6, address, 64, gw); /* DNS servers */ - nm_setting_ip6_config_add_dns (s_ip6, dns); + nm_setting_ip_config_add_dns (s_ip6, dns); /* DNS searches */ - nm_setting_ip6_config_add_dns_search (s_ip6, "wallaceandgromit.com"); + nm_setting_ip_config_add_dns_search (s_ip6, "wallaceandgromit.com"); /* Write out the connection */ owner_uid = geteuid (); @@ -949,7 +896,7 @@ test_read_valid_wireless_connection (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWireless *s_wireless; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; GError *error = NULL; const char *bssid; const guint8 expected_bssid[ETH_ALEN] = { 0x00, 0x1a, 0x33, 0x44, 0x99, 0x82 }; @@ -1045,12 +992,12 @@ test_read_valid_wireless_connection (void) NM_SETTING_IP4_CONFIG_SETTING_NAME); /* Method */ - tmp = nm_setting_ip4_config_get_method (s_ip4); + tmp = nm_setting_ip_config_get_method (s_ip4); ASSERT (strcmp (tmp, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0, "connection-verify-wireless", "failed to verify %s: unexpected %s / %s key value", TEST_WIRELESS_FILE, NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_METHOD); + NM_SETTING_IP_CONFIG_METHOD); g_object_unref (connection); } @@ -1061,8 +1008,8 @@ test_write_wireless_connection (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWireless *s_wireless; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; const char *bssid = "aa:b9:a1:74:55:44"; GBytes *ssid; @@ -1109,20 +1056,20 @@ test_write_wireless_connection (void) /* IP4 setting */ - s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); + s_ip4 = NM_SETTING_IP_CONFIG (nm_setting_ip4_config_new ()); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = NM_SETTING_IP6_CONFIG (nm_setting_ip6_config_new ()); + s_ip6 = NM_SETTING_IP_CONFIG (nm_setting_ip6_config_new ()); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); /* Write out the connection */ @@ -1199,7 +1146,7 @@ test_write_string_ssid (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWireless *s_wireless; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *uuid, *testfile = NULL, *tmp; GBytes *ssid; unsigned char tmpssid[] = { 65, 49, 50, 51, 32, 46, 92, 46, 36, 37, 126, 93 }; @@ -1236,11 +1183,11 @@ test_write_string_ssid (void) /* IP4 setting */ - s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); + s_ip4 = NM_SETTING_IP_CONFIG (nm_setting_ip4_config_new ()); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* Write out the connection */ @@ -1322,7 +1269,7 @@ test_write_intlist_ssid (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWireless *s_wifi; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *uuid, *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 65, 49, 50, 51, 0, 50, 50 }; @@ -1362,10 +1309,10 @@ test_write_intlist_ssid (void) g_bytes_unref (ssid); /* IP4 setting */ - s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); + s_ip4 = NM_SETTING_IP_CONFIG (nm_setting_ip4_config_new ()); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* Write out the connection */ owner_uid = geteuid (); @@ -1483,7 +1430,7 @@ test_write_intlike_ssid (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWireless *s_wifi; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *uuid, *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 49, 48, 49 }; @@ -1522,10 +1469,10 @@ test_write_intlike_ssid (void) g_bytes_unref (ssid); /* IP4 setting */ - s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); + s_ip4 = NM_SETTING_IP_CONFIG (nm_setting_ip4_config_new ()); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* Write out the connection */ owner_uid = geteuid (); @@ -1569,7 +1516,7 @@ test_write_intlike_ssid_2 (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWireless *s_wifi; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; char *uuid, *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 49, 49, 59, 49, 50, 59, 49, 51, 59}; @@ -1608,10 +1555,10 @@ test_write_intlike_ssid_2 (void) g_bytes_unref (ssid); /* IP4 setting */ - s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); + s_ip4 = NM_SETTING_IP_CONFIG (nm_setting_ip4_config_new ()); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* Write out the connection */ owner_uid = geteuid (); @@ -1816,7 +1763,7 @@ test_write_bt_dun_connection (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingBluetooth *s_bt; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSettingGsm *s_gsm; char *uuid; const char *bdaddr = "aa:b9:a1:74:55:44"; @@ -1857,11 +1804,11 @@ test_write_bt_dun_connection (void) /* IP4 setting */ - s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); + s_ip4 = NM_SETTING_IP_CONFIG (nm_setting_ip4_config_new ()); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* GSM setting */ @@ -2065,7 +2012,7 @@ test_write_gsm_connection (void) { NMConnection *connection; NMSettingConnection *s_con; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSettingGsm *s_gsm; char *uuid; gboolean success; @@ -2095,11 +2042,11 @@ test_write_gsm_connection (void) /* IP4 setting */ - s_ip4 = NM_SETTING_IP4_CONFIG (nm_setting_ip4_config_new ()); + s_ip4 = NM_SETTING_IP_CONFIG (nm_setting_ip4_config_new ()); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* GSM setting */ @@ -2401,7 +2348,7 @@ create_wired_tls_connection (NMSetting8021xCKScheme scheme) { NMConnection *connection; NMSettingConnection *s_con; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSetting *s_wired; NMSetting8021x *s_8021x; char *uuid; @@ -2425,9 +2372,9 @@ create_wired_tls_connection (NMSetting8021xCKScheme scheme) g_free (uuid); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); /* Wired setting */ @@ -2704,8 +2651,8 @@ test_write_infiniband_connection (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingInfiniband *s_ib; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; const char *mac = "99:88:77:66:55:44:ab:bc:cd:de:ef:f0:0a:1b:2c:3d:4e:5f:6f:ba"; gboolean success; @@ -2745,16 +2692,16 @@ test_write_infiniband_connection (void) NULL); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_assert (s_ip6); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); - g_object_set (s_ip6, NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip6, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); /* Write out the connection */ owner_uid = geteuid (); @@ -2785,7 +2732,7 @@ test_read_bridge_main (void) { NMConnection *connection; NMSettingConnection *s_con; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSettingBridge *s_bridge; GError *error = NULL; const char *expected_id = "Test Bridge Main"; @@ -2809,7 +2756,7 @@ test_read_bridge_main (void) /* IPv4 setting */ s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); - g_assert_cmpstr (nm_setting_ip4_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); + g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); /* Bridge setting */ s_bridge = nm_connection_get_setting_bridge (connection); @@ -2830,8 +2777,8 @@ test_write_bridge_main (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingBridge *s_bridge; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; NMConnection *reread; @@ -2864,21 +2811,21 @@ test_write_bridge_main (void) nm_connection_add_setting (connection, NM_SETTING (s_bridge)); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); g_assert (s_ip4); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, - NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); - add_one_ip4_address (s_ip4, "1.2.3.4", "1.1.1.1", 24); + add_one_ip_address (s_ip4, "1.2.3.4", 24, "1.1.1.1"); /* IP6 setting */ - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); g_assert (s_ip6); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); - g_object_set (s_ip6, NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip6, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); /* Write out the connection */ owner_uid = geteuid (); @@ -3401,7 +3348,7 @@ static void test_read_enum_property (void) { NMConnection *connection; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; GError *error = NULL; gboolean success; @@ -3415,7 +3362,7 @@ test_read_enum_property (void) /* IPv6 setting */ s_ip6 = nm_connection_get_setting_ip6_config (connection); g_assert (s_ip6); - g_assert_cmpint (nm_setting_ip6_config_get_ip6_privacy (s_ip6), ==, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR); + g_assert_cmpint (nm_setting_ip6_config_get_ip6_privacy (NM_SETTING_IP6_CONFIG (s_ip6)), ==, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR); g_object_unref (connection); } @@ -3426,7 +3373,7 @@ test_write_enum_property (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; char *uuid; gboolean success; NMConnection *reread; @@ -3455,10 +3402,10 @@ test_write_enum_property (void) nm_connection_add_setting (connection, NM_SETTING (s_wired)); /* IP6 setting */ - s_ip6 = NM_SETTING_IP6_CONFIG (nm_setting_ip6_config_new ()); + s_ip6 = NM_SETTING_IP_CONFIG (nm_setting_ip6_config_new ()); nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NM_SETTING_IP6_CONFIG_IP6_PRIVACY, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR, NULL); diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c index 766eabf44f..fdf4823a51 100644 --- a/src/settings/plugins/keyfile/writer.c +++ b/src/settings/plugins/keyfile/writer.c @@ -578,25 +578,25 @@ static KeyWriter key_writers[] = { NM_SETTING_CONNECTION_TYPE, setting_alias_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ADDRESSES, + NM_SETTING_IP_CONFIG_ADDRESSES, addr_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, "address-labels", ip4_addr_label_writer }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ADDRESSES, + NM_SETTING_IP_CONFIG_ADDRESSES, addr_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES, + NM_SETTING_IP_CONFIG_ROUTES, route_writer }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ROUTES, + NM_SETTING_IP_CONFIG_ROUTES, route_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS, + NM_SETTING_IP_CONFIG_DNS, dns_writer }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS, + NM_SETTING_IP_CONFIG_DNS, dns_writer }, { NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID, diff --git a/src/supplicant-manager/tests/test-supplicant-config.c b/src/supplicant-manager/tests/test-supplicant-config.c index 2d16eecfd6..4f1950128f 100644 --- a/src/supplicant-manager/tests/test-supplicant-config.c +++ b/src/supplicant-manager/tests/test-supplicant-config.c @@ -108,7 +108,7 @@ test_wifi_open (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWireless *s_wifi; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSupplicantConfig *config; GHashTable *hash; char *uuid; @@ -149,10 +149,10 @@ test_wifi_open (void) g_bytes_unref (ssid); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, "wifi-open", "failed to verify connection: %s", @@ -203,7 +203,7 @@ test_wifi_wep_key (const char *detail, NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSupplicantConfig *config; GHashTable *hash; char *uuid; @@ -254,10 +254,10 @@ test_wifi_wep_key (const char *detail, nm_setting_wireless_security_set_wep_key (s_wsec, 0, key_data); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, detail, "failed to verify connection: %s", @@ -340,7 +340,7 @@ test_wifi_wpa_psk (const char *detail, NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; NMSupplicantConfig *config; GHashTable *hash; char *uuid; @@ -397,10 +397,10 @@ test_wifi_wpa_psk (const char *detail, nm_setting_wireless_security_add_group (s_wsec, "ccmp"); /* IP4 setting */ - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); ASSERT (nm_connection_verify (connection, &error) == TRUE, detail, "failed to verify connection: %s", diff --git a/src/tests/test-general.c b/src/tests/test-general.c index e16d702ddb..2f073c9cac 100644 --- a/src/tests/test-general.c +++ b/src/tests/test-general.c @@ -288,8 +288,7 @@ _match_connection_new (void) NMConnection *connection; NMSettingConnection *s_con; NMSettingWired *s_wired; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4, *s_ip6; char *uuid; connection = nm_simple_connection_new (); @@ -308,16 +307,16 @@ _match_connection_new (void) s_wired = (NMSettingWired *) nm_setting_wired_new (); nm_connection_add_setting (connection, (NMSetting *) s_wired); - s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new (); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, (NMSetting *) s_ip4); g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); - s_ip6 = (NMSettingIP6Config *) nm_setting_ip6_config_new (); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); nm_connection_add_setting (connection, (NMSetting *) s_ip6); g_object_set (G_OBJECT (s_ip6), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); return connection; @@ -328,7 +327,7 @@ test_connection_match_basic (void) { NMConnection *orig, *copy, *matched; GSList *connections = NULL; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; orig = _match_connection_new (); copy = nm_simple_connection_new_clone (orig); @@ -341,7 +340,7 @@ test_connection_match_basic (void) s_ip4 = nm_connection_get_setting_ip4_config (orig); g_assert (s_ip4); g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL, NULL); matched = nm_utils_match_connection (connections, orig, TRUE, NULL, NULL); g_assert (matched == NULL); @@ -356,7 +355,7 @@ test_connection_match_ip6_method (void) { NMConnection *orig, *copy, *matched; GSList *connections = NULL; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; orig = _match_connection_new (); copy = nm_simple_connection_new_clone (orig); @@ -369,14 +368,14 @@ test_connection_match_ip6_method (void) s_ip6 = nm_connection_get_setting_ip6_config (orig); g_assert (s_ip6); g_object_set (G_OBJECT (s_ip6), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL, NULL); s_ip6 = nm_connection_get_setting_ip6_config (copy); g_assert (s_ip6); g_object_set (G_OBJECT (s_ip6), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, - NM_SETTING_IP6_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); matched = nm_utils_match_connection (connections, orig, TRUE, NULL, NULL); @@ -392,7 +391,7 @@ test_connection_match_ip6_method_ignore (void) { NMConnection *orig, *copy, *matched; GSList *connections = NULL; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; orig = _match_connection_new (); copy = nm_simple_connection_new_clone (orig); @@ -404,13 +403,13 @@ test_connection_match_ip6_method_ignore (void) s_ip6 = nm_connection_get_setting_ip6_config (orig); g_assert (s_ip6); g_object_set (G_OBJECT (s_ip6), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL, NULL); s_ip6 = nm_connection_get_setting_ip6_config (copy); g_assert (s_ip6); g_object_set (G_OBJECT (s_ip6), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL); matched = nm_utils_match_connection (connections, orig, TRUE, NULL, NULL); @@ -426,7 +425,7 @@ test_connection_match_ip6_method_ignore_auto (void) { NMConnection *orig, *copy, *matched; GSList *connections = NULL; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip6; orig = _match_connection_new (); copy = nm_simple_connection_new_clone (orig); @@ -438,13 +437,13 @@ test_connection_match_ip6_method_ignore_auto (void) s_ip6 = nm_connection_get_setting_ip6_config (orig); g_assert (s_ip6); g_object_set (G_OBJECT (s_ip6), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); s_ip6 = nm_connection_get_setting_ip6_config (copy); g_assert (s_ip6); g_object_set (G_OBJECT (s_ip6), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL); matched = nm_utils_match_connection (connections, orig, TRUE, NULL, NULL); @@ -461,7 +460,7 @@ test_connection_match_ip4_method (void) { NMConnection *orig, *copy, *matched; GSList *connections = NULL; - NMSettingIP4Config *s_ip4; + NMSettingIPConfig *s_ip4; orig = _match_connection_new (); copy = nm_simple_connection_new_clone (orig); @@ -474,14 +473,14 @@ test_connection_match_ip4_method (void) s_ip4 = nm_connection_get_setting_ip4_config (orig); g_assert (s_ip4); g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_DISABLED, NULL); s_ip4 = nm_connection_get_setting_ip4_config (copy); g_assert (s_ip4); g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, - NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, + NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); matched = nm_utils_match_connection (connections, orig, FALSE, NULL, NULL); @@ -574,8 +573,7 @@ test_connection_no_match_ip4_addr (void) { NMConnection *orig, *copy, *matched; GSList *connections = NULL; - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; + NMSettingIPConfig *s_ip4, *s_ip6; NMIPAddress *nm_addr; GError *error = NULL; @@ -589,34 +587,34 @@ test_connection_no_match_ip4_addr (void) s_ip6 = nm_connection_get_setting_ip6_config (orig); g_assert (s_ip6); g_object_set (G_OBJECT (s_ip6), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL, NULL); s_ip6 = nm_connection_get_setting_ip6_config (copy); g_assert (s_ip6); g_object_set (G_OBJECT (s_ip6), - NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL); s_ip4 = nm_connection_get_setting_ip4_config (orig); g_assert (s_ip4); g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); nm_addr = nm_ip_address_new (AF_INET, "1.1.1.4", 24, "1.1.1.254", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, nm_addr); + nm_setting_ip_config_add_address (s_ip4, nm_addr); nm_ip_address_unref (nm_addr); s_ip4 = nm_connection_get_setting_ip4_config (copy); g_assert (s_ip4); g_object_set (G_OBJECT (s_ip4), - NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); nm_addr = nm_ip_address_new (AF_INET, "2.2.2.4", 24, "2.2.2.254", &error); g_assert_no_error (error); - nm_setting_ip4_config_add_address (s_ip4, nm_addr); + nm_setting_ip_config_add_address (s_ip4, nm_addr); nm_ip_address_unref (nm_addr); matched = nm_utils_match_connection (connections, orig, TRUE, NULL, NULL); diff --git a/src/tests/test-resolvconf-capture.c b/src/tests/test-resolvconf-capture.c index 2d787b7188..555f10c016 100644 --- a/src/tests/test-resolvconf-capture.c +++ b/src/tests/test-resolvconf-capture.c @@ -20,6 +20,7 @@ #include #include +#include #include "NetworkManagerUtils.h" #include "nm-ip4-config.h" -- cgit v1.2.1 From 35f6264745e0372212513ebee9aad53a0a37708e Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 4 Nov 2014 10:46:05 -0500 Subject: core, clients: implement dhcp-send-hostname for IPv6 Now that NMSettingIP6Config inherits the dhcp-send-hostname property from NMSettingIPConfig, fix things up so that it actually gets used. (Note that this changes behavior: previously if ip6.dhcp-hostname was unset, no hostname would be sent. Now, the system hostname will be set. Also, ifcfg-rh does not currently support this property, so there is no way to disable this...) --- clients/cli/settings.c | 15 +++++++++++++-- src/devices/nm-device.c | 1 + src/dhcp-manager/nm-dhcp-manager.c | 7 ++++--- src/dhcp-manager/nm-dhcp-manager.h | 1 + 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/clients/cli/settings.c b/clients/cli/settings.c index 96932e5a16..cb3dccf48e 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -295,7 +295,8 @@ NmcOutputField nmc_fields_setting_ip6_config[] = { SETTING_FIELD (NM_SETTING_IP_CONFIG_NEVER_DEFAULT, 15), /* 8 */ SETTING_FIELD (NM_SETTING_IP_CONFIG_MAY_FAIL, 12), /* 9 */ SETTING_FIELD (NM_SETTING_IP6_CONFIG_IP6_PRIVACY, 15), /* 10 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, 14), /* 11 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, 19), /* 11 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, 14), /* 12 */ {NULL, NULL, 0, NULL, FALSE, FALSE, 0} }; #define NMC_FIELDS_SETTING_IP6_CONFIG_ALL "name"","\ @@ -309,6 +310,7 @@ NmcOutputField nmc_fields_setting_ip6_config[] = { NM_SETTING_IP_CONFIG_NEVER_DEFAULT","\ NM_SETTING_IP_CONFIG_MAY_FAIL","\ NM_SETTING_IP6_CONFIG_IP6_PRIVACY","\ + NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME","\ NM_SETTING_IP_CONFIG_DHCP_HOSTNAME #define NMC_FIELDS_SETTING_IP6_CONFIG_COMMON NMC_FIELDS_SETTING_IP4_CONFIG_ALL @@ -1363,6 +1365,7 @@ DEFINE_GETTER (nmc_property_ipv6_get_ignore_auto_routes, NM_SETTING_IP_CONFIG_IG DEFINE_GETTER (nmc_property_ipv6_get_ignore_auto_dns, NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS) DEFINE_GETTER (nmc_property_ipv6_get_never_default, NM_SETTING_IP_CONFIG_NEVER_DEFAULT) DEFINE_GETTER (nmc_property_ipv6_get_may_fail, NM_SETTING_IP_CONFIG_MAY_FAIL) +DEFINE_GETTER (nmc_property_ipv6_get_dhcp_send_hostname, NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME) DEFINE_GETTER (nmc_property_ipv6_get_dhcp_hostname, NM_SETTING_IP_CONFIG_DHCP_HOSTNAME) static char * @@ -5579,6 +5582,13 @@ nmc_properties_init (void) NULL, NULL, nmc_property_out2in_cut_paren); + nmc_add_prop_funcs (GLUE_IP (6, DHCP_SEND_HOSTNAME), + nmc_property_ipv6_get_dhcp_send_hostname, + nmc_property_set_bool, + NULL, + NULL, + NULL, + NULL); nmc_add_prop_funcs (GLUE_IP (6, DHCP_HOSTNAME), nmc_property_ipv6_get_dhcp_hostname, nmc_property_set_string, @@ -6780,7 +6790,8 @@ setting_ip6_config_details (NMSetting *setting, NmCli *nmc, const char *one_pro set_val_str (arr, 8, nmc_property_ipv6_get_never_default (setting)); set_val_str (arr, 9, nmc_property_ipv6_get_may_fail (setting)); set_val_str (arr, 10, nmc_property_ipv6_get_ip6_privacy (setting)); - set_val_str (arr, 11, nmc_property_ipv6_get_dhcp_hostname (setting)); + set_val_str (arr, 11, nmc_property_ipv6_get_dhcp_send_hostname (setting)); + set_val_str (arr, 12, nmc_property_ipv6_get_dhcp_hostname (setting)); g_ptr_array_add (nmc->output_data, arr); print_data (nmc); /* Print all data */ diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index a9ea2cb61c..d9fdd1bb8b 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -3356,6 +3356,7 @@ dhcp6_start (NMDevice *self, tmp, nm_connection_get_uuid (connection), nm_device_get_priority (self), + nm_setting_ip_config_get_dhcp_send_hostname (s_ip6), nm_setting_ip_config_get_dhcp_hostname (s_ip6), priv->dhcp_timeout, priv->dhcp_anycast_address, diff --git a/src/dhcp-manager/nm-dhcp-manager.c b/src/dhcp-manager/nm-dhcp-manager.c index b98260d431..2df3cb108c 100644 --- a/src/dhcp-manager/nm-dhcp-manager.c +++ b/src/dhcp-manager/nm-dhcp-manager.c @@ -459,18 +459,19 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self, const GByteArray *hwaddr, const char *uuid, guint priority, + gboolean send_hostname, const char *dhcp_hostname, guint32 timeout, const char *dhcp_anycast_addr, gboolean info_only, NMSettingIP6ConfigPrivacy privacy) { - const char *hostname; + const char *hostname = NULL; g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL); - hostname = dhcp_hostname ? get_send_hostname (self, dhcp_hostname) : NULL; - + if (send_hostname) + hostname = get_send_hostname (self, dhcp_hostname); return client_start (self, iface, ifindex, hwaddr, uuid, priority, TRUE, NULL, timeout, dhcp_anycast_addr, hostname, info_only, privacy); diff --git a/src/dhcp-manager/nm-dhcp-manager.h b/src/dhcp-manager/nm-dhcp-manager.h index 58fc23d28e..0228130e0f 100644 --- a/src/dhcp-manager/nm-dhcp-manager.h +++ b/src/dhcp-manager/nm-dhcp-manager.h @@ -69,6 +69,7 @@ NMDhcpClient * nm_dhcp_manager_start_ip6 (NMDhcpManager *manager, const GByteArray *hwaddr, const char *uuid, guint priority, + gboolean send_hostname, const char *dhcp_hostname, guint32 timeout, const char *dhcp_anycast_addr, -- cgit v1.2.1 From 329791ad55ee66e2d12c3cedcc2daf8c0865f7a6 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Mon, 20 Oct 2014 20:36:57 -0400 Subject: all: stop pretending to support multiple "gateway"s NMSettingIP[46]Config let you associate a gateway with each address, and the writable settings backends record that information. But it never actually gets used: NMIP4Config and NMIP6Config only ever use the first gateway, and completely ignore any others. (And in the common usage of the term, an interface can only have one gateway anyway.) So, stop pretending that multiple gateways are meaningful; don't serialize or deserialize gateways other than the first in the 'addresses' properties, and don't read or write multiple gateway values either. --- libnm-core/nm-utils.c | 21 +++++++++++++-------- src/nm-ip4-config.c | 18 +++++++----------- src/nm-ip6-config.c | 11 ++++++----- src/settings/plugins/ifcfg-rh/reader.c | 3 +++ src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c | 7 ++----- src/settings/plugins/ifcfg-rh/writer.c | 4 ++-- src/settings/plugins/ifnet/connection_parser.c | 9 ++++----- src/settings/plugins/ifupdown/parser.c | 8 ++++++-- src/settings/plugins/keyfile/reader.c | 9 ++++++--- src/settings/plugins/keyfile/tests/test-keyfile.c | 11 +++++------ src/settings/plugins/keyfile/writer.c | 5 ++++- 11 files changed, 58 insertions(+), 48 deletions(-) diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 0f67da3733..110142fbfc 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -1140,7 +1140,7 @@ nm_utils_ip4_dns_from_variant (GVariant *value) * Utility function to convert a #GPtrArray of #NMIPAddress objects representing * IPv4 addresses into a #GVariant of type 'aau' representing an array of * NetworkManager IPv4 addresses (which are tuples of address, prefix, and - * gateway). + * gateway, although only the first "gateway" field is preserved) * * Returns: (transfer none): a new floating #GVariant representing @addresses. **/ @@ -1162,7 +1162,7 @@ nm_utils_ip4_addresses_to_variant (GPtrArray *addresses) nm_ip_address_get_address_binary (addr, &array[0]); array[1] = nm_ip_address_get_prefix (addr); - if (nm_ip_address_get_gateway (addr)) + if (i == 0 && nm_ip_address_get_gateway (addr)) inet_pton (AF_INET, nm_ip_address_get_gateway (addr), &array[2]); else array[2] = 0; @@ -1182,7 +1182,8 @@ nm_utils_ip4_addresses_to_variant (GPtrArray *addresses) * * Utility function to convert a #GVariant of type 'aau' representing a list of * NetworkManager IPv4 addresses (which are tuples of address, prefix, and - * gateway) into a #GPtrArray of #NMIPAddress objects. + * gateway, although only the first "gateway" field is preserved) into a + * #GPtrArray of #NMIPAddress objects. * * Returns: (transfer full) (element-type NMIPAddress): a newly allocated * #GPtrArray of #NMIPAddress objects @@ -1213,7 +1214,8 @@ nm_utils_ip4_addresses_from_variant (GVariant *value) } addr = nm_ip_address_new_binary (AF_INET, - &addr_array[0], addr_array[1], &addr_array[2], + &addr_array[0], addr_array[1], + addresses->len == 0 ? &addr_array[2] : NULL, &error); if (addr) g_ptr_array_add (addresses, addr); @@ -1471,7 +1473,7 @@ nm_utils_ip6_dns_from_variant (GVariant *value) * Utility function to convert a #GPtrArray of #NMIPAddress objects representing * IPv6 addresses into a #GVariant of type 'a(ayuay)' representing an array of * NetworkManager IPv6 addresses (which are tuples of address, prefix, and - * gateway). + * gateway, although only the first "gateway" field is preserved). * * Returns: (transfer none): a new floating #GVariant representing @addresses. **/ @@ -1498,7 +1500,7 @@ nm_utils_ip6_addresses_to_variant (GPtrArray *addresses) prefix = nm_ip_address_get_prefix (addr); - if (nm_ip_address_get_gateway (addr)) + if (i == 0 && nm_ip_address_get_gateway (addr)) inet_pton (AF_INET6, nm_ip_address_get_gateway (addr), &gateway_bytes); else memset (&gateway_bytes, 0, sizeof (gateway_bytes)); @@ -1517,7 +1519,8 @@ nm_utils_ip6_addresses_to_variant (GPtrArray *addresses) * * Utility function to convert a #GVariant of type 'a(ayuay)' representing a * list of NetworkManager IPv6 addresses (which are tuples of address, prefix, - * and gateway) into a #GPtrArray of #NMIPAddress objects. + * and gateway, although only the first "gateway" field is preserved) into a + * #GPtrArray of #NMIPAddress objects. * * Returns: (transfer full) (element-type NMIPAddress): a newly allocated * #GPtrArray of #NMIPAddress objects @@ -1560,7 +1563,9 @@ nm_utils_ip6_addresses_from_variant (GVariant *value) goto next; } - addr = nm_ip_address_new_binary (AF_INET6, addr_bytes, prefix, gateway_bytes, &error); + addr = nm_ip_address_new_binary (AF_INET6, addr_bytes, prefix, + addresses->len == 0 ? gateway_bytes : NULL, + &error); if (addr) g_ptr_array_add (addresses, addr); else { diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index c51b449920..af284f0bc2 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -319,14 +319,13 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIPConfig *setting, in nm_ip4_config_set_never_default (config, TRUE); else if (nm_setting_ip_config_get_ignore_auto_routes (setting)) nm_ip4_config_set_never_default (config, FALSE); - for (i = 0; i < naddresses; i++) { - const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip_config_get_address (setting, i)); + if (naddresses) { + const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip_config_get_address (setting, 0)); guint32 gateway; if (gateway_str) { inet_pton (AF_INET, gateway_str, &gateway); nm_ip4_config_set_gateway (config, gateway); - break; } } @@ -426,13 +425,9 @@ nm_ip4_config_create_setting (const NMIP4Config *config) if (!method) method = NM_SETTING_IP4_CONFIG_METHOD_MANUAL; - s_addr = nm_ip_address_new_binary (AF_INET, &address->address, address->plen, NULL, NULL); - /* For backwards compatibility, attach the gateway to an address if it's - * in the same subnet. - */ - if (same_prefix (address->address, gateway, address->plen)) - nm_ip_address_set_gateway (s_addr, nm_utils_inet4_ntop (gateway, NULL)); - + s_addr = nm_ip_address_new_binary (AF_INET, &address->address, address->plen, + i == 0 ? &gateway : NULL, + NULL); if (*address->label) nm_ip_address_set_attribute (s_addr, "label", g_variant_new_string (address->label)); @@ -1723,10 +1718,11 @@ get_property (GObject *object, guint prop_id, for (i = 0; i < naddr; i++) { const NMPlatformIP4Address *address = nm_ip4_config_get_address (config, i); GArray *array = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 3); + guint32 gateway = i == 0 ? priv->gateway : 0; g_array_append_val (array, address->address); g_array_append_val (array, address->plen); - g_array_append_val (array, priv->gateway); + g_array_append_val (array, gateway); g_ptr_array_add (addresses, array); } diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index e022e7178c..fd21e5a624 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -423,14 +423,13 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIPConfig *setting, in nm_ip6_config_set_never_default (config, TRUE); else if (nm_setting_ip_config_get_ignore_auto_routes (setting)) nm_ip6_config_set_never_default (config, FALSE); - for (i = 0; i < naddresses; i++) { - const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip_config_get_address (setting, i)); + if (naddresses) { + const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip_config_get_address (setting, 0)); struct in6_addr gateway; if (gateway_str) { inet_pton (AF_INET6, gateway_str, &gateway); nm_ip6_config_set_gateway (config, &gateway); - break; } } @@ -532,7 +531,9 @@ nm_ip6_config_create_setting (const NMIP6Config *config) if (!method || strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0) method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL; - s_addr = nm_ip_address_new_binary (AF_INET6, &address->address, address->plen, gateway, NULL); + s_addr = nm_ip_address_new_binary (AF_INET6, &address->address, address->plen, + i == 0 ? gateway : NULL, + NULL); nm_setting_ip_config_add_address (s_ip6, s_addr); nm_ip_address_unref (s_addr); } @@ -1610,7 +1611,7 @@ get_property (GObject *object, guint prop_id, /* Gateway */ g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); ba = g_byte_array_new (); - g_byte_array_append (ba, (guint8 *) (gateway ? gateway : &in6addr_any), sizeof (*gateway)); + g_byte_array_append (ba, (guint8 *) (i == 0 && gateway ? gateway : &in6addr_any), sizeof (*gateway)); g_value_take_boxed (&element, ba); g_value_array_append (array, &element); g_value_unset (&element); diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index b16302c0ba..7b193589d2 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -1042,6 +1042,9 @@ make_ip4_setting (shvarFile *ifcfg, continue; } + if (nm_setting_ip_config_get_num_addresses (s_ip4)) + nm_ip_address_set_gateway (addr, NULL); + if (!nm_setting_ip_config_add_address (s_ip4, addr)) PARSE_WARNING ("duplicate IP4 address"); nm_ip_address_unref (addr); diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index 3c43a2f5df..e9ce7ca43b 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -7831,14 +7831,11 @@ test_write_gateway (void) g_assert_cmpstr (val, ==, "1.1.1.254"); g_free (val); - val = svGetValue (f, "GATEWAY1", FALSE); - g_assert (val); - g_assert_cmpstr (val, ==, "2.2.2.254"); - g_free (val); - val = svGetValue (f, "GATEWAY0", FALSE); g_assert (val == NULL); + val = svGetValue (f, "GATEWAY1", FALSE); + g_assert (val == NULL); svCloseFile (f); diff --git a/src/settings/plugins/ifcfg-rh/writer.c b/src/settings/plugins/ifcfg-rh/writer.c index d12ff03cf4..694ed4315e 100644 --- a/src/settings/plugins/ifcfg-rh/writer.c +++ b/src/settings/plugins/ifcfg-rh/writer.c @@ -1916,7 +1916,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) svSetValue (ifcfg, netmask_key, NULL, FALSE); - svSetValue (ifcfg, gw_key, nm_ip_address_get_gateway (addr), FALSE); + svSetValue (ifcfg, gw_key, n == 0 ? nm_ip_address_get_gateway (addr) : NULL, FALSE); g_free (addr_key); g_free (prefix_key); @@ -2172,7 +2172,7 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) svSetValue (ifcfg, "PREFIX", tmp, FALSE); g_free (tmp); - svSetValue (ifcfg, "GATEWAY", nm_ip_address_get_gateway (addr), FALSE); + svSetValue (ifcfg, "GATEWAY", i == 0 ? nm_ip_address_get_gateway (addr) : NULL, FALSE); svWriteFile (ifcfg, 0644, NULL); svCloseFile (ifcfg); diff --git a/src/settings/plugins/ifnet/connection_parser.c b/src/settings/plugins/ifnet/connection_parser.c index 4233c75f2f..6deee55f0a 100644 --- a/src/settings/plugins/ifnet/connection_parser.c +++ b/src/settings/plugins/ifnet/connection_parser.c @@ -614,8 +614,9 @@ make_ip4_setting (NMConnection *connection, NMIPAddress *ip4_addr; GError *local = NULL; - /* currently all the IPs has the same gateway */ - ip4_addr = nm_ip_address_new (AF_INET, iblock->ip, iblock->prefix, iblock->next_hop, &local); + ip4_addr = nm_ip_address_new (AF_INET, iblock->ip, iblock->prefix, + nm_setting_ip_config_get_num_addresses (ip4_setting) == 0 ? iblock->next_hop : NULL, + &local); if (iblock->next_hop) g_object_set (ip4_setting, NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, @@ -2379,7 +2380,6 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err GString *ips; GString *routes; GString *dns; - gboolean has_def_route = FALSE; gboolean success = FALSE; s_ip4 = nm_connection_get_setting_ip4_config (connection); @@ -2408,11 +2408,10 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err nm_ip_address_get_prefix (addr)); /* only the first gateway will be written */ - if (!has_def_route && nm_ip_address_get_gateway (addr)) { + if (i == 0 && nm_ip_address_get_gateway (addr)) { g_string_append_printf (routes, "\"default via %s\" ", nm_ip_address_get_gateway (addr)); - has_def_route = TRUE; } } ifnet_set_data (conn_name, "config", ips->str); diff --git a/src/settings/plugins/ifupdown/parser.c b/src/settings/plugins/ifupdown/parser.c index f916fa0e78..8dc37dcd7d 100644 --- a/src/settings/plugins/ifupdown/parser.c +++ b/src/settings/plugins/ifupdown/parser.c @@ -486,7 +486,9 @@ update_ip4_setting_from_if_block(NMConnection *connection, gateway_v = address_v; /* dcbw: whaaa?? */ /* Add the new address to the setting */ - addr = nm_ip_address_new (AF_INET, address_v, netmask_int, gateway_v, error); + addr = nm_ip_address_new (AF_INET, address_v, netmask_int, + nm_setting_ip_config_get_num_addresses (s_ip4) == 0 ? gateway_v : NULL, + error); if (!addr) goto error; @@ -599,7 +601,9 @@ update_ip6_setting_from_if_block(NMConnection *connection, gateway_v = address_v; /* dcbw: whaaa?? */ /* Add the new address to the setting */ - addr = nm_ip_address_new (AF_INET6, address_v, prefix_int, gateway_v, error); + addr = nm_ip_address_new (AF_INET6, address_v, prefix_int, + nm_setting_ip_config_get_num_addresses (s_ip6) == 0 ? gateway_v : NULL, + error); if (!addr) goto error; diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c index 526831a32e..46c80552e9 100644 --- a/src/settings/plugins/keyfile/reader.c +++ b/src/settings/plugins/keyfile/reader.c @@ -360,11 +360,14 @@ ip_address_or_route_parser (NMSetting *setting, const char *key, GKeyFile *keyfi key_name = g_strdup (*key_basename); item = read_one_ip_address_or_route (keyfile, setting_name, key_name, ipv6, routes); + g_free (key_name); - if (item) - g_ptr_array_add (list, item); + if (!item) + continue; - g_free (key_name); + if (!routes && list->len > 0) + nm_ip_address_set_gateway (item, NULL); + g_ptr_array_add (list, item); } } diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index 89504b38a6..9cc98615ff 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -247,10 +247,10 @@ test_read_valid_wired_connection (void) /* IPv4 addresses */ g_assert (nm_setting_ip_config_get_num_addresses (s_ip4) == 6); check_ip_address (s_ip4, 0, "2.3.4.5", 24, "2.3.4.6"); - check_ip_address (s_ip4, 1, "192.168.0.5", 24, "192.168.0.1"); - check_ip_address (s_ip4, 2, "1.2.3.4", 16, "1.2.1.1"); + check_ip_address (s_ip4, 1, "192.168.0.5", 24, NULL); + check_ip_address (s_ip4, 2, "1.2.3.4", 16, NULL); check_ip_address (s_ip4, 3, "3.4.5.6", 16, NULL); - check_ip_address (s_ip4, 4, "4.5.6.7", 24, "1.2.3.4"); + check_ip_address (s_ip4, 4, "4.5.6.7", 24, NULL); check_ip_address (s_ip4, 5, "5.6.7.8", 24, NULL); /* IPv4 routes */ @@ -337,7 +337,7 @@ test_read_valid_wired_connection (void) check_ip_address (s_ip6, 6, "3:4:5:6:7:8:9:16", 66, NULL); check_ip_address (s_ip6, 7, "3:4:5:6:7:8:9:17", 67, NULL); check_ip_address (s_ip6, 8, "3:4:5:6:7:8:9:18", 68, NULL); - check_ip_address (s_ip6, 9, "3:4:5:6:7:8:9:19", 69, "1::9"); + check_ip_address (s_ip6, 9, "3:4:5:6:7:8:9:19", 69, NULL); /* Route #1 */ g_assert (nm_setting_ip_config_get_num_routes (s_ip6) == 7); @@ -406,7 +406,6 @@ test_write_wired_connection (void) const char *address1 = "192.168.0.5"; const char *address1_gw = "192.168.0.1"; const char *address2 = "1.2.3.4"; - const char *address2_gw = "1.2.1.1"; const char *route1 = "10.10.10.2"; const char *route1_nh = "10.10.10.1"; const char *route2 = "1.1.1.1"; @@ -467,7 +466,7 @@ test_write_wired_connection (void) /* Addresses */ add_one_ip_address (s_ip4, address1, 24, address1_gw); - add_one_ip_address (s_ip4, address2, 8, address2_gw); + add_one_ip_address (s_ip4, address2, 8, NULL); /* Routes */ add_one_ip_route (s_ip4, route1, route1_nh, 24, 3); diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c index fdf4823a51..e6515398e2 100644 --- a/src/settings/plugins/keyfile/writer.c +++ b/src/settings/plugins/keyfile/writer.c @@ -147,7 +147,10 @@ write_ip_values (GKeyFile *file, addr = nm_ip_address_get_address (address); plen = nm_ip_address_get_prefix (address); - gw = nm_ip_address_get_gateway (address); + if (i == 0) + gw = nm_ip_address_get_gateway (address); + else + gw = NULL; metric = 0; } -- cgit v1.2.1 From f17699f4e3dacb9358a8503c8b15efe3cb852b48 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Mon, 20 Oct 2014 21:30:56 -0400 Subject: libnm-core: add NMSettingIPConfig:gateway, drop NMIPAddress:gateway MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The gateway is a global property of the IPv4/IPv6 configuration, not an attribute of any particular address. So represent it as such in the API; remove the gateway from NMIPAddress, and add it to NMSettingIPConfig. Behind the scenes, the gateway is still serialized along with the first address in NMSettingIPConfig:addresses, and is deserialized from that if the settings dictionary doesn't contain a 'gateway' key. Adjust nmcli's interactive mode to prompt for IP addresses and gateway separately. (Patch partly from Jirka Klimeš.) --- callouts/nm-dispatcher-utils.c | 28 ++- callouts/tests/test-dispatcher-envp.c | 15 +- clients/cli/common.c | 23 +- clients/cli/common.h | 2 +- clients/cli/connections.c | 121 +++++++++-- clients/cli/settings.c | 238 ++++++++++----------- clients/tui/nm-editor-bindings.c | 112 +--------- clients/tui/nm-editor-bindings.h | 6 - clients/tui/nmt-page-ip4.c | 7 +- clients/tui/nmt-page-ip6.c | 7 +- libnm-core/nm-setting-ip-config.c | 130 ++++++----- libnm-core/nm-setting-ip-config.h | 8 +- libnm-core/nm-setting-ip4-config.c | 23 +- libnm-core/nm-setting-ip6-config.c | 57 +++-- libnm-core/nm-utils.c | 83 ++++--- libnm-core/nm-utils.h | 12 +- libnm-core/tests/test-general.c | 152 ++++++++++++- libnm/libnm.ver | 3 +- libnm/nm-ip4-config.c | 2 +- libnm/nm-ip6-config.c | 2 +- src/nm-ip4-config.c | 20 +- src/nm-ip6-config.c | 22 +- src/settings/plugins/ibft/reader.c | 4 +- src/settings/plugins/ibft/tests/test-ibft.c | 6 +- src/settings/plugins/ifcfg-rh/reader.c | 101 ++++----- .../plugins/ifcfg-rh/tests/test-ifcfg-rh.c | 85 +++++--- src/settings/plugins/ifcfg-rh/writer.c | 15 +- src/settings/plugins/ifnet/connection_parser.c | 10 +- src/settings/plugins/ifupdown/parser.c | 42 ++-- src/settings/plugins/keyfile/reader.c | 77 ++++--- src/settings/plugins/keyfile/tests/test-keyfile.c | 80 ++++--- src/settings/plugins/keyfile/writer.c | 28 ++- src/tests/test-general.c | 6 +- 33 files changed, 899 insertions(+), 628 deletions(-) diff --git a/callouts/nm-dispatcher-utils.c b/callouts/nm-dispatcher-utils.c index d2deb41fdf..8feec72d25 100644 --- a/callouts/nm-dispatcher-utils.c +++ b/callouts/nm-dispatcher-utils.c @@ -92,7 +92,7 @@ static GSList * construct_ip4_items (GSList *items, GVariant *ip4_config, const char *prefix) { GPtrArray *addresses, *routes; - char **dns, **wins; + char **dns, **wins, *gateway; GString *tmp; GVariant *val; int i; @@ -106,26 +106,24 @@ construct_ip4_items (GSList *items, GVariant *ip4_config, const char *prefix) /* IP addresses */ val = g_variant_lookup_value (ip4_config, "addresses", G_VARIANT_TYPE ("aau")); if (val) { - addresses = nm_utils_ip4_addresses_from_variant (val); + addresses = nm_utils_ip4_addresses_from_variant (val, &gateway); + if (!gateway) + gateway = g_strdup ("0.0.0.0"); for (i = 0; i < addresses->len; i++) { NMIPAddress *addr = addresses->pdata[i]; - const char *gw; char *addrtmp; - gw = nm_ip_address_get_gateway (addr); - if (!gw) - gw = "0.0.0.0"; - addrtmp = g_strdup_printf ("%sIP4_ADDRESS_%d=%s/%d %s", prefix, i, nm_ip_address_get_address (addr), nm_ip_address_get_prefix (addr), - gw); + gateway); items = g_slist_prepend (items, addrtmp); } if (addresses->len) items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ADDRESSES=%d", prefix, addresses->len)); g_ptr_array_unref (addresses); + g_free (gateway); g_variant_unref (val); } @@ -228,7 +226,7 @@ static GSList * construct_ip6_items (GSList *items, GVariant *ip6_config, const char *prefix) { GPtrArray *addresses, *routes; - char **dns; + char **dns, *gateway = NULL; GString *tmp; GVariant *val; int i; @@ -242,26 +240,24 @@ construct_ip6_items (GSList *items, GVariant *ip6_config, const char *prefix) /* IP addresses */ val = g_variant_lookup_value (ip6_config, "addresses", G_VARIANT_TYPE ("a(ayuay)")); if (val) { - addresses = nm_utils_ip6_addresses_from_variant (val); + addresses = nm_utils_ip6_addresses_from_variant (val, &gateway); + if (!gateway) + gateway = g_strdup ("::"); for (i = 0; i < addresses->len; i++) { NMIPAddress *addr = addresses->pdata[i]; - const char *gw; char *addrtmp; - gw = nm_ip_address_get_gateway (addr); - if (!gw) - gw = "::"; - addrtmp = g_strdup_printf ("%sIP6_ADDRESS_%d=%s/%d %s", prefix, i, nm_ip_address_get_address (addr), nm_ip_address_get_prefix (addr), - gw); + gateway); items = g_slist_prepend (items, addrtmp); } if (addresses->len) items = g_slist_prepend (items, g_strdup_printf ("%sIP6_NUM_ADDRESSES=%d", prefix, addresses->len)); g_ptr_array_unref (addresses); + g_free (gateway); g_variant_unref (val); } diff --git a/callouts/tests/test-dispatcher-envp.c b/callouts/tests/test-dispatcher-envp.c index 5daf5e9a64..423d29fd83 100644 --- a/callouts/tests/test-dispatcher-envp.c +++ b/callouts/tests/test-dispatcher-envp.c @@ -183,6 +183,7 @@ parse_ip4 (GKeyFile *kf, GVariant **out_props, const char *section, GError **err char *tmp; char **split, **iter; GPtrArray *addresses, *routes; + const char *gateway = NULL; g_variant_builder_init (&props, G_VARIANT_TYPE ("a{sv}")); @@ -221,7 +222,7 @@ parse_ip4 (GKeyFile *kf, GVariant **out_props, const char *section, GError **err addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_address_unref); for (iter = split; iter && *iter; iter++) { NMIPAddress *addr; - char *ip, *prefix, *gw; + char *ip, *prefix; if (strlen (g_strstrip (*iter)) == 0) continue; @@ -232,11 +233,13 @@ parse_ip4 (GKeyFile *kf, GVariant **out_props, const char *section, GError **err g_assert (prefix); *prefix++ = '\0'; - gw = strchr (prefix, ' '); - g_assert (gw); - gw++; + if (addresses->len == 0) { + gateway = strchr (prefix, ' '); + g_assert (gateway); + gateway++; + } - addr = nm_ip_address_new (AF_INET, ip, (guint) atoi (prefix), gw, error); + addr = nm_ip_address_new (AF_INET, ip, (guint) atoi (prefix), error); if (!addr) { g_ptr_array_unref (addresses); return FALSE; @@ -245,7 +248,7 @@ parse_ip4 (GKeyFile *kf, GVariant **out_props, const char *section, GError **err } g_variant_builder_add (&props, "{sv}", "addresses", - nm_utils_ip4_addresses_to_variant (addresses)); + nm_utils_ip4_addresses_to_variant (addresses, gateway)); g_ptr_array_unref (addresses); } g_strfreev (split); diff --git a/clients/cli/common.c b/clients/cli/common.c index e7382b985b..2c56976a26 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -82,6 +82,7 @@ print_ip4_config (NMIP4Config *cfg4, const char *one_field) { GPtrArray *ptr_array; + const char *gw; char **addr_arr = NULL; char **route_arr = NULL; char **dns_arr = NULL; @@ -103,20 +104,16 @@ print_ip4_config (NMIP4Config *cfg4, /* addresses */ ptr_array = nm_ip4_config_get_addresses (cfg4); + gw = nm_ip4_config_get_gateway (cfg4); if (ptr_array) { addr_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { NMIPAddress *addr = (NMIPAddress *) g_ptr_array_index (ptr_array, i); - const char *gw; - - gw = nm_ip_address_get_gateway (addr); - if (!gw) - gw = "0.0.0.0"; addr_arr[i] = g_strdup_printf ("ip = %s/%u, gw = %s", nm_ip_address_get_address (addr), nm_ip_address_get_prefix (addr), - gw); + (i == 0) && gw ? gw : "0.0.0.0"); } addr_arr[i] = NULL; } @@ -175,6 +172,7 @@ print_ip6_config (NMIP6Config *cfg6, const char *one_field) { GPtrArray *ptr_array; + const char *gw; char **addr_arr = NULL; char **route_arr = NULL; char **dns_arr = NULL; @@ -195,20 +193,16 @@ print_ip6_config (NMIP6Config *cfg6, /* addresses */ ptr_array = nm_ip6_config_get_addresses (cfg6); + gw = nm_ip6_config_get_gateway (cfg6); if (ptr_array) { addr_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { NMIPAddress *addr = (NMIPAddress *) g_ptr_array_index (ptr_array, i); - const char *gw; - - gw = nm_ip_address_get_gateway (addr); - if (!gw) - gw = "::"; addr_arr[i] = g_strdup_printf ("ip = %s/%u, gw = %s", nm_ip_address_get_address (addr), nm_ip_address_get_prefix (addr), - gw); + (i == 0) && gw ? gw : "::"); } addr_arr[i] = NULL; } @@ -355,10 +349,9 @@ print_dhcp6_config (NMDhcp6Config *dhcp6, /* * Parse IP address from string to NMIPAddress stucture. * ip_str is the IP address in the form address/prefix - * gw_str is the gateway address (it is optional) */ NMIPAddress * -nmc_parse_and_build_address (int family, const char *ip_str, const char *gw_str, GError **error) +nmc_parse_and_build_address (int family, const char *ip_str, GError **error) { int max_prefix = (family == AF_INET) ? 32 : 128; NMIPAddress *addr = NULL; @@ -387,7 +380,7 @@ nmc_parse_and_build_address (int family, const char *ip_str, const char *gw_str, } } - addr = nm_ip_address_new (family, ip, (guint32) prefix, gw_str, &local); + addr = nm_ip_address_new (family, ip, (guint32) prefix, &local); if (!addr) { g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, _("invalid IP address: %s"), local->message); diff --git a/clients/cli/common.h b/clients/cli/common.h index d608ef5ce0..bf881bf2f4 100644 --- a/clients/cli/common.h +++ b/clients/cli/common.h @@ -29,7 +29,7 @@ gboolean print_ip6_config (NMIP6Config *cfg6, NmCli *nmc, const char *group_pref gboolean print_dhcp4_config (NMDhcp4Config *dhcp4, NmCli *nmc, const char *group_prefix, const char *one_field); gboolean print_dhcp6_config (NMDhcp6Config *dhcp6, NmCli *nmc, const char *group_prefix, const char *one_field); -NMIPAddress *nmc_parse_and_build_address (int family, const char *ip_str, const char *gw_str, GError **error); +NMIPAddress *nmc_parse_and_build_address (int family, const char *ip_str, GError **error); NMIPRoute *nmc_parse_and_build_route (int family, const char *first, const char *second, const char *third, GError **error); const char * nmc_device_state_to_string (NMDeviceState state); diff --git a/clients/cli/connections.c b/clients/cli/connections.c index c131054168..969adb80d0 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -3811,24 +3811,21 @@ do_questionnaire_olpc (char **channel, char **dhcp_anycast) } static gboolean -split_address (char* str, char **ip, char **gw, char **rest) +split_address (char* str, char **ip, char **rest) { - size_t n1, n2, n3, n4, n5; + size_t n1, n2, n3; - *ip = *gw = *rest = NULL; + *ip = *rest = NULL; if (!str) return FALSE; n1 = strspn (str, " \t"); n2 = strcspn (str+n1, " \t\0") + n1; n3 = strspn (str+n2, " \t") + n2; - n4 = strcspn (str+n3, " \t\0") + n3; - n5 = strspn (str+n4, " \t") + n4; - str[n2] = str[n4] = '\0'; + str[n2] = '\0'; *ip = str[n1] ? str + n1 : NULL; - *gw = str[n3] ? str + n3 : NULL; - *rest = str[n5] ? str + n5 : NULL; + *rest = str[n3] ? str + n3 : NULL; return TRUE; } @@ -3838,32 +3835,31 @@ ask_for_ip_addresses (NMConnection *connection, int family) { gboolean ip_loop; GError *error = NULL; - char *str, *ip, *gw, *rest; + char *str, *ip, *rest; const char *prompt; gboolean added; NMIPAddress *ipaddr; if (family == AF_INET) - prompt =_("IPv4 address (IP[/plen] [gateway]) [none]: "); + prompt =_("IPv4 address (IP[/plen]) [none]: "); else - prompt =_("IPv6 address (IP[/plen] [gateway]) [none]: "); + prompt =_("IPv6 address (IP[/plen]) [none]: "); ip_loop = TRUE; do { str = nmc_readline ("%s", prompt); - split_address (str, &ip, &gw, &rest); + split_address (str, &ip, &rest); if (ip) { - ipaddr = nmc_parse_and_build_address (family, ip, gw, &error); + ipaddr = nmc_parse_and_build_address (family, ip, &error); if (ipaddr) { if (family == AF_INET) added = add_ip4_address_to_connection (ipaddr, connection); else added = add_ip6_address_to_connection (ipaddr, connection); - gw = gw ? gw : (family == AF_INET) ? "0.0.0.0" : "::"; if (added) - g_print (_(" Address successfully added: %s %s\n"), ip, gw); + g_print (_(" Address successfully added: %s\n"), ip); else - g_print (_(" Warning: address already present: %s %s\n"), ip, gw); + g_print (_(" Warning: address already present: %s\n"), ip); if (rest) g_print (_(" Warning: ignoring garbage at the end: '%s'\n"), rest); } else { @@ -3878,6 +3874,45 @@ ask_for_ip_addresses (NMConnection *connection, int family) } while (ip_loop); } +static void +maybe_ask_for_gateway (NMConnection *connection, int family) +{ + gboolean gw_loop; + char *str, *gw, *rest; + const char *prompt; + NMSettingIPConfig *s_ip; + + if (family == AF_INET) { + prompt =_("IPv4 gateway [none]: "); + s_ip = nm_connection_get_setting_ip4_config (connection); + } else { + prompt =_("IPv6 gateway [none]: "); + s_ip = nm_connection_get_setting_ip6_config (connection); + } + if (s_ip == NULL) + return; + if ( nm_setting_ip_config_get_num_addresses (s_ip) == 0 + || nm_setting_ip_config_get_gateway (s_ip) != NULL) + return; + + gw_loop = TRUE; + do { + str = nmc_readline ("%s", prompt); + split_address (str, &gw, &rest); + if (gw) { + if (nm_utils_ipaddr_valid (family, gw)) { + g_object_set (s_ip, + NM_SETTING_IP_CONFIG_GATEWAY, gw, + NULL); + gw_loop = FALSE; + } else + g_print (_("Error: invalid gateway address '%s'\n"), gw); + } else + gw_loop = FALSE; + g_free (str); + } while (gw_loop); +} + static void do_questionnaire_ip (NMConnection *connection) { @@ -3890,14 +3925,14 @@ do_questionnaire_ip (NMConnection *connection) g_free (answer); return; } + g_free (answer); g_print (_("Press to finish adding addresses.\n")); ask_for_ip_addresses (connection, AF_INET); + maybe_ask_for_gateway (connection, AF_INET); ask_for_ip_addresses (connection, AF_INET6); - - g_free (answer); - return; + maybe_ask_for_gateway (connection, AF_INET6); } static gboolean @@ -5168,7 +5203,7 @@ cleanup_olpc: /* coverity[dead_error_begin] */ if (ip4) { - ip4addr = nmc_parse_and_build_address (AF_INET, ip4, gw4, error); + ip4addr = nmc_parse_and_build_address (AF_INET, ip4, error); if (!ip4addr) { g_prefix_error (error, _("Error: ")); return FALSE; @@ -5176,15 +5211,59 @@ cleanup_olpc: add_ip4_address_to_connection (ip4addr, connection); } + if (gw4) { + NMSettingIPConfig *s_ip = nm_connection_get_setting_ip4_config (connection); + + if (!s_ip) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("Error: IPv4 gateway specified without IPv4 addresses")); + return FALSE; + } else if (nm_setting_ip_config_get_gateway (s_ip)) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("Error: multiple IPv4 gateways specified")); + return FALSE; + } else if (!nm_utils_ipaddr_valid (AF_INET, gw4)) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("Error: Invalid IPv4 gateway '%s'"), + gw4); + } + + g_object_set (s_ip, + NM_SETTING_IP_CONFIG_GATEWAY, gw4, + NULL); + } + /* coverity[dead_error_begin] */ if (ip6) { - ip6addr = nmc_parse_and_build_address (AF_INET6, ip6, gw6, error); + ip6addr = nmc_parse_and_build_address (AF_INET6, ip6, error); if (!ip6addr) { g_prefix_error (error, _("Error: ")); return FALSE; } add_ip6_address_to_connection (ip6addr, connection); } + + if (gw6) { + NMSettingIPConfig *s_ip = nm_connection_get_setting_ip6_config (connection); + + if (!s_ip) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("Error: IPv6 gateway specified without IPv6 addresses")); + return FALSE; + } else if (nm_setting_ip_config_get_gateway (s_ip)) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("Error: multiple IPv6 gateways specified")); + return FALSE; + } else if (!nm_utils_ipaddr_valid (AF_INET, gw6)) { + g_set_error (error, NMCLI_ERROR, NMC_RESULT_ERROR_USER_INPUT, + _("Error: Invalid IPv6 gateway '%s'"), + gw6); + } + + g_object_set (s_ip, + NM_SETTING_IP_CONFIG_GATEWAY, gw6, + NULL); + } } /* Ask for addresses if '--ask' is specified. */ diff --git a/clients/cli/settings.c b/clients/cli/settings.c index cb3dccf48e..292a09b52a 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -257,14 +257,15 @@ NmcOutputField nmc_fields_setting_ip4_config[] = { SETTING_FIELD (NM_SETTING_IP_CONFIG_DNS, 20), /* 2 */ SETTING_FIELD (NM_SETTING_IP_CONFIG_DNS_SEARCH, 15), /* 3 */ SETTING_FIELD (NM_SETTING_IP_CONFIG_ADDRESSES, 20), /* 4 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_ROUTES, 20), /* 5 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, 19), /* 6 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, 16), /* 7 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, 15), /* 8 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, 19), /* 9 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, 14), /* 10 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_NEVER_DEFAULT, 15), /* 11 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_MAY_FAIL, 12), /* 12 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_GATEWAY, 20), /* 5 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_ROUTES, 20), /* 7 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, 19), /* 7 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, 16), /* 8 */ + SETTING_FIELD (NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, 15), /* 9 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, 19), /* 10 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, 14), /* 11 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_NEVER_DEFAULT, 15), /* 12 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_MAY_FAIL, 12), /* 13 */ {NULL, NULL, 0, NULL, FALSE, FALSE, 0} }; #define NMC_FIELDS_SETTING_IP4_CONFIG_ALL "name"","\ @@ -272,6 +273,7 @@ NmcOutputField nmc_fields_setting_ip4_config[] = { NM_SETTING_IP_CONFIG_DNS","\ NM_SETTING_IP_CONFIG_DNS_SEARCH","\ NM_SETTING_IP_CONFIG_ADDRESSES","\ + NM_SETTING_IP_CONFIG_GATEWAY","\ NM_SETTING_IP_CONFIG_ROUTES","\ NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES","\ NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS","\ @@ -289,14 +291,15 @@ NmcOutputField nmc_fields_setting_ip6_config[] = { SETTING_FIELD (NM_SETTING_IP_CONFIG_DNS, 20), /* 2 */ SETTING_FIELD (NM_SETTING_IP_CONFIG_DNS_SEARCH, 15), /* 3 */ SETTING_FIELD (NM_SETTING_IP_CONFIG_ADDRESSES, 20), /* 4 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_ROUTES, 20), /* 5 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, 19), /* 6 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, 16), /* 7 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_NEVER_DEFAULT, 15), /* 8 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_MAY_FAIL, 12), /* 9 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_IP6_PRIVACY, 15), /* 10 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, 19), /* 11 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, 14), /* 12 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_GATEWAY, 20), /* 5 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_ROUTES, 20), /* 6 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, 19), /* 7 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, 16), /* 8 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_NEVER_DEFAULT, 15), /* 9 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_MAY_FAIL, 12), /* 10 */ + SETTING_FIELD (NM_SETTING_IP6_CONFIG_IP6_PRIVACY, 15), /* 11 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, 19), /* 12 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, 14), /* 13 */ {NULL, NULL, 0, NULL, FALSE, FALSE, 0} }; #define NMC_FIELDS_SETTING_IP6_CONFIG_ALL "name"","\ @@ -304,6 +307,7 @@ NmcOutputField nmc_fields_setting_ip6_config[] = { NM_SETTING_IP_CONFIG_DNS","\ NM_SETTING_IP_CONFIG_DNS_SEARCH","\ NM_SETTING_IP_CONFIG_ADDRESSES","\ + NM_SETTING_IP_CONFIG_GATEWAY","\ NM_SETTING_IP_CONFIG_ROUTES","\ NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES","\ NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS","\ @@ -1207,34 +1211,25 @@ DEFINE_GETTER (nmc_property_ipv4_get_dns, NM_SETTING_IP_CONFIG_DNS) DEFINE_GETTER (nmc_property_ipv4_get_dns_search, NM_SETTING_IP_CONFIG_DNS_SEARCH) static char * -nmc_property_ipv4_get_addresses (NMSetting *setting) +nmc_property_ip_get_addresses (NMSetting *setting) { - NMSettingIPConfig *s_ip4 = NM_SETTING_IP_CONFIG (setting); + NMSettingIPConfig *s_ip = NM_SETTING_IP_CONFIG (setting); GString *printable; guint32 num_addresses, i; NMIPAddress *addr; printable = g_string_new (NULL); - num_addresses = nm_setting_ip_config_get_num_addresses (s_ip4); + num_addresses = nm_setting_ip_config_get_num_addresses (s_ip); for (i = 0; i < num_addresses; i++) { - addr = nm_setting_ip_config_get_address (s_ip4, i); + addr = nm_setting_ip_config_get_address (s_ip, i); if (printable->len > 0) - g_string_append (printable, "; "); - - g_string_append (printable, "{ "); + g_string_append (printable, ", "); - g_string_append_printf (printable, "ip = %s/%u", + g_string_append_printf (printable, "%s/%u", nm_ip_address_get_address (addr), nm_ip_address_get_prefix (addr)); - - if (nm_ip_address_get_gateway (addr)) { - g_string_append_printf (printable, ", gw = %s", - nm_ip_address_get_gateway (addr)); - } - - g_string_append (printable, " }"); } return g_string_free (printable, FALSE); @@ -1277,6 +1272,7 @@ nmc_property_ipv4_get_routes (NMSetting *setting) return g_string_free (printable, FALSE); } +DEFINE_GETTER (nmc_property_ipv4_get_gateway, NM_SETTING_IP_CONFIG_GATEWAY) DEFINE_GETTER (nmc_property_ipv4_get_ignore_auto_routes, NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES) DEFINE_GETTER (nmc_property_ipv4_get_ignore_auto_dns, NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS) DEFINE_GETTER (nmc_property_ipv4_get_dhcp_client_id, NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID) @@ -1290,40 +1286,6 @@ DEFINE_GETTER (nmc_property_ipv6_get_method, NM_SETTING_IP_CONFIG_METHOD) DEFINE_GETTER (nmc_property_ipv6_get_dns, NM_SETTING_IP_CONFIG_DNS) DEFINE_GETTER (nmc_property_ipv6_get_dns_search, NM_SETTING_IP_CONFIG_DNS_SEARCH) -static char * -nmc_property_ipv6_get_addresses (NMSetting *setting) -{ - NMSettingIPConfig *s_ip6 = NM_SETTING_IP_CONFIG (setting); - GString *printable; - guint32 num_addresses, i; - NMIPAddress *addr; - - printable = g_string_new (NULL); - - num_addresses = nm_setting_ip_config_get_num_addresses (s_ip6); - for (i = 0; i < num_addresses; i++) { - addr = nm_setting_ip_config_get_address (s_ip6, i); - - if (printable->len > 0) - g_string_append (printable, "; "); - - g_string_append (printable, "{ "); - - g_string_append_printf (printable, "ip = %s/%u", - nm_ip_address_get_address (addr), - nm_ip_address_get_prefix (addr)); - - if (nm_ip_address_get_gateway (addr)) { - g_string_append_printf (printable, ", gw = %s", - nm_ip_address_get_gateway (addr)); - } - - g_string_append (printable, " }"); - } - - return g_string_free (printable, FALSE); -} - static char * nmc_property_ipv6_get_routes (NMSetting *setting) { @@ -1361,6 +1323,7 @@ nmc_property_ipv6_get_routes (NMSetting *setting) return g_string_free (printable, FALSE); } +DEFINE_GETTER (nmc_property_ipv6_get_gateway, NM_SETTING_IP_CONFIG_GATEWAY) DEFINE_GETTER (nmc_property_ipv6_get_ignore_auto_routes, NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES) DEFINE_GETTER (nmc_property_ipv6_get_ignore_auto_dns, NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS) DEFINE_GETTER (nmc_property_ipv6_get_never_default, NM_SETTING_IP_CONFIG_NEVER_DEFAULT) @@ -3075,20 +3038,10 @@ static NMIPAddress * _parse_ip_address (int family, const char *address, GError **error) { char *value = g_strdup (address); - char **addrv; NMIPAddress *ipaddr; - addrv = nmc_strsplit_set (g_strstrip (value), " \t", 0); - if (addrv[0] == NULL || g_strv_length (addrv) > 2) { - g_set_error (error, 1, 0, _("'%s' is not valid (use ip[/prefix] [gateway])"), - address); - g_free (value); - g_strfreev (addrv); - return NULL; - } - ipaddr = nmc_parse_and_build_address (family, addrv[0], addrv[1], error); + ipaddr = nmc_parse_and_build_address (family, g_strstrip (value), error); g_free (value); - g_strfreev (addrv); return ipaddr; } @@ -3292,44 +3245,31 @@ static const char * nmc_property_ipv4_describe_addresses (NMSetting *setting, const char *prop) { return _("Enter a list of IPv4 addresses formatted as:\n" - " ip[/prefix] [gateway], ip[/prefix] [gateway],...\n" + " ip[/prefix], ip[/prefix],...\n" "Missing prefix is regarded as prefix of 32.\n\n" - "Example: 192.168.1.5/24 192.168.1.1, 10.0.0.11/24\n"); + "Example: 192.168.1.5/24, 10.0.0.11/24\n"); } -/* - * from: { ip = 1.2.3.4/24, gw = 1.2.3.254 }; { ip = 2.2.2.2/16, gw = 5.5.5.5 } - * to: 1.2.3.4/24 1.2.3.254, 2.2.2.2/16 5.5.5.5 - * from: { ip = 11::22/64, gw = 22::33 }; { ip = ab::cd/64, gw = ab::1 } - * to: 11::22/64 22:33, ab::cd/64 ab::1 -*/ -static char * -nmc_property_out2in_addresses (const char *out_format) +/* 'gateway' */ +static gboolean +nmc_property_ipv4_set_gateway (NMSetting *setting, const char *prop, const char *val, GError **error) { - GRegex *regex; - GString *str; - char **strv; - int i; + NMIPAddress *ip4addr; - str = g_string_sized_new (128); - regex = g_regex_new ("\\{ ip = ([^/]+)/([^,]+), gw = ([^ ]+) \\}", 0, 0, NULL); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - strv = g_regex_split (regex, out_format, 0); - for (i = 1; strv && strv[i] && strv[i+1] && strv[i+2]; i=i+4) { - g_string_append (str, strv[i]); /* IP */ - g_string_append_c (str, '/'); - g_string_append (str, strv[i+1]); /* prefix */ - g_string_append_c (str, ' '); - g_string_append (str, strv[i+2]); /* gateway */ - g_string_append (str, ", "); + if (strchr (val, '/')) { + g_set_error (error, 1, 0, + _("invalid gateway address '%s'"), val); + return FALSE; } - if (str->len > 0) - g_string_truncate (str, str->len - 2); - - g_strfreev (strv); - g_regex_unref (regex); + ip4addr = _parse_ipv4_address (val, error); + if (!ip4addr) + return FALSE; - return g_string_free (str, FALSE); + g_object_set (setting, prop, val, NULL); + nm_ip_address_unref (ip4addr); + return TRUE; } /* 'routes' */ @@ -3611,9 +3551,31 @@ static const char * nmc_property_ipv6_describe_addresses (NMSetting *setting, const char *prop) { return _("Enter a list of IPv6 addresses formatted as:\n" - " ip[/prefix] [gateway], ip[/prefix] [gateway],...\n" + " ip[/prefix], ip[/prefix],...\n" "Missing prefix is regarded as prefix of 128.\n\n" - "Example: 2607:f0d0:1002:51::4/64 2607:f0d0:1002:51::1, 1050:0:0:0:5:600:300c:326b\n"); + "Example: 2607:f0d0:1002:51::4/64, 1050:0:0:0:5:600:300c:326b\n"); +} + +/* 'gateway' */ +static gboolean +nmc_property_ipv6_set_gateway (NMSetting *setting, const char *prop, const char *val, GError **error) +{ + NMIPAddress *ip6addr; + + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + if (strchr (val, '/')) { + g_set_error (error, 1, 0, + _("invalid gateway address '%s'"), val); + return FALSE; + } + ip6addr = _parse_ipv6_address (val, error); + if (!ip6addr) + return FALSE; + + g_object_set (setting, prop, val, NULL); + nm_ip_address_unref (ip6addr); + return TRUE; } /* 'routes' */ @@ -5448,12 +5410,19 @@ nmc_properties_init (void) NULL, NULL); nmc_add_prop_funcs (GLUE_IP (4, ADDRESSES), - nmc_property_ipv4_get_addresses, + nmc_property_ip_get_addresses, nmc_property_ipv4_set_addresses, nmc_property_ipv4_remove_addresses, nmc_property_ipv4_describe_addresses, NULL, - nmc_property_out2in_addresses); + NULL); + nmc_add_prop_funcs (GLUE_IP (4, GATEWAY), + nmc_property_ipv4_get_gateway, + nmc_property_ipv4_set_gateway, + NULL, + NULL, + NULL, + NULL); nmc_add_prop_funcs (GLUE_IP (4, ROUTES), nmc_property_ipv4_get_routes, nmc_property_ipv4_set_routes, @@ -5534,12 +5503,19 @@ nmc_properties_init (void) NULL, NULL); nmc_add_prop_funcs (GLUE_IP (6, ADDRESSES), - nmc_property_ipv6_get_addresses, + nmc_property_ip_get_addresses, nmc_property_ipv6_set_addresses, nmc_property_ipv6_remove_addresses, nmc_property_ipv6_describe_addresses, NULL, - nmc_property_out2in_addresses); + NULL); + nmc_add_prop_funcs (GLUE_IP (6, GATEWAY), + nmc_property_ipv6_get_gateway, + nmc_property_ipv6_set_gateway, + NULL, + NULL, + NULL, + NULL); nmc_add_prop_funcs (GLUE_IP (6, ROUTES), nmc_property_ipv6_get_routes, nmc_property_ipv6_set_routes, @@ -6746,15 +6722,16 @@ setting_ip4_config_details (NMSetting *setting, NmCli *nmc, const char *one_pro set_val_str (arr, 1, nmc_property_ipv4_get_method (setting)); set_val_str (arr, 2, nmc_property_ipv4_get_dns (setting)); set_val_str (arr, 3, nmc_property_ipv4_get_dns_search (setting)); - set_val_str (arr, 4, nmc_property_ipv4_get_addresses (setting)); - set_val_str (arr, 5, nmc_property_ipv4_get_routes (setting)); - set_val_str (arr, 6, nmc_property_ipv4_get_ignore_auto_routes (setting)); - set_val_str (arr, 7, nmc_property_ipv4_get_ignore_auto_dns (setting)); - set_val_str (arr, 8, nmc_property_ipv4_get_dhcp_client_id (setting)); - set_val_str (arr, 9, nmc_property_ipv4_get_dhcp_send_hostname (setting)); - set_val_str (arr, 10, nmc_property_ipv4_get_dhcp_hostname (setting)); - set_val_str (arr, 11, nmc_property_ipv4_get_never_default (setting)); - set_val_str (arr, 12, nmc_property_ipv4_get_may_fail (setting)); + set_val_str (arr, 4, nmc_property_ip_get_addresses (setting)); + set_val_str (arr, 5, nmc_property_ipv4_get_gateway (setting)); + set_val_str (arr, 6, nmc_property_ipv4_get_routes (setting)); + set_val_str (arr, 7, nmc_property_ipv4_get_ignore_auto_routes (setting)); + set_val_str (arr, 8, nmc_property_ipv4_get_ignore_auto_dns (setting)); + set_val_str (arr, 9, nmc_property_ipv4_get_dhcp_client_id (setting)); + set_val_str (arr, 10, nmc_property_ipv4_get_dhcp_send_hostname (setting)); + set_val_str (arr, 11, nmc_property_ipv4_get_dhcp_hostname (setting)); + set_val_str (arr, 12, nmc_property_ipv4_get_never_default (setting)); + set_val_str (arr, 13, nmc_property_ipv4_get_may_fail (setting)); g_ptr_array_add (nmc->output_data, arr); print_data (nmc); /* Print all data */ @@ -6783,15 +6760,16 @@ setting_ip6_config_details (NMSetting *setting, NmCli *nmc, const char *one_pro set_val_str (arr, 1, nmc_property_ipv6_get_method (setting)); set_val_str (arr, 2, nmc_property_ipv6_get_dns (setting)); set_val_str (arr, 3, nmc_property_ipv6_get_dns_search (setting)); - set_val_str (arr, 4, nmc_property_ipv6_get_addresses (setting)); - set_val_str (arr, 5, nmc_property_ipv6_get_routes (setting)); - set_val_str (arr, 6, nmc_property_ipv6_get_ignore_auto_routes (setting)); - set_val_str (arr, 7, nmc_property_ipv6_get_ignore_auto_dns (setting)); - set_val_str (arr, 8, nmc_property_ipv6_get_never_default (setting)); - set_val_str (arr, 9, nmc_property_ipv6_get_may_fail (setting)); - set_val_str (arr, 10, nmc_property_ipv6_get_ip6_privacy (setting)); - set_val_str (arr, 11, nmc_property_ipv6_get_dhcp_send_hostname (setting)); - set_val_str (arr, 12, nmc_property_ipv6_get_dhcp_hostname (setting)); + set_val_str (arr, 4, nmc_property_ip_get_addresses (setting)); + set_val_str (arr, 5, nmc_property_ipv6_get_gateway (setting)); + set_val_str (arr, 6, nmc_property_ipv6_get_routes (setting)); + set_val_str (arr, 7, nmc_property_ipv6_get_ignore_auto_routes (setting)); + set_val_str (arr, 8, nmc_property_ipv6_get_ignore_auto_dns (setting)); + set_val_str (arr, 9, nmc_property_ipv6_get_never_default (setting)); + set_val_str (arr, 10, nmc_property_ipv6_get_may_fail (setting)); + set_val_str (arr, 11, nmc_property_ipv6_get_ip6_privacy (setting)); + set_val_str (arr, 12, nmc_property_ipv6_get_dhcp_send_hostname (setting)); + set_val_str (arr, 13, nmc_property_ipv6_get_dhcp_hostname (setting)); g_ptr_array_add (nmc->output_data, arr); print_data (nmc); /* Print all data */ diff --git a/clients/tui/nm-editor-bindings.c b/clients/tui/nm-editor-bindings.c index 4c42caad35..9db44c9d80 100644 --- a/clients/tui/nm-editor-bindings.c +++ b/clients/tui/nm-editor-bindings.c @@ -156,7 +156,7 @@ ip_addresses_with_prefix_from_strv (GBinding *binding, int i; strings = g_value_get_boxed (source_value); - /* Fetch the original property value, so as to preserve the gateway elements */ + /* Fetch the original property value, so as to preserve their extra attributes */ g_object_get (g_binding_get_source (binding), g_binding_get_source_property (binding), &addrs, NULL); @@ -164,9 +164,9 @@ ip_addresses_with_prefix_from_strv (GBinding *binding, for (i = 0; strings[i]; i++) { if (i >= addrs->len) { if (family == AF_INET) - addr = nm_ip_address_new (AF_INET, "0.0.0.0", 32, NULL, NULL); + addr = nm_ip_address_new (AF_INET, "0.0.0.0", 32, NULL); else - addr = nm_ip_address_new (AF_INET6, "::", 128, NULL, NULL); + addr = nm_ip_address_new (AF_INET6, "::", 128, NULL); g_ptr_array_add (addrs, addr); } else addr = addrs->pdata[i]; @@ -202,9 +202,7 @@ ip_addresses_with_prefix_from_strv (GBinding *binding, * * Each #NMIPAddress in @source_property will be converted to a string of the * form "ip.ad.dr.ess/prefix" or "ip:ad:dr:ess/prefix" in @target_property (and - * vice versa if %G_BINDING_BIDIRECTIONAL) is specified. The "gateway" fields in - * @source_property are ignored when converting to strings, and unmodified when - * converting from strings. + * vice versa if %G_BINDING_BIDIRECTIONAL) is specified. */ void nm_editor_bind_ip_addresses_with_prefix_to_strv (int family, @@ -274,108 +272,6 @@ nm_editor_bind_ip_addresses_to_strv (int family, GINT_TO_POINTER (family), NULL); } -static gboolean -ip_gateway_to_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - GPtrArray *addrs; - NMIPAddress *addr; - const char *gateway = NULL; - int i; - - addrs = g_value_get_boxed (source_value); - for (i = 0; i < addrs->len; i++) { - addr = addrs->pdata[i]; - gateway = nm_ip_address_get_gateway (addr); - if (gateway) - break; - } - - if (!gateway) - gateway = ""; - g_value_set_string (target_value, gateway); - return TRUE; -} - -static gboolean -ip_gateway_from_string (GBinding *binding, - const GValue *source_value, - GValue *target_value, - gpointer user_data) -{ - int family = GPOINTER_TO_INT (user_data); - const char *text; - GPtrArray *addrs; - NMIPAddress *addr; - int i; - - text = g_value_get_string (source_value); - if (*text) { - if (!nm_utils_ipaddr_valid (family, text)) - return FALSE; - } else - text = NULL; - - /* Fetch the original property value, so as to preserve the IP address elements */ - g_object_get (g_binding_get_source (binding), - g_binding_get_source_property (binding), &addrs, - NULL); - if (!addrs->len) { - g_ptr_array_unref (addrs); - return FALSE; - } - addr = addrs->pdata[0]; - if (!g_strcmp0 (text, nm_ip_address_get_gateway (addr))) { - g_ptr_array_unref (addrs); - return FALSE; - } - nm_ip_address_set_gateway (addr, text); - - for (i = 1; i < addrs->len; i++) { - addr = addrs->pdata[i]; - nm_ip_address_set_gateway (addr, NULL); - } - - g_value_take_boxed (target_value, addrs); - return TRUE; -} - -/** - * nm_editor_bind_ip_gateway_to_string: - * @family: the IP address family - * @source: the source object (eg, an #NMSettingIP4Config) - * @source_property: the property on @source to bind (eg, - * %NM_SETTING_IP4_CONFIG_ADDRESSES) - * @target: the target object (eg, an #NmtNewtEntry) - * @target_property: the property on @target to bind - * (eg, "text") - * @flags: %GBindingFlags - * - * Binds the #GPtrArray-of-#NMIPRoute property @source_property on @source to - * the %G_TYPE_STRING property @target_property on @target. - * - * Specifically, this binds the "gateway" field of the first address in - * @source_property; all other addresses in @source_property are ignored, and - * its "address" and "prefix" fields are unmodified. - */ -void -nm_editor_bind_ip_gateway_to_string (int family, - gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags) -{ - g_object_bind_property_full (source, source_property, - target, target_property, - flags, - ip_gateway_to_string, - ip_gateway_from_string, - GINT_TO_POINTER (family), NULL); -} - static gboolean ip_route_transform_to_dest_string (GBinding *binding, const GValue *source_value, diff --git a/clients/tui/nm-editor-bindings.h b/clients/tui/nm-editor-bindings.h index 5e34c42dec..19a172f344 100644 --- a/clients/tui/nm-editor-bindings.h +++ b/clients/tui/nm-editor-bindings.h @@ -37,12 +37,6 @@ void nm_editor_bind_ip_addresses_to_strv (int family, gpointer target, const gchar *target_property, GBindingFlags flags); -void nm_editor_bind_ip_gateway_to_string (int family, - gpointer source, - const gchar *source_property, - gpointer target, - const gchar *target_property, - GBindingFlags flags); void nm_editor_bind_ip_route_to_strings (int family, gpointer source, diff --git a/clients/tui/nmt-page-ip4.c b/clients/tui/nmt-page-ip4.c index 782a204fc9..b365cc41a2 100644 --- a/clients/tui/nmt-page-ip4.c +++ b/clients/tui/nmt-page-ip4.c @@ -145,10 +145,9 @@ nmt_page_ip4_constructed (GObject *object) nmt_page_grid_append (grid, _("Addresses"), widget, NULL); widget = nmt_ip_entry_new (25, AF_INET, FALSE, TRUE); - nm_editor_bind_ip_gateway_to_string (AF_INET, - s_ip4, NM_SETTING_IP_CONFIG_ADDRESSES, - widget, "text", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + g_object_bind_property (s_ip4, NM_SETTING_IP_CONFIG_GATEWAY, + widget, "text", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Gateway"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP4); diff --git a/clients/tui/nmt-page-ip6.c b/clients/tui/nmt-page-ip6.c index 88b948cd14..1003c1a8aa 100644 --- a/clients/tui/nmt-page-ip6.c +++ b/clients/tui/nmt-page-ip6.c @@ -145,10 +145,9 @@ nmt_page_ip6_constructed (GObject *object) nmt_page_grid_append (grid, _("Addresses"), widget, NULL); widget = nmt_ip_entry_new (25, AF_INET6, FALSE, TRUE); - nm_editor_bind_ip_gateway_to_string (AF_INET6, - s_ip6, NM_SETTING_IP_CONFIG_ADDRESSES, - widget, "text", - G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + g_object_bind_property (s_ip6, NM_SETTING_IP_CONFIG_GATEWAY, + widget, "text", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); nmt_page_grid_append (grid, _("Gateway"), widget, NULL); widget = nmt_address_list_new (NMT_ADDRESS_LIST_IP6); diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c index ebe38712b2..0ed612cd06 100644 --- a/libnm-core/nm-setting-ip-config.c +++ b/libnm-core/nm-setting-ip-config.c @@ -102,7 +102,7 @@ G_DEFINE_BOXED_TYPE (NMIPAddress, nm_ip_address, nm_ip_address_dup, nm_ip_addres struct NMIPAddress { guint refcount; - char *address, *gateway; + char *address; int prefix, family; GHashTable *attributes; @@ -113,7 +113,6 @@ struct NMIPAddress { * @family: the IP address family (%AF_INET or %AF_INET6) * @addr: the IP address * @prefix: the address prefix length - * @gateway: (allow-none): the gateway * @error: location to store error, or %NULL * * Creates a new #NMIPAddress object. @@ -122,7 +121,7 @@ struct NMIPAddress { **/ NMIPAddress * nm_ip_address_new (int family, - const char *addr, guint prefix, const char *gateway, + const char *addr, guint prefix, GError **error) { NMIPAddress *address; @@ -134,8 +133,6 @@ nm_ip_address_new (int family, return NULL; if (!valid_prefix (family, prefix, error)) return NULL; - if (gateway && !valid_ip (family, gateway, error)) - return NULL; address = g_slice_new0 (NMIPAddress); address->refcount = 1; @@ -143,7 +140,6 @@ nm_ip_address_new (int family, address->family = family; address->address = canonicalize_ip (family, addr, FALSE); address->prefix = prefix; - address->gateway = canonicalize_ip (family, gateway, TRUE); return address; } @@ -153,17 +149,16 @@ nm_ip_address_new (int family, * @family: the IP address family (%AF_INET or %AF_INET6) * @addr: the IP address * @prefix: the address prefix length - * @gateway: (allow-none): the gateway * @error: location to store error, or %NULL * - * Creates a new #NMIPAddress object. @addr and @gateway (if non-%NULL) must - * point to buffers of the correct size for @family. + * Creates a new #NMIPAddress object. @addr must point to a buffer of the + * correct size for @family. * * Returns: (transfer full): the new #NMIPAddress object, or %NULL on error **/ NMIPAddress * nm_ip_address_new_binary (int family, - gconstpointer addr, guint prefix, gconstpointer gateway, + gconstpointer addr, guint prefix, GError **error) { NMIPAddress *address; @@ -181,8 +176,6 @@ nm_ip_address_new_binary (int family, address->family = family; address->address = g_strdup (inet_ntop (family, addr, string, sizeof (string))); address->prefix = prefix; - if (gateway) - address->gateway = g_strdup (inet_ntop (family, gateway, string, sizeof (string))); return address; } @@ -218,7 +211,6 @@ nm_ip_address_unref (NMIPAddress *address) address->refcount--; if (address->refcount == 0) { g_free (address->address); - g_free (address->gateway); if (address->attributes) g_hash_table_unref (address->attributes); g_slice_free (NMIPAddress, address); @@ -230,8 +222,8 @@ nm_ip_address_unref (NMIPAddress *address) * @address: the #NMIPAddress * @other: the #NMIPAddress to compare @address to. * - * Determines if two #NMIPAddress objects contain the same address, prefix, and - * gateway (attributes are not compared). + * Determines if two #NMIPAddress objects contain the same address and prefix + * (attributes are not compared). * * Returns: %TRUE if the objects contain the same values, %FALSE if they do not. **/ @@ -246,8 +238,7 @@ nm_ip_address_equal (NMIPAddress *address, NMIPAddress *other) if ( address->family != other->family || address->prefix != other->prefix - || strcmp (address->address, other->address) != 0 - || g_strcmp0 (address->gateway, other->gateway) != 0) + || strcmp (address->address, other->address) != 0) return FALSE; return TRUE; } @@ -269,7 +260,7 @@ nm_ip_address_dup (NMIPAddress *address) g_return_val_if_fail (address->refcount > 0, NULL); copy = nm_ip_address_new (address->family, - address->address, address->prefix, address->gateway, + address->address, address->prefix, NULL); if (address->attributes) { GHashTableIter iter; @@ -417,46 +408,6 @@ nm_ip_address_set_prefix (NMIPAddress *address, address->prefix = prefix; } -/** - * nm_ip_address_get_gateway: - * @address: the #NMIPAddress - * - * Gets the gateway property of this address object; this will be %NULL if the - * address has no associated gateway. - * - * Returns: the gateway - **/ -const char * -nm_ip_address_get_gateway (NMIPAddress *address) -{ - g_return_val_if_fail (address != NULL, NULL); - g_return_val_if_fail (address->refcount > 0, NULL); - - return address->gateway; -} - -/** - * nm_ip_address_set_gateway: - * @address: the #NMIPAddress - * @gateway: (allow-none): the gateway, as a string - * - * Sets the gateway property of this address object. - * - * @gateway (if non-%NULL) must be a valid address of @address's family. If you - * aren't sure you have a valid address, use nm_utils_ipaddr_valid() to check - * it. - **/ -void -nm_ip_address_set_gateway (NMIPAddress *address, - const char *gateway) -{ - g_return_if_fail (address != NULL); - g_return_if_fail (!gateway || nm_utils_ipaddr_valid (address->family, gateway)); - - g_free (address->gateway); - address->gateway = canonicalize_ip (address->family, gateway, TRUE); -} - /** * nm_ip_address_get_attribute_names: * @address: the #NMIPAddress @@ -1081,6 +1032,7 @@ typedef struct { GPtrArray *dns_search; /* array of domain name strings */ GPtrArray *addresses; /* array of NMIPAddress */ GPtrArray *routes; /* array of NMIPRoute */ + char *gateway; gboolean ignore_auto_routes; gboolean ignore_auto_dns; char *dhcp_hostname; @@ -1095,6 +1047,7 @@ enum { PROP_DNS, PROP_DNS_SEARCH, PROP_ADDRESSES, + PROP_GATEWAY, PROP_ROUTES, PROP_IGNORE_AUTO_ROUTES, PROP_IGNORE_AUTO_DNS, @@ -1543,6 +1496,21 @@ nm_setting_ip_config_clear_addresses (NMSettingIPConfig *setting) g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_ADDRESSES); } +/** + * nm_setting_ip_config_get_gateway: + * @setting: the #NMSettingIPConfig + * + * Returns: the IP address of the gateway associated with this configuration, or + * %NULL. + **/ +const char * +nm_setting_ip_config_get_gateway (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), NULL); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->gateway; +} + /** * nm_setting_ip_config_get_num_routes: * @setting: the #NMSettingIPConfig @@ -1882,6 +1850,27 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) } } + /* Validate gateway */ + if (priv->gateway) { + if (!priv->addresses->len) { + g_set_error_literal (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("gateway cannot be set if there are no addresses configured")); + g_prefix_error (error, "%s.%s: ", nm_setting_get_name (setting), NM_SETTING_IP_CONFIG_GATEWAY); + return FALSE; + } + + if (!nm_utils_ipaddr_valid (NM_SETTING_IP_CONFIG_GET_FAMILY (setting), priv->gateway)) { + g_set_error_literal (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("gateway is invalid")); + g_prefix_error (error, "%s.%s: ", nm_setting_get_name (setting), NM_SETTING_IP_CONFIG_GATEWAY); + return FALSE; + } + } + /* Validate routes */ for (i = 0; i < priv->routes->len; i++) { NMIPRoute *route = (NMIPRoute *) priv->routes->pdata[i]; @@ -1919,6 +1908,7 @@ finalize (GObject *object) NMSettingIPConfigPrivate *priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (self); g_free (priv->method); + g_free (priv->gateway); g_free (priv->dhcp_hostname); g_ptr_array_unref (priv->dns); @@ -1935,6 +1925,7 @@ set_property (GObject *object, guint prop_id, { NMSettingIPConfig *setting = NM_SETTING_IP_CONFIG (object); NMSettingIPConfigPrivate *priv = NM_SETTING_IP_CONFIG_GET_PRIVATE (setting); + const char *gateway; switch (prop_id) { case PROP_METHOD: @@ -1955,6 +1946,12 @@ set_property (GObject *object, guint prop_id, (NMUtilsCopyFunc) nm_ip_address_dup, (GDestroyNotify) nm_ip_address_unref); break; + case PROP_GATEWAY: + gateway = g_value_get_string (value); + g_return_if_fail (!gateway || nm_utils_ipaddr_valid (NM_SETTING_IP_CONFIG_GET_FAMILY (setting), gateway)); + g_free (priv->gateway); + priv->gateway = canonicalize_ip (NM_SETTING_IP_CONFIG_GET_FAMILY (setting), gateway, TRUE); + break; case PROP_ROUTES: g_ptr_array_unref (priv->routes); priv->routes = _nm_utils_copy_array (g_value_get_boxed (value), @@ -2008,6 +2005,9 @@ get_property (GObject *object, guint prop_id, (NMUtilsCopyFunc) nm_ip_address_dup, (GDestroyNotify) nm_ip_address_unref)); break; + case PROP_GATEWAY: + g_value_set_string (value, nm_setting_ip_config_get_gateway (setting)); + break; case PROP_ROUTES: g_value_take_boxed (value, _nm_utils_copy_array (priv->routes, (NMUtilsCopyFunc) nm_ip_route_dup, @@ -2118,6 +2118,20 @@ nm_setting_ip_config_class_init (NMSettingIPConfigClass *setting_class) NM_SETTING_PARAM_INFERRABLE | G_PARAM_STATIC_STRINGS)); + /** + * NMSettingIPConfig:gateway: + * + * The gateway associated with this configuration. This is only meaningful + * if #NMSettingIPConfig:addresses is also set. + **/ + g_object_class_install_property + (object_class, PROP_GATEWAY, + g_param_spec_string (NM_SETTING_IP_CONFIG_GATEWAY, "", "", + NULL, + G_PARAM_READWRITE | + NM_SETTING_PARAM_INFERRABLE | + G_PARAM_STATIC_STRINGS)); + /** * NMSettingIPConfig:routes: * diff --git a/libnm-core/nm-setting-ip-config.h b/libnm-core/nm-setting-ip-config.h index 0f2d16367c..37437761c4 100644 --- a/libnm-core/nm-setting-ip-config.h +++ b/libnm-core/nm-setting-ip-config.h @@ -38,12 +38,10 @@ GType nm_ip_address_get_type (void); NMIPAddress *nm_ip_address_new (int family, const char *addr, guint prefix, - const char *gateway, GError **error); NMIPAddress *nm_ip_address_new_binary (int family, gconstpointer addr, guint prefix, - gconstpointer gateway, GError **error); void nm_ip_address_ref (NMIPAddress *address); @@ -63,9 +61,6 @@ void nm_ip_address_set_address_binary (NMIPAddress *address, guint nm_ip_address_get_prefix (NMIPAddress *address); void nm_ip_address_set_prefix (NMIPAddress *address, guint prefix); -const char *nm_ip_address_get_gateway (NMIPAddress *address); -void nm_ip_address_set_gateway (NMIPAddress *address, - const char *gateway); char **nm_ip_address_get_attribute_names (NMIPAddress *address); GVariant *nm_ip_address_get_attribute (NMIPAddress *address, @@ -139,6 +134,7 @@ void nm_ip_route_set_attribute (NMIPRoute *route, #define NM_SETTING_IP_CONFIG_DNS "dns" #define NM_SETTING_IP_CONFIG_DNS_SEARCH "dns-search" #define NM_SETTING_IP_CONFIG_ADDRESSES "addresses" +#define NM_SETTING_IP_CONFIG_GATEWAY "gateway" #define NM_SETTING_IP_CONFIG_ROUTES "routes" #define NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES "ignore-auto-routes" #define NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS "ignore-auto-dns" @@ -195,6 +191,8 @@ gboolean nm_setting_ip_config_remove_address_by_value (NMSettingIPConfig NMIPAddress *address); void nm_setting_ip_config_clear_addresses (NMSettingIPConfig *setting); +const char *nm_setting_ip_config_get_gateway (NMSettingIPConfig *setting); + guint nm_setting_ip_config_get_num_routes (NMSettingIPConfig *setting); NMIPRoute *nm_setting_ip_config_get_route (NMSettingIPConfig *setting, int i); diff --git a/libnm-core/nm-setting-ip4-config.c b/libnm-core/nm-setting-ip4-config.c index 068ba0451a..66f2ea997e 100644 --- a/libnm-core/nm-setting-ip4-config.c +++ b/libnm-core/nm-setting-ip4-config.c @@ -102,10 +102,12 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) { NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting); NMSettingIPConfig *s_ip = NM_SETTING_IP_CONFIG (setting); + NMSettingVerifyResult ret; const char *method; - if (!NM_SETTING_CLASS (nm_setting_ip4_config_parent_class)->verify (setting, connection, error)) - return FALSE; + ret = NM_SETTING_CLASS (nm_setting_ip4_config_parent_class)->verify (setting, connection, error); + if (ret != NM_SETTING_VERIFY_SUCCESS) + return ret; method = nm_setting_ip_config_get_method (s_ip); /* Base class already checked that it exists */ @@ -245,10 +247,12 @@ ip4_addresses_get (NMSetting *setting, const char *property) { GPtrArray *addrs; + const char *gateway; GVariant *ret; g_object_get (setting, property, &addrs, NULL); - ret = nm_utils_ip4_addresses_to_variant (addrs); + gateway = nm_setting_ip_config_get_gateway (NM_SETTING_IP_CONFIG (setting)); + ret = nm_utils_ip4_addresses_to_variant (addrs, gateway); g_ptr_array_unref (addrs); return ret; @@ -262,18 +266,27 @@ ip4_addresses_set (NMSetting *setting, { GPtrArray *addrs; GVariant *s_ip4; - char **labels; + char **labels, *gateway = NULL; int i; - addrs = nm_utils_ip4_addresses_from_variant (value); + addrs = nm_utils_ip4_addresses_from_variant (value, &gateway); s_ip4 = g_variant_lookup_value (connection_dict, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); + if (g_variant_lookup (s_ip4, "address-labels", "^as", &labels)) { for (i = 0; i < addrs->len && labels[i]; i++) if (*labels[i]) nm_ip_address_set_attribute (addrs->pdata[i], "label", g_variant_new_string (labels[i])); g_strfreev (labels); } + + if (gateway && !g_variant_lookup (s_ip4, "gateway", "s", NULL)) { + g_object_set (setting, + NM_SETTING_IP_CONFIG_GATEWAY, gateway, + NULL); + } + g_free (gateway); + g_variant_unref (s_ip4); g_object_set (setting, property, addrs, NULL); diff --git a/libnm-core/nm-setting-ip6-config.c b/libnm-core/nm-setting-ip6-config.c index dc05e541c3..9e3001157e 100644 --- a/libnm-core/nm-setting-ip6-config.c +++ b/libnm-core/nm-setting-ip6-config.c @@ -100,10 +100,12 @@ static gboolean verify (NMSetting *setting, NMConnection *connection, GError **error) { NMSettingIPConfig *s_ip = NM_SETTING_IP_CONFIG (setting); + NMSettingVerifyResult ret; const char *method; - if (!NM_SETTING_CLASS (nm_setting_ip6_config_parent_class)->verify (setting, connection, error)) - return FALSE; + ret = NM_SETTING_CLASS (nm_setting_ip6_config_parent_class)->verify (setting, connection, error); + if (ret != NM_SETTING_VERIFY_SUCCESS) + return ret; method = nm_setting_ip_config_get_method (s_ip); /* Base class already checked that it exists */ @@ -187,16 +189,46 @@ ip6_dns_from_dbus (GVariant *dbus_value, } static GVariant * -ip6_addresses_to_dbus (const GValue *prop_value) +ip6_addresses_get (NMSetting *setting, + const char *property) { - return nm_utils_ip6_addresses_to_variant (g_value_get_boxed (prop_value)); + GPtrArray *addrs; + const char *gateway; + GVariant *ret; + + g_object_get (setting, property, &addrs, NULL); + gateway = nm_setting_ip_config_get_gateway (NM_SETTING_IP_CONFIG (setting)); + ret = nm_utils_ip6_addresses_to_variant (addrs, gateway); + g_ptr_array_unref (addrs); + + return ret; } static void -ip6_addresses_from_dbus (GVariant *dbus_value, - GValue *prop_value) +ip6_addresses_set (NMSetting *setting, + GVariant *connection_dict, + const char *property, + GVariant *value) { - g_value_take_boxed (prop_value, nm_utils_ip6_addresses_from_variant (dbus_value)); + GPtrArray *addrs; + GVariant *s_ip6; + char *gateway = NULL; + + addrs = nm_utils_ip6_addresses_from_variant (value, &gateway); + + s_ip6 = g_variant_lookup_value (connection_dict, NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); + + if (gateway && !g_variant_lookup (s_ip6, "gateway", "s", NULL)) { + g_object_set (setting, + NM_SETTING_IP_CONFIG_GATEWAY, gateway, + NULL); + } + g_free (gateway); + + g_variant_unref (s_ip6); + + g_object_set (setting, property, addrs, NULL); + g_ptr_array_unref (addrs); } static GVariant * @@ -285,11 +317,12 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *ip6_class) ip6_dns_to_dbus, ip6_dns_from_dbus); - _nm_setting_class_transform_property (setting_class, - NM_SETTING_IP_CONFIG_ADDRESSES, - G_VARIANT_TYPE ("a(ayuay)"), - ip6_addresses_to_dbus, - ip6_addresses_from_dbus); + _nm_setting_class_override_property (setting_class, + NM_SETTING_IP_CONFIG_ADDRESSES, + G_VARIANT_TYPE ("a(ayuay)"), + ip6_addresses_get, + ip6_addresses_set, + NULL); _nm_setting_class_transform_property (setting_class, NM_SETTING_IP_CONFIG_ROUTES, diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 110142fbfc..62698eefc8 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -1136,16 +1136,18 @@ nm_utils_ip4_dns_from_variant (GVariant *value) /** * nm_utils_ip4_addresses_to_variant: * @addresses: (element-type NMIPAddress): an array of #NMIPAddress objects + * @gateway: (allow-none): the gateway IP address * * Utility function to convert a #GPtrArray of #NMIPAddress objects representing * IPv4 addresses into a #GVariant of type 'aau' representing an array of * NetworkManager IPv4 addresses (which are tuples of address, prefix, and - * gateway, although only the first "gateway" field is preserved) + * gateway). The "gateway" field of the first address will get the value of + * @gateway (if non-%NULL). In all of the other addresses, that field will be 0. * * Returns: (transfer none): a new floating #GVariant representing @addresses. **/ GVariant * -nm_utils_ip4_addresses_to_variant (GPtrArray *addresses) +nm_utils_ip4_addresses_to_variant (GPtrArray *addresses, const char *gateway) { GVariantBuilder builder; int i; @@ -1162,8 +1164,8 @@ nm_utils_ip4_addresses_to_variant (GPtrArray *addresses) nm_ip_address_get_address_binary (addr, &array[0]); array[1] = nm_ip_address_get_prefix (addr); - if (i == 0 && nm_ip_address_get_gateway (addr)) - inet_pton (AF_INET, nm_ip_address_get_gateway (addr), &array[2]); + if (i == 0 && gateway) + inet_pton (AF_INET, gateway, &array[2]); else array[2] = 0; @@ -1179,17 +1181,19 @@ nm_utils_ip4_addresses_to_variant (GPtrArray *addresses) /** * nm_utils_ip4_addresses_from_variant: * @value: a #GVariant of type 'aau' + * @out_gateway: (out) (allow-none) (transfer full): on return, will contain the IP gateway * * Utility function to convert a #GVariant of type 'aau' representing a list of * NetworkManager IPv4 addresses (which are tuples of address, prefix, and - * gateway, although only the first "gateway" field is preserved) into a - * #GPtrArray of #NMIPAddress objects. + * gateway) into a #GPtrArray of #NMIPAddress objects. The "gateway" field of + * the first address (if set) will be returned in @out_gateway; the "gateway" fields + * of the other addresses are ignored. * * Returns: (transfer full) (element-type NMIPAddress): a newly allocated * #GPtrArray of #NMIPAddress objects **/ GPtrArray * -nm_utils_ip4_addresses_from_variant (GVariant *value) +nm_utils_ip4_addresses_from_variant (GVariant *value, char **out_gateway) { GPtrArray *addresses; GVariantIter iter; @@ -1197,6 +1201,9 @@ nm_utils_ip4_addresses_from_variant (GVariant *value) g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("aau")), NULL); + if (out_gateway) + *out_gateway = NULL; + g_variant_iter_init (&iter, value); addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_address_unref); @@ -1213,16 +1220,19 @@ nm_utils_ip4_addresses_from_variant (GVariant *value) continue; } - addr = nm_ip_address_new_binary (AF_INET, - &addr_array[0], addr_array[1], - addresses->len == 0 ? &addr_array[2] : NULL, - &error); + addr = nm_ip_address_new_binary (AF_INET, &addr_array[0], addr_array[1], &error); + if (addresses->len == 0 && out_gateway) { + if (addr_array[2]) + *out_gateway = g_strdup (nm_utils_inet4_ntop (addr_array[2], NULL)); + } + if (addr) g_ptr_array_add (addresses, addr); else { g_warning ("Ignoring invalid IP4 address: %s", error->message); g_clear_error (&error); } + g_variant_unref (addr_var); } @@ -1469,16 +1479,19 @@ nm_utils_ip6_dns_from_variant (GVariant *value) /** * nm_utils_ip6_addresses_to_variant: * @addresses: (element-type NMIPAddress): an array of #NMIPAddress objects + * @gateway: (allow-none): the gateway IP address * * Utility function to convert a #GPtrArray of #NMIPAddress objects representing * IPv6 addresses into a #GVariant of type 'a(ayuay)' representing an array of * NetworkManager IPv6 addresses (which are tuples of address, prefix, and - * gateway, although only the first "gateway" field is preserved). + * gateway). The "gateway" field of the first address will get the value of + * @gateway (if non-%NULL). In all of the other addresses, that field will be + * all 0s. * * Returns: (transfer none): a new floating #GVariant representing @addresses. **/ GVariant * -nm_utils_ip6_addresses_to_variant (GPtrArray *addresses) +nm_utils_ip6_addresses_to_variant (GPtrArray *addresses, const char *gateway) { GVariantBuilder builder; int i; @@ -1489,24 +1502,24 @@ nm_utils_ip6_addresses_to_variant (GPtrArray *addresses) for (i = 0; i < addresses->len; i++) { NMIPAddress *addr = addresses->pdata[i]; struct in6_addr ip_bytes, gateway_bytes; - GVariant *ip, *gateway; + GVariant *ip_var, *gateway_var; guint32 prefix; if (nm_ip_address_get_family (addr) != AF_INET6) continue; nm_ip_address_get_address_binary (addr, &ip_bytes); - ip = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &ip_bytes, 16, 1); + ip_var = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &ip_bytes, 16, 1); prefix = nm_ip_address_get_prefix (addr); - - if (i == 0 && nm_ip_address_get_gateway (addr)) - inet_pton (AF_INET6, nm_ip_address_get_gateway (addr), &gateway_bytes); + + if (i == 0 && gateway) + inet_pton (AF_INET6, gateway, &gateway_bytes); else memset (&gateway_bytes, 0, sizeof (gateway_bytes)); - gateway = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &gateway_bytes, 16, 1); + gateway_var = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &gateway_bytes, 16, 1); - g_variant_builder_add (&builder, "(@ayu@ay)", ip, prefix, gateway); + g_variant_builder_add (&builder, "(@ayu@ay)", ip_var, prefix, gateway_var); } } @@ -1516,17 +1529,19 @@ nm_utils_ip6_addresses_to_variant (GPtrArray *addresses) /** * nm_utils_ip6_addresses_from_variant: * @value: a #GVariant of type 'a(ayuay)' + * @out_gateway: (out) (allow-none) (transfer full): on return, will contain the IP gateway * * Utility function to convert a #GVariant of type 'a(ayuay)' representing a * list of NetworkManager IPv6 addresses (which are tuples of address, prefix, - * and gateway, although only the first "gateway" field is preserved) into a - * #GPtrArray of #NMIPAddress objects. + * and gateway) into a #GPtrArray of #NMIPAddress objects. The "gateway" field + * of the first address (if set) will be returned in @out_gateway; the "gateway" + * fields of the other addresses are ignored. * * Returns: (transfer full) (element-type NMIPAddress): a newly allocated * #GPtrArray of #NMIPAddress objects **/ GPtrArray * -nm_utils_ip6_addresses_from_variant (GVariant *value) +nm_utils_ip6_addresses_from_variant (GVariant *value, char **out_gateway) { GVariantIter iter; GVariant *addr_var, *gateway_var; @@ -1535,6 +1550,9 @@ nm_utils_ip6_addresses_from_variant (GVariant *value) g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("a(ayuay)")), NULL); + if (out_gateway) + *out_gateway = NULL; + g_variant_iter_init (&iter, value); addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_address_unref); @@ -1556,16 +1574,19 @@ nm_utils_ip6_addresses_from_variant (GVariant *value) __func__, (int) addr_len); goto next; } - gateway_bytes = g_variant_get_fixed_array (gateway_var, &gateway_len, 1); - if (gateway_len != 16) { - g_warning ("%s: ignoring invalid IP6 address of length %d", - __func__, (int) gateway_len); - goto next; + + if (addresses->len == 0 && out_gateway) { + gateway_bytes = g_variant_get_fixed_array (gateway_var, &gateway_len, 1); + if (gateway_len != 16) { + g_warning ("%s: ignoring invalid IP6 address of length %d", + __func__, (int) gateway_len); + goto next; + } + if (!IN6_IS_ADDR_UNSPECIFIED (gateway_bytes)) + *out_gateway = g_strdup (nm_utils_inet6_ntop (gateway_bytes, NULL)); } - addr = nm_ip_address_new_binary (AF_INET6, addr_bytes, prefix, - addresses->len == 0 ? gateway_bytes : NULL, - &error); + addr = nm_ip_address_new_binary (AF_INET6, addr_bytes, prefix, &error); if (addr) g_ptr_array_add (addresses, addr); else { diff --git a/libnm-core/nm-utils.h b/libnm-core/nm-utils.h index ccf15f7b84..628fa0f5a6 100644 --- a/libnm-core/nm-utils.h +++ b/libnm-core/nm-utils.h @@ -97,8 +97,10 @@ gboolean nm_utils_wpa_psk_valid (const char *psk); GVariant *nm_utils_ip4_dns_to_variant (char **dns); char **nm_utils_ip4_dns_from_variant (GVariant *value); -GVariant *nm_utils_ip4_addresses_to_variant (GPtrArray *addresses); -GPtrArray *nm_utils_ip4_addresses_from_variant (GVariant *value); +GVariant *nm_utils_ip4_addresses_to_variant (GPtrArray *addresses, + const char *gateway); +GPtrArray *nm_utils_ip4_addresses_from_variant (GVariant *value, + char **out_gateway); GVariant *nm_utils_ip4_routes_to_variant (GPtrArray *routes); GPtrArray *nm_utils_ip4_routes_from_variant (GVariant *value); @@ -108,8 +110,10 @@ guint32 nm_utils_ip4_get_default_prefix (guint32 ip); GVariant *nm_utils_ip6_dns_to_variant (char **dns); char **nm_utils_ip6_dns_from_variant (GVariant *value); -GVariant *nm_utils_ip6_addresses_to_variant (GPtrArray *addresses); -GPtrArray *nm_utils_ip6_addresses_from_variant (GVariant *value); +GVariant *nm_utils_ip6_addresses_to_variant (GPtrArray *addresses, + const char *gateway); +GPtrArray *nm_utils_ip6_addresses_from_variant (GVariant *value, + char **out_gateway); GVariant *nm_utils_ip6_routes_to_variant (GPtrArray *routes); GPtrArray *nm_utils_ip6_routes_from_variant (GVariant *value); diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 041c4263f4..d66a9c9f6e 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -338,7 +338,7 @@ test_setting_ip4_config_labels (void) NULL); /* addr 1 */ - addr = nm_ip_address_new (AF_INET, "1.1.1.1", 24, NULL, &error); + addr = nm_ip_address_new (AF_INET, "1.1.1.1", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, addr); @@ -351,7 +351,7 @@ test_setting_ip4_config_labels (void) g_assert (label == NULL); /* addr 2 */ - addr = nm_ip_address_new (AF_INET, "2.2.2.2", 24, NULL, &error); + addr = nm_ip_address_new (AF_INET, "2.2.2.2", 24, &error); g_assert_no_error (error); nm_ip_address_set_attribute (addr, "label", g_variant_new_string ("eth0:1")); @@ -366,7 +366,7 @@ test_setting_ip4_config_labels (void) g_assert_cmpstr (g_variant_get_string (label, NULL), ==, "eth0:1"); /* addr 3 */ - addr = nm_ip_address_new (AF_INET, "3.3.3.3", 24, NULL, &error); + addr = nm_ip_address_new (AF_INET, "3.3.3.3", 24, &error); g_assert_no_error (error); nm_ip_address_set_attribute (addr, "label", NULL); @@ -1634,6 +1634,7 @@ test_connection_diff_a_only (void) { NM_SETTING_IP_CONFIG_DNS, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP_CONFIG_DNS_SEARCH, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP_CONFIG_ADDRESSES, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_GATEWAY, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP_CONFIG_ROUTES, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, NM_SETTING_DIFF_RESULT_IN_A }, @@ -2480,7 +2481,7 @@ test_setting_ip4_changed_signal (void) ASSERT_CHANGED (nm_setting_ip_config_add_dns_search (s_ip4, "foobar.com")); ASSERT_CHANGED (nm_setting_ip_config_clear_dns_searches (s_ip4)); - addr = nm_ip_address_new (AF_INET, "22.33.0.0", 24, NULL, &error); + addr = nm_ip_address_new (AF_INET, "22.33.0.0", 24, &error); g_assert_no_error (error); ASSERT_CHANGED (nm_setting_ip_config_add_address (s_ip4, addr)); ASSERT_CHANGED (nm_setting_ip_config_remove_address (s_ip4, 0)); @@ -2549,7 +2550,7 @@ test_setting_ip6_changed_signal (void) nm_setting_ip_config_add_dns_search (s_ip6, "foobar.com"); ASSERT_CHANGED (nm_setting_ip_config_clear_dns_searches (s_ip6)); - addr = nm_ip_address_new (AF_INET6, "1:2:3::4:5:6", 64, NULL, &error); + addr = nm_ip_address_new (AF_INET6, "1:2:3::4:5:6", 64, &error); g_assert_no_error (error); ASSERT_CHANGED (nm_setting_ip_config_add_address (s_ip6, addr)); @@ -3336,6 +3337,145 @@ test_connection_normalize_infiniband_mtu (void) g_assert_cmpint (65520, ==, nm_setting_infiniband_get_mtu (s_infini)); } +static void +test_setting_ip4_gateway (void) +{ + NMConnection *conn; + NMSettingIPConfig *s_ip4; + NMIPAddress *addr; + GVariant *conn_dict, *ip4_dict, *value; + GVariantIter iter; + GVariant *addr_var; + GError *error = NULL; + + /* When serializing, ipv4.gateway is copied to the first entry of ipv4.addresses */ + conn = nmtst_create_minimal_connection ("test_setting_ip4_gateway", NULL, + NM_SETTING_WIRED_SETTING_NAME, NULL); + s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); + g_object_set (s_ip4, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, "192.168.1.1", + NULL); + nm_connection_add_setting (conn, NM_SETTING (s_ip4)); + + addr = nm_ip_address_new (AF_INET, "192.168.1.10", 24, &error); + g_assert_no_error (error); + nm_setting_ip_config_add_address (s_ip4, addr); + nm_ip_address_unref (addr); + + conn_dict = nm_connection_to_dbus (conn, NM_CONNECTION_SERIALIZE_ALL); + g_object_unref (conn); + + ip4_dict = g_variant_lookup_value (conn_dict, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); + g_assert (ip4_dict != NULL); + + value = g_variant_lookup_value (ip4_dict, NM_SETTING_IP_CONFIG_GATEWAY, G_VARIANT_TYPE_STRING); + g_assert (value != NULL); + g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "192.168.1.1"); + g_variant_unref (value); + + value = g_variant_lookup_value (ip4_dict, NM_SETTING_IP_CONFIG_ADDRESSES, G_VARIANT_TYPE ("aau")); + g_assert (value != NULL); + + g_variant_iter_init (&iter, value); + while (g_variant_iter_next (&iter, "@au", &addr_var)) { + const guint32 *addr_array; + gsize length; + + addr_array = g_variant_get_fixed_array (addr_var, &length, sizeof (guint32)); + g_assert_cmpint (length, ==, 3); + g_assert_cmpstr (nm_utils_inet4_ntop (addr_array[2], NULL), ==, "192.168.1.1"); + g_variant_unref (addr_var); + } + g_variant_unref (value); + + g_variant_unref (ip4_dict); + + /* When deserializing, the first gateway in ipv4.addresses is copied to ipv4.gateway */ + NMTST_VARIANT_EDITOR (conn_dict, + NMTST_VARIANT_DROP_PROPERTY (NM_SETTING_IP4_CONFIG_SETTING_NAME, + NM_SETTING_IP_CONFIG_GATEWAY); + ); + + conn = nm_simple_connection_new_from_dbus (conn_dict, &error); + g_variant_unref (conn_dict); + g_assert_no_error (error); + + s_ip4 = (NMSettingIPConfig *) nm_connection_get_setting_ip4_config (conn); + g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "192.168.1.1"); + + g_object_unref (conn); +} + +static void +test_setting_ip6_gateway (void) +{ + NMConnection *conn; + NMSettingIPConfig *s_ip6; + NMIPAddress *addr; + GVariant *conn_dict, *ip6_dict, *value; + GVariantIter iter; + GVariant *gateway_var; + GError *error = NULL; + + /* When serializing, ipv6.gateway is copied to the first entry of ipv6.addresses */ + conn = nmtst_create_minimal_connection ("test_setting_ip6_gateway", NULL, + NM_SETTING_WIRED_SETTING_NAME, NULL); + s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); + g_object_set (s_ip6, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, "abcd::1", + NULL); + nm_connection_add_setting (conn, NM_SETTING (s_ip6)); + + addr = nm_ip_address_new (AF_INET6, "abcd::10", 64, &error); + g_assert_no_error (error); + nm_setting_ip_config_add_address (s_ip6, addr); + nm_ip_address_unref (addr); + + conn_dict = nm_connection_to_dbus (conn, NM_CONNECTION_SERIALIZE_ALL); + g_object_unref (conn); + + ip6_dict = g_variant_lookup_value (conn_dict, NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); + g_assert (ip6_dict != NULL); + + value = g_variant_lookup_value (ip6_dict, NM_SETTING_IP_CONFIG_GATEWAY, G_VARIANT_TYPE_STRING); + g_assert (value != NULL); + g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "abcd::1"); + + value = g_variant_lookup_value (ip6_dict, NM_SETTING_IP_CONFIG_ADDRESSES, G_VARIANT_TYPE ("a(ayuay)")); + g_assert (value != NULL); + + g_variant_iter_init (&iter, value); + while (g_variant_iter_next (&iter, "(@ayu@ay)", NULL, NULL, &gateway_var)) { + const guint8 *gateway_bytes; + gsize length; + + gateway_bytes = g_variant_get_fixed_array (gateway_var, &length, 1); + g_assert_cmpint (length, ==, 16); + g_assert_cmpstr (nm_utils_inet6_ntop ((struct in6_addr *) gateway_bytes, NULL), ==, "abcd::1"); + g_variant_unref (gateway_var); + } + g_variant_unref (value); + + g_variant_unref (ip6_dict); + + /* When deserializing, the first gateway in ipv4.addresses is copied to ipv4.gateway */ + NMTST_VARIANT_EDITOR (conn_dict, + NMTST_VARIANT_DROP_PROPERTY (NM_SETTING_IP6_CONFIG_SETTING_NAME, + NM_SETTING_IP_CONFIG_GATEWAY); + ); + + conn = nm_simple_connection_new_from_dbus (conn_dict, &error); + g_variant_unref (conn_dict); + g_assert_no_error (error); + + s_ip6 = (NMSettingIPConfig *) nm_connection_get_setting_ip6_config (conn); + g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip6), ==, "abcd::1"); + + g_object_unref (conn); +} + NMTST_DEFINE (); int main (int argc, char **argv) @@ -3424,6 +3564,8 @@ int main (int argc, char **argv) g_test_add_func ("/core/general/test_setting_wireless_changed_signal", test_setting_wireless_changed_signal); g_test_add_func ("/core/general/test_setting_wireless_security_changed_signal", test_setting_wireless_security_changed_signal); g_test_add_func ("/core/general/test_setting_802_1x_changed_signal", test_setting_802_1x_changed_signal); + g_test_add_func ("/core/general/test_setting_ip4_gateway", test_setting_ip4_gateway); + g_test_add_func ("/core/general/test_setting_ip6_gateway", test_setting_ip6_gateway); return g_test_run (); } diff --git a/libnm/libnm.ver b/libnm/libnm.ver index 5a5d534bfc..952b1dc263 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -289,7 +289,6 @@ global: nm_ip_address_get_attribute; nm_ip_address_get_attribute_names; nm_ip_address_get_family; - nm_ip_address_get_gateway; nm_ip_address_get_prefix; nm_ip_address_get_type; nm_ip_address_new; @@ -298,7 +297,6 @@ global: nm_ip_address_set_address; nm_ip_address_set_address_binary; nm_ip_address_set_attribute; - nm_ip_address_set_gateway; nm_ip_address_set_prefix; nm_ip_address_unref; nm_ip_route_equal; @@ -571,6 +569,7 @@ global: nm_setting_ip_config_get_dhcp_send_hostname; nm_setting_ip_config_get_dns; nm_setting_ip_config_get_dns_search; + nm_setting_ip_config_get_gateway; nm_setting_ip_config_get_ignore_auto_dns; nm_setting_ip_config_get_ignore_auto_routes; nm_setting_ip_config_get_may_fail; diff --git a/libnm/nm-ip4-config.c b/libnm/nm-ip4-config.c index bc86821043..a37f835774 100644 --- a/libnm/nm-ip4-config.c +++ b/libnm/nm-ip4-config.c @@ -74,7 +74,7 @@ demarshal_ip4_address_array (NMObject *object, GParamSpec *pspec, GVariant *valu NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); g_ptr_array_unref (priv->addresses); - priv->addresses = nm_utils_ip4_addresses_from_variant (value); + priv->addresses = nm_utils_ip4_addresses_from_variant (value, NULL); _nm_object_queue_notify (object, NM_IP4_CONFIG_ADDRESSES); return TRUE; diff --git a/libnm/nm-ip6-config.c b/libnm/nm-ip6-config.c index 589b79ce1c..82d377ee93 100644 --- a/libnm/nm-ip6-config.c +++ b/libnm/nm-ip6-config.c @@ -59,7 +59,7 @@ demarshal_ip6_address_array (NMObject *object, GParamSpec *pspec, GVariant *valu NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); g_ptr_array_unref (priv->addresses); - priv->addresses = nm_utils_ip6_addresses_from_variant (value); + priv->addresses = nm_utils_ip6_addresses_from_variant (value, NULL); _nm_object_queue_notify (object, NM_IP6_CONFIG_ADDRESSES); return TRUE; diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index af284f0bc2..12d3919e77 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -319,14 +319,11 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIPConfig *setting, in nm_ip4_config_set_never_default (config, TRUE); else if (nm_setting_ip_config_get_ignore_auto_routes (setting)) nm_ip4_config_set_never_default (config, FALSE); - if (naddresses) { - const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip_config_get_address (setting, 0)); + if (nm_setting_ip_config_get_gateway (setting)) { guint32 gateway; - if (gateway_str) { - inet_pton (AF_INET, gateway_str, &gateway); - nm_ip4_config_set_gateway (config, gateway); - } + inet_pton (AF_INET, nm_setting_ip_config_get_gateway (setting), &gateway); + nm_ip4_config_set_gateway (config, gateway); } /* Addresses */ @@ -425,9 +422,7 @@ nm_ip4_config_create_setting (const NMIP4Config *config) if (!method) method = NM_SETTING_IP4_CONFIG_METHOD_MANUAL; - s_addr = nm_ip_address_new_binary (AF_INET, &address->address, address->plen, - i == 0 ? &gateway : NULL, - NULL); + s_addr = nm_ip_address_new_binary (AF_INET, &address->address, address->plen, NULL); if (*address->label) nm_ip_address_set_attribute (s_addr, "label", g_variant_new_string (address->label)); @@ -435,6 +430,13 @@ nm_ip4_config_create_setting (const NMIP4Config *config) nm_ip_address_unref (s_addr); } + /* Gateway */ + if (gateway) { + g_object_set (s_ip4, + NM_SETTING_IP_CONFIG_GATEWAY, nm_utils_inet4_ntop (gateway, NULL), + NULL); + } + /* Use 'disabled' if the method wasn't previously set */ if (!method) method = NM_SETTING_IP4_CONFIG_METHOD_DISABLED; diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index fd21e5a624..e007b20104 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -404,6 +404,7 @@ void nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIPConfig *setting, int default_route_metric) { guint naddresses, nroutes, nnameservers, nsearches; + const char *gateway_str; int i; if (!setting) @@ -423,14 +424,12 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIPConfig *setting, in nm_ip6_config_set_never_default (config, TRUE); else if (nm_setting_ip_config_get_ignore_auto_routes (setting)) nm_ip6_config_set_never_default (config, FALSE); - if (naddresses) { - const char *gateway_str = nm_ip_address_get_gateway (nm_setting_ip_config_get_address (setting, 0)); + gateway_str = nm_setting_ip_config_get_gateway (setting); + if (gateway_str) { struct in6_addr gateway; - if (gateway_str) { - inet_pton (AF_INET6, gateway_str, &gateway); - nm_ip6_config_set_gateway (config, &gateway); - } + inet_pton (AF_INET6, gateway_str, &gateway); + nm_ip6_config_set_gateway (config, &gateway); } /* Addresses */ @@ -531,13 +530,18 @@ nm_ip6_config_create_setting (const NMIP6Config *config) if (!method || strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL) == 0) method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL; - s_addr = nm_ip_address_new_binary (AF_INET6, &address->address, address->plen, - i == 0 ? gateway : NULL, - NULL); + s_addr = nm_ip_address_new_binary (AF_INET6, &address->address, address->plen, NULL); nm_setting_ip_config_add_address (s_ip6, s_addr); nm_ip_address_unref (s_addr); } + /* Gateway */ + if (gateway) { + g_object_set (s_ip6, + NM_SETTING_IP_CONFIG_GATEWAY, nm_utils_inet6_ntop (gateway, NULL), + NULL); + } + /* Use 'ignore' if the method wasn't previously set */ if (!method) method = NM_SETTING_IP6_CONFIG_METHOD_IGNORE; diff --git a/src/settings/plugins/ibft/reader.c b/src/settings/plugins/ibft/reader.c index 487e3f8b05..340e197bff 100644 --- a/src/settings/plugins/ibft/reader.c +++ b/src/settings/plugins/ibft/reader.c @@ -349,7 +349,7 @@ ip4_setting_add_from_block (const GPtrArray *block, goto error; } - addr = nm_ip_address_new (AF_INET, s_ipaddr, prefix, s_gateway, error); + addr = nm_ip_address_new (AF_INET, s_ipaddr, prefix, error); if (!addr) { g_prefix_error (error, "iBFT: malformed iscsiadm record: "); goto error; @@ -358,6 +358,8 @@ ip4_setting_add_from_block (const GPtrArray *block, nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_GATEWAY, s_gateway, NULL); + if (s_dns1) nm_setting_ip_config_add_dns (s_ip4, s_dns1); if (s_dns2) diff --git a/src/settings/plugins/ibft/tests/test-ibft.c b/src/settings/plugins/ibft/tests/test-ibft.c index 28a05bafe1..5fc63b1180 100644 --- a/src/settings/plugins/ibft/tests/test-ibft.c +++ b/src/settings/plugins/ibft/tests/test-ibft.c @@ -162,7 +162,8 @@ test_read_ibft_static (void) g_assert (ip4_addr); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.32.72"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 22); - g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.35.254"); + + g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "192.168.35.254"); g_object_unref (connection); g_ptr_array_unref (block); @@ -259,7 +260,8 @@ test_read_ibft_vlan (void) g_assert (ip4_addr); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.6.200"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); - g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, NULL); + + g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, NULL); g_object_unref (connection); g_ptr_array_ref (block); diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index 7b193589d2..9c3e9b0d2d 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -334,13 +334,13 @@ read_full_ip4_address (shvarFile *ifcfg, gint32 which, NMIPAddress *base_addr, NMIPAddress **out_address, + char **out_gateway, GError **error) { char *ip_tag, *prefix_tag, *netmask_tag, *gw_tag; - char *ip = NULL, *gw = NULL; + char *ip = NULL; long prefix = 0; gboolean success = FALSE; - shvarFile *network_ifcfg; char *value; guint32 tmp; @@ -370,21 +370,9 @@ read_full_ip4_address (shvarFile *ifcfg, } /* Gateway */ - if (!read_ip4_address (ifcfg, gw_tag, &gw, error)) - goto done; - if (!gw && base_addr) - gw = g_strdup (nm_ip_address_get_gateway (base_addr)); - if (!gw) { - gboolean read_success; - - /* If no gateway in the ifcfg, try /etc/sysconfig/network instead */ - network_ifcfg = svOpenFile (network_file, NULL); - if (network_ifcfg) { - read_success = read_ip4_address (network_ifcfg, "GATEWAY", &gw, error); - svCloseFile (network_ifcfg); - if (!read_success) - goto done; - } + if (out_gateway && !*out_gateway) { + if (!read_ip4_address (ifcfg, gw_tag, out_gateway, error)) + goto done; } /* Prefix */ @@ -431,13 +419,12 @@ read_full_ip4_address (shvarFile *ifcfg, goto done; } - *out_address = nm_ip_address_new (AF_INET, ip, prefix, gw, error); + *out_address = nm_ip_address_new (AF_INET, ip, prefix, error); if (*out_address) success = TRUE; done: g_free (ip); - g_free (gw); g_free (ip_tag); g_free (prefix_tag); g_free (netmask_tag); @@ -698,9 +685,8 @@ parse_full_ip6_address (shvarFile *ifcfg, GError **error) { char **list; - char *ip_val, *prefix_val, *gateway_val = NULL; + char *ip_val, *prefix_val; long prefix; - shvarFile *network_ifcfg; gboolean success = FALSE; g_return_val_if_fail (addr_str != NULL, FALSE); @@ -733,33 +719,12 @@ parse_full_ip6_address (shvarFile *ifcfg, prefix = 64; } - /* Gateway */ - if (i == 0) { - char *ptr; - - gateway_val = svGetValue (ifcfg, "IPV6_DEFAULTGW", FALSE); - if (!gateway_val) { - /* If no gateway in the ifcfg, try global /etc/sysconfig/network instead */ - network_ifcfg = svOpenFile (network_file, NULL); - if (network_ifcfg) { - gateway_val = svGetValue (network_ifcfg, "IPV6_DEFAULTGW", FALSE); - svCloseFile (network_ifcfg); - } - } - - if ( gateway_val - && (ptr = strchr (gateway_val, '%')) != NULL) - *ptr = '\0'; /* remove %interface suffix if present */ - } else - gateway_val = NULL; - - *out_address = nm_ip_address_new (AF_INET6, ip_val, prefix, gateway_val, error); + *out_address = nm_ip_address_new (AF_INET6, ip_val, prefix, error); if (*out_address) success = TRUE; error: g_strfreev (list); - g_free (gateway_val); return success; } @@ -927,6 +892,7 @@ make_ip4_setting (shvarFile *ifcfg, char *value = NULL; char *route_path = NULL; char *method; + char *gateway = NULL; gint32 i; shvarFile *network_ifcfg; shvarFile *route_ifcfg; @@ -1031,7 +997,7 @@ make_ip4_setting (shvarFile *ifcfg, for (i = -1; i < 256; i++) { NMIPAddress *addr = NULL; - if (!read_full_ip4_address (ifcfg, network_file, i, NULL, &addr, error)) + if (!read_full_ip4_address (ifcfg, network_file, i, NULL, &addr, &gateway, error)) goto done; if (!addr) { @@ -1042,14 +1008,25 @@ make_ip4_setting (shvarFile *ifcfg, continue; } - if (nm_setting_ip_config_get_num_addresses (s_ip4)) - nm_ip_address_set_gateway (addr, NULL); - if (!nm_setting_ip_config_add_address (s_ip4, addr)) PARSE_WARNING ("duplicate IP4 address"); nm_ip_address_unref (addr); } + /* Gateway */ + if (!gateway) { + network_ifcfg = svOpenFile (network_file, NULL); + if (network_ifcfg) { + gboolean read_success; + + read_success = read_ip4_address (network_ifcfg, "GATEWAY", &gateway, error); + svCloseFile (network_ifcfg); + if (!read_success) + goto done; + } + } + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_GATEWAY, gateway, NULL); + /* DNS servers * Pick up just IPv4 addresses (IPv6 addresses are taken by make_ip6_setting()) */ @@ -1155,6 +1132,7 @@ make_ip4_setting (shvarFile *ifcfg, return NM_SETTING (s_ip4); done: + g_free (gateway); g_free (route_path); g_object_unref (s_ip4); return NULL; @@ -1235,7 +1213,7 @@ read_aliases (NMSettingIPConfig *s_ip4, const char *filename, const char *networ } addr = NULL; - ok = read_full_ip4_address (parsed, network_file, -1, base_addr, &addr, &err); + ok = read_full_ip4_address (parsed, network_file, -1, base_addr, &addr, NULL, &err); svCloseFile (parsed); if (ok) { nm_ip_address_set_attribute (addr, "label", g_variant_new_string (device)); @@ -1431,6 +1409,33 @@ make_ip6_setting (shvarFile *ifcfg, } g_strfreev (list); + /* Gateway */ + if (nm_setting_ip_config_get_num_addresses (s_ip6)) { + value = svGetValue (ifcfg, "IPV6_DEFAULTGW", FALSE); + if (!value) { + /* If no gateway in the ifcfg, try global /etc/sysconfig/network instead */ + network_ifcfg = svOpenFile (network_file, NULL); + if (network_ifcfg) { + value = svGetValue (network_ifcfg, "IPV6_DEFAULTGW", FALSE); + svCloseFile (network_ifcfg); + } + } + if (value) { + char *ptr; + if ((ptr = strchr (value, '%')) != NULL) + *ptr = '\0'; /* remove %interface prefix if present */ + if (!nm_utils_ipaddr_valid (AF_INET6, value)) { + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, + "Invalid IP6 address '%s'", value); + g_free (value); + goto error; + } + + g_object_set (s_ip6, NM_SETTING_IP_CONFIG_GATEWAY, value, NULL); + g_free (value); + } + } + /* DNS servers * Pick up just IPv6 addresses (IPv4 addresses are taken by make_ip4_setting()) */ diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index e9ce7ca43b..4511878642 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -493,7 +493,9 @@ test_read_wired_static (const char *file, g_assert (ip4_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); - g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.1.1"); + + /* Gateway */ + g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "192.168.1.1"); /* ===== IPv6 SETTING ===== */ s_ip6 = nm_connection_get_setting_ip6_config (connection); @@ -762,7 +764,9 @@ test_read_wired_dhcp_plus_ip (void) g_assert (ip4_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "1.2.3.4"); - g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "1.1.1.1"); + + /* Gateway */ + g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "1.1.1.1"); ip4_addr = nm_setting_ip_config_get_address (s_ip4, 1); g_assert (ip4_addr); @@ -836,7 +840,9 @@ test_read_wired_global_gateway (void) g_assert (ip4_addr); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); - g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.1.2"); + + /* Gateway */ + g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "192.168.1.2"); g_object_unref (connection); } @@ -2377,7 +2383,7 @@ test_read_wired_aliases_good (void) int expected_num_addresses = 4; const char *expected_address[4] = { "192.168.1.5", "192.168.1.6", "192.168.1.9", "192.168.1.99" }; const char *expected_label[4] = { NULL, "aliasem0:1", "aliasem0:2", "aliasem0:99" }; - const char *expected_gateway[4] = { "192.168.1.1", "192.168.1.1", "192.168.1.1", "192.168.1.1" }; + const char *expected_gateway = "192.168.1.1"; int i, j; connection = connection_from_file (TEST_IFCFG_ALIASES_GOOD, @@ -2457,7 +2463,6 @@ test_read_wired_aliases_good (void) g_assert (j < expected_num_addresses); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); - g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, expected_gateway[j]); label = nm_ip_address_get_attribute (ip4_addr, "label"); if (expected_label[j]) g_assert_cmpstr (g_variant_get_string (label, NULL), ==, expected_label[j]); @@ -2465,10 +2470,12 @@ test_read_wired_aliases_good (void) g_assert (label == NULL); expected_address[j] = NULL; - expected_gateway[j] = NULL; expected_label[j] = NULL; } + /* Gateway */ + g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, expected_gateway); + for (i = 0; i < expected_num_addresses; i++) { ASSERT (expected_address[i] == NULL, "aliases-good-verify-ip4", "failed to verify %s: did not find IP4 address %s", @@ -2561,9 +2568,11 @@ test_read_wired_aliases_bad (const char *base, const char *expected_id) g_assert (ip4_addr != NULL); g_assert_cmpstr (nm_ip_address_get_address (ip4_addr), ==, "192.168.1.5"); g_assert_cmpint (nm_ip_address_get_prefix (ip4_addr), ==, 24); - g_assert_cmpstr (nm_ip_address_get_gateway (ip4_addr), ==, "192.168.1.1"); g_assert (nm_ip_address_get_attribute (ip4_addr, "label") == NULL); + /* Gateway */ + g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "192.168.1.1"); + g_free (keyfile); g_free (routefile); g_free (route6file); @@ -6282,14 +6291,15 @@ test_write_wired_static (void) g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_GATEWAY, "1.1.1.1", NULL); - addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); - addr = nm_ip_address_new (AF_INET, "1.1.1.5", 24, "1.1.1.1", &error); + addr = nm_ip_address_new (AF_INET, "1.1.1.5", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); @@ -6310,17 +6320,17 @@ test_write_wired_static (void) NULL); /* Add addresses */ - addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, NULL, &error); + addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip6, addr6); nm_ip_address_unref (addr6); - addr6 = nm_ip_address_new (AF_INET6, "2003:1234:abcd::2", 22, NULL, &error); + addr6 = nm_ip_address_new (AF_INET6, "2003:1234:abcd::2", 22, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip6, addr6); nm_ip_address_unref (addr6); - addr6 = nm_ip_address_new (AF_INET6, "3003:1234:abcd::3", 33, NULL, &error); + addr6 = nm_ip_address_new (AF_INET6, "3003:1234:abcd::3", 33, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip6, addr6); nm_ip_address_unref (addr6); @@ -6689,7 +6699,7 @@ test_write_wired_static_ip6_only (void) NULL); /* Add addresses */ - addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, NULL, &error); + addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip6, addr6); nm_ip_address_unref (addr6); @@ -6815,10 +6825,11 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) g_object_set (s_ip6, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, gateway6, NULL); /* Add addresses */ - addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, gateway6, &error); + addr6 = nm_ip_address_new (AF_INET6, "1003:1234:abcd::1", 11, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip6, addr6); nm_ip_address_unref (addr6); @@ -6873,11 +6884,11 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) /* assert that the gateway was written and reloaded as expected */ if (!gateway6 || !strcmp (gateway6, "::")) { - g_assert (nm_ip_address_get_gateway (addr6) == NULL); + g_assert (nm_setting_ip_config_get_gateway (s_ip6) == NULL); g_assert (written_ifcfg_gateway == NULL); } else { - g_assert (nm_ip_address_get_gateway (addr6) != NULL); - g_assert_cmpstr (nm_ip_address_get_gateway (addr6), ==, gateway6); + g_assert (nm_setting_ip_config_get_gateway (s_ip6) != NULL); + g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip6), ==, gateway6); g_assert_cmpstr (written_ifcfg_gateway, ==, gateway6); } @@ -7092,14 +7103,15 @@ test_write_wired_static_routes (void) g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, "1.1.1.1", NULL); - addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); - addr = nm_ip_address_new (AF_INET, "1.1.1.5", 24, "1.1.1.1", &error); + addr = nm_ip_address_new (AF_INET, "1.1.1.5", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); @@ -7610,11 +7622,12 @@ test_write_wired_aliases (void) g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, "1.1.1.1", NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); for (i = 0; i < num_addresses; i++) { - addr = nm_ip_address_new (AF_INET, ip[i], 24, "1.1.1.1", &error); + addr = nm_ip_address_new (AF_INET, ip[i], 24, &error); g_assert_no_error (error); if (label[i]) nm_ip_address_set_attribute (addr, "label", g_variant_new_string (label[i])); @@ -7709,7 +7722,6 @@ test_write_wired_aliases (void) g_assert (j < num_addresses); g_assert_cmpint (nm_ip_address_get_prefix (addr), ==, 24); - g_assert_cmpstr (nm_ip_address_get_gateway (addr), ==, "1.1.1.1"); if (label[j]) g_assert_cmpstr (g_variant_get_string (nm_ip_address_get_attribute (addr, "label"), NULL), ==, label[j]); else @@ -7725,6 +7737,9 @@ test_write_wired_aliases (void) ip[i]); } + /* Gateway */ + g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip4), ==, "1.1.1.1"); + g_free (testfile); g_free (keyfile); g_free (routefile); @@ -7770,15 +7785,16 @@ test_write_gateway (void) g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, "1.1.1.254", NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); - addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.254", &error); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); - addr = nm_ip_address_new (AF_INET, "2.2.2.5", 24, "2.2.2.254", &error); + addr = nm_ip_address_new (AF_INET, "2.2.2.5", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); @@ -8319,10 +8335,13 @@ test_write_wifi_wep_adhoc (void) s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); + g_object_set (s_ip4, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, "1.1.1.1", + NULL); /* IP Address */ - addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); @@ -9313,10 +9332,13 @@ test_write_wifi_wpa_psk_adhoc (void) s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); nm_connection_add_setting (connection, NM_SETTING (s_ip4)); - g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); + g_object_set (s_ip4, + NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, "1.1.1.1", + NULL); /* IP Address */ - addr = nm_ip_address_new (AF_INET, "1.1.1.3", 25, "1.1.1.1", &error); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 25, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); @@ -11262,10 +11284,11 @@ test_write_bridge_main (void) g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, "1.1.1.1", NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); - addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); @@ -12040,10 +12063,11 @@ test_write_bond_main (void) g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, "1.1.1.1", NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); - addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); @@ -12380,10 +12404,11 @@ test_write_infiniband (void) g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, "1.1.1.1", NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); - addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, "1.1.1.1", &error); + addr = nm_ip_address_new (AF_INET, "1.1.1.3", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, addr); nm_ip_address_unref (addr); diff --git a/src/settings/plugins/ifcfg-rh/writer.c b/src/settings/plugins/ifcfg-rh/writer.c index 694ed4315e..559342d4cd 100644 --- a/src/settings/plugins/ifcfg-rh/writer.c +++ b/src/settings/plugins/ifcfg-rh/writer.c @@ -1915,8 +1915,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) g_free (tmp); svSetValue (ifcfg, netmask_key, NULL, FALSE); - - svSetValue (ifcfg, gw_key, n == 0 ? nm_ip_address_get_gateway (addr) : NULL, FALSE); + svSetValue (ifcfg, gw_key, NULL, FALSE); g_free (addr_key); g_free (prefix_key); @@ -1943,6 +1942,8 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) g_free (gw_key); } + svSetValue (ifcfg, "GATEWAY", nm_setting_ip_config_get_gateway (s_ip4), FALSE); + num = nm_setting_ip_config_get_num_dns (s_ip4); for (i = 0; i < 254; i++) { const char *dns; @@ -2172,8 +2173,6 @@ write_ip4_aliases (NMConnection *connection, char *base_ifcfg_path) svSetValue (ifcfg, "PREFIX", tmp, FALSE); g_free (tmp); - svSetValue (ifcfg, "GATEWAY", i == 0 ? nm_ip_address_get_gateway (addr) : NULL, FALSE); - svWriteFile (ifcfg, 0644, NULL); svCloseFile (ifcfg); } @@ -2237,7 +2236,6 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) char *addr_key; guint32 i, num, num4; GString *searches; - const char *ipv6_defaultgw; NMIPAddress *addr; const char *dns; GString *ip_str1, *ip_str2, *ip_ptr; @@ -2292,7 +2290,6 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) num = nm_setting_ip_config_get_num_addresses (s_ip6); ip_str1 = g_string_new (NULL); ip_str2 = g_string_new (NULL); - ipv6_defaultgw = NULL; for (i = 0; i < num; i++) { if (i == 0) ip_ptr = ip_str1; @@ -2306,14 +2303,10 @@ write_ip6_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) g_string_append_printf (ip_ptr, "%s/%u", nm_ip_address_get_address (addr), nm_ip_address_get_prefix (addr)); - - /* We only support gateway for the first IP address */ - if (i == 0) - ipv6_defaultgw = nm_ip_address_get_gateway (addr); } svSetValue (ifcfg, "IPV6ADDR", ip_str1->str, FALSE); svSetValue (ifcfg, "IPV6ADDR_SECONDARIES", ip_str2->str, FALSE); - svSetValue (ifcfg, "IPV6_DEFAULTGW", ipv6_defaultgw, FALSE); + svSetValue (ifcfg, "IPV6_DEFAULTGW", nm_setting_ip_config_get_gateway (s_ip6), FALSE); g_string_free (ip_str1, TRUE); g_string_free (ip_str2, TRUE); diff --git a/src/settings/plugins/ifnet/connection_parser.c b/src/settings/plugins/ifnet/connection_parser.c index 6deee55f0a..3880e8f274 100644 --- a/src/settings/plugins/ifnet/connection_parser.c +++ b/src/settings/plugins/ifnet/connection_parser.c @@ -614,9 +614,7 @@ make_ip4_setting (NMConnection *connection, NMIPAddress *ip4_addr; GError *local = NULL; - ip4_addr = nm_ip_address_new (AF_INET, iblock->ip, iblock->prefix, - nm_setting_ip_config_get_num_addresses (ip4_setting) == 0 ? iblock->next_hop : NULL, - &local); + ip4_addr = nm_ip_address_new (AF_INET, iblock->ip, iblock->prefix, &local); if (iblock->next_hop) g_object_set (ip4_setting, NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, @@ -795,7 +793,7 @@ make_ip6_setting (NMConnection *connection, NMIPAddress *ip6_addr; GError *local = NULL; - ip6_addr = nm_ip_address_new (AF_INET6, iblock->ip, iblock->prefix, NULL, &local); + ip6_addr = nm_ip_address_new (AF_INET6, iblock->ip, iblock->prefix, &local); if (ip6_addr) { if (nm_setting_ip_config_add_address (s_ip6, ip6_addr)) { nm_log_info (LOGD_SETTINGS, "ipv6 addresses count: %d", @@ -2408,10 +2406,10 @@ write_ip4_setting (NMConnection *connection, const char *conn_name, GError **err nm_ip_address_get_prefix (addr)); /* only the first gateway will be written */ - if (i == 0 && nm_ip_address_get_gateway (addr)) { + if (i == 0 && nm_setting_ip_config_get_gateway (s_ip4)) { g_string_append_printf (routes, "\"default via %s\" ", - nm_ip_address_get_gateway (addr)); + nm_setting_ip_config_get_gateway (s_ip4)); } } ifnet_set_data (conn_name, "config", ips->str); diff --git a/src/settings/plugins/ifupdown/parser.c b/src/settings/plugins/ifupdown/parser.c index 8dc37dcd7d..4ccfb62f8a 100644 --- a/src/settings/plugins/ifupdown/parser.c +++ b/src/settings/plugins/ifupdown/parser.c @@ -480,15 +480,8 @@ update_ip4_setting_from_if_block(NMConnection *connection, } } - /* gateway */ - gateway_v = ifparser_getkey (block, "gateway"); - if (!gateway_v) - gateway_v = address_v; /* dcbw: whaaa?? */ - /* Add the new address to the setting */ - addr = nm_ip_address_new (AF_INET, address_v, netmask_int, - nm_setting_ip_config_get_num_addresses (s_ip4) == 0 ? gateway_v : NULL, - error); + addr = nm_ip_address_new (AF_INET, address_v, netmask_int, error); if (!addr) goto error; @@ -500,6 +493,18 @@ update_ip4_setting_from_if_block(NMConnection *connection, } nm_ip_address_unref (addr); + /* gateway */ + gateway_v = ifparser_getkey (block, "gateway"); + if (gateway_v) { + if (!nm_utils_ipaddr_valid (AF_INET, gateway_v)) { + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, + "Invalid IPv4 gateway '%s'", gateway_v); + goto error; + } + if (!nm_setting_ip_config_get_gateway (s_ip4)) + g_object_set (s_ip4, NM_SETTING_IP_CONFIG_GATEWAY, gateway_v, NULL); + } + nameserver_v = ifparser_getkey (block, "dns-nameserver"); ifupdown_ip4_add_dns (s_ip4, nameserver_v); @@ -595,15 +600,8 @@ update_ip6_setting_from_if_block(NMConnection *connection, if (prefix_v) prefix_int = g_ascii_strtoll (prefix_v, NULL, 10); - /* Gateway */ - gateway_v = ifparser_getkey (block, "gateway"); - if (!gateway_v) - gateway_v = address_v; /* dcbw: whaaa?? */ - /* Add the new address to the setting */ - addr = nm_ip_address_new (AF_INET6, address_v, prefix_int, - nm_setting_ip_config_get_num_addresses (s_ip6) == 0 ? gateway_v : NULL, - error); + addr = nm_ip_address_new (AF_INET6, address_v, prefix_int, error); if (!addr) goto error; @@ -615,6 +613,18 @@ update_ip6_setting_from_if_block(NMConnection *connection, } nm_ip_address_unref (addr); + /* gateway */ + gateway_v = ifparser_getkey (block, "gateway"); + if (gateway_v) { + if (!nm_utils_ipaddr_valid (AF_INET6, gateway_v)) { + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, + "Invalid IPv6 gateway '%s'", gateway_v); + goto error; + } + if (!nm_setting_ip_config_get_gateway (s_ip6)) + g_object_set (s_ip6, NM_SETTING_IP_CONFIG_GATEWAY, gateway_v, NULL); + } + nameserver_v = ifparser_getkey(block, "dns-nameserver"); ifupdown_ip6_add_dns (s_ip6, nameserver_v); diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c index 46c80552e9..793e485993 100644 --- a/src/settings/plugins/keyfile/reader.c +++ b/src/settings/plugins/keyfile/reader.c @@ -108,15 +108,37 @@ get_one_int (const char *str, guint32 max_val, const char *key_name, guint32 *ou } static gpointer -build_address_or_route (const char *key_name, const char *address_str, guint32 plen, const char *gateway_str, const char *metric_str, int family, gboolean route) +build_address (int family, const char *address_str, guint32 plen) { - gpointer result; - guint32 metric = 0; + NMIPAddress *addr; GError *error = NULL; g_return_val_if_fail (address_str, NULL); - /* Gateway */ + addr = nm_ip_address_new (family, address_str, plen, &error); + if (!addr) { + nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid %s address: %s", __func__, + family == AF_INET ? "IPv4" : "IPv6", + error->message); + g_error_free (error); + } + + return addr; +} + +static gpointer +build_route (int family, + const char *dest_str, guint32 plen, + const char *gateway_str, const char *metric_str, + const char *key_name) +{ + NMIPRoute *route; + guint32 metric = 0; + GError *error = NULL; + + g_return_val_if_fail (dest_str, NULL); + + /* Next hop */ if (gateway_str && gateway_str[0]) { if (!nm_utils_ipaddr_valid (family, gateway_str)) { /* Try workaround for routes written by broken keyfile writer. @@ -128,7 +150,6 @@ build_address_or_route (const char *key_name, const char *address_str, guint32 p * supported. **/ if ( family == AF_INET6 - && route && !metric_str && get_one_int (gateway_str, G_MAXUINT32, NULL, &metric)) gateway_str = NULL; @@ -146,19 +167,15 @@ build_address_or_route (const char *key_name, const char *address_str, guint32 p return NULL; } - if (route) - result = nm_ip_route_new (family, address_str, plen, gateway_str, metric, &error); - else - result = nm_ip_address_new (family, address_str, plen, gateway_str, &error); - if (!result) { - nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid %s %s: %s", __func__, + route = nm_ip_route_new (family, dest_str, plen, gateway_str, metric, &error); + if (!route) { + nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid %s route: %s", __func__, family == AF_INET ? "IPv4" : "IPv6", - route ? "route" : "address", error->message); g_error_free (error); } - return result; + return route; } /* On success, returns pointer to the zero-terminated field (original @current). @@ -251,7 +268,8 @@ read_one_ip_address_or_route (GKeyFile *file, const char *setting_name, const char *key_name, gboolean ipv6, - gboolean route) + gboolean route, + char **out_gateway) { guint32 plen; gpointer result; @@ -317,8 +335,16 @@ read_one_ip_address_or_route (GKeyFile *file, } /* build the appropriate data structure for NetworkManager settings */ - result = build_address_or_route (key_name, address_str, plen, gateway_str, metric_str, - ipv6 ? AF_INET6 : AF_INET, route); + if (route) { + result = build_route (ipv6 ? AF_INET6 : AF_INET, + address_str, plen, gateway_str, metric_str, + key_name); + } else { + result = build_address (ipv6 ? AF_INET6 : AF_INET, + address_str, plen); + if (out_gateway && gateway_str) + *out_gateway = g_strdup (gateway_str); + } g_free (value); return result; @@ -336,6 +362,7 @@ ip_address_or_route_parser (NMSetting *setting, const char *key, GKeyFile *keyfi static const char *key_names_routes[] = { "route", "routes", NULL }; static const char *key_names_addresses[] = { "address", "addresses", NULL }; const char **key_names = routes ? key_names_routes : key_names_addresses; + char *gateway = NULL; GPtrArray *list; GDestroyNotify free_func; int i; @@ -359,21 +386,23 @@ ip_address_or_route_parser (NMSetting *setting, const char *key, GKeyFile *keyfi else key_name = g_strdup (*key_basename); - item = read_one_ip_address_or_route (keyfile, setting_name, key_name, ipv6, routes); - g_free (key_name); - - if (!item) - continue; + item = read_one_ip_address_or_route (keyfile, setting_name, key_name, ipv6, routes, + gateway ? NULL : &gateway); + if (item) + g_ptr_array_add (list, item); - if (!routes && list->len > 0) - nm_ip_address_set_gateway (item, NULL); - g_ptr_array_add (list, item); + g_free (key_name); } } if (list->len >= 1) g_object_set (setting, key, list, NULL); + if (gateway) { + g_object_set (setting, "gateway", gateway, NULL); + g_free (gateway); + } + g_ptr_array_unref (list); } diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index 9cc98615ff..2e0b619dad 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -38,14 +38,13 @@ #define TEST_WIRELESS_FILE TEST_KEYFILES_DIR"/Test_Wireless_Connection" static void -check_ip_address (NMSettingIPConfig *config, int idx, const char *address, int plen, const char *gateway) +check_ip_address (NMSettingIPConfig *config, int idx, const char *address, int plen) { NMIPAddress *ip4 = nm_setting_ip_config_get_address (config, idx); g_assert (ip4); g_assert_cmpstr (nm_ip_address_get_address (ip4), ==, address); g_assert_cmpint (nm_ip_address_get_prefix (ip4), ==, plen); - g_assert_cmpstr (nm_ip_address_get_gateway (ip4), ==, gateway); } static void @@ -246,12 +245,19 @@ test_read_valid_wired_connection (void) /* IPv4 addresses */ g_assert (nm_setting_ip_config_get_num_addresses (s_ip4) == 6); - check_ip_address (s_ip4, 0, "2.3.4.5", 24, "2.3.4.6"); - check_ip_address (s_ip4, 1, "192.168.0.5", 24, NULL); - check_ip_address (s_ip4, 2, "1.2.3.4", 16, NULL); - check_ip_address (s_ip4, 3, "3.4.5.6", 16, NULL); - check_ip_address (s_ip4, 4, "4.5.6.7", 24, NULL); - check_ip_address (s_ip4, 5, "5.6.7.8", 24, NULL); + check_ip_address (s_ip4, 0, "2.3.4.5", 24); + check_ip_address (s_ip4, 1, "192.168.0.5", 24); + check_ip_address (s_ip4, 2, "1.2.3.4", 16); + check_ip_address (s_ip4, 3, "3.4.5.6", 16); + check_ip_address (s_ip4, 4, "4.5.6.7", 24); + check_ip_address (s_ip4, 5, "5.6.7.8", 24); + + /* IPv4 gateway */ + ASSERT (strcmp (nm_setting_ip_config_get_gateway (s_ip4), "2.3.4.6") == 0, + "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", + TEST_WIRED_FILE, + NM_SETTING_IP4_CONFIG_SETTING_NAME, + NM_SETTING_IP_CONFIG_GATEWAY); /* IPv4 routes */ g_assert (nm_setting_ip_config_get_num_routes (s_ip4) == 12); @@ -328,16 +334,23 @@ test_read_valid_wired_connection (void) /* IPv6 addresses */ g_assert (nm_setting_ip_config_get_num_addresses (s_ip6) == 10); - check_ip_address (s_ip6, 0, "2:3:4:5:6:7:8:9", 64, "2:3:4:5:1:2:3:4"); - check_ip_address (s_ip6, 1, "abcd:1234:ffff::cdde", 64, NULL); - check_ip_address (s_ip6, 2, "1:2:3:4:5:6:7:8", 96, NULL); - check_ip_address (s_ip6, 3, "3:4:5:6:7:8:9:0", 128, NULL); - check_ip_address (s_ip6, 4, "3:4:5:6:7:8:9:14", 64, NULL); - check_ip_address (s_ip6, 5, "3:4:5:6:7:8:9:15", 64, NULL); - check_ip_address (s_ip6, 6, "3:4:5:6:7:8:9:16", 66, NULL); - check_ip_address (s_ip6, 7, "3:4:5:6:7:8:9:17", 67, NULL); - check_ip_address (s_ip6, 8, "3:4:5:6:7:8:9:18", 68, NULL); - check_ip_address (s_ip6, 9, "3:4:5:6:7:8:9:19", 69, NULL); + check_ip_address (s_ip6, 0, "2:3:4:5:6:7:8:9", 64); + check_ip_address (s_ip6, 1, "abcd:1234:ffff::cdde", 64); + check_ip_address (s_ip6, 2, "1:2:3:4:5:6:7:8", 96); + check_ip_address (s_ip6, 3, "3:4:5:6:7:8:9:0", 128); + check_ip_address (s_ip6, 4, "3:4:5:6:7:8:9:14", 64); + check_ip_address (s_ip6, 5, "3:4:5:6:7:8:9:15", 64); + check_ip_address (s_ip6, 6, "3:4:5:6:7:8:9:16", 66); + check_ip_address (s_ip6, 7, "3:4:5:6:7:8:9:17", 67); + check_ip_address (s_ip6, 8, "3:4:5:6:7:8:9:18", 68); + check_ip_address (s_ip6, 9, "3:4:5:6:7:8:9:19", 69); + + /* IPv6 gateway */ + ASSERT (strcmp (nm_setting_ip_config_get_gateway (s_ip6), "2:3:4:5:1:2:3:4") == 0, + "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", + TEST_WIRED_FILE, + NM_SETTING_IP6_CONFIG_SETTING_NAME, + NM_SETTING_IP_CONFIG_GATEWAY); /* Route #1 */ g_assert (nm_setting_ip_config_get_num_routes (s_ip6) == 7); @@ -354,14 +367,13 @@ test_read_valid_wired_connection (void) static void add_one_ip_address (NMSettingIPConfig *s_ip, const char *addr, - guint32 prefix, - const char *gw) + guint32 prefix) { NMIPAddress *ip_addr; GError *error = NULL; ip_addr = nm_ip_address_new (NM_IS_SETTING_IP4_CONFIG (s_ip) ? AF_INET : AF_INET6, - addr, prefix, gw, &error); + addr, prefix, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip, ip_addr); nm_ip_address_unref (ip_addr); @@ -404,8 +416,8 @@ test_write_wired_connection (void) const char *dns1 = "4.2.2.1"; const char *dns2 = "4.2.2.2"; const char *address1 = "192.168.0.5"; - const char *address1_gw = "192.168.0.1"; const char *address2 = "1.2.3.4"; + const char *gw = "192.168.0.1"; const char *route1 = "10.10.10.2"; const char *route1_nh = "10.10.10.1"; const char *route2 = "1.1.1.1"; @@ -462,11 +474,12 @@ test_write_wired_connection (void) g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, gw, NULL); /* Addresses */ - add_one_ip_address (s_ip4, address1, 24, address1_gw); - add_one_ip_address (s_ip4, address2, 8, NULL); + add_one_ip_address (s_ip4, address1, 24); + add_one_ip_address (s_ip4, address2, 8); /* Routes */ add_one_ip_route (s_ip4, route1, route1_nh, 24, 3); @@ -488,8 +501,8 @@ test_write_wired_connection (void) NULL); /* Addresses */ - add_one_ip_address (s_ip6, address6_1, 64, NULL); - add_one_ip_address (s_ip6, address6_2, 56, NULL); + add_one_ip_address (s_ip6, address6_1, 64); + add_one_ip_address (s_ip6, address6_2, 56); /* Routes */ add_one_ip_route (s_ip6, route6_1, route6_1_nh, 64, 3); @@ -634,7 +647,14 @@ test_read_ip6_wired_connection (void) /* IPv6 address */ g_assert (nm_setting_ip_config_get_num_addresses (s_ip6) == 1); - check_ip_address (s_ip6, 0, "abcd:1234:ffff::cdde", 64, "abcd:1234:ffff::cdd1"); + check_ip_address (s_ip6, 0, "abcd:1234:ffff::cdde", 64); + + /* IPv6 gateway */ + ASSERT (strcmp (nm_setting_ip_config_get_gateway (s_ip6), "abcd:1234:ffff::cdd1") == 0, + "connection-verify-wired", "failed to verify %s: unexpected %s / %s key value", + TEST_WIRED_IP6_FILE, + NM_SETTING_IP6_CONFIG_SETTING_NAME, + NM_SETTING_IP_CONFIG_GATEWAY); g_object_unref (connection); } @@ -695,10 +715,11 @@ test_write_ip6_wired_connection (void) g_object_set (s_ip6, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, gw, NULL); /* Addresses */ - add_one_ip_address (s_ip6, address, 64, gw); + add_one_ip_address (s_ip6, address, 64); /* DNS servers */ nm_setting_ip_config_add_dns (s_ip6, dns); @@ -2816,9 +2837,10 @@ test_write_bridge_main (void) g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, + NM_SETTING_IP_CONFIG_GATEWAY, "1.1.1.1", NULL); - add_one_ip_address (s_ip4, "1.2.3.4", 24, "1.1.1.1"); + add_one_ip_address (s_ip4, "1.2.3.4", 24); /* IP6 setting */ s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new (); diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c index e6515398e2..24d7aba991 100644 --- a/src/settings/plugins/keyfile/writer.c +++ b/src/settings/plugins/keyfile/writer.c @@ -117,6 +117,7 @@ static void write_ip_values (GKeyFile *file, const char *setting_name, GPtrArray *array, + const char *gateway, gboolean is_route) { GString *output; @@ -147,10 +148,7 @@ write_ip_values (GKeyFile *file, addr = nm_ip_address_get_address (address); plen = nm_ip_address_get_prefix (address); - if (i == 0) - gw = nm_ip_address_get_gateway (address); - else - gw = NULL; + gw = i == 0 ? gateway : NULL; metric = 0; } @@ -190,10 +188,11 @@ addr_writer (GKeyFile *file, { GPtrArray *array; const char *setting_name = nm_setting_get_name (setting); + const char *gateway = nm_setting_ip_config_get_gateway (NM_SETTING_IP_CONFIG (setting)); array = (GPtrArray *) g_value_get_boxed (value); if (array && array->len) - write_ip_values (file, setting_name, array, FALSE); + write_ip_values (file, setting_name, array, gateway, FALSE); } static void @@ -207,6 +206,17 @@ ip4_addr_label_writer (GKeyFile *file, /* skip */ } +static void +gateway_writer (GKeyFile *file, + const char *keyfile_dir, + const char *uuid, + NMSetting *setting, + const char *key, + const GValue *value) +{ + /* skip */ +} + static void route_writer (GKeyFile *file, const char *keyfile_dir, @@ -220,7 +230,7 @@ route_writer (GKeyFile *file, array = (GPtrArray *) g_value_get_boxed (value); if (array && array->len) - write_ip_values (file, setting_name, array, TRUE); + write_ip_values (file, setting_name, array, NULL, TRUE); } static void @@ -589,6 +599,12 @@ static KeyWriter key_writers[] = { { NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_ADDRESSES, addr_writer }, + { NM_SETTING_IP4_CONFIG_SETTING_NAME, + NM_SETTING_IP_CONFIG_GATEWAY, + gateway_writer }, + { NM_SETTING_IP6_CONFIG_SETTING_NAME, + NM_SETTING_IP_CONFIG_GATEWAY, + gateway_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_ROUTES, route_writer }, diff --git a/src/tests/test-general.c b/src/tests/test-general.c index 2f073c9cac..968a4bae87 100644 --- a/src/tests/test-general.c +++ b/src/tests/test-general.c @@ -601,8 +601,9 @@ test_connection_no_match_ip4_addr (void) g_assert (s_ip4); g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, "1.1.1.254", NULL); - nm_addr = nm_ip_address_new (AF_INET, "1.1.1.4", 24, "1.1.1.254", &error); + nm_addr = nm_ip_address_new (AF_INET, "1.1.1.4", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, nm_addr); nm_ip_address_unref (nm_addr); @@ -611,8 +612,9 @@ test_connection_no_match_ip4_addr (void) g_assert (s_ip4); g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, + NM_SETTING_IP_CONFIG_GATEWAY, "2.2.2.254", NULL); - nm_addr = nm_ip_address_new (AF_INET, "2.2.2.4", 24, "2.2.2.254", &error); + nm_addr = nm_ip_address_new (AF_INET, "2.2.2.4", 24, &error); g_assert_no_error (error); nm_setting_ip_config_add_address (s_ip4, nm_addr); nm_ip_address_unref (nm_addr); -- cgit v1.2.1 From d16905df633ceea08c93b6e982f660627d06ff34 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 21 Oct 2014 08:33:18 -0400 Subject: libnm-core, libnm, core: add AddressData and RouteData properties Add AddressData and RouteData properties to NMSettingIPConfig and NMIP[46]Config. These are like the existing "addresses" and "routes" properties, but using strings and containing additional attributes, like NMIPAddress and NMIPRoute. This only affects the D-Bus representations; there are no API changes to NMSettingIP{,4,6}Config or NMIP{4,6}Config as a result of this; the additional information is just added to the existing 'addresses' and 'routes' properties. NMSettingIP4Config and NMSettingIP6Config now always generate both old-style data ('addresses', 'address-labels', 'routes') and new-style data ('address-data', 'gateway', 'route-data') when serializing to D-Bus, for backward compatibility. When deserializing, they will fill in the 'addresses' and 'routes' properties from the new-style data if it is present (ignoring the old-style data), or from the old-style data if the new-style isn't present. The daemon-side NMIP4Config and NMIP6Config always emit changes for both 'Addresses'/'Routes' and 'AddressData'/'RouteData'. The libnm-side classes initially listen for changes on both properties, but start ignoring the 'Addresses' and 'Routes' properties once they know the daemon is also providing 'AddressData' and 'RouteData'. --- include/nm-dbus-glib-types.h | 5 + introspection/nm-ip4-config.xml | 40 +++++-- introspection/nm-ip6-config.xml | 31 ++++- libnm-core/nm-setting-ip-config.c | 3 + libnm-core/nm-setting-ip4-config.c | 122 +++++++++++++++++-- libnm-core/nm-setting-ip6-config.c | 121 +++++++++++++++++-- libnm-core/nm-utils.c | 239 +++++++++++++++++++++++++++++++++++++ libnm-core/nm-utils.h | 7 ++ libnm-core/tests/test-general.c | 70 +++++++++-- libnm/nm-ip4-config.c | 46 ++++++- libnm/nm-ip6-config.c | 46 ++++++- src/nm-ip4-config.c | 133 ++++++++++++++++++--- src/nm-ip4-config.h | 8 +- src/nm-ip6-config.c | 130 +++++++++++++++++--- src/nm-ip6-config.h | 8 +- 15 files changed, 923 insertions(+), 86 deletions(-) diff --git a/include/nm-dbus-glib-types.h b/include/nm-dbus-glib-types.h index 66f45685a7..1b41c9ff18 100644 --- a/include/nm-dbus-glib-types.h +++ b/include/nm-dbus-glib-types.h @@ -37,4 +37,9 @@ #define DBUS_TYPE_G_IP6_ROUTE (dbus_g_type_get_struct ("GValueArray", DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT, DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT, G_TYPE_INVALID)) #define DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_IP6_ROUTE)) +#define DBUS_TYPE_NM_IP_ADDRESS (dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_UINT, DBUS_TYPE_G_MAP_OF_STRING, G_TYPE_INVALID)) +#define DBUS_TYPE_NM_IP_ADDRESSES (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_NM_IP_ADDRESS)) +#define DBUS_TYPE_NM_IP_ROUTE (dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_UINT, DBUS_TYPE_G_MAP_OF_STRING, G_TYPE_INVALID)) +#define DBUS_TYPE_NM_IP_ROUTES (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_NM_IP_ROUTE)) + #endif /* __NM_DBUS_GLIB_TYPES_H__ */ diff --git a/introspection/nm-ip4-config.xml b/introspection/nm-ip4-config.xml index 6a8750b7f6..9807653495 100644 --- a/introspection/nm-ip4-config.xml +++ b/introspection/nm-ip4-config.xml @@ -2,20 +2,42 @@ + + + Array of arrays of IPv4 address/prefix/gateway. All 3 + elements of each array are in network byte order. Essentially: + [(addr, prefix, gateway), (addr, prefix, gateway), ...] + + Deprecated: use AddressData and Gateway + + + + + Array of IP address data objects. All addresses will include + "address" (an IP address string), and "prefix" (a uint). Some + addresses may include additional attributes. + + The gateway in use. - - Array of tuples of IPv4 address/prefix/gateway. All 3 - elements of each tuple are in network byte order. Essentially: - [(addr, prefix, gateway), (addr, prefix, gateway), ...] + + + Arrays of IPv4 route/prefix/next-hop/metric. All 4 elements of + each tuple are in network byte order. 'route' and 'next hop' + are IPv4 addresses, while prefix and metric are simple + unsigned integers. Essentially: [(route, prefix, next-hop, + metric), (route, prefix, next-hop, metric), ...] + + Deprecated: use RouteData - - Tuples of IPv4 route/prefix/next-hop/metric. All 4 elements - of each tuple are in network byte order. 'route' and 'next hop' are IPv4 - addresses, while prefix and metric are simple unsigned integers. Essentially: - [(route, prefix, next-hop, metric), (route, prefix, next-hop, metric), ...] + + + Array of IP route data objects. All routes will include "dest" + (an IP address string) and "prefix" (a uint). Some routes may + include "next-hop" (an IP address string), "metric" (a uint), + and additional attributes. diff --git a/introspection/nm-ip6-config.xml b/introspection/nm-ip6-config.xml index 55c519e701..985dd2e331 100644 --- a/introspection/nm-ip6-config.xml +++ b/introspection/nm-ip6-config.xml @@ -2,14 +2,37 @@ + + + Array of tuples of IPv6 address/prefix/gateway. + + Deprecated: use AddressData and Gateway. + + + + + Array of IP address data objects. All addresses will include + "address" (an IP address string), and "prefix" (a uint). Some + addresses may include additional attributes. + + The gateway in use. - - Tuples of IPv6 address/prefix/gateway. - - Tuples of IPv6 route/prefix/next-hop/metric. + + Tuples of IPv6 route/prefix/next-hop/metric. + + Deprecated: use RouteData + + + + + Array of IP route data objects. All routes will include "dest" + (an IP address string) and "prefix" (a uint). Some routes may + include "next-hop" (an IP address string), "metric" (a uint), + and additional attributes. + The nameservers in use. diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c index 0ed612cd06..30e7b267c8 100644 --- a/libnm-core/nm-setting-ip-config.c +++ b/libnm-core/nm-setting-ip-config.c @@ -472,6 +472,7 @@ nm_ip_address_set_attribute (NMIPAddress *address, const char *name, GVariant *v { g_return_if_fail (address != NULL); g_return_if_fail (name != NULL && *name != '\0'); + g_return_if_fail (strcmp (name, "address") != 0 && strcmp (name, "prefix") != 0); if (!address->attributes) { address->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, @@ -1009,6 +1010,8 @@ nm_ip_route_set_attribute (NMIPRoute *route, const char *name, GVariant *value) { g_return_if_fail (route != NULL); g_return_if_fail (name != NULL && *name != '\0'); + g_return_if_fail ( strcmp (name, "dest") != 0 && strcmp (name, "prefix") != 0 + && strcmp (name, "next-hop") != 0 && strcmp (name, "metric") != 0); if (!route->attributes) { route->attributes = g_hash_table_new_full (g_str_hash, g_str_equal, diff --git a/libnm-core/nm-setting-ip4-config.c b/libnm-core/nm-setting-ip4-config.c index 66f2ea997e..9eab49d7f5 100644 --- a/libnm-core/nm-setting-ip4-config.c +++ b/libnm-core/nm-setting-ip4-config.c @@ -269,9 +269,14 @@ ip4_addresses_set (NMSetting *setting, char **labels, *gateway = NULL; int i; - addrs = nm_utils_ip4_addresses_from_variant (value, &gateway); - s_ip4 = g_variant_lookup_value (connection_dict, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); + /* If 'address-data' is set then ignore 'addresses' */ + if (g_variant_lookup (s_ip4, "address-data", "aa{sv}", NULL)) { + g_variant_unref (s_ip4); + return; + } + + addrs = nm_utils_ip4_addresses_from_variant (value, &gateway); if (g_variant_lookup (s_ip4, "address-labels", "^as", &labels)) { for (i = 0; i < addrs->len && labels[i]; i++) @@ -319,16 +324,95 @@ ip4_address_labels_get (NMSetting *setting, } static GVariant * -ip4_routes_to_dbus (const GValue *prop_value) +ip4_address_data_get (NMSetting *setting, + NMConnection *connection, + const char *property) +{ + GPtrArray *addrs; + GVariant *ret; + + g_object_get (setting, NM_SETTING_IP_CONFIG_ADDRESSES, &addrs, NULL); + ret = nm_utils_ip_addresses_to_variant (addrs); + g_ptr_array_unref (addrs); + + return ret; +} + +static void +ip4_address_data_set (NMSetting *setting, + GVariant *connection_dict, + const char *property, + GVariant *value) +{ + GPtrArray *addrs; + + addrs = nm_utils_ip_addresses_from_variant (value, AF_INET); + g_object_set (setting, NM_SETTING_IP_CONFIG_ADDRESSES, addrs, NULL); + g_ptr_array_unref (addrs); +} + +static GVariant * +ip4_routes_get (NMSetting *setting, + const char *property) +{ + GPtrArray *routes; + GVariant *ret; + + g_object_get (setting, property, &routes, NULL); + ret = nm_utils_ip4_routes_to_variant (routes); + g_ptr_array_unref (routes); + + return ret; +} + +static void +ip4_routes_set (NMSetting *setting, + GVariant *connection_dict, + const char *property, + GVariant *value) +{ + GPtrArray *routes; + GVariant *s_ip4; + + s_ip4 = g_variant_lookup_value (connection_dict, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); + /* If 'route-data' is set then ignore 'routes' */ + if (g_variant_lookup (s_ip4, "route-data", "aa{sv}", NULL)) { + g_variant_unref (s_ip4); + return; + } + g_variant_unref (s_ip4); + + routes = nm_utils_ip4_routes_from_variant (value); + g_object_set (setting, property, routes, NULL); + g_ptr_array_unref (routes); +} + +static GVariant * +ip4_route_data_get (NMSetting *setting, + NMConnection *connection, + const char *property) { - return nm_utils_ip4_routes_to_variant (g_value_get_boxed (prop_value)); + GPtrArray *routes; + GVariant *ret; + + g_object_get (setting, NM_SETTING_IP_CONFIG_ROUTES, &routes, NULL); + ret = nm_utils_ip_routes_to_variant (routes); + g_ptr_array_unref (routes); + + return ret; } static void -ip4_routes_from_dbus (GVariant *dbus_value, - GValue *prop_value) +ip4_route_data_set (NMSetting *setting, + GVariant *connection_dict, + const char *property, + GVariant *value) { - g_value_take_boxed (prop_value, nm_utils_ip4_routes_from_variant (dbus_value)); + GPtrArray *routes; + + routes = nm_utils_ip_routes_from_variant (value, AF_INET); + g_object_set (setting, NM_SETTING_IP_CONFIG_ROUTES, routes, NULL); + g_ptr_array_unref (routes); } @@ -381,9 +465,23 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *ip4_class) ip4_address_labels_get, NULL); - _nm_setting_class_transform_property (setting_class, - NM_SETTING_IP_CONFIG_ROUTES, - G_VARIANT_TYPE ("aau"), - ip4_routes_to_dbus, - ip4_routes_from_dbus); + _nm_setting_class_add_dbus_only_property (setting_class, + "address-data", + G_VARIANT_TYPE ("aa{sv}"), + ip4_address_data_get, + ip4_address_data_set); + + _nm_setting_class_override_property (setting_class, + NM_SETTING_IP_CONFIG_ROUTES, + G_VARIANT_TYPE ("aau"), + ip4_routes_get, + ip4_routes_set, + NULL); + + _nm_setting_class_add_dbus_only_property (setting_class, + "route-data", + G_VARIANT_TYPE ("aa{sv}"), + ip4_route_data_get, + ip4_route_data_set); + } diff --git a/libnm-core/nm-setting-ip6-config.c b/libnm-core/nm-setting-ip6-config.c index 9e3001157e..6e3ff6164e 100644 --- a/libnm-core/nm-setting-ip6-config.c +++ b/libnm-core/nm-setting-ip6-config.c @@ -214,9 +214,14 @@ ip6_addresses_set (NMSetting *setting, GVariant *s_ip6; char *gateway = NULL; - addrs = nm_utils_ip6_addresses_from_variant (value, &gateway); - s_ip6 = g_variant_lookup_value (connection_dict, NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); + /* If 'address-data' is set then ignore 'addresses' */ + if (g_variant_lookup (s_ip6, "address-data", "aa{sv}", NULL)) { + g_variant_unref (s_ip6); + return; + } + + addrs = nm_utils_ip6_addresses_from_variant (value, &gateway); if (gateway && !g_variant_lookup (s_ip6, "gateway", "s", NULL)) { g_object_set (setting, @@ -232,16 +237,95 @@ ip6_addresses_set (NMSetting *setting, } static GVariant * -ip6_routes_to_dbus (const GValue *prop_value) +ip6_address_data_get (NMSetting *setting, + NMConnection *connection, + const char *property) +{ + GPtrArray *addrs; + GVariant *ret; + + g_object_get (setting, NM_SETTING_IP_CONFIG_ADDRESSES, &addrs, NULL); + ret = nm_utils_ip_addresses_to_variant (addrs); + g_ptr_array_unref (addrs); + + return ret; +} + +static void +ip6_address_data_set (NMSetting *setting, + GVariant *connection_dict, + const char *property, + GVariant *value) +{ + GPtrArray *addrs; + + addrs = nm_utils_ip_addresses_from_variant (value, AF_INET6); + g_object_set (setting, NM_SETTING_IP_CONFIG_ADDRESSES, addrs, NULL); + g_ptr_array_unref (addrs); +} + +static GVariant * +ip6_routes_get (NMSetting *setting, + const char *property) +{ + GPtrArray *routes; + GVariant *ret; + + g_object_get (setting, property, &routes, NULL); + ret = nm_utils_ip6_routes_to_variant (routes); + g_ptr_array_unref (routes); + + return ret; +} + +static void +ip6_routes_set (NMSetting *setting, + GVariant *connection_dict, + const char *property, + GVariant *value) +{ + GPtrArray *routes; + GVariant *s_ip6; + + s_ip6 = g_variant_lookup_value (connection_dict, NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); + /* If 'route-data' is set then ignore 'routes' */ + if (g_variant_lookup (s_ip6, "route-data", "aa{sv}", NULL)) { + g_variant_unref (s_ip6); + return; + } + g_variant_unref (s_ip6); + + routes = nm_utils_ip6_routes_from_variant (value); + g_object_set (setting, property, routes, NULL); + g_ptr_array_unref (routes); +} + +static GVariant * +ip6_route_data_get (NMSetting *setting, + NMConnection *connection, + const char *property) { - return nm_utils_ip6_routes_to_variant (g_value_get_boxed (prop_value)); + GPtrArray *routes; + GVariant *ret; + + g_object_get (setting, NM_SETTING_IP_CONFIG_ROUTES, &routes, NULL); + ret = nm_utils_ip_routes_to_variant (routes); + g_ptr_array_unref (routes); + + return ret; } static void -ip6_routes_from_dbus (GVariant *dbus_value, - GValue *prop_value) +ip6_route_data_set (NMSetting *setting, + GVariant *connection_dict, + const char *property, + GVariant *value) { - g_value_take_boxed (prop_value, nm_utils_ip6_routes_from_variant (dbus_value)); + GPtrArray *routes; + + routes = nm_utils_ip_routes_from_variant (value, AF_INET6); + g_object_set (setting, NM_SETTING_IP_CONFIG_ROUTES, routes, NULL); + g_ptr_array_unref (routes); } static void @@ -324,9 +408,22 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *ip6_class) ip6_addresses_set, NULL); - _nm_setting_class_transform_property (setting_class, - NM_SETTING_IP_CONFIG_ROUTES, - G_VARIANT_TYPE ("a(ayuayu)"), - ip6_routes_to_dbus, - ip6_routes_from_dbus); + _nm_setting_class_add_dbus_only_property (setting_class, + "address-data", + G_VARIANT_TYPE ("aa{sv}"), + ip6_address_data_get, + ip6_address_data_set); + + _nm_setting_class_override_property (setting_class, + NM_SETTING_IP_CONFIG_ROUTES, + G_VARIANT_TYPE ("a(ayuayu)"), + ip6_routes_get, + ip6_routes_set, + NULL); + + _nm_setting_class_add_dbus_only_property (setting_class, + "route-data", + G_VARIANT_TYPE ("aa{sv}"), + ip6_route_data_get, + ip6_route_data_set); } diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 62698eefc8..f8105da810 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -1711,6 +1711,245 @@ nm_utils_ip6_routes_from_variant (GVariant *value) return routes; } +/** + * nm_utils_ip_addresses_to_variant: + * @addresses: (element-type NMIPAddress): an array of #NMIPAddress objects + * + * Utility function to convert a #GPtrArray of #NMIPAddress objects representing + * IPv4 or IPv6 addresses into a #GVariant of type 'aa{sv}' representing an + * array of new-style NetworkManager IP addresses. All addresses will include + * "address" (an IP address string), and "prefix" (a uint). Some addresses may + * include additional attributes. + * + * Returns: (transfer none): a new floating #GVariant representing @addresses. + **/ +GVariant * +nm_utils_ip_addresses_to_variant (GPtrArray *addresses) +{ + GVariantBuilder builder; + int i; + + g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}")); + + if (addresses) { + for (i = 0; i < addresses->len; i++) { + NMIPAddress *addr = addresses->pdata[i]; + GVariantBuilder addr_builder; + char **names; + int n; + + g_variant_builder_init (&addr_builder, G_VARIANT_TYPE ("a{sv}")); + g_variant_builder_add (&addr_builder, "{sv}", + "address", + g_variant_new_string (nm_ip_address_get_address (addr))); + g_variant_builder_add (&addr_builder, "{sv}", + "prefix", + g_variant_new_uint32 (nm_ip_address_get_prefix (addr))); + + names = nm_ip_address_get_attribute_names (addr); + for (n = 0; names[n]; n++) { + g_variant_builder_add (&addr_builder, "{sv}", + names[n], + nm_ip_address_get_attribute (addr, names[n])); + } + g_strfreev (names); + + g_variant_builder_add (&builder, "a{sv}", &addr_builder); + } + } + + return g_variant_builder_end (&builder); +} + +/** + * nm_utils_ip_addresses_from_variant: + * @value: a #GVariant of type 'aa{sv}' + * @family: an IP address family + * + * Utility function to convert a #GVariant representing a list of new-style + * NetworkManager IPv4 or IPv6 addresses (as described in the documentation for + * nm_utils_ip_addresses_to_variant()) into a #GPtrArray of #NMIPAddress + * objects. + * + * Returns: (transfer full) (element-type NMIPAddress): a newly allocated + * #GPtrArray of #NMIPAddress objects + **/ +GPtrArray * +nm_utils_ip_addresses_from_variant (GVariant *value, + int family) +{ + GPtrArray *addresses; + GVariantIter iter, attrs_iter; + GVariant *addr_var; + const char *ip; + guint32 prefix; + const char *attr_name; + GVariant *attr_val; + NMIPAddress *addr; + GError *error = NULL; + + g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("aa{sv}")), NULL); + + g_variant_iter_init (&iter, value); + addresses = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_address_unref); + + while (g_variant_iter_next (&iter, "@a{sv}", &addr_var)) { + if ( !g_variant_lookup (addr_var, "address", "&s", &ip) + || !g_variant_lookup (addr_var, "prefix", "u", &prefix)) { + g_warning ("Ignoring invalid address"); + g_variant_unref (addr_var); + continue; + } + + addr = nm_ip_address_new (family, ip, prefix, &error); + if (!addr) { + g_warning ("Ignoring invalid address: %s", error->message); + g_clear_error (&error); + g_variant_unref (addr_var); + continue; + } + + g_variant_iter_init (&attrs_iter, addr_var); + while (g_variant_iter_next (&attrs_iter, "{&sv}", &attr_name, &attr_val)) { + if ( strcmp (attr_name, "address") != 0 + && strcmp (attr_name, "prefix") != 0) + nm_ip_address_set_attribute (addr, attr_name, attr_val); + g_variant_unref (attr_val); + } + + g_ptr_array_add (addresses, addr); + } + + return addresses; +} + +/** + * nm_utils_ip_routes_to_variant: + * @routes: (element-type NMIPRoute): an array of #NMIPRoute objects + * + * Utility function to convert a #GPtrArray of #NMIPRoute objects representing + * IPv4 or IPv6 routes into a #GVariant of type 'aa{sv}' representing an array + * of new-style NetworkManager IP routes (which are tuples of destination, + * prefix, next hop, metric, and additional attributes). + * + * Returns: (transfer none): a new floating #GVariant representing @routes. + **/ +GVariant * +nm_utils_ip_routes_to_variant (GPtrArray *routes) +{ + GVariantBuilder builder; + int i; + + g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}")); + + if (routes) { + for (i = 0; i < routes->len; i++) { + NMIPRoute *route = routes->pdata[i]; + GVariantBuilder route_builder; + char **names; + int n; + + g_variant_builder_init (&route_builder, G_VARIANT_TYPE ("a{sv}")); + g_variant_builder_add (&route_builder, "{sv}", + "dest", + g_variant_new_string (nm_ip_route_get_dest (route))); + g_variant_builder_add (&route_builder, "{sv}", + "prefix", + g_variant_new_uint32 (nm_ip_route_get_prefix (route))); + if (nm_ip_route_get_next_hop (route)) { + g_variant_builder_add (&route_builder, "{sv}", + "next-hop", + g_variant_new_string (nm_ip_route_get_next_hop (route))); + } + if (nm_ip_route_get_metric (route)) { + g_variant_builder_add (&route_builder, "{sv}", + "metric", + g_variant_new_uint32 (nm_ip_route_get_metric (route))); + } + + names = nm_ip_route_get_attribute_names (route); + for (n = 0; names[n]; n++) { + g_variant_builder_add (&route_builder, "{sv}", + names[n], + nm_ip_route_get_attribute (route, names[n])); + } + g_strfreev (names); + + g_variant_builder_add (&builder, "a{sv}", &route_builder); + } + } + + return g_variant_builder_end (&builder); +} + +/** + * nm_utils_ip_routes_from_variant: + * @value: a #GVariant of type 'aa{sv}' + * @family: an IP address family + * + * Utility function to convert a #GVariant representing a list of new-style + * NetworkManager IPv4 or IPv6 addresses (which are tuples of destination, + * prefix, next hop, metric, and additional attributes) into a #GPtrArray of + * #NMIPRoute objects. + * + * Returns: (transfer full) (element-type NMIPRoute): a newly allocated + * #GPtrArray of #NMIPRoute objects + **/ +GPtrArray * +nm_utils_ip_routes_from_variant (GVariant *value, + int family) +{ + GPtrArray *routes; + GVariantIter iter, attrs_iter; + GVariant *route_var; + const char *dest, *next_hop; + guint32 prefix, metric; + const char *attr_name; + GVariant *attr_val; + NMIPRoute *route; + GError *error = NULL; + + g_return_val_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE ("aa{sv}")), NULL); + + g_variant_iter_init (&iter, value); + routes = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_ip_route_unref); + + while (g_variant_iter_next (&iter, "@a{sv}", &route_var)) { + if ( !g_variant_lookup (route_var, "dest", "&s", &dest) + || !g_variant_lookup (route_var, "prefix", "u", &prefix)) { + g_warning ("Ignoring invalid address"); + g_variant_unref (route_var); + continue; + } + if (!g_variant_lookup (route_var, "next-hop", "&s", &next_hop)) + next_hop = NULL; + if (!g_variant_lookup (route_var, "metric", "u", &metric)) + metric = 0; + + route = nm_ip_route_new (family, dest, prefix, next_hop, metric, &error); + if (!route) { + g_warning ("Ignoring invalid route: %s", error->message); + g_clear_error (&error); + g_variant_unref (route_var); + continue; + } + + g_variant_iter_init (&attrs_iter, route_var); + while (g_variant_iter_next (&attrs_iter, "{&sv}", &attr_name, &attr_val)) { + if ( strcmp (attr_name, "dest") != 0 + && strcmp (attr_name, "prefix") != 0 + && strcmp (attr_name, "next-hop") != 0 + && strcmp (attr_name, "metric") != 0) + nm_ip_route_set_attribute (route, attr_name, attr_val); + g_variant_unref (attr_val); + } + + g_ptr_array_add (routes, route); + } + + return routes; +} + /** * nm_utils_uuid_generate: * diff --git a/libnm-core/nm-utils.h b/libnm-core/nm-utils.h index 628fa0f5a6..25f09c45ea 100644 --- a/libnm-core/nm-utils.h +++ b/libnm-core/nm-utils.h @@ -117,6 +117,13 @@ GPtrArray *nm_utils_ip6_addresses_from_variant (GVariant *value, GVariant *nm_utils_ip6_routes_to_variant (GPtrArray *routes); GPtrArray *nm_utils_ip6_routes_from_variant (GVariant *value); +GVariant *nm_utils_ip_addresses_to_variant (GPtrArray *addresses); +GPtrArray *nm_utils_ip_addresses_from_variant (GVariant *value, + int family); +GVariant *nm_utils_ip_routes_to_variant (GPtrArray *routes); +GPtrArray *nm_utils_ip_routes_from_variant (GVariant *value, + int family); + char *nm_utils_uuid_generate (void); char *nm_utils_uuid_generate_from_string (const char *s); diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index d66a9c9f6e..1bab4e011c 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -329,7 +329,7 @@ test_setting_ip4_config_labels (void) GPtrArray *addrs; char **labels; NMConnection *conn; - GVariant *dict, *setting_dict, *value; + GVariant *dict, *dict2, *setting_dict, *value; GError *error = NULL; s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); @@ -395,7 +395,9 @@ test_setting_ip4_config_labels (void) label = nm_ip_address_get_attribute (addr, "label"); g_assert (label == NULL); - /* The labels should appear in the D-Bus serialization */ + /* The labels should appear in the D-Bus serialization under both + * 'address-labels' and 'address-data'. + */ conn = nmtst_create_minimal_connection ("label test", NULL, NM_SETTING_WIRED_SETTING_NAME, NULL); nm_connection_add_setting (conn, NM_SETTING (s_ip4)); dict = nm_connection_to_dbus (conn, NM_CONNECTION_SERIALIZE_ALL); @@ -403,19 +405,41 @@ test_setting_ip4_config_labels (void) setting_dict = g_variant_lookup_value (dict, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); g_assert (setting_dict != NULL); + value = g_variant_lookup_value (setting_dict, "address-labels", G_VARIANT_TYPE_STRING_ARRAY); g_assert (value != NULL); - g_variant_get (value, "^as", &labels); g_assert_cmpint (g_strv_length (labels), ==, 2); g_assert_cmpstr (labels[0], ==, "eth0:1"); g_assert_cmpstr (labels[1], ==, ""); - - g_variant_unref (setting_dict); g_variant_unref (value); g_strfreev (labels); - /* And should be deserialized */ + value = g_variant_lookup_value (setting_dict, "address-data", G_VARIANT_TYPE ("aa{sv}")); + addrs = nm_utils_ip_addresses_from_variant (value, AF_INET); + g_variant_unref (value); + g_assert (addrs != NULL); + g_assert_cmpint (addrs->len, ==, 2); + addr = addrs->pdata[0]; + label = nm_ip_address_get_attribute (addr, "label"); + g_assert (label != NULL); + g_assert_cmpstr (g_variant_get_string (label, NULL), ==, "eth0:1"); + addr = addrs->pdata[1]; + label = nm_ip_address_get_attribute (addr, "label"); + g_assert (label == NULL); + g_ptr_array_unref (addrs); + + g_variant_unref (setting_dict); + + /* We should be able to deserialize the labels from either 'address-labels' + * or 'address-data'. + */ + dict2 = g_variant_ref (dict); + + NMTST_VARIANT_EDITOR (dict, + NMTST_VARIANT_DROP_PROPERTY (NM_SETTING_IP4_CONFIG_SETTING_NAME, + "address-data"); + ); conn = nm_simple_connection_new_from_dbus (dict, &error); g_assert_no_error (error); g_variant_unref (dict); @@ -433,6 +457,28 @@ test_setting_ip4_config_labels (void) label = nm_ip_address_get_attribute (addr, "label"); g_assert (label == NULL); + g_object_unref (conn); + + NMTST_VARIANT_EDITOR (dict2, + NMTST_VARIANT_DROP_PROPERTY (NM_SETTING_IP4_CONFIG_SETTING_NAME, + "address-labels"); + ); + conn = nm_simple_connection_new_from_dbus (dict2, &error); + g_assert_no_error (error); + g_variant_unref (dict2); + + s_ip4 = nm_connection_get_setting_ip4_config (conn); + + addr = nm_setting_ip_config_get_address (s_ip4, 0); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "2.2.2.2"); + label = nm_ip_address_get_attribute (addr, "label"); + g_assert_cmpstr (g_variant_get_string (label, NULL), ==, "eth0:1"); + + addr = nm_setting_ip_config_get_address (s_ip4, 1); + g_assert_cmpstr (nm_ip_address_get_address (addr), ==, "3.3.3.3"); + label = nm_ip_address_get_attribute (addr, "label"); + g_assert (label == NULL); + /* Test explicit property assignment */ g_object_get (G_OBJECT (s_ip4), NM_SETTING_IP_CONFIG_ADDRESSES, &addrs, @@ -3391,10 +3437,14 @@ test_setting_ip4_gateway (void) g_variant_unref (ip4_dict); - /* When deserializing, the first gateway in ipv4.addresses is copied to ipv4.gateway */ + /* When deserializing an old-style connection, the gateway from the first address + * is copied to :gateway. + */ NMTST_VARIANT_EDITOR (conn_dict, NMTST_VARIANT_DROP_PROPERTY (NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_GATEWAY); + NMTST_VARIANT_DROP_PROPERTY (NM_SETTING_IP4_CONFIG_SETTING_NAME, + "address-data"); ); conn = nm_simple_connection_new_from_dbus (conn_dict, &error); @@ -3460,10 +3510,14 @@ test_setting_ip6_gateway (void) g_variant_unref (ip6_dict); - /* When deserializing, the first gateway in ipv4.addresses is copied to ipv4.gateway */ + /* When deserializing an old-style connection, the gateway from the first address + * is copied to :gateway. + */ NMTST_VARIANT_EDITOR (conn_dict, NMTST_VARIANT_DROP_PROPERTY (NM_SETTING_IP6_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_GATEWAY); + NMTST_VARIANT_DROP_PROPERTY (NM_SETTING_IP6_CONFIG_SETTING_NAME, + "address-data"); ); conn = nm_simple_connection_new_from_dbus (conn_dict, &error); diff --git a/libnm/nm-ip4-config.c b/libnm/nm-ip4-config.c index a37f835774..d242fe55db 100644 --- a/libnm/nm-ip4-config.c +++ b/libnm/nm-ip4-config.c @@ -40,6 +40,8 @@ typedef struct { char **domains; char **searches; char **wins; + + gboolean new_style_data; } NMIP4ConfigPrivate; enum { @@ -69,10 +71,13 @@ nm_ip4_config_init (NMIP4Config *config) } static gboolean -demarshal_ip4_address_array (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +demarshal_ip4_addresses (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) { NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); + if (priv->new_style_data) + return TRUE; + g_ptr_array_unref (priv->addresses); priv->addresses = nm_utils_ip4_addresses_from_variant (value, NULL); _nm_object_queue_notify (object, NM_IP4_CONFIG_ADDRESSES); @@ -80,6 +85,20 @@ demarshal_ip4_address_array (NMObject *object, GParamSpec *pspec, GVariant *valu return TRUE; } +static gboolean +demarshal_ip4_address_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +{ + NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); + + priv->new_style_data = TRUE; + + g_ptr_array_unref (priv->addresses); + priv->addresses = nm_utils_ip_addresses_from_variant (value, AF_INET); + _nm_object_queue_notify (object, NM_IP4_CONFIG_ADDRESSES); + + return TRUE; +} + static gboolean demarshal_ip4_array (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) { @@ -96,10 +115,13 @@ demarshal_ip4_array (NMObject *object, GParamSpec *pspec, GVariant *value, gpoin } static gboolean -demarshal_ip4_routes_array (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +demarshal_ip4_routes (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) { NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); + if (priv->new_style_data) + return TRUE; + g_ptr_array_unref (priv->routes); priv->routes = nm_utils_ip4_routes_from_variant (value); _nm_object_queue_notify (object, NM_IP4_CONFIG_ROUTES); @@ -107,14 +129,30 @@ demarshal_ip4_routes_array (NMObject *object, GParamSpec *pspec, GVariant *value return TRUE; } +static gboolean +demarshal_ip4_route_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +{ + NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); + + priv->new_style_data = TRUE; + + g_ptr_array_unref (priv->routes); + priv->routes = nm_utils_ip_routes_from_variant (value, AF_INET); + _nm_object_queue_notify (object, NM_IP4_CONFIG_ROUTES); + + return TRUE; +} + static void init_dbus (NMObject *object) { NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); const NMPropertiesInfo property_info[] = { { NM_IP4_CONFIG_GATEWAY, &priv->gateway, }, - { NM_IP4_CONFIG_ADDRESSES, &priv->addresses, demarshal_ip4_address_array }, - { NM_IP4_CONFIG_ROUTES, &priv->routes, demarshal_ip4_routes_array }, + { NM_IP4_CONFIG_ADDRESSES, &priv->addresses, demarshal_ip4_addresses }, + { "address-data", &priv->addresses, demarshal_ip4_address_data }, + { NM_IP4_CONFIG_ROUTES, &priv->routes, demarshal_ip4_routes }, + { "route-data", &priv->routes, demarshal_ip4_route_data }, { NM_IP4_CONFIG_NAMESERVERS, &priv->nameservers, demarshal_ip4_array }, { NM_IP4_CONFIG_DOMAINS, &priv->domains, }, { NM_IP4_CONFIG_SEARCHES, &priv->searches, }, diff --git a/libnm/nm-ip6-config.c b/libnm/nm-ip6-config.c index 82d377ee93..7318f5f6ea 100644 --- a/libnm/nm-ip6-config.c +++ b/libnm/nm-ip6-config.c @@ -39,6 +39,8 @@ typedef struct { char **nameservers; char **domains; char **searches; + + gboolean new_style_data; } NMIP6ConfigPrivate; enum { @@ -54,10 +56,13 @@ enum { }; static gboolean -demarshal_ip6_address_array (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +demarshal_ip6_addresses (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) { NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); + if (priv->new_style_data) + return TRUE; + g_ptr_array_unref (priv->addresses); priv->addresses = nm_utils_ip6_addresses_from_variant (value, NULL); _nm_object_queue_notify (object, NM_IP6_CONFIG_ADDRESSES); @@ -65,6 +70,20 @@ demarshal_ip6_address_array (NMObject *object, GParamSpec *pspec, GVariant *valu return TRUE; } +static gboolean +demarshal_ip6_address_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +{ + NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); + + priv->new_style_data = TRUE; + + g_ptr_array_unref (priv->addresses); + priv->addresses = nm_utils_ip_addresses_from_variant (value, AF_INET6); + _nm_object_queue_notify (object, NM_IP6_CONFIG_ADDRESSES); + + return TRUE; +} + static gboolean demarshal_ip6_nameserver_array (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) { @@ -81,10 +100,13 @@ demarshal_ip6_nameserver_array (NMObject *object, GParamSpec *pspec, GVariant *v } static gboolean -demarshal_ip6_routes_array (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +demarshal_ip6_routes (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) { NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); + if (priv->new_style_data) + return TRUE; + g_ptr_array_unref (priv->routes); priv->routes = nm_utils_ip6_routes_from_variant (value); _nm_object_queue_notify (object, NM_IP6_CONFIG_ROUTES); @@ -92,14 +114,30 @@ demarshal_ip6_routes_array (NMObject *object, GParamSpec *pspec, GVariant *value return TRUE; } +static gboolean +demarshal_ip6_route_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +{ + NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); + + priv->new_style_data = TRUE; + + g_ptr_array_unref (priv->routes); + priv->routes = nm_utils_ip_routes_from_variant (value, AF_INET6); + _nm_object_queue_notify (object, NM_IP6_CONFIG_ROUTES); + + return TRUE; +} + static void init_dbus (NMObject *object) { NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); const NMPropertiesInfo property_info[] = { { NM_IP6_CONFIG_GATEWAY, &priv->gateway, }, - { NM_IP6_CONFIG_ADDRESSES, &priv->addresses, demarshal_ip6_address_array }, - { NM_IP6_CONFIG_ROUTES, &priv->routes, demarshal_ip6_routes_array }, + { NM_IP6_CONFIG_ADDRESSES, &priv->addresses, demarshal_ip6_addresses }, + { "address-data", &priv->addresses, demarshal_ip6_address_data }, + { NM_IP6_CONFIG_ROUTES, &priv->routes, demarshal_ip6_routes }, + { "route-data", &priv->routes, demarshal_ip6_route_data }, { NM_IP6_CONFIG_NAMESERVERS, &priv->nameservers, demarshal_ip6_nameserver_array }, { NM_IP6_CONFIG_DOMAINS, &priv->domains, }, { NM_IP6_CONFIG_SEARCHES, &priv->searches, }, diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 12d3919e77..74a7556f38 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -59,9 +59,11 @@ G_STATIC_ASSERT (G_MAXUINT >= 0xFFFFFFFF); enum { PROP_0, - PROP_GATEWAY, + PROP_ADDRESS_DATA, PROP_ADDRESSES, + PROP_ROUTE_DATA, PROP_ROUTES, + PROP_GATEWAY, PROP_NAMESERVERS, PROP_DOMAINS, PROP_SEARCHES, @@ -235,6 +237,8 @@ nm_ip4_config_capture (int ifindex, gboolean capture_resolv_conf) } /* actually, nobody should be connected to the signal, just to be sure, notify */ + _NOTIFY (config, PROP_ADDRESS_DATA); + _NOTIFY (config, PROP_ROUTE_DATA); _NOTIFY (config, PROP_ADDRESSES); _NOTIFY (config, PROP_ROUTES); if (priv->gateway != old_gateway) @@ -1013,6 +1017,7 @@ nm_ip4_config_reset_addresses (NMIP4Config *config) if (priv->addresses->len != 0) { g_array_set_size (priv->addresses, 0); + _NOTIFY (config, PROP_ADDRESS_DATA); _NOTIFY (config, PROP_ADDRESSES); } } @@ -1070,6 +1075,7 @@ nm_ip4_config_add_address (NMIP4Config *config, const NMPlatformIP4Address *new) g_array_append_val (priv->addresses, *new); NOTIFY: + _NOTIFY (config, PROP_ADDRESS_DATA); _NOTIFY (config, PROP_ADDRESSES); } @@ -1081,6 +1087,7 @@ nm_ip4_config_del_address (NMIP4Config *config, guint i) g_return_if_fail (i < priv->addresses->len); g_array_remove_index (priv->addresses, i); + _NOTIFY (config, PROP_ADDRESS_DATA); _NOTIFY (config, PROP_ADDRESSES); } @@ -1125,6 +1132,7 @@ nm_ip4_config_reset_routes (NMIP4Config *config) if (priv->routes->len != 0) { g_array_set_size (priv->routes, 0); + _NOTIFY (config, PROP_ROUTE_DATA); _NOTIFY (config, PROP_ROUTES); } } @@ -1164,6 +1172,7 @@ nm_ip4_config_add_route (NMIP4Config *config, const NMPlatformIP4Route *new) g_array_append_val (priv->routes, *new); NOTIFY: + _NOTIFY (config, PROP_ROUTE_DATA); _NOTIFY (config, PROP_ROUTES); } @@ -1175,6 +1184,7 @@ nm_ip4_config_del_route (NMIP4Config *config, guint i) g_return_if_fail (i < priv->routes->len); g_array_remove_index (priv->routes, i); + _NOTIFY (config, PROP_ROUTE_DATA); _NOTIFY (config, PROP_ROUTES); } @@ -1705,11 +1715,41 @@ get_property (GObject *object, guint prop_id, NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); switch (prop_id) { - case PROP_GATEWAY: - if (priv->gateway) - g_value_set_string (value, nm_utils_inet4_ntop (priv->gateway, NULL)); - else - g_value_set_string (value, NULL); + case PROP_ADDRESS_DATA: + { + GPtrArray *addresses = g_ptr_array_new (); + int naddr = nm_ip4_config_get_num_addresses (config); + int i; + + for (i = 0; i < naddr; i++) { + const NMPlatformIP4Address *address = nm_ip4_config_get_address (config, i); + GValueArray *array = g_value_array_new (3); + GHashTable *attrs; + GValue val = { 0, }; + + g_value_init (&val, G_TYPE_STRING); + g_value_set_string (&val, nm_utils_inet4_ntop (address->address, NULL)); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_value_init (&val, G_TYPE_UINT); + g_value_set_uint (&val, address->plen); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_value_init (&val, DBUS_TYPE_G_MAP_OF_STRING); + attrs = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free); + if (*address->label) + g_hash_table_insert (attrs, "label", g_strdup (address->label)); + g_value_take_boxed (&val, attrs); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_ptr_array_add (addresses, array); + } + + g_value_take_boxed (value, addresses); + } break; case PROP_ADDRESSES: { @@ -1732,6 +1772,53 @@ get_property (GObject *object, guint prop_id, g_value_take_boxed (value, addresses); } break; + case PROP_ROUTE_DATA: + { + GPtrArray *routes = g_ptr_array_new (); + guint nroutes = nm_ip4_config_get_num_routes (config); + int i; + + for (i = 0; i < nroutes; i++) { + const NMPlatformIP4Route *route = nm_ip4_config_get_route (config, i); + GValueArray *array = g_value_array_new (5); + GHashTable *attrs; + GValue val = { 0, }; + + g_value_init (&val, G_TYPE_STRING); + g_value_set_string (&val, nm_utils_inet4_ntop (route->network, NULL)); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_value_init (&val, G_TYPE_UINT); + g_value_set_uint (&val, route->plen); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_value_init (&val, G_TYPE_STRING); + if (route->gateway) + g_value_set_string (&val, nm_utils_inet4_ntop (route->gateway, NULL)); + else + g_value_set_string (&val, ""); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_value_init (&val, G_TYPE_UINT); + g_value_set_uint (&val, route->metric); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_value_init (&val, DBUS_TYPE_G_MAP_OF_STRING); + attrs = g_hash_table_new (g_str_hash, g_str_equal); + g_value_take_boxed (&val, attrs); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_ptr_array_add (routes, array); + } + + g_value_take_boxed (value, routes); + } + break; case PROP_ROUTES: { GPtrArray *routes = g_ptr_array_new (); @@ -1753,6 +1840,12 @@ get_property (GObject *object, guint prop_id, g_value_take_boxed (value, routes); } break; + case PROP_GATEWAY: + if (priv->gateway) + g_value_set_string (value, nm_utils_inet4_ntop (priv->gateway, NULL)); + else + g_value_set_string (value, NULL); + break; case PROP_NAMESERVERS: g_value_set_boxed (value, priv->nameservers); break; @@ -1781,19 +1874,29 @@ nm_ip4_config_class_init (NMIP4ConfigClass *config_class) object_class->get_property = get_property; object_class->finalize = finalize; - obj_properties[PROP_GATEWAY] = - g_param_spec_string (NM_IP4_CONFIG_GATEWAY, "", "", - NULL, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS); + obj_properties[PROP_ADDRESS_DATA] = + g_param_spec_boxed (NM_IP4_CONFIG_ADDRESS_DATA, "", "", + DBUS_TYPE_NM_IP_ADDRESSES, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS); obj_properties[PROP_ADDRESSES] = - g_param_spec_boxed (NM_IP4_CONFIG_ADDRESSES, "", "", - DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT, + g_param_spec_boxed (NM_IP4_CONFIG_ADDRESSES, "", "", + DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS); + obj_properties[PROP_ROUTE_DATA] = + g_param_spec_boxed (NM_IP4_CONFIG_ROUTE_DATA, "", "", + DBUS_TYPE_NM_IP_ROUTES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); obj_properties[PROP_ROUTES] = - g_param_spec_boxed (NM_IP4_CONFIG_ROUTES, "", "", - DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT, + g_param_spec_boxed (NM_IP4_CONFIG_ROUTES, "", "", + DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS); + obj_properties[PROP_GATEWAY] = + g_param_spec_string (NM_IP4_CONFIG_GATEWAY, "", "", + NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); obj_properties[PROP_NAMESERVERS] = diff --git a/src/nm-ip4-config.h b/src/nm-ip4-config.h index e9f2642af7..555f1678a7 100644 --- a/src/nm-ip4-config.h +++ b/src/nm-ip4-config.h @@ -41,14 +41,18 @@ typedef struct { GObjectClass parent; } NMIP4ConfigClass; +#define NM_IP4_CONFIG_ADDRESS_DATA "address-data" +#define NM_IP4_CONFIG_ROUTE_DATA "route-data" #define NM_IP4_CONFIG_GATEWAY "gateway" -#define NM_IP4_CONFIG_ADDRESSES "addresses" -#define NM_IP4_CONFIG_ROUTES "routes" #define NM_IP4_CONFIG_NAMESERVERS "nameservers" #define NM_IP4_CONFIG_DOMAINS "domains" #define NM_IP4_CONFIG_SEARCHES "searches" #define NM_IP4_CONFIG_WINS_SERVERS "wins-servers" +/* deprecated */ +#define NM_IP4_CONFIG_ADDRESSES "addresses" +#define NM_IP4_CONFIG_ROUTES "routes" + GType nm_ip4_config_get_type (void); diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index e007b20104..19552e0339 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -51,9 +51,11 @@ typedef struct { enum { PROP_0, - PROP_GATEWAY, + PROP_ADDRESS_DATA, PROP_ADDRESSES, + PROP_ROUTE_DATA, PROP_ROUTES, + PROP_GATEWAY, PROP_NAMESERVERS, PROP_DOMAINS, PROP_SEARCHES, @@ -271,6 +273,7 @@ nm_ip6_config_addresses_sort (NMIP6Config *self, NMSettingIP6ConfigPrivacy use_t g_free (data_pre); if (changed) { + _NOTIFY (self, PROP_ADDRESS_DATA); _NOTIFY (self, PROP_ADDRESSES); return TRUE; } @@ -346,7 +349,9 @@ nm_ip6_config_capture (int ifindex, gboolean capture_resolv_conf, NMSettingIP6Co /* actually, nobody should be connected to the signal, just to be sure, notify */ if (notify_nameservers) _NOTIFY (config, PROP_NAMESERVERS); + _NOTIFY (config, PROP_ADDRESS_DATA); _NOTIFY (config, PROP_ADDRESSES); + _NOTIFY (config, PROP_ROUTE_DATA); _NOTIFY (config, PROP_ROUTES); if (!IN6_ARE_ADDR_EQUAL (&priv->gateway, &old_gateway)) _NOTIFY (config, PROP_GATEWAY); @@ -1016,6 +1021,7 @@ nm_ip6_config_reset_addresses (NMIP6Config *config) if (priv->addresses->len != 0) { g_array_set_size (priv->addresses, 0); + _NOTIFY (config, PROP_ADDRESS_DATA); _NOTIFY (config, PROP_ADDRESSES); } } @@ -1073,6 +1079,7 @@ nm_ip6_config_add_address (NMIP6Config *config, const NMPlatformIP6Address *new) g_array_append_val (priv->addresses, *new); NOTIFY: + _NOTIFY (config, PROP_ADDRESS_DATA); _NOTIFY (config, PROP_ADDRESSES); } @@ -1084,6 +1091,7 @@ nm_ip6_config_del_address (NMIP6Config *config, guint i) g_return_if_fail (i < priv->addresses->len); g_array_remove_index (priv->addresses, i); + _NOTIFY (config, PROP_ADDRESS_DATA); _NOTIFY (config, PROP_ADDRESSES); } @@ -1129,6 +1137,7 @@ nm_ip6_config_reset_routes (NMIP6Config *config) if (priv->routes->len != 0) { g_array_set_size (priv->routes, 0); + _NOTIFY (config, PROP_ROUTE_DATA); _NOTIFY (config, PROP_ROUTES); } } @@ -1168,6 +1177,7 @@ nm_ip6_config_add_route (NMIP6Config *config, const NMPlatformIP6Route *new) g_array_append_val (priv->routes, *new); NOTIFY: + _NOTIFY (config, PROP_ROUTE_DATA); _NOTIFY (config, PROP_ROUTES); } @@ -1179,6 +1189,7 @@ nm_ip6_config_del_route (NMIP6Config *config, guint i) g_return_if_fail (i < priv->routes->len); g_array_remove_index (priv->routes, i); + _NOTIFY (config, PROP_ROUTE_DATA); _NOTIFY (config, PROP_ROUTES); } @@ -1578,11 +1589,39 @@ get_property (GObject *object, guint prop_id, NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); switch (prop_id) { - case PROP_GATEWAY: - if (!IN6_IS_ADDR_UNSPECIFIED (&priv->gateway)) - g_value_set_string (value, nm_utils_inet6_ntop (&priv->gateway, NULL)); - else - g_value_set_string (value, NULL); + case PROP_ADDRESS_DATA: + { + GPtrArray *addresses = g_ptr_array_new (); + int naddr = nm_ip6_config_get_num_addresses (config); + int i; + + for (i = 0; i < naddr; i++) { + const NMPlatformIP6Address *address = nm_ip6_config_get_address (config, i); + GValueArray *array = g_value_array_new (3); + GHashTable *attrs; + GValue val = { 0, }; + + g_value_init (&val, G_TYPE_STRING); + g_value_set_string (&val, nm_utils_inet6_ntop (&address->address, NULL)); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_value_init (&val, G_TYPE_UINT); + g_value_set_uint (&val, address->plen); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_value_init (&val, DBUS_TYPE_G_MAP_OF_STRING); + attrs = g_hash_table_new (g_str_hash, g_str_equal); + g_value_take_boxed (&val, attrs); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_ptr_array_add (addresses, array); + } + + g_value_take_boxed (value, addresses); + } break; case PROP_ADDRESSES: { @@ -1626,6 +1665,53 @@ get_property (GObject *object, guint prop_id, g_value_take_boxed (value, addresses); } break; + case PROP_ROUTE_DATA: + { + GPtrArray *routes = g_ptr_array_new (); + guint nroutes = nm_ip6_config_get_num_routes (config); + int i; + + for (i = 0; i < nroutes; i++) { + const NMPlatformIP6Route *route = nm_ip6_config_get_route (config, i); + GValueArray *array = g_value_array_new (5); + GHashTable *attrs; + GValue val = { 0, }; + + g_value_init (&val, G_TYPE_STRING); + g_value_set_string (&val, nm_utils_inet6_ntop (&route->network, NULL)); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_value_init (&val, G_TYPE_UINT); + g_value_set_uint (&val, route->plen); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_value_init (&val, G_TYPE_STRING); + if (memcmp (&route->gateway, &in6addr_any, sizeof (struct in6_addr)) != 0) + g_value_set_string (&val, nm_utils_inet6_ntop (&route->gateway, NULL)); + else + g_value_set_string (&val, ""); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_value_init (&val, G_TYPE_UINT); + g_value_set_uint (&val, route->metric); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_value_init (&val, DBUS_TYPE_G_MAP_OF_STRING); + attrs = g_hash_table_new (g_str_hash, g_str_equal); + g_value_take_boxed (&val, attrs); + g_value_array_append (array, &val); + g_value_unset (&val); + + g_ptr_array_add (routes, array); + } + + g_value_take_boxed (value, routes); + } + break; case PROP_ROUTES: { GPtrArray *routes = g_ptr_array_new (); @@ -1669,6 +1755,12 @@ get_property (GObject *object, guint prop_id, g_value_take_boxed (value, routes); } break; + case PROP_GATEWAY: + if (!IN6_IS_ADDR_UNSPECIFIED (&priv->gateway)) + g_value_set_string (value, nm_utils_inet6_ntop (&priv->gateway, NULL)); + else + g_value_set_string (value, NULL); + break; case PROP_NAMESERVERS: nameservers_to_gvalue (priv->nameservers, value); break; @@ -1696,21 +1788,31 @@ nm_ip6_config_class_init (NMIP6ConfigClass *config_class) object_class->finalize = finalize; /* properties */ - obj_properties[PROP_GATEWAY] = - g_param_spec_string (NM_IP6_CONFIG_GATEWAY, "", "", - NULL, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS); - obj_properties[PROP_ADDRESSES] = - g_param_spec_boxed (NM_IP6_CONFIG_ADDRESSES, "", "", - DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS, + obj_properties[PROP_ADDRESS_DATA] = + g_param_spec_boxed (NM_IP6_CONFIG_ADDRESS_DATA, "", "", + DBUS_TYPE_NM_IP_ADDRESSES, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_ADDRESSES] = + g_param_spec_boxed (NM_IP6_CONFIG_ADDRESSES, "", "", + DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS); + obj_properties[PROP_ROUTE_DATA] = + g_param_spec_boxed (NM_IP6_CONFIG_ROUTE_DATA, "", "", + DBUS_TYPE_NM_IP_ROUTES, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS); obj_properties[PROP_ROUTES] = g_param_spec_boxed (NM_IP6_CONFIG_ROUTES, "", "", DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + obj_properties[PROP_GATEWAY] = + g_param_spec_string (NM_IP6_CONFIG_GATEWAY, "", "", + NULL, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS); obj_properties[PROP_NAMESERVERS] = g_param_spec_boxed (NM_IP6_CONFIG_NAMESERVERS, "", "", DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UCHAR, diff --git a/src/nm-ip6-config.h b/src/nm-ip6-config.h index 19eef01372..0174158832 100644 --- a/src/nm-ip6-config.h +++ b/src/nm-ip6-config.h @@ -42,13 +42,17 @@ typedef struct { GObjectClass parent; } NMIP6ConfigClass; +#define NM_IP6_CONFIG_ADDRESS_DATA "address-data" +#define NM_IP6_CONFIG_ROUTE_DATA "route-data" #define NM_IP6_CONFIG_GATEWAY "gateway" -#define NM_IP6_CONFIG_ADDRESSES "addresses" -#define NM_IP6_CONFIG_ROUTES "routes" #define NM_IP6_CONFIG_NAMESERVERS "nameservers" #define NM_IP6_CONFIG_DOMAINS "domains" #define NM_IP6_CONFIG_SEARCHES "searches" +/* deprecated */ +#define NM_IP6_CONFIG_ADDRESSES "addresses" +#define NM_IP6_CONFIG_ROUTES "routes" + GType nm_ip6_config_get_type (void); -- cgit v1.2.1 From f8dcb9510c11055a40fd9e42448097c2751e1f2a Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 21 Oct 2014 11:41:44 -0400 Subject: libnm-core: don't warn about unrecognized properties b64c82a3 removed the warning in libnm-util when nm_setting_new_from_hash() sees an unrecognized property, but we were still warning in the equivalent libnm-core code. But it doesn't make sense to warn here either: we do add new properties sometimes, but we always make sure that older clients still get the information they need as well, so they can just ignore the new property. --- libnm-core/nm-setting.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/libnm-core/nm-setting.c b/libnm-core/nm-setting.c index 680db50938..f8fb3e95ef 100644 --- a/libnm-core/nm-setting.c +++ b/libnm-core/nm-setting.c @@ -716,8 +716,6 @@ _nm_setting_new_from_dbus (GType setting_type, { NMSettingClass *class; NMSetting *setting; - GVariantIter iter; - const char *prop_name; const NMSettingProperty *properties; guint n_properties; guint i; @@ -736,17 +734,11 @@ _nm_setting_new_from_dbus (GType setting_type, */ class = g_type_class_ref (setting_type); - /* Check for invalid properties first. */ - g_variant_iter_init (&iter, setting_dict); - while (g_variant_iter_next (&iter, "{&sv}", &prop_name, NULL)) { - if (!nm_setting_class_find_property (class, prop_name)) { - /* Oh, we're so nice and only warn, maybe it should be a fatal error? */ - g_warning ("Ignoring invalid property '%s'", prop_name); - continue; - } - } - - /* Now build the setting object from the legitimate properties */ + /* Build the setting object from the properties we know about; we assume + * that any propreties in @setting_dict that we don't know about can + * either be ignored or else has a backward-compatibility equivalent + * that we do know about. + */ setting = (NMSetting *) g_object_new (setting_type, NULL); properties = nm_setting_class_get_properties (class, &n_properties); -- cgit v1.2.1 From d34910b12860eb080b1a519f441946cbc378f769 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Wed, 22 Oct 2014 13:48:18 -0400 Subject: libnm: create NMIPConfig as parent of NMIP4Config and NMIP6Config Create NMIPConfig as the parent of NMIP4Config and NMIP6Config, and remove the two subclasses from the public API; while it's convenient to still have both internally, they are now identical to the outside world. --- clients/cli/common.c | 26 +-- clients/cli/common.h | 4 +- clients/cli/connections.c | 4 +- clients/cli/devices.c | 3 +- docs/libnm/Makefile.am | 2 + docs/libnm/libnm-docs.xml | 3 +- libnm/Makefile.am | 6 +- libnm/NetworkManager.h | 3 +- libnm/libnm.ver | 24 +-- libnm/nm-active-connection.c | 30 ++- libnm/nm-active-connection.h | 4 +- libnm/nm-device.c | 33 ++- libnm/nm-device.h | 4 +- libnm/nm-ip-config.c | 498 +++++++++++++++++++++++++++++++++++++++++++ libnm/nm-ip-config.h | 73 +++++++ libnm/nm-ip4-config.c | 416 +----------------------------------- libnm/nm-ip4-config.h | 30 +-- libnm/nm-ip6-config.c | 380 +-------------------------------- libnm/nm-ip6-config.h | 28 +-- libnm/nm-types.h | 3 +- 20 files changed, 655 insertions(+), 919 deletions(-) create mode 100644 libnm/nm-ip-config.c create mode 100644 libnm/nm-ip-config.h diff --git a/clients/cli/common.c b/clients/cli/common.c index 2c56976a26..5493b166d7 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -76,7 +76,7 @@ NmcOutputField nmc_fields_dhcp6_config[] = { gboolean -print_ip4_config (NMIP4Config *cfg4, +print_ip4_config (NMIPConfig *cfg4, NmCli *nmc, const char *group_prefix, const char *one_field) @@ -103,8 +103,8 @@ print_ip4_config (NMIP4Config *cfg4, g_ptr_array_add (nmc->output_data, arr); /* addresses */ - ptr_array = nm_ip4_config_get_addresses (cfg4); - gw = nm_ip4_config_get_gateway (cfg4); + ptr_array = nm_ip_config_get_addresses (cfg4); + gw = nm_ip_config_get_gateway (cfg4); if (ptr_array) { addr_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { @@ -119,7 +119,7 @@ print_ip4_config (NMIP4Config *cfg4, } /* routes */ - ptr_array = nm_ip4_config_get_routes (cfg4); + ptr_array = nm_ip_config_get_routes (cfg4); if (ptr_array) { route_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { @@ -140,13 +140,13 @@ print_ip4_config (NMIP4Config *cfg4, } /* DNS */ - dns_arr = g_strdupv ((char **) nm_ip4_config_get_nameservers (cfg4)); + dns_arr = g_strdupv ((char **) nm_ip_config_get_nameservers (cfg4)); /* domains */ - domain_arr = g_strdupv ((char **) nm_ip4_config_get_domains (cfg4)); + domain_arr = g_strdupv ((char **) nm_ip_config_get_domains (cfg4)); /* WINS */ - wins_arr = g_strdupv ((char **) nm_ip4_config_get_wins_servers (cfg4)); + wins_arr = g_strdupv ((char **) nm_ip_config_get_wins_servers (cfg4)); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, group_prefix); @@ -166,7 +166,7 @@ print_ip4_config (NMIP4Config *cfg4, } gboolean -print_ip6_config (NMIP6Config *cfg6, +print_ip6_config (NMIPConfig *cfg6, NmCli *nmc, const char *group_prefix, const char *one_field) @@ -192,8 +192,8 @@ print_ip6_config (NMIP6Config *cfg6, g_ptr_array_add (nmc->output_data, arr); /* addresses */ - ptr_array = nm_ip6_config_get_addresses (cfg6); - gw = nm_ip6_config_get_gateway (cfg6); + ptr_array = nm_ip_config_get_addresses (cfg6); + gw = nm_ip_config_get_gateway (cfg6); if (ptr_array) { addr_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { @@ -208,7 +208,7 @@ print_ip6_config (NMIP6Config *cfg6, } /* routes */ - ptr_array = nm_ip6_config_get_routes (cfg6); + ptr_array = nm_ip_config_get_routes (cfg6); if (ptr_array) { route_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { @@ -229,10 +229,10 @@ print_ip6_config (NMIP6Config *cfg6, } /* DNS */ - dns_arr = g_strdupv ((char **) nm_ip6_config_get_nameservers (cfg6)); + dns_arr = g_strdupv ((char **) nm_ip_config_get_nameservers (cfg6)); /* domains */ - domain_arr = g_strdupv ((char **) nm_ip6_config_get_domains (cfg6)); + domain_arr = g_strdupv ((char **) nm_ip_config_get_domains (cfg6)); arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, group_prefix); diff --git a/clients/cli/common.h b/clients/cli/common.h index bf881bf2f4..2787b1935b 100644 --- a/clients/cli/common.h +++ b/clients/cli/common.h @@ -24,8 +24,8 @@ #include "nmcli.h" -gboolean print_ip4_config (NMIP4Config *cfg4, NmCli *nmc, const char *group_prefix, const char *one_field); -gboolean print_ip6_config (NMIP6Config *cfg6, NmCli *nmc, const char *group_prefix, const char *one_field); +gboolean print_ip4_config (NMIPConfig *cfg4, NmCli *nmc, const char *group_prefix, const char *one_field); +gboolean print_ip6_config (NMIPConfig *cfg6, NmCli *nmc, const char *group_prefix, const char *one_field); gboolean print_dhcp4_config (NMDhcp4Config *dhcp4, NmCli *nmc, const char *group_prefix, const char *one_field); gboolean print_dhcp6_config (NMDhcp6Config *dhcp6, NmCli *nmc, const char *group_prefix, const char *one_field); diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 969adb80d0..35e88b4378 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -1128,7 +1128,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) /* IP4 */ if (strcasecmp (nmc_fields_con_active_details_groups[group_idx].name, nmc_fields_con_active_details_groups[1].name) == 0) { gboolean b1 = FALSE; - NMIP4Config *cfg4 = nm_active_connection_get_ip4_config (acon); + NMIPConfig *cfg4 = nm_active_connection_get_ip4_config (acon); b1 = print_ip4_config (cfg4, nmc, "IP4", group_fld); was_output = was_output || b1; @@ -1146,7 +1146,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) /* IP6 */ if (strcasecmp (nmc_fields_con_active_details_groups[group_idx].name, nmc_fields_con_active_details_groups[3].name) == 0) { gboolean b1 = FALSE; - NMIP6Config *cfg6 = nm_active_connection_get_ip6_config (acon); + NMIPConfig *cfg6 = nm_active_connection_get_ip6_config (acon); b1 = print_ip6_config (cfg6, nmc, "IP6", group_fld); was_output = was_output || b1; diff --git a/clients/cli/devices.c b/clients/cli/devices.c index c073ec252b..371c05bf13 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -767,8 +767,7 @@ show_device_info (NMDevice *device, NmCli *nmc) NmcOutputField *tmpl, *arr; size_t tmpl_len; gboolean was_output = FALSE; - NMIP4Config *cfg4; - NMIP6Config *cfg6; + NMIPConfig *cfg4, *cfg6; NMDhcp4Config *dhcp4; NMDhcp6Config *dhcp6; const char *base_hdr = _("Device details"); diff --git a/docs/libnm/Makefile.am b/docs/libnm/Makefile.am index 1e1e9ae276..0c82e751a9 100644 --- a/docs/libnm/Makefile.am +++ b/docs/libnm/Makefile.am @@ -34,6 +34,8 @@ IGNORE_HFILES= \ nm-dbus-helpers-private.h \ nm-core-internal.h \ nm-device-private.h \ + nm-ip4-config.h \ + nm-ip6-config.h \ nm-manager.h \ nm-object-cache.h \ nm-object-private.h \ diff --git a/docs/libnm/libnm-docs.xml b/docs/libnm/libnm-docs.xml index 130c690429..c60b4727e6 100644 --- a/docs/libnm/libnm-docs.xml +++ b/docs/libnm/libnm-docs.xml @@ -128,8 +128,7 @@ - - + diff --git a/libnm/Makefile.am b/libnm/Makefile.am index e14dc83a3f..3656cddfe0 100644 --- a/libnm/Makefile.am +++ b/libnm/Makefile.am @@ -46,8 +46,7 @@ libnminclude_hfiles = \ nm-dhcp4-config.h \ nm-dhcp6-config.h \ nm-enum-types.h \ - nm-ip4-config.h \ - nm-ip6-config.h \ + nm-ip-config.h \ nm-object.h \ nm-remote-connection.h \ nm-secret-agent.h \ @@ -66,6 +65,8 @@ libnminclude_HEADERS = \ libnm_la_private_headers = \ nm-dbus-helpers.h \ nm-device-private.h \ + nm-ip4-config.h \ + nm-ip6-config.h \ nm-manager.h \ nm-object-cache.h \ nm-object-private.h \ @@ -94,6 +95,7 @@ libnm_la_csources = \ nm-dhcp4-config.c \ nm-dhcp6-config.c \ nm-enum-types.c \ + nm-ip-config.c \ nm-ip4-config.c \ nm-ip6-config.c \ nm-manager.c \ diff --git a/libnm/NetworkManager.h b/libnm/NetworkManager.h index d112e33480..7604e77df5 100644 --- a/libnm/NetworkManager.h +++ b/libnm/NetworkManager.h @@ -46,8 +46,7 @@ #include #include #include -#include -#include +#include #include #include #include diff --git a/libnm/libnm.ver b/libnm/libnm.ver index 952b1dc263..50ee710961 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -268,21 +268,6 @@ global: nm_dhcp6_config_get_one_option; nm_dhcp6_config_get_options; nm_dhcp6_config_get_type; - nm_ip4_config_get_addresses; - nm_ip4_config_get_domains; - nm_ip4_config_get_gateway; - nm_ip4_config_get_nameservers; - nm_ip4_config_get_routes; - nm_ip4_config_get_searches; - nm_ip4_config_get_type; - nm_ip4_config_get_wins_servers; - nm_ip6_config_get_addresses; - nm_ip6_config_get_domains; - nm_ip6_config_get_gateway; - nm_ip6_config_get_nameservers; - nm_ip6_config_get_routes; - nm_ip6_config_get_searches; - nm_ip6_config_get_type; nm_ip_address_equal; nm_ip_address_get_address; nm_ip_address_get_address_binary; @@ -299,6 +284,15 @@ global: nm_ip_address_set_attribute; nm_ip_address_set_prefix; nm_ip_address_unref; + nm_ip_config_get_addresses; + nm_ip_config_get_domains; + nm_ip_config_get_family; + nm_ip_config_get_gateway; + nm_ip_config_get_nameservers; + nm_ip_config_get_routes; + nm_ip_config_get_searches; + nm_ip_config_get_type; + nm_ip_config_get_wins_servers; nm_ip_route_equal; nm_ip_route_get_attribute; nm_ip_route_get_attribute_names; diff --git a/libnm/nm-active-connection.c b/libnm/nm-active-connection.c index 742b270cc6..3551afeb94 100644 --- a/libnm/nm-active-connection.c +++ b/libnm/nm-active-connection.c @@ -57,10 +57,10 @@ typedef struct { GPtrArray *devices; NMActiveConnectionState state; gboolean is_default; - NMIP4Config *ip4_config; + NMIPConfig *ip4_config; NMDhcp4Config *dhcp4_config; gboolean is_default6; - NMIP6Config *ip6_config; + NMIPConfig *ip6_config; NMDhcp6Config *dhcp6_config; gboolean is_vpn; NMDevice *master; @@ -242,13 +242,12 @@ nm_active_connection_get_default (NMActiveConnection *connection) * nm_active_connection_get_ip4_config: * @connection: an #NMActiveConnection * - * Gets the current #NMIP4Config associated with the #NMActiveConnection. + * Gets the current IPv4 #NMIPConfig associated with the #NMActiveConnection. * - * Returns: (transfer none): the #NMIP4Config, or %NULL if the - * connection is not in the %NM_ACTIVE_CONNECTION_STATE_ACTIVATED - * state. + * Returns: (transfer none): the IPv4 #NMIPConfig, or %NULL if the connection is + * not in the %NM_ACTIVE_CONNECTION_STATE_ACTIVATED state. **/ -NMIP4Config * +NMIPConfig * nm_active_connection_get_ip4_config (NMActiveConnection *connection) { g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL); @@ -296,13 +295,12 @@ nm_active_connection_get_default6 (NMActiveConnection *connection) * nm_active_connection_get_ip6_config: * @connection: an #NMActiveConnection * - * Gets the current #NMIP6Config associated with the #NMActiveConnection. + * Gets the current IPv6 #NMIPConfig associated with the #NMActiveConnection. * - * Returns: (transfer none): the #NMIP6Config, or %NULL if the - * connection is not in the %NM_ACTIVE_CONNECTION_STATE_ACTIVATED - * state. + * Returns: (transfer none): the IPv6 #NMIPConfig, or %NULL if the connection is + * not in the %NM_ACTIVE_CONNECTION_STATE_ACTIVATED state. **/ -NMIP6Config * +NMIPConfig * nm_active_connection_get_ip6_config (NMActiveConnection *connection) { g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL); @@ -631,12 +629,12 @@ nm_active_connection_class_init (NMActiveConnectionClass *ap_class) /** * NMActiveConnection:ip4-config: * - * The #NMIP4Config of the connection. + * The IPv4 #NMIPConfig of the connection. **/ g_object_class_install_property (object_class, PROP_IP4_CONFIG, g_param_spec_object (NM_ACTIVE_CONNECTION_IP4_CONFIG, "", "", - NM_TYPE_IP4_CONFIG, + NM_TYPE_IP_CONFIG, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); @@ -667,12 +665,12 @@ nm_active_connection_class_init (NMActiveConnectionClass *ap_class) /** * NMActiveConnection:ip6-config: * - * The #NMIP6Config of the connection. + * The IPv6 #NMIPConfig of the connection. **/ g_object_class_install_property (object_class, PROP_IP6_CONFIG, g_param_spec_object (NM_ACTIVE_CONNECTION_IP6_CONFIG, "", "", - NM_TYPE_IP6_CONFIG, + NM_TYPE_IP_CONFIG, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); diff --git a/libnm/nm-active-connection.h b/libnm/nm-active-connection.h index ef8dd1980b..b6b31d9c0a 100644 --- a/libnm/nm-active-connection.h +++ b/libnm/nm-active-connection.h @@ -75,10 +75,10 @@ const GPtrArray *nm_active_connection_get_devices (NMActive NMActiveConnectionState nm_active_connection_get_state (NMActiveConnection *connection); NMDevice *nm_active_connection_get_master (NMActiveConnection *connection); gboolean nm_active_connection_get_default (NMActiveConnection *connection); -NMIP4Config *nm_active_connection_get_ip4_config (NMActiveConnection *connection); +NMIPConfig *nm_active_connection_get_ip4_config (NMActiveConnection *connection); NMDhcp4Config *nm_active_connection_get_dhcp4_config (NMActiveConnection *connection); gboolean nm_active_connection_get_default6 (NMActiveConnection *connection); -NMIP6Config *nm_active_connection_get_ip6_config (NMActiveConnection *connection); +NMIPConfig *nm_active_connection_get_ip6_config (NMActiveConnection *connection); NMDhcp6Config *nm_active_connection_get_dhcp6_config (NMActiveConnection *connection); gboolean nm_active_connection_get_vpn (NMActiveConnection *connection); diff --git a/libnm/nm-device.c b/libnm/nm-device.c index 5a1caf6d9f..16f88d9308 100644 --- a/libnm/nm-device.c +++ b/libnm/nm-device.c @@ -83,9 +83,9 @@ typedef struct { gboolean managed; gboolean firmware_missing; gboolean autoconnect; - NMIP4Config *ip4_config; + NMIPConfig *ip4_config; NMDhcp4Config *dhcp4_config; - NMIP6Config *ip6_config; + NMIPConfig *ip6_config; NMDhcp6Config *dhcp6_config; NMDeviceState state; NMDeviceState last_seen_state; @@ -651,7 +651,7 @@ nm_device_class_init (NMDeviceClass *device_class) g_object_class_install_property (object_class, PROP_IP4_CONFIG, g_param_spec_object (NM_DEVICE_IP4_CONFIG, "", "", - NM_TYPE_IP4_CONFIG, + NM_TYPE_IP_CONFIG, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); @@ -670,12 +670,12 @@ nm_device_class_init (NMDeviceClass *device_class) /** * NMDevice:ip6-config: * - * The #NMIP6Config of the device. + * The IPv6 #NMIPConfig of the device. **/ g_object_class_install_property (object_class, PROP_IP6_CONFIG, g_param_spec_object (NM_DEVICE_IP6_CONFIG, "", "", - NM_TYPE_IP6_CONFIG, + NM_TYPE_IP_CONFIG, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); @@ -1106,15 +1106,15 @@ nm_device_get_firmware_missing (NMDevice *device) * nm_device_get_ip4_config: * @device: a #NMDevice * - * Gets the current #NMIP4Config associated with the #NMDevice. + * Gets the current IPv4 #NMIPConfig associated with the #NMDevice. * - * Note that as of NetworkManager 0.9.10, you can alternatively use - * nm_active_connection_get_ip4_config(), which also works with VPN - * connections. + * You can alternatively use nm_active_connection_get_ip4_config(), which also + * works with VPN connections. * - * Returns: (transfer none): the #NMIP4Config or %NULL if the device is not activated. + * Returns: (transfer none): the IPv4 #NMIPConfig, or %NULL if the device is not + * activated. **/ -NMIP4Config * +NMIPConfig * nm_device_get_ip4_config (NMDevice *device) { g_return_val_if_fail (NM_IS_DEVICE (device), NULL); @@ -1147,15 +1147,14 @@ nm_device_get_dhcp4_config (NMDevice *device) * nm_device_get_ip6_config: * @device: a #NMDevice * - * Gets the current #NMIP6Config associated with the #NMDevice. + * Gets the current IPv6 #NMIPConfig associated with the #NMDevice. * - * Note that as of NetworkManager 0.9.10, you can alternatively use - * nm_active_connection_get_ip6_config(), which also works with VPN - * connections. + * You can alternatively use nm_active_connection_get_ip6_config(), which also + * works with VPN connections. * - * Returns: (transfer none): the #NMIP6Config or %NULL if the device is not activated. + * Returns: (transfer none): the IPv6 #NMIPConfig or %NULL if the device is not activated. **/ -NMIP6Config * +NMIPConfig * nm_device_get_ip6_config (NMDevice *device) { g_return_val_if_fail (NM_IS_DEVICE (device), NULL); diff --git a/libnm/nm-device.h b/libnm/nm-device.h index 412dff3f96..2b103e5486 100644 --- a/libnm/nm-device.h +++ b/libnm/nm-device.h @@ -104,9 +104,9 @@ gboolean nm_device_get_managed (NMDevice *device); gboolean nm_device_get_autoconnect (NMDevice *device); void nm_device_set_autoconnect (NMDevice *device, gboolean autoconnect); gboolean nm_device_get_firmware_missing (NMDevice *device); -NMIP4Config * nm_device_get_ip4_config (NMDevice *device); +NMIPConfig * nm_device_get_ip4_config (NMDevice *device); NMDhcp4Config * nm_device_get_dhcp4_config (NMDevice *device); -NMIP6Config * nm_device_get_ip6_config (NMDevice *device); +NMIPConfig * nm_device_get_ip6_config (NMDevice *device); NMDhcp6Config * nm_device_get_dhcp6_config (NMDevice *device); NMDeviceState nm_device_get_state (NMDevice *device); NMDeviceStateReason nm_device_get_state_reason (NMDevice *device); diff --git a/libnm/nm-ip-config.c b/libnm/nm-ip-config.c new file mode 100644 index 0000000000..fb50ac0393 --- /dev/null +++ b/libnm/nm-ip-config.c @@ -0,0 +1,498 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2007 - 2011 Novell, Inc. + * Copyright 2008 - 2014 Red Hat, Inc. + */ + +#include + +#include "nm-ip-config.h" +#include "nm-ip4-config.h" +#include "nm-ip6-config.h" +#include "nm-setting-ip-config.h" +#include "nm-dbus-interface.h" +#include "nm-object-private.h" +#include "nm-utils.h" +#include "nm-core-internal.h" + +G_DEFINE_ABSTRACT_TYPE (NMIPConfig, nm_ip_config, NM_TYPE_OBJECT) + +#define NM_IP_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_IP_CONFIG, NMIPConfigPrivate)) + +typedef struct { + char *gateway; + GPtrArray *addresses; + GPtrArray *routes; + char **nameservers; + char **domains; + char **searches; + char **wins; + + gboolean new_style_data; +} NMIPConfigPrivate; + +enum { + PROP_0, + PROP_FAMILY, + PROP_GATEWAY, + PROP_ADDRESSES, + PROP_ROUTES, + PROP_NAMESERVERS, + PROP_DOMAINS, + PROP_SEARCHES, + PROP_WINS_SERVERS, + + LAST_PROP +}; + +static void +nm_ip_config_init (NMIPConfig *config) +{ + NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (config); + + priv->addresses = g_ptr_array_new (); + priv->routes = g_ptr_array_new (); + priv->nameservers = g_new0 (char *, 1); + priv->domains = g_new0 (char *, 1); + priv->searches = g_new0 (char *, 1); + priv->wins = g_new0 (char *, 1); +} + +static gboolean +demarshal_ip_addresses (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +{ + NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object); + + if (priv->new_style_data) + return TRUE; + + g_ptr_array_unref (priv->addresses); + if (NM_IS_IP4_CONFIG (object)) + priv->addresses = nm_utils_ip4_addresses_from_variant (value, NULL); + else + priv->addresses = nm_utils_ip6_addresses_from_variant (value, NULL); + _nm_object_queue_notify (object, NM_IP_CONFIG_ADDRESSES); + + return TRUE; +} + +static gboolean +demarshal_ip_address_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +{ + NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object); + + priv->new_style_data = TRUE; + + g_ptr_array_unref (priv->addresses); + if (NM_IS_IP4_CONFIG (object)) + priv->addresses = nm_utils_ip_addresses_from_variant (value, AF_INET); + else + priv->addresses = nm_utils_ip_addresses_from_variant (value, AF_INET6); + _nm_object_queue_notify (object, NM_IP_CONFIG_ADDRESSES); + + return TRUE; +} + +static gboolean +demarshal_ip_array (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +{ + char ***obj_field; + + obj_field = field; + if (*obj_field) + g_strfreev (*obj_field); + + if (NM_IS_IP4_CONFIG (object)) + *obj_field = nm_utils_ip4_dns_from_variant (value); + else + *obj_field = nm_utils_ip6_dns_from_variant (value); + + _nm_object_queue_notify (object, pspec->name); + return TRUE; +} + +static gboolean +demarshal_ip_routes (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +{ + NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object); + + if (priv->new_style_data) + return TRUE; + + g_ptr_array_unref (priv->routes); + if (NM_IS_IP4_CONFIG (object)) + priv->routes = nm_utils_ip4_routes_from_variant (value); + else + priv->routes = nm_utils_ip6_routes_from_variant (value); + _nm_object_queue_notify (object, NM_IP_CONFIG_ROUTES); + + return TRUE; +} + +static gboolean +demarshal_ip_route_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +{ + NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object); + + priv->new_style_data = TRUE; + + g_ptr_array_unref (priv->routes); + if (NM_IS_IP4_CONFIG (object)) + priv->routes = nm_utils_ip_routes_from_variant (value, AF_INET); + else + priv->routes = nm_utils_ip_routes_from_variant (value, AF_INET6); + _nm_object_queue_notify (object, NM_IP_CONFIG_ROUTES); + + return TRUE; +} + +static void +init_dbus (NMObject *object) +{ + NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object); + const NMPropertiesInfo property_info[] = { + { NM_IP_CONFIG_GATEWAY, &priv->gateway, }, + { NM_IP_CONFIG_ADDRESSES, &priv->addresses, demarshal_ip_addresses }, + { "address-data", &priv->addresses, demarshal_ip_address_data }, + { NM_IP_CONFIG_ROUTES, &priv->routes, demarshal_ip_routes }, + { "route-data", &priv->routes, demarshal_ip_route_data }, + { NM_IP_CONFIG_NAMESERVERS, &priv->nameservers, demarshal_ip_array }, + { NM_IP_CONFIG_DOMAINS, &priv->domains, }, + { NM_IP_CONFIG_SEARCHES, &priv->searches, }, + { NM_IP_CONFIG_WINS_SERVERS, &priv->wins, demarshal_ip_array }, + { NULL }, + }; + + NM_OBJECT_CLASS (nm_ip_config_parent_class)->init_dbus (object); + + _nm_object_register_properties (object, + (NM_IS_IP4_CONFIG (object) ? + NM_DBUS_INTERFACE_IP4_CONFIG : + NM_DBUS_INTERFACE_IP6_CONFIG), + property_info); +} + +static void +finalize (GObject *object) +{ + NMIPConfigPrivate *priv = NM_IP_CONFIG_GET_PRIVATE (object); + + g_free (priv->gateway); + + g_ptr_array_unref (priv->addresses); + g_ptr_array_unref (priv->routes); + + g_strfreev (priv->nameservers); + g_strfreev (priv->domains); + g_strfreev (priv->searches); + g_strfreev (priv->wins); + + G_OBJECT_CLASS (nm_ip_config_parent_class)->finalize (object); +} + +static void +get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + NMIPConfig *self = NM_IP_CONFIG (object); + + switch (prop_id) { + case PROP_FAMILY: + g_value_set_int (value, nm_ip_config_get_family (self)); + break; + case PROP_GATEWAY: + g_value_set_string (value, nm_ip_config_get_gateway (self)); + break; + case PROP_ADDRESSES: + g_value_take_boxed (value, _nm_utils_copy_array (nm_ip_config_get_addresses (self), + (NMUtilsCopyFunc) nm_ip_address_dup, + (GDestroyNotify) nm_ip_address_unref)); + break; + case PROP_ROUTES: + g_value_take_boxed (value, _nm_utils_copy_array (nm_ip_config_get_routes (self), + (NMUtilsCopyFunc) nm_ip_route_dup, + (GDestroyNotify) nm_ip_route_unref)); + break; + case PROP_NAMESERVERS: + g_value_set_boxed (value, (char **) nm_ip_config_get_nameservers (self)); + break; + case PROP_DOMAINS: + g_value_set_boxed (value, (char **) nm_ip_config_get_domains (self)); + break; + case PROP_SEARCHES: + g_value_set_boxed (value, (char **) nm_ip_config_get_searches (self)); + break; + case PROP_WINS_SERVERS: + g_value_set_boxed (value, (char **) nm_ip_config_get_wins_servers (self)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +nm_ip_config_class_init (NMIPConfigClass *config_class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (config_class); + NMObjectClass *nm_object_class = NM_OBJECT_CLASS (config_class); + + g_type_class_add_private (config_class, sizeof (NMIPConfigPrivate)); + + /* virtual methods */ + object_class->get_property = get_property; + object_class->finalize = finalize; + + nm_object_class->init_dbus = init_dbus; + + /* properties */ + + /** + * NMIPConfig:family: + * + * The IP address family of the configuration; either %AF_INET or %AF_INET6. + **/ + g_object_class_install_property + (object_class, PROP_FAMILY, + g_param_spec_int (NM_IP_CONFIG_FAMILY, "", "", + 0, 255, AF_UNSPEC, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMIPConfig:gateway: + * + * The IP gateway address of the configuration as string. + **/ + g_object_class_install_property + (object_class, PROP_GATEWAY, + g_param_spec_string (NM_IP_CONFIG_GATEWAY, "", "", + NULL, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMIPConfig:addresses: + * + * A #GPtrArray containing the addresses (#NMIPAddress) of the configuration. + **/ + g_object_class_install_property + (object_class, PROP_ADDRESSES, + g_param_spec_boxed (NM_IP_CONFIG_ADDRESSES, "", "", + G_TYPE_PTR_ARRAY, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMIPConfig:routes: + * + * A #GPtrArray containing the routes (#NMIPRoute) of the configuration. + **/ + g_object_class_install_property + (object_class, PROP_ROUTES, + g_param_spec_boxed (NM_IP_CONFIG_ROUTES, "", "", + G_TYPE_PTR_ARRAY, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMIPConfig:nameservers: + * + * The array containing name server IP addresses of the configuration. + **/ + g_object_class_install_property + (object_class, PROP_NAMESERVERS, + g_param_spec_boxed (NM_IP_CONFIG_NAMESERVERS, "", "", + G_TYPE_STRV, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMIPConfig:domains: + * + * The array containing domain strings of the configuration. + **/ + g_object_class_install_property + (object_class, PROP_DOMAINS, + g_param_spec_boxed (NM_IP_CONFIG_DOMAINS, "", "", + G_TYPE_STRV, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMIPConfig:searches: + * + * The array containing DNS search strings of the configuration. + **/ + g_object_class_install_property + (object_class, PROP_SEARCHES, + g_param_spec_boxed (NM_IP_CONFIG_SEARCHES, "", "", + G_TYPE_STRV, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMIPConfig:wins-servers: + * + * The array containing WINS server IP addresses of the configuration. + * (This will always be empty for IPv6 configurations.) + **/ + g_object_class_install_property + (object_class, PROP_WINS_SERVERS, + g_param_spec_boxed (NM_IP_CONFIG_WINS_SERVERS, "", "", + G_TYPE_STRV, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); +} + +/** + * nm_ip_config_get_family: + * @config: a #NMIPConfig + * + * Gets the IP address family + * + * Returns: the IP address family; either %AF_INET or %AF_INET6 + **/ +int +nm_ip_config_get_family (NMIPConfig *config) +{ + g_return_val_if_fail (NM_IS_IP_CONFIG (config), AF_UNSPEC); + + return NM_IS_IP4_CONFIG (config) ? AF_INET : AF_INET6; +} + +/** + * nm_ip_config_get_gateway: + * @config: a #NMIPConfig + * + * Gets the IP gateway address. + * + * Returns: (transfer none): the IP address of the gateway. + **/ +const char * +nm_ip_config_get_gateway (NMIPConfig *config) +{ + g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL); + + return NM_IP_CONFIG_GET_PRIVATE (config)->gateway; +} + +/** + * nm_ip_config_get_addresses: + * @config: a #NMIPConfig + * + * Gets the IP addresses (containing the address, prefix, and gateway). + * + * Returns: (element-type NMIPAddress) (transfer none): the #GPtrArray + * containing #NMIPAddresses. This is the internal copy used by the + * configuration and must not be modified. + **/ +GPtrArray * +nm_ip_config_get_addresses (NMIPConfig *config) +{ + g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL); + + return NM_IP_CONFIG_GET_PRIVATE (config)->addresses; +} + +/** + * nm_ip_config_get_nameservers: + * @config: a #NMIPConfig + * + * Gets the domain name servers (DNS). + * + * Returns: (transfer none): the array of nameserver IP addresses + **/ +const char * const * +nm_ip_config_get_nameservers (NMIPConfig *config) +{ + g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL); + + return (const char * const *) NM_IP_CONFIG_GET_PRIVATE (config)->nameservers; +} + +/** + * nm_ip_config_get_domains: + * @config: a #NMIPConfig + * + * Gets the domain names. + * + * Returns: (transfer none): the array of domains. + * (This is never %NULL, though it may be 0-length). + **/ +const char * const * +nm_ip_config_get_domains (NMIPConfig *config) +{ + g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL); + + return (const char * const *) NM_IP_CONFIG_GET_PRIVATE (config)->domains; +} + +/** + * nm_ip_config_get_searches: + * @config: a #NMIPConfig + * + * Gets the DNS searches. + * + * Returns: (transfer none): the array of DNS search strings. + * (This is never %NULL, though it may be 0-length). + **/ +const char * const * +nm_ip_config_get_searches (NMIPConfig *config) +{ + g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL); + + return (const char * const *) NM_IP_CONFIG_GET_PRIVATE (config)->searches; +} + +/** + * nm_ip_config_get_wins_servers: + * @config: a #NMIPConfig + * + * Gets the Windows Internet Name Service servers (WINS). + * + * Returns: (transfer none): the arry of WINS server IP address strings. + * (This is never %NULL, though it may be 0-length.) + **/ +const char * const * +nm_ip_config_get_wins_servers (NMIPConfig *config) +{ + g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL); + + return (const char * const *) NM_IP_CONFIG_GET_PRIVATE (config)->wins; +} + +/** + * nm_ip_config_get_routes: + * @config: a #NMIPConfig + * + * Gets the routes. + * + * Returns: (element-type NMIPRoute) (transfer none): the #GPtrArray containing + * #NMIPRoutes. This is the internal copy used by the configuration, and must + * not be modified. + **/ +GPtrArray * +nm_ip_config_get_routes (NMIPConfig *config) +{ + g_return_val_if_fail (NM_IS_IP_CONFIG (config), NULL); + + return NM_IP_CONFIG_GET_PRIVATE (config)->routes; +} diff --git a/libnm/nm-ip-config.h b/libnm/nm-ip-config.h new file mode 100644 index 0000000000..815135a055 --- /dev/null +++ b/libnm/nm-ip-config.h @@ -0,0 +1,73 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2007 - 2008 Novell, Inc. + * Copyright 2008 - 2014 Red Hat, Inc. + */ + +#ifndef __NM_IP_CONFIG_H__ +#define __NM_IP_CONFIG_H__ + +#if !defined (__NETWORKMANAGER_H_INSIDE__) && !defined (NETWORKMANAGER_COMPILATION) +#error "Only can be included directly." +#endif + +#include + +G_BEGIN_DECLS + +#define NM_TYPE_IP_CONFIG (nm_ip_config_get_type ()) +#define NM_IP_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_IP_CONFIG, NMIPConfig)) +#define NM_IP_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_IP_CONFIG, NMIPConfigClass)) +#define NM_IS_IP_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_IP_CONFIG)) +#define NM_IS_IP_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_IP_CONFIG)) +#define NM_IP_CONFIG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_IP_CONFIG, NMIPConfigClass)) + +struct _NMIPConfig { + NMObject parent; +}; + +typedef struct { + NMObjectClass parent; + + /*< private >*/ + gpointer padding[8]; +} NMIPConfigClass; + +#define NM_IP_CONFIG_FAMILY "family" +#define NM_IP_CONFIG_GATEWAY "gateway" +#define NM_IP_CONFIG_ADDRESSES "addresses" +#define NM_IP_CONFIG_ROUTES "routes" +#define NM_IP_CONFIG_NAMESERVERS "nameservers" +#define NM_IP_CONFIG_DOMAINS "domains" +#define NM_IP_CONFIG_SEARCHES "searches" +#define NM_IP_CONFIG_WINS_SERVERS "wins-servers" + +GType nm_ip_config_get_type (void); + +int nm_ip_config_get_family (NMIPConfig *config); +const char * nm_ip_config_get_gateway (NMIPConfig *config); +GPtrArray * nm_ip_config_get_addresses (NMIPConfig *config); +GPtrArray * nm_ip_config_get_routes (NMIPConfig *config); +const char * const *nm_ip_config_get_nameservers (NMIPConfig *config); +const char * const *nm_ip_config_get_domains (NMIPConfig *config); +const char * const *nm_ip_config_get_searches (NMIPConfig *config); +const char * const *nm_ip_config_get_wins_servers (NMIPConfig *config); + +G_END_DECLS + +#endif /* __NM_IP_CONFIG_H__ */ diff --git a/libnm/nm-ip4-config.c b/libnm/nm-ip4-config.c index d242fe55db..8a7e007bfe 100644 --- a/libnm/nm-ip4-config.c +++ b/libnm/nm-ip4-config.c @@ -15,435 +15,23 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * Copyright 2007 - 2011 Novell, Inc. - * Copyright 2008 Red Hat, Inc. + * Copyright 2014 Red Hat, Inc. */ -#include - -#include #include "nm-ip4-config.h" -#include "nm-dbus-interface.h" #include "nm-object-private.h" -#include "nm-utils.h" -#include "nm-core-internal.h" - -G_DEFINE_TYPE (NMIP4Config, nm_ip4_config, NM_TYPE_OBJECT) - -#define NM_IP4_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_IP4_CONFIG, NMIP4ConfigPrivate)) - -typedef struct { - char *gateway; - GPtrArray *addresses; - GPtrArray *routes; - char **nameservers; - char **domains; - char **searches; - char **wins; - gboolean new_style_data; -} NMIP4ConfigPrivate; - -enum { - PROP_0, - PROP_GATEWAY, - PROP_ADDRESSES, - PROP_ROUTES, - PROP_NAMESERVERS, - PROP_DOMAINS, - PROP_SEARCHES, - PROP_WINS_SERVERS, - - LAST_PROP -}; +G_DEFINE_TYPE (NMIP4Config, nm_ip4_config, NM_TYPE_IP_CONFIG) static void nm_ip4_config_init (NMIP4Config *config) { - NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (config); - - priv->addresses = g_ptr_array_new (); - priv->routes = g_ptr_array_new (); - priv->nameservers = g_new0 (char *, 1); - priv->domains = g_new0 (char *, 1); - priv->searches = g_new0 (char *, 1); - priv->wins = g_new0 (char *, 1); -} - -static gboolean -demarshal_ip4_addresses (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) -{ - NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); - - if (priv->new_style_data) - return TRUE; - - g_ptr_array_unref (priv->addresses); - priv->addresses = nm_utils_ip4_addresses_from_variant (value, NULL); - _nm_object_queue_notify (object, NM_IP4_CONFIG_ADDRESSES); - - return TRUE; -} - -static gboolean -demarshal_ip4_address_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) -{ - NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); - - priv->new_style_data = TRUE; - - g_ptr_array_unref (priv->addresses); - priv->addresses = nm_utils_ip_addresses_from_variant (value, AF_INET); - _nm_object_queue_notify (object, NM_IP4_CONFIG_ADDRESSES); - - return TRUE; -} - -static gboolean -demarshal_ip4_array (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) -{ - char ***obj_field; - - obj_field = field; - if (*obj_field) - g_strfreev (*obj_field); - - *obj_field = nm_utils_ip4_dns_from_variant (value); - - _nm_object_queue_notify (object, pspec->name); - return TRUE; -} - -static gboolean -demarshal_ip4_routes (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) -{ - NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); - - if (priv->new_style_data) - return TRUE; - - g_ptr_array_unref (priv->routes); - priv->routes = nm_utils_ip4_routes_from_variant (value); - _nm_object_queue_notify (object, NM_IP4_CONFIG_ROUTES); - - return TRUE; -} - -static gboolean -demarshal_ip4_route_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) -{ - NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); - - priv->new_style_data = TRUE; - - g_ptr_array_unref (priv->routes); - priv->routes = nm_utils_ip_routes_from_variant (value, AF_INET); - _nm_object_queue_notify (object, NM_IP4_CONFIG_ROUTES); - - return TRUE; -} - -static void -init_dbus (NMObject *object) -{ - NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); - const NMPropertiesInfo property_info[] = { - { NM_IP4_CONFIG_GATEWAY, &priv->gateway, }, - { NM_IP4_CONFIG_ADDRESSES, &priv->addresses, demarshal_ip4_addresses }, - { "address-data", &priv->addresses, demarshal_ip4_address_data }, - { NM_IP4_CONFIG_ROUTES, &priv->routes, demarshal_ip4_routes }, - { "route-data", &priv->routes, demarshal_ip4_route_data }, - { NM_IP4_CONFIG_NAMESERVERS, &priv->nameservers, demarshal_ip4_array }, - { NM_IP4_CONFIG_DOMAINS, &priv->domains, }, - { NM_IP4_CONFIG_SEARCHES, &priv->searches, }, - { NM_IP4_CONFIG_WINS_SERVERS, &priv->wins, demarshal_ip4_array }, - { NULL }, - }; - - NM_OBJECT_CLASS (nm_ip4_config_parent_class)->init_dbus (object); - - _nm_object_register_properties (object, - NM_DBUS_INTERFACE_IP4_CONFIG, - property_info); -} - -static void -finalize (GObject *object) -{ - NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (object); - - g_free (priv->gateway); - - g_ptr_array_unref (priv->addresses); - g_ptr_array_unref (priv->routes); - - g_strfreev (priv->nameservers); - g_strfreev (priv->domains); - g_strfreev (priv->searches); - g_strfreev (priv->wins); - - G_OBJECT_CLASS (nm_ip4_config_parent_class)->finalize (object); -} - -static void -get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - NMIP4Config *self = NM_IP4_CONFIG (object); - - switch (prop_id) { - case PROP_GATEWAY: - g_value_set_string (value, nm_ip4_config_get_gateway (self)); - break; - case PROP_ADDRESSES: - g_value_take_boxed (value, _nm_utils_copy_array (nm_ip4_config_get_addresses (self), - (NMUtilsCopyFunc) nm_ip_address_dup, - (GDestroyNotify) nm_ip_address_unref)); - break; - case PROP_ROUTES: - g_value_take_boxed (value, _nm_utils_copy_array (nm_ip4_config_get_routes (self), - (NMUtilsCopyFunc) nm_ip_route_dup, - (GDestroyNotify) nm_ip_route_unref)); - break; - case PROP_NAMESERVERS: - g_value_set_boxed (value, (char **) nm_ip4_config_get_nameservers (self)); - break; - case PROP_DOMAINS: - g_value_set_boxed (value, (char **) nm_ip4_config_get_domains (self)); - break; - case PROP_SEARCHES: - g_value_set_boxed (value, (char **) nm_ip4_config_get_searches (self)); - break; - case PROP_WINS_SERVERS: - g_value_set_boxed (value, (char **) nm_ip4_config_get_wins_servers (self)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } } static void nm_ip4_config_class_init (NMIP4ConfigClass *config_class) { - GObjectClass *object_class = G_OBJECT_CLASS (config_class); NMObjectClass *nm_object_class = NM_OBJECT_CLASS (config_class); - g_type_class_add_private (config_class, sizeof (NMIP4ConfigPrivate)); - _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_IP4_CONFIG); - - /* virtual methods */ - object_class->get_property = get_property; - object_class->finalize = finalize; - - nm_object_class->init_dbus = init_dbus; - - /* properties */ - - /** - * NMIP4Config:gateway: - * - * The IP4 gateway address of the configuration as string. - **/ - g_object_class_install_property - (object_class, PROP_GATEWAY, - g_param_spec_string (NM_IP4_CONFIG_GATEWAY, "", "", - NULL, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMIP4Config:addresses: - * - * A #GPtrArray containing the addresses (#NMIPAddress) of the configuration. - **/ - g_object_class_install_property - (object_class, PROP_ADDRESSES, - g_param_spec_boxed (NM_IP4_CONFIG_ADDRESSES, "", "", - G_TYPE_PTR_ARRAY, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMIP4Config:routes: - * - * A #GPtrArray containing the routes (#NMIPRoute) of the configuration. - **/ - g_object_class_install_property - (object_class, PROP_ROUTES, - g_param_spec_boxed (NM_IP4_CONFIG_ROUTES, "", "", - G_TYPE_PTR_ARRAY, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMIP4Config:nameservers: - * - * The array containing name server IP addresses of the configuration. - **/ - g_object_class_install_property - (object_class, PROP_NAMESERVERS, - g_param_spec_boxed (NM_IP4_CONFIG_NAMESERVERS, "", "", - G_TYPE_STRV, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMIP4Config:domains: - * - * The array containing domain strings of the configuration. - **/ - g_object_class_install_property - (object_class, PROP_DOMAINS, - g_param_spec_boxed (NM_IP4_CONFIG_DOMAINS, "", "", - G_TYPE_STRV, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMIP4Config:searches: - * - * The array containing DNS search strings of the configuration. - **/ - g_object_class_install_property - (object_class, PROP_SEARCHES, - g_param_spec_boxed (NM_IP4_CONFIG_SEARCHES, "", "", - G_TYPE_STRV, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMIP4Config:wins-servers: - * - * The array containing WINS server IP addresses of the configuration. - **/ - g_object_class_install_property - (object_class, PROP_WINS_SERVERS, - g_param_spec_boxed (NM_IP4_CONFIG_WINS_SERVERS, "", "", - G_TYPE_STRV, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); -} - -/** - * nm_ip4_config_get_gateway: - * @config: a #NMIP4Config - * - * Gets the IP4 gateway address. - * - * Returns: (transfer none): the IP4 address of the gateway. - **/ -const char * -nm_ip4_config_get_gateway (NMIP4Config *config) -{ - g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL); - - return NM_IP4_CONFIG_GET_PRIVATE (config)->gateway; -} - -/** - * nm_ip4_config_get_addresses: - * @config: a #NMIP4Config - * - * Gets the IP4 addresses (containing the address, prefix, and gateway). - * - * Returns: (element-type NMIPAddress) (transfer none): the #GPtrArray - * containing #NMIPAddresses. This is the internal copy used by the - * configuration and must not be modified. - **/ -GPtrArray * -nm_ip4_config_get_addresses (NMIP4Config *config) -{ - g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL); - - return NM_IP4_CONFIG_GET_PRIVATE (config)->addresses; -} - -/** - * nm_ip4_config_get_nameservers: - * @config: a #NMIP4Config - * - * Gets the domain name servers (DNS). - * - * Returns: (transfer none): the array of nameserver IP addresses - **/ -const char * const * -nm_ip4_config_get_nameservers (NMIP4Config *config) -{ - g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL); - - return (const char * const *) NM_IP4_CONFIG_GET_PRIVATE (config)->nameservers; -} - -/** - * nm_ip4_config_get_domains: - * @config: a #NMIP4Config - * - * Gets the domain names. - * - * Returns: (transfer none): the array of domains. - * (This is never %NULL, though it may be 0-length). - **/ -const char * const * -nm_ip4_config_get_domains (NMIP4Config *config) -{ - g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL); - - return (const char * const *) NM_IP4_CONFIG_GET_PRIVATE (config)->domains; -} - -/** - * nm_ip4_config_get_searches: - * @config: a #NMIP4Config - * - * Gets the DNS searches. - * - * Returns: (transfer none): the array of DNS search strings. - * (This is never %NULL, though it may be 0-length). - **/ -const char * const * -nm_ip4_config_get_searches (NMIP4Config *config) -{ - g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL); - - return (const char * const *) NM_IP4_CONFIG_GET_PRIVATE (config)->searches; -} - -/** - * nm_ip4_config_get_wins_servers: - * @config: a #NMIP4Config - * - * Gets the Windows Internet Name Service servers (WINS). - * - * Returns: (element-type guint32) (transfer none): the #GArray containing #guint32s. - * This is the internal copy used by the configuration and must not be - * modified. - **/ -const char * const * -nm_ip4_config_get_wins_servers (NMIP4Config *config) -{ - g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL); - - return (const char * const *) NM_IP4_CONFIG_GET_PRIVATE (config)->wins; -} - -/** - * nm_ip4_config_get_routes: - * @config: a #NMIP4Config - * - * Gets the routes. - * - * Returns: (element-type NMIPRoute) (transfer none): the #GPtrArray containing - * #NMIPRoutes. This is the internal copy used by the configuration, and must - * not be modified. - **/ -GPtrArray * -nm_ip4_config_get_routes (NMIP4Config *config) -{ - g_return_val_if_fail (NM_IS_IP4_CONFIG (config), NULL); - - return NM_IP4_CONFIG_GET_PRIVATE (config)->routes; } diff --git a/libnm/nm-ip4-config.h b/libnm/nm-ip4-config.h index a933a5e277..e2a18e0f31 100644 --- a/libnm/nm-ip4-config.h +++ b/libnm/nm-ip4-config.h @@ -22,11 +22,7 @@ #ifndef __NM_IP4_CONFIG_H__ #define __NM_IP4_CONFIG_H__ -#if !defined (__NETWORKMANAGER_H_INSIDE__) && !defined (NETWORKMANAGER_COMPILATION) -#error "Only can be included directly." -#endif - -#include +#include G_BEGIN_DECLS @@ -37,35 +33,19 @@ G_BEGIN_DECLS #define NM_IS_IP4_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_IP4_CONFIG)) #define NM_IP4_CONFIG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_IP4_CONFIG, NMIP4ConfigClass)) -struct _NMIP4Config { - NMObject parent; -}; +typedef struct { + NMIPConfig parent; +} NMIP4Config; typedef struct { - NMObjectClass parent; + NMIPConfigClass parent; /*< private >*/ gpointer padding[4]; } NMIP4ConfigClass; -#define NM_IP4_CONFIG_GATEWAY "gateway" -#define NM_IP4_CONFIG_ADDRESSES "addresses" -#define NM_IP4_CONFIG_ROUTES "routes" -#define NM_IP4_CONFIG_NAMESERVERS "nameservers" -#define NM_IP4_CONFIG_DOMAINS "domains" -#define NM_IP4_CONFIG_SEARCHES "searches" -#define NM_IP4_CONFIG_WINS_SERVERS "wins-servers" - GType nm_ip4_config_get_type (void); -const char * nm_ip4_config_get_gateway (NMIP4Config *config); -GPtrArray * nm_ip4_config_get_addresses (NMIP4Config *config); -GPtrArray * nm_ip4_config_get_routes (NMIP4Config *config); -const char * const *nm_ip4_config_get_nameservers (NMIP4Config *config); -const char * const *nm_ip4_config_get_domains (NMIP4Config *config); -const char * const *nm_ip4_config_get_searches (NMIP4Config *config); -const char * const *nm_ip4_config_get_wins_servers (NMIP4Config *config); - G_END_DECLS #endif /* __NM_IP4_CONFIG_H__ */ diff --git a/libnm/nm-ip6-config.c b/libnm/nm-ip6-config.c index 7318f5f6ea..cd64e7594f 100644 --- a/libnm/nm-ip6-config.c +++ b/libnm/nm-ip6-config.c @@ -15,399 +15,23 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * Copyright 2007 - 2008 Novell, Inc. - * Copyright 2008 - 2014 Red Hat, Inc. + * Copyright 2014 Red Hat, Inc. */ -#include - -#include #include "nm-ip6-config.h" -#include "nm-dbus-interface.h" #include "nm-object-private.h" -#include "nm-utils.h" -#include "nm-core-internal.h" - -G_DEFINE_TYPE (NMIP6Config, nm_ip6_config, NM_TYPE_OBJECT) - -#define NM_IP6_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_IP6_CONFIG, NMIP6ConfigPrivate)) - -typedef struct { - char *gateway; - GPtrArray *addresses; - GPtrArray *routes; - char **nameservers; - char **domains; - char **searches; - - gboolean new_style_data; -} NMIP6ConfigPrivate; - -enum { - PROP_0, - PROP_GATEWAY, - PROP_ADDRESSES, - PROP_ROUTES, - PROP_NAMESERVERS, - PROP_DOMAINS, - PROP_SEARCHES, - - LAST_PROP -}; - -static gboolean -demarshal_ip6_addresses (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) -{ - NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); - - if (priv->new_style_data) - return TRUE; - - g_ptr_array_unref (priv->addresses); - priv->addresses = nm_utils_ip6_addresses_from_variant (value, NULL); - _nm_object_queue_notify (object, NM_IP6_CONFIG_ADDRESSES); - - return TRUE; -} - -static gboolean -demarshal_ip6_address_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) -{ - NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); - - priv->new_style_data = TRUE; - - g_ptr_array_unref (priv->addresses); - priv->addresses = nm_utils_ip_addresses_from_variant (value, AF_INET6); - _nm_object_queue_notify (object, NM_IP6_CONFIG_ADDRESSES); - - return TRUE; -} - -static gboolean -demarshal_ip6_nameserver_array (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) -{ - char ***obj_field; - - obj_field = field; - if (*obj_field) - g_strfreev (*obj_field); - - *obj_field = nm_utils_ip6_dns_from_variant (value); - - _nm_object_queue_notify (object, pspec->name); - return TRUE; -} - -static gboolean -demarshal_ip6_routes (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) -{ - NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); - - if (priv->new_style_data) - return TRUE; - - g_ptr_array_unref (priv->routes); - priv->routes = nm_utils_ip6_routes_from_variant (value); - _nm_object_queue_notify (object, NM_IP6_CONFIG_ROUTES); - - return TRUE; -} - -static gboolean -demarshal_ip6_route_data (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) -{ - NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); - priv->new_style_data = TRUE; - - g_ptr_array_unref (priv->routes); - priv->routes = nm_utils_ip_routes_from_variant (value, AF_INET6); - _nm_object_queue_notify (object, NM_IP6_CONFIG_ROUTES); - - return TRUE; -} - -static void -init_dbus (NMObject *object) -{ - NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); - const NMPropertiesInfo property_info[] = { - { NM_IP6_CONFIG_GATEWAY, &priv->gateway, }, - { NM_IP6_CONFIG_ADDRESSES, &priv->addresses, demarshal_ip6_addresses }, - { "address-data", &priv->addresses, demarshal_ip6_address_data }, - { NM_IP6_CONFIG_ROUTES, &priv->routes, demarshal_ip6_routes }, - { "route-data", &priv->routes, demarshal_ip6_route_data }, - { NM_IP6_CONFIG_NAMESERVERS, &priv->nameservers, demarshal_ip6_nameserver_array }, - { NM_IP6_CONFIG_DOMAINS, &priv->domains, }, - { NM_IP6_CONFIG_SEARCHES, &priv->searches, }, - { NULL }, - }; - - NM_OBJECT_CLASS (nm_ip6_config_parent_class)->init_dbus (object); - - _nm_object_register_properties (object, - NM_DBUS_INTERFACE_IP6_CONFIG, - property_info); -} - -/** - * nm_ip6_config_get_gateway: - * @config: a #NMIP6Config - * - * Gets the IP6 gateway. - * - * Returns: (transfer none): the IPv6 gateway of the configuration. - **/ -const char * -nm_ip6_config_get_gateway (NMIP6Config *config) -{ - g_return_val_if_fail (NM_IS_IP6_CONFIG (config), NULL); - - return NM_IP6_CONFIG_GET_PRIVATE (config)->gateway; -} - -/** - * nm_ip6_config_get_addresses: - * @config: a #NMIP6Config - * - * Gets the IP6 addresses (containing the address, prefix, and gateway). - * - * Returns: (element-type NMIPAddress) (transfer none): the #GPtrArray - * containing #NMIPAddresses. This is the internal copy used by the - * configuration and must not be modified. - **/ -GPtrArray * -nm_ip6_config_get_addresses (NMIP6Config *config) -{ - g_return_val_if_fail (NM_IS_IP6_CONFIG (config), NULL); - - return NM_IP6_CONFIG_GET_PRIVATE (config)->addresses; -} - -/** - * nm_ip6_config_get_nameservers: - * @config: a #NMIP6Config - * - * Gets the domain name servers (DNS). - * - * Returns: (transfer none): the array of nameserver IP addresses - **/ -const char * const * -nm_ip6_config_get_nameservers (NMIP6Config *config) -{ - g_return_val_if_fail (NM_IS_IP6_CONFIG (config), NULL); - - return (const char * const *) NM_IP6_CONFIG_GET_PRIVATE (config)->nameservers; -} - -/** - * nm_ip6_config_get_domains: - * @config: a #NMIP6Config - * - * Gets the domain names. - * - * Returns: (transfer none): the array of domains. - * (This is never %NULL, though it may be 0-length). - **/ -const char * const * -nm_ip6_config_get_domains (NMIP6Config *config) -{ - g_return_val_if_fail (NM_IS_IP6_CONFIG (config), NULL); - - return (const char * const *) NM_IP6_CONFIG_GET_PRIVATE (config)->domains; -} - -/** - * nm_ip6_config_get_searches: - * @config: a #NMIP6Config - * - * Gets the DNS search strings. - * - * Returns: (transfer none): the array of DNS search strings. - * (This is never %NULL, though it may be 0-length). - **/ -const char * const * -nm_ip6_config_get_searches (NMIP6Config *config) -{ - g_return_val_if_fail (NM_IS_IP6_CONFIG (config), NULL); - - return (const char * const *) NM_IP6_CONFIG_GET_PRIVATE (config)->searches; -} - -/** - * nm_ip6_config_get_routes: - * @config: a #NMIP6Config - * - * Gets the routes. - * - * Returns: (element-type NMIPRoute) (transfer none): the #GPtrArray containing - * #NMIPRoutes. This is the internal copy used by the configuration, and must - * not be modified. - **/ -GPtrArray * -nm_ip6_config_get_routes (NMIP6Config *config) -{ - g_return_val_if_fail (NM_IS_IP6_CONFIG (config), NULL); - - return NM_IP6_CONFIG_GET_PRIVATE (config)->routes; -} - -static void -finalize (GObject *object) -{ - NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (object); - - g_free (priv->gateway); - - g_ptr_array_unref (priv->addresses); - g_ptr_array_unref (priv->routes); - - g_strfreev (priv->nameservers); - g_strfreev (priv->domains); - g_strfreev (priv->searches); - - G_OBJECT_CLASS (nm_ip6_config_parent_class)->finalize (object); -} - -static void -get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - NMIP6Config *self = NM_IP6_CONFIG (object); - - switch (prop_id) { - case PROP_GATEWAY: - g_value_set_string (value, nm_ip6_config_get_gateway (self)); - break; - case PROP_ADDRESSES: - g_value_take_boxed (value, _nm_utils_copy_array (nm_ip6_config_get_addresses (self), - (NMUtilsCopyFunc) nm_ip_address_dup, - (GDestroyNotify) nm_ip_address_unref)); - break; - case PROP_ROUTES: - g_value_take_boxed (value, _nm_utils_copy_array (nm_ip6_config_get_routes (self), - (NMUtilsCopyFunc) nm_ip_route_dup, - (GDestroyNotify) nm_ip_route_unref)); - break; - case PROP_NAMESERVERS: - g_value_set_boxed (value, (char **) nm_ip6_config_get_nameservers (self)); - break; - case PROP_DOMAINS: - g_value_set_boxed (value, (char **) nm_ip6_config_get_domains (self)); - break; - case PROP_SEARCHES: - g_value_set_boxed (value, (char **) nm_ip6_config_get_searches (self)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} +G_DEFINE_TYPE (NMIP6Config, nm_ip6_config, NM_TYPE_IP_CONFIG) static void nm_ip6_config_init (NMIP6Config *config) { - NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (config); - - priv->addresses = g_ptr_array_new (); - priv->routes = g_ptr_array_new (); - priv->nameservers = g_new0 (char *, 1); - priv->domains = g_new0 (char *, 1); - priv->searches = g_new0 (char *, 1); } static void nm_ip6_config_class_init (NMIP6ConfigClass *config_class) { - GObjectClass *object_class = G_OBJECT_CLASS (config_class); NMObjectClass *nm_object_class = NM_OBJECT_CLASS (config_class); - g_type_class_add_private (config_class, sizeof (NMIP6ConfigPrivate)); - _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_IP6_CONFIG); - - /* virtual methods */ - object_class->get_property = get_property; - object_class->finalize = finalize; - - nm_object_class->init_dbus = init_dbus; - - /* properties */ - - /** - * NMIP6Config:gateway: - * - * The IPv6 gateway as string - **/ - g_object_class_install_property - (object_class, PROP_GATEWAY, - g_param_spec_string (NM_IP6_CONFIG_GATEWAY, "", "", - NULL, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMIP6Config:addresses: - * - * The #GPtrArray containing the IPv6 addresses (#NMIPAddress). - **/ - g_object_class_install_property - (object_class, PROP_ADDRESSES, - g_param_spec_boxed (NM_IP6_CONFIG_ADDRESSES, "", "", - G_TYPE_PTR_ARRAY, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMIP6Config:routes: - * - * The #GPtrArray containing the IPv6 routes (#NMIPRoute). - **/ - g_object_class_install_property - (object_class, PROP_ROUTES, - g_param_spec_boxed (NM_IP6_CONFIG_ROUTES, "", "", - G_TYPE_PTR_ARRAY, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMIP6Config:nameservers: - * - * The #GPtrArray containing elements of type 'struct ip6_addr' which - * contain the addresses of nameservers of the configuration. - **/ - g_object_class_install_property - (object_class, PROP_NAMESERVERS, - g_param_spec_boxed (NM_IP6_CONFIG_NAMESERVERS, "", "", - G_TYPE_STRV, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMIP6Config:domains: - * - * The #GPtrArray containing domain strings of the configuration. - **/ - g_object_class_install_property - (object_class, PROP_DOMAINS, - g_param_spec_boxed (NM_IP6_CONFIG_DOMAINS, "", "", - G_TYPE_STRV, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMIP6Config:searches: - * - * The #GPtrArray containing dns search strings of the configuration. - **/ - g_object_class_install_property - (object_class, PROP_SEARCHES, - g_param_spec_boxed (NM_IP6_CONFIG_SEARCHES, "", "", - G_TYPE_STRV, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - } diff --git a/libnm/nm-ip6-config.h b/libnm/nm-ip6-config.h index 2c98633411..1953476d00 100644 --- a/libnm/nm-ip6-config.h +++ b/libnm/nm-ip6-config.h @@ -22,11 +22,7 @@ #ifndef __NM_IP6_CONFIG_H__ #define __NM_IP6_CONFIG_H__ -#if !defined (__NETWORKMANAGER_H_INSIDE__) && !defined (NETWORKMANAGER_COMPILATION) -#error "Only can be included directly." -#endif - -#include +#include G_BEGIN_DECLS @@ -37,33 +33,19 @@ G_BEGIN_DECLS #define NM_IS_IP6_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_IP6_CONFIG)) #define NM_IP6_CONFIG_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_IP6_CONFIG, NMIP6ConfigClass)) -struct _NMIP6Config { - NMObject parent; -}; +typedef struct { + NMIPConfig parent; +} NMIP6Config; typedef struct { - NMObjectClass parent; + NMIPConfigClass parent; /*< private >*/ gpointer padding[4]; } NMIP6ConfigClass; -#define NM_IP6_CONFIG_GATEWAY "gateway" -#define NM_IP6_CONFIG_ADDRESSES "addresses" -#define NM_IP6_CONFIG_ROUTES "routes" -#define NM_IP6_CONFIG_NAMESERVERS "nameservers" -#define NM_IP6_CONFIG_DOMAINS "domains" -#define NM_IP6_CONFIG_SEARCHES "searches" - GType nm_ip6_config_get_type (void); -const char * nm_ip6_config_get_gateway (NMIP6Config *config); -GPtrArray * nm_ip6_config_get_addresses (NMIP6Config *config); -GPtrArray * nm_ip6_config_get_routes (NMIP6Config *config); -const char * const * nm_ip6_config_get_nameservers (NMIP6Config *config); -const char * const * nm_ip6_config_get_domains (NMIP6Config *config); -const char * const * nm_ip6_config_get_searches (NMIP6Config *config); - G_END_DECLS #endif /* __NM_IP6_CONFIG_H__ */ diff --git a/libnm/nm-types.h b/libnm/nm-types.h index 940a329454..eb922036d0 100644 --- a/libnm/nm-types.h +++ b/libnm/nm-types.h @@ -45,8 +45,7 @@ typedef struct _NMDeviceWifi NMDeviceWifi; typedef struct _NMDeviceWimax NMDeviceWimax; typedef struct _NMDhcp4Config NMDhcp4Config; typedef struct _NMDhcp6Config NMDhcp6Config; -typedef struct _NMIP4Config NMIP4Config; -typedef struct _NMIP6Config NMIP6Config; +typedef struct _NMIPConfig NMIPConfig; typedef struct _NMObject NMObject; typedef struct _NMRemoteConnection NMRemoteConnection; typedef struct _NMSecretAgent NMSecretAgent; -- cgit v1.2.1 From ca18b2d44290c938894dc63f7b03d04a3be271bc Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Wed, 22 Oct 2014 13:48:18 -0400 Subject: libnm: create NMDhcpConfig as parent of NMDhcp4Config and NMDhcp6Config As with NMIP4Config and NMIP6Config, merge the two DHCP config classes into one in the public API. --- clients/cli/common.c | 8 +- clients/cli/common.h | 4 +- clients/cli/connections.c | 4 +- clients/cli/devices.c | 3 +- docs/libnm/Makefile.am | 2 + docs/libnm/libnm-docs.xml | 3 +- libnm/Makefile.am | 6 +- libnm/NetworkManager.h | 3 +- libnm/libnm.ver | 10 +- libnm/nm-active-connection.c | 32 +++---- libnm/nm-active-connection.h | 4 +- libnm/nm-device.c | 38 ++++---- libnm/nm-device.h | 4 +- libnm/nm-dhcp-config.c | 217 +++++++++++++++++++++++++++++++++++++++++++ libnm/nm-dhcp-config.h | 62 +++++++++++++ libnm/nm-dhcp4-config.c | 150 +----------------------------- libnm/nm-dhcp4-config.h | 23 ++--- libnm/nm-dhcp6-config.c | 148 +---------------------------- libnm/nm-dhcp6-config.h | 23 ++--- libnm/nm-types.h | 3 +- 20 files changed, 354 insertions(+), 393 deletions(-) create mode 100644 libnm/nm-dhcp-config.c create mode 100644 libnm/nm-dhcp-config.h diff --git a/clients/cli/common.c b/clients/cli/common.c index 5493b166d7..2003934993 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -251,7 +251,7 @@ print_ip6_config (NMIPConfig *cfg6, } gboolean -print_dhcp4_config (NMDhcp4Config *dhcp4, +print_dhcp4_config (NMDhcpConfig *dhcp4, NmCli *nmc, const char *group_prefix, const char *one_field) @@ -263,7 +263,7 @@ print_dhcp4_config (NMDhcp4Config *dhcp4, if (dhcp4 == NULL) return FALSE; - table = nm_dhcp4_config_get_options (dhcp4); + table = nm_dhcp_config_get_options (dhcp4); if (table) { GHashTableIter table_iter; gpointer key, value; @@ -299,7 +299,7 @@ print_dhcp4_config (NMDhcp4Config *dhcp4, } gboolean -print_dhcp6_config (NMDhcp6Config *dhcp6, +print_dhcp6_config (NMDhcpConfig *dhcp6, NmCli *nmc, const char *group_prefix, const char *one_field) @@ -311,7 +311,7 @@ print_dhcp6_config (NMDhcp6Config *dhcp6, if (dhcp6 == NULL) return FALSE; - table = nm_dhcp6_config_get_options (dhcp6); + table = nm_dhcp_config_get_options (dhcp6); if (table) { GHashTableIter table_iter; gpointer key, value; diff --git a/clients/cli/common.h b/clients/cli/common.h index 2787b1935b..6ae74f4f4a 100644 --- a/clients/cli/common.h +++ b/clients/cli/common.h @@ -26,8 +26,8 @@ gboolean print_ip4_config (NMIPConfig *cfg4, NmCli *nmc, const char *group_prefix, const char *one_field); gboolean print_ip6_config (NMIPConfig *cfg6, NmCli *nmc, const char *group_prefix, const char *one_field); -gboolean print_dhcp4_config (NMDhcp4Config *dhcp4, NmCli *nmc, const char *group_prefix, const char *one_field); -gboolean print_dhcp6_config (NMDhcp6Config *dhcp6, NmCli *nmc, const char *group_prefix, const char *one_field); +gboolean print_dhcp4_config (NMDhcpConfig *dhcp4, NmCli *nmc, const char *group_prefix, const char *one_field); +gboolean print_dhcp6_config (NMDhcpConfig *dhcp6, NmCli *nmc, const char *group_prefix, const char *one_field); NMIPAddress *nmc_parse_and_build_address (int family, const char *ip_str, GError **error); NMIPRoute *nmc_parse_and_build_route (int family, const char *first, const char *second, const char *third, GError **error); diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 35e88b4378..3f921a4003 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -1137,7 +1137,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) /* DHCP4 */ if (strcasecmp (nmc_fields_con_active_details_groups[group_idx].name, nmc_fields_con_active_details_groups[2].name) == 0) { gboolean b1 = FALSE; - NMDhcp4Config *dhcp4 = nm_active_connection_get_dhcp4_config (acon); + NMDhcpConfig *dhcp4 = nm_active_connection_get_dhcp4_config (acon); b1 = print_dhcp4_config (dhcp4, nmc, "DHCP4", group_fld); was_output = was_output || b1; @@ -1155,7 +1155,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) /* DHCP6 */ if (strcasecmp (nmc_fields_con_active_details_groups[group_idx].name, nmc_fields_con_active_details_groups[4].name) == 0) { gboolean b1 = FALSE; - NMDhcp6Config *dhcp6 = nm_active_connection_get_dhcp6_config (acon); + NMDhcpConfig *dhcp6 = nm_active_connection_get_dhcp6_config (acon); b1 = print_dhcp6_config (dhcp6, nmc, "DHCP6", group_fld); was_output = was_output || b1; diff --git a/clients/cli/devices.c b/clients/cli/devices.c index 371c05bf13..ae8664e987 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -768,8 +768,7 @@ show_device_info (NMDevice *device, NmCli *nmc) size_t tmpl_len; gboolean was_output = FALSE; NMIPConfig *cfg4, *cfg6; - NMDhcp4Config *dhcp4; - NMDhcp6Config *dhcp6; + NMDhcpConfig *dhcp4, *dhcp6; const char *base_hdr = _("Device details"); GPtrArray *fields_in_section = NULL; diff --git a/docs/libnm/Makefile.am b/docs/libnm/Makefile.am index 0c82e751a9..2d1200b485 100644 --- a/docs/libnm/Makefile.am +++ b/docs/libnm/Makefile.am @@ -34,6 +34,8 @@ IGNORE_HFILES= \ nm-dbus-helpers-private.h \ nm-core-internal.h \ nm-device-private.h \ + nm-dhcp4-config.h \ + nm-dhcp6-config.h \ nm-ip4-config.h \ nm-ip6-config.h \ nm-manager.h \ diff --git a/docs/libnm/libnm-docs.xml b/docs/libnm/libnm-docs.xml index c60b4727e6..e722a30964 100644 --- a/docs/libnm/libnm-docs.xml +++ b/docs/libnm/libnm-docs.xml @@ -129,8 +129,7 @@ - - + diff --git a/libnm/Makefile.am b/libnm/Makefile.am index 3656cddfe0..26e0a4c737 100644 --- a/libnm/Makefile.am +++ b/libnm/Makefile.am @@ -43,8 +43,7 @@ libnminclude_hfiles = \ nm-device-wifi.h \ nm-device-wimax.h \ nm-device.h \ - nm-dhcp4-config.h \ - nm-dhcp6-config.h \ + nm-dhcp-config.h \ nm-enum-types.h \ nm-ip-config.h \ nm-object.h \ @@ -65,6 +64,8 @@ libnminclude_HEADERS = \ libnm_la_private_headers = \ nm-dbus-helpers.h \ nm-device-private.h \ + nm-dhcp4-config.h \ + nm-dhcp6-config.h \ nm-ip4-config.h \ nm-ip6-config.h \ nm-manager.h \ @@ -92,6 +93,7 @@ libnm_la_csources = \ nm-device-wifi.c \ nm-device-wimax.c \ nm-device.c \ + nm-dhcp-config.c \ nm-dhcp4-config.c \ nm-dhcp6-config.c \ nm-enum-types.c \ diff --git a/libnm/NetworkManager.h b/libnm/NetworkManager.h index 7604e77df5..39212fda1d 100644 --- a/libnm/NetworkManager.h +++ b/libnm/NetworkManager.h @@ -43,8 +43,7 @@ #include #include #include -#include -#include +#include #include #include #include diff --git a/libnm/libnm.ver b/libnm/libnm.ver index 50ee710961..e4e347aaab 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -262,12 +262,10 @@ global: nm_device_wimax_get_rssi; nm_device_wimax_get_tx_power; nm_device_wimax_get_type; - nm_dhcp4_config_get_one_option; - nm_dhcp4_config_get_options; - nm_dhcp4_config_get_type; - nm_dhcp6_config_get_one_option; - nm_dhcp6_config_get_options; - nm_dhcp6_config_get_type; + nm_dhcp_config_get_family; + nm_dhcp_config_get_one_option; + nm_dhcp_config_get_options; + nm_dhcp_config_get_type; nm_ip_address_equal; nm_ip_address_get_address; nm_ip_address_get_address_binary; diff --git a/libnm/nm-active-connection.c b/libnm/nm-active-connection.c index 3551afeb94..6988f44c67 100644 --- a/libnm/nm-active-connection.c +++ b/libnm/nm-active-connection.c @@ -58,10 +58,10 @@ typedef struct { NMActiveConnectionState state; gboolean is_default; NMIPConfig *ip4_config; - NMDhcp4Config *dhcp4_config; + NMDhcpConfig *dhcp4_config; gboolean is_default6; NMIPConfig *ip6_config; - NMDhcp6Config *dhcp6_config; + NMDhcpConfig *dhcp6_config; gboolean is_vpn; NMDevice *master; } NMActiveConnectionPrivate; @@ -259,14 +259,14 @@ nm_active_connection_get_ip4_config (NMActiveConnection *connection) * nm_active_connection_get_dhcp4_config: * @connection: an #NMActiveConnection * - * Gets the current #NMDhcp4Config (if any) associated with the + * Gets the current IPv4 #NMDhcpConfig (if any) associated with the * #NMActiveConnection. * - * Returns: (transfer none): the #NMDhcp4Config, or %NULL if the - * connection does not use DHCP, or is not in the - * %NM_ACTIVE_CONNECTION_STATE_ACTIVATED state. + * Returns: (transfer none): the IPv4 #NMDhcpConfig, or %NULL if the connection + * does not use DHCP, or is not in the %NM_ACTIVE_CONNECTION_STATE_ACTIVATED + * state. **/ -NMDhcp4Config * +NMDhcpConfig * nm_active_connection_get_dhcp4_config (NMActiveConnection *connection) { g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL); @@ -312,14 +312,14 @@ nm_active_connection_get_ip6_config (NMActiveConnection *connection) * nm_active_connection_get_dhcp6_config: * @connection: an #NMActiveConnection * - * Gets the current #NMDhcp6Config (if any) associated with the + * Gets the current IPv6 #NMDhcpConfig (if any) associated with the * #NMActiveConnection. * - * Returns: (transfer none): the #NMDhcp6Config, or %NULL if the - * connection does not use DHCPv6, or is not in the - * %NM_ACTIVE_CONNECTION_STATE_ACTIVATED state. + * Returns: (transfer none): the IPv6 #NMDhcpConfig, or %NULL if the connection + * does not use DHCPv6, or is not in the %NM_ACTIVE_CONNECTION_STATE_ACTIVATED + * state. **/ -NMDhcp6Config * +NMDhcpConfig * nm_active_connection_get_dhcp6_config (NMActiveConnection *connection) { g_return_val_if_fail (NM_IS_ACTIVE_CONNECTION (connection), NULL); @@ -641,12 +641,12 @@ nm_active_connection_class_init (NMActiveConnectionClass *ap_class) /** * NMActiveConnection:dhcp4-config: * - * The #NMDhcp4Config of the connection. + * The IPv4 #NMDhcpConfig of the connection. **/ g_object_class_install_property (object_class, PROP_DHCP4_CONFIG, g_param_spec_object (NM_ACTIVE_CONNECTION_DHCP4_CONFIG, "", "", - NM_TYPE_DHCP4_CONFIG, + NM_TYPE_DHCP_CONFIG, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); @@ -677,12 +677,12 @@ nm_active_connection_class_init (NMActiveConnectionClass *ap_class) /** * NMActiveConnection:dhcp6-config: * - * The #NMDhcp6Config of the connection. + * The IPv6 #NMDhcpConfig of the connection. **/ g_object_class_install_property (object_class, PROP_DHCP6_CONFIG, g_param_spec_object (NM_ACTIVE_CONNECTION_DHCP6_CONFIG, "", "", - NM_TYPE_DHCP6_CONFIG, + NM_TYPE_DHCP_CONFIG, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); diff --git a/libnm/nm-active-connection.h b/libnm/nm-active-connection.h index b6b31d9c0a..4bc8414828 100644 --- a/libnm/nm-active-connection.h +++ b/libnm/nm-active-connection.h @@ -76,10 +76,10 @@ NMActiveConnectionState nm_active_connection_get_state (NMActive NMDevice *nm_active_connection_get_master (NMActiveConnection *connection); gboolean nm_active_connection_get_default (NMActiveConnection *connection); NMIPConfig *nm_active_connection_get_ip4_config (NMActiveConnection *connection); -NMDhcp4Config *nm_active_connection_get_dhcp4_config (NMActiveConnection *connection); +NMDhcpConfig *nm_active_connection_get_dhcp4_config (NMActiveConnection *connection); gboolean nm_active_connection_get_default6 (NMActiveConnection *connection); NMIPConfig *nm_active_connection_get_ip6_config (NMActiveConnection *connection); -NMDhcp6Config *nm_active_connection_get_dhcp6_config (NMActiveConnection *connection); +NMDhcpConfig *nm_active_connection_get_dhcp6_config (NMActiveConnection *connection); gboolean nm_active_connection_get_vpn (NMActiveConnection *connection); G_END_DECLS diff --git a/libnm/nm-device.c b/libnm/nm-device.c index 16f88d9308..29f5db8d33 100644 --- a/libnm/nm-device.c +++ b/libnm/nm-device.c @@ -84,9 +84,9 @@ typedef struct { gboolean firmware_missing; gboolean autoconnect; NMIPConfig *ip4_config; - NMDhcp4Config *dhcp4_config; + NMDhcpConfig *dhcp4_config; NMIPConfig *ip6_config; - NMDhcp6Config *dhcp6_config; + NMDhcpConfig *dhcp6_config; NMDeviceState state; NMDeviceState last_seen_state; NMDeviceStateReason reason; @@ -658,12 +658,12 @@ nm_device_class_init (NMDeviceClass *device_class) /** * NMDevice:dhcp4-config: * - * The #NMDhcp4Config of the device. + * The IPv4 #NMDhcpConfig of the device. **/ g_object_class_install_property (object_class, PROP_DHCP4_CONFIG, g_param_spec_object (NM_DEVICE_DHCP4_CONFIG, "", "", - NM_TYPE_DHCP4_CONFIG, + NM_TYPE_DHCP_CONFIG, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); @@ -682,12 +682,12 @@ nm_device_class_init (NMDeviceClass *device_class) /** * NMDevice:dhcp6-config: * - * The #NMDhcp6Config of the device. + * The IPv6 #NMDhcpConfig of the device. **/ g_object_class_install_property (object_class, PROP_DHCP6_CONFIG, g_param_spec_object (NM_DEVICE_DHCP6_CONFIG, "", "", - NM_TYPE_DHCP6_CONFIG, + NM_TYPE_DHCP_CONFIG, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); @@ -1126,16 +1126,15 @@ nm_device_get_ip4_config (NMDevice *device) * nm_device_get_dhcp4_config: * @device: a #NMDevice * - * Gets the current #NMDhcp4Config associated with the #NMDevice. + * Gets the current IPv4 #NMDhcpConfig associated with the #NMDevice. * - * Note that as of NetworkManager 0.9.10, you can alternatively use - * nm_active_connection_get_dhcp4_config(), which also works with VPN - * connections. + * You can alternatively use nm_active_connection_get_dhcp4_config(), which also + * works with VPN connections. * - * Returns: (transfer none): the #NMDhcp4Config or %NULL if the device is not activated or not - * using DHCP. + * Returns: (transfer none): the IPv4 #NMDhcpConfig, or %NULL if the device is + * not activated or not using DHCP. **/ -NMDhcp4Config * +NMDhcpConfig * nm_device_get_dhcp4_config (NMDevice *device) { g_return_val_if_fail (NM_IS_DEVICE (device), NULL); @@ -1166,16 +1165,15 @@ nm_device_get_ip6_config (NMDevice *device) * nm_device_get_dhcp6_config: * @device: a #NMDevice * - * Gets the current #NMDhcp6Config associated with the #NMDevice. + * Gets the current IPv6 #NMDhcpConfig associated with the #NMDevice. * - * Note that as of NetworkManager 0.9.10, you can alternatively use - * nm_active_connection_get_dhcp6_config(), which also works with VPN - * connections. + * You can alternatively use nm_active_connection_get_dhcp6_config(), which also + * works with VPN connections. * - * Returns: (transfer none): the #NMDhcp6Config or %NULL if the device is not activated or not - * using DHCP. + * Returns: (transfer none): the IPv6 #NMDhcpConfig, or %NULL if the device is + * not activated or not using DHCPv6. **/ -NMDhcp6Config * +NMDhcpConfig * nm_device_get_dhcp6_config (NMDevice *device) { g_return_val_if_fail (NM_IS_DEVICE (device), NULL); diff --git a/libnm/nm-device.h b/libnm/nm-device.h index 2b103e5486..5cc3735695 100644 --- a/libnm/nm-device.h +++ b/libnm/nm-device.h @@ -105,9 +105,9 @@ gboolean nm_device_get_autoconnect (NMDevice *device); void nm_device_set_autoconnect (NMDevice *device, gboolean autoconnect); gboolean nm_device_get_firmware_missing (NMDevice *device); NMIPConfig * nm_device_get_ip4_config (NMDevice *device); -NMDhcp4Config * nm_device_get_dhcp4_config (NMDevice *device); +NMDhcpConfig * nm_device_get_dhcp4_config (NMDevice *device); NMIPConfig * nm_device_get_ip6_config (NMDevice *device); -NMDhcp6Config * nm_device_get_dhcp6_config (NMDevice *device); +NMDhcpConfig * nm_device_get_dhcp6_config (NMDevice *device); NMDeviceState nm_device_get_state (NMDevice *device); NMDeviceStateReason nm_device_get_state_reason (NMDevice *device); NMActiveConnection * nm_device_get_active_connection(NMDevice *device); diff --git a/libnm/nm-dhcp-config.c b/libnm/nm-dhcp-config.c new file mode 100644 index 0000000000..5b72fbc10f --- /dev/null +++ b/libnm/nm-dhcp-config.c @@ -0,0 +1,217 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2008 - 2014 Red Hat, Inc. + * Copyright 2008 Novell, Inc. + */ + +#include + +#include "nm-dhcp-config.h" +#include "nm-dhcp4-config.h" +#include "nm-dhcp6-config.h" +#include "nm-dbus-interface.h" +#include "nm-object-private.h" +#include "nm-utils.h" + +G_DEFINE_ABSTRACT_TYPE (NMDhcpConfig, nm_dhcp_config, NM_TYPE_OBJECT) + +#define NM_DHCP_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_CONFIG, NMDhcpConfigPrivate)) + +typedef struct { + GHashTable *options; +} NMDhcpConfigPrivate; + +enum { + PROP_0, + PROP_FAMILY, + PROP_OPTIONS, + + LAST_PROP +}; + +static void +nm_dhcp_config_init (NMDhcpConfig *config) +{ + NMDhcpConfigPrivate *priv = NM_DHCP_CONFIG_GET_PRIVATE (config); + + priv->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); +} + +static gboolean +demarshal_dhcp_options (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) +{ + NMDhcpConfigPrivate *priv = NM_DHCP_CONFIG_GET_PRIVATE (object); + GVariantIter iter; + const char *key; + GVariant *opt; + + g_hash_table_remove_all (priv->options); + + g_variant_iter_init (&iter, value); + while (g_variant_iter_next (&iter, "{&sv}", &key, &opt)) { + g_hash_table_insert (priv->options, g_strdup (key), g_variant_dup_string (opt, NULL)); + g_variant_unref (opt); + } + + _nm_object_queue_notify (object, NM_DHCP_CONFIG_OPTIONS); + return TRUE; +} + +static void +init_dbus (NMObject *object) +{ + NMDhcpConfigPrivate *priv = NM_DHCP_CONFIG_GET_PRIVATE (object); + const NMPropertiesInfo property_info[] = { + { NM_DHCP_CONFIG_OPTIONS, &priv->options, demarshal_dhcp_options }, + { NULL }, + }; + + NM_OBJECT_CLASS (nm_dhcp_config_parent_class)->init_dbus (object); + + _nm_object_register_properties (object, + (NM_IS_DHCP4_CONFIG (object) ? + NM_DBUS_INTERFACE_DHCP4_CONFIG : + NM_DBUS_INTERFACE_DHCP6_CONFIG), + property_info); +} + +static void +finalize (GObject *object) +{ + NMDhcpConfigPrivate *priv = NM_DHCP_CONFIG_GET_PRIVATE (object); + + if (priv->options) + g_hash_table_destroy (priv->options); + + G_OBJECT_CLASS (nm_dhcp_config_parent_class)->finalize (object); +} + +static void +get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + NMDhcpConfig *self = NM_DHCP_CONFIG (object); + + switch (prop_id) { + case PROP_FAMILY: + g_value_set_int (value, nm_dhcp_config_get_family (self)); + case PROP_OPTIONS: + g_value_set_boxed (value, nm_dhcp_config_get_options (self)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +nm_dhcp_config_class_init (NMDhcpConfigClass *config_class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (config_class); + NMObjectClass *nm_object_class = NM_OBJECT_CLASS (config_class); + + g_type_class_add_private (config_class, sizeof (NMDhcpConfigPrivate)); + + /* virtual methods */ + object_class->get_property = get_property; + object_class->finalize = finalize; + + nm_object_class->init_dbus = init_dbus; + + /* properties */ + + /** + * NMDhcpConfig:family: + * + * The IP address family of the configuration; either %AF_INET or %AF_INET6. + **/ + g_object_class_install_property + (object_class, PROP_FAMILY, + g_param_spec_int (NM_DHCP_CONFIG_FAMILY, "", "", + 0, 255, AF_UNSPEC, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMDhcpConfig:options: + * + * The #GHashTable containing options of the configuration. + * + * Type: GLib.HashTable(utf8,utf8) + **/ + g_object_class_install_property + (object_class, PROP_OPTIONS, + g_param_spec_boxed (NM_DHCP_CONFIG_OPTIONS, "", "", + G_TYPE_HASH_TABLE, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); +} + +/** + * nm_dhcp_config_get_family: + * @config: a #NMDhcpConfig + * + * Gets the IP address family of the configuration + * + * Returns: the IP address family; either %AF_INET or %AF_INET6 + **/ +int +nm_dhcp_config_get_family (NMDhcpConfig *config) +{ + g_return_val_if_fail (NM_IS_DHCP_CONFIG (config), AF_UNSPEC); + + return NM_IS_DHCP4_CONFIG (config) ? AF_INET : AF_INET6; +} + +/** + * nm_dhcp_config_get_options: + * @config: a #NMDhcpConfig + * + * Gets all the options contained in the configuration. + * + * Returns: (transfer none) (element-type utf8 utf8): the #GHashTable containing + * strings for keys and values. This is the internal copy used by the + * configuration, and must not be modified. + **/ +GHashTable * +nm_dhcp_config_get_options (NMDhcpConfig *config) +{ + g_return_val_if_fail (NM_IS_DHCP_CONFIG (config), NULL); + + return NM_DHCP_CONFIG_GET_PRIVATE (config)->options; +} + +/** + * nm_dhcp_config_get_one_option: + * @config: a #NMDhcpConfig + * @option: the option to retrieve + * + * Gets one option by option name. + * + * Returns: the configuration option's value. This is the internal string used by the + * configuration, and must not be modified. + **/ +const char * +nm_dhcp_config_get_one_option (NMDhcpConfig *config, const char *option) +{ + g_return_val_if_fail (NM_IS_DHCP_CONFIG (config), NULL); + + return g_hash_table_lookup (nm_dhcp_config_get_options (config), option); +} diff --git a/libnm/nm-dhcp-config.h b/libnm/nm-dhcp-config.h new file mode 100644 index 0000000000..85aa3bd2c1 --- /dev/null +++ b/libnm/nm-dhcp-config.h @@ -0,0 +1,62 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2008 Red Hat, Inc. + * Copyright 2008 Novell, Inc. + */ + +#ifndef __NM_DHCP_CONFIG_H__ +#define __NM_DHCP_CONFIG_H__ + +#if !defined (__NETWORKMANAGER_H_INSIDE__) && !defined (NETWORKMANAGER_COMPILATION) +#error "Only can be included directly." +#endif + +#include + +G_BEGIN_DECLS + +#define NM_TYPE_DHCP_CONFIG (nm_dhcp_config_get_type ()) +#define NM_DHCP_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DHCP_CONFIG, NMDhcpConfig)) +#define NM_DHCP_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DHCP_CONFIG, NMDhcpConfigClass)) +#define NM_IS_DHCP_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DHCP_CONFIG)) +#define NM_IS_DHCP_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DHCP_CONFIG)) + +struct _NMDhcpConfig { + NMObject parent; +}; + +typedef struct { + NMObjectClass parent; + + /*< private >*/ + gpointer padding[8]; +} NMDhcpConfigClass; + +#define NM_DHCP_CONFIG_FAMILY "family" +#define NM_DHCP_CONFIG_OPTIONS "options" + +GType nm_dhcp_config_get_type (void); + +int nm_dhcp_config_get_family (NMDhcpConfig *config); + +GHashTable *nm_dhcp_config_get_options (NMDhcpConfig *config); +const char *nm_dhcp_config_get_one_option (NMDhcpConfig *config, const char *option); + +G_END_DECLS + +#endif /* __NM_DHCP_CONFIG_H__ */ diff --git a/libnm/nm-dhcp4-config.c b/libnm/nm-dhcp4-config.c index 2bf84fa408..6bbd8d6b54 100644 --- a/libnm/nm-dhcp4-config.c +++ b/libnm/nm-dhcp4-config.c @@ -15,169 +15,23 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * Copyright 2008 - 2011 Red Hat, Inc. - * Copyright 2008 Novell, Inc. + * Copyright 2014 Red Hat, Inc. */ -#include - #include "nm-dhcp4-config.h" -#include "nm-dbus-interface.h" #include "nm-object-private.h" -#include "nm-utils.h" - -G_DEFINE_TYPE (NMDhcp4Config, nm_dhcp4_config, NM_TYPE_OBJECT) - -#define NM_DHCP4_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP4_CONFIG, NMDhcp4ConfigPrivate)) -typedef struct { - GHashTable *options; -} NMDhcp4ConfigPrivate; - -enum { - PROP_0, - PROP_OPTIONS, - - LAST_PROP -}; +G_DEFINE_TYPE (NMDhcp4Config, nm_dhcp4_config, NM_TYPE_DHCP_CONFIG) static void nm_dhcp4_config_init (NMDhcp4Config *config) { - NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (config); - - priv->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); -} - -static gboolean -demarshal_dhcp4_options (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) -{ - NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (object); - GVariantIter iter; - const char *key; - GVariant *opt; - - g_hash_table_remove_all (priv->options); - - g_variant_iter_init (&iter, value); - while (g_variant_iter_next (&iter, "{&sv}", &key, &opt)) { - g_hash_table_insert (priv->options, g_strdup (key), g_variant_dup_string (opt, NULL)); - g_variant_unref (opt); - } - - _nm_object_queue_notify (object, NM_DHCP4_CONFIG_OPTIONS); - return TRUE; -} - -static void -init_dbus (NMObject *object) -{ - NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (object); - const NMPropertiesInfo property_info[] = { - { NM_DHCP4_CONFIG_OPTIONS, &priv->options, demarshal_dhcp4_options }, - { NULL }, - }; - - NM_OBJECT_CLASS (nm_dhcp4_config_parent_class)->init_dbus (object); - - _nm_object_register_properties (object, - NM_DBUS_INTERFACE_DHCP4_CONFIG, - property_info); -} - -static void -finalize (GObject *object) -{ - NMDhcp4ConfigPrivate *priv = NM_DHCP4_CONFIG_GET_PRIVATE (object); - - if (priv->options) - g_hash_table_destroy (priv->options); - - G_OBJECT_CLASS (nm_dhcp4_config_parent_class)->finalize (object); -} - -static void -get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - NMDhcp4Config *self = NM_DHCP4_CONFIG (object); - - switch (prop_id) { - case PROP_OPTIONS: - g_value_set_boxed (value, nm_dhcp4_config_get_options (self)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } } static void nm_dhcp4_config_class_init (NMDhcp4ConfigClass *config_class) { - GObjectClass *object_class = G_OBJECT_CLASS (config_class); NMObjectClass *nm_object_class = NM_OBJECT_CLASS (config_class); - g_type_class_add_private (config_class, sizeof (NMDhcp4ConfigPrivate)); - _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_DHCP4_CONFIG); - - /* virtual methods */ - object_class->get_property = get_property; - object_class->finalize = finalize; - - nm_object_class->init_dbus = init_dbus; - - /* properties */ - - /** - * NMDhcp4Config:options: - * - * The #GHashTable containing options of the configuration. - * - * Type: GLib.HashTable(utf8,utf8) - **/ - g_object_class_install_property - (object_class, PROP_OPTIONS, - g_param_spec_boxed (NM_DHCP4_CONFIG_OPTIONS, "", "", - G_TYPE_HASH_TABLE, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); -} - -/** - * nm_dhcp4_config_get_options: - * @config: a #NMDhcp4Config - * - * Gets all the options contained in the configuration. - * - * Returns: (transfer none) (element-type utf8 GObject.Value): the #GHashTable containing strings for keys and values. - * This is the internal copy used by the configuration, and must not be modified. - **/ -GHashTable * -nm_dhcp4_config_get_options (NMDhcp4Config *config) -{ - g_return_val_if_fail (NM_IS_DHCP4_CONFIG (config), NULL); - - return NM_DHCP4_CONFIG_GET_PRIVATE (config)->options; -} - -/** - * nm_dhcp4_config_get_one_option: - * @config: a #NMDhcp4Config - * @option: the option to retrieve - * - * Gets one option by option name. - * - * Returns: the configuration option's value. This is the internal string used by the - * configuration, and must not be modified. - **/ -const char * -nm_dhcp4_config_get_one_option (NMDhcp4Config *config, const char *option) -{ - g_return_val_if_fail (NM_IS_DHCP4_CONFIG (config), NULL); - - return g_hash_table_lookup (nm_dhcp4_config_get_options (config), option); } diff --git a/libnm/nm-dhcp4-config.h b/libnm/nm-dhcp4-config.h index 90152c8f63..3423740db8 100644 --- a/libnm/nm-dhcp4-config.h +++ b/libnm/nm-dhcp4-config.h @@ -15,18 +15,13 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * Copyright 2008 Red Hat, Inc. - * Copyright 2008 Novell, Inc. + * Copyright 2014 Red Hat, Inc. */ #ifndef __NM_DHCP4_CONFIG_H__ #define __NM_DHCP4_CONFIG_H__ -#if !defined (__NETWORKMANAGER_H_INSIDE__) && !defined (NETWORKMANAGER_COMPILATION) -#error "Only can be included directly." -#endif - -#include +#include G_BEGIN_DECLS @@ -36,25 +31,19 @@ G_BEGIN_DECLS #define NM_IS_DHCP4_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DHCP4_CONFIG)) #define NM_IS_DHCP4_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DHCP4_CONFIG)) -struct _NMDhcp4Config { - NMObject parent; -}; +typedef struct { + NMDhcpConfig parent; +} NMDhcp4Config; typedef struct { - NMObjectClass parent; + NMDhcpConfigClass parent; /*< private >*/ gpointer padding[4]; } NMDhcp4ConfigClass; -#define NM_DHCP4_CONFIG_OPTIONS "options" - GType nm_dhcp4_config_get_type (void); -GHashTable * nm_dhcp4_config_get_options (NMDhcp4Config *config); - -const char * nm_dhcp4_config_get_one_option (NMDhcp4Config *config, const char *option); - G_END_DECLS #endif /* __NM_DHCP4_CONFIG_H__ */ diff --git a/libnm/nm-dhcp6-config.c b/libnm/nm-dhcp6-config.c index 498f1d3b4c..c332dcaa8a 100644 --- a/libnm/nm-dhcp6-config.c +++ b/libnm/nm-dhcp6-config.c @@ -15,169 +15,23 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * Copyright 2008 - 2011 Red Hat, Inc. - * Copyright 2008 Novell, Inc. + * Copyright 2014 Red Hat, Inc. */ -#include - #include "nm-dhcp6-config.h" -#include "nm-dbus-interface.h" #include "nm-object-private.h" -#include "nm-utils.h" G_DEFINE_TYPE (NMDhcp6Config, nm_dhcp6_config, NM_TYPE_OBJECT) -#define NM_DHCP6_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP6_CONFIG, NMDhcp6ConfigPrivate)) - -typedef struct { - GHashTable *options; -} NMDhcp6ConfigPrivate; - -enum { - PROP_0, - PROP_OPTIONS, - - LAST_PROP -}; - static void nm_dhcp6_config_init (NMDhcp6Config *config) { - NMDhcp6ConfigPrivate *priv = NM_DHCP6_CONFIG_GET_PRIVATE (config); - - priv->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); -} - -static gboolean -demarshal_dhcp6_options (NMObject *object, GParamSpec *pspec, GVariant *value, gpointer field) -{ - NMDhcp6ConfigPrivate *priv = NM_DHCP6_CONFIG_GET_PRIVATE (object); - GVariantIter iter; - const char *key; - GVariant *opt; - - g_hash_table_remove_all (priv->options); - - g_variant_iter_init (&iter, value); - while (g_variant_iter_next (&iter, "{&sv}", &key, &opt)) { - g_hash_table_insert (priv->options, g_strdup (key), g_variant_dup_string (opt, NULL)); - g_variant_unref (opt); - } - - _nm_object_queue_notify (object, NM_DHCP6_CONFIG_OPTIONS); - return TRUE; -} - -static void -init_dbus (NMObject *object) -{ - NMDhcp6ConfigPrivate *priv = NM_DHCP6_CONFIG_GET_PRIVATE (object); - const NMPropertiesInfo property_info[] = { - { NM_DHCP6_CONFIG_OPTIONS, &priv->options, demarshal_dhcp6_options }, - { NULL }, - }; - - NM_OBJECT_CLASS (nm_dhcp6_config_parent_class)->init_dbus (object); - - _nm_object_register_properties (object, - NM_DBUS_INTERFACE_DHCP6_CONFIG, - property_info); -} - -static void -finalize (GObject *object) -{ - NMDhcp6ConfigPrivate *priv = NM_DHCP6_CONFIG_GET_PRIVATE (object); - - if (priv->options) - g_hash_table_destroy (priv->options); - - G_OBJECT_CLASS (nm_dhcp6_config_parent_class)->finalize (object); -} - -static void -get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - NMDhcp6Config *self = NM_DHCP6_CONFIG (object); - - switch (prop_id) { - case PROP_OPTIONS: - g_value_set_boxed (value, nm_dhcp6_config_get_options (self)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } } static void nm_dhcp6_config_class_init (NMDhcp6ConfigClass *config_class) { - GObjectClass *object_class = G_OBJECT_CLASS (config_class); NMObjectClass *nm_object_class = NM_OBJECT_CLASS (config_class); - g_type_class_add_private (config_class, sizeof (NMDhcp6ConfigPrivate)); - _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_DHCP6_CONFIG); - - /* virtual methods */ - object_class->get_property = get_property; - object_class->finalize = finalize; - - nm_object_class->init_dbus = init_dbus; - - /* properties */ - - /** - * NMDhcp6Config:options: - * - * The #GHashTable containing options of the configuration. - * - * Type: GLib.HashTable(utf8,utf8) - **/ - g_object_class_install_property - (object_class, PROP_OPTIONS, - g_param_spec_boxed (NM_DHCP6_CONFIG_OPTIONS, "", "", - G_TYPE_HASH_TABLE, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); -} - -/** - * nm_dhcp6_config_get_options: - * @config: a #NMDhcp6Config - * - * Gets all the options contained in the configuration. - * - * Returns: (transfer none) (element-type utf8 GObject.Value): the #GHashTable containing strings for keys and values. - * This is the internal copy used by the configuration, and must not be modified. - **/ -GHashTable * -nm_dhcp6_config_get_options (NMDhcp6Config *config) -{ - g_return_val_if_fail (NM_IS_DHCP6_CONFIG (config), NULL); - - return NM_DHCP6_CONFIG_GET_PRIVATE (config)->options; -} - -/** - * nm_dhcp6_config_get_one_option: - * @config: a #NMDhcp6Config - * @option: the option to retrieve - * - * Gets one option by option name. - * - * Returns: the configuration option's value. This is the internal string used by the - * configuration, and must not be modified. - **/ -const char * -nm_dhcp6_config_get_one_option (NMDhcp6Config *config, const char *option) -{ - g_return_val_if_fail (NM_IS_DHCP6_CONFIG (config), NULL); - - return g_hash_table_lookup (nm_dhcp6_config_get_options (config), option); } diff --git a/libnm/nm-dhcp6-config.h b/libnm/nm-dhcp6-config.h index 53bd45eeb7..abb24adfd6 100644 --- a/libnm/nm-dhcp6-config.h +++ b/libnm/nm-dhcp6-config.h @@ -15,18 +15,13 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * Copyright 2008 - 2010 Red Hat, Inc. - * Copyright 2008 Novell, Inc. + * Copyright 2014 Red Hat, Inc. */ #ifndef __NM_DHCP6_CONFIG_H__ #define __NM_DHCP6_CONFIG_H__ -#if !defined (__NETWORKMANAGER_H_INSIDE__) && !defined (NETWORKMANAGER_COMPILATION) -#error "Only can be included directly." -#endif - -#include +#include G_BEGIN_DECLS @@ -36,25 +31,19 @@ G_BEGIN_DECLS #define NM_IS_DHCP6_CONFIG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DHCP6_CONFIG)) #define NM_IS_DHCP6_CONFIG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DHCP6_CONFIG)) -struct _NMDhcp6Config { - NMObject parent; -}; +typedef struct { + NMDhcpConfig parent; +} NMDhcp6Config; typedef struct { - NMObjectClass parent; + NMDhcpConfigClass parent; /*< private >*/ gpointer padding[4]; } NMDhcp6ConfigClass; -#define NM_DHCP6_CONFIG_OPTIONS "options" - GType nm_dhcp6_config_get_type (void); -GHashTable * nm_dhcp6_config_get_options (NMDhcp6Config *config); - -const char * nm_dhcp6_config_get_one_option (NMDhcp6Config *config, const char *option); - G_END_DECLS #endif /* __NM_DHCP6_CONFIG_H__ */ diff --git a/libnm/nm-types.h b/libnm/nm-types.h index eb922036d0..3602878a6a 100644 --- a/libnm/nm-types.h +++ b/libnm/nm-types.h @@ -43,8 +43,7 @@ typedef struct _NMDeviceTeam NMDeviceTeam; typedef struct _NMDeviceVlan NMDeviceVlan; typedef struct _NMDeviceWifi NMDeviceWifi; typedef struct _NMDeviceWimax NMDeviceWimax; -typedef struct _NMDhcp4Config NMDhcp4Config; -typedef struct _NMDhcp6Config NMDhcp6Config; +typedef struct _NMDhcpConfig NMDhcpConfig; typedef struct _NMIPConfig NMIPConfig; typedef struct _NMObject NMObject; typedef struct _NMRemoteConnection NMRemoteConnection; -- cgit v1.2.1 From ff608c24cd1ac409092f1a883452225a8be6513a Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 28 Oct 2014 12:49:56 -0400 Subject: libnm-core: don't serialize empty address-labels If no address in an NMSettingIP4Config has a label, then don't bother serializing an array of empty strings. --- libnm-core/nm-setting-ip4-config.c | 15 ++++++++++++++- libnm-core/tests/test-general.c | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/libnm-core/nm-setting-ip4-config.c b/libnm-core/nm-setting-ip4-config.c index 9eab49d7f5..6d897b56de 100644 --- a/libnm-core/nm-setting-ip4-config.c +++ b/libnm-core/nm-setting-ip4-config.c @@ -304,16 +304,29 @@ ip4_address_labels_get (NMSetting *setting, const char *property) { NMSettingIPConfig *s_ip = NM_SETTING_IP_CONFIG (setting); + gboolean have_labels = FALSE; GPtrArray *labels; GVariant *ret; int num_addrs, i; - labels = g_ptr_array_new (); num_addrs = nm_setting_ip_config_get_num_addresses (s_ip); for (i = 0; i < num_addrs; i++) { NMIPAddress *addr = nm_setting_ip_config_get_address (s_ip, i); GVariant *label = nm_ip_address_get_attribute (addr, "label"); + if (label) { + have_labels = TRUE; + break; + } + } + if (!have_labels) + return NULL; + + labels = g_ptr_array_sized_new (num_addrs); + for (i = 0; i < num_addrs; i++) { + NMIPAddress *addr = nm_setting_ip_config_get_address (s_ip, i); + GVariant *label = nm_ip_address_get_attribute (addr, "label"); + g_ptr_array_add (labels, (char *) (label ? g_variant_get_string (label, NULL) : "")); } diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 1bab4e011c..09e05856c8 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -350,6 +350,25 @@ test_setting_ip4_config_labels (void) label = nm_ip_address_get_attribute (addr, "label"); g_assert (label == NULL); + /* The 'address-labels' property should be omitted from the serialization if + * there are no non-NULL labels. + */ + conn = nmtst_create_minimal_connection ("label test", NULL, NM_SETTING_WIRED_SETTING_NAME, NULL); + nm_connection_add_setting (conn, nm_setting_duplicate (NM_SETTING (s_ip4))); + dict = nm_connection_to_dbus (conn, NM_CONNECTION_SERIALIZE_ALL); + g_object_unref (conn); + + setting_dict = g_variant_lookup_value (dict, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); + g_assert (setting_dict != NULL); + + value = g_variant_lookup_value (setting_dict, "address-labels", NULL); + g_assert (value == NULL); + + g_variant_unref (setting_dict); + g_variant_unref (dict); + + /* Now back to constructing the original s_ip4... */ + /* addr 2 */ addr = nm_ip_address_new (AF_INET, "2.2.2.2", 24, &error); g_assert_no_error (error); -- cgit v1.2.1 From e374923bbe4a9f608644756f749b9bae9aa5f349 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 4 Nov 2014 15:48:48 -0500 Subject: all: allow route metrics to be "0" Change NMIPRoute to use "-1" for "default", so that "0" is a valid metric. Update everything for that. --- callouts/nm-dispatcher-utils.c | 8 ++--- clients/cli/common.c | 12 ++++--- clients/cli/settings.c | 12 +++---- clients/tui/nm-editor-bindings.c | 9 +++-- libnm-core/nm-setting-ip-config.c | 41 +++++++++++++++++----- libnm-core/nm-setting-ip-config.h | 8 ++--- libnm-core/nm-utils.c | 30 ++++++++++------ src/nm-ip4-config.c | 11 +++--- src/nm-ip6-config.c | 11 +++--- src/settings/plugins/ifcfg-rh/reader.c | 12 +++---- .../plugins/ifcfg-rh/tests/test-ifcfg-rh.c | 4 +-- src/settings/plugins/ifcfg-rh/writer.c | 33 +++++++++++------ src/settings/plugins/ifnet/connection_parser.c | 21 ++++++----- src/settings/plugins/keyfile/reader.c | 4 ++- src/settings/plugins/keyfile/tests/test-keyfile.c | 34 +++++++++--------- src/settings/plugins/keyfile/writer.c | 2 +- 16 files changed, 153 insertions(+), 99 deletions(-) diff --git a/callouts/nm-dispatcher-utils.c b/callouts/nm-dispatcher-utils.c index 8feec72d25..8d7274329c 100644 --- a/callouts/nm-dispatcher-utils.c +++ b/callouts/nm-dispatcher-utils.c @@ -185,11 +185,11 @@ construct_ip4_items (GSList *items, GVariant *ip4_config, const char *prefix) if (!next_hop) next_hop = "0.0.0.0"; - routetmp = g_strdup_printf ("%sIP4_ROUTE_%d=%s/%d %s %d", prefix, i, + routetmp = g_strdup_printf ("%sIP4_ROUTE_%d=%s/%d %s %u", prefix, i, nm_ip_route_get_dest (route), nm_ip_route_get_prefix (route), next_hop, - nm_ip_route_get_metric (route)); + (guint32) MAX (0, nm_ip_route_get_metric (route))); items = g_slist_prepend (items, routetmp); } items = g_slist_prepend (items, g_strdup_printf ("%sIP4_NUM_ROUTES=%d", prefix, routes->len)); @@ -299,11 +299,11 @@ construct_ip6_items (GSList *items, GVariant *ip6_config, const char *prefix) if (!next_hop) next_hop = "::"; - routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %d", prefix, i, + routetmp = g_strdup_printf ("%sIP6_ROUTE_%d=%s/%d %s %u", prefix, i, nm_ip_route_get_dest (route), nm_ip_route_get_prefix (route), next_hop, - nm_ip_route_get_metric (route)); + (guint32) MAX (0, nm_ip_route_get_metric (route))); items = g_slist_prepend (items, routetmp); } if (routes->len) diff --git a/clients/cli/common.c b/clients/cli/common.c index 2003934993..8539af28d8 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -130,11 +130,12 @@ print_ip4_config (NMIPConfig *cfg4, if (!next_hop) next_hop = "0.0.0.0"; - route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s, mt = %u", + route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s%c mt = %u", nm_ip_route_get_dest (route), nm_ip_route_get_prefix (route), next_hop, - nm_ip_route_get_metric (route)); + nm_ip_route_get_metric (route) == -1 ? '\0' : ',', + (guint32) nm_ip_route_get_metric (route)); } route_arr[i] = NULL; } @@ -219,11 +220,12 @@ print_ip6_config (NMIPConfig *cfg6, if (!next_hop) next_hop = "::"; - route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s, mt = %u", + route_arr[i] = g_strdup_printf ("dst = %s/%u, nh = %s%c mt = %u", nm_ip_route_get_dest (route), nm_ip_route_get_prefix (route), next_hop, - nm_ip_route_get_metric (route)); + nm_ip_route_get_metric (route) == -1 ? '\0' : ',', + (guint32) nm_ip_route_get_metric (route)); } route_arr[i] = NULL; } @@ -417,7 +419,7 @@ nmc_parse_and_build_route (int family, char *dest = NULL, *plen = NULL; const char *next_hop = NULL; const char *canon_dest; - long int prefix = max_prefix, metric = 0; + long int prefix = max_prefix, metric = -1; NMIPRoute *route = NULL; gboolean success = FALSE; GError *local = NULL; diff --git a/clients/cli/settings.c b/clients/cli/settings.c index 292a09b52a..e4d7589385 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -1263,8 +1263,8 @@ nmc_property_ipv4_get_routes (NMSetting *setting) nm_ip_route_get_next_hop (route)); } - if (nm_ip_route_get_metric (route)) - g_string_append_printf (printable, ", mt = %u", nm_ip_route_get_metric (route)); + if (nm_ip_route_get_metric (route) != -1) + g_string_append_printf (printable, ", mt = %u", (guint32) nm_ip_route_get_metric (route)); g_string_append (printable, " }"); } @@ -1314,8 +1314,8 @@ nmc_property_ipv6_get_routes (NMSetting *setting) nm_ip_route_get_next_hop (route)); } - if (nm_ip_route_get_metric (route)) - g_string_append_printf (printable, ", mt = %u", nm_ip_route_get_metric (route)); + if (nm_ip_route_get_metric (route) != -1) + g_string_append_printf (printable, ", mt = %u", (guint32) nm_ip_route_get_metric (route)); g_string_append (printable, " }"); } @@ -3332,7 +3332,7 @@ nmc_property_ipv4_describe_routes (NMSetting *setting, const char *prop) " ip[/prefix] [next-hop] [metric],...\n\n" "Missing prefix is regarded as a prefix of 32.\n" "Missing next-hop is regarded as 0.0.0.0.\n" - "Missing metric or 0 means a default metric (NM/kernel will set a default value).\n\n" + "Missing metric means default (NM/kernel will set a default value).\n\n" "Examples: 192.168.2.0/24 192.168.2.1 3, 10.1.0.0/16 10.0.0.254\n" " 10.1.2.0/24\n"); } @@ -3638,7 +3638,7 @@ nmc_property_ipv6_describe_routes (NMSetting *setting, const char *prop) " ip[/prefix] [next-hop] [metric],...\n\n" "Missing prefix is regarded as a prefix of 128.\n" "Missing next-hop is regarded as \"::\".\n" - "Missing metric or 0 means a default metric (NM/kernel will set a default value).\n\n" + "Missing metric means default (NM/kernel will set a default value).\n\n" "Examples: 2001:db8:beef:2::/64 2001:db8:beef::2, 2001:db8:beef:3::/64 2001:db8:beef::3 2\n" " abbe::/64 55\n"); } diff --git a/clients/tui/nm-editor-bindings.c b/clients/tui/nm-editor-bindings.c index 9db44c9d80..5560f83ca6 100644 --- a/clients/tui/nm-editor-bindings.c +++ b/clients/tui/nm-editor-bindings.c @@ -327,7 +327,7 @@ ip_route_transform_to_metric_string (GBinding *binding, char *string; route = g_value_get_boxed (source_value); - if (route && nm_ip_route_get_dest (route)) { + if (route && nm_ip_route_get_dest (route) && nm_ip_route_get_metric (route) != -1) { string = g_strdup_printf ("%lu", (gulong) nm_ip_route_get_metric (route)); g_value_take_string (target_value, string); } else @@ -400,10 +400,13 @@ ip_route_transform_from_metric_string (GBinding *binding, { NMIPRoute *route; const char *text; - guint32 metric; + gint64 metric; text = g_value_get_string (source_value); - metric = strtoul (text, NULL, 10); + if (*text) + metric = strtoul (text, NULL, 10); + else + metric = -1; /* Fetch the original property value */ g_object_get (g_binding_get_source (binding), diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c index 30e7b267c8..8d0504e2b1 100644 --- a/libnm-core/nm-setting-ip-config.c +++ b/libnm-core/nm-setting-ip-config.c @@ -96,6 +96,24 @@ valid_prefix (int family, guint prefix, GError **error) return TRUE; } +static gboolean +valid_metric (gint64 metric, GError **error) +{ + if (metric < -1 || metric > G_MAXUINT32) { + if (error) { + char buf[64]; + + /* We can't concatenate G_GINT64_FORMAT into a translatable string */ + g_snprintf (buf, sizeof (buf), "%" G_GINT64_FORMAT, metric); + g_set_error (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_FAILED, + _("Invalid routing metric '%s'"), buf); + } + return FALSE; + } + + return TRUE; +} + G_DEFINE_BOXED_TYPE (NMIPAddress, nm_ip_address, nm_ip_address_dup, nm_ip_address_unref) @@ -495,7 +513,7 @@ struct NMIPRoute { char *dest; guint prefix; char *next_hop; - guint32 metric; + gint64 metric; GHashTable *attributes; }; @@ -506,7 +524,7 @@ struct NMIPRoute { * @dest: the IP address of the route's destination * @prefix: the address prefix length * @next_hop: (allow-none): the IP address of the next hop (or %NULL) - * @metric: the route metric (or 0 for "default") + * @metric: the route metric (or -1 for "default") * @error: location to store error, or %NULL * * Creates a new #NMIPRoute object. @@ -518,7 +536,7 @@ nm_ip_route_new (int family, const char *dest, guint prefix, const char *next_hop, - guint metric, + gint64 metric, GError **error) { NMIPRoute *route; @@ -531,6 +549,8 @@ nm_ip_route_new (int family, return NULL; if (next_hop && !valid_ip (family, next_hop, error)) return NULL; + if (!valid_metric (metric, error)) + return NULL; route = g_slice_new0 (NMIPRoute); route->refcount = 1; @@ -550,7 +570,7 @@ nm_ip_route_new (int family, * @dest: the IP address of the route's destination * @prefix: the address prefix length * @next_hop: (allow-none): the IP address of the next hop (or %NULL) - * @metric: the route metric (or 0 for "default") + * @metric: the route metric (or -1 for "default") * @error: location to store error, or %NULL * * Creates a new #NMIPRoute object. @dest and @next_hop (if non-%NULL) must @@ -563,7 +583,7 @@ nm_ip_route_new_binary (int family, gconstpointer dest, guint prefix, gconstpointer next_hop, - guint metric, + gint64 metric, GError **error) { NMIPRoute *route; @@ -573,6 +593,8 @@ nm_ip_route_new_binary (int family, if (!valid_prefix (family, prefix, error)) return NULL; + if (!valid_metric (metric, error)) + return NULL; route = g_slice_new0 (NMIPRoute); route->refcount = 1; @@ -916,12 +938,12 @@ nm_ip_route_set_next_hop_binary (NMIPRoute *route, * @route: the #NMIPRoute * * Gets the route metric property of this route object; lower values - * indicate "better" or more preferred routes; 0 indicates "default" + * indicate "better" or more preferred routes; -1 indicates "default" * (meaning NetworkManager will set it appropriately). * * Returns: the route metric **/ -guint32 +gint64 nm_ip_route_get_metric (NMIPRoute *route) { g_return_val_if_fail (route != NULL, 0); @@ -933,15 +955,16 @@ nm_ip_route_get_metric (NMIPRoute *route) /** * nm_ip_route_set_metric: * @route: the #NMIPRoute - * @metric: the route metric + * @metric: the route metric (or -1 for "default") * * Sets the metric property of this route object. **/ void nm_ip_route_set_metric (NMIPRoute *route, - guint32 metric) + gint64 metric) { g_return_if_fail (route != NULL); + g_return_if_fail (valid_metric (metric, NULL)); route->metric = metric; } diff --git a/libnm-core/nm-setting-ip-config.h b/libnm-core/nm-setting-ip-config.h index 37437761c4..fba226c331 100644 --- a/libnm-core/nm-setting-ip-config.h +++ b/libnm-core/nm-setting-ip-config.h @@ -78,13 +78,13 @@ NMIPRoute *nm_ip_route_new (int family, const char *dest, guint prefix, const char *next_hop, - guint metric, + gint64 metric, GError **error); NMIPRoute *nm_ip_route_new_binary (int family, gconstpointer dest, guint prefix, gconstpointer next_hop, - guint metric, + gint64 metric, GError **error); void nm_ip_route_ref (NMIPRoute *route); @@ -111,9 +111,9 @@ gboolean nm_ip_route_get_next_hop_binary (NMIPRoute *route, gpointer next_hop); void nm_ip_route_set_next_hop_binary (NMIPRoute *route, gconstpointer next_hop); -guint32 nm_ip_route_get_metric (NMIPRoute *route); +gint64 nm_ip_route_get_metric (NMIPRoute *route); void nm_ip_route_set_metric (NMIPRoute *route, - guint32 metric); + gint64 metric); char **nm_ip_route_get_attribute_names (NMIPRoute *route); GVariant *nm_ip_route_get_attribute (NMIPRoute *route, diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index f8105da810..5356399baa 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -1269,7 +1269,8 @@ nm_utils_ip4_routes_to_variant (GPtrArray *routes) nm_ip_route_get_dest_binary (route, &array[0]); array[1] = nm_ip_route_get_prefix (route); nm_ip_route_get_next_hop_binary (route, &array[2]); - array[3] = nm_ip_route_get_metric (route); + /* The old routes format uses "0" for default, not "-1" */ + array[3] = MAX (0, nm_ip_route_get_metric (route)); g_variant_builder_add (&builder, "@au", g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, @@ -1317,8 +1318,11 @@ nm_utils_ip4_routes_from_variant (GVariant *value) } route = nm_ip_route_new_binary (AF_INET, - &route_array[0], route_array[1], - &route_array[2], route_array[3], + &route_array[0], + route_array[1], + &route_array[2], + /* The old routes format uses "0" for default, not "-1" */ + route_array[3] ? (gint64) route_array[3] : -1, &error); if (route) g_ptr_array_add (routes, route); @@ -1636,7 +1640,8 @@ nm_utils_ip6_routes_to_variant (GPtrArray *routes) prefix = nm_ip_route_get_prefix (route); nm_ip_route_get_next_hop_binary (route, &next_hop_bytes); next_hop = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, &next_hop_bytes, 16, 1); - metric = nm_ip_route_get_metric (route); + /* The old routes format uses "0" for default, not "-1" */ + metric = MAX (0, nm_ip_route_get_metric (route)); g_variant_builder_add (&builder, "(@ayu@ayu)", dest, prefix, next_hop, metric); } @@ -1695,7 +1700,9 @@ nm_utils_ip6_routes_from_variant (GVariant *value) goto next; } - route = nm_ip_route_new_binary (AF_INET6, dest, prefix, next_hop, metric, &error); + route = nm_ip_route_new_binary (AF_INET6, dest, prefix, next_hop, + metric ? (gint64) metric : -1, + &error); if (route) g_ptr_array_add (routes, route); else { @@ -1861,10 +1868,10 @@ nm_utils_ip_routes_to_variant (GPtrArray *routes) "next-hop", g_variant_new_string (nm_ip_route_get_next_hop (route))); } - if (nm_ip_route_get_metric (route)) { + if (nm_ip_route_get_metric (route) != -1) { g_variant_builder_add (&route_builder, "{sv}", "metric", - g_variant_new_uint32 (nm_ip_route_get_metric (route))); + g_variant_new_uint32 ((guint32) nm_ip_route_get_metric (route))); } names = nm_ip_route_get_attribute_names (route); @@ -1903,7 +1910,8 @@ nm_utils_ip_routes_from_variant (GVariant *value, GVariantIter iter, attrs_iter; GVariant *route_var; const char *dest, *next_hop; - guint32 prefix, metric; + guint32 prefix, metric32; + gint64 metric; const char *attr_name; GVariant *attr_val; NMIPRoute *route; @@ -1923,8 +1931,10 @@ nm_utils_ip_routes_from_variant (GVariant *value, } if (!g_variant_lookup (route_var, "next-hop", "&s", &next_hop)) next_hop = NULL; - if (!g_variant_lookup (route_var, "metric", "u", &metric)) - metric = 0; + if (g_variant_lookup (route_var, "metric", "u", &metric32)) + metric = metric32; + else + metric = -1; route = nm_ip_route_new (family, dest, prefix, next_hop, metric, &error); if (!route) { diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 74a7556f38..604d627f52 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -178,7 +178,7 @@ nm_ip4_config_capture (int ifindex, gboolean capture_resolv_conf) NMIP4Config *config; NMIP4ConfigPrivate *priv; guint i; - guint lowest_metric = G_MAXUINT; + guint32 lowest_metric = G_MAXUINT32; guint32 old_gateway = 0; gboolean has_gateway = FALSE; @@ -361,9 +361,10 @@ nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIPConfig *setting, in nm_ip_route_get_dest_binary (s_route, &route.network); route.plen = nm_ip_route_get_prefix (s_route); nm_ip_route_get_next_hop_binary (s_route, &route.gateway); - route.metric = nm_ip_route_get_metric (s_route); - if (!route.metric) + if (nm_ip_route_get_metric (s_route) == -1) route.metric = default_route_metric; + else + route.metric = nm_ip_route_get_metric (s_route); route.source = NM_IP_CONFIG_SOURCE_USER; nm_ip4_config_add_route (config, &route); @@ -1802,8 +1803,8 @@ get_property (GObject *object, guint prop_id, g_value_array_append (array, &val); g_value_unset (&val); - g_value_init (&val, G_TYPE_UINT); - g_value_set_uint (&val, route->metric); + g_value_init (&val, G_TYPE_INT64); + g_value_set_int64 (&val, route->metric); g_value_array_append (array, &val); g_value_unset (&val); diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index 19552e0339..13db237cc6 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -287,7 +287,7 @@ nm_ip6_config_capture (int ifindex, gboolean capture_resolv_conf, NMSettingIP6Co NMIP6Config *config; NMIP6ConfigPrivate *priv; guint i; - guint lowest_metric = G_MAXUINT; + guint32 lowest_metric = G_MAXUINT32; struct in6_addr old_gateway = IN6ADDR_ANY_INIT; gboolean has_gateway = FALSE; gboolean notify_nameservers = FALSE; @@ -463,9 +463,10 @@ nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIPConfig *setting, in nm_ip_route_get_dest_binary (s_route, &route.network); route.plen = nm_ip_route_get_prefix (s_route); nm_ip_route_get_next_hop_binary (s_route, &route.gateway); - route.metric = nm_ip_route_get_metric (s_route); - if (!route.metric) + if (nm_ip_route_get_metric (s_route) == -1) route.metric = default_route_metric; + else + route.metric = nm_ip_route_get_metric (s_route); route.source = NM_IP_CONFIG_SOURCE_USER; nm_ip6_config_add_route (config, &route); @@ -1695,8 +1696,8 @@ get_property (GObject *object, guint prop_id, g_value_array_append (array, &val); g_value_unset (&val); - g_value_init (&val, G_TYPE_UINT); - g_value_set_uint (&val, route->metric); + g_value_init (&val, G_TYPE_INT64); + g_value_set_int64 (&val, route->metric); g_value_array_append (array, &val); g_value_unset (&val); diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index 9c3e9b0d2d..5243d77f18 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -443,7 +443,7 @@ read_one_ip4_route (shvarFile *ifcfg, { char *ip_tag, *netmask_tag, *gw_tag, *metric_tag, *value; char *dest = NULL, *next_hop = NULL; - long prefix, metric; + gint64 prefix, metric; gboolean success = FALSE; g_return_val_if_fail (ifcfg != NULL, FALSE); @@ -510,7 +510,7 @@ read_one_ip4_route (shvarFile *ifcfg, } g_free (value); } else - metric = 0; + metric = -1; *out_route = nm_ip_route_new (AF_INET, dest, prefix, next_hop, metric, error); if (*out_route) @@ -536,7 +536,7 @@ read_route_file_legacy (const char *filename, NMSettingIPConfig *s_ip4, GError * GMatchInfo *match_info; NMIPRoute *route = NULL; char *dest = NULL, *prefix = NULL, *next_hop = NULL, *metric = NULL; - long int prefix_int, metric_int; + gint64 prefix_int, metric_int; gboolean success = FALSE; const char *pattern_empty = "^\\s*(\\#.*)?$"; @@ -633,7 +633,7 @@ read_route_file_legacy (const char *filename, NMSettingIPConfig *s_ip4, GError * /* Metric */ g_regex_match (regex_metric, *iter, 0, &match_info); - metric_int = 0; + metric_int = -1; if (g_match_info_matches (match_info)) { metric = g_match_info_fetch (match_info, 1); errno = 0; @@ -745,7 +745,7 @@ read_route6_file (const char *filename, NMSettingIPConfig *s_ip6, GError **error GMatchInfo *match_info; NMIPRoute *route = NULL; char *dest = NULL, *prefix = NULL, *next_hop = NULL, *metric = NULL; - long int prefix_int, metric_int; + gint64 prefix_int, metric_int; gboolean success = FALSE; const char *pattern_empty = "^\\s*(\\#.*)?$"; @@ -840,7 +840,7 @@ read_route6_file (const char *filename, NMSettingIPConfig *s_ip6, GError **error /* Metric */ g_regex_match (regex_metric, *iter, 0, &match_info); - metric_int = 0; + metric_int = -1; if (g_match_info_matches (match_info)) { metric = g_match_info_fetch (match_info, 1); errno = 0; diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index 4511878642..9964254b71 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -1158,7 +1158,7 @@ test_read_wired_static_routes (void) g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "11.22.33.0"); g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 24); g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "192.168.1.5"); - g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 0); + g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, -1); ip4_route = nm_setting_ip_config_get_route (s_ip4, 1); g_assert (ip4_route); @@ -1279,7 +1279,7 @@ test_read_wired_static_routes_legacy (void) g_assert_cmpstr (nm_ip_route_get_dest (ip4_route), ==, "32.42.52.62"); g_assert_cmpint (nm_ip_route_get_prefix (ip4_route), ==, 32); g_assert_cmpstr (nm_ip_route_get_next_hop (ip4_route), ==, "8.8.8.8"); - g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, 0); + g_assert_cmpint (nm_ip_route_get_metric (ip4_route), ==, -1); /* Route #3 */ ip4_route = nm_setting_ip_config_get_route (s_ip4, 2); diff --git a/src/settings/plugins/ifcfg-rh/writer.c b/src/settings/plugins/ifcfg-rh/writer.c index 559342d4cd..6857c1f70a 100644 --- a/src/settings/plugins/ifcfg-rh/writer.c +++ b/src/settings/plugins/ifcfg-rh/writer.c @@ -1747,7 +1747,8 @@ write_route_file_legacy (const char *filename, NMSettingIPConfig *s_ip4, GError char **route_items; char *route_contents; NMIPRoute *route; - guint32 prefix, metric; + guint32 prefix; + gint64 metric; guint32 i, num; gboolean success = FALSE; @@ -1771,7 +1772,10 @@ write_route_file_legacy (const char *filename, NMSettingIPConfig *s_ip4, GError next_hop = nm_ip_route_get_next_hop (route); metric = nm_ip_route_get_metric (route); - route_items[i] = g_strdup_printf ("%s/%u via %s metric %u\n", dest, prefix, next_hop, metric); + if (metric == -1) + route_items[i] = g_strdup_printf ("%s/%u via %s\n", dest, prefix, next_hop); + else + route_items[i] = g_strdup_printf ("%s/%u via %s metric %u\n", dest, prefix, next_hop, (guint32) metric); } route_items[num] = NULL; route_contents = g_strjoinv (NULL, route_items); @@ -2033,7 +2037,8 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) for (i = 0; i < 256; i++) { char buf[INET_ADDRSTRLEN]; NMIPRoute *route; - guint32 netmask, metric; + guint32 netmask; + gint64 metric; addr_key = g_strdup_printf ("ADDRESS%d", i); netmask_key = g_strdup_printf ("NETMASK%d", i); @@ -2059,10 +2064,10 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) memset (buf, 0, sizeof (buf)); metric = nm_ip_route_get_metric (route); - if (metric == 0) + if (metric == -1) svSetValue (routefile, metric_key, NULL, FALSE); else { - tmp = g_strdup_printf ("%u", metric); + tmp = g_strdup_printf ("%u", (guint32) metric); svSetValue (routefile, metric_key, tmp, FALSE); g_free (tmp); } @@ -2204,11 +2209,19 @@ write_route6_file (const char *filename, NMSettingIPConfig *s_ip6, GError **erro route_items = g_malloc0 (sizeof (char*) * (num + 1)); for (i = 0; i < num; i++) { route = nm_setting_ip_config_get_route (s_ip6, i); - route_items[i] = g_strdup_printf ("%s/%u via %s metric %u\n", - nm_ip_route_get_dest (route), - nm_ip_route_get_prefix (route), - nm_ip_route_get_next_hop (route), - nm_ip_route_get_metric (route)); + + if (nm_ip_route_get_metric (route) == -1) { + route_items[i] = g_strdup_printf ("%s/%u via %s\n", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + nm_ip_route_get_next_hop (route)); + } else { + route_items[i] = g_strdup_printf ("%s/%u via %s metric %u\n", + nm_ip_route_get_dest (route), + nm_ip_route_get_prefix (route), + nm_ip_route_get_next_hop (route), + (guint32) nm_ip_route_get_metric (route)); + } } route_items[num] = NULL; route_contents = g_strjoinv (NULL, route_items); diff --git a/src/settings/plugins/ifnet/connection_parser.c b/src/settings/plugins/ifnet/connection_parser.c index 3880e8f274..5ffc0605e1 100644 --- a/src/settings/plugins/ifnet/connection_parser.c +++ b/src/settings/plugins/ifnet/connection_parser.c @@ -30,6 +30,7 @@ #include "nm-system-config-interface.h" #include "nm-logging.h" #include "nm-core-internal.h" +#include "NetworkManagerUtils.h" #include "net_utils.h" #include "wpa_parser.h" @@ -692,21 +693,21 @@ make_ip4_setting (NMConnection *connection, ip_block *current_iblock = iblock; const char *metric_str; char *stripped; - long int metric; + gint64 metric; NMIPRoute *route; GError *local = NULL; if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) { - metric = strtol (metric_str, NULL, 10); + metric = nm_utils_ascii_str_to_int64 (metric_str, 10, 0, G_MAXUINT32, -1); } else { metric_str = ifnet_get_global_data ("metric"); if (metric_str) { stripped = g_strdup (metric_str); strip_string (stripped, '"'); - metric = strtol (metric_str, NULL, 10); + metric = nm_utils_ascii_str_to_int64 (metric_str, 10, 0, G_MAXUINT32, -1); g_free (stripped); } else - metric = 0; + metric = -1; } route = nm_ip_route_new (AF_INET, iblock->ip, iblock->prefix, iblock->next_hop, metric, &local); @@ -830,22 +831,20 @@ make_ip6_setting (NMConnection *connection, ip_block *current_iblock = iblock; const char *metric_str; char *stripped; - long int metric; + gint64 metric; NMIPRoute *route; GError *local = NULL; /* metric is not per routes configuration right now * global metric is also supported (metric="x") */ - if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) { - metric = strtol (metric_str, NULL, 10); - nm_ip_route_set_metric (route, (guint32) metric); - } else { + if ((metric_str = ifnet_get_data (conn_name, "metric")) != NULL) + metric = nm_utils_ascii_str_to_int64 (metric_str, 10, 0, G_MAXUINT32, -1); + else { metric_str = ifnet_get_global_data ("metric"); if (metric_str) { stripped = g_strdup (metric_str); strip_string (stripped, '"'); - metric = strtol (metric_str, NULL, 10); - nm_ip_route_set_metric (route, (guint32) metric); + metric = nm_utils_ascii_str_to_int64 (metric_str, 10, 0, G_MAXUINT32, -1); g_free (stripped); } else metric = 1; diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c index 793e485993..9d65539149 100644 --- a/src/settings/plugins/keyfile/reader.c +++ b/src/settings/plugins/keyfile/reader.c @@ -167,7 +167,9 @@ build_route (int family, return NULL; } - route = nm_ip_route_new (family, dest_str, plen, gateway_str, metric, &error); + route = nm_ip_route_new (family, dest_str, plen, gateway_str, + metric ? (gint64) metric : -1, + &error); if (!route) { nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid %s route: %s", __func__, family == AF_INET ? "IPv4" : "IPv6", diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index 2e0b619dad..a38a3bd0f0 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -49,7 +49,7 @@ check_ip_address (NMSettingIPConfig *config, int idx, const char *address, int p static void check_ip_route (NMSettingIPConfig *config, int idx, const char *destination, int plen, - const char *next_hop, int metric) + const char *next_hop, gint64 metric) { NMIPRoute *route = nm_setting_ip_config_get_route (config, idx); @@ -261,17 +261,17 @@ test_read_valid_wired_connection (void) /* IPv4 routes */ g_assert (nm_setting_ip_config_get_num_routes (s_ip4) == 12); - check_ip_route (s_ip4, 0, "5.6.7.8", 32, NULL, 0); + check_ip_route (s_ip4, 0, "5.6.7.8", 32, NULL, -1); check_ip_route (s_ip4, 1, "1.2.3.0", 24, "2.3.4.8", 99); - check_ip_route (s_ip4, 2, "1.1.1.2", 12, NULL, 0); - check_ip_route (s_ip4, 3, "1.1.1.3", 13, NULL, 0); - check_ip_route (s_ip4, 4, "1.1.1.4", 14, "2.2.2.4", 0); - check_ip_route (s_ip4, 5, "1.1.1.5", 15, "2.2.2.5", 0); - check_ip_route (s_ip4, 6, "1.1.1.6", 16, "2.2.2.6", 0); - check_ip_route (s_ip4, 7, "1.1.1.7", 17, NULL, 0); - check_ip_route (s_ip4, 8, "1.1.1.8", 18, NULL, 0); - check_ip_route (s_ip4, 9, "1.1.1.9", 19, NULL, 0); - check_ip_route (s_ip4, 10, "1.1.1.10", 20, NULL, 0); + check_ip_route (s_ip4, 2, "1.1.1.2", 12, NULL, -1); + check_ip_route (s_ip4, 3, "1.1.1.3", 13, NULL, -1); + check_ip_route (s_ip4, 4, "1.1.1.4", 14, "2.2.2.4", -1); + check_ip_route (s_ip4, 5, "1.1.1.5", 15, "2.2.2.5", -1); + check_ip_route (s_ip4, 6, "1.1.1.6", 16, "2.2.2.6", -1); + check_ip_route (s_ip4, 7, "1.1.1.7", 17, NULL, -1); + check_ip_route (s_ip4, 8, "1.1.1.8", 18, NULL, -1); + check_ip_route (s_ip4, 9, "1.1.1.9", 19, NULL, -1); + check_ip_route (s_ip4, 10, "1.1.1.10", 20, NULL, -1); check_ip_route (s_ip4, 11, "1.1.1.11", 21, NULL, 21); /* ===== IPv6 SETTING ===== */ @@ -354,13 +354,13 @@ test_read_valid_wired_connection (void) /* Route #1 */ g_assert (nm_setting_ip_config_get_num_routes (s_ip6) == 7); - check_ip_route (s_ip6, 0, "d:e:f:0:1:2:3:4", 64, "f:e:d:c:1:2:3:4", 0); + check_ip_route (s_ip6, 0, "d:e:f:0:1:2:3:4", 64, "f:e:d:c:1:2:3:4", -1); check_ip_route (s_ip6, 1, "a:b:c:d::", 64, "f:e:d:c:1:2:3:4", 99); - check_ip_route (s_ip6, 2, "8:7:6:5:4:3:2:1", 128, NULL, 0); + check_ip_route (s_ip6, 2, "8:7:6:5:4:3:2:1", 128, NULL, -1); check_ip_route (s_ip6, 3, "6:7:8:9:0:1:2:3", 126, NULL, 1); check_ip_route (s_ip6, 4, "7:8:9:0:1:2:3:4", 125, NULL, 5); check_ip_route (s_ip6, 5, "8:9:0:1:2:3:4:5", 124, NULL, 6); - check_ip_route (s_ip6, 6, "8:9:0:1:2:3:4:6", 123, NULL, 0); + check_ip_route (s_ip6, 6, "8:9:0:1:2:3:4:6", 123, NULL, -1); g_object_unref (connection); } @@ -384,7 +384,7 @@ add_one_ip_route (NMSettingIPConfig *s_ip, const char *dest, const char *nh, guint32 prefix, - guint32 metric) + gint64 metric) { NMIPRoute *route; GError *error = NULL; @@ -484,7 +484,7 @@ test_write_wired_connection (void) /* Routes */ add_one_ip_route (s_ip4, route1, route1_nh, 24, 3); add_one_ip_route (s_ip4, route2, route2_nh, 8, 1); - add_one_ip_route (s_ip4, route3, route3_nh, 7, 0); + add_one_ip_route (s_ip4, route3, route3_nh, 7, -1); add_one_ip_route (s_ip4, route4, route4_nh, 6, 4); /* DNS servers */ @@ -508,7 +508,7 @@ test_write_wired_connection (void) add_one_ip_route (s_ip6, route6_1, route6_1_nh, 64, 3); add_one_ip_route (s_ip6, route6_2, route6_2_nh, 56, 1); add_one_ip_route (s_ip6, route6_3, route6_3_nh, 63, 5); - add_one_ip_route (s_ip6, route6_4, route6_4_nh, 62, 0); + add_one_ip_route (s_ip6, route6_4, route6_4_nh, 62, -1); /* DNS servers */ nm_setting_ip_config_add_dns (s_ip6, dns6_1); diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c index 24d7aba991..77c996095b 100644 --- a/src/settings/plugins/keyfile/writer.c +++ b/src/settings/plugins/keyfile/writer.c @@ -142,7 +142,7 @@ write_ip_values (GKeyFile *file, addr = nm_ip_route_get_dest (route); plen = nm_ip_route_get_prefix (route); gw = nm_ip_route_get_next_hop (route); - metric = nm_ip_route_get_metric (route); + metric = MAX (0, nm_ip_route_get_metric (route)); } else { NMIPAddress *address = array->pdata[i]; -- cgit v1.2.1 From 8e99b44f08be88f4574a2b47712c981311f794fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Fri, 7 Nov 2014 13:48:25 +0100 Subject: dhcp: fix building without sys/auxv.h --- src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h b/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h index 1308109d9a..2823acb0cf 100644 --- a/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h +++ b/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h @@ -30,7 +30,9 @@ #include #include #include +#ifdef HAVE_SYS_AUXV_H #include +#endif #include #include -- cgit v1.2.1 From 7c214de45bb4f4adea946a6961b2aab01a678038 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Wed, 5 Nov 2014 11:01:09 -0500 Subject: libnm: rename NMVpnPluginUiInterface, add to NetworkManager.h Rename NMVpnPluginUiInterface to NMVpnEditorPlugin (to clarify that it's unrelated to NMVpnPlugin), and add it to NetworkManager.h. Rename NMVpnPluginUiWidgetInterface to NMVpnEditor, because it's not a widget, and will soon be used for non-gui editing too. (Also, add a placeholder for the method that non-gui editing will use.) Fix the typedefs to not mix up the (dummy) NMVpnEditorPlugin and NMVpnEditor types with the types of their interface structs. Update to use G_DEFINE_INTERFACE. Drop NMVpnPluginUiInterfaceProp; it doesn't matter what codes plugin implementations use for the interface properties that they implement. --- libnm/Makefile.am | 4 +- libnm/NetworkManager.h | 1 + libnm/libnm.ver | 21 ++-- libnm/nm-types.h | 2 + libnm/nm-vpn-editor-plugin.c | 179 +++++++++++++++++++++++++++ libnm/nm-vpn-editor-plugin.h | 178 +++++++++++++++++++++++++++ libnm/nm-vpn-plugin-ui-interface.c | 244 ------------------------------------- libnm/nm-vpn-plugin-ui-interface.h | 206 ------------------------------- 8 files changed, 372 insertions(+), 463 deletions(-) create mode 100644 libnm/nm-vpn-editor-plugin.c create mode 100644 libnm/nm-vpn-editor-plugin.h delete mode 100644 libnm/nm-vpn-plugin-ui-interface.c delete mode 100644 libnm/nm-vpn-plugin-ui-interface.h diff --git a/libnm/Makefile.am b/libnm/Makefile.am index 26e0a4c737..f911d86e66 100644 --- a/libnm/Makefile.am +++ b/libnm/Makefile.am @@ -51,7 +51,7 @@ libnminclude_hfiles = \ nm-secret-agent.h \ nm-types.h \ nm-vpn-connection.h \ - nm-vpn-plugin-ui-interface.h \ + nm-vpn-editor-plugin.h \ nm-wimax-nsp.h libnminclude_nointrospect_hfiles = \ @@ -108,7 +108,7 @@ libnm_la_csources = \ nm-secret-agent.c \ nm-vpn-connection.c \ nm-vpn-plugin-old.c \ - nm-vpn-plugin-ui-interface.c \ + nm-vpn-editor-plugin.c \ nm-wimax-nsp.c libnm_la_SOURCES = \ diff --git a/libnm/NetworkManager.h b/libnm/NetworkManager.h index 39212fda1d..3a31c5fa07 100644 --- a/libnm/NetworkManager.h +++ b/libnm/NetworkManager.h @@ -82,6 +82,7 @@ #include #include #include +#include #include #undef __NETWORKMANAGER_H_INSIDE__ diff --git a/libnm/libnm.ver b/libnm/libnm.ver index e4e347aaab..8d9ca07834 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -808,6 +808,16 @@ global: nm_vpn_connection_get_vpn_state; nm_vpn_connection_state_get_type; nm_vpn_connection_state_reason_get_type; + nm_vpn_editor_get_type; + nm_vpn_editor_get_widget; + nm_vpn_editor_plugin_capability_get_type; + nm_vpn_editor_plugin_export; + nm_vpn_editor_plugin_get_capabilities; + nm_vpn_editor_plugin_get_editor; + nm_vpn_editor_plugin_get_suggested_filename; + nm_vpn_editor_plugin_get_type; + nm_vpn_editor_plugin_import; + nm_vpn_editor_update_connection; nm_vpn_plugin_error_get_type; nm_vpn_plugin_error_quark; nm_vpn_plugin_failure_get_type; @@ -822,17 +832,6 @@ global: nm_vpn_plugin_old_set_ip4_config; nm_vpn_plugin_old_set_login_banner; nm_vpn_plugin_old_set_state; - nm_vpn_plugin_ui_capability_get_type; - nm_vpn_plugin_ui_interface_export; - nm_vpn_plugin_ui_interface_get_capabilities; - nm_vpn_plugin_ui_interface_get_suggested_name; - nm_vpn_plugin_ui_interface_get_type; - nm_vpn_plugin_ui_interface_import; - nm_vpn_plugin_ui_interface_prop_get_type; - nm_vpn_plugin_ui_interface_ui_factory; - nm_vpn_plugin_ui_widget_interface_get_type; - nm_vpn_plugin_ui_widget_interface_get_widget; - nm_vpn_plugin_ui_widget_interface_update_connection; nm_vpn_service_state_get_type; nm_wep_key_type_get_type; nm_wimax_nsp_connection_valid; diff --git a/libnm/nm-types.h b/libnm/nm-types.h index 3602878a6a..2e62ffdea3 100644 --- a/libnm/nm-types.h +++ b/libnm/nm-types.h @@ -49,6 +49,8 @@ typedef struct _NMObject NMObject; typedef struct _NMRemoteConnection NMRemoteConnection; typedef struct _NMSecretAgent NMSecretAgent; typedef struct _NMVpnConnection NMVpnConnection; +typedef struct _NMVpnEditorPlugin NMVpnEditorPlugin; +typedef struct _NMVpnEditor NMVpnEditor; typedef struct _NMWimaxNsp NMWimaxNsp; #endif /* NM_TYPES_H */ diff --git a/libnm/nm-vpn-editor-plugin.c b/libnm/nm-vpn-editor-plugin.c new file mode 100644 index 0000000000..02393fa8e8 --- /dev/null +++ b/libnm/nm-vpn-editor-plugin.c @@ -0,0 +1,179 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2008 - 2010 Red Hat, Inc. + * Copyright 2008 Novell, Inc. + */ + +#include "nm-vpn-editor-plugin.h" + +static void nm_vpn_editor_plugin_default_init (NMVpnEditorPluginInterface *iface); + +G_DEFINE_INTERFACE (NMVpnEditorPlugin, nm_vpn_editor_plugin, G_TYPE_OBJECT) + +static void +nm_vpn_editor_plugin_default_init (NMVpnEditorPluginInterface *iface) +{ + /* Properties */ + + /** + * NMVpnEditorPlugin:name: + * + * Short display name of the VPN plugin. + */ + g_object_interface_install_property (iface, + g_param_spec_string (NM_VPN_EDITOR_PLUGIN_NAME, "", "", + NULL, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMVpnEditorPlugin:description: + * + * Longer description of the VPN plugin. + */ + g_object_interface_install_property (iface, + g_param_spec_string (NM_VPN_EDITOR_PLUGIN_DESCRIPTION, "", "", + NULL, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); + + /** + * NMVpnEditorPlugin:service: + * + * D-Bus service name of the plugin's VPN service. + */ + g_object_interface_install_property (iface, + g_param_spec_string (NM_VPN_EDITOR_PLUGIN_SERVICE, "", "", + NULL, + G_PARAM_READABLE | + G_PARAM_STATIC_STRINGS)); +} + +/** + * nm_vpn_editor_plugin_get_editor: + * + * Returns: (transfer full): + */ +NMVpnEditor * +nm_vpn_editor_plugin_get_editor (NMVpnEditorPlugin *plugin, + NMConnection *connection, + GError **error) +{ + g_return_val_if_fail (NM_IS_VPN_EDITOR_PLUGIN (plugin), NULL); + + return NM_VPN_EDITOR_PLUGIN_GET_INTERFACE (plugin)->get_editor (plugin, connection, error); +} + +NMVpnEditorPluginCapability +nm_vpn_editor_plugin_get_capabilities (NMVpnEditorPlugin *plugin) +{ + g_return_val_if_fail (NM_IS_VPN_EDITOR_PLUGIN (plugin), 0); + + return NM_VPN_EDITOR_PLUGIN_GET_INTERFACE (plugin)->get_capabilities (plugin); +} + +/** + * nm_vpn_editor_plugin_import: + * + * Returns: (transfer full): + */ +NMConnection * +nm_vpn_editor_plugin_import (NMVpnEditorPlugin *plugin, + const char *path, + GError **error) +{ + g_return_val_if_fail (NM_IS_VPN_EDITOR_PLUGIN (plugin), NULL); + + if (nm_vpn_editor_plugin_get_capabilities (plugin) & NM_VPN_EDITOR_PLUGIN_CAPABILITY_IMPORT) { + g_return_val_if_fail (NM_VPN_EDITOR_PLUGIN_GET_INTERFACE (plugin)->import_from_file != NULL, NULL); + return NM_VPN_EDITOR_PLUGIN_GET_INTERFACE (plugin)->import_from_file (plugin, path, error); + } + return NULL; +} + +gboolean +nm_vpn_editor_plugin_export (NMVpnEditorPlugin *plugin, + const char *path, + NMConnection *connection, + GError **error) +{ + g_return_val_if_fail (NM_IS_VPN_EDITOR_PLUGIN (plugin), FALSE); + + if (nm_vpn_editor_plugin_get_capabilities (plugin) & NM_VPN_EDITOR_PLUGIN_CAPABILITY_EXPORT) { + g_return_val_if_fail (NM_VPN_EDITOR_PLUGIN_GET_INTERFACE (plugin)->export_to_file != NULL, FALSE); + return NM_VPN_EDITOR_PLUGIN_GET_INTERFACE (plugin)->export_to_file (plugin, path, connection, error); + } + return FALSE; +} + +char * +nm_vpn_editor_plugin_get_suggested_filename (NMVpnEditorPlugin *plugin, + NMConnection *connection) +{ + g_return_val_if_fail (NM_IS_VPN_EDITOR_PLUGIN (plugin), NULL); + + if (NM_VPN_EDITOR_PLUGIN_GET_INTERFACE (plugin)->get_suggested_filename) + return NM_VPN_EDITOR_PLUGIN_GET_INTERFACE (plugin)->get_suggested_filename (plugin, connection); + return NULL; +} + + +static void nm_vpn_editor_default_init (NMVpnEditorInterface *iface); + +G_DEFINE_INTERFACE (NMVpnEditor, nm_vpn_editor, G_TYPE_OBJECT) + +static void +nm_vpn_editor_default_init (NMVpnEditorInterface *iface) +{ + GType iface_type = G_TYPE_FROM_INTERFACE (iface); + + /* Signals */ + g_signal_new ("changed", + iface_type, + G_SIGNAL_RUN_FIRST, + G_STRUCT_OFFSET (NMVpnEditorInterface, changed), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +/** + * nm_vpn_editor_get_widget: + * + * Returns: (transfer none): + */ +GObject * +nm_vpn_editor_get_widget (NMVpnEditor *editor) +{ + g_return_val_if_fail (NM_IS_VPN_EDITOR (editor), NULL); + + return NM_VPN_EDITOR_GET_INTERFACE (editor)->get_widget (editor); +} + +gboolean +nm_vpn_editor_update_connection (NMVpnEditor *editor, + NMConnection *connection, + GError **error) +{ + g_return_val_if_fail (NM_IS_VPN_EDITOR (editor), FALSE); + + if (error) + g_return_val_if_fail (*error == NULL, FALSE); + + return NM_VPN_EDITOR_GET_INTERFACE (editor)->update_connection (editor, connection, error); +} diff --git a/libnm/nm-vpn-editor-plugin.h b/libnm/nm-vpn-editor-plugin.h new file mode 100644 index 0000000000..76b365876e --- /dev/null +++ b/libnm/nm-vpn-editor-plugin.h @@ -0,0 +1,178 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2008 - 2014 Red Hat, Inc. + * Copyright 2008 Novell, Inc. + */ + +#ifndef __NM_VPN_EDITOR_PLUGIN_H__ +#define __NM_VPN_EDITOR_PLUGIN_H__ + +#if !defined (__NETWORKMANAGER_H_INSIDE__) && !defined (NETWORKMANAGER_COMPILATION) +#error "Only can be included directly." +#endif + +#include +#include +#include + +G_BEGIN_DECLS + +/* Plugin's factory function that returns a GObject that implements + * NMVpnEditorPlugin. + */ +#ifndef __GI_SCANNER__ +typedef NMVpnEditorPlugin * (*NMVpnEditorPluginFactory) (GError **error); +NMVpnEditorPlugin *nm_vpn_editor_plugin_factory (GError **error); +#endif + + +/**************************************************/ +/* Editor plugin interface */ +/**************************************************/ + +#define NM_TYPE_VPN_EDITOR_PLUGIN (nm_vpn_editor_plugin_get_type ()) +#define NM_VPN_EDITOR_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VPN_EDITOR_PLUGIN, NMVpnEditorPlugin)) +#define NM_IS_VPN_EDITOR_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_VPN_EDITOR_PLUGIN)) +#define NM_VPN_EDITOR_PLUGIN_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), NM_TYPE_VPN_EDITOR_PLUGIN, NMVpnEditorPluginInterface)) + +/** + * NMVpnEditorPluginCapability: + * @NM_VPN_EDITOR_PLUGIN_CAPABILITY_NONE: unknown or no capability + * @NM_VPN_EDITOR_PLUGIN_CAPABILITY_IMPORT: the plugin can import new connections + * @NM_VPN_EDITOR_PLUGIN_CAPABILITY_EXPORT: the plugin can export connections + * @NM_VPN_EDITOR_PLUGIN_CAPABILITY_IPV6: the plugin supports IPv6 addressing + * + * Flags that indicate certain capabilities of the plugin to editor programs. + **/ +typedef enum /*< flags >*/ { + NM_VPN_EDITOR_PLUGIN_CAPABILITY_NONE = 0x00, + NM_VPN_EDITOR_PLUGIN_CAPABILITY_IMPORT = 0x01, + NM_VPN_EDITOR_PLUGIN_CAPABILITY_EXPORT = 0x02, + NM_VPN_EDITOR_PLUGIN_CAPABILITY_IPV6 = 0x04 +} NMVpnEditorPluginCapability; + +/* Short display name of the VPN plugin */ +#define NM_VPN_EDITOR_PLUGIN_NAME "name" + +/* Longer description of the VPN plugin */ +#define NM_VPN_EDITOR_PLUGIN_DESCRIPTION "description" + +/* D-Bus service name of the plugin's VPN service */ +#define NM_VPN_EDITOR_PLUGIN_SERVICE "service" + +typedef struct { + GTypeInterface g_iface; + + /* Returns an #NMVpnEditor, pre-filled with values from @connection if + * non-%NULL. + */ + NMVpnEditor * (*get_editor) (NMVpnEditorPlugin *plugin, + NMConnection *connection, + GError **error); + + /* Plugin's capabiltity function that returns a bitmask of capabilities. */ + NMVpnEditorPluginCapability (*get_capabilities) (NMVpnEditorPlugin *plugin); + + /* Try to import a connection from the specified path. On success, return a + * partial #NMConnection object. On error, return %NULL and set @error with + * additional information. Note that @error can be %NULL, in which case no + * additional error information should be provided. + */ + NMConnection * (*import_from_file) (NMVpnEditorPlugin *plugin, + const char *path, + GError **error); + + /* Export the given connection to the specified path. Return %TRUE on success. + * On error, return %FALSE and set @error with additional error information. + * Note that @error can be %NULL, in which case no additional error information + * should be provided. + */ + gboolean (*export_to_file) (NMVpnEditorPlugin *plugin, + const char *path, + NMConnection *connection, + GError **error); + + /* For a given connection, return a suggested file name. Returned value + * will be %NULL or a suggested file name to be freed by the caller. + */ + char * (*get_suggested_filename) (NMVpnEditorPlugin *plugin, NMConnection *connection); +} NMVpnEditorPluginInterface; + +GType nm_vpn_editor_plugin_get_type (void); + +NMVpnEditor *nm_vpn_editor_plugin_get_editor (NMVpnEditorPlugin *plugin, + NMConnection *connection, + GError **error); + +NMVpnEditorPluginCapability nm_vpn_editor_plugin_get_capabilities (NMVpnEditorPlugin *plugin); + +NMConnection *nm_vpn_editor_plugin_import (NMVpnEditorPlugin *plugin, + const char *path, + GError **error); +gboolean nm_vpn_editor_plugin_export (NMVpnEditorPlugin *plugin, + const char *path, + NMConnection *connection, + GError **error); +char *nm_vpn_editor_plugin_get_suggested_filename (NMVpnEditorPlugin *plugin, + NMConnection *connection); + +/**************************************************/ +/* Editor interface */ +/**************************************************/ + +#define NM_TYPE_VPN_EDITOR (nm_vpn_editor_get_type ()) +#define NM_VPN_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VPN_EDITOR, NMVpnEditor)) +#define NM_IS_VPN_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_VPN_EDITOR)) +#define NM_VPN_EDITOR_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), NM_TYPE_VPN_EDITOR, NMVpnEditorInterface)) + +typedef struct { + GTypeInterface g_iface; + + /* Return the #GtkWidget for the VPN editor's UI */ + GObject * (*get_widget) (NMVpnEditor *editor); + + void (*placeholder) (void); + + /* Called to save the user-entered options to the connection object. Should + * return %FALSE and set @error if the current options are invalid. @error + * should contain enough information for the plugin to determine which UI + * widget is invalid at a later point in time. For example, creating unique + * error codes for what error occurred and populating the message field + * of @error with the name of the invalid property. + */ + gboolean (*update_connection) (NMVpnEditor *editor, + NMConnection *connection, + GError **error); + + /* Emitted when the value of a UI widget changes. May trigger a validity + * check via update_connection() to write values to the connection. + */ + void (*changed) (NMVpnEditor *editor); +} NMVpnEditorInterface; + +GType nm_vpn_editor_get_type (void); + +GObject * nm_vpn_editor_get_widget (NMVpnEditor *editor); + +gboolean nm_vpn_editor_update_connection (NMVpnEditor *editor, + NMConnection *connection, + GError **error); + +G_END_DECLS + +#endif /* NM_VPN_EDITOR_PLUGIN_H */ diff --git a/libnm/nm-vpn-plugin-ui-interface.c b/libnm/nm-vpn-plugin-ui-interface.c deleted file mode 100644 index af00cfd1da..0000000000 --- a/libnm/nm-vpn-plugin-ui-interface.c +++ /dev/null @@ -1,244 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. - * - * Copyright 2008 - 2010 Red Hat, Inc. - * Copyright 2008 Novell, Inc. - */ - -#include "nm-vpn-plugin-ui-interface.h" - -static void -interface_init (gpointer g_iface) -{ - static gboolean initialized = FALSE; - - if (initialized) - return; - - /* Properties */ - - /** - * NMVpnPluginUiInterface:name: - * - * Short display name of the VPN plugin. - */ - g_object_interface_install_property (g_iface, - g_param_spec_string (NM_VPN_PLUGIN_UI_INTERFACE_NAME, "", "", - NULL, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMVpnPluginUiInterface:desc: - * - * Longer description of the VPN plugin. - */ - g_object_interface_install_property (g_iface, - g_param_spec_string (NM_VPN_PLUGIN_UI_INTERFACE_DESC, "", "", - NULL, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - /** - * NMVpnPluginUiInterface:service: - * - * D-Bus service name of the plugin's VPN service. - */ - g_object_interface_install_property (g_iface, - g_param_spec_string (NM_VPN_PLUGIN_UI_INTERFACE_SERVICE, "", "", - NULL, - G_PARAM_READABLE | - G_PARAM_STATIC_STRINGS)); - - initialized = TRUE; -} - - -GType -nm_vpn_plugin_ui_interface_get_type (void) -{ - static GType vpn_plugin_ui_interface_type = 0; - - if (!vpn_plugin_ui_interface_type) { - const GTypeInfo vpn_plugin_ui_interface_info = { - sizeof (NMVpnPluginUiInterface), /* class_size */ - interface_init, /* base_init */ - NULL, /* base_finalize */ - NULL, - NULL, /* class_finalize */ - NULL, /* class_data */ - 0, - 0, /* n_preallocs */ - NULL - }; - - vpn_plugin_ui_interface_type = g_type_register_static (G_TYPE_INTERFACE, - "NMVpnPluginUiInterface", - &vpn_plugin_ui_interface_info, - 0); - - g_type_interface_add_prerequisite (vpn_plugin_ui_interface_type, G_TYPE_OBJECT); - } - - return vpn_plugin_ui_interface_type; -} - - -/** - * nm_vpn_plugin_ui_interface_ui_factory: - * - * Returns: (transfer full): - */ -NMVpnPluginUiWidgetInterface * -nm_vpn_plugin_ui_interface_ui_factory (NMVpnPluginUiInterface *iface, - NMConnection *connection, - GError **error) -{ - g_return_val_if_fail (NM_IS_VPN_PLUGIN_UI_INTERFACE (iface), NULL); - - return NM_VPN_PLUGIN_UI_INTERFACE_GET_INTERFACE (iface)->ui_factory (iface, connection, error); -} - -guint32 -nm_vpn_plugin_ui_interface_get_capabilities (NMVpnPluginUiInterface *iface) -{ - g_return_val_if_fail (NM_IS_VPN_PLUGIN_UI_INTERFACE (iface), 0); - - return NM_VPN_PLUGIN_UI_INTERFACE_GET_INTERFACE (iface)->get_capabilities (iface); -} - -/** - * nm_vpn_plugin_ui_interface_import: - * - * Returns: (transfer full): - */ -NMConnection * -nm_vpn_plugin_ui_interface_import (NMVpnPluginUiInterface *iface, - const char *path, - GError **error) -{ - g_return_val_if_fail (NM_IS_VPN_PLUGIN_UI_INTERFACE (iface), NULL); - - if (nm_vpn_plugin_ui_interface_get_capabilities (iface) & NM_VPN_PLUGIN_UI_CAPABILITY_IMPORT) { - g_return_val_if_fail (NM_VPN_PLUGIN_UI_INTERFACE_GET_INTERFACE (iface)->import_from_file != NULL, NULL); - return NM_VPN_PLUGIN_UI_INTERFACE_GET_INTERFACE (iface)->import_from_file (iface, path, error); - } - return NULL; -} - -gboolean -nm_vpn_plugin_ui_interface_export (NMVpnPluginUiInterface *iface, - const char *path, - NMConnection *connection, - GError **error) -{ - g_return_val_if_fail (NM_IS_VPN_PLUGIN_UI_INTERFACE (iface), FALSE); - - if (nm_vpn_plugin_ui_interface_get_capabilities (iface) & NM_VPN_PLUGIN_UI_CAPABILITY_EXPORT) { - g_return_val_if_fail (NM_VPN_PLUGIN_UI_INTERFACE_GET_INTERFACE (iface)->export_to_file != NULL, FALSE); - return NM_VPN_PLUGIN_UI_INTERFACE_GET_INTERFACE (iface)->export_to_file (iface, path, connection, error); - } - return FALSE; -} - -char * -nm_vpn_plugin_ui_interface_get_suggested_name (NMVpnPluginUiInterface *iface, - NMConnection *connection) -{ - g_return_val_if_fail (NM_IS_VPN_PLUGIN_UI_INTERFACE (iface), NULL); - - if (NM_VPN_PLUGIN_UI_INTERFACE_GET_INTERFACE (iface)->get_suggested_name) - return NM_VPN_PLUGIN_UI_INTERFACE_GET_INTERFACE (iface)->get_suggested_name (iface, connection); - return NULL; -} - - -static void -widget_interface_init (gpointer g_iface) -{ - GType iface_type = G_TYPE_FROM_INTERFACE (g_iface); - static gboolean initialized = FALSE; - - if (initialized) - return; - - /* Signals */ - g_signal_new ("changed", - iface_type, - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (NMVpnPluginUiWidgetInterface, changed), - NULL, NULL, - g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, 0); - - initialized = TRUE; -} - -GType -nm_vpn_plugin_ui_widget_interface_get_type (void) -{ - static GType vpn_plugin_ui_widget_interface_type = 0; - - if (!vpn_plugin_ui_widget_interface_type) { - const GTypeInfo vpn_plugin_ui_widget_interface_info = { - sizeof (NMVpnPluginUiWidgetInterface), /* class_size */ - widget_interface_init, /* base_init */ - NULL, /* base_finalize */ - NULL, - NULL, /* class_finalize */ - NULL, /* class_data */ - 0, - 0, /* n_preallocs */ - NULL - }; - - vpn_plugin_ui_widget_interface_type = g_type_register_static (G_TYPE_INTERFACE, - "NMVpnPluginUiWidgetInterface", - &vpn_plugin_ui_widget_interface_info, - 0); - - g_type_interface_add_prerequisite (vpn_plugin_ui_widget_interface_type, G_TYPE_OBJECT); - } - - return vpn_plugin_ui_widget_interface_type; -} - -/** - * nm_vpn_plugin_ui_widget_interface_get_widget: - * - * Returns: (transfer none): - */ -GObject * -nm_vpn_plugin_ui_widget_interface_get_widget (NMVpnPluginUiWidgetInterface *iface) -{ - g_return_val_if_fail (NM_IS_VPN_PLUGIN_UI_WIDGET_INTERFACE (iface), NULL); - - return NM_VPN_PLUGIN_UI_WIDGET_INTERFACE_GET_INTERFACE (iface)->get_widget (iface); -} - -gboolean -nm_vpn_plugin_ui_widget_interface_update_connection (NMVpnPluginUiWidgetInterface *iface, - NMConnection *connection, - GError **error) -{ - g_return_val_if_fail (NM_IS_VPN_PLUGIN_UI_WIDGET_INTERFACE (iface), FALSE); - - if (error) - g_return_val_if_fail (*error == NULL, FALSE); - - return NM_VPN_PLUGIN_UI_WIDGET_INTERFACE_GET_INTERFACE (iface)->update_connection (iface, connection, error); -} diff --git a/libnm/nm-vpn-plugin-ui-interface.h b/libnm/nm-vpn-plugin-ui-interface.h deleted file mode 100644 index 4aab74482e..0000000000 --- a/libnm/nm-vpn-plugin-ui-interface.h +++ /dev/null @@ -1,206 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. - * - * Copyright 2008 - 2010 Red Hat, Inc. - * Copyright 2008 Novell, Inc. - */ - -#ifndef __NM_VPN_PLUGIN_UI_INTERFACE_H__ -#define __NM_VPN_PLUGIN_UI_INTERFACE_H__ - -#if !defined (__NETWORKMANAGER_H_INSIDE__) && !defined (NETWORKMANAGER_COMPILATION) -#error "Only can be included directly." -#endif - -#include -#include -#include - -G_BEGIN_DECLS - -typedef struct _NMVpnPluginUiInterface NMVpnPluginUiInterface; -typedef struct _NMVpnPluginUiWidgetInterface NMVpnPluginUiWidgetInterface; - -/* Plugin's factory function that returns a GObject that implements - * NMVpnPluginUiInterface. - */ -#ifndef __GI_SCANNER__ -typedef NMVpnPluginUiInterface * (*NMVpnPluginUiFactory) (GError **error); -NMVpnPluginUiInterface *nm_vpn_plugin_ui_factory (GError **error); -#endif - - -/**************************************************/ -/* Plugin interface */ -/**************************************************/ - -#define NM_TYPE_VPN_PLUGIN_UI_INTERFACE (nm_vpn_plugin_ui_interface_get_type ()) -#define NM_VPN_PLUGIN_UI_INTERFACE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VPN_PLUGIN_UI_INTERFACE, NMVpnPluginUiInterface)) -#define NM_IS_VPN_PLUGIN_UI_INTERFACE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_VPN_PLUGIN_UI_INTERFACE)) -#define NM_VPN_PLUGIN_UI_INTERFACE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), NM_TYPE_VPN_PLUGIN_UI_INTERFACE, NMVpnPluginUiInterface)) - -/** - * NMVpnPluginUiCapability: - * @NM_VPN_PLUGIN_UI_CAPABILITY_NONE: unknown or no capability - * @NM_VPN_PLUGIN_UI_CAPABILITY_IMPORT: the plugin can import new connections - * @NM_VPN_PLUGIN_UI_CAPABILITY_EXPORT: the plugin can export connections - * @NM_VPN_PLUGIN_UI_CAPABILITY_IPV6: the plugin supports IPv6 addressing - * - * Flags that indicate to UI programs certain capabilities of the plugin. - **/ -typedef enum /*< flags >*/ { - NM_VPN_PLUGIN_UI_CAPABILITY_NONE = 0x00, - NM_VPN_PLUGIN_UI_CAPABILITY_IMPORT = 0x01, - NM_VPN_PLUGIN_UI_CAPABILITY_EXPORT = 0x02, - NM_VPN_PLUGIN_UI_CAPABILITY_IPV6 = 0x04 -} NMVpnPluginUiCapability; - -/* Short display name of the VPN plugin */ -#define NM_VPN_PLUGIN_UI_INTERFACE_NAME "name" - -/* Longer description of the VPN plugin */ -#define NM_VPN_PLUGIN_UI_INTERFACE_DESC "desc" - -/* D-Bus service name of the plugin's VPN service */ -#define NM_VPN_PLUGIN_UI_INTERFACE_SERVICE "service" - -/** - * NMVpnPluginUiInterfaceProp: - * @NM_VPN_PLUGIN_UI_INTERFACE_PROP_NAME: the VPN plugin's name - * @NM_VPN_PLUGIN_UI_INTERFACE_PROP_DESC: description of the VPN plugin and what - * VPN services it supports - * @NM_VPN_PLUGIN_UI_INTERFACE_PROP_SERVICE: the D-Bus service name used by the - * plugin's VPN service daemon - * - * #GObject property numbers that plugins should override to provide certain - * information to UI programs. - **/ -typedef enum { - /* private */ - NM_VPN_PLUGIN_UI_INTERFACE_PROP_FIRST = 0x1000, - - /* public */ - NM_VPN_PLUGIN_UI_INTERFACE_PROP_NAME = NM_VPN_PLUGIN_UI_INTERFACE_PROP_FIRST, - NM_VPN_PLUGIN_UI_INTERFACE_PROP_DESC, - NM_VPN_PLUGIN_UI_INTERFACE_PROP_SERVICE -} NMVpnPluginUiInterfaceProp; - - -struct _NMVpnPluginUiInterface { - GTypeInterface g_iface; - - /* Plugin's factory function that returns a GObject that implements - * NMVpnPluginUiWidgetInterface, pre-filled with values from 'connection' - * if non-NULL. - */ - NMVpnPluginUiWidgetInterface * (*ui_factory) (NMVpnPluginUiInterface *iface, - NMConnection *connection, - GError **error); - - /* Plugin's capabiltity function that returns a bitmask of capabilities - * described by NM_VPN_PLUGIN_UI_CAPABILITY_* defines. - */ - guint32 (*get_capabilities) (NMVpnPluginUiInterface *iface); - - /* Try to import a connection from the specified path. On success, return a - * partial NMConnection object. On error, return NULL and set 'error' with - * additional information. Note that 'error' can be NULL, in which case no - * additional error information should be provided. - */ - NMConnection * (*import_from_file) (NMVpnPluginUiInterface *iface, - const char *path, - GError **error); - - /* Export the given connection to the specified path. Return TRUE on success. - * On error, return FALSE and set 'error' with additional error information. - * Note that 'error' can be NULL, in which case no additional error information - * should be provided. - */ - gboolean (*export_to_file) (NMVpnPluginUiInterface *iface, - const char *path, - NMConnection *connection, - GError **error); - - /* For a given connection, return a suggested file name. Returned value should - * be NULL or a suggested file name allocated via g_malloc/g_new/etc to be freed - * by the caller. - */ - char * (*get_suggested_name) (NMVpnPluginUiInterface *iface, NMConnection *connection); -}; - -GType nm_vpn_plugin_ui_interface_get_type (void); - -NMVpnPluginUiWidgetInterface *nm_vpn_plugin_ui_interface_ui_factory (NMVpnPluginUiInterface *iface, - NMConnection *connection, - GError **error); - -guint32 nm_vpn_plugin_ui_interface_get_capabilities (NMVpnPluginUiInterface *iface); - -NMConnection *nm_vpn_plugin_ui_interface_import (NMVpnPluginUiInterface *iface, - const char *path, - GError **error); - -gboolean nm_vpn_plugin_ui_interface_export (NMVpnPluginUiInterface *iface, - const char *path, - NMConnection *connection, - GError **error); - -char *nm_vpn_plugin_ui_interface_get_suggested_name (NMVpnPluginUiInterface *iface, - NMConnection *connection); - -/**************************************************/ -/* UI widget interface */ -/**************************************************/ - -#define NM_TYPE_VPN_PLUGIN_UI_WIDGET_INTERFACE (nm_vpn_plugin_ui_widget_interface_get_type ()) -#define NM_VPN_PLUGIN_UI_WIDGET_INTERFACE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VPN_PLUGIN_UI_WIDGET_INTERFACE, NMVpnPluginUiWidgetInterface)) -#define NM_IS_VPN_PLUGIN_UI_WIDGET_INTERFACE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_VPN_PLUGIN_UI_WIDGET_INTERFACE)) -#define NM_VPN_PLUGIN_UI_WIDGET_INTERFACE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), NM_TYPE_VPN_PLUGIN_UI_WIDGET_INTERFACE, NMVpnPluginUiWidgetInterface)) - -struct _NMVpnPluginUiWidgetInterface { - GTypeInterface g_iface; - - /* Return the GtkWidget for the VPN's UI */ - GObject * (*get_widget) (NMVpnPluginUiWidgetInterface *iface); - - /* Called to save the user-entered options to the connection object. Should - * return FALSE and set 'error' if the current options are invalid. 'error' - * should contain enough information for the plugin to determine which UI - * widget is invalid at a later point in time. For example, creating unique - * error codes for what error occurred and populating the message field - * of 'error' with the name of the invalid property. - */ - gboolean (*update_connection) (NMVpnPluginUiWidgetInterface *iface, - NMConnection *connection, - GError **error); - - /* Emitted when the value of a UI widget changes. May trigger a validity - * check via update_connection() to write values to the connection */ - void (*changed) (NMVpnPluginUiWidgetInterface *iface); -}; - -GType nm_vpn_plugin_ui_widget_interface_get_type (void); - -GObject * nm_vpn_plugin_ui_widget_interface_get_widget (NMVpnPluginUiWidgetInterface *iface); - -gboolean nm_vpn_plugin_ui_widget_interface_update_connection (NMVpnPluginUiWidgetInterface *iface, - NMConnection *connection, - GError **error); - -G_END_DECLS - -#endif /* NM_VPN_PLUGIN_UI_INTERFACE_H */ -- cgit v1.2.1 From b4fe3b7cd97df2360b49b8bcf3b7f065384519a3 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Mon, 3 Nov 2014 21:22:09 +0100 Subject: libnm: Drop a wrong assert An active connection object could disappear from the bus before its removal from NMManager:active-connections is signalled -- don't assert it's gone from there already. Here /o/fd/NM/ActiveConnection/81 disappears shortly after it's added: libnm-Message: PC: (0x9ebd088) NMManager:active-connections => '['/org/freedesktop/NetworkManager/ActiveConnection/81', '/org/freedesktop/NetworkManager/ActiveConnection/80']' (ao / NMActiveConnection) libnm-Message: PC: (0x9ebd088) NMManager:activating-connection => ''/'' (o / NMActiveConnection) libnm-Message: PC: (0x9ed1458) NMDeviceTeam:state => '110' (u) libnm-Message: PC: (0x9ed1458) NMDeviceTeam:state-reason => '(110, 0)' ((uu)) libnm-Message: PC: (0x9ece9a0) NMActiveConnection:state => '3' (u) libnm-Message: PC: (0x9ebd088) NMManager:state => '20' (u) libnm-Message: PC: (0x9ebd088) NMManager:devices => '['/org/freedesktop/NetworkManager/Devices/0', '/org/freedesktop/NetworkManager/Devices/2', '/org/freedesktop/NetworkManager/Devices/3']' (ao / NMDevice) libnm-Message: PC: (0x9ebd088) NMManager:active-connections => '['/org/freedesktop/NetworkManager/ActiveConnection/81', '/org/freedesktop/NetworkManager/ActiveConnection/80']' (ao / NMActiveConnection) libnm-Message: PC: (0x9ece9a0) NMActiveConnection:state => '4' (u) libnm-Message: PC: (0x9ece9a0) NMActiveConnection:devices => '[]' (ao / NMDevice) libnm-Message: Could not create object for /org/freedesktop/NetworkManager/ActiveConnection/81: Method "GetAll" with signature "s" on interface "org.freedesktop.DBus.Properties" doesn't exist (process:18042): libnm-CRITICAL **: object_creation_failed: assertion 'find_active_connection_by_path (self, failed_path) == NULL' failed --- libnm/nm-manager.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/libnm/nm-manager.c b/libnm/nm-manager.c index d60845edc9..8d78d221bc 100644 --- a/libnm/nm-manager.c +++ b/libnm/nm-manager.c @@ -1043,8 +1043,6 @@ object_creation_failed (NMObject *object, const char *failed_path) GError *error; GSList *iter; - g_return_if_fail (find_active_connection_by_path (self, failed_path) == NULL); - /* A newly activated connection failed due to some immediate error * and disappeared from active connection list. Make sure the * callback gets called. -- cgit v1.2.1 From 42b9e8283969a851751a9eff5c767ae99d717156 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Mon, 3 Nov 2014 23:25:23 +0100 Subject: libnm: Complete activation when ActiveConnection abruptly disappears A NMActiveConnection may disappear before a match with NMDevice is found. In such case recheck_pending_activations() would never call the activation callback and the client would hang indefinitely: libnm-Message: PC: (0x95bf088) NMManager:active-connections => '['/org/freedesktop/NetworkManager/ActiveConnection/225']' (ao / NMActiveConnection) libnm-Message: PC: (0x95bf088) NMManager:activating-connection => ''/'' (o / NMActiveConnection) libnm-Message: PC: (0x95d0a28) NMActiveConnection:state => '4' (u) libnm-Message: PC: (0x95d0a28) NMActiveConnection:devices => '[]' (ao / NMDevice) libnm-Message: PC: (0x95bf088) NMManager:active-connections => '[]' (ao / NMActiveConnection) *hang* Let's listen for active-connection-removed and tear down the activation with an error if the removed connection is one we're activating. --- libnm/nm-manager.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/libnm/nm-manager.c b/libnm/nm-manager.c index 8d78d221bc..fc558620c7 100644 --- a/libnm/nm-manager.c +++ b/libnm/nm-manager.c @@ -743,6 +743,8 @@ typedef struct { char *new_connection_path; } ActivateInfo; +static void active_removed (NMObject *object, NMActiveConnection *active, gpointer user_data); + static void activate_info_complete (ActivateInfo *info, NMActiveConnection *active, @@ -750,6 +752,7 @@ activate_info_complete (ActivateInfo *info, { NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (info->manager); + g_signal_handlers_disconnect_by_func (info->manager, G_CALLBACK (active_removed), info); if (active) g_simple_async_result_set_op_res_gpointer (info->simple, g_object_ref (active), g_object_unref); else @@ -836,6 +839,22 @@ activation_cancelled (GCancellable *cancellable, g_clear_error (&error); } +static void +active_removed (NMObject *object, NMActiveConnection *active, gpointer user_data) +{ + ActivateInfo *info = user_data; + GError *error = NULL; + + if (strcmp (info->active_path, nm_object_get_path (NM_OBJECT (active)))) + return; + + error = g_error_new_literal (NM_CLIENT_ERROR, + NM_CLIENT_ERROR_FAILED, + _("Active connection could not be attached to the device")); + activate_info_complete (info, NULL, error); + g_clear_error (&error); +} + static void activate_cb (GObject *object, GAsyncResult *result, @@ -852,6 +871,9 @@ activate_cb (GObject *object, G_CALLBACK (activation_cancelled), info); } + g_signal_connect (info->manager, "active-connection-removed", + G_CALLBACK (active_removed), info); + recheck_pending_activations (info->manager); } else { g_dbus_error_strip_remote_error (error); -- cgit v1.2.1 From 5aa93e55183ba3b67355f384ac54b4dd7e047d61 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Sun, 19 Oct 2014 15:08:44 +0200 Subject: connections: Don't give up if we've not seen an active connection yet It will appear later on. https://bugzilla.redhat.com/show_bug.cgi?id=1149200 --- clients/cli/connections.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 3f921a4003..65d900b9b7 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -1736,7 +1736,7 @@ device_state_cb (NMDevice *device, GParamSpec *pspec, gpointer user_data) g_print (_("Connection successfully activated (master waiting for slaves) (D-Bus active path: %s)\n"), nm_object_get_path (NM_OBJECT (active))); quit (); - } else if (ac_state != NM_ACTIVE_CONNECTION_STATE_ACTIVATING) { + } else if (active && ac_state != NM_ACTIVE_CONNECTION_STATE_ACTIVATING) { g_string_printf (nmc->return_text, _("Error: Connection activation failed.")); nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION; quit (); -- cgit v1.2.1 From 842648a99211441c4b7c41895ecbabf456f285c0 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Tue, 4 Nov 2014 09:38:01 +0100 Subject: team: Don't let teamd rip off the link when it terminates We might be reactivating a connection on the device and don't want it to go away. In other cases we still take care of the link deletion ourselves. --- src/devices/team/nm-device-team.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/devices/team/nm-device-team.c b/src/devices/team/nm-device-team.c index df27562019..3c949258c0 100644 --- a/src/devices/team/nm-device-team.c +++ b/src/devices/team/nm-device-team.c @@ -453,6 +453,7 @@ teamd_start (NMDevice *device, NMSettingTeam *s_team) g_ptr_array_add (argv, (gpointer) "-n"); g_ptr_array_add (argv, (gpointer) "-U"); g_ptr_array_add (argv, (gpointer) "-D"); + g_ptr_array_add (argv, (gpointer) "-N"); g_ptr_array_add (argv, (gpointer) "-t"); g_ptr_array_add (argv, (gpointer) iface); -- cgit v1.2.1 From 892602c912cd3cb8e2b4ddecd5f99649c4acab2b Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Tue, 4 Nov 2014 09:41:42 +0100 Subject: device: Don't delete the link if a re-activation is scheduled --- src/devices/nm-device.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index d9fdd1bb8b..efb7f25d2d 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -5081,6 +5081,8 @@ delete_on_deactivate_check_and_schedule (NMDevice *self, int ifindex) return; if (!priv->is_nm_owned) return; + if (priv->queued_act_request) + return; if (!nm_device_is_software (self)) return; if (nm_device_get_state (self) == NM_DEVICE_STATE_UNMANAGED) -- cgit v1.2.1 From 6a59ab0b86e6e679780d6c67aadd454133783721 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 31 Oct 2014 21:57:21 +0100 Subject: all: add macro NM_IN_SET Copied from systemd's macro IN_SET from /src/shared/macro.h Signed-off-by: Thomas Haller --- include/nm-utils-internal.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/nm-utils-internal.h b/include/nm-utils-internal.h index 4f5850aace..69ab8391be 100644 --- a/include/nm-utils-internal.h +++ b/include/nm-utils-internal.h @@ -82,4 +82,19 @@ /********************************************************/ +#define NM_IN_SET(x, y, ...) \ + ({ \ + const typeof(y) _y = (y); \ + typeof(_y) _x = (x); \ + unsigned _i; \ + gboolean _found = FALSE; \ + for (_i = 0; _i < 1 + sizeof((typeof(_x)[]) { __VA_ARGS__ })/sizeof(typeof(_x)); _i++) { \ + if (((typeof(_x)[]) { _y, __VA_ARGS__ })[_i] == _x) { \ + _found = TRUE; \ + break; \ + } \ + } \ + _found; \ + }) + #endif -- cgit v1.2.1 From f892ac4f457accbee92408f132d53d0eeaa4f624 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 29 Aug 2014 10:35:35 +0200 Subject: core/trivial: remove semicolon from macro Signed-off-by: Thomas Haller --- src/NetworkManagerUtils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h index 90182b1921..0dab6babcb 100644 --- a/src/NetworkManagerUtils.h +++ b/src/NetworkManagerUtils.h @@ -188,7 +188,7 @@ typedef struct { }; } NMUtilsIPv6IfaceId; -#define NM_UTILS_IPV6_IFACE_ID_INIT { .id = 0 }; +#define NM_UTILS_IPV6_IFACE_ID_INIT { .id = 0 } gboolean nm_utils_get_ipv6_interface_identifier (NMLinkType link_type, const guint8 *hwaddr, -- cgit v1.2.1 From 0500bade7711a20742fc65efab4dd005710cc98c Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 29 Aug 2014 10:44:00 +0200 Subject: core: fix leak of lookup_addr in NMPolicy Also, as we now evaluate the arguments of logging statements lazily, refactor a logging statement. Signed-off-by: Thomas Haller --- src/nm-policy.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/nm-policy.c b/src/nm-policy.c index e5a295bfb7..480364e9b9 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -473,12 +473,14 @@ update_system_hostname (NMPolicy *policy, NMDevice *best4, NMDevice *best6) const NMPlatformIP4Address *addr4; addr4 = nm_ip4_config_get_address (ip4_config, 0); + g_clear_object (&priv->lookup_addr); priv->lookup_addr = g_inet_address_new_from_bytes ((guint8 *) &addr4->address, G_SOCKET_FAMILY_IPV4); } else if (ip6_config && nm_ip6_config_get_num_addresses (ip6_config) > 0) { const NMPlatformIP6Address *addr6; addr6 = nm_ip6_config_get_address (ip6_config, 0); + g_clear_object (&priv->lookup_addr); priv->lookup_addr = g_inet_address_new_from_bytes ((guint8 *) &addr6->address, G_SOCKET_FAMILY_IPV6); } else { @@ -2015,9 +2017,10 @@ dns_config_changed (NMDnsManager *dns_manager, gpointer user_data) /* Re-start the hostname lookup thread if we don't have hostname yet. */ if (priv->lookup_addr) { - char *str = g_inet_address_to_string (priv->lookup_addr); + char *str = NULL; - nm_log_dbg (LOGD_DNS, "restarting reverse-lookup thread for address %s", str); + nm_log_dbg (LOGD_DNS, "restarting reverse-lookup thread for address %s", + (str = g_inet_address_to_string (priv->lookup_addr))); g_free (str); priv->lookup_cancellable = g_cancellable_new (); -- cgit v1.2.1 From c2628193607ccf6f8fb162f8459ccbb815c5cd86 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 29 Oct 2014 17:23:46 +0100 Subject: platform: don't include gsystem-local-alloc.h in nm-platform.h Signed-off-by: Thomas Haller --- src/NetworkManagerUtils.c | 1 + src/dhcp-manager/nm-dhcp-systemd.c | 1 + src/nm-manager.c | 1 + src/platform/nm-fake-platform.c | 1 + src/platform/nm-linux-platform.c | 1 + src/platform/nm-platform.c | 1 + src/platform/nm-platform.h | 1 - src/platform/tests/platform.c | 1 + src/settings/nm-settings.c | 1 + 9 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 19896696bb..c8bf226659 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -22,6 +22,7 @@ #include "config.h" #include +#include #include #include #include diff --git a/src/dhcp-manager/nm-dhcp-systemd.c b/src/dhcp-manager/nm-dhcp-systemd.c index 804be3590a..363c87f870 100644 --- a/src/dhcp-manager/nm-dhcp-systemd.c +++ b/src/dhcp-manager/nm-dhcp-systemd.c @@ -36,6 +36,7 @@ #include "nm-logging.h" #include "nm-dhcp-utils.h" #include "NetworkManagerUtils.h" +#include "gsystem-local-alloc.h" #include "nm-sd-adapt.h" diff --git a/src/nm-manager.c b/src/nm-manager.c index 459d531a97..f936235c78 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -33,6 +33,7 @@ #include #include +#include "gsystem-local-alloc.h" #include "nm-glib-compat.h" #include "nm-manager.h" #include "nm-logging.h" diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c index 9e376e5035..c3ae25f7ec 100644 --- a/src/platform/nm-fake-platform.c +++ b/src/platform/nm-fake-platform.c @@ -23,6 +23,7 @@ #include #include +#include "gsystem-local-alloc.h" #include "NetworkManagerUtils.h" #include "nm-fake-platform.h" #include "nm-logging.h" diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index ef5537d897..601066b785 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -54,6 +54,7 @@ #endif #endif +#include "gsystem-local-alloc.h" #include "NetworkManagerUtils.h" #include "nm-linux-platform.h" #include "NetworkManagerUtils.h" diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 1aa72a4dba..ae25cf8156 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -26,6 +26,7 @@ #include #include +#include "gsystem-local-alloc.h" #include "NetworkManagerUtils.h" #include "nm-utils.h" #include "nm-platform.h" diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index 13e25affbd..0ec372ff89 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -28,7 +28,6 @@ #include #include -#include "gsystem-local-alloc.h" #include "nm-types.h" #define NM_TYPE_PLATFORM (nm_platform_get_type ()) diff --git a/src/platform/tests/platform.c b/src/platform/tests/platform.c index 469652c55f..90513d4579 100644 --- a/src/platform/tests/platform.c +++ b/src/platform/tests/platform.c @@ -24,6 +24,7 @@ #include #include +#include "gsystem-local-alloc.h" #include "nm-platform.h" #include "nm-linux-platform.h" #include "nm-fake-platform.h" diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index bd90668203..9ed03f69b9 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -34,6 +34,7 @@ #include #include +#include "gsystem-local-alloc.h" #include #include #include -- cgit v1.2.1 From c52e33101436bfd5cabffdc01947b6850616a7c1 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 28 Aug 2014 19:17:07 +0200 Subject: core: move definition of NMLinkType to nm-types.h As we use NMLinkType in NetworkManagerUtils.h, we cannot use the utils header without nm-platform.h. That is clearly wrong. Apparently NMLinkType has a wider use outside of platform (and its name is not prefixed with 'platform' either). Move the enum definition to nm-types.h. Signed-off-by: Thomas Haller --- src/NetworkManagerUtils.c | 1 + src/NetworkManagerUtils.h | 3 ++- src/dhcp-manager/nm-dhcp-systemd.c | 1 + src/nm-ip4-config.c | 1 + src/nm-ip6-config.c | 1 + src/nm-types.h | 43 ++++++++++++++++++++++++++++++++++++++ src/platform/nm-platform.h | 43 -------------------------------------- 7 files changed, 49 insertions(+), 44 deletions(-) diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index c8bf226659..538295a10c 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -36,6 +36,7 @@ #include #include "NetworkManagerUtils.h" +#include "nm-platform.h" #include "nm-utils.h" #include "nm-core-internal.h" #include "nm-logging.h" diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h index 0dab6babcb..492edd133e 100644 --- a/src/NetworkManagerUtils.h +++ b/src/NetworkManagerUtils.h @@ -24,9 +24,10 @@ #include #include +#include #include "nm-connection.h" -#include "nm-platform.h" +#include "nm-types.h" gboolean nm_ethernet_address_is_valid (gconstpointer addr, gssize len); diff --git a/src/dhcp-manager/nm-dhcp-systemd.c b/src/dhcp-manager/nm-dhcp-systemd.c index 363c87f870..18b7b32180 100644 --- a/src/dhcp-manager/nm-dhcp-systemd.c +++ b/src/dhcp-manager/nm-dhcp-systemd.c @@ -37,6 +37,7 @@ #include "nm-dhcp-utils.h" #include "NetworkManagerUtils.h" #include "gsystem-local-alloc.h" +#include "nm-platform.h" #include "nm-sd-adapt.h" diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 604d627f52..d491e62fb9 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -25,6 +25,7 @@ #include "nm-ip4-config.h" #include "nm-utils.h" +#include "nm-platform.h" #include "nm-dbus-manager.h" #include "nm-dbus-glib-types.h" #include "nm-ip4-config-glue.h" diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index 13db237cc6..6932ab028a 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -26,6 +26,7 @@ #include "nm-glib-compat.h" #include "nm-utils.h" +#include "nm-platform.h" #include "nm-dbus-manager.h" #include "nm-dbus-glib-types.h" #include "nm-ip6-config-glue.h" diff --git a/src/nm-types.h b/src/nm-types.h index e67ae20527..4d204fc2b4 100644 --- a/src/nm-types.h +++ b/src/nm-types.h @@ -60,6 +60,49 @@ typedef struct _NMPlatformIP6Address NMPlatformIP6Address; typedef struct _NMPlatformIP6Route NMPlatformIP6Route; typedef struct _NMPlatformLink NMPlatformLink; +typedef enum { + /* Please don't interpret type numbers outside nm-platform and use functions + * like nm_platform_link_is_software() and nm_platform_supports_slaves(). + * + * type & 0x10000 -> Software device type + * type & 0x20000 -> Type supports slaves + */ + + /* No type, used as error value */ + NM_LINK_TYPE_NONE, + + /* Unknown type */ + NM_LINK_TYPE_UNKNOWN, + + /* Hardware types */ + NM_LINK_TYPE_ETHERNET, + NM_LINK_TYPE_INFINIBAND, + NM_LINK_TYPE_OLPC_MESH, + NM_LINK_TYPE_WIFI, + NM_LINK_TYPE_WWAN_ETHERNET, /* WWAN pseudo-ethernet */ + NM_LINK_TYPE_WIMAX, + + /* Software types */ + NM_LINK_TYPE_DUMMY = 0x10000, + NM_LINK_TYPE_GRE, + NM_LINK_TYPE_GRETAP, + NM_LINK_TYPE_IFB, + NM_LINK_TYPE_LOOPBACK, + NM_LINK_TYPE_MACVLAN, + NM_LINK_TYPE_MACVTAP, + NM_LINK_TYPE_OPENVSWITCH, + NM_LINK_TYPE_TAP, + NM_LINK_TYPE_TUN, + NM_LINK_TYPE_VETH, + NM_LINK_TYPE_VLAN, + NM_LINK_TYPE_VXLAN, + + /* Software types with slaves */ + NM_LINK_TYPE_BRIDGE = 0x10000 | 0x20000, + NM_LINK_TYPE_BOND, + NM_LINK_TYPE_TEAM, +} NMLinkType; + /* settings */ typedef struct _NMAgentManager NMAgentManager; typedef struct _NMSecretAgent NMSecretAgent; diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index 0ec372ff89..d6836d20ac 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -72,49 +72,6 @@ typedef enum { NM_PLATFORM_REASON_CACHE_CHECK } NMPlatformReason; -typedef enum { - /* Please don't interpret type numbers outside nm-platform and use functions - * like nm_platform_link_is_software() and nm_platform_supports_slaves(). - * - * type & 0x10000 -> Software device type - * type & 0x20000 -> Type supports slaves - */ - - /* No type, used as error value */ - NM_LINK_TYPE_NONE, - - /* Unknown type */ - NM_LINK_TYPE_UNKNOWN, - - /* Hardware types */ - NM_LINK_TYPE_ETHERNET, - NM_LINK_TYPE_INFINIBAND, - NM_LINK_TYPE_OLPC_MESH, - NM_LINK_TYPE_WIFI, - NM_LINK_TYPE_WWAN_ETHERNET, /* WWAN pseudo-ethernet */ - NM_LINK_TYPE_WIMAX, - - /* Software types */ - NM_LINK_TYPE_DUMMY = 0x10000, - NM_LINK_TYPE_GRE, - NM_LINK_TYPE_GRETAP, - NM_LINK_TYPE_IFB, - NM_LINK_TYPE_LOOPBACK, - NM_LINK_TYPE_MACVLAN, - NM_LINK_TYPE_MACVTAP, - NM_LINK_TYPE_OPENVSWITCH, - NM_LINK_TYPE_TAP, - NM_LINK_TYPE_TUN, - NM_LINK_TYPE_VETH, - NM_LINK_TYPE_VLAN, - NM_LINK_TYPE_VXLAN, - - /* Software types with slaves */ - NM_LINK_TYPE_BRIDGE = 0x10000 | 0x20000, - NM_LINK_TYPE_BOND, - NM_LINK_TYPE_TEAM, -} NMLinkType; - #define __NMPlatformObject_COMMON \ int ifindex; \ ; -- cgit v1.2.1 From 808ad85ff4f0a4905f8e81be04d04a784402bc24 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 4 Nov 2014 09:58:28 +0100 Subject: core: forward declare NMVpnConnection in nm-types.h Signed-off-by: Thomas Haller --- src/nm-types.h | 1 + src/vpn-manager/nm-vpn-connection.h | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/nm-types.h b/src/nm-types.h index 4d204fc2b4..c1c02ac60e 100644 --- a/src/nm-types.h +++ b/src/nm-types.h @@ -23,6 +23,7 @@ /* core */ typedef struct _NMActiveConnection NMActiveConnection; +typedef struct _NMVpnConnection NMVpnConnection; typedef struct _NMActRequest NMActRequest; typedef struct _NMAuthSubject NMAuthSubject; typedef struct _NMConnectionProvider NMConnectionProvider; diff --git a/src/vpn-manager/nm-vpn-connection.h b/src/vpn-manager/nm-vpn-connection.h index 74ea38a5db..31a2734e39 100644 --- a/src/vpn-manager/nm-vpn-connection.h +++ b/src/vpn-manager/nm-vpn-connection.h @@ -46,9 +46,9 @@ #define NM_VPN_CONNECTION_INTERNAL_RETRY_AFTER_FAILURE "internal-retry-after-failure" -typedef struct { +struct _NMVpnConnection { NMActiveConnection parent; -} NMVpnConnection; +}; typedef struct { NMActiveConnectionClass parent; -- cgit v1.2.1 From 627ad6f8056137244c6671eb18670ee227acb788 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 28 Aug 2014 19:03:58 +0200 Subject: keyfile: add support for G_INT64 properties Signed-off-by: Thomas Haller --- src/settings/plugins/keyfile/reader.c | 14 ++++++++++++++ src/settings/plugins/keyfile/writer.c | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c index 9d65539149..19149e0723 100644 --- a/src/settings/plugins/keyfile/reader.c +++ b/src/settings/plugins/keyfile/reader.c @@ -36,6 +36,7 @@ #include "common.h" #include "utils.h" #include "nm-core-internal.h" +#include "NetworkManagerUtils.h" /* Some setting properties also contain setting names, such as * NMSettingConnection's 'type' property (which specifies the base type of the @@ -1021,6 +1022,7 @@ read_one_setting_value (NMSetting *setting, { ReadInfo *info = user_data; const char *setting_name; + int errsv; GType type; GError *err = NULL; gboolean check_for_key = TRUE; @@ -1121,6 +1123,18 @@ read_one_setting_value (NMSetting *setting, uint_val = g_ascii_strtoull (tmp_str, NULL, 10); g_free (tmp_str); g_object_set (setting, key, uint_val, NULL); + } else if (type == G_TYPE_INT64) { + char *tmp_str; + gint64 int_val; + + tmp_str = nm_keyfile_plugin_kf_get_value (info->keyfile, setting_name, key, NULL); + int_val = nm_utils_ascii_str_to_int64 (tmp_str, 10, G_MININT64, G_MAXINT64, 0); + errsv = errno; + if (errsv) + nm_log_warn (LOGD_SETTINGS, "Invalid int64 value (%s)", tmp_str); + else + g_object_set (setting, key, int_val, NULL); + g_free (tmp_str); } else if (type == G_TYPE_BYTES) { gint *tmp; GByteArray *array; diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c index 77c996095b..02f2d7ff77 100644 --- a/src/settings/plugins/keyfile/writer.c +++ b/src/settings/plugins/keyfile/writer.c @@ -722,6 +722,12 @@ write_setting_value (NMSetting *setting, numstr = g_strdup_printf ("%" G_GUINT64_FORMAT, g_value_get_uint64 (value)); nm_keyfile_plugin_kf_set_value (info->keyfile, setting_name, key, numstr); g_free (numstr); + } else if (type == G_TYPE_INT64) { + char *numstr; + + numstr = g_strdup_printf ("%" G_GINT64_FORMAT, g_value_get_int64 (value)); + nm_keyfile_plugin_kf_set_value (info->keyfile, setting_name, key, numstr); + g_free (numstr); } else if (type == G_TYPE_BOOLEAN) { nm_keyfile_plugin_kf_set_boolean (info->keyfile, setting_name, key, g_value_get_boolean (value)); } else if (type == G_TYPE_CHAR) { -- cgit v1.2.1 From ce7fc351dbfca50eaf231779697fcdf4d7a2d04a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 27 Aug 2014 19:57:24 +0200 Subject: libnm: add NMSettingIPConfig:route-metric https://bugzilla.gnome.org/show_bug.cgi?id=735512 https://bugzilla.redhat.com/show_bug.cgi?id=663730 Signed-off-by: Thomas Haller --- libnm-core/nm-setting-ip-config.c | 49 ++++++++++++++++++++++++++++++++++++ libnm-core/nm-setting-ip-config.h | 3 +++ libnm-core/tests/test-general.c | 3 ++- libnm-util/generate-setting-docs.py | 6 ++++- libnm-util/libnm-util.ver | 2 ++ libnm-util/nm-setting-ip4-config.c | 50 +++++++++++++++++++++++++++++++++++++ libnm-util/nm-setting-ip4-config.h | 4 +++ libnm-util/nm-setting-ip6-config.c | 50 +++++++++++++++++++++++++++++++++++++ libnm-util/nm-setting-ip6-config.h | 4 +++ libnm-util/tests/test-general.c | 1 + libnm/libnm.ver | 1 + 11 files changed, 171 insertions(+), 2 deletions(-) diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c index 8d0504e2b1..0a781798fe 100644 --- a/libnm-core/nm-setting-ip-config.c +++ b/libnm-core/nm-setting-ip-config.c @@ -1058,6 +1058,7 @@ typedef struct { GPtrArray *dns_search; /* array of domain name strings */ GPtrArray *addresses; /* array of NMIPAddress */ GPtrArray *routes; /* array of NMIPRoute */ + gint64 route_metric; char *gateway; gboolean ignore_auto_routes; gboolean ignore_auto_dns; @@ -1075,6 +1076,7 @@ enum { PROP_ADDRESSES, PROP_GATEWAY, PROP_ROUTES, + PROP_ROUTE_METRIC, PROP_IGNORE_AUTO_ROUTES, PROP_IGNORE_AUTO_DNS, PROP_DHCP_HOSTNAME, @@ -1671,6 +1673,25 @@ nm_setting_ip_config_clear_routes (NMSettingIPConfig *setting) g_object_notify (G_OBJECT (setting), NM_SETTING_IP_CONFIG_ROUTES); } +/** + * nm_setting_ip_config_get_route_metric: + * @setting: the #NMSettingIPConfig + * + * Returns the value contained in the #NMSettingIPConfig:route-metric + * property. + * + * Returns: the route metric that is used for routes that don't explicitly + * specify a metric. See #NMSettingIPConfig:route-metric for more details. + **/ +gint64 +nm_setting_ip_config_get_route_metric (NMSettingIPConfig *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP_CONFIG (setting), -1); + + return NM_SETTING_IP_CONFIG_GET_PRIVATE (setting)->route_metric; +} + + /** * nm_setting_ip_config_get_ignore_auto_routes: * @setting: the #NMSettingIPConfig @@ -1984,6 +2005,9 @@ set_property (GObject *object, guint prop_id, (NMUtilsCopyFunc) nm_ip_route_dup, (GDestroyNotify) nm_ip_route_unref); break; + case PROP_ROUTE_METRIC: + priv->route_metric = g_value_get_int64 (value); + break; case PROP_IGNORE_AUTO_ROUTES: priv->ignore_auto_routes = g_value_get_boolean (value); break; @@ -2039,6 +2063,9 @@ get_property (GObject *object, guint prop_id, (NMUtilsCopyFunc) nm_ip_route_dup, (GDestroyNotify) nm_ip_route_unref)); break; + case PROP_ROUTE_METRIC: + g_value_set_int64 (value, priv->route_metric); + break; case PROP_IGNORE_AUTO_ROUTES: g_value_set_boolean (value, nm_setting_ip_config_get_ignore_auto_routes (setting)); break; @@ -2173,6 +2200,28 @@ nm_setting_ip_config_class_init (NMSettingIPConfigClass *setting_class) NM_SETTING_PARAM_INFERRABLE | G_PARAM_STATIC_STRINGS)); + /** + * NMSettingIPConfig:route-metric: + * + * The default metric for routes that don't explicitly specify a metric. + * The default value -1 means that the metric is choosen automatically + * based on the device type. + * The metric applies to dynamic routes, manual (static) routes that + * don't have an explicit metric setting, address prefix routes, and + * the default route. + * Note that for IPv6, the kernel accepts accepts zero (0) but coerces + * it to 1024 (user default). Hence, setting this value to zero effectively + * mean setting it to 1024. + * For IPv4, zero is a regular value for the metric. + **/ + g_object_class_install_property + (object_class, PROP_ROUTE_METRIC, + g_param_spec_int64 (NM_SETTING_IP_CONFIG_ROUTE_METRIC, "", "", + -1, G_MAXUINT32, -1, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); + /** * NMSettingIPConfig:ignore-auto-routes: * diff --git a/libnm-core/nm-setting-ip-config.h b/libnm-core/nm-setting-ip-config.h index fba226c331..12763fa857 100644 --- a/libnm-core/nm-setting-ip-config.h +++ b/libnm-core/nm-setting-ip-config.h @@ -136,6 +136,7 @@ void nm_ip_route_set_attribute (NMIPRoute *route, #define NM_SETTING_IP_CONFIG_ADDRESSES "addresses" #define NM_SETTING_IP_CONFIG_GATEWAY "gateway" #define NM_SETTING_IP_CONFIG_ROUTES "routes" +#define NM_SETTING_IP_CONFIG_ROUTE_METRIC "route-metric" #define NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES "ignore-auto-routes" #define NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS "ignore-auto-dns" #define NM_SETTING_IP_CONFIG_DHCP_HOSTNAME "dhcp-hostname" @@ -204,6 +205,8 @@ gboolean nm_setting_ip_config_remove_route_by_value (NMSettingIPConfig NMIPRoute *route); void nm_setting_ip_config_clear_routes (NMSettingIPConfig *setting); +gint64 nm_setting_ip_config_get_route_metric (NMSettingIPConfig *setting); + gboolean nm_setting_ip_config_get_ignore_auto_routes (NMSettingIPConfig *setting); gboolean nm_setting_ip_config_get_ignore_auto_dns (NMSettingIPConfig *setting); diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 09e05856c8..2c77726654 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -1701,9 +1701,10 @@ test_connection_diff_a_only (void) { NM_SETTING_IP_CONFIG_ADDRESSES, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP_CONFIG_GATEWAY, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP_CONFIG_ROUTES, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP_CONFIG_ROUTE_METRIC, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, NM_SETTING_DIFF_RESULT_IN_A }, - { NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP_CONFIG_NEVER_DEFAULT, NM_SETTING_DIFF_RESULT_IN_A }, diff --git a/libnm-util/generate-setting-docs.py b/libnm-util/generate-setting-docs.py index 69b115a7e1..463d0f2ec7 100755 --- a/libnm-util/generate-setting-docs.py +++ b/libnm-util/generate-setting-docs.py @@ -48,7 +48,11 @@ identifier_key = '{%s}identifier' % ns_map['c'] nick_key = '{%s}nick' % ns_map['glib'] symbol_prefix_key = '{%s}symbol-prefix' % ns_map['c'] -constants = { 'TRUE': 'TRUE', 'FALSE': 'FALSE', 'NULL': 'NULL' } +constants = { + 'TRUE': 'TRUE', + 'FALSE': 'FALSE', + 'G_MAXUINT32': 'G_MAXUINT32', + 'NULL': 'NULL' } setting_names = {} def init_constants(girxml): diff --git a/libnm-util/libnm-util.ver b/libnm-util/libnm-util.ver index c6fc0d7f69..794cc87f43 100644 --- a/libnm-util/libnm-util.ver +++ b/libnm-util/libnm-util.ver @@ -367,6 +367,7 @@ global: nm_setting_ip4_config_get_num_dns_searches; nm_setting_ip4_config_get_num_routes; nm_setting_ip4_config_get_route; + nm_setting_ip4_config_get_route_metric; nm_setting_ip4_config_get_type; nm_setting_ip4_config_new; nm_setting_ip4_config_remove_address; @@ -402,6 +403,7 @@ global: nm_setting_ip6_config_get_num_dns_searches; nm_setting_ip6_config_get_num_routes; nm_setting_ip6_config_get_route; + nm_setting_ip6_config_get_route_metric; nm_setting_ip6_config_get_type; nm_setting_ip6_config_new; nm_setting_ip6_config_privacy_get_type; diff --git a/libnm-util/nm-setting-ip4-config.c b/libnm-util/nm-setting-ip4-config.c index f7f709ec54..c7a730769c 100644 --- a/libnm-util/nm-setting-ip4-config.c +++ b/libnm-util/nm-setting-ip4-config.c @@ -76,6 +76,7 @@ typedef struct { GSList *dns_search; /* list of strings */ GSList *addresses; /* array of NMIP4Address */ GSList *routes; /* array of NMIP4Route */ + gint64 route_metric; gboolean ignore_auto_routes; gboolean ignore_auto_dns; char *dhcp_client_id; @@ -92,6 +93,7 @@ enum { PROP_DNS_SEARCH, PROP_ADDRESSES, PROP_ROUTES, + PROP_ROUTE_METRIC, PROP_IGNORE_AUTO_ROUTES, PROP_IGNORE_AUTO_DNS, PROP_DHCP_CLIENT_ID, @@ -688,6 +690,26 @@ nm_setting_ip4_config_clear_routes (NMSettingIP4Config *setting) g_object_notify (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_ROUTES); } +/** + * nm_setting_ip4_config_get_route_metric: + * @setting: the #NMSettingIP4Config + * + * Returns the value contained in the #NMSettingIP4Config:route-metric + * property. + * + * Returns: the route metric that is used for IPv4 routes that don't explicitly + * specify a metric. See #NMSettingIP4Config:route-metric for more details. + * + * Since: 1.0 + **/ +gint64 +nm_setting_ip4_config_get_route_metric (NMSettingIP4Config *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), -1); + + return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->route_metric; +} + /** * nm_setting_ip4_config_get_ignore_auto_routes: * @setting: the #NMSettingIP4Config @@ -1018,6 +1040,9 @@ set_property (GObject *object, guint prop_id, g_slist_free_full (priv->routes, (GDestroyNotify) nm_ip4_route_unref); priv->routes = nm_utils_ip4_routes_from_gvalue (value); break; + case PROP_ROUTE_METRIC: + priv->route_metric = g_value_get_int64 (value); + break; case PROP_IGNORE_AUTO_ROUTES: priv->ignore_auto_routes = g_value_get_boolean (value); break; @@ -1070,6 +1095,9 @@ get_property (GObject *object, guint prop_id, case PROP_ROUTES: nm_utils_ip4_routes_to_gvalue (priv->routes, value); break; + case PROP_ROUTE_METRIC: + g_value_set_int64 (value, priv->route_metric); + break; case PROP_IGNORE_AUTO_ROUTES: g_value_set_boolean (value, nm_setting_ip4_config_get_ignore_auto_routes (setting)); break; @@ -1275,6 +1303,28 @@ nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *setting_class) NM_SETTING_PARAM_INFERRABLE | G_PARAM_STATIC_STRINGS)); + /** + * NMSettingIP4Config:route-metric: + * + * The default metric for routes that don't explicitly specify a metric. + * The default value -1 means that the metric is choosen automatically + * based on the device type. + * The metric applies to dynamic routes, manual (static) routes that + * don't have an explicit metric setting, address prefix routes, and + * the default route. + * As the linux kernel accepts zero (0) as a valid metric, zero is + * a valid value. + * + * Since: 1.0 + **/ + g_object_class_install_property + (object_class, PROP_ROUTE_METRIC, + g_param_spec_int64 (NM_SETTING_IP4_CONFIG_ROUTE_METRIC, "", "", + -1, G_MAXUINT32, -1, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); + /** * NMSettingIP4Config:ignore-auto-routes: * diff --git a/libnm-util/nm-setting-ip4-config.h b/libnm-util/nm-setting-ip4-config.h index 1b7c23072d..6e9a9a227a 100644 --- a/libnm-util/nm-setting-ip4-config.h +++ b/libnm-util/nm-setting-ip4-config.h @@ -60,6 +60,7 @@ GQuark nm_setting_ip4_config_error_quark (void); #define NM_SETTING_IP4_CONFIG_DNS_SEARCH "dns-search" #define NM_SETTING_IP4_CONFIG_ADDRESSES "addresses" #define NM_SETTING_IP4_CONFIG_ROUTES "routes" +#define NM_SETTING_IP4_CONFIG_ROUTE_METRIC "route-metric" #define NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES "ignore-auto-routes" #define NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS "ignore-auto-dns" #define NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID "dhcp-client-id" @@ -214,6 +215,9 @@ NM_AVAILABLE_IN_0_9_10 gboolean nm_setting_ip4_config_remove_route_by_value (NMSettingIP4Config *setting, NMIP4Route *route); void nm_setting_ip4_config_clear_routes (NMSettingIP4Config *setting); +NM_AVAILABLE_IN_1_0 +gint64 nm_setting_ip4_config_get_route_metric (NMSettingIP4Config *setting); + gboolean nm_setting_ip4_config_get_ignore_auto_routes (NMSettingIP4Config *setting); gboolean nm_setting_ip4_config_get_ignore_auto_dns (NMSettingIP4Config *setting); const char * nm_setting_ip4_config_get_dhcp_client_id (NMSettingIP4Config *setting); diff --git a/libnm-util/nm-setting-ip6-config.c b/libnm-util/nm-setting-ip6-config.c index ec90fd2bd7..64a81136a9 100644 --- a/libnm-util/nm-setting-ip6-config.c +++ b/libnm-util/nm-setting-ip6-config.c @@ -75,6 +75,7 @@ typedef struct { GSList *dns_search; /* list of strings */ GSList *addresses; /* array of NMIP6Address */ GSList *routes; /* array of NMIP6Route */ + gint64 route_metric; gboolean ignore_auto_routes; gboolean ignore_auto_dns; gboolean never_default; @@ -91,6 +92,7 @@ enum { PROP_DNS_SEARCH, PROP_ADDRESSES, PROP_ROUTES, + PROP_ROUTE_METRIC, PROP_IGNORE_AUTO_ROUTES, PROP_IGNORE_AUTO_DNS, PROP_NEVER_DEFAULT, @@ -708,6 +710,26 @@ nm_setting_ip6_config_clear_routes (NMSettingIP6Config *setting) g_object_notify (G_OBJECT (setting), NM_SETTING_IP6_CONFIG_ROUTES); } +/** + * nm_setting_ip6_config_get_route_metric: + * @setting: the #NMSettingIP4Config + * + * Returns the value contained in the #NMSettingIP6Config:route-metric + * property. + * + * Returns: the route metric that is used for IPv6 routes that don't explicitly + * specify a metric. See #NMSettingIP6Config:route-metric for more details. + * + * Since: 1.0 + **/ +gint64 +nm_setting_ip6_config_get_route_metric (NMSettingIP6Config *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_IP6_CONFIG (setting), -1); + + return NM_SETTING_IP6_CONFIG_GET_PRIVATE (setting)->route_metric; +} + /** * nm_setting_ip6_config_get_ignore_auto_routes: * @setting: the #NMSettingIP6Config @@ -927,6 +949,9 @@ set_property (GObject *object, guint prop_id, g_slist_free_full (priv->routes, g_free); priv->routes = nm_utils_ip6_routes_from_gvalue (value); break; + case PROP_ROUTE_METRIC: + priv->route_metric = g_value_get_int64 (value); + break; case PROP_IGNORE_AUTO_ROUTES: priv->ignore_auto_routes = g_value_get_boolean (value); break; @@ -974,6 +999,9 @@ get_property (GObject *object, guint prop_id, case PROP_ROUTES: nm_utils_ip6_routes_to_gvalue (priv->routes, value); break; + case PROP_ROUTE_METRIC: + g_value_set_int64 (value, priv->route_metric); + break; case PROP_IGNORE_AUTO_ROUTES: g_value_set_boolean (value, priv->ignore_auto_routes); break; @@ -1186,6 +1214,28 @@ nm_setting_ip6_config_class_init (NMSettingIP6ConfigClass *setting_class) NM_SETTING_PARAM_INFERRABLE | G_PARAM_STATIC_STRINGS)); + /** + * NMSettingIP6Config:route-metric: + * + * The default metric for routes that don't explicitly specify a metric. + * The default value -1 means that the metric is choosen automatically + * based on the device type. + * The metric applies to dynamic routes, manual (static) routes that + * don't have an explicit metric setting, address prefix routes, and + * the default route. + * As the linux kernel replaces zero (0) by 1024 (user-default), setting + * this property to 0 means effectively setting it to 1024. + * + * Since: 1.0 + **/ + g_object_class_install_property + (object_class, PROP_ROUTE_METRIC, + g_param_spec_int64 (NM_SETTING_IP6_CONFIG_ROUTE_METRIC, "", "", + -1, G_MAXUINT32, -1, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT | + G_PARAM_STATIC_STRINGS)); + /** * NMSettingIP6Config:ignore-auto-routes: * diff --git a/libnm-util/nm-setting-ip6-config.h b/libnm-util/nm-setting-ip6-config.h index 4486a40e26..0a2f00ddcc 100644 --- a/libnm-util/nm-setting-ip6-config.h +++ b/libnm-util/nm-setting-ip6-config.h @@ -61,6 +61,7 @@ GQuark nm_setting_ip6_config_error_quark (void); #define NM_SETTING_IP6_CONFIG_DNS_SEARCH "dns-search" #define NM_SETTING_IP6_CONFIG_ADDRESSES "addresses" #define NM_SETTING_IP6_CONFIG_ROUTES "routes" +#define NM_SETTING_IP6_CONFIG_ROUTE_METRIC "route-metric" #define NM_SETTING_IP6_CONFIG_IGNORE_AUTO_ROUTES "ignore-auto-routes" #define NM_SETTING_IP6_CONFIG_IGNORE_AUTO_DNS "ignore-auto-dns" #define NM_SETTING_IP6_CONFIG_NEVER_DEFAULT "never-default" @@ -245,6 +246,9 @@ gboolean nm_setting_ip6_config_remove_route_by_value (NMSettingIP void nm_setting_ip6_config_clear_routes (NMSettingIP6Config *setting); gboolean nm_setting_ip6_config_get_ignore_auto_routes (NMSettingIP6Config *setting); +NM_AVAILABLE_IN_1_0 +gint64 nm_setting_ip6_config_get_route_metric (NMSettingIP6Config *setting); + gboolean nm_setting_ip6_config_get_ignore_auto_dns (NMSettingIP6Config *setting); const char * nm_setting_ip6_config_get_dhcp_hostname (NMSettingIP6Config *setting); gboolean nm_setting_ip6_config_get_never_default (NMSettingIP6Config *setting); diff --git a/libnm-util/tests/test-general.c b/libnm-util/tests/test-general.c index bdec7e1354..eea22b5f3b 100644 --- a/libnm-util/tests/test-general.c +++ b/libnm-util/tests/test-general.c @@ -1268,6 +1268,7 @@ test_connection_diff_a_only (void) { NM_SETTING_IP4_CONFIG_DNS_SEARCH, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP4_CONFIG_ADDRESSES, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP4_CONFIG_ROUTES, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_IP4_CONFIG_ROUTE_METRIC, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, NM_SETTING_DIFF_RESULT_IN_A }, diff --git a/libnm/libnm.ver b/libnm/libnm.ver index 8d9ca07834..baf83ca548 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -572,6 +572,7 @@ global: nm_setting_ip_config_get_num_dns_searches; nm_setting_ip_config_get_num_routes; nm_setting_ip_config_get_route; + nm_setting_ip_config_get_route_metric; nm_setting_ip_config_get_type; nm_setting_ip_config_remove_address; nm_setting_ip_config_remove_address_by_value; -- cgit v1.2.1 From 7a55191079b03e15bc57d2c0f112967e757de725 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 28 Aug 2014 18:51:11 +0200 Subject: cli: add utility methods for gint64 types Signed-off-by: Thomas Haller --- clients/cli/settings.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/clients/cli/settings.c b/clients/cli/settings.c index e4d7589385..8b11487f8d 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -1949,6 +1949,27 @@ validate_int (NMSetting *setting, const char* prop, gint val, GError **error) return success; } +static gboolean +validate_int64 (NMSetting *setting, const char* prop, gint64 val, GError **error) +{ + GParamSpec *pspec; + GValue value = G_VALUE_INIT; + gboolean success = TRUE; + + g_value_init (&value, G_TYPE_INT64); + g_value_set_int64 (&value, val); + pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (setting)), prop); + g_assert (G_IS_PARAM_SPEC (pspec)); + if (g_param_value_validate (pspec, &value)) { + GParamSpecInt64 *pspec_int = (GParamSpecInt64 *) pspec; + g_set_error (error, 1, 0, _("'%"G_GINT64_FORMAT"' is not valid; use <%"G_GINT64_FORMAT"-%"G_GINT64_FORMAT">"), + val, pspec_int->minimum, pspec_int->maximum); + success = FALSE; + } + g_value_unset (&value); + return success; +} + /* Validate 'val' number against to uint property spec */ static gboolean validate_uint (NMSetting *setting, const char* prop, guint val, GError **error) @@ -2176,6 +2197,26 @@ nmc_property_set_int (NMSetting *setting, const char *prop, const char *val, GEr return TRUE; } +static gboolean +nmc_property_set_int64 (NMSetting *setting, const char *prop, const char *val, GError **error) +{ + long val_int; + + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + if (!nmc_string_to_int (val, FALSE, 0, 0, &val_int)) { + g_set_error (error, 1, 0, _("'%s' is not a valid number (or out of range)"), val); + return FALSE; + } + + /* Validate the number according to the property spec */ + if (!validate_int64 (setting, prop, (gint64) val_int, error)) + return FALSE; + + g_object_set (setting, prop, (gint64) val_int, NULL); + return TRUE; +} + static gboolean nmc_property_set_bool (NMSetting *setting, const char *prop, const char *val, GError **error) { -- cgit v1.2.1 From 36d99dbed5839633b19c43daaeedff1a62b0b19b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 28 Aug 2014 18:42:58 +0200 Subject: cli: support new property NM_SETTING_IP_CONFIG_ROUTE_METRIC Signed-off-by: Thomas Haller --- clients/cli/settings.c | 80 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/clients/cli/settings.c b/clients/cli/settings.c index 8b11487f8d..2c6be705dc 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -258,14 +258,15 @@ NmcOutputField nmc_fields_setting_ip4_config[] = { SETTING_FIELD (NM_SETTING_IP_CONFIG_DNS_SEARCH, 15), /* 3 */ SETTING_FIELD (NM_SETTING_IP_CONFIG_ADDRESSES, 20), /* 4 */ SETTING_FIELD (NM_SETTING_IP_CONFIG_GATEWAY, 20), /* 5 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_ROUTES, 20), /* 7 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, 19), /* 7 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, 16), /* 8 */ - SETTING_FIELD (NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, 15), /* 9 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, 19), /* 10 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, 14), /* 11 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_NEVER_DEFAULT, 15), /* 12 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_MAY_FAIL, 12), /* 13 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_ROUTES, 20), /* 6 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_ROUTE_METRIC, 15), /* 7 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, 19), /* 8 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, 16), /* 9 */ + SETTING_FIELD (NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, 15), /* 10 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, 19), /* 11 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, 14), /* 12 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_NEVER_DEFAULT, 15), /* 13 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_MAY_FAIL, 12), /* 14 */ {NULL, NULL, 0, NULL, FALSE, FALSE, 0} }; #define NMC_FIELDS_SETTING_IP4_CONFIG_ALL "name"","\ @@ -275,6 +276,7 @@ NmcOutputField nmc_fields_setting_ip4_config[] = { NM_SETTING_IP_CONFIG_ADDRESSES","\ NM_SETTING_IP_CONFIG_GATEWAY","\ NM_SETTING_IP_CONFIG_ROUTES","\ + NM_SETTING_IP_CONFIG_ROUTE_METRIC","\ NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES","\ NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS","\ NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID","\ @@ -293,13 +295,14 @@ NmcOutputField nmc_fields_setting_ip6_config[] = { SETTING_FIELD (NM_SETTING_IP_CONFIG_ADDRESSES, 20), /* 4 */ SETTING_FIELD (NM_SETTING_IP_CONFIG_GATEWAY, 20), /* 5 */ SETTING_FIELD (NM_SETTING_IP_CONFIG_ROUTES, 20), /* 6 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, 19), /* 7 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, 16), /* 8 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_NEVER_DEFAULT, 15), /* 9 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_MAY_FAIL, 12), /* 10 */ - SETTING_FIELD (NM_SETTING_IP6_CONFIG_IP6_PRIVACY, 15), /* 11 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, 19), /* 12 */ - SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, 14), /* 13 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_ROUTE_METRIC, 15), /* 7 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES, 19), /* 8 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS, 16), /* 9 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_NEVER_DEFAULT, 15), /* 10 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_MAY_FAIL, 12), /* 11 */ + SETTING_FIELD (NM_SETTING_IP6_CONFIG_IP6_PRIVACY, 15), /* 12 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_SEND_HOSTNAME, 19), /* 13 */ + SETTING_FIELD (NM_SETTING_IP_CONFIG_DHCP_HOSTNAME, 14), /* 14 */ {NULL, NULL, 0, NULL, FALSE, FALSE, 0} }; #define NMC_FIELDS_SETTING_IP6_CONFIG_ALL "name"","\ @@ -309,6 +312,7 @@ NmcOutputField nmc_fields_setting_ip6_config[] = { NM_SETTING_IP_CONFIG_ADDRESSES","\ NM_SETTING_IP_CONFIG_GATEWAY","\ NM_SETTING_IP_CONFIG_ROUTES","\ + NM_SETTING_IP_CONFIG_ROUTE_METRIC","\ NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES","\ NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS","\ NM_SETTING_IP_CONFIG_NEVER_DEFAULT","\ @@ -1273,6 +1277,7 @@ nmc_property_ipv4_get_routes (NMSetting *setting) } DEFINE_GETTER (nmc_property_ipv4_get_gateway, NM_SETTING_IP_CONFIG_GATEWAY) +DEFINE_GETTER (nmc_property_ipv4_get_route_metric, NM_SETTING_IP_CONFIG_ROUTE_METRIC) DEFINE_GETTER (nmc_property_ipv4_get_ignore_auto_routes, NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES) DEFINE_GETTER (nmc_property_ipv4_get_ignore_auto_dns, NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS) DEFINE_GETTER (nmc_property_ipv4_get_dhcp_client_id, NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID) @@ -1324,6 +1329,7 @@ nmc_property_ipv6_get_routes (NMSetting *setting) } DEFINE_GETTER (nmc_property_ipv6_get_gateway, NM_SETTING_IP_CONFIG_GATEWAY) +DEFINE_GETTER (nmc_property_ipv6_get_route_metric, NM_SETTING_IP_CONFIG_ROUTE_METRIC) DEFINE_GETTER (nmc_property_ipv6_get_ignore_auto_routes, NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES) DEFINE_GETTER (nmc_property_ipv6_get_ignore_auto_dns, NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS) DEFINE_GETTER (nmc_property_ipv6_get_never_default, NM_SETTING_IP_CONFIG_NEVER_DEFAULT) @@ -5471,6 +5477,13 @@ nmc_properties_init (void) nmc_property_ipv4_describe_routes, NULL, nmc_property_out2in_routes); + nmc_add_prop_funcs (GLUE_IP (4, ROUTE_METRIC), + nmc_property_ipv4_get_route_metric, + nmc_property_set_int64, + NULL, + NULL, + NULL, + NULL); nmc_add_prop_funcs (GLUE_IP (4, IGNORE_AUTO_ROUTES), nmc_property_ipv4_get_ignore_auto_routes, nmc_property_set_bool, @@ -5564,6 +5577,13 @@ nmc_properties_init (void) nmc_property_ipv6_describe_routes, NULL, nmc_property_out2in_routes); + nmc_add_prop_funcs (GLUE_IP (6, ROUTE_METRIC), + nmc_property_ipv6_get_route_metric, + nmc_property_set_int64, + NULL, + NULL, + NULL, + NULL); nmc_add_prop_funcs (GLUE_IP (6, IGNORE_AUTO_ROUTES), nmc_property_ipv6_get_ignore_auto_routes, nmc_property_set_bool, @@ -6766,13 +6786,14 @@ setting_ip4_config_details (NMSetting *setting, NmCli *nmc, const char *one_pro set_val_str (arr, 4, nmc_property_ip_get_addresses (setting)); set_val_str (arr, 5, nmc_property_ipv4_get_gateway (setting)); set_val_str (arr, 6, nmc_property_ipv4_get_routes (setting)); - set_val_str (arr, 7, nmc_property_ipv4_get_ignore_auto_routes (setting)); - set_val_str (arr, 8, nmc_property_ipv4_get_ignore_auto_dns (setting)); - set_val_str (arr, 9, nmc_property_ipv4_get_dhcp_client_id (setting)); - set_val_str (arr, 10, nmc_property_ipv4_get_dhcp_send_hostname (setting)); - set_val_str (arr, 11, nmc_property_ipv4_get_dhcp_hostname (setting)); - set_val_str (arr, 12, nmc_property_ipv4_get_never_default (setting)); - set_val_str (arr, 13, nmc_property_ipv4_get_may_fail (setting)); + set_val_str (arr, 7, nmc_property_ipv4_get_route_metric (setting)); + set_val_str (arr, 8, nmc_property_ipv4_get_ignore_auto_routes (setting)); + set_val_str (arr, 9, nmc_property_ipv4_get_ignore_auto_dns (setting)); + set_val_str (arr, 10, nmc_property_ipv4_get_dhcp_client_id (setting)); + set_val_str (arr, 11, nmc_property_ipv4_get_dhcp_send_hostname (setting)); + set_val_str (arr, 12, nmc_property_ipv4_get_dhcp_hostname (setting)); + set_val_str (arr, 13, nmc_property_ipv4_get_never_default (setting)); + set_val_str (arr, 14, nmc_property_ipv4_get_may_fail (setting)); g_ptr_array_add (nmc->output_data, arr); print_data (nmc); /* Print all data */ @@ -6804,13 +6825,14 @@ setting_ip6_config_details (NMSetting *setting, NmCli *nmc, const char *one_pro set_val_str (arr, 4, nmc_property_ip_get_addresses (setting)); set_val_str (arr, 5, nmc_property_ipv6_get_gateway (setting)); set_val_str (arr, 6, nmc_property_ipv6_get_routes (setting)); - set_val_str (arr, 7, nmc_property_ipv6_get_ignore_auto_routes (setting)); - set_val_str (arr, 8, nmc_property_ipv6_get_ignore_auto_dns (setting)); - set_val_str (arr, 9, nmc_property_ipv6_get_never_default (setting)); - set_val_str (arr, 10, nmc_property_ipv6_get_may_fail (setting)); - set_val_str (arr, 11, nmc_property_ipv6_get_ip6_privacy (setting)); - set_val_str (arr, 12, nmc_property_ipv6_get_dhcp_send_hostname (setting)); - set_val_str (arr, 13, nmc_property_ipv6_get_dhcp_hostname (setting)); + set_val_str (arr, 7, nmc_property_ipv6_get_route_metric (setting)); + set_val_str (arr, 8, nmc_property_ipv6_get_ignore_auto_routes (setting)); + set_val_str (arr, 9, nmc_property_ipv6_get_ignore_auto_dns (setting)); + set_val_str (arr, 10, nmc_property_ipv6_get_never_default (setting)); + set_val_str (arr, 11, nmc_property_ipv6_get_may_fail (setting)); + set_val_str (arr, 12, nmc_property_ipv6_get_ip6_privacy (setting)); + set_val_str (arr, 13, nmc_property_ipv6_get_dhcp_send_hostname (setting)); + set_val_str (arr, 14, nmc_property_ipv6_get_dhcp_hostname (setting)); g_ptr_array_add (nmc->output_data, arr); print_data (nmc); /* Print all data */ -- cgit v1.2.1 From 52ddd72bde2324d2ecd78d03b50391e491ffcbec Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 28 Aug 2014 17:25:36 +0200 Subject: core: cleanup type of route metric to ensure guint32 Kernel, netlink an NMPlatformRoute treat route metrics as uint32. Fix several places to use the exact type. Signed-off-by: Thomas Haller --- src/dhcp-manager/nm-dhcp-client.c | 4 ++-- src/dhcp-manager/nm-dhcp-manager.c | 6 +++--- src/dhcp-manager/nm-dhcp-manager.h | 4 ++-- src/dhcp-manager/nm-dhcp-utils.c | 12 ++++++------ src/nm-ip4-config.c | 2 +- src/nm-ip4-config.h | 2 +- src/nm-ip6-config.c | 2 +- src/nm-ip6-config.h | 2 +- src/platform/nm-fake-platform.c | 16 ++++++++-------- src/platform/nm-linux-platform.c | 18 +++++++++--------- src/platform/nm-platform.c | 24 ++++++++++-------------- src/platform/nm-platform.h | 29 ++++++++++++++--------------- src/vpn-manager/nm-vpn-connection.c | 2 +- 13 files changed, 59 insertions(+), 64 deletions(-) diff --git a/src/dhcp-manager/nm-dhcp-client.c b/src/dhcp-manager/nm-dhcp-client.c index 085f7f3655..b6b571eb5f 100644 --- a/src/dhcp-manager/nm-dhcp-client.c +++ b/src/dhcp-manager/nm-dhcp-client.c @@ -42,7 +42,7 @@ typedef struct { GByteArray * hwaddr; gboolean ipv6; char * uuid; - guint priority; + guint32 priority; guint32 timeout; GByteArray * duid; @@ -885,7 +885,7 @@ nm_dhcp_client_class_init (NMDhcpClientClass *client_class) g_object_class_install_property (object_class, PROP_PRIORITY, g_param_spec_uint (NM_DHCP_CLIENT_PRIORITY, "", "", - 0, G_MAXUINT, 0, + 0, G_MAXUINT32, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); diff --git a/src/dhcp-manager/nm-dhcp-manager.c b/src/dhcp-manager/nm-dhcp-manager.c index 2df3cb108c..5c139b8ca2 100644 --- a/src/dhcp-manager/nm-dhcp-manager.c +++ b/src/dhcp-manager/nm-dhcp-manager.c @@ -359,7 +359,7 @@ client_start (NMDhcpManager *self, int ifindex, const GByteArray *hwaddr, const char *uuid, - guint priority, + guint32 priority, gboolean ipv6, const char *dhcp_client_id, guint32 timeout, @@ -433,7 +433,7 @@ nm_dhcp_manager_start_ip4 (NMDhcpManager *self, int ifindex, const GByteArray *hwaddr, const char *uuid, - guint priority, + guint32 priority, gboolean send_hostname, const char *dhcp_hostname, const char *dhcp_client_id, @@ -458,7 +458,7 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self, int ifindex, const GByteArray *hwaddr, const char *uuid, - guint priority, + guint32 priority, gboolean send_hostname, const char *dhcp_hostname, guint32 timeout, diff --git a/src/dhcp-manager/nm-dhcp-manager.h b/src/dhcp-manager/nm-dhcp-manager.h index 0228130e0f..0c3c3f2506 100644 --- a/src/dhcp-manager/nm-dhcp-manager.h +++ b/src/dhcp-manager/nm-dhcp-manager.h @@ -56,7 +56,7 @@ NMDhcpClient * nm_dhcp_manager_start_ip4 (NMDhcpManager *manager, int ifindex, const GByteArray *hwaddr, const char *uuid, - guint priority, + guint32 priority, gboolean send_hostname, const char *dhcp_hostname, const char *dhcp_client_id, @@ -68,7 +68,7 @@ NMDhcpClient * nm_dhcp_manager_start_ip6 (NMDhcpManager *manager, int ifindex, const GByteArray *hwaddr, const char *uuid, - guint priority, + guint32 priority, gboolean send_hostname, const char *dhcp_hostname, guint32 timeout, diff --git a/src/dhcp-manager/nm-dhcp-utils.c b/src/dhcp-manager/nm-dhcp-utils.c index 424d93db11..d9eb85df86 100644 --- a/src/dhcp-manager/nm-dhcp-utils.c +++ b/src/dhcp-manager/nm-dhcp-utils.c @@ -34,7 +34,7 @@ static gboolean ip4_process_dhcpcd_rfc3442_routes (const char *str, - guint priority, + guint32 priority, NMIP4Config *ip4_config, guint32 *gwaddr) { @@ -161,7 +161,7 @@ error: static gboolean ip4_process_dhclient_rfc3442_routes (const char *str, - guint priority, + guint32 priority, NMIP4Config *ip4_config, guint32 *gwaddr) { @@ -209,7 +209,7 @@ out: static gboolean ip4_process_classless_routes (GHashTable *options, - guint priority, + guint32 priority, NMIP4Config *ip4_config, guint32 *gwaddr) { @@ -272,7 +272,7 @@ ip4_process_classless_routes (GHashTable *options, } static void -process_classful_routes (GHashTable *options, guint priority, NMIP4Config *ip4_config) +process_classful_routes (GHashTable *options, guint32 priority, NMIP4Config *ip4_config) { const char *str; char **searches, **s; @@ -374,7 +374,7 @@ ip4_add_domain_search (gpointer data, gpointer user_data) NMIP4Config * nm_dhcp_utils_ip4_config_from_options (const char *iface, GHashTable *options, - guint priority) + guint32 priority) { NMIP4Config *ip4_config = NULL; guint32 tmp_addr; @@ -591,7 +591,7 @@ ip6_add_domain_search (gpointer data, gpointer user_data) NMIP6Config * nm_dhcp_utils_ip6_config_from_options (const char *iface, GHashTable *options, - guint priority, + guint32 priority, gboolean info_only) { NMIP6Config *ip6_config = NULL; diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index d491e62fb9..adc2780615 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -302,7 +302,7 @@ nm_ip4_config_commit (const NMIP4Config *config, int ifindex) } void -nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIPConfig *setting, int default_route_metric) +nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIPConfig *setting, guint32 default_route_metric) { guint naddresses, nroutes, nnameservers, nsearches; int i; diff --git a/src/nm-ip4-config.h b/src/nm-ip4-config.h index 555f1678a7..a7e88d97a0 100644 --- a/src/nm-ip4-config.h +++ b/src/nm-ip4-config.h @@ -65,7 +65,7 @@ const char * nm_ip4_config_get_dbus_path (const NMIP4Config *config); /* Integration with nm-platform and nm-setting */ NMIP4Config *nm_ip4_config_capture (int ifindex, gboolean capture_resolv_conf); gboolean nm_ip4_config_commit (const NMIP4Config *config, int ifindex); -void nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIPConfig *setting, int default_route_metric); +void nm_ip4_config_merge_setting (NMIP4Config *config, NMSettingIPConfig *setting, guint32 default_route_metric); NMSetting *nm_ip4_config_create_setting (const NMIP4Config *config); /* Utility functions */ diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index 6932ab028a..ad7042f013 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -407,7 +407,7 @@ nm_ip6_config_commit (const NMIP6Config *config, int ifindex) } void -nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIPConfig *setting, int default_route_metric) +nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIPConfig *setting, guint32 default_route_metric) { guint naddresses, nroutes, nnameservers, nsearches; const char *gateway_str; diff --git a/src/nm-ip6-config.h b/src/nm-ip6-config.h index 0174158832..fc908b6180 100644 --- a/src/nm-ip6-config.h +++ b/src/nm-ip6-config.h @@ -65,7 +65,7 @@ const char * nm_ip6_config_get_dbus_path (const NMIP6Config *config); /* Integration with nm-platform and nm-setting */ NMIP6Config *nm_ip6_config_capture (int ifindex, gboolean capture_resolv_conf, NMSettingIP6ConfigPrivacy use_temporary); gboolean nm_ip6_config_commit (const NMIP6Config *config, int ifindex); -void nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIPConfig *setting, int default_route_metric); +void nm_ip6_config_merge_setting (NMIP6Config *config, NMSettingIPConfig *setting, guint32 default_route_metric); NMSetting *nm_ip6_config_create_setting (const NMIP6Config *config); /* Utility functions */ diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c index c3ae25f7ec..ad82751e26 100644 --- a/src/platform/nm-fake-platform.c +++ b/src/platform/nm-fake-platform.c @@ -1043,7 +1043,7 @@ ip6_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default) static gboolean ip4_route_add (NMPlatform *platform, int ifindex, NMIPConfigSource source, in_addr_t network, int plen, in_addr_t gateway, - int metric, int mss) + guint32 metric, guint32 mss) { NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform); NMPlatformIP4Route route; @@ -1083,7 +1083,7 @@ ip4_route_add (NMPlatform *platform, int ifindex, NMIPConfigSource source, static gboolean ip6_route_add (NMPlatform *platform, int ifindex, NMIPConfigSource source, struct in6_addr network, int plen, struct in6_addr gateway, - int metric, int mss) + guint32 metric, guint32 mss) { NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform); NMPlatformIP6Route route; @@ -1121,7 +1121,7 @@ ip6_route_add (NMPlatform *platform, int ifindex, NMIPConfigSource source, } static NMPlatformIP4Route * -ip4_route_get (NMPlatform *platform, int ifindex, in_addr_t network, int plen, int metric) +ip4_route_get (NMPlatform *platform, int ifindex, in_addr_t network, int plen, guint32 metric) { NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform); int i; @@ -1140,7 +1140,7 @@ ip4_route_get (NMPlatform *platform, int ifindex, in_addr_t network, int plen, i } static NMPlatformIP6Route * -ip6_route_get (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, int metric) +ip6_route_get (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, guint32 metric) { NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform); int i; @@ -1159,7 +1159,7 @@ ip6_route_get (NMPlatform *platform, int ifindex, struct in6_addr network, int p } static gboolean -ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, int plen, int metric) +ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, int plen, guint32 metric) { NMPlatformIP4Route *route = ip4_route_get (platform, ifindex, network, plen, metric); NMPlatformIP4Route deleted_route; @@ -1174,7 +1174,7 @@ ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, int plen } static gboolean -ip6_route_delete (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, int metric) +ip6_route_delete (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, guint32 metric) { NMPlatformIP6Route *route = ip6_route_get (platform, ifindex, network, plen, metric); NMPlatformIP6Route deleted_route; @@ -1189,13 +1189,13 @@ ip6_route_delete (NMPlatform *platform, int ifindex, struct in6_addr network, in } static gboolean -ip4_route_exists (NMPlatform *platform, int ifindex, in_addr_t network, int plen, int metric) +ip4_route_exists (NMPlatform *platform, int ifindex, in_addr_t network, int plen, guint32 metric) { return !!ip4_route_get (platform, ifindex, network, plen, metric); } static gboolean -ip6_route_exists (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, int metric) +ip6_route_exists (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, guint32 metric) { return !!ip6_route_get (platform, ifindex, network, plen, metric); } diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index 601066b785..e3b12a432b 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -3711,7 +3711,7 @@ clear_host_address (int family, const void *network, int plen, void *dst) static struct nl_object * build_rtnl_route (int family, int ifindex, NMIPConfigSource source, gconstpointer network, int plen, gconstpointer gateway, - int metric, int mss) + guint32 metric, guint32 mss) { guint32 network_clean[4]; struct rtnl_route *rtnlroute; @@ -3752,7 +3752,7 @@ build_rtnl_route (int family, int ifindex, NMIPConfigSource source, static gboolean ip4_route_add (NMPlatform *platform, int ifindex, NMIPConfigSource source, in_addr_t network, int plen, in_addr_t gateway, - int metric, int mss) + guint32 metric, guint32 mss) { return add_object (platform, build_rtnl_route (AF_INET, ifindex, source, &network, plen, &gateway, metric, mss)); } @@ -3760,13 +3760,13 @@ ip4_route_add (NMPlatform *platform, int ifindex, NMIPConfigSource source, static gboolean ip6_route_add (NMPlatform *platform, int ifindex, NMIPConfigSource source, struct in6_addr network, int plen, struct in6_addr gateway, - int metric, int mss) + guint32 metric, guint32 mss) { return add_object (platform, build_rtnl_route (AF_INET6, ifindex, source, &network, plen, &gateway, metric, mss)); } static struct rtnl_route * -route_search_cache (struct nl_cache *cache, int family, int ifindex, const void *network, int plen, int metric) +route_search_cache (struct nl_cache *cache, int family, int ifindex, const void *network, int plen, guint32 metric) { guint32 network_clean[4], dst_clean[4]; struct nl_object *object; @@ -3815,7 +3815,7 @@ refresh_route (NMPlatform *platform, int family, int ifindex, const void *networ } static gboolean -ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, int plen, int metric) +ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, int plen, guint32 metric) { in_addr_t gateway = 0; struct rtnl_route *cached_object; @@ -3875,7 +3875,7 @@ ip4_route_delete (NMPlatform *platform, int ifindex, in_addr_t network, int plen } static gboolean -ip6_route_delete (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, int metric) +ip6_route_delete (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, guint32 metric) { struct in6_addr gateway = IN6ADDR_ANY_INIT; @@ -3884,7 +3884,7 @@ ip6_route_delete (NMPlatform *platform, int ifindex, struct in6_addr network, in } static gboolean -ip_route_exists (NMPlatform *platform, int family, int ifindex, gpointer network, int plen, int metric) +ip_route_exists (NMPlatform *platform, int family, int ifindex, gpointer network, int plen, guint32 metric) { auto_nl_object struct nl_object *object = build_rtnl_route (family, ifindex, NM_IP_CONFIG_SOURCE_UNKNOWN, @@ -3898,13 +3898,13 @@ ip_route_exists (NMPlatform *platform, int family, int ifindex, gpointer network } static gboolean -ip4_route_exists (NMPlatform *platform, int ifindex, in_addr_t network, int plen, int metric) +ip4_route_exists (NMPlatform *platform, int ifindex, in_addr_t network, int plen, guint32 metric) { return ip_route_exists (platform, AF_INET, ifindex, &network, plen, metric); } static gboolean -ip6_route_exists (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, int metric) +ip6_route_exists (NMPlatform *platform, int ifindex, struct in6_addr network, int plen, guint32 metric) { return ip_route_exists (platform, AF_INET6, ifindex, &network, plen, metric); } diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index ae25cf8156..09ec69cde6 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -1861,14 +1861,12 @@ nm_platform_ip6_route_get_all (int ifindex, gboolean include_default) gboolean nm_platform_ip4_route_add (int ifindex, NMIPConfigSource source, in_addr_t network, int plen, - in_addr_t gateway, int metric, int mss) + in_addr_t gateway, guint32 metric, guint32 mss) { reset_error (); g_return_val_if_fail (platform, FALSE); g_return_val_if_fail (0 <= plen && plen <= 32, FALSE); - g_return_val_if_fail (metric >= 0, FALSE); - g_return_val_if_fail (mss >= 0, FALSE); g_return_val_if_fail (klass->ip4_route_add, FALSE); if (nm_logging_enabled (LOGL_DEBUG, LOGD_PLATFORM)) { @@ -1890,12 +1888,10 @@ nm_platform_ip4_route_add (int ifindex, NMIPConfigSource source, gboolean nm_platform_ip6_route_add (int ifindex, NMIPConfigSource source, struct in6_addr network, int plen, struct in6_addr gateway, - int metric, int mss) + guint32 metric, guint32 mss) { g_return_val_if_fail (platform, FALSE); g_return_val_if_fail (0 <= plen && plen <= 128, FALSE); - g_return_val_if_fail (metric >= 0, FALSE); - g_return_val_if_fail (mss >= 0, FALSE); g_return_val_if_fail (klass->ip6_route_add, FALSE); if (nm_logging_enabled (LOGL_DEBUG, LOGD_PLATFORM)) { @@ -1915,7 +1911,7 @@ nm_platform_ip6_route_add (int ifindex, NMIPConfigSource source, } gboolean -nm_platform_ip4_route_delete (int ifindex, in_addr_t network, int plen, int metric) +nm_platform_ip4_route_delete (int ifindex, in_addr_t network, int plen, guint32 metric) { char str_dev[TO_STRING_DEV_BUF_SIZE]; @@ -1924,14 +1920,14 @@ nm_platform_ip4_route_delete (int ifindex, in_addr_t network, int plen, int metr g_return_val_if_fail (platform, FALSE); g_return_val_if_fail (klass->ip4_route_delete, FALSE); - debug ("route: deleting IPv4 route %s/%d, metric=%d, ifindex %d%s", + debug ("route: deleting IPv4 route %s/%d, metric=%"G_GUINT32_FORMAT", ifindex %d%s", nm_utils_inet4_ntop (network, NULL), plen, metric, ifindex, _to_string_dev (ifindex, str_dev, sizeof (str_dev))); return klass->ip4_route_delete (platform, ifindex, network, plen, metric); } gboolean -nm_platform_ip6_route_delete (int ifindex, struct in6_addr network, int plen, int metric) +nm_platform_ip6_route_delete (int ifindex, struct in6_addr network, int plen, guint32 metric) { char str_dev[TO_STRING_DEV_BUF_SIZE]; @@ -1940,14 +1936,14 @@ nm_platform_ip6_route_delete (int ifindex, struct in6_addr network, int plen, in g_return_val_if_fail (platform, FALSE); g_return_val_if_fail (klass->ip6_route_delete, FALSE); - debug ("route: deleting IPv6 route %s/%d, metric=%d, ifindex %d%s", + debug ("route: deleting IPv6 route %s/%d, metric=%"G_GUINT32_FORMAT", ifindex %d%s", nm_utils_inet6_ntop (&network, NULL), plen, metric, ifindex, _to_string_dev (ifindex, str_dev, sizeof (str_dev))); return klass->ip6_route_delete (platform, ifindex, network, plen, metric); } gboolean -nm_platform_ip4_route_exists (int ifindex, in_addr_t network, int plen, int metric) +nm_platform_ip4_route_exists (int ifindex, in_addr_t network, int plen, guint32 metric) { reset_error (); @@ -1958,7 +1954,7 @@ nm_platform_ip4_route_exists (int ifindex, in_addr_t network, int plen, int metr } gboolean -nm_platform_ip6_route_exists (int ifindex, struct in6_addr network, int plen, int metric) +nm_platform_ip6_route_exists (int ifindex, struct in6_addr network, int plen, guint32 metric) { reset_error (); @@ -2428,7 +2424,7 @@ nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route) _to_string_dev (route->ifindex, str_dev, sizeof (str_dev)); - g_snprintf (to_string_buffer, sizeof (to_string_buffer), "%s/%d via %s%s metric %u mss %u src %s", + g_snprintf (to_string_buffer, sizeof (to_string_buffer), "%s/%d via %s%s metric %"G_GUINT32_FORMAT" mss %"G_GUINT32_FORMAT" src %s", s_network, route->plen, s_gateway, str_dev, route->metric, route->mss, @@ -2461,7 +2457,7 @@ nm_platform_ip6_route_to_string (const NMPlatformIP6Route *route) _to_string_dev (route->ifindex, str_dev, sizeof (str_dev)); - g_snprintf (to_string_buffer, sizeof (to_string_buffer), "%s/%d via %s%s metric %u mss %u src %s", + g_snprintf (to_string_buffer, sizeof (to_string_buffer), "%s/%d via %s%s metric %"G_GUINT32_FORMAT" mss %"G_GUINT32_FORMAT" src %s", s_network, route->plen, s_gateway, str_dev, route->metric, route->mss, diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index d6836d20ac..f3970f7978 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -180,8 +180,8 @@ G_STATIC_ASSERT (G_STRUCT_OFFSET (NMPlatformIPAddress, address_ptr) == G_STRUCT_ __NMPlatformObject_COMMON; \ NMIPConfigSource source; \ int plen; \ - guint metric; \ - guint mss; \ + guint32 metric; \ + guint32 mss; \ ; typedef struct { @@ -399,14 +399,14 @@ typedef struct { GArray * (*ip6_route_get_all) (NMPlatform *, int ifindex, gboolean include_default); gboolean (*ip4_route_add) (NMPlatform *, int ifindex, NMIPConfigSource source, in_addr_t network, int plen, in_addr_t gateway, - int prio, int mss); + guint32 metric, guint32 mss); gboolean (*ip6_route_add) (NMPlatform *, int ifindex, NMIPConfigSource source, struct in6_addr network, int plen, struct in6_addr gateway, - int prio, int mss); - gboolean (*ip4_route_delete) (NMPlatform *, int ifindex, in_addr_t network, int plen, int metric); - gboolean (*ip6_route_delete) (NMPlatform *, int ifindex, struct in6_addr network, int plen, int metric); - gboolean (*ip4_route_exists) (NMPlatform *, int ifindex, in_addr_t network, int plen, int metric); - gboolean (*ip6_route_exists) (NMPlatform *, int ifindex, struct in6_addr network, int plen, int metric); + guint32 metric, guint32 mss); + gboolean (*ip4_route_delete) (NMPlatform *, int ifindex, in_addr_t network, int plen, guint32 metric); + gboolean (*ip6_route_delete) (NMPlatform *, int ifindex, struct in6_addr network, int plen, guint32 metric); + gboolean (*ip4_route_exists) (NMPlatform *, int ifindex, in_addr_t network, int plen, guint32 metric); + gboolean (*ip6_route_exists) (NMPlatform *, int ifindex, struct in6_addr network, int plen, guint32 metric); gboolean (*check_support_kernel_extended_ifa_flags) (NMPlatform *); gboolean (*check_support_user_ipv6ll) (NMPlatform *); @@ -544,17 +544,16 @@ gboolean nm_platform_address_flush (int ifindex); GArray *nm_platform_ip4_route_get_all (int ifindex, gboolean include_default); GArray *nm_platform_ip6_route_get_all (int ifindex, gboolean include_default); -gboolean nm_platform_route_set_metric (int ifindex, int metric); gboolean nm_platform_ip4_route_add (int ifindex, NMIPConfigSource source, in_addr_t network, int plen, in_addr_t gateway, - int metric, int mss); + guint32 metric, guint32 mss); gboolean nm_platform_ip6_route_add (int ifindex, NMIPConfigSource source, struct in6_addr network, int plen, struct in6_addr gateway, - int metric, int mss); -gboolean nm_platform_ip4_route_delete (int ifindex, in_addr_t network, int plen, int metric); -gboolean nm_platform_ip6_route_delete (int ifindex, struct in6_addr network, int plen, int metric); -gboolean nm_platform_ip4_route_exists (int ifindex, in_addr_t network, int plen, int metric); -gboolean nm_platform_ip6_route_exists (int ifindex, struct in6_addr network, int plen, int metric); + guint32 metric, guint32 mss); +gboolean nm_platform_ip4_route_delete (int ifindex, in_addr_t network, int plen, guint32 metric); +gboolean nm_platform_ip6_route_delete (int ifindex, struct in6_addr network, int plen, guint32 metric); +gboolean nm_platform_ip4_route_exists (int ifindex, in_addr_t network, int plen, guint32 metric); +gboolean nm_platform_ip6_route_exists (int ifindex, struct in6_addr network, int plen, guint32 metric); gboolean nm_platform_ip4_route_sync (int ifindex, const GArray *known_routes); gboolean nm_platform_ip6_route_sync (int ifindex, const GArray *known_routes); gboolean nm_platform_route_flush (int ifindex); diff --git a/src/vpn-manager/nm-vpn-connection.c b/src/vpn-manager/nm-vpn-connection.c index 2f02706b1f..81936e4dbe 100644 --- a/src/vpn-manager/nm-vpn-connection.c +++ b/src/vpn-manager/nm-vpn-connection.c @@ -1104,7 +1104,7 @@ nm_vpn_connection_config_get (DBusGProxy *proxy, g_clear_object (&priv->ip6_config); } -static guint +static guint32 vpn_routing_metric (NMVpnConnection *connection) { NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection); -- cgit v1.2.1 From a2662633a5ea21387cb9645440dc48b96b6100df Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 28 Aug 2014 18:16:35 +0200 Subject: core: modify the values/route metric returned by nm_device_get_priority() nm_device_get_priority() is used to select the "best" device for the default route. The absolute values don't matter at that point and the relative ordering is not changed by this patch. It is also directly used for route priority/metric. As we soon allow the user to overwrite the setting, we want to get more space between the individual device-types. That way, a user could overwrite the default metric for a wifi device to be 109 (making it lower then the default value 110), but still less preferred then other non-wifi types. Obviously, this patch is a visible change of behavior as now routes get different metrics assigned. Signed-off-by: Thomas Haller --- src/devices/nm-device.c | 27 ++++++++++++++------------- src/vpn-manager/nm-vpn-connection.h | 3 +++ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index efb7f25d2d..efb4a69257 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -640,7 +640,7 @@ nm_device_get_device_type (NMDevice *self) int nm_device_get_priority (NMDevice *self) { - g_return_val_if_fail (NM_IS_DEVICE (self), 100); + g_return_val_if_fail (NM_IS_DEVICE (self), 1000); /* Device 'priority' is used for two things: * @@ -653,30 +653,31 @@ nm_device_get_priority (NMDevice *self) */ switch (nm_device_get_device_type (self)) { + /* 10 is reserved for VPN (NM_VPN_ROUTE_METRIC_DEFAULT) */ case NM_DEVICE_TYPE_ETHERNET: - return 1; + return 20; case NM_DEVICE_TYPE_INFINIBAND: - return 2; + return 30; case NM_DEVICE_TYPE_ADSL: - return 3; + return 40; case NM_DEVICE_TYPE_WIMAX: - return 4; + return 50; case NM_DEVICE_TYPE_BOND: - return 5; + return 60; case NM_DEVICE_TYPE_TEAM: - return 6; + return 70; case NM_DEVICE_TYPE_VLAN: - return 7; + return 80; case NM_DEVICE_TYPE_MODEM: - return 8; + return 90; case NM_DEVICE_TYPE_BT: - return 9; + return 100; case NM_DEVICE_TYPE_WIFI: - return 10; + return 110; case NM_DEVICE_TYPE_OLPC_MESH: - return 11; + return 120; default: - return 20; + return 200; } } diff --git a/src/vpn-manager/nm-vpn-connection.h b/src/vpn-manager/nm-vpn-connection.h index 31a2734e39..047113c318 100644 --- a/src/vpn-manager/nm-vpn-connection.h +++ b/src/vpn-manager/nm-vpn-connection.h @@ -46,6 +46,9 @@ #define NM_VPN_CONNECTION_INTERNAL_RETRY_AFTER_FAILURE "internal-retry-after-failure" +#define NM_VPN_ROUTE_METRIC_DEFAULT 10 + + struct _NMVpnConnection { NMActiveConnection parent; }; -- cgit v1.2.1 From 172c1eb65241ca420050787d92ed71776718d184 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 28 Aug 2014 18:11:35 +0200 Subject: core: add explicit functions for the route priority/metric Before, we would always call unanimously nm_device_get_priority() to get the default route metric for a device. Add new functions nm_device_get_ip4_route_priority() and nm_device_get_ip6_route_priority() and use them at the proper places. Also add new function nm_vpn_connection_get_ip4_route_metric() and nm_vpn_connection_get_ip6_route_metric(). Signed-off-by: Thomas Haller --- src/devices/nm-device.c | 24 ++++++++++++++----- src/devices/nm-device.h | 2 ++ src/vpn-manager/nm-vpn-connection.c | 48 ++++++++++++++++++++++++++++--------- src/vpn-manager/nm-vpn-connection.h | 3 +++ 4 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index efb4a69257..419ea87174 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -681,6 +681,18 @@ nm_device_get_priority (NMDevice *self) } } +guint32 +nm_device_get_ip4_route_metric (NMDevice *self) +{ + return nm_device_get_priority (self); +} + +guint32 +nm_device_get_ip6_route_metric (NMDevice *self) +{ + return nm_device_get_priority (self); +} + const char * nm_device_get_type_desc (NMDevice *self) { @@ -2394,7 +2406,7 @@ aipd_get_ip4_config (NMDevice *self, guint32 lla) route.network = htonl (0xE0000000L); route.plen = 4; route.source = NM_IP_CONFIG_SOURCE_IP4LL; - route.metric = nm_device_get_priority (self); + route.metric = nm_device_get_ip4_route_metric (self); nm_ip4_config_add_route (config, &route); return config; @@ -2662,7 +2674,7 @@ ip4_config_merge_and_apply (NMDevice *self, && !nm_settings_connection_get_nm_generated_assumed (NM_SETTINGS_CONNECTION (connection))) { nm_ip4_config_merge_setting (composite, nm_connection_get_setting_ip4_config (connection), - nm_device_get_priority (self)); + nm_device_get_ip4_route_metric (self)); } /* Allow setting MTU etc */ @@ -2803,7 +2815,7 @@ dhcp4_start (NMDevice *self, nm_device_get_ip_ifindex (self), tmp, nm_connection_get_uuid (connection), - nm_device_get_priority (self), + nm_device_get_ip4_route_metric (self), nm_setting_ip_config_get_dhcp_send_hostname (s_ip4), nm_setting_ip_config_get_dhcp_hostname (s_ip4), nm_setting_ip4_config_get_dhcp_client_id (NM_SETTING_IP4_CONFIG (s_ip4)), @@ -3163,7 +3175,7 @@ ip6_config_merge_and_apply (NMDevice *self, && !nm_settings_connection_get_nm_generated_assumed (NM_SETTINGS_CONNECTION (connection))) { nm_ip6_config_merge_setting (composite, nm_connection_get_setting_ip6_config (connection), - nm_device_get_priority (self)); + nm_device_get_ip6_route_metric (self)); } nm_ip6_config_addresses_sort (composite, @@ -3356,7 +3368,7 @@ dhcp6_start (NMDevice *self, nm_device_get_ip_ifindex (self), tmp, nm_connection_get_uuid (connection), - nm_device_get_priority (self), + nm_device_get_ip6_route_metric (self), nm_setting_ip_config_get_dhcp_send_hostname (s_ip6), nm_setting_ip_config_get_dhcp_hostname (s_ip6), priv->dhcp_timeout, @@ -3700,7 +3712,7 @@ rdisc_config_changed (NMRDisc *rdisc, NMRDiscConfigMap changed, NMDevice *self) route.plen = discovered_route->plen; route.gateway = discovered_route->gateway; route.source = NM_IP_CONFIG_SOURCE_RDISC; - route.metric = nm_device_get_priority (self); + route.metric = nm_device_get_ip6_route_metric (self); nm_ip6_config_add_route (priv->ac_ip6_config, &route); } diff --git a/src/devices/nm-device.h b/src/devices/nm-device.h index b053070388..a2663a0253 100644 --- a/src/devices/nm-device.h +++ b/src/devices/nm-device.h @@ -228,6 +228,8 @@ const char * nm_device_get_type_desc (NMDevice *dev); NMDeviceType nm_device_get_device_type (NMDevice *dev); int nm_device_get_priority (NMDevice *dev); +guint32 nm_device_get_ip4_route_metric (NMDevice *dev); +guint32 nm_device_get_ip6_route_metric (NMDevice *dev); const char * nm_device_get_hw_address (NMDevice *dev); diff --git a/src/vpn-manager/nm-vpn-connection.c b/src/vpn-manager/nm-vpn-connection.c index 81936e4dbe..9f5e47b65e 100644 --- a/src/vpn-manager/nm-vpn-connection.c +++ b/src/vpn-manager/nm-vpn-connection.c @@ -487,6 +487,7 @@ add_ip4_vpn_gateway_route (NMIP4Config *config, NMDevice *parent_device, guint32 NMIP4Config *parent_config; guint32 parent_gw; NMPlatformIP4Route route; + guint32 route_metric; g_return_if_fail (NM_IS_IP4_CONFIG (config)); g_return_if_fail (NM_IS_DEVICE (parent_device)); @@ -502,6 +503,8 @@ add_ip4_vpn_gateway_route (NMIP4Config *config, NMDevice *parent_device, guint32 if (!parent_gw) return; + route_metric = nm_device_get_ip4_route_metric (parent_device); + memset (&route, 0, sizeof (route)); route.network = vpn_gw; route.plen = 32; @@ -515,7 +518,7 @@ add_ip4_vpn_gateway_route (NMIP4Config *config, NMDevice *parent_device, guint32 route.gateway = 0; route.source = NM_IP_CONFIG_SOURCE_VPN; - route.metric = nm_device_get_priority (parent_device); + route.metric = route_metric; nm_ip4_config_add_route (config, &route); /* Ensure there's a route to the parent device's gateway through the @@ -527,7 +530,7 @@ add_ip4_vpn_gateway_route (NMIP4Config *config, NMDevice *parent_device, guint32 route.network = parent_gw; route.plen = 32; route.source = NM_IP_CONFIG_SOURCE_VPN; - route.metric = nm_device_get_priority (parent_device); + route.metric = route_metric; nm_ip4_config_add_route (config, &route); } @@ -540,6 +543,7 @@ add_ip6_vpn_gateway_route (NMIP6Config *config, NMIP6Config *parent_config; const struct in6_addr *parent_gw; NMPlatformIP6Route route; + guint32 route_metric; g_return_if_fail (NM_IS_IP6_CONFIG (config)); g_return_if_fail (NM_IS_DEVICE (parent_device)); @@ -551,6 +555,8 @@ add_ip6_vpn_gateway_route (NMIP6Config *config, if (!parent_gw) return; + route_metric = nm_device_get_ip6_route_metric (parent_device); + memset (&route, 0, sizeof (route)); route.network = *vpn_gw; route.plen = 128; @@ -564,7 +570,7 @@ add_ip6_vpn_gateway_route (NMIP6Config *config, route.gateway = in6addr_any; route.source = NM_IP_CONFIG_SOURCE_VPN; - route.metric = nm_device_get_priority (parent_device); + route.metric = route_metric; nm_ip6_config_add_route (config, &route); /* Ensure there's a route to the parent device's gateway through the @@ -576,7 +582,7 @@ add_ip6_vpn_gateway_route (NMIP6Config *config, route.network = *parent_gw; route.plen = 128; route.source = NM_IP_CONFIG_SOURCE_VPN; - route.metric = nm_device_get_priority (parent_device); + route.metric = route_metric; nm_ip6_config_add_route (config, &route); } @@ -1104,8 +1110,8 @@ nm_vpn_connection_config_get (DBusGProxy *proxy, g_clear_object (&priv->ip6_config); } -static guint32 -vpn_routing_metric (NMVpnConnection *connection) +guint32 +nm_vpn_connection_get_ip4_route_metric (NMVpnConnection *connection) { NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection); @@ -1114,7 +1120,21 @@ vpn_routing_metric (NMVpnConnection *connection) else { NMDevice *parent_dev = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (connection)); - return nm_device_get_priority (parent_dev); + return nm_device_get_ip4_route_metric (parent_dev); + } +} + +guint32 +nm_vpn_connection_get_ip6_route_metric (NMVpnConnection *connection) +{ + NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection); + + if (priv->ip_ifindex) + return NM_PLATFORM_ROUTE_METRIC_DEFAULT; + else { + NMDevice *parent_dev = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (connection)); + + return nm_device_get_ip6_route_metric (parent_dev); } } @@ -1129,6 +1149,7 @@ nm_vpn_connection_ip4_config_get (DBusGProxy *proxy, NMIP4Config *config; GValue *val; int i; + guint32 route_metric; if (priv->vpn_state == STATE_CONNECT) _set_vpn_state (connection, STATE_IP_CONFIG_GET, NM_VPN_CONNECTION_STATE_REASON_NONE, FALSE); @@ -1226,6 +1247,8 @@ nm_vpn_connection_ip4_config_get (DBusGProxy *proxy, nm_ip4_config_add_domain (config, *domain); } + route_metric = nm_vpn_connection_get_ip4_route_metric (connection); + val = (GValue *) g_hash_table_lookup (config_hash, NM_VPN_PLUGIN_IP4_CONFIG_ROUTES); if (val) { GSList *routes; @@ -1241,7 +1264,7 @@ nm_vpn_connection_ip4_config_get (DBusGProxy *proxy, route.plen = nm_ip_route_get_prefix (item); nm_ip_route_get_next_hop_binary (item, &route.gateway); route.source = NM_IP_CONFIG_SOURCE_VPN; - route.metric = vpn_routing_metric (connection); + route.metric = route_metric; /* Ignore host routes to the VPN gateway since NM adds one itself * below. Since NM knows more about the routing situation than @@ -1265,7 +1288,7 @@ nm_vpn_connection_ip4_config_get (DBusGProxy *proxy, /* Merge in user overrides from the NMConnection's IPv4 setting */ nm_ip4_config_merge_setting (config, nm_connection_get_setting_ip4_config (priv->connection), - vpn_routing_metric (connection)); + route_metric); g_clear_object (&priv->ip4_config); priv->ip4_config = config; @@ -1285,6 +1308,7 @@ nm_vpn_connection_ip6_config_get (DBusGProxy *proxy, NMIP6Config *config; GValue *val; int i; + guint32 route_metric; nm_log_info (LOGD_VPN, "VPN connection '%s' (IP6 Config Get) reply received.", nm_connection_get_id (priv->connection)); @@ -1373,6 +1397,8 @@ nm_vpn_connection_ip6_config_get (DBusGProxy *proxy, nm_ip6_config_add_domain (config, *domain); } + route_metric = nm_vpn_connection_get_ip6_route_metric (connection); + val = (GValue *) g_hash_table_lookup (config_hash, NM_VPN_PLUGIN_IP6_CONFIG_ROUTES); if (val) { GSList *routes; @@ -1388,7 +1414,7 @@ nm_vpn_connection_ip6_config_get (DBusGProxy *proxy, route.plen = nm_ip_route_get_prefix (item); nm_ip_route_get_next_hop_binary (item, &route.gateway); route.source = NM_IP_CONFIG_SOURCE_VPN; - route.metric = vpn_routing_metric (connection); + route.metric = route_metric; /* Ignore host routes to the VPN gateway since NM adds one itself * below. Since NM knows more about the routing situation than @@ -1412,7 +1438,7 @@ nm_vpn_connection_ip6_config_get (DBusGProxy *proxy, /* Merge in user overrides from the NMConnection's IPv6 setting */ nm_ip6_config_merge_setting (config, nm_connection_get_setting_ip6_config (priv->connection), - vpn_routing_metric (connection)); + route_metric); g_clear_object (&priv->ip6_config); priv->ip6_config = config; diff --git a/src/vpn-manager/nm-vpn-connection.h b/src/vpn-manager/nm-vpn-connection.h index 047113c318..512bdf932a 100644 --- a/src/vpn-manager/nm-vpn-connection.h +++ b/src/vpn-manager/nm-vpn-connection.h @@ -96,4 +96,7 @@ int nm_vpn_connection_get_ip_ifindex (NMVpnConnection *connect guint32 nm_vpn_connection_get_ip4_internal_gateway (NMVpnConnection *connection); struct in6_addr * nm_vpn_connection_get_ip6_internal_gateway (NMVpnConnection *connection); +guint32 nm_vpn_connection_get_ip4_route_metric (NMVpnConnection *connection); +guint32 nm_vpn_connection_get_ip6_route_metric (NMVpnConnection *connection); + #endif /* __NETWORKMANAGER_VPN_CONNECTION_H__ */ -- cgit v1.2.1 From df923622b1c1b12b3e2e8b00bc411ff7f081c10f Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 28 Aug 2014 18:32:05 +0200 Subject: core: overwrite the default route priority via connection setting Make use of the new setting nm_setting_ip_config_get_route_metric() If set, this override the route metric determined based on the device type. Similarly for VPN also prefer the setting from the connection. Thereby change the default priority (for VPN that have their own device) to NM_VPN_ROUTE_METRIC_DEFAULT instead of NM_PLATFORM_ROUTE_METRIC_DEFAULT. The latter would be a very low priority compared to the default metrics for devices. Signed-off-by: Thomas Haller --- src/devices/nm-device.c | 24 ++++++++++++++++++++++-- src/vpn-manager/nm-vpn-connection.c | 32 ++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 419ea87174..f4a8a1c424 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -684,13 +684,33 @@ nm_device_get_priority (NMDevice *self) guint32 nm_device_get_ip4_route_metric (NMDevice *self) { - return nm_device_get_priority (self); + NMConnection *connection; + gint64 route_metric = -1; + + g_return_val_if_fail (NM_IS_DEVICE (self), G_MAXUINT32); + + connection = nm_device_get_connection (self); + + if (connection) + route_metric = nm_setting_ip_config_get_route_metric (nm_connection_get_setting_ip4_config (connection)); + + return route_metric >= 0 ? route_metric : nm_device_get_priority (self); } guint32 nm_device_get_ip6_route_metric (NMDevice *self) { - return nm_device_get_priority (self); + NMConnection *connection; + gint64 route_metric = -1; + + g_return_val_if_fail (NM_IS_DEVICE (self), G_MAXUINT32); + + connection = nm_device_get_connection (self); + + if (connection) + route_metric = nm_setting_ip_config_get_route_metric (nm_connection_get_setting_ip6_config (connection)); + + return route_metric >= 0 ? route_metric : nm_device_get_priority (self); } const char * diff --git a/src/vpn-manager/nm-vpn-connection.c b/src/vpn-manager/nm-vpn-connection.c index 9f5e47b65e..d41e2e2c67 100644 --- a/src/vpn-manager/nm-vpn-connection.c +++ b/src/vpn-manager/nm-vpn-connection.c @@ -1113,29 +1113,41 @@ nm_vpn_connection_config_get (DBusGProxy *proxy, guint32 nm_vpn_connection_get_ip4_route_metric (NMVpnConnection *connection) { + NMDevice *parent_dev; NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection); - if (priv->ip_ifindex) - return NM_PLATFORM_ROUTE_METRIC_DEFAULT; - else { - NMDevice *parent_dev = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (connection)); + if (priv->connection) { + gint64 route_metric = nm_setting_ip_config_get_route_metric (nm_connection_get_setting_ip4_config (priv->connection)); - return nm_device_get_ip4_route_metric (parent_dev); + if (route_metric >= 0) + return route_metric; } + + if ( priv->ip_ifindex + || !(parent_dev = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (connection)))) + return NM_VPN_ROUTE_METRIC_DEFAULT; + + return nm_device_get_ip4_route_metric (parent_dev); } guint32 nm_vpn_connection_get_ip6_route_metric (NMVpnConnection *connection) { + NMDevice *parent_dev; NMVpnConnectionPrivate *priv = NM_VPN_CONNECTION_GET_PRIVATE (connection); - if (priv->ip_ifindex) - return NM_PLATFORM_ROUTE_METRIC_DEFAULT; - else { - NMDevice *parent_dev = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (connection)); + if (priv->connection) { + gint64 route_metric = nm_setting_ip_config_get_route_metric (nm_connection_get_setting_ip6_config (priv->connection)); - return nm_device_get_ip6_route_metric (parent_dev); + if (route_metric >= 0) + return route_metric; } + + if ( priv->ip_ifindex + || !(parent_dev = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (connection)))) + return NM_VPN_ROUTE_METRIC_DEFAULT; + + return nm_device_get_ip6_route_metric (parent_dev); } static void -- cgit v1.2.1 From 3c17254823128a4423ff1f20f36a9db1fd6d274c Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 20 Oct 2014 16:43:41 +0200 Subject: core: fix comparing metric for IPv6 routes Signed-off-by: Thomas Haller --- src/nm-ip6-config.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index ad7042f013..9959268e30 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -171,7 +171,9 @@ static gboolean routes_are_duplicate (const NMPlatformIP6Route *a, const NMPlatformIP6Route *b, gboolean consider_gateway_and_metric) { return IN6_ARE_ADDR_EQUAL (&a->network, &b->network) && a->plen == b->plen && - (!consider_gateway_and_metric || (IN6_ARE_ADDR_EQUAL (&a->gateway, &b->gateway) && a->metric == b->metric)); + ( !consider_gateway_and_metric + || ( IN6_ARE_ADDR_EQUAL (&a->gateway, &b->gateway) + && nm_utils_ip6_route_metric_normalize (a->metric) == nm_utils_ip6_route_metric_normalize (b->metric))); } static gint -- cgit v1.2.1 From 644eadcf80cc2d46e526d96e298807a6a6a73fd1 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 6 Nov 2014 21:01:22 +0100 Subject: core: add nm_ip4_config_get_subnet_for_host() function And nm_ip6_config_get_subnet_for_host(). Signed-off-by: Thomas Haller --- src/nm-ip4-config.c | 26 ++++++++++++++++++++++++-- src/nm-ip4-config.h | 1 + src/nm-ip6-config.c | 31 +++++++++++++++++++++++++++++-- src/nm-ip6-config.h | 1 + 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index adc2780615..b9f2ea7f0d 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -1210,12 +1210,12 @@ const NMPlatformIP4Route * nm_ip4_config_get_direct_route_for_host (const NMIP4Config *config, guint32 host) { NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (config); - int i; + guint i; NMPlatformIP4Route *best_route = NULL; g_return_val_if_fail (host, NULL); - for (i = 0; i < priv->routes->len; i++ ) { + for (i = 0; i < priv->routes->len; i++) { NMPlatformIP4Route *item = &g_array_index (priv->routes, NMPlatformIP4Route, i); if (item->gateway != 0) @@ -1236,6 +1236,28 @@ nm_ip4_config_get_direct_route_for_host (const NMIP4Config *config, guint32 host return best_route; } +const NMPlatformIP4Address * +nm_ip4_config_get_subnet_for_host (const NMIP4Config *config, guint32 host) +{ + NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (config); + guint i; + NMPlatformIP4Address *subnet = NULL; + + g_return_val_if_fail (host, NULL); + + for (i = 0; i < priv->addresses->len; i++) { + NMPlatformIP4Address *item = &g_array_index (priv->addresses, NMPlatformIP4Address, i); + + if (subnet && subnet->plen >= item->plen) + continue; + if (nm_utils_ip4_address_clear_host_address (host, item->plen) != nm_utils_ip4_address_clear_host_address (item->address, item->plen)) + continue; + subnet = item; + } + + return subnet; +} + /******************************************************************/ void diff --git a/src/nm-ip4-config.h b/src/nm-ip4-config.h index a7e88d97a0..afdd8d09a2 100644 --- a/src/nm-ip4-config.h +++ b/src/nm-ip4-config.h @@ -97,6 +97,7 @@ guint32 nm_ip4_config_get_num_routes (const NMIP4Config *config); const NMPlatformIP4Route *nm_ip4_config_get_route (const NMIP4Config *config, guint32 i); const NMPlatformIP4Route *nm_ip4_config_get_direct_route_for_host (const NMIP4Config *config, guint32 host); +const NMPlatformIP4Address *nm_ip4_config_get_subnet_for_host (const NMIP4Config *config, guint32 host); /* Nameservers */ void nm_ip4_config_reset_nameservers (NMIP4Config *config); diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index 9959268e30..2899b722f6 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -1217,13 +1217,13 @@ const NMPlatformIP6Route * nm_ip6_config_get_direct_route_for_host (const NMIP6Config *config, const struct in6_addr *host) { NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (config); - int i; + guint i; struct in6_addr network2, host2; NMPlatformIP6Route *best_route = NULL; g_return_val_if_fail (host && !IN6_IS_ADDR_UNSPECIFIED (host), NULL); - for (i = 0; i < priv->routes->len; i++ ) { + for (i = 0; i < priv->routes->len; i++) { NMPlatformIP6Route *item = &g_array_index (priv->routes, NMPlatformIP6Route, i); if (!IN6_IS_ADDR_UNSPECIFIED (&item->gateway)) @@ -1248,6 +1248,33 @@ nm_ip6_config_get_direct_route_for_host (const NMIP6Config *config, const struct return best_route; } +const NMPlatformIP6Address * +nm_ip6_config_get_subnet_for_host (const NMIP6Config *config, const struct in6_addr *host) +{ + NMIP6ConfigPrivate *priv = NM_IP6_CONFIG_GET_PRIVATE (config); + guint i; + NMPlatformIP6Address *subnet = NULL; + struct in6_addr subnet2, host2; + + g_return_val_if_fail (host && !IN6_IS_ADDR_UNSPECIFIED (host), NULL); + + for (i = 0; i < priv->addresses->len; i++) { + NMPlatformIP6Address *item = &g_array_index (priv->addresses, NMPlatformIP6Address, i); + + if (subnet && subnet->plen >= item->plen) + continue; + + nm_utils_ip6_address_clear_host_address (&host2, host, item->plen); + nm_utils_ip6_address_clear_host_address (&subnet2, &item->address, item->plen); + + if (IN6_ARE_ADDR_EQUAL (&subnet2, &host2)) + subnet = item; + } + + return subnet; +} + + /******************************************************************/ void diff --git a/src/nm-ip6-config.h b/src/nm-ip6-config.h index fc908b6180..f1d2dc8dd9 100644 --- a/src/nm-ip6-config.h +++ b/src/nm-ip6-config.h @@ -99,6 +99,7 @@ guint32 nm_ip6_config_get_num_routes (const NMIP6Config *config); const NMPlatformIP6Route *nm_ip6_config_get_route (const NMIP6Config *config, guint32 i); const NMPlatformIP6Route *nm_ip6_config_get_direct_route_for_host (const NMIP6Config *config, const struct in6_addr *host); +const NMPlatformIP6Address *nm_ip6_config_get_subnet_for_host (const NMIP6Config *config, const struct in6_addr *host); /* Nameservers */ void nm_ip6_config_reset_nameservers (NMIP6Config *config); -- cgit v1.2.1 From 3ef807c6ae48cac04df7195c0a03dd8c5de13fc5 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 14 Oct 2014 19:02:50 +0200 Subject: platform: extend nm_platform_ipX_route_get_all() to return default-routes only Add a new enum NMPlatformGetRouteMode. This extends the existing functions nm_platform_ip4_route_get_all() and nm_platform_ip6_route_get_all() to return default routes only. Signed-off-by: Thomas Haller --- src/nm-ip4-config.c | 2 +- src/nm-ip6-config.c | 2 +- src/platform/nm-fake-platform.c | 26 +++++++++++++++++++------ src/platform/nm-linux-platform.c | 40 +++++++++++++++++++++++++++++++-------- src/platform/nm-platform.c | 12 ++++++------ src/platform/nm-platform.h | 14 ++++++++++---- src/platform/tests/dump.c | 4 ++-- src/platform/tests/platform.c | 4 ++-- src/platform/tests/test-cleanup.c | 8 ++++---- src/platform/tests/test-route.c | 4 ++-- 10 files changed, 80 insertions(+), 36 deletions(-) diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index b9f2ea7f0d..a781f0342c 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -194,7 +194,7 @@ nm_ip4_config_capture (int ifindex, gboolean capture_resolv_conf) g_array_unref (priv->routes); priv->addresses = nm_platform_ip4_address_get_all (ifindex); - priv->routes = nm_platform_ip4_route_get_all (ifindex, TRUE); + priv->routes = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL); /* Extract gateway from default route */ old_gateway = priv->gateway; diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index 2899b722f6..d901781ba4 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -306,7 +306,7 @@ nm_ip6_config_capture (int ifindex, gboolean capture_resolv_conf, NMSettingIP6Co g_array_unref (priv->routes); priv->addresses = nm_platform_ip6_address_get_all (ifindex); - priv->routes = nm_platform_ip6_route_get_all (ifindex, TRUE); + priv->routes = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL); /* Extract gateway from default route */ old_gateway = priv->gateway; diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c index ad82751e26..f397f5b731 100644 --- a/src/platform/nm-fake-platform.c +++ b/src/platform/nm-fake-platform.c @@ -983,13 +983,15 @@ ip6_address_exists (NMPlatform *platform, int ifindex, struct in6_addr addr, int /******************************************************************/ static GArray * -ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default) +ip4_route_get_all (NMPlatform *platform, int ifindex, NMPlatformGetRouteMode mode) { NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform); GArray *routes; NMPlatformIP4Route *route; int count = 0, i; + g_return_val_if_fail (NM_IN_SET (mode, NM_PLATFORM_GET_ROUTE_MODE_ALL, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT), NULL); + /* Count routes */ for (i = 0; i < priv->ip4_routes->len; i++) { route = &g_array_index (priv->ip4_routes, NMPlatformIP4Route, i); @@ -1003,8 +1005,13 @@ ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default) for (i = 0; i < priv->ip4_routes->len; i++) { route = &g_array_index (priv->ip4_routes, NMPlatformIP4Route, i); if (route && route->ifindex == ifindex) { - if (route->plen != 0 || include_default) - g_array_append_val (routes, *route); + if (NM_PLATFORM_IP_ROUTE_IS_DEFAULT (route)) { + if (mode != NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT) + g_array_append_val (routes, *route); + } else { + if (mode != NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT) + g_array_append_val (routes, *route); + } } } @@ -1012,13 +1019,15 @@ ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default) } static GArray * -ip6_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default) +ip6_route_get_all (NMPlatform *platform, int ifindex, NMPlatformGetRouteMode mode) { NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform); GArray *routes; NMPlatformIP6Route *route; int count = 0, i; + g_return_val_if_fail (NM_IN_SET (mode, NM_PLATFORM_GET_ROUTE_MODE_ALL, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT), NULL); + /* Count routes */ for (i = 0; i < priv->ip6_routes->len; i++) { route = &g_array_index (priv->ip6_routes, NMPlatformIP6Route, i); @@ -1032,8 +1041,13 @@ ip6_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default) for (i = 0; i < priv->ip6_routes->len; i++) { route = &g_array_index (priv->ip6_routes, NMPlatformIP6Route, i); if (route && route->ifindex == ifindex) { - if (route->plen != 0 || include_default) - g_array_append_val (routes, *route); + if (NM_PLATFORM_IP_ROUTE_IS_DEFAULT (route)) { + if (mode != NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT) + g_array_append_val (routes, *route); + } else { + if (mode != NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT) + g_array_append_val (routes, *route); + } } } diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index e3b12a432b..37eb5c7cce 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -1297,6 +1297,16 @@ rtprot_to_source (guint rtprot) } } +static gboolean +_rtnl_route_is_default (const struct rtnl_route *rtnlroute) +{ + struct nl_addr *dst; + + return rtnlroute + && (dst = rtnl_route_get_dst ((struct rtnl_route *) rtnlroute)) + && nl_addr_get_prefixlen (dst) == 0; +} + static gboolean init_ip4_route (NMPlatformIP4Route *route, struct rtnl_route *rtnlroute) { @@ -3647,21 +3657,28 @@ _route_match (struct rtnl_route *rtnlroute, int family, int ifindex) } static GArray * -ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default) +ip4_route_get_all (NMPlatform *platform, int ifindex, NMPlatformGetRouteMode mode) { NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform); GArray *routes; NMPlatformIP4Route route; struct nl_object *object; + g_return_val_if_fail (NM_IN_SET (mode, NM_PLATFORM_GET_ROUTE_MODE_ALL, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT), NULL); + routes = g_array_new (FALSE, FALSE, sizeof (NMPlatformIP4Route)); for (object = nl_cache_get_first (priv->route_cache); object; object = nl_cache_get_next (object)) { if (_route_match ((struct rtnl_route *) object, AF_INET, ifindex)) { - if (init_ip4_route (&route, (struct rtnl_route *) object)) { - if (!NM_PLATFORM_IP_ROUTE_IS_DEFAULT (&route) || include_default) - g_array_append_val (routes, route); + if (_rtnl_route_is_default ((struct rtnl_route *) object)) { + if (mode == NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT) + continue; + } else { + if (mode == NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT) + continue; } + if (init_ip4_route (&route, (struct rtnl_route *) object)) + g_array_append_val (routes, route); } } @@ -3669,21 +3686,28 @@ ip4_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default) } static GArray * -ip6_route_get_all (NMPlatform *platform, int ifindex, gboolean include_default) +ip6_route_get_all (NMPlatform *platform, int ifindex, NMPlatformGetRouteMode mode) { NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform); GArray *routes; NMPlatformIP6Route route; struct nl_object *object; + g_return_val_if_fail (NM_IN_SET (mode, NM_PLATFORM_GET_ROUTE_MODE_ALL, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT), NULL); + routes = g_array_new (FALSE, FALSE, sizeof (NMPlatformIP6Route)); for (object = nl_cache_get_first (priv->route_cache); object; object = nl_cache_get_next (object)) { if (_route_match ((struct rtnl_route *) object, AF_INET6, ifindex)) { - if (init_ip6_route (&route, (struct rtnl_route *) object)) { - if (!NM_PLATFORM_IP_ROUTE_IS_DEFAULT (&route) || include_default) - g_array_append_val (routes, route); + if (_rtnl_route_is_default ((struct rtnl_route *) object)) { + if (mode == NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT) + continue; + } else { + if (mode == NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT) + continue; } + if (init_ip6_route (&route, (struct rtnl_route *) object)) + g_array_append_val (routes, route); } } diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 09ec69cde6..70395ca3cf 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -1837,25 +1837,25 @@ nm_platform_address_flush (int ifindex) /******************************************************************/ GArray * -nm_platform_ip4_route_get_all (int ifindex, gboolean include_default) +nm_platform_ip4_route_get_all (int ifindex, NMPlatformGetRouteMode mode) { reset_error (); g_return_val_if_fail (ifindex > 0, NULL); g_return_val_if_fail (klass->ip4_route_get_all, NULL); - return klass->ip4_route_get_all (platform, ifindex, include_default); + return klass->ip4_route_get_all (platform, ifindex, mode); } GArray * -nm_platform_ip6_route_get_all (int ifindex, gboolean include_default) +nm_platform_ip6_route_get_all (int ifindex, NMPlatformGetRouteMode mode) { reset_error (); g_return_val_if_fail (ifindex > 0, NULL); g_return_val_if_fail (klass->ip6_route_get_all, NULL); - return klass->ip6_route_get_all (platform, ifindex, include_default); + return klass->ip6_route_get_all (platform, ifindex, mode); } gboolean @@ -2026,7 +2026,7 @@ nm_platform_ip4_route_sync (int ifindex, const GArray *known_routes) int i, i_type; /* Delete unknown routes */ - routes = nm_platform_ip4_route_get_all (ifindex, FALSE); + routes = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT); for (i = 0; i < routes->len; i++) { route = &g_array_index (routes, NMPlatformIP4Route, i); @@ -2099,7 +2099,7 @@ nm_platform_ip6_route_sync (int ifindex, const GArray *known_routes) int i, i_type; /* Delete unknown routes */ - routes = nm_platform_ip6_route_get_all (ifindex, FALSE); + routes = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT); for (i = 0; i < routes->len; i++) { route = &g_array_index (routes, NMPlatformIP6Route, i); route->ifindex = 0; diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index f3970f7978..3514dd084d 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -99,6 +99,12 @@ typedef enum { #define NM_PLATFORM_LIFETIME_PERMANENT G_MAXUINT32 +typedef enum { + NM_PLATFORM_GET_ROUTE_MODE_ALL, + NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT, + NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT, +} NMPlatformGetRouteMode; + typedef struct { __NMPlatformObject_COMMON; } NMPlatformObject; @@ -395,8 +401,8 @@ typedef struct { gboolean (*ip4_address_exists) (NMPlatform *, int ifindex, in_addr_t address, int plen); gboolean (*ip6_address_exists) (NMPlatform *, int ifindex, struct in6_addr address, int plen); - GArray * (*ip4_route_get_all) (NMPlatform *, int ifindex, gboolean include_default); - GArray * (*ip6_route_get_all) (NMPlatform *, int ifindex, gboolean include_default); + GArray * (*ip4_route_get_all) (NMPlatform *, int ifindex, NMPlatformGetRouteMode mode); + GArray * (*ip6_route_get_all) (NMPlatform *, int ifindex, NMPlatformGetRouteMode mode); gboolean (*ip4_route_add) (NMPlatform *, int ifindex, NMIPConfigSource source, in_addr_t network, int plen, in_addr_t gateway, guint32 metric, guint32 mss); @@ -542,8 +548,8 @@ gboolean nm_platform_ip4_address_sync (int ifindex, const GArray *known_addresse gboolean nm_platform_ip6_address_sync (int ifindex, const GArray *known_addresses); gboolean nm_platform_address_flush (int ifindex); -GArray *nm_platform_ip4_route_get_all (int ifindex, gboolean include_default); -GArray *nm_platform_ip6_route_get_all (int ifindex, gboolean include_default); +GArray *nm_platform_ip4_route_get_all (int ifindex, NMPlatformGetRouteMode mode); +GArray *nm_platform_ip6_route_get_all (int ifindex, NMPlatformGetRouteMode mode); gboolean nm_platform_ip4_route_add (int ifindex, NMIPConfigSource source, in_addr_t network, int plen, in_addr_t gateway, guint32 metric, guint32 mss); diff --git a/src/platform/tests/dump.c b/src/platform/tests/dump.c index e97ef138cb..8aea3aec59 100644 --- a/src/platform/tests/dump.c +++ b/src/platform/tests/dump.c @@ -83,8 +83,8 @@ dump_interface (NMPlatformLink *link) g_array_unref (ip4_addresses); g_array_unref (ip6_addresses); - ip4_routes = nm_platform_ip4_route_get_all (link->ifindex, TRUE); - ip6_routes = nm_platform_ip6_route_get_all (link->ifindex, TRUE); + ip4_routes = nm_platform_ip4_route_get_all (link->ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL); + ip6_routes = nm_platform_ip6_route_get_all (link->ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL); g_assert (ip4_routes); g_assert (ip6_routes); diff --git a/src/platform/tests/platform.c b/src/platform/tests/platform.c index 90513d4579..a70d43601d 100644 --- a/src/platform/tests/platform.c +++ b/src/platform/tests/platform.c @@ -631,7 +631,7 @@ do_ip4_route_get_all (char **argv) int i; if (ifindex) { - routes = nm_platform_ip4_route_get_all (ifindex, TRUE); + routes = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL); for (i = 0; i < routes->len; i++) { route = &g_array_index (routes, NMPlatformIP4Route, i); inet_ntop (AF_INET, &route->network, networkstr, sizeof (networkstr)); @@ -655,7 +655,7 @@ do_ip6_route_get_all (char **argv) int i; if (ifindex) { - routes = nm_platform_ip6_route_get_all (ifindex, TRUE); + routes = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL); for (i = 0; i < routes->len; i++) { route = &g_array_index (routes, NMPlatformIP6Route, i); inet_ntop (AF_INET6, &route->network, networkstr, sizeof (networkstr)); diff --git a/src/platform/tests/test-cleanup.c b/src/platform/tests/test-cleanup.c index 646fb8a8db..6bf7f12396 100644 --- a/src/platform/tests/test-cleanup.c +++ b/src/platform/tests/test-cleanup.c @@ -52,8 +52,8 @@ test_cleanup_internal (void) addresses4 = nm_platform_ip4_address_get_all (ifindex); addresses6 = nm_platform_ip6_address_get_all (ifindex); - routes4 = nm_platform_ip4_route_get_all (ifindex, TRUE); - routes6 = nm_platform_ip6_route_get_all (ifindex, TRUE); + routes4 = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL); + routes6 = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL); g_assert_cmpint (addresses4->len, ==, 1); g_assert_cmpint (addresses6->len, ==, 1); @@ -70,8 +70,8 @@ test_cleanup_internal (void) addresses4 = nm_platform_ip4_address_get_all (ifindex); addresses6 = nm_platform_ip6_address_get_all (ifindex); - routes4 = nm_platform_ip4_route_get_all (ifindex, TRUE); - routes6 = nm_platform_ip6_route_get_all (ifindex, TRUE); + routes4 = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL); + routes6 = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL); g_assert_cmpint (addresses4->len, ==, 0); g_assert_cmpint (addresses6->len, ==, 0); diff --git a/src/platform/tests/test-route.c b/src/platform/tests/test-route.c index 44598f4c78..299eddda95 100644 --- a/src/platform/tests/test-route.c +++ b/src/platform/tests/test-route.c @@ -99,7 +99,7 @@ test_ip4_route (void) accept_signal (route_changed); /* Test route listing */ - routes = nm_platform_ip4_route_get_all (ifindex, TRUE); + routes = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL); memset (rts, 0, sizeof (rts)); rts[0].source = NM_IP_CONFIG_SOURCE_USER; rts[0].network = gateway; @@ -194,7 +194,7 @@ test_ip6_route (void) accept_signal (route_changed); /* Test route listing */ - routes = nm_platform_ip6_route_get_all (ifindex, TRUE); + routes = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ALL); memset (rts, 0, sizeof (rts)); rts[0].source = NM_IP_CONFIG_SOURCE_USER; rts[0].network = gateway; -- cgit v1.2.1 From 0c355ea5a0a59f688dcb2f07910a4a4be87ae843 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Nov 2014 10:56:21 +0100 Subject: platform: support route_get_all() to return route for every ifindex By passing an ifindex of 0, the search is not limited to a certain ifindex. Signed-off-by: Thomas Haller --- src/platform/nm-fake-platform.c | 26 ++++++-------------------- src/platform/nm-platform.c | 4 ++-- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c index f397f5b731..17304544be 100644 --- a/src/platform/nm-fake-platform.c +++ b/src/platform/nm-fake-platform.c @@ -988,23 +988,16 @@ ip4_route_get_all (NMPlatform *platform, int ifindex, NMPlatformGetRouteMode mod NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform); GArray *routes; NMPlatformIP4Route *route; - int count = 0, i; + guint i; g_return_val_if_fail (NM_IN_SET (mode, NM_PLATFORM_GET_ROUTE_MODE_ALL, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT), NULL); - /* Count routes */ - for (i = 0; i < priv->ip4_routes->len; i++) { - route = &g_array_index (priv->ip4_routes, NMPlatformIP4Route, i); - if (route && route->ifindex == ifindex) - count++; - } - - routes = g_array_sized_new (TRUE, TRUE, sizeof (NMPlatformIP4Route), count); + routes = g_array_new (TRUE, TRUE, sizeof (NMPlatformIP4Route)); /* Fill routes */ for (i = 0; i < priv->ip4_routes->len; i++) { route = &g_array_index (priv->ip4_routes, NMPlatformIP4Route, i); - if (route && route->ifindex == ifindex) { + if (route && (!ifindex || route->ifindex == ifindex)) { if (NM_PLATFORM_IP_ROUTE_IS_DEFAULT (route)) { if (mode != NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT) g_array_append_val (routes, *route); @@ -1024,23 +1017,16 @@ ip6_route_get_all (NMPlatform *platform, int ifindex, NMPlatformGetRouteMode mod NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform); GArray *routes; NMPlatformIP6Route *route; - int count = 0, i; + guint i; g_return_val_if_fail (NM_IN_SET (mode, NM_PLATFORM_GET_ROUTE_MODE_ALL, NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT), NULL); - /* Count routes */ - for (i = 0; i < priv->ip6_routes->len; i++) { - route = &g_array_index (priv->ip6_routes, NMPlatformIP6Route, i); - if (route && route->ifindex == ifindex) - count++; - } - - routes = g_array_sized_new (TRUE, TRUE, sizeof (NMPlatformIP6Route), count); + routes = g_array_new (TRUE, TRUE, sizeof (NMPlatformIP6Route)); /* Fill routes */ for (i = 0; i < priv->ip6_routes->len; i++) { route = &g_array_index (priv->ip6_routes, NMPlatformIP6Route, i); - if (route && route->ifindex == ifindex) { + if (route && (!ifindex || route->ifindex == ifindex)) { if (NM_PLATFORM_IP_ROUTE_IS_DEFAULT (route)) { if (mode != NM_PLATFORM_GET_ROUTE_MODE_NO_DEFAULT) g_array_append_val (routes, *route); diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 70395ca3cf..07e9979692 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -1841,7 +1841,7 @@ nm_platform_ip4_route_get_all (int ifindex, NMPlatformGetRouteMode mode) { reset_error (); - g_return_val_if_fail (ifindex > 0, NULL); + g_return_val_if_fail (ifindex >= 0, NULL); g_return_val_if_fail (klass->ip4_route_get_all, NULL); return klass->ip4_route_get_all (platform, ifindex, mode); @@ -1852,7 +1852,7 @@ nm_platform_ip6_route_get_all (int ifindex, NMPlatformGetRouteMode mode) { reset_error (); - g_return_val_if_fail (ifindex > 0, NULL); + g_return_val_if_fail (ifindex >= 0, NULL); g_return_val_if_fail (klass->ip6_route_get_all, NULL); return klass->ip6_route_get_all (platform, ifindex, mode); -- cgit v1.2.1 From f5c0646e1c0807aa4379ddde820a675e48c2f4bd Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 5 Nov 2014 08:28:40 +0100 Subject: device: add function nm_device_uses_assumed_connection() Signed-off-by: Thomas Haller --- src/devices/nm-device.c | 11 +++++++++++ src/devices/nm-device.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index f4a8a1c424..4d5f8a449b 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -775,6 +775,17 @@ nm_device_uses_generated_assumed_connection (NMDevice *self) return FALSE; } +gboolean +nm_device_uses_assumed_connection (NMDevice *self) +{ + NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); + + if ( priv->act_request + && nm_active_connection_get_assumed (NM_ACTIVE_CONNECTION (priv->act_request))) + return TRUE; + return FALSE; +} + static SlaveInfo * find_slave_info (NMDevice *self, NMDevice *slave) { diff --git a/src/devices/nm-device.h b/src/devices/nm-device.h index a2663a0253..6b732cae96 100644 --- a/src/devices/nm-device.h +++ b/src/devices/nm-device.h @@ -275,6 +275,8 @@ gboolean nm_device_complete_connection (NMDevice *device, gboolean nm_device_check_connection_compatible (NMDevice *device, NMConnection *connection); +gboolean nm_device_uses_assumed_connection (NMDevice *device); + gboolean nm_device_can_assume_active_connection (NMDevice *device); gboolean nm_device_spec_match_list (NMDevice *device, const GSList *specs); -- cgit v1.2.1 From 276424c881c25cecff60895d034a4919033d0537 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 19 Oct 2014 00:35:24 +0200 Subject: core: remove unneeded check in nm_ip4_config_commit() for default routes These lines are part of NM for a very long time. I think they are wrong, because the default route is not added to the NMIP4Config/NMIP6Config objects. Signed-off-by: Thomas Haller --- src/nm-ip4-config.c | 7 ------- src/nm-ip6-config.c | 7 ------- src/platform/nm-platform.c | 10 ++++------ 3 files changed, 4 insertions(+), 20 deletions(-) diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index a781f0342c..3566b68569 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -278,13 +278,6 @@ nm_ip4_config_commit (const NMIP4Config *config, int ifindex) && nm_ip4_config_destination_is_direct (config, route->network, route->plen)) continue; - /* Don't add the default route if the connection - * is never supposed to be the default connection. - */ - if ( nm_ip4_config_get_never_default (config) - && NM_PLATFORM_IP_ROUTE_IS_DEFAULT (route)) - continue; - g_array_append_vals (routes, route, 1); } diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index d901781ba4..3b0f291a91 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -391,13 +391,6 @@ nm_ip6_config_commit (const NMIP6Config *config, int ifindex) && nm_ip6_config_destination_is_direct (config, &route->network, route->plen)) continue; - /* Don't add the default route if the connection - * is never supposed to be the default connection. - */ - if ( nm_ip6_config_get_never_default (config) - && NM_PLATFORM_IP_ROUTE_IS_DEFAULT (route)) - continue; - g_array_append_vals (routes, route, 1); } diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 07e9979692..8edf46f75c 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -2010,9 +2010,8 @@ array_contains_ip6_route (const GArray *routes, const NMPlatformIP6Route *route) * A convenience function to synchronize routes for a specific interface * with the least possible disturbance. It simply removes routes that are * not listed and adds routes that are. - * - * @known_routes should not contain a default route; if it does, it will be - * ignored. + * Default routes are ignored (both in @known_routes and those already + * configured on the device). * * Returns: %TRUE on success. */ @@ -2083,9 +2082,8 @@ nm_platform_ip4_route_sync (int ifindex, const GArray *known_routes) * A convenience function to synchronize routes for a specific interface * with the least possible disturbance. It simply removes routes that are * not listed and adds routes that are. - * - * @known_routes should not contain a default route; if it does, it will be - * ignored. + * Default routes are ignored (both in @known_routes and those already + * configured on the device). * * Returns: %TRUE on success. */ -- cgit v1.2.1 From 22fe5d67a7d86b6703c1f6118256814c37a75e40 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 4 Nov 2014 10:20:28 +0100 Subject: vpn: add nm_vpn_connection_get_connection_id() function Signed-off-by: Thomas Haller --- src/vpn-manager/nm-vpn-connection.c | 11 +++++++++++ src/vpn-manager/nm-vpn-connection.h | 1 + 2 files changed, 12 insertions(+) diff --git a/src/vpn-manager/nm-vpn-connection.c b/src/vpn-manager/nm-vpn-connection.c index d41e2e2c67..444b1948d4 100644 --- a/src/vpn-manager/nm-vpn-connection.c +++ b/src/vpn-manager/nm-vpn-connection.c @@ -1703,6 +1703,17 @@ nm_vpn_connection_get_connection (NMVpnConnection *connection) return NM_VPN_CONNECTION_GET_PRIVATE (connection)->connection; } +const char* +nm_vpn_connection_get_connection_id (NMVpnConnection *connection) +{ + NMConnection *c; + + g_return_val_if_fail (NM_IS_VPN_CONNECTION (connection), NULL); + + c = NM_VPN_CONNECTION_GET_PRIVATE (connection)->connection; + return c ? nm_connection_get_id (c) : NULL; +} + NMVpnConnectionState nm_vpn_connection_get_vpn_state (NMVpnConnection *connection) { diff --git a/src/vpn-manager/nm-vpn-connection.h b/src/vpn-manager/nm-vpn-connection.h index 512bdf932a..c444bc13db 100644 --- a/src/vpn-manager/nm-vpn-connection.h +++ b/src/vpn-manager/nm-vpn-connection.h @@ -79,6 +79,7 @@ NMVpnConnection * nm_vpn_connection_new (NMConnection *connection, void nm_vpn_connection_activate (NMVpnConnection *connection); NMConnection * nm_vpn_connection_get_connection (NMVpnConnection *connection); +const char* nm_vpn_connection_get_connection_id (NMVpnConnection *connection); NMVpnConnectionState nm_vpn_connection_get_vpn_state (NMVpnConnection *connection); const char * nm_vpn_connection_get_banner (NMVpnConnection *connection); -- cgit v1.2.1 From 227aebf4b6d1cdf764f2eb75d2d1970038d80a91 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 4 Nov 2014 11:51:03 +0100 Subject: policy: fix updating the default route for VPN When adding a default route fails, the most common reason is that we don't have a direct route to the gateway. In that case, NMPolicy tries to add a direct route to the gateway and then retries adding the default route. For VPN however, previously NMPolicy would not added a direct route to the gateway via the VPN device. Instead it would add a direct route to the external gateway via the parent interface. That is wrong. Indeed the external gateway must be reachable directly not via the VPN interface itself. But for that the vpn connection already sets a route via nm_device_set_vpn4_config(). Signed-off-by: Thomas Haller --- src/nm-policy.c | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/nm-policy.c b/src/nm-policy.c index 480364e9b9..3394fc7e8a 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -675,27 +675,26 @@ update_ip4_routing (NMPolicy *policy, gboolean force_update) } if (vpn) { - NMDevice *parent = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn)); - int parent_ifindex = nm_device_get_ip_ifindex (parent); - NMIP4Config *parent_ip4 = nm_device_get_ip4_config (parent); - guint32 parent_mss = parent_ip4 ? nm_ip4_config_get_mss (parent_ip4) : 0; in_addr_t int_gw = nm_vpn_connection_get_ip4_internal_gateway (vpn); int mss = nm_ip4_config_get_mss (ip4_config); /* If no VPN interface, use the parent interface */ if (ip_ifindex <= 0) - ip_ifindex = parent_ifindex; + ip_ifindex = nm_device_get_ip_ifindex (nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn))); if (!nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, 0, 0, int_gw, NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) { - (void) nm_platform_ip4_route_add (parent_ifindex, NM_IP_CONFIG_SOURCE_VPN, - gw_addr, 32, 0, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, parent_mss); - if (!nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, - 0, 0, int_gw, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) - nm_log_err (LOGD_IP4 | LOGD_VPN, "Failed to set default route."); + if (int_gw) { + (void) nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, + int_gw, 32, 0, + NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss); + if (!nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, + 0, 0, int_gw, + NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) + nm_log_err (LOGD_IP4 | LOGD_VPN, "Failed to set IPv4 default route via VPN."); + } else + nm_log_err (LOGD_IP4 | LOGD_VPN, "Failed to set IPv4 default route via VPN."); } default_device = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn)); @@ -892,10 +891,6 @@ update_ip6_routing (NMPolicy *policy, gboolean force_update) } if (vpn) { - NMDevice *parent = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn)); - int parent_ifindex = nm_device_get_ip_ifindex (parent); - NMIP6Config *parent_ip6 = nm_device_get_ip6_config (parent); - guint32 parent_mss = parent_ip6 ? nm_ip6_config_get_mss (parent_ip6) : 0; const struct in6_addr *int_gw = nm_vpn_connection_get_ip6_internal_gateway (vpn); int mss = nm_ip6_config_get_mss (ip6_config); @@ -904,19 +899,21 @@ update_ip6_routing (NMPolicy *policy, gboolean force_update) /* If no VPN interface, use the parent interface */ if (ip_ifindex <= 0) - ip_ifindex = parent_ifindex; + ip_ifindex = nm_device_get_ip_ifindex (nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn))); if (!nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, in6addr_any, 0, *int_gw, NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) { - (void) nm_platform_ip6_route_add (parent_ifindex, NM_IP_CONFIG_SOURCE_VPN, - *gw_addr, 128, in6addr_any, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, parent_mss); - if (!nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, - in6addr_any, 0, *int_gw, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) { - nm_log_err (LOGD_IP6 | LOGD_VPN, "Failed to set default route."); - } + if (!IN6_IS_ADDR_UNSPECIFIED (int_gw)) { + (void) nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, + *int_gw, 128, in6addr_any, + NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss); + if (!nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, + in6addr_any, 0, *int_gw, + NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) + nm_log_err (LOGD_IP6 | LOGD_VPN, "Failed to set IPv6 default route via VPN."); + } else + nm_log_err (LOGD_IP6 | LOGD_VPN, "Failed to set IPv6 default route via VPN."); } default_device6 = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn)); -- cgit v1.2.1 From 2f90ecbfbb15c8ed2a4103083864f951274b8214 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 3 Nov 2014 12:13:14 +0100 Subject: policy: minor refactoring in get_best_ipx_device() In get_best_ip4_device() and get_best_ip6_device(), move conditions to check for suitable connection first. Makes the following patch more coherent. Signed-off-by: Thomas Haller --- src/nm-policy.c | 60 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/nm-policy.c b/src/nm-policy.c index 3394fc7e8a..1a12ea2777 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -121,22 +121,6 @@ get_best_ip4_device (NMPolicy *self, gboolean fully_activated) if (fully_activated && state < NM_DEVICE_STATE_SECONDARIES) continue; - if (fully_activated) { - NMIP4Config *ip4_config; - - ip4_config = nm_device_get_ip4_config (dev); - if (!ip4_config) - continue; - - /* Make sure the device has a gateway */ - if (!nm_ip4_config_get_gateway (ip4_config) && (devtype != NM_DEVICE_TYPE_MODEM)) - continue; - - /* 'never-default' devices can't ever be the default */ - if (nm_ip4_config_get_never_default (ip4_config)) - continue; - } - req = nm_device_get_act_request (dev); g_assert (req); connection = nm_act_request_get_connection (req); @@ -154,6 +138,22 @@ get_best_ip4_device (NMPolicy *self, gboolean fully_activated) if (nm_setting_ip_config_get_never_default (s_ip4)) continue; + if (fully_activated) { + NMIP4Config *ip4_config; + + ip4_config = nm_device_get_ip4_config (dev); + if (!ip4_config) + continue; + + /* Make sure the device has a gateway */ + if (!nm_ip4_config_get_gateway (ip4_config) && (devtype != NM_DEVICE_TYPE_MODEM)) + continue; + + /* 'never-default' devices can't ever be the default */ + if (nm_ip4_config_get_never_default (ip4_config)) + continue; + } + prio = nm_device_get_priority (dev); if ( prio < best_prio || (priv->default_device4 == dev && prio == best_prio) @@ -205,20 +205,6 @@ get_best_ip6_device (NMPolicy *self, gboolean fully_activated) if (fully_activated && state < NM_DEVICE_STATE_SECONDARIES) continue; - if (fully_activated) { - NMIP6Config *ip6_config; - - ip6_config = nm_device_get_ip6_config (dev); - if (!ip6_config) - continue; - - if (!nm_ip6_config_get_gateway (ip6_config) && (devtype != NM_DEVICE_TYPE_MODEM)) - continue; - - if (nm_ip6_config_get_never_default (ip6_config)) - continue; - } - req = nm_device_get_act_request (dev); g_assert (req); connection = nm_act_request_get_connection (req); @@ -234,6 +220,20 @@ get_best_ip6_device (NMPolicy *self, gboolean fully_activated) if (nm_setting_ip_config_get_never_default (s_ip6)) continue; + if (fully_activated) { + NMIP6Config *ip6_config; + + ip6_config = nm_device_get_ip6_config (dev); + if (!ip6_config) + continue; + + if (!nm_ip6_config_get_gateway (ip6_config) && (devtype != NM_DEVICE_TYPE_MODEM)) + continue; + + if (nm_ip6_config_get_never_default (ip6_config)) + continue; + } + prio = nm_device_get_priority (dev); if ( prio < best_prio || (priv->default_device6 == dev && prio == best_prio) -- cgit v1.2.1 From cc9fad612e4885cee7099bda8c4ff21ef661ebaa Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 6 Nov 2014 22:02:19 +0100 Subject: policy: remove redundant check for never-default in get_best_ipx_config() get_best_ip4_config() and get_best_ip6_config() checked both for never-default of the setting. This check was redundant, because the never-default value was already merged into NMIPXConfig. Signed-off-by: Thomas Haller --- src/nm-policy.c | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/src/nm-policy.c b/src/nm-policy.c index 1a12ea2777..73f3bf4846 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -541,7 +541,6 @@ get_best_ip4_config (NMPolicy *policy, NMVpnConnection *candidate; NMIP4Config *vpn_ip4; NMConnection *tmp; - NMSettingIPConfig *s_ip4; NMVpnConnectionState vpn_state; if (!NM_IS_VPN_CONNECTION (active)) @@ -560,16 +559,8 @@ get_best_ip4_config (NMPolicy *policy, if (!vpn_ip4) continue; - if (ignore_never_default == FALSE) { - /* Check for a VPN-provided config never-default */ - if (nm_ip4_config_get_never_default (vpn_ip4)) - continue; - - /* Check the user's preference from the NMConnection */ - s_ip4 = nm_connection_get_setting_ip4_config (tmp); - if (nm_setting_ip_config_get_never_default (s_ip4)) - continue; - } + if (!ignore_never_default && nm_ip4_config_get_never_default (vpn_ip4)) + continue; ip4_config = vpn_ip4; if (out_vpn) @@ -752,7 +743,6 @@ get_best_ip6_config (NMPolicy *policy, NMVpnConnection *candidate; NMIP6Config *vpn_ip6; NMConnection *tmp; - NMSettingIPConfig *s_ip6; NMVpnConnectionState vpn_state; if (!NM_IS_VPN_CONNECTION (active)) @@ -771,16 +761,8 @@ get_best_ip6_config (NMPolicy *policy, if (!vpn_ip6) continue; - if (ignore_never_default == FALSE) { - /* Check for a VPN-provided config never-default */ - if (nm_ip6_config_get_never_default (vpn_ip6)) - continue; - - /* Check the user's preference from the NMConnection */ - s_ip6 = nm_connection_get_setting_ip6_config (tmp); - if (nm_setting_ip_config_get_never_default (s_ip6)) - continue; - } + if (!ignore_never_default && nm_ip6_config_get_never_default (vpn_ip6)) + continue; ip6_config = vpn_ip6; if (out_vpn) -- cgit v1.2.1 From e8824f6a5205ffcf761abd3e0897a22b254c7797 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 29 Aug 2014 00:03:47 +0200 Subject: policy: add manager for default routes and support multiple default routes Up to now, NMPolicy would iterate over all devices to find the "best" device and assign the default route to that device. A better approach is to add a default route to *all* devices that are never-default=no. The relative priority is choosen according to the route metrics. If two devices receive the same metric, we want to prefer the device that activates first. That way, the default route sticks to the same device until a better device activates or the device deactivates. Hence, the order of activation is imporant in this case (as it is already now). Also, if several devices have identical metrics, increment their metrics so that every metric is unique. This makes the routing deterministic according to what we choose as best device. A special case is assumed devices. In this case we cannot adjust the metric in face of equal metrics. Add a new singleton class NMDefaultRouteManager that has a list of all devices and their default routes. The manager will order the devices by their priority and configure the routes using platform. Also update the metric for VPN connections. Later we will track VPN routes also via NMDefaultRouteManager. For now, fix the VPN metric because otherwise VPNs would always get metric 1024 (which is usually much larger then the device metrics). https://bugzilla.gnome.org/show_bug.cgi?id=735512 Signed-off-by: Thomas Haller --- src/Makefile.am | 2 + src/devices/nm-device.c | 201 +++++++++++- src/devices/nm-device.h | 3 + src/nm-default-route-manager.c | 715 +++++++++++++++++++++++++++++++++++++++++ src/nm-default-route-manager.h | 64 ++++ src/nm-ip4-config.c | 8 +- src/nm-ip6-config.c | 10 +- src/nm-policy.c | 136 ++------ src/nm-types.h | 1 + 9 files changed, 1021 insertions(+), 119 deletions(-) create mode 100644 src/nm-default-route-manager.c create mode 100644 src/nm-default-route-manager.h diff --git a/src/Makefile.am b/src/Makefile.am index 60f7ef6a30..a0447bdd42 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -281,6 +281,8 @@ nm_sources = \ nm-dbus-manager.h \ nm-dcb.c \ nm-dcb.h \ + nm-default-route-manager.c \ + nm-default-route-manager.h \ nm-dhcp4-config.c \ nm-dhcp4-config.h \ nm-dhcp6-config.c \ diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 4d5f8a449b..3d7a34ba8a 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -68,6 +68,7 @@ #include "nm-config.h" #include "nm-dns-manager.h" #include "nm-core-internal.h" +#include "nm-default-route-manager.h" #include "nm-device-logging.h" _LOG_DECLARE_SELF (NMDevice); @@ -237,6 +238,12 @@ typedef struct { NMIP4Config * dev_ip4_config; /* Config from DHCP, PPP, LLv4, etc */ NMIP4Config * ext_ip4_config; /* Stuff added outside NM */ NMIP4Config * wwan_ip4_config; /* WWAN configuration */ + struct { + gboolean v4_has; + NMPlatformIP4Route v4; + gboolean v6_has; + NMPlatformIP6Route v6; + } default_route; /* DHCPv4 tracking */ NMDhcpClient * dhcp4_client; @@ -713,6 +720,30 @@ nm_device_get_ip6_route_metric (NMDevice *self) return route_metric >= 0 ? route_metric : nm_device_get_priority (self); } +const NMPlatformIP4Route * +nm_device_get_ip4_default_route (NMDevice *self) +{ + NMDevicePrivate *priv; + + g_return_val_if_fail (NM_IS_DEVICE (self), NULL); + + priv = NM_DEVICE_GET_PRIVATE (self); + + return priv->default_route.v4_has ? &priv->default_route.v4 : NULL; +} + +const NMPlatformIP6Route * +nm_device_get_ip6_default_route (NMDevice *self) +{ + NMDevicePrivate *priv; + + g_return_val_if_fail (NM_IS_DEVICE (self), NULL); + + priv = NM_DEVICE_GET_PRIVATE (self); + + return priv->default_route.v6_has ? &priv->default_route.v6 : NULL; +} + const char * nm_device_get_type_desc (NMDevice *self) { @@ -2636,6 +2667,52 @@ aipd_start (NMDevice *self, NMDeviceStateReason *reason) return NM_ACT_STAGE_RETURN_POSTPONE; } +/*********************************************/ + +static gboolean +_device_get_default_route_from_platform (NMDevice *self, int addr_family, NMPlatformIPRoute *out_route) +{ + gboolean success = FALSE; + int ifindex = nm_device_get_ip_ifindex (self); + GArray *routes; + + if (addr_family == AF_INET) + routes = nm_platform_ip4_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT); + else + routes = nm_platform_ip6_route_get_all (ifindex, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT); + + if (routes) { + guint route_metric = G_MAXUINT32, m; + const NMPlatformIPRoute *route = NULL, *r; + guint i; + + /* if there are several default routes, find the one with the best metric */ + for (i = 0; i < routes->len; i++) { + if (addr_family == AF_INET) { + r = (const NMPlatformIPRoute *) &g_array_index (routes, NMPlatformIP4Route, i); + m = r->metric; + } else { + r = (const NMPlatformIPRoute *) &g_array_index (routes, NMPlatformIP6Route, i); + m = nm_utils_ip6_route_metric_normalize (r->metric); + } + if (!route || m < route_metric) { + route = r; + route_metric = m; + } + } + + if (route) { + if (addr_family == AF_INET) + *((NMPlatformIP4Route *) out_route) = *((NMPlatformIP4Route *) route); + else + *((NMPlatformIP6Route *) out_route) = *((NMPlatformIP6Route *) route); + success = TRUE; + } + g_array_free (routes, TRUE); + } + return success; +} + /*********************************************/ /* DHCPv4 stuff */ @@ -2701,11 +2778,58 @@ ip4_config_merge_and_apply (NMDevice *self, * be redundant, so don't bother. */ connection = nm_device_get_connection (self); - if ( connection - && !nm_settings_connection_get_nm_generated_assumed (NM_SETTINGS_CONNECTION (connection))) { - nm_ip4_config_merge_setting (composite, - nm_connection_get_setting_ip4_config (connection), - nm_device_get_ip4_route_metric (self)); + priv->default_route.v4_has = FALSE; + if (connection) { + gboolean assumed = nm_device_uses_assumed_connection (self); + + if (!nm_settings_connection_get_nm_generated_assumed (NM_SETTINGS_CONNECTION (connection))) { + nm_ip4_config_merge_setting (composite, + nm_connection_get_setting_ip4_config (connection), + nm_device_get_ip4_route_metric (self)); + } + + /* Add the default route. + * + * We keep track of the default route of a device in a private field. + * NMDevice needs to know the default route at this point, because the gateway + * might require a direct route (see below). + * + * But also, we don't want to add the default route to priv->ip4_config, + * because the default route from the setting might not be the same that + * NMDefaultRouteManager eventually configures (because the it might + * tweak the effective metric). + */ + if (nm_default_route_manager_ip4_connection_has_default_route (nm_default_route_manager_get (), connection)) { + guint32 gateway = 0; + NMPlatformIP4Route *route = &priv->default_route.v4; + + if (assumed) + priv->default_route.v4_has = _device_get_default_route_from_platform (self, AF_INET, (NMPlatformIPRoute *) route); + else { + gateway = nm_ip4_config_get_gateway (composite); + if ( gateway + || nm_device_get_device_type (self) == NM_DEVICE_TYPE_MODEM) { + memset (route, 0, sizeof (*route)); + route->source = NM_IP_CONFIG_SOURCE_USER; + route->gateway = gateway; + route->metric = nm_device_get_ip4_route_metric (self); + route->mss = nm_ip4_config_get_mss (composite); + priv->default_route.v4_has = TRUE; + + if ( gateway + && !nm_ip4_config_get_subnet_for_host (composite, gateway) + && !nm_ip4_config_get_direct_route_for_host (composite, gateway)) { + /* add a direct route to the gateway */ + NMPlatformIP4Route r = *route; + + r.network = gateway; + r.plen = 32; + r.gateway = 0; + nm_ip4_config_add_route (composite, &r); + } + } + } + } } /* Allow setting MTU etc */ @@ -3202,11 +3326,58 @@ ip6_config_merge_and_apply (NMDevice *self, * be redundant, so don't bother. */ connection = nm_device_get_connection (self); - if ( connection - && !nm_settings_connection_get_nm_generated_assumed (NM_SETTINGS_CONNECTION (connection))) { - nm_ip6_config_merge_setting (composite, - nm_connection_get_setting_ip6_config (connection), - nm_device_get_ip6_route_metric (self)); + priv->default_route.v6_has = FALSE; + if (connection) { + gboolean assumed = nm_device_uses_assumed_connection (self); + + if (!nm_settings_connection_get_nm_generated_assumed (NM_SETTINGS_CONNECTION (connection))) { + nm_ip6_config_merge_setting (composite, + nm_connection_get_setting_ip6_config (connection), + nm_device_get_ip6_route_metric (self)); + } + + /* Add the default route. + * + * We keep track of the default route of a device in a private field. + * NMDevice needs to know the default route at this point, because the gateway + * might require a direct route (see below). + * + * But also, we don't want to add the default route to priv->ip4_config, + * because the default route from the setting might not be the same that + * NMDefaultRouteManager eventually configures (because the it might + * tweak the effective metric). + */ + if (nm_default_route_manager_ip6_connection_has_default_route (nm_default_route_manager_get (), connection)) { + const struct in6_addr *gateway = NULL; + NMPlatformIP6Route *route = &priv->default_route.v6; + + if (assumed) + priv->default_route.v6_has = _device_get_default_route_from_platform (self, AF_INET, (NMPlatformIPRoute *) route); + else { + gateway = nm_ip6_config_get_gateway (composite); + if ( gateway + || nm_device_get_device_type (self) == NM_DEVICE_TYPE_MODEM) { + memset (route, 0, sizeof (*route)); + route->source = NM_IP_CONFIG_SOURCE_USER; + route->gateway = gateway ? *gateway : in6addr_any; + route->metric = nm_device_get_ip6_route_metric (self); + route->mss = nm_ip6_config_get_mss (composite); + priv->default_route.v6_has = TRUE; + + if ( gateway + && !nm_ip6_config_get_subnet_for_host (composite, gateway) + && !nm_ip6_config_get_direct_route_for_host (composite, gateway)) { + /* add a direct route to the gateway */ + NMPlatformIP6Route r = *route; + + r.network = *gateway; + r.plen = 128; + r.gateway = in6addr_any; + nm_ip6_config_add_route (composite, &r); + } + } + } + } } nm_ip6_config_addresses_sort (composite, @@ -5405,6 +5576,8 @@ nm_device_set_ip4_config (NMDevice *self, g_clear_object (&priv->dev_ip4_config); } + nm_default_route_manager_ip4_update_default_route (nm_default_route_manager_get (), self); + if (has_changes) { _update_ip4_address (self); @@ -5526,6 +5699,8 @@ nm_device_set_ip6_config (NMDevice *self, nm_ip6_config_get_dbus_path (old_config)); } + nm_default_route_manager_ip6_update_default_route (nm_default_route_manager_get (), self); + if (has_changes) { if (old_config != priv->ip6_config) g_object_notify (G_OBJECT (self), NM_DEVICE_IP6_CONFIG); @@ -6720,6 +6895,12 @@ _cleanup_generic_post (NMDevice *self, gboolean deconfigure) NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); NMDeviceStateReason ignored = NM_DEVICE_STATE_REASON_NONE; + priv->default_route.v4_has = FALSE; + priv->default_route.v6_has = FALSE; + + nm_default_route_manager_ip4_remove_default_route (nm_default_route_manager_get (), self); + nm_default_route_manager_ip6_remove_default_route (nm_default_route_manager_get (), self); + /* Clean up IP configs; this does not actually deconfigure the * interface; the caller must flush routes and addresses explicitly. */ diff --git a/src/devices/nm-device.h b/src/devices/nm-device.h index 6b732cae96..f7b91e00cc 100644 --- a/src/devices/nm-device.h +++ b/src/devices/nm-device.h @@ -362,6 +362,9 @@ gboolean nm_device_owns_iface (NMDevice *device, const char *iface); NMConnection *nm_device_new_default_connection (NMDevice *self); +const NMPlatformIP4Route *nm_device_get_ip4_default_route (NMDevice *self); +const NMPlatformIP6Route *nm_device_get_ip6_default_route (NMDevice *self); + G_END_DECLS /* For testing only */ diff --git a/src/nm-default-route-manager.c b/src/nm-default-route-manager.c new file mode 100644 index 0000000000..2ff75ae34e --- /dev/null +++ b/src/nm-default-route-manager.c @@ -0,0 +1,715 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2014 Red Hat, Inc. + */ + + +#include "nm-default-route-manager.h" + +#include "config.h" + +#include "string.h" + +#include "nm-logging.h" +#include "nm-device.h" +#include "nm-platform.h" +#include "nm-ip4-config.h" +#include "nm-ip6-config.h" + +typedef struct { + GPtrArray *entries_ip4; + GPtrArray *entries_ip6; +} NMDefaultRouteManagerPrivate; + +#define NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEFAULT_ROUTE_MANAGER, NMDefaultRouteManagerPrivate)) + +G_DEFINE_TYPE (NMDefaultRouteManager, nm_default_route_manager, G_TYPE_OBJECT) + +static NMDefaultRouteManager *_instance; + +#define _LOG(level, addr_family, ...) \ + G_STMT_START { \ + int __addr_family = (addr_family); \ + guint64 __domain = __addr_family == AF_INET ? LOGD_IP4 : LOGD_IP6; \ + \ + if (nm_logging_enabled ((level), (__domain))) { \ + char __ch = __addr_family == AF_INET ? '4' : '6'; \ + char __prefix[30] = "default-route"; \ + \ + if ((self) != _instance) \ + g_snprintf (__prefix, sizeof (__prefix), "default-route%c[%p]", __ch, (self)); \ + else \ + __prefix[STRLEN ("default-route")] = __ch; \ + nm_log ((level), (__domain), \ + "%s: " _NM_UTILS_MACRO_FIRST(__VA_ARGS__), \ + __prefix _NM_UTILS_MACRO_REST(__VA_ARGS__)); \ + } \ + } G_STMT_END + +#define _LOGD(addr_family, ...) _LOG (LOGL_DEBUG, addr_family, __VA_ARGS__) +#define _LOGI(addr_family, ...) _LOG (LOGL_INFO , addr_family, __VA_ARGS__) +#define _LOGW(addr_family, ...) _LOG (LOGL_WARN , addr_family, __VA_ARGS__) +#define _LOGE(addr_family, ...) _LOG (LOGL_ERR , addr_family, __VA_ARGS__) + +#define LOG_ENTRY_FMT "entry[%u/%s:%p:%s]" +#define LOG_ENTRY_ARGS(entry_idx, entry) \ + entry_idx, \ + NM_IS_DEVICE (entry->source.pointer) ? "dev" : "vpn", \ + entry->source.pointer, \ + nm_device_get_iface (entry->source.device) + +/***********************************************************************************/ + +typedef struct { + union { + void *pointer; + GObject *object; + NMDevice *device; + } source; + union { + NMPlatformIPRoute route; + NMPlatformIP4Route route4; + NMPlatformIP6Route route6; + }; + gboolean synced; /* if true, we synced the entry to platform. We don't sync assumed devices */ + guint32 effective_metric; +} Entry; + +typedef struct { + int addr_family; + GPtrArray *(*get_entries) (NMDefaultRouteManagerPrivate *priv); + const char *(*platform_route_to_string) (const NMPlatformIPRoute *route); + gboolean (*platform_route_delete_default) (int ifindex, guint32 metric); + guint32 (*route_metric_normalize) (guint32 metric); +} VTableIP; + +static const VTableIP vtable_ip4, vtable_ip6; + +#define VTABLE_IS_IP4 (vtable->addr_family == AF_INET) + +static void +_entry_free (Entry *entry) +{ + if (entry) { + g_object_unref (entry->source.object); + g_slice_free (Entry, entry); + } +} + +static Entry * +_entry_find_by_source (GPtrArray *entries, gpointer source, guint *out_idx) +{ + guint i; + + for (i = 0; i < entries->len; i++) { + Entry *e = g_ptr_array_index (entries, i); + + if (e->source.pointer == source) { + if (out_idx) + *out_idx = i; + return e; + } + } + + if (out_idx) + *out_idx = G_MAXUINT; + return NULL; +} + +static void +_platform_route_sync_add (const VTableIP *vtable, NMDefaultRouteManager *self, guint32 metric) +{ + NMDefaultRouteManagerPrivate *priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + GPtrArray *entries = vtable->get_entries (priv); + guint i; + gboolean has_unsynced_entry = FALSE; + Entry *entry = NULL; + gboolean success; + + /* Find the entries for the given metric. + * The effective metric for synced entries is choosen in a way that it + * is unique (except for G_MAXUINT32, where a clash is not solvable). */ + for (i = 0; i < entries->len; i++) { + Entry *e = g_ptr_array_index (entries, i); + + if (e->effective_metric != metric) + continue; + + if (e->synced) { + g_assert (!entry || metric == G_MAXUINT32); + entry = e; + } else + has_unsynced_entry = TRUE; + } + + /* For synced entries, we expect that the metric is chosen uniquely. */ + g_assert (!entry || !has_unsynced_entry || metric == G_MAXUINT32); + + /* we only add the route, if we have an (to be synced) entry for it. */ + if (!entry) + return; + + if (VTABLE_IS_IP4) { + success = nm_platform_ip4_route_add (entry->route.ifindex, + entry->route.source, + 0, + 0, + entry->route4.gateway, + entry->effective_metric, + entry->route.mss); + } else { + success = nm_platform_ip6_route_add (entry->route.ifindex, + entry->route.source, + in6addr_any, + 0, + entry->route6.gateway, + entry->effective_metric, + entry->route.mss); + } + if (!success) + _LOGW (vtable->addr_family, "failed to add default route %s with effective metric %u", vtable->platform_route_to_string (&entry->route), (guint) entry->effective_metric); +} + +static void +_platform_route_sync_flush (const VTableIP *vtable, NMDefaultRouteManager *self) +{ + NMDefaultRouteManagerPrivate *priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + GPtrArray *entries = vtable->get_entries (priv); + GArray *routes; + guint i; + + /* prune all other default routes from this device. */ + if (VTABLE_IS_IP4) + routes = nm_platform_ip4_route_get_all (0, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT); + else + routes = nm_platform_ip6_route_get_all (0, NM_PLATFORM_GET_ROUTE_MODE_ONLY_DEFAULT); + + for (i = 0; i < routes->len; i++) { + const NMPlatformIPRoute *route; + gboolean has_ifindex_synced = FALSE; + Entry *entry = NULL; + + if (VTABLE_IS_IP4) + route = (const NMPlatformIPRoute *) &g_array_index (routes, NMPlatformIP4Route, i); + else + route = (const NMPlatformIPRoute *) &g_array_index (routes, NMPlatformIP6Route, i); + + /* look at all entires and see if the route for this ifindex pair is + * a known entry. */ + for (i = 0; i < entries->len; i++) { + Entry *e = g_ptr_array_index (entries, i); + + if ( e->route.ifindex == route->ifindex + && e->synced) { + has_ifindex_synced = TRUE; + if (e->effective_metric == route->metric) + entry = e; + } + } + + /* we only delete the route if we don't have a matching entry, + * and there is at least one entry that references this ifindex + * (indicating that the ifindex is managed by us -- not assumed). + * + * Otherwise, don't delete the route because it's configured + * externally (and will be assumed -- or already is assumed). + */ + if (has_ifindex_synced && !entry) + vtable->platform_route_delete_default (route->ifindex, route->metric); + } + g_array_free (routes, TRUE); +} + +static int +_sort_entries_cmp (gconstpointer a, gconstpointer b, gpointer user_data) +{ + guint32 m_a, m_b; + const Entry *e_a = *((const Entry **) a); + const Entry *e_b = *((const Entry **) b); + + /* when comparing routes, we consider the (original) metric. */ + m_a = e_a->route.metric; + m_b = e_b->route.metric; + + /* we normalize route.metric already in _ipx_update_default_route(). + * so we can just compare the metrics numerically */ + + if (m_a != m_b) + return (m_a < m_b) ? -1 : 1; + + /* If the metrics are equal, we prefer the one that is assumed (!synced). + * Entries that we sync, can be modified so that only the best + * entry has a (deterministically) lowest metric. + * With assumed devices we cannot increase/change the metric. + * For example: two devices, both metric 0. One is assumed the other is + * synced. + * If we would choose the synced entry as best, we cannot + * increase the metric of the assumed one and we would have non-determinism. + * If we instead prefer the assumed device, we can increase the metric + * of the synced device and the assumed device is (deterministically) + * prefered. + * If both devices are assumed, we also have non-determinism, but also + * we don't reorder either. + */ + if (!!e_a->synced != !!e_b->synced) + return e_a->synced ? 1 : -1; + + /* otherwise, do not reorder */ + return 0; +} + +static void +_resync_all (const VTableIP *vtable, NMDefaultRouteManager *self, const Entry *changed_entry, const Entry *old_entry, gboolean do_sync) +{ + NMDefaultRouteManagerPrivate *priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + Entry *entry; + guint i; + gint64 last_metric = -1; + guint32 expected_metric; + GPtrArray *entries; + GHashTableIter iter; + gpointer ptr; + GHashTable *changed_metrics = g_hash_table_new (NULL, NULL); + + entries = vtable->get_entries (priv); + + if (old_entry && old_entry->synced) { + /* The old version obviously changed. */ + g_hash_table_add (changed_metrics, GUINT_TO_POINTER (old_entry->effective_metric)); + } + + /* first iterate over all entries and adjust the effective metrics. */ + for (i = 0; i < entries->len; i++) { + entry = g_ptr_array_index (entries, i); + + g_assert (entry != old_entry); + + if (!entry->synced) { + last_metric = MAX (last_metric, (gint64) entry->effective_metric); + continue; + } + + expected_metric = entry->route.metric; + if ((gint64) expected_metric <= last_metric) + expected_metric = last_metric == G_MAXUINT32 ? G_MAXUINT32 : last_metric + 1; + + if (changed_entry == entry) { + /* for the changed entry, the previous metric was either old_entry->effective_metric, + * or none. Hence, we only have to remember what is going to change. */ + g_hash_table_add (changed_metrics, GUINT_TO_POINTER (expected_metric)); + if (old_entry) + _LOGD (vtable->addr_family, LOG_ENTRY_FMT": update %s (%u -> %u)", LOG_ENTRY_ARGS (i, entry), vtable->platform_route_to_string (&entry->route), (guint) old_entry->effective_metric, (guint) expected_metric); + else + _LOGD (vtable->addr_family, LOG_ENTRY_FMT": add %s (%u)", LOG_ENTRY_ARGS (i, entry), vtable->platform_route_to_string (&entry->route), (guint) expected_metric); + } else if (entry->effective_metric != expected_metric) { + g_hash_table_add (changed_metrics, GUINT_TO_POINTER (entry->effective_metric)); + g_hash_table_add (changed_metrics, GUINT_TO_POINTER (expected_metric)); + _LOGD (vtable->addr_family, LOG_ENTRY_FMT": resync metric %s (%u -> %u)", LOG_ENTRY_ARGS (i, entry), vtable->platform_route_to_string (&entry->route), (guint) entry->effective_metric, (guint) expected_metric); + } + + entry->effective_metric = expected_metric; + last_metric = expected_metric; + } + + if (do_sync) { + g_hash_table_iter_init (&iter, changed_metrics); + while (g_hash_table_iter_next (&iter, &ptr, NULL)) + _platform_route_sync_add (vtable, self, GPOINTER_TO_UINT (ptr)); + _platform_route_sync_flush (vtable, self); + } + + g_hash_table_unref (changed_metrics); +} + +static void +_entry_at_idx_update (const VTableIP *vtable, NMDefaultRouteManager *self, guint entry_idx, const Entry *old_entry, gboolean do_sync) +{ + NMDefaultRouteManagerPrivate *priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + Entry *entry; + NMDevice *device; + GPtrArray *entries; + + entries = vtable->get_entries (priv); + g_assert (entry_idx < entries->len); + + entry = g_ptr_array_index (entries, entry_idx); + + g_return_if_fail (NM_IS_DEVICE (entry->source.pointer)); + device = entry->source.device; + + g_assert ( !old_entry + || (entry->source.pointer == old_entry->source.pointer && entry->route.ifindex == old_entry->route.ifindex)); + + if (!entry->synced) { + entry->effective_metric = entry->route.metric; + _LOGD (vtable->addr_family, LOG_ENTRY_FMT": %s %s%s", + LOG_ENTRY_ARGS (entry_idx, entry), + old_entry ? "update" : "add", + vtable->platform_route_to_string (&entry->route), + entry->synced ? "" : " (not synced)"); + } + + g_ptr_array_sort_with_data (entries, _sort_entries_cmp, NULL); + + _resync_all (vtable, self, entry, old_entry, do_sync); +} + +static void +_entry_at_idx_remove (const VTableIP *vtable, NMDefaultRouteManager *self, guint entry_idx, gboolean do_sync) +{ + NMDefaultRouteManagerPrivate *priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + Entry *entry; + GPtrArray *entries; + + entries = vtable->get_entries (priv); + + g_assert (entry_idx < entries->len); + + entry = g_ptr_array_index (entries, entry_idx); + + _LOGD (vtable->addr_family, LOG_ENTRY_FMT": remove %s (%u%s)", LOG_ENTRY_ARGS (entry_idx, entry), vtable->platform_route_to_string (&entry->route), (guint) entry->effective_metric, entry->synced ? "" : ", not synced"); + + /* Remove the entry from the list (but don't free it yet) */ + g_ptr_array_index (entries, entry_idx) = NULL; + g_ptr_array_remove_index (entries, entry_idx); + + _resync_all (vtable, self, NULL, entry, do_sync); + + _entry_free (entry); +} + +/***********************************************************************************/ + +static void +_ipx_update_default_route (const VTableIP *vtable, NMDefaultRouteManager *self, gpointer source) +{ + NMDefaultRouteManagerPrivate *priv; + Entry *entry; + guint entry_idx; + const NMPlatformIPRoute *default_route = NULL; + int ip_ifindex; + GPtrArray *entries; + NMDevice *device = NULL; + gboolean synced; + + g_return_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self)); + if (NM_IS_DEVICE (source)) + device = source; + else + g_return_if_reached (); + + ip_ifindex = nm_device_get_ip_ifindex (device); + + priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + + entries = vtable->get_entries (priv); + entry = _entry_find_by_source (entries, source, &entry_idx); + + if ( entry + && entry->route.ifindex != ip_ifindex) { + /* Strange... the ifindex changed... Remove the device and start again. */ + _LOGD (vtable->addr_family, "ifindex of "LOG_ENTRY_FMT" changed: %d -> %d", + LOG_ENTRY_ARGS (entry_idx, entry), + entry->route.ifindex, ip_ifindex); + + g_object_freeze_notify (G_OBJECT (self)); + _entry_at_idx_remove (vtable, self, entry_idx, FALSE); + g_assert (!_entry_find_by_source (entries, source, NULL)); + _ipx_update_default_route (vtable, self, source); + g_object_thaw_notify (G_OBJECT (self)); + return; + } + + /* get the @default_route from the device. */ + if (ip_ifindex > 0) { + if (VTABLE_IS_IP4) + default_route = (const NMPlatformIPRoute *) nm_device_get_ip4_default_route (device); + else + default_route = (const NMPlatformIPRoute *) nm_device_get_ip6_default_route (device); + } + g_assert (!default_route || default_route->plen == 0); + + /* if the device uses an assumed connection, we don't sync the route. */ + synced = !nm_device_uses_assumed_connection (device); + + if (!entry && !default_route) + /* nothing to do */; + else if (!entry) { + /* add */ + entry = g_slice_new0 (Entry); + entry->source.object = g_object_ref (source); + + if (VTABLE_IS_IP4) + entry->route4 = *((const NMPlatformIP4Route *) default_route); + else + entry->route6 = *((const NMPlatformIP6Route *) default_route); + + /* only use normalized metrics */ + entry->route.metric = vtable->route_metric_normalize (entry->route.metric); + entry->route.ifindex = ip_ifindex; + entry->effective_metric = entry->route.metric; + entry->synced = synced; + + g_ptr_array_add (entries, entry); + _entry_at_idx_update (vtable, self, entries->len - 1, NULL, TRUE); + } else if (default_route) { + /* update */ + Entry old_entry, new_entry; + + new_entry = *entry; + if (VTABLE_IS_IP4) + new_entry.route4 = *((const NMPlatformIP4Route *) default_route); + else + new_entry.route6 = *((const NMPlatformIP6Route *) default_route); + /* only use normalized metrics */ + new_entry.route.metric = vtable->route_metric_normalize (new_entry.route.metric); + new_entry.route.ifindex = ip_ifindex; + new_entry.synced = synced; + + if (memcmp (entry, &new_entry, sizeof (new_entry)) == 0) + return; + + old_entry = *entry; + *entry = new_entry; + _entry_at_idx_update (vtable, self, entry_idx, &old_entry, TRUE); + } else { + /* delete */ + _entry_at_idx_remove (vtable, self, entry_idx, TRUE); + } +} + +void +nm_default_route_manager_ip4_update_default_route (NMDefaultRouteManager *self, gpointer source) +{ + _ipx_update_default_route (&vtable_ip4, self, source); +} + +void +nm_default_route_manager_ip6_update_default_route (NMDefaultRouteManager *self, gpointer source) +{ + _ipx_update_default_route (&vtable_ip6, self, source); +} + +/***********************************************************************************/ + +static void +_ipx_remove_default_route (const VTableIP *vtable, NMDefaultRouteManager *self, gpointer source) +{ + NMDefaultRouteManagerPrivate *priv; + guint entry_idx; + + g_return_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self)); + g_return_if_fail (NM_IS_DEVICE (source)); + + priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + + if (_entry_find_by_source (vtable->get_entries (priv), source, &entry_idx)) + _entry_at_idx_remove (vtable, self, entry_idx, TRUE); +} + +void +nm_default_route_manager_ip4_remove_default_route (NMDefaultRouteManager *self, gpointer source) +{ + _ipx_remove_default_route (&vtable_ip4, self, source); +} + +void +nm_default_route_manager_ip6_remove_default_route (NMDefaultRouteManager *self, gpointer source) +{ + _ipx_remove_default_route (&vtable_ip6, self, source); +} + +/***********************************************************************************/ + +static gint64 +_ipx_get_effective_metric (const VTableIP *vtable, NMDefaultRouteManager *self, NMDevice *device) +{ + NMDefaultRouteManagerPrivate *priv; + Entry *entry; + + g_return_val_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self), -1); + g_return_val_if_fail (NM_IS_DEVICE (device), -1); + + priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + + entry = _entry_find_by_source (vtable->get_entries (priv), device, NULL); + if (entry) + return entry->effective_metric; + return -1; +} + +gint64 +nm_default_route_manager_ip4_get_effective_metric (NMDefaultRouteManager *self, NMDevice *device) +{ + return _ipx_get_effective_metric (&vtable_ip4, self, device); +} + + +gint64 +nm_default_route_manager_ip6_get_effective_metric (NMDefaultRouteManager *self, NMDevice *device) +{ + return _ipx_get_effective_metric (&vtable_ip4, self, device); +} + +/***********************************************************************************/ + +static gboolean +_ipx_connection_has_default_route (const VTableIP *vtable, NMDefaultRouteManager *self, NMConnection *connection) +{ + const char *method; + NMSettingIPConfig *s_ip; + + g_return_val_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self), FALSE); + g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE); + + if (VTABLE_IS_IP4) + s_ip = nm_connection_get_setting_ip4_config (connection); + else + s_ip = nm_connection_get_setting_ip6_config (connection); + if (!s_ip || nm_setting_ip_config_get_never_default (s_ip)) + return FALSE; + + if (VTABLE_IS_IP4) { + method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); + if ( !method + || !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) + || !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL)) + return FALSE; + } else { + method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); + if ( !method + || !strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) + || !strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL)) + return FALSE; + } + + return TRUE; +} + +gboolean +nm_default_route_manager_ip4_connection_has_default_route (NMDefaultRouteManager *self, NMConnection *connection) +{ + return _ipx_connection_has_default_route (&vtable_ip4, self, connection); +} + +gboolean +nm_default_route_manager_ip6_connection_has_default_route (NMDefaultRouteManager *self, NMConnection *connection) +{ + return _ipx_connection_has_default_route (&vtable_ip6, self, connection); +} + +/***********************************************************************************/ + +static GPtrArray * +_v4_get_entries (NMDefaultRouteManagerPrivate *priv) +{ + return priv->entries_ip4; +} + +static GPtrArray * +_v6_get_entries (NMDefaultRouteManagerPrivate *priv) +{ + return priv->entries_ip6; +} + +static gboolean +_v4_platform_route_delete_default (int ifindex, guint32 metric) +{ + return nm_platform_ip4_route_delete (ifindex, 0, 0, metric); +} + +static gboolean +_v6_platform_route_delete_default (int ifindex, guint32 metric) +{ + return nm_platform_ip6_route_delete (ifindex, in6addr_any, 0, metric); +} + +static guint32 +_v4_route_metric_normalize (guint32 metric) +{ + return metric; +} + +static const VTableIP vtable_ip4 = { + .addr_family = AF_INET, + .get_entries = _v4_get_entries, + .platform_route_to_string = (const char *(*)(const NMPlatformIPRoute *)) nm_platform_ip4_route_to_string, + .platform_route_delete_default = _v4_platform_route_delete_default, + .route_metric_normalize = _v4_route_metric_normalize, +}; + +static const VTableIP vtable_ip6 = { + .addr_family = AF_INET6, + .get_entries = _v6_get_entries, + .platform_route_to_string = (const char *(*)(const NMPlatformIPRoute *)) nm_platform_ip6_route_to_string, + .platform_route_delete_default = _v6_platform_route_delete_default, + .route_metric_normalize = nm_utils_ip6_route_metric_normalize, +}; + +/***********************************************************************************/ + +NMDefaultRouteManager * +nm_default_route_manager_get () +{ + if (G_UNLIKELY (!_instance)) { + _instance = NM_DEFAULT_ROUTE_MANAGER (g_object_new (NM_TYPE_DEFAULT_ROUTE_MANAGER, NULL)); + g_object_add_weak_pointer (G_OBJECT (_instance), (gpointer *) &_instance); + } + return _instance; +} + +/***********************************************************************************/ + +static void +nm_default_route_manager_init (NMDefaultRouteManager *self) +{ + NMDefaultRouteManagerPrivate *priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + + priv->entries_ip4 = g_ptr_array_new_full (0, (GDestroyNotify) _entry_free); + priv->entries_ip6 = g_ptr_array_new_full (0, (GDestroyNotify) _entry_free); +} + +static void +dispose (GObject *object) +{ + NMDefaultRouteManager *self = NM_DEFAULT_ROUTE_MANAGER (object); + NMDefaultRouteManagerPrivate *priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + + if (priv->entries_ip4) { + g_ptr_array_free (priv->entries_ip4, TRUE); + priv->entries_ip4 = NULL; + } + if (priv->entries_ip6) { + g_ptr_array_free (priv->entries_ip6, TRUE); + priv->entries_ip6 = NULL; + } + + G_OBJECT_CLASS (nm_default_route_manager_parent_class)->dispose (object); +} + +static void +nm_default_route_manager_class_init (NMDefaultRouteManagerClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (NMDefaultRouteManagerPrivate)); + + /* virtual methods */ + object_class->dispose = dispose; +} + diff --git a/src/nm-default-route-manager.h b/src/nm-default-route-manager.h new file mode 100644 index 0000000000..d60a2f86d4 --- /dev/null +++ b/src/nm-default-route-manager.h @@ -0,0 +1,64 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2014 Red Hat, Inc. + */ + +#include + +#include "nm-connection.h" +#include "nm-types.h" + +#ifndef __NETWORKMANAGER_DEFAULT_ROUTE_MANAGER_H__ +#define __NETWORKMANAGER_DEFAULT_ROUTE_MANAGER_H__ + + +#define NM_TYPE_DEFAULT_ROUTE_MANAGER (nm_default_route_manager_get_type ()) +#define NM_DEFAULT_ROUTE_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DEFAULT_ROUTE_MANAGER, NMDefaultRouteManager)) +#define NM_DEFAULT_ROUTE_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DEFAULT_ROUTE_MANAGER, NMDefaultRouteManagerClass)) +#define NM_IS_DEFAULT_ROUTE_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DEFAULT_ROUTE_MANAGER)) +#define NM_IS_DEFAULT_ROUTE_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DEFAULT_ROUTE_MANAGER)) +#define NM_DEFAULT_ROUTE_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DEFAULT_ROUTE_MANAGER, NMDefaultRouteManagerClass)) + + + +struct _NMDefaultRouteManager { + GObject parent; +}; + +typedef struct { + GObjectClass parent; +} NMDefaultRouteManagerClass; + +GType nm_default_route_manager_get_type (void); + +NMDefaultRouteManager *nm_default_route_manager_get (void); + +void nm_default_route_manager_ip4_update_default_route (NMDefaultRouteManager *manager, gpointer source); +void nm_default_route_manager_ip6_update_default_route (NMDefaultRouteManager *manager, gpointer source); + +void nm_default_route_manager_ip4_remove_default_route (NMDefaultRouteManager *manager, gpointer source); +void nm_default_route_manager_ip6_remove_default_route (NMDefaultRouteManager *manager, gpointer source); + +gint64 nm_default_route_manager_ip4_get_effective_metric (NMDefaultRouteManager *manager, NMDevice *device); +gint64 nm_default_route_manager_ip6_get_effective_metric (NMDefaultRouteManager *manager, NMDevice *device); + +gboolean nm_default_route_manager_ip4_connection_has_default_route (NMDefaultRouteManager *manager, NMConnection *connection); +gboolean nm_default_route_manager_ip6_connection_has_default_route (NMDefaultRouteManager *manager, NMConnection *connection); + +#endif /* NM_DEFAULT_ROUTE_MANAGER_H */ + diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 3566b68569..5fddc73798 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -1844,8 +1844,14 @@ get_property (GObject *object, guint prop_id, for (i = 0; i < nroutes; i++) { const NMPlatformIP4Route *route = nm_ip4_config_get_route (config, i); - GArray *array = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 4); + GArray *array; + /* legacy versions of nm_ip4_route_set_prefix() in libnm-util assert that the + * plen is positive. Skip the default routes not to break older clients. */ + if (NM_PLATFORM_IP_ROUTE_IS_DEFAULT (route)) + continue; + + array = g_array_sized_new (FALSE, TRUE, sizeof (guint32), 4); g_array_append_val (array, route->network); g_array_append_val (array, route->plen); g_array_append_val (array, route->gateway); diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index 3b0f291a91..9423d8a0f9 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -1743,12 +1743,18 @@ get_property (GObject *object, guint prop_id, int i; for (i = 0; i < nroutes; i++) { + GValueArray *array; const NMPlatformIP6Route *route = nm_ip6_config_get_route (config, i); - - GValueArray *array = g_value_array_new (4); GByteArray *ba; GValue element = G_VALUE_INIT; + /* legacy versions of nm_ip6_route_set_prefix() in libnm-util assert that the + * plen is positive. Skip the default routes not to break older clients. */ + if (NM_PLATFORM_IP_ROUTE_IS_DEFAULT (route)) + continue; + + array = g_value_array_new (4); + g_value_init (&element, DBUS_TYPE_G_UCHAR_ARRAY); ba = g_byte_array_new (); g_byte_array_append (ba, (guint8 *) &route->network, sizeof (route->network)); diff --git a/src/nm-policy.c b/src/nm-policy.c index 73f3bf4846..938343a274 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -32,6 +32,7 @@ #include "nm-activation-request.h" #include "nm-logging.h" #include "nm-device.h" +#include "nm-default-route-manager.h" #include "nm-dbus-manager.h" #include "nm-setting-ip4-config.h" #include "nm-setting-connection.h" @@ -102,17 +103,12 @@ get_best_ip4_device (NMPolicy *self, gboolean fully_activated) NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self); const GSList *iter; NMDevice *best = NULL; - int best_prio = G_MAXINT; + guint32 best_prio = G_MAXUINT32; for (iter = nm_manager_get_devices (priv->manager); iter; iter = g_slist_next (iter)) { NMDevice *dev = NM_DEVICE (iter->data); - NMDeviceType devtype = nm_device_get_device_type (dev); NMDeviceState state = nm_device_get_state (dev); - NMActRequest *req; - NMConnection *connection; - NMSettingIPConfig *s_ip4; - int prio; - const char *method = NULL; + guint32 prio; if ( state <= NM_DEVICE_STATE_DISCONNECTED || state >= NM_DEVICE_STATE_DEACTIVATING) @@ -121,40 +117,19 @@ get_best_ip4_device (NMPolicy *self, gboolean fully_activated) if (fully_activated && state < NM_DEVICE_STATE_SECONDARIES) continue; - req = nm_device_get_act_request (dev); - g_assert (req); - connection = nm_act_request_get_connection (req); - g_assert (connection); - - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); - /* If IPv4 is disabled or link-local-only, it can't be the default */ - if ( !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED) - || !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL)) - continue; - - /* 'never-default' devices can't ever be the default */ - s_ip4 = nm_connection_get_setting_ip4_config (connection); - g_assert (s_ip4); - if (nm_setting_ip_config_get_never_default (s_ip4)) + if (!nm_default_route_manager_ip4_connection_has_default_route (nm_default_route_manager_get (), + nm_device_get_connection (dev))) continue; if (fully_activated) { - NMIP4Config *ip4_config; + gint64 effective_metric = nm_default_route_manager_ip4_get_effective_metric (nm_default_route_manager_get (), dev); - ip4_config = nm_device_get_ip4_config (dev); - if (!ip4_config) + if (effective_metric == -1) continue; + prio = (guint32) effective_metric; + } else + prio = nm_device_get_ip6_route_metric (dev); - /* Make sure the device has a gateway */ - if (!nm_ip4_config_get_gateway (ip4_config) && (devtype != NM_DEVICE_TYPE_MODEM)) - continue; - - /* 'never-default' devices can't ever be the default */ - if (nm_ip4_config_get_never_default (ip4_config)) - continue; - } - - prio = nm_device_get_priority (dev); if ( prio < best_prio || (priv->default_device4 == dev && prio == best_prio) || !best) { @@ -186,17 +161,12 @@ get_best_ip6_device (NMPolicy *self, gboolean fully_activated) NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self); const GSList *iter; NMDevice *best = NULL; - int best_prio = G_MAXINT; + guint32 best_prio = G_MAXUINT32; for (iter = nm_manager_get_devices (priv->manager); iter; iter = g_slist_next (iter)) { NMDevice *dev = NM_DEVICE (iter->data); - NMDeviceType devtype = nm_device_get_device_type (dev); NMDeviceState state = nm_device_get_state (dev); - NMActRequest *req; - NMConnection *connection; - NMSettingIPConfig *s_ip6; - int prio; - const char *method = NULL; + guint32 prio; if ( state <= NM_DEVICE_STATE_DISCONNECTED || state >= NM_DEVICE_STATE_DEACTIVATING) @@ -205,36 +175,20 @@ get_best_ip6_device (NMPolicy *self, gboolean fully_activated) if (fully_activated && state < NM_DEVICE_STATE_SECONDARIES) continue; - req = nm_device_get_act_request (dev); - g_assert (req); - connection = nm_act_request_get_connection (req); - g_assert (connection); - - method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); - if ( !strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_IGNORE) - || !strcmp (method, NM_SETTING_IP6_CONFIG_METHOD_LINK_LOCAL)) - continue; - - s_ip6 = nm_connection_get_setting_ip6_config (connection); - g_assert (s_ip6); - if (nm_setting_ip_config_get_never_default (s_ip6)) + if (!nm_default_route_manager_ip6_connection_has_default_route (nm_default_route_manager_get (), + nm_device_get_connection (dev))) continue; if (fully_activated) { - NMIP6Config *ip6_config; - - ip6_config = nm_device_get_ip6_config (dev); - if (!ip6_config) - continue; + gint64 effective_metric = nm_default_route_manager_ip6_get_effective_metric (nm_default_route_manager_get (), dev); - if (!nm_ip6_config_get_gateway (ip6_config) && (devtype != NM_DEVICE_TYPE_MODEM)) + if (effective_metric == -1) continue; + prio = (guint32) effective_metric; + } else + prio = nm_device_get_ip6_route_metric (dev); + prio = nm_utils_ip6_route_metric_normalize (prio); - if (nm_ip6_config_get_never_default (ip6_config)) - continue; - } - - prio = nm_device_get_priority (dev); if ( prio < best_prio || (priv->default_device6 == dev && prio == best_prio) || !best) { @@ -668,6 +622,7 @@ update_ip4_routing (NMPolicy *policy, gboolean force_update) if (vpn) { in_addr_t int_gw = nm_vpn_connection_get_ip4_internal_gateway (vpn); int mss = nm_ip4_config_get_mss (ip4_config); + guint32 route_metric = nm_vpn_connection_get_ip4_route_metric (vpn); /* If no VPN interface, use the parent interface */ if (ip_ifindex <= 0) @@ -675,39 +630,22 @@ update_ip4_routing (NMPolicy *policy, gboolean force_update) if (!nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, 0, 0, int_gw, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) { + route_metric, mss)) { if (int_gw) { (void) nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, int_gw, 32, 0, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss); + route_metric, mss); if (!nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, 0, 0, int_gw, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) + route_metric, mss)) nm_log_err (LOGD_IP4 | LOGD_VPN, "Failed to set IPv4 default route via VPN."); } else nm_log_err (LOGD_IP4 | LOGD_VPN, "Failed to set IPv4 default route via VPN."); } default_device = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn)); - } else { - int mss = nm_ip4_config_get_mss (ip4_config); - - g_assert (ip_iface); - if (!nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_USER, - 0, 0, gw_addr, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) { - (void) nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_USER, - gw_addr, 32, 0, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss); - if (!nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_USER, - 0, 0, gw_addr, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) { - nm_log_err (LOGD_IP4, "Failed to set default route."); - } - } - + } else default_device = best; - } update_default_ac (policy, best_ac, nm_active_connection_set_default); @@ -875,6 +813,7 @@ update_ip6_routing (NMPolicy *policy, gboolean force_update) if (vpn) { const struct in6_addr *int_gw = nm_vpn_connection_get_ip6_internal_gateway (vpn); int mss = nm_ip6_config_get_mss (ip6_config); + guint32 route_metric = nm_vpn_connection_get_ip6_route_metric (vpn); if (!int_gw) int_gw = &in6addr_any; @@ -885,37 +824,22 @@ update_ip6_routing (NMPolicy *policy, gboolean force_update) if (!nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, in6addr_any, 0, *int_gw, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) { + route_metric, mss)) { if (!IN6_IS_ADDR_UNSPECIFIED (int_gw)) { (void) nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, *int_gw, 128, in6addr_any, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss); + route_metric, mss); if (!nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, in6addr_any, 0, *int_gw, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) + route_metric, mss)) nm_log_err (LOGD_IP6 | LOGD_VPN, "Failed to set IPv6 default route via VPN."); } else nm_log_err (LOGD_IP6 | LOGD_VPN, "Failed to set IPv6 default route via VPN."); } default_device6 = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn)); - } else { - int mss = nm_ip6_config_get_mss (ip6_config); - - if (!nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_USER, - in6addr_any, 0, *gw_addr, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) { - (void) nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_USER, - *gw_addr, 128, in6addr_any, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss); - if (!nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_USER, - in6addr_any, 0, *gw_addr, - NM_PLATFORM_ROUTE_METRIC_DEFAULT, mss)) - nm_log_err (LOGD_IP6, "Failed to set default route."); - } - + } else default_device6 = best; - } update_default_ac (policy, best_ac, nm_active_connection_set_default6); diff --git a/src/nm-types.h b/src/nm-types.h index c1c02ac60e..4a93567e11 100644 --- a/src/nm-types.h +++ b/src/nm-types.h @@ -29,6 +29,7 @@ typedef struct _NMAuthSubject NMAuthSubject; typedef struct _NMConnectionProvider NMConnectionProvider; typedef struct _NMConnectivity NMConnectivity; typedef struct _NMDBusManager NMDBusManager; +typedef struct _NMDefaultRouteManager NMDefaultRouteManager; typedef struct _NMDevice NMDevice; typedef struct _NMDhcp4Config NMDhcp4Config; typedef struct _NMDhcp6Config NMDhcp6Config; -- cgit v1.2.1 From 0fc47f3b5744ba678f3baf75ddd73ff5b3222461 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 31 Oct 2014 23:26:48 +0100 Subject: policy: move get_best_device() function to nm-default-route-manager No functional change, only refactoring by moving and combining the code. Signed-off-by: Thomas Haller --- src/nm-default-route-manager.c | 100 ++++++++++++++++++++++++++++++++----- src/nm-default-route-manager.h | 6 +-- src/nm-policy.c | 111 +++-------------------------------------- 3 files changed, 98 insertions(+), 119 deletions(-) diff --git a/src/nm-default-route-manager.c b/src/nm-default-route-manager.c index 2ff75ae34e..717c3a0e38 100644 --- a/src/nm-default-route-manager.c +++ b/src/nm-default-route-manager.c @@ -30,6 +30,7 @@ #include "nm-platform.h" #include "nm-ip4-config.h" #include "nm-ip6-config.h" +#include "nm-activation-request.h" typedef struct { GPtrArray *entries_ip4; @@ -96,6 +97,7 @@ typedef struct { const char *(*platform_route_to_string) (const NMPlatformIPRoute *route); gboolean (*platform_route_delete_default) (int ifindex, guint32 metric); guint32 (*route_metric_normalize) (guint32 metric); + guint32 (*device_get_route_metric) (NMDevice *self); } VTableIP; static const VTableIP vtable_ip4, vtable_ip6; @@ -553,19 +555,6 @@ _ipx_get_effective_metric (const VTableIP *vtable, NMDefaultRouteManager *self, return -1; } -gint64 -nm_default_route_manager_ip4_get_effective_metric (NMDefaultRouteManager *self, NMDevice *device) -{ - return _ipx_get_effective_metric (&vtable_ip4, self, device); -} - - -gint64 -nm_default_route_manager_ip6_get_effective_metric (NMDefaultRouteManager *self, NMDevice *device) -{ - return _ipx_get_effective_metric (&vtable_ip4, self, device); -} - /***********************************************************************************/ static gboolean @@ -615,6 +604,89 @@ nm_default_route_manager_ip6_connection_has_default_route (NMDefaultRouteManager /***********************************************************************************/ +/** _ipx_get_best_device: + * @vtable: the virtual table + * @self: #NMDefaultRouteManager + * @devices: list of devices to be searched. Only devices from this list will be considered + * @fully_activated: if #TRUE, only search for devices that are fully activated. Otherwise, + * search if there is a best device going to be activated. In the latter case, this will + * return NULL if the best device is already activated. + * @preferred_device: if not-NULL, this device is preferred if there are more devices with + * the same priority. + **/ +static NMDevice * +_ipx_get_best_device (const VTableIP *vtable, NMDefaultRouteManager *self, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device) +{ + const GSList *iter; + NMDevice *best_device = NULL; + guint32 best_prio = G_MAXUINT32; + + g_return_val_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self), NULL); + + for (iter = devices; iter; iter = g_slist_next (iter)) { + NMDevice *device = NM_DEVICE (iter->data); + NMDeviceState state = nm_device_get_state (device); + guint32 prio; + + if ( state <= NM_DEVICE_STATE_DISCONNECTED + || state >= NM_DEVICE_STATE_DEACTIVATING) + continue; + + if (fully_activated && state < NM_DEVICE_STATE_SECONDARIES) + continue; + + if (!_ipx_connection_has_default_route (vtable, self, nm_device_get_connection (device))) + continue; + + if (fully_activated) { + gint64 effective_metric = _ipx_get_effective_metric (vtable, self, device); + + if (effective_metric == -1) + continue; + prio = (guint32) effective_metric; + } else + prio = vtable->device_get_route_metric (device); + prio = vtable->route_metric_normalize (prio); + + if ( prio < best_prio + || (preferred_device == device && prio == best_prio) + || !best_device) { + best_device = device; + best_prio = prio; + } + } + + if (!best_device) + return NULL; + + if (!fully_activated) { + NMDeviceState state = nm_device_get_state (best_device); + + /* There's only a best activating device if the best device + * among all activating and already-activated devices is a + * still-activating one. + */ + if (state >= NM_DEVICE_STATE_SECONDARIES) + return NULL; + } + + return best_device; +} + +NMDevice * +nm_default_route_manager_ip4_get_best_device (NMDefaultRouteManager *self, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device) +{ + return _ipx_get_best_device (&vtable_ip4, self, devices, fully_activated, preferred_device); +} + +NMDevice * +nm_default_route_manager_ip6_get_best_device (NMDefaultRouteManager *self, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device) +{ + return _ipx_get_best_device (&vtable_ip6, self, devices, fully_activated, preferred_device); +} + +/***********************************************************************************/ + static GPtrArray * _v4_get_entries (NMDefaultRouteManagerPrivate *priv) { @@ -651,6 +723,7 @@ static const VTableIP vtable_ip4 = { .platform_route_to_string = (const char *(*)(const NMPlatformIPRoute *)) nm_platform_ip4_route_to_string, .platform_route_delete_default = _v4_platform_route_delete_default, .route_metric_normalize = _v4_route_metric_normalize, + .device_get_route_metric = nm_device_get_ip4_route_metric, }; static const VTableIP vtable_ip6 = { @@ -659,6 +732,7 @@ static const VTableIP vtable_ip6 = { .platform_route_to_string = (const char *(*)(const NMPlatformIPRoute *)) nm_platform_ip6_route_to_string, .platform_route_delete_default = _v6_platform_route_delete_default, .route_metric_normalize = nm_utils_ip6_route_metric_normalize, + .device_get_route_metric = nm_device_get_ip6_route_metric, }; /***********************************************************************************/ diff --git a/src/nm-default-route-manager.h b/src/nm-default-route-manager.h index d60a2f86d4..bf24366329 100644 --- a/src/nm-default-route-manager.h +++ b/src/nm-default-route-manager.h @@ -54,11 +54,11 @@ void nm_default_route_manager_ip6_update_default_route (NMDefaultRouteManager *m void nm_default_route_manager_ip4_remove_default_route (NMDefaultRouteManager *manager, gpointer source); void nm_default_route_manager_ip6_remove_default_route (NMDefaultRouteManager *manager, gpointer source); -gint64 nm_default_route_manager_ip4_get_effective_metric (NMDefaultRouteManager *manager, NMDevice *device); -gint64 nm_default_route_manager_ip6_get_effective_metric (NMDefaultRouteManager *manager, NMDevice *device); - gboolean nm_default_route_manager_ip4_connection_has_default_route (NMDefaultRouteManager *manager, NMConnection *connection); gboolean nm_default_route_manager_ip6_connection_has_default_route (NMDefaultRouteManager *manager, NMConnection *connection); +NMDevice *nm_default_route_manager_ip4_get_best_device (NMDefaultRouteManager *manager, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device); +NMDevice *nm_default_route_manager_ip6_get_best_device (NMDefaultRouteManager *manager, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device); + #endif /* NM_DEFAULT_ROUTE_MANAGER_H */ diff --git a/src/nm-policy.c b/src/nm-policy.c index 938343a274..37e34440f2 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -101,117 +101,22 @@ static NMDevice * get_best_ip4_device (NMPolicy *self, gboolean fully_activated) { NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self); - const GSList *iter; - NMDevice *best = NULL; - guint32 best_prio = G_MAXUINT32; - - for (iter = nm_manager_get_devices (priv->manager); iter; iter = g_slist_next (iter)) { - NMDevice *dev = NM_DEVICE (iter->data); - NMDeviceState state = nm_device_get_state (dev); - guint32 prio; - - if ( state <= NM_DEVICE_STATE_DISCONNECTED - || state >= NM_DEVICE_STATE_DEACTIVATING) - continue; - - if (fully_activated && state < NM_DEVICE_STATE_SECONDARIES) - continue; - - if (!nm_default_route_manager_ip4_connection_has_default_route (nm_default_route_manager_get (), - nm_device_get_connection (dev))) - continue; - - if (fully_activated) { - gint64 effective_metric = nm_default_route_manager_ip4_get_effective_metric (nm_default_route_manager_get (), dev); - - if (effective_metric == -1) - continue; - prio = (guint32) effective_metric; - } else - prio = nm_device_get_ip6_route_metric (dev); - - if ( prio < best_prio - || (priv->default_device4 == dev && prio == best_prio) - || !best) { - best = dev; - best_prio = prio; - } - } - - if (!best) - return NULL; - - if (!fully_activated) { - NMDeviceState state = nm_device_get_state (best); - - /* There's only a best activating device if the best device - * among all activating and already-activated devices is a - * still-activating one. - */ - if (state >= NM_DEVICE_STATE_SECONDARIES) - return NULL; - } - return best; + return nm_default_route_manager_ip4_get_best_device (nm_default_route_manager_get (), + nm_manager_get_devices (priv->manager), + fully_activated, + priv->default_device4); } static NMDevice * get_best_ip6_device (NMPolicy *self, gboolean fully_activated) { NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self); - const GSList *iter; - NMDevice *best = NULL; - guint32 best_prio = G_MAXUINT32; - - for (iter = nm_manager_get_devices (priv->manager); iter; iter = g_slist_next (iter)) { - NMDevice *dev = NM_DEVICE (iter->data); - NMDeviceState state = nm_device_get_state (dev); - guint32 prio; - - if ( state <= NM_DEVICE_STATE_DISCONNECTED - || state >= NM_DEVICE_STATE_DEACTIVATING) - continue; - - if (fully_activated && state < NM_DEVICE_STATE_SECONDARIES) - continue; - - if (!nm_default_route_manager_ip6_connection_has_default_route (nm_default_route_manager_get (), - nm_device_get_connection (dev))) - continue; - - if (fully_activated) { - gint64 effective_metric = nm_default_route_manager_ip6_get_effective_metric (nm_default_route_manager_get (), dev); - - if (effective_metric == -1) - continue; - prio = (guint32) effective_metric; - } else - prio = nm_device_get_ip6_route_metric (dev); - prio = nm_utils_ip6_route_metric_normalize (prio); - - if ( prio < best_prio - || (priv->default_device6 == dev && prio == best_prio) - || !best) { - best = dev; - best_prio = prio; - } - } - - if (!best) - return NULL; - - if (!fully_activated) { - NMDeviceState state = nm_device_get_state (best); - - /* There's only a best activating device if the best device - * among all activating and already-activated devices is an - * activating one. - */ - if (state >= NM_DEVICE_STATE_SECONDARIES) - return NULL; - } - return best; + return nm_default_route_manager_ip6_get_best_device (nm_default_route_manager_get (), + nm_manager_get_devices (priv->manager), + fully_activated, + priv->default_device6); } #define FALLBACK_HOSTNAME4 "localhost.localdomain" -- cgit v1.2.1 From a94605f92b7dc25dcb8f574da0e48e44e9fdcf29 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 3 Nov 2014 12:33:15 +0100 Subject: policy: better sync get_best_device() with NMDefaultRouteManager NMDefaultRouteManager has a sorted list of routes. Change get_best_device() to better consider that list when choosing a best device. Always prefer the priority as reported from entry->effective_metric if the device is registered to have a default route. Before for !fully_activated, we choose the metric based on nm_device_get_ipx_route_metric(). Add more checks in case of equal priority. For @fully_activated, always prefer the device that is sorted by NMDefaultRouteManager. For non @fully_activated, prefer the device with an entry. Signed-off-by: Thomas Haller --- src/nm-default-route-manager.c | 87 ++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/src/nm-default-route-manager.c b/src/nm-default-route-manager.c index 717c3a0e38..ac00ab21e1 100644 --- a/src/nm-default-route-manager.c +++ b/src/nm-default-route-manager.c @@ -97,7 +97,6 @@ typedef struct { const char *(*platform_route_to_string) (const NMPlatformIPRoute *route); gboolean (*platform_route_delete_default) (int ifindex, guint32 metric); guint32 (*route_metric_normalize) (guint32 metric); - guint32 (*device_get_route_metric) (NMDevice *self); } VTableIP; static const VTableIP vtable_ip4, vtable_ip6; @@ -538,25 +537,6 @@ nm_default_route_manager_ip6_remove_default_route (NMDefaultRouteManager *self, /***********************************************************************************/ -static gint64 -_ipx_get_effective_metric (const VTableIP *vtable, NMDefaultRouteManager *self, NMDevice *device) -{ - NMDefaultRouteManagerPrivate *priv; - Entry *entry; - - g_return_val_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self), -1); - g_return_val_if_fail (NM_IS_DEVICE (device), -1); - - priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); - - entry = _entry_find_by_source (vtable->get_entries (priv), device, NULL); - if (entry) - return entry->effective_metric; - return -1; -} - -/***********************************************************************************/ - static gboolean _ipx_connection_has_default_route (const VTableIP *vtable, NMDefaultRouteManager *self, NMConnection *connection) { @@ -617,16 +597,22 @@ nm_default_route_manager_ip6_connection_has_default_route (NMDefaultRouteManager static NMDevice * _ipx_get_best_device (const VTableIP *vtable, NMDefaultRouteManager *self, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device) { + NMDefaultRouteManagerPrivate *priv; const GSList *iter; NMDevice *best_device = NULL; guint32 best_prio = G_MAXUINT32; + guint best_idx = G_MAXUINT; g_return_val_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self), NULL); + priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + for (iter = devices; iter; iter = g_slist_next (iter)) { NMDevice *device = NM_DEVICE (iter->data); NMDeviceState state = nm_device_get_state (device); guint32 prio; + guint idx; + Entry *entry; if ( state <= NM_DEVICE_STATE_DISCONNECTED || state >= NM_DEVICE_STATE_DEACTIVATING) @@ -638,35 +624,56 @@ _ipx_get_best_device (const VTableIP *vtable, NMDefaultRouteManager *self, const if (!_ipx_connection_has_default_route (vtable, self, nm_device_get_connection (device))) continue; - if (fully_activated) { - gint64 effective_metric = _ipx_get_effective_metric (vtable, self, device); + entry = _entry_find_by_source (vtable->get_entries (priv), device, &idx); + if (fully_activated && !entry) + continue; - if (effective_metric == -1) - continue; - prio = (guint32) effective_metric; - } else - prio = vtable->device_get_route_metric (device); + if (entry) + prio = entry->effective_metric; + else + prio = nm_device_get_ip4_route_metric (device); prio = vtable->route_metric_normalize (prio); - if ( prio < best_prio - || (preferred_device == device && prio == best_prio) - || !best_device) { - best_device = device; - best_prio = prio; + if (!best_device || prio < best_prio) + goto NEW_BEST_DEVICE; + + if (prio == best_prio) { + /* the devices have the same priority... which one to prefer? */ + + if (fully_activated) { + /* for @fully_activated, respect the order as from the sorted entries. */ + if (idx < best_idx) + goto NEW_BEST_DEVICE; + } else { + /* for !fully_activated, prefer @preferred_device. */ + if (preferred_device == device) + goto NEW_BEST_DEVICE; + if (preferred_device != best_device) { + /* if both devices are not @preferred_device, prefer + * the one with an entry (or the one with lower index, + * if both have an entry). + * + * The following is correct because if @best_device has no entry, @best_idx + * is G_MAXUINT. Also, if the currenty device has no entry, @idx is G_MAXUINT. */ + if (idx < best_idx) + goto NEW_BEST_DEVICE; + } + } } - } - if (!best_device) - return NULL; - - if (!fully_activated) { - NMDeviceState state = nm_device_get_state (best_device); + continue; +NEW_BEST_DEVICE: + best_device = device; + best_prio = prio; + best_idx = idx; + } + if (best_device && !fully_activated) { /* There's only a best activating device if the best device * among all activating and already-activated devices is a * still-activating one. */ - if (state >= NM_DEVICE_STATE_SECONDARIES) + if (nm_device_get_state (best_device) >= NM_DEVICE_STATE_SECONDARIES) return NULL; } @@ -723,7 +730,6 @@ static const VTableIP vtable_ip4 = { .platform_route_to_string = (const char *(*)(const NMPlatformIPRoute *)) nm_platform_ip4_route_to_string, .platform_route_delete_default = _v4_platform_route_delete_default, .route_metric_normalize = _v4_route_metric_normalize, - .device_get_route_metric = nm_device_get_ip4_route_metric, }; static const VTableIP vtable_ip6 = { @@ -732,7 +738,6 @@ static const VTableIP vtable_ip6 = { .platform_route_to_string = (const char *(*)(const NMPlatformIPRoute *)) nm_platform_ip6_route_to_string, .platform_route_delete_default = _v6_platform_route_delete_default, .route_metric_normalize = nm_utils_ip6_route_metric_normalize, - .device_get_route_metric = nm_device_get_ip6_route_metric, }; /***********************************************************************************/ -- cgit v1.2.1 From a39a3ae4cd72d695f1b5d10eaa79544f2020a54e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 4 Nov 2014 10:26:01 +0100 Subject: policy: track default route for VPN in NMDefaultRouteManager Extend NMDefaultRouteManager to track NMVpnConnection beside NMDevice. That way, all default routes are managed by NMDefaultRouteManager. For VPN connections the manager also tracks connections that are set never_default. That is useful because NMPolicy still uses VPNs without default route to setup DNS. Hence, NMDefaultRouteManager trackes those connections to have the relative priority of the devices. Interestingly, that means that for VPNs that are ipv4.never-default, ipv4.route-metric still has an effect in determining relative priorities for DNS configuration. This commit only adds the parts to track the default route. NMPolicy still sets the route as before. Signed-off-by: Thomas Haller --- src/nm-default-route-manager.c | 114 ++++++++++++++++++++++++++++++++---- src/vpn-manager/nm-vpn-connection.c | 10 ++++ 2 files changed, 111 insertions(+), 13 deletions(-) diff --git a/src/nm-default-route-manager.c b/src/nm-default-route-manager.c index ac00ab21e1..8c0529e356 100644 --- a/src/nm-default-route-manager.c +++ b/src/nm-default-route-manager.c @@ -27,6 +27,7 @@ #include "nm-logging.h" #include "nm-device.h" +#include "nm-vpn-connection.h" #include "nm-platform.h" #include "nm-ip4-config.h" #include "nm-ip6-config.h" @@ -72,7 +73,7 @@ static NMDefaultRouteManager *_instance; entry_idx, \ NM_IS_DEVICE (entry->source.pointer) ? "dev" : "vpn", \ entry->source.pointer, \ - nm_device_get_iface (entry->source.device) + NM_IS_DEVICE (entry->source.pointer) ? nm_device_get_iface (entry->source.device) : nm_vpn_connection_get_connection_id (entry->source.vpn) /***********************************************************************************/ @@ -81,6 +82,7 @@ typedef struct { void *pointer; GObject *object; NMDevice *device; + NMVpnConnection *vpn; } source; union { NMPlatformIPRoute route; @@ -88,6 +90,12 @@ typedef struct { NMPlatformIP6Route route6; }; gboolean synced; /* if true, we synced the entry to platform. We don't sync assumed devices */ + + /* it makes sense to order sources based on their priority, without + * actually adding a default route. This is useful to decide which + * DNS server to prefer. never_default entries are not synced to platform. */ + gboolean never_default; + guint32 effective_metric; } Entry; @@ -148,6 +156,9 @@ _platform_route_sync_add (const VTableIP *vtable, NMDefaultRouteManager *self, g for (i = 0; i < entries->len; i++) { Entry *e = g_ptr_array_index (entries, i); + if (e->never_default) + continue; + if (e->effective_metric != metric) continue; @@ -215,6 +226,9 @@ _platform_route_sync_flush (const VTableIP *vtable, NMDefaultRouteManager *self) for (i = 0; i < entries->len; i++) { Entry *e = g_ptr_array_index (entries, i); + if (e->never_default) + continue; + if ( e->route.ifindex == route->ifindex && e->synced) { has_ifindex_synced = TRUE; @@ -253,6 +267,10 @@ _sort_entries_cmp (gconstpointer a, gconstpointer b, gpointer user_data) if (m_a != m_b) return (m_a < m_b) ? -1 : 1; + /* If the metrics are equal, we prefer the one that is !never_default */ + if (!!e_a->never_default != !!e_b->never_default) + return e_a->never_default ? 1 : -1; + /* If the metrics are equal, we prefer the one that is assumed (!synced). * Entries that we sync, can be modified so that only the best * entry has a (deterministically) lowest metric. @@ -300,6 +318,9 @@ _resync_all (const VTableIP *vtable, NMDefaultRouteManager *self, const Entry *c g_assert (entry != old_entry); + if (entry->never_default) + continue; + if (!entry->synced) { last_metric = MAX (last_metric, (gint64) entry->effective_metric); continue; @@ -342,7 +363,7 @@ _entry_at_idx_update (const VTableIP *vtable, NMDefaultRouteManager *self, guint { NMDefaultRouteManagerPrivate *priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); Entry *entry; - NMDevice *device; + NMDevice *device = NULL; GPtrArray *entries; entries = vtable->get_entries (priv); @@ -350,8 +371,8 @@ _entry_at_idx_update (const VTableIP *vtable, NMDefaultRouteManager *self, guint entry = g_ptr_array_index (entries, entry_idx); - g_return_if_fail (NM_IS_DEVICE (entry->source.pointer)); - device = entry->source.device; + if (NM_IS_DEVICE (entry->source.pointer)) + device = entry->source.device; g_assert ( !old_entry || (entry->source.pointer == old_entry->source.pointer && entry->route.ifindex == old_entry->route.ifindex)); @@ -362,7 +383,7 @@ _entry_at_idx_update (const VTableIP *vtable, NMDefaultRouteManager *self, guint LOG_ENTRY_ARGS (entry_idx, entry), old_entry ? "update" : "add", vtable->platform_route_to_string (&entry->route), - entry->synced ? "" : " (not synced)"); + entry->never_default ? " (never-default)" : (entry->synced ? "" : " (not synced)")); } g_ptr_array_sort_with_data (entries, _sort_entries_cmp, NULL); @@ -403,18 +424,38 @@ _ipx_update_default_route (const VTableIP *vtable, NMDefaultRouteManager *self, Entry *entry; guint entry_idx; const NMPlatformIPRoute *default_route = NULL; + union { + NMPlatformIPRoute vx; + NMPlatformIP4Route v4; + NMPlatformIP6Route v6; + } rt; int ip_ifindex; GPtrArray *entries; NMDevice *device = NULL; + NMVpnConnection *vpn = NULL; + gboolean never_default = FALSE; gboolean synced; g_return_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self)); if (NM_IS_DEVICE (source)) device = source; + else if (NM_IS_VPN_CONNECTION (source)) + vpn = source; else g_return_if_reached (); - ip_ifindex = nm_device_get_ip_ifindex (device); + if (device) + ip_ifindex = nm_device_get_ip_ifindex (device); + else { + ip_ifindex = nm_vpn_connection_get_ip_ifindex (vpn); + + if (ip_ifindex <= 0) { + NMDevice *parent = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn)); + + if (parent) + ip_ifindex = nm_device_get_ip_ifindex (parent); + } + } priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); @@ -438,15 +479,60 @@ _ipx_update_default_route (const VTableIP *vtable, NMDefaultRouteManager *self, /* get the @default_route from the device. */ if (ip_ifindex > 0) { - if (VTABLE_IS_IP4) - default_route = (const NMPlatformIPRoute *) nm_device_get_ip4_default_route (device); - else - default_route = (const NMPlatformIPRoute *) nm_device_get_ip6_default_route (device); + if (device) { + if (VTABLE_IS_IP4) + default_route = (const NMPlatformIPRoute *) nm_device_get_ip4_default_route (device); + else + default_route = (const NMPlatformIPRoute *) nm_device_get_ip6_default_route (device); + } else { + NMConnection *connection = nm_active_connection_get_connection ((NMActiveConnection *) vpn); + + if ( connection + && nm_vpn_connection_get_vpn_state (vpn) == NM_VPN_CONNECTION_STATE_ACTIVATED) { + + if (VTABLE_IS_IP4) { + NMIP4Config *vpn_config; + + vpn_config = nm_vpn_connection_get_ip4_config (vpn); + if (vpn_config) { + never_default = nm_ip4_config_get_never_default (vpn_config); + memset (&rt.v4, 0, sizeof (rt.v4)); + rt.v4.ifindex = ip_ifindex; + rt.v4.source = NM_IP_CONFIG_SOURCE_VPN; + rt.v4.gateway = nm_vpn_connection_get_ip4_internal_gateway (vpn); + rt.v4.metric = nm_vpn_connection_get_ip4_route_metric (vpn); + rt.v4.mss = nm_ip4_config_get_mss (vpn_config); + default_route = &rt.vx; + } + } else { + NMIP6Config *vpn_config; + + vpn_config = nm_vpn_connection_get_ip6_config (vpn); + if (vpn_config) { + const struct in6_addr *int_gw = nm_vpn_connection_get_ip6_internal_gateway (vpn); + + never_default = nm_ip6_config_get_never_default (vpn_config); + memset (&rt.v6, 0, sizeof (rt.v6)); + rt.v6.ifindex = ip_ifindex; + rt.v6.source = NM_IP_CONFIG_SOURCE_VPN; + rt.v6.gateway = int_gw ? *int_gw : in6addr_any; + rt.v6.metric = nm_vpn_connection_get_ip6_route_metric (vpn); + rt.v6.mss = nm_ip6_config_get_mss (vpn_config); + default_route = &rt.vx; + } + } + } + + /* FIXME: for now, only track the default route for VPN. + * Enable actual configuration of the route later. */ + never_default = TRUE; + } } g_assert (!default_route || default_route->plen == 0); - /* if the device uses an assumed connection, we don't sync the route. */ - synced = !nm_device_uses_assumed_connection (device); + /* if the source is never_default or the device uses an assumed connection, + * we don't sync the route. */ + synced = !never_default && (!device || !nm_device_uses_assumed_connection (device)); if (!entry && !default_route) /* nothing to do */; @@ -463,6 +549,7 @@ _ipx_update_default_route (const VTableIP *vtable, NMDefaultRouteManager *self, /* only use normalized metrics */ entry->route.metric = vtable->route_metric_normalize (entry->route.metric); entry->route.ifindex = ip_ifindex; + entry->never_default = never_default; entry->effective_metric = entry->route.metric; entry->synced = synced; @@ -480,6 +567,7 @@ _ipx_update_default_route (const VTableIP *vtable, NMDefaultRouteManager *self, /* only use normalized metrics */ new_entry.route.metric = vtable->route_metric_normalize (new_entry.route.metric); new_entry.route.ifindex = ip_ifindex; + new_entry.never_default = never_default; new_entry.synced = synced; if (memcmp (entry, &new_entry, sizeof (new_entry)) == 0) @@ -515,7 +603,7 @@ _ipx_remove_default_route (const VTableIP *vtable, NMDefaultRouteManager *self, guint entry_idx; g_return_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self)); - g_return_if_fail (NM_IS_DEVICE (source)); + g_return_if_fail (NM_IS_DEVICE (source) || NM_IS_VPN_CONNECTION (source)); priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); diff --git a/src/vpn-manager/nm-vpn-connection.c b/src/vpn-manager/nm-vpn-connection.c index 444b1948d4..fe5d812791 100644 --- a/src/vpn-manager/nm-vpn-connection.c +++ b/src/vpn-manager/nm-vpn-connection.c @@ -41,6 +41,7 @@ #include "nm-dispatcher.h" #include "nm-agent-manager.h" #include "nm-core-internal.h" +#include "nm-default-route-manager.h" #include "nm-vpn-connection-glue.h" @@ -234,6 +235,9 @@ vpn_cleanup (NMVpnConnection *connection, NMDevice *parent_dev) nm_platform_address_flush (priv->ip_ifindex); } + nm_default_route_manager_ip4_remove_default_route (nm_default_route_manager_get (), connection); + nm_default_route_manager_ip6_remove_default_route (nm_default_route_manager_get (), connection); + nm_device_set_vpn4_config (parent_dev, NULL); nm_device_set_vpn6_config (parent_dev, NULL); @@ -338,6 +342,9 @@ _set_vpn_state (NMVpnConnection *connection, g_object_notify (G_OBJECT (connection), NM_VPN_CONNECTION_VPN_STATE); } + nm_default_route_manager_ip4_update_default_route (nm_default_route_manager_get (), connection); + nm_default_route_manager_ip6_update_default_route (nm_default_route_manager_get (), connection); + switch (vpn_state) { case STATE_NEED_AUTH: /* Do nothing; not part of 'default' because we don't want to touch @@ -942,6 +949,9 @@ nm_vpn_connection_apply_config (NMVpnConnection *connection) apply_parent_device_config (connection); + nm_default_route_manager_ip4_update_default_route (nm_default_route_manager_get (), connection); + nm_default_route_manager_ip6_update_default_route (nm_default_route_manager_get (), connection); + nm_log_info (LOGD_VPN, "VPN connection '%s' (IP Config Get) complete.", nm_connection_get_id (priv->connection)); _set_vpn_state (connection, STATE_PRE_UP, NM_VPN_CONNECTION_STATE_REASON_NONE, FALSE); -- cgit v1.2.1 From ff40ccf8994ab5a45f88bc95bd0ef7467c0810e0 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 31 Oct 2014 23:26:48 +0100 Subject: policy: move get_best_config() function to nm-default-route-manager No functional change, only refactoring by moving and combining the code. Signed-off-by: Thomas Haller --- src/nm-default-route-manager.c | 152 +++++++++++++++++++++++++++++++++++++++ src/nm-default-route-manager.h | 19 +++++ src/nm-policy.c | 156 ++++++----------------------------------- 3 files changed, 193 insertions(+), 134 deletions(-) diff --git a/src/nm-default-route-manager.c b/src/nm-default-route-manager.c index 8c0529e356..49831375ee 100644 --- a/src/nm-default-route-manager.c +++ b/src/nm-default-route-manager.c @@ -29,6 +29,7 @@ #include "nm-device.h" #include "nm-vpn-connection.h" #include "nm-platform.h" +#include "nm-manager.h" #include "nm-ip4-config.h" #include "nm-ip6-config.h" #include "nm-activation-request.h" @@ -782,6 +783,157 @@ nm_default_route_manager_ip6_get_best_device (NMDefaultRouteManager *self, const /***********************************************************************************/ +static gpointer +_ipx_get_best_config (const VTableIP *vtable, + NMDefaultRouteManager *self, + NMManager *manager, + gboolean ignore_never_default, + NMDevice *preferred_device, + const char **out_ip_iface, + int *out_ip_ifindex, + NMActiveConnection **out_ac, + NMDevice **out_device, + NMVpnConnection **out_vpn) +{ + NMDefaultRouteManagerPrivate *priv; + const GSList *connections, *iter; + NMDevice *device; + NMActRequest *req = NULL; + gpointer config_result = NULL; + + g_return_val_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self), NULL); + + priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + + /* If a VPN connection is active, it is preferred */ + connections = nm_manager_get_active_connections (manager); + for (iter = connections; iter; iter = g_slist_next (iter)) { + NMActiveConnection *active = NM_ACTIVE_CONNECTION (iter->data); + NMVpnConnection *candidate; + NMConnection *tmp; + NMVpnConnectionState vpn_state; + + if (!NM_IS_VPN_CONNECTION (active)) + continue; + + candidate = NM_VPN_CONNECTION (active); + + tmp = nm_active_connection_get_connection (active); + g_assert (tmp); + + vpn_state = nm_vpn_connection_get_vpn_state (candidate); + if (vpn_state != NM_VPN_CONNECTION_STATE_ACTIVATED) + continue; + + if (VTABLE_IS_IP4) { + NMIP4Config *vpn_ip4; + + vpn_ip4 = nm_vpn_connection_get_ip4_config (candidate); + if (!vpn_ip4) + continue; + + if (!ignore_never_default && nm_ip4_config_get_never_default (vpn_ip4)) + continue; + + config_result = vpn_ip4; + } else { + NMIP6Config *vpn_ip6; + + vpn_ip6 = nm_vpn_connection_get_ip6_config (candidate); + if (!vpn_ip6) + continue; + + if (!ignore_never_default && nm_ip6_config_get_never_default (vpn_ip6)) + continue; + + config_result = vpn_ip6; + } + + if (out_vpn) + *out_vpn = candidate; + if (out_ac) + *out_ac = active; + if (out_ip_iface) + *out_ip_iface = nm_vpn_connection_get_ip_iface (candidate); + if (out_ip_ifindex) + *out_ip_ifindex = nm_vpn_connection_get_ip_ifindex (candidate); + break; + } + + /* If no VPN connections, we use the best device instead */ + if (!config_result) { + device = _ipx_get_best_device (vtable, self, nm_manager_get_devices (manager), TRUE, preferred_device); + if (device) { + if (VTABLE_IS_IP4) + config_result = nm_device_get_ip4_config (device); + else + config_result = nm_device_get_ip6_config (device); + g_assert (config_result); + req = nm_device_get_act_request (device); + g_assert (req); + + if (out_device) + *out_device = device; + if (out_ac) + *out_ac = NM_ACTIVE_CONNECTION (req); + if (out_ip_iface) + *out_ip_iface = nm_device_get_ip_iface (device); + if (out_ip_ifindex) + *out_ip_ifindex = nm_device_get_ip_ifindex (device); + } + } + + return config_result; +} + +NMIP4Config * +nm_default_route_manager_ip4_get_best_config (NMDefaultRouteManager *self, + NMManager *manager, + gboolean ignore_never_default, + NMDevice *preferred_device, + const char **out_ip_iface, + int *out_ip_ifindex, + NMActiveConnection **out_ac, + NMDevice **out_device, + NMVpnConnection **out_vpn) +{ + return _ipx_get_best_config (&vtable_ip4, + self, + manager, + ignore_never_default, + preferred_device, + out_ip_iface, + out_ip_ifindex, + out_ac, + out_device, + out_vpn); +} + +NMIP6Config * +nm_default_route_manager_ip6_get_best_config (NMDefaultRouteManager *self, + NMManager *manager, + gboolean ignore_never_default, + NMDevice *preferred_device, + const char **out_ip_iface, + int *out_ip_ifindex, + NMActiveConnection **out_ac, + NMDevice **out_device, + NMVpnConnection **out_vpn) +{ + return _ipx_get_best_config (&vtable_ip6, + self, + manager, + ignore_never_default, + preferred_device, + out_ip_iface, + out_ip_ifindex, + out_ac, + out_device, + out_vpn); +} + +/***********************************************************************************/ + static GPtrArray * _v4_get_entries (NMDefaultRouteManagerPrivate *priv) { diff --git a/src/nm-default-route-manager.h b/src/nm-default-route-manager.h index bf24366329..6961979404 100644 --- a/src/nm-default-route-manager.h +++ b/src/nm-default-route-manager.h @@ -60,5 +60,24 @@ gboolean nm_default_route_manager_ip6_connection_has_default_route (NMDefaultRou NMDevice *nm_default_route_manager_ip4_get_best_device (NMDefaultRouteManager *manager, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device); NMDevice *nm_default_route_manager_ip6_get_best_device (NMDefaultRouteManager *manager, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device); +NMIP4Config *nm_default_route_manager_ip4_get_best_config (NMDefaultRouteManager *manager, + NMManager *nm_manager, + gboolean ignore_never_default, + NMDevice *preferred_device, + const char **out_ip_iface, + int *out_ip_ifindex, + NMActiveConnection **out_ac, + NMDevice **out_device, + NMVpnConnection **out_vpn); +NMIP6Config *nm_default_route_manager_ip6_get_best_config (NMDefaultRouteManager *manager, + NMManager *nm_manager, + gboolean ignore_never_default, + NMDevice *preferred_device, + const char **out_ip_iface, + int *out_ip_ifindex, + NMActiveConnection **out_ac, + NMDevice **out_device, + NMVpnConnection **out_vpn); + #endif /* NM_DEFAULT_ROUTE_MANAGER_H */ diff --git a/src/nm-policy.c b/src/nm-policy.c index 37e34440f2..0a90965892 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -379,7 +379,7 @@ update_default_ac (NMPolicy *policy, } static NMIP4Config * -get_best_ip4_config (NMPolicy *policy, +get_best_ip4_config (NMPolicy *self, gboolean ignore_never_default, const char **out_ip_iface, int *out_ip_ifindex, @@ -387,73 +387,17 @@ get_best_ip4_config (NMPolicy *policy, NMDevice **out_device, NMVpnConnection **out_vpn) { - NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (policy); - const GSList *connections, *iter; - NMDevice *device; - NMActRequest *req = NULL; - NMIP4Config *ip4_config = NULL; - - /* If a VPN connection is active, it is preferred */ - connections = nm_manager_get_active_connections (priv->manager); - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMActiveConnection *active = NM_ACTIVE_CONNECTION (iter->data); - NMVpnConnection *candidate; - NMIP4Config *vpn_ip4; - NMConnection *tmp; - NMVpnConnectionState vpn_state; - - if (!NM_IS_VPN_CONNECTION (active)) - continue; - - candidate = NM_VPN_CONNECTION (active); - - tmp = nm_active_connection_get_connection (active); - g_assert (tmp); - - vpn_state = nm_vpn_connection_get_vpn_state (candidate); - if (vpn_state != NM_VPN_CONNECTION_STATE_ACTIVATED) - continue; - - vpn_ip4 = nm_vpn_connection_get_ip4_config (candidate); - if (!vpn_ip4) - continue; - - if (!ignore_never_default && nm_ip4_config_get_never_default (vpn_ip4)) - continue; - - ip4_config = vpn_ip4; - if (out_vpn) - *out_vpn = candidate; - if (out_ac) - *out_ac = active; - if (out_ip_iface) - *out_ip_iface = nm_vpn_connection_get_ip_iface (candidate); - if (out_ip_ifindex) - *out_ip_ifindex = nm_vpn_connection_get_ip_ifindex (candidate); - break; - } - - /* If no VPN connections, we use the best device instead */ - if (!ip4_config) { - device = get_best_ip4_device (policy, TRUE); - if (device) { - ip4_config = nm_device_get_ip4_config (device); - g_assert (ip4_config); - req = nm_device_get_act_request (device); - g_assert (req); - - if (out_device) - *out_device = device; - if (out_ac) - *out_ac = NM_ACTIVE_CONNECTION (req); - if (out_ip_iface) - *out_ip_iface = nm_device_get_ip_iface (device); - if (out_ip_ifindex) - *out_ip_ifindex = nm_device_get_ip_ifindex (device); - } - } + NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self); - return ip4_config; + return nm_default_route_manager_ip4_get_best_config (nm_default_route_manager_get (), + priv->manager, + ignore_never_default, + priv->default_device4, + out_ip_iface, + out_ip_ifindex, + out_ac, + out_device, + out_vpn); } static void @@ -565,7 +509,7 @@ update_ip4_routing (NMPolicy *policy, gboolean force_update) } static NMIP6Config * -get_best_ip6_config (NMPolicy *policy, +get_best_ip6_config (NMPolicy *self, gboolean ignore_never_default, const char **out_ip_iface, int *out_ip_ifindex, @@ -573,73 +517,17 @@ get_best_ip6_config (NMPolicy *policy, NMDevice **out_device, NMVpnConnection **out_vpn) { - NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (policy); - const GSList *connections, *iter; - NMDevice *device; - NMActRequest *req = NULL; - NMIP6Config *ip6_config = NULL; - - /* If a VPN connection is active, it is preferred */ - connections = nm_manager_get_active_connections (priv->manager); - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMActiveConnection *active = NM_ACTIVE_CONNECTION (iter->data); - NMVpnConnection *candidate; - NMIP6Config *vpn_ip6; - NMConnection *tmp; - NMVpnConnectionState vpn_state; - - if (!NM_IS_VPN_CONNECTION (active)) - continue; - - candidate = NM_VPN_CONNECTION (active); - - tmp = nm_active_connection_get_connection (active); - g_assert (tmp); - - vpn_state = nm_vpn_connection_get_vpn_state (candidate); - if (vpn_state != NM_VPN_CONNECTION_STATE_ACTIVATED) - continue; - - vpn_ip6 = nm_vpn_connection_get_ip6_config (candidate); - if (!vpn_ip6) - continue; - - if (!ignore_never_default && nm_ip6_config_get_never_default (vpn_ip6)) - continue; - - ip6_config = vpn_ip6; - if (out_vpn) - *out_vpn = candidate; - if (out_ac) - *out_ac = NM_ACTIVE_CONNECTION (candidate); - if (out_ip_iface) - *out_ip_iface = nm_vpn_connection_get_ip_iface (candidate); - if (out_ip_ifindex) - *out_ip_ifindex = nm_vpn_connection_get_ip_ifindex (candidate); - break; - } - - /* If no VPN connections, we use the best device instead */ - if (!ip6_config) { - device = get_best_ip6_device (policy, TRUE); - if (device) { - req = nm_device_get_act_request (device); - g_assert (req); - ip6_config = nm_device_get_ip6_config (device); - g_assert (ip6_config); - - if (out_device) - *out_device = device; - if (out_ac) - *out_ac = NM_ACTIVE_CONNECTION (req); - if (out_ip_iface) - *out_ip_iface = nm_device_get_ip_iface (device); - if (out_ip_ifindex) - *out_ip_ifindex = nm_device_get_ip_ifindex (device); - } - } + NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self); - return ip6_config; + return nm_default_route_manager_ip6_get_best_config (nm_default_route_manager_get (), + priv->manager, + ignore_never_default, + priv->default_device6, + out_ip_iface, + out_ip_ifindex, + out_ac, + out_device, + out_vpn); } static void -- cgit v1.2.1 From eb61cdc6c5df248c6c42eeaa8ba54a9fe24ef809 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 4 Nov 2014 15:05:24 +0100 Subject: policy: set default routes for VPN via NMDefaultRouteManager Signed-off-by: Thomas Haller --- src/nm-default-route-manager.c | 22 +++++------ src/nm-default-route-manager.h | 2 - src/nm-policy.c | 86 ++++-------------------------------------- 3 files changed, 17 insertions(+), 93 deletions(-) diff --git a/src/nm-default-route-manager.c b/src/nm-default-route-manager.c index 49831375ee..f5b671a32a 100644 --- a/src/nm-default-route-manager.c +++ b/src/nm-default-route-manager.c @@ -523,10 +523,6 @@ _ipx_update_default_route (const VTableIP *vtable, NMDefaultRouteManager *self, } } } - - /* FIXME: for now, only track the default route for VPN. - * Enable actual configuration of the route later. */ - never_default = TRUE; } } g_assert (!default_route || default_route->plen == 0); @@ -790,7 +786,6 @@ _ipx_get_best_config (const VTableIP *vtable, gboolean ignore_never_default, NMDevice *preferred_device, const char **out_ip_iface, - int *out_ip_ifindex, NMActiveConnection **out_ac, NMDevice **out_device, NMVpnConnection **out_vpn) @@ -803,6 +798,15 @@ _ipx_get_best_config (const VTableIP *vtable, g_return_val_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self), NULL); + if (out_ip_iface) + *out_ip_iface = NULL; + if (out_ac) + *out_ac = NULL; + if (out_device) + *out_device = NULL; + if (out_vpn) + *out_vpn = NULL; + priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); /* If a VPN connection is active, it is preferred */ @@ -855,8 +859,6 @@ _ipx_get_best_config (const VTableIP *vtable, *out_ac = active; if (out_ip_iface) *out_ip_iface = nm_vpn_connection_get_ip_iface (candidate); - if (out_ip_ifindex) - *out_ip_ifindex = nm_vpn_connection_get_ip_ifindex (candidate); break; } @@ -878,8 +880,6 @@ _ipx_get_best_config (const VTableIP *vtable, *out_ac = NM_ACTIVE_CONNECTION (req); if (out_ip_iface) *out_ip_iface = nm_device_get_ip_iface (device); - if (out_ip_ifindex) - *out_ip_ifindex = nm_device_get_ip_ifindex (device); } } @@ -892,7 +892,6 @@ nm_default_route_manager_ip4_get_best_config (NMDefaultRouteManager *self, gboolean ignore_never_default, NMDevice *preferred_device, const char **out_ip_iface, - int *out_ip_ifindex, NMActiveConnection **out_ac, NMDevice **out_device, NMVpnConnection **out_vpn) @@ -903,7 +902,6 @@ nm_default_route_manager_ip4_get_best_config (NMDefaultRouteManager *self, ignore_never_default, preferred_device, out_ip_iface, - out_ip_ifindex, out_ac, out_device, out_vpn); @@ -915,7 +913,6 @@ nm_default_route_manager_ip6_get_best_config (NMDefaultRouteManager *self, gboolean ignore_never_default, NMDevice *preferred_device, const char **out_ip_iface, - int *out_ip_ifindex, NMActiveConnection **out_ac, NMDevice **out_device, NMVpnConnection **out_vpn) @@ -926,7 +923,6 @@ nm_default_route_manager_ip6_get_best_config (NMDefaultRouteManager *self, ignore_never_default, preferred_device, out_ip_iface, - out_ip_ifindex, out_ac, out_device, out_vpn); diff --git a/src/nm-default-route-manager.h b/src/nm-default-route-manager.h index 6961979404..6f49d3130a 100644 --- a/src/nm-default-route-manager.h +++ b/src/nm-default-route-manager.h @@ -65,7 +65,6 @@ NMIP4Config *nm_default_route_manager_ip4_get_best_config (NMDefaultRouteManager gboolean ignore_never_default, NMDevice *preferred_device, const char **out_ip_iface, - int *out_ip_ifindex, NMActiveConnection **out_ac, NMDevice **out_device, NMVpnConnection **out_vpn); @@ -74,7 +73,6 @@ NMIP6Config *nm_default_route_manager_ip6_get_best_config (NMDefaultRouteManager gboolean ignore_never_default, NMDevice *preferred_device, const char **out_ip_iface, - int *out_ip_ifindex, NMActiveConnection **out_ac, NMDevice **out_device, NMVpnConnection **out_vpn); diff --git a/src/nm-policy.c b/src/nm-policy.c index 0a90965892..e31ba13e11 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -382,7 +382,6 @@ static NMIP4Config * get_best_ip4_config (NMPolicy *self, gboolean ignore_never_default, const char **out_ip_iface, - int *out_ip_ifindex, NMActiveConnection **out_ac, NMDevice **out_device, NMVpnConnection **out_vpn) @@ -394,7 +393,6 @@ get_best_ip4_config (NMPolicy *self, ignore_never_default, priv->default_device4, out_ip_iface, - out_ip_ifindex, out_ac, out_device, out_vpn); @@ -408,7 +406,7 @@ update_ip4_dns (NMPolicy *policy, NMDnsManager *dns_mgr) NMVpnConnection *vpn = NULL; NMDnsIPConfigType dns_type = NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE; - ip4_config = get_best_ip4_config (policy, TRUE, &ip_iface, NULL, NULL, NULL, &vpn); + ip4_config = get_best_ip4_config (policy, TRUE, &ip_iface, NULL, NULL, &vpn); if (ip4_config) { if (vpn) dns_type = NM_DNS_IP_CONFIG_TYPE_VPN; @@ -428,16 +426,12 @@ update_ip4_routing (NMPolicy *policy, gboolean force_update) NMConnection *connection = NULL; NMVpnConnection *vpn = NULL; NMActiveConnection *best_ac = NULL; - NMIP4Config *ip4_config = NULL; const char *ip_iface = NULL; - int ip_ifindex = -1; - guint32 gw_addr = 0; /* Note that we might have an IPv4 VPN tunneled over an IPv6-only device, * so we can get (vpn != NULL && best == NULL). */ - ip4_config = get_best_ip4_config (policy, FALSE, &ip_iface, &ip_ifindex, &best_ac, &best, &vpn); - if (!ip4_config) { + if (!get_best_ip4_config (policy, FALSE, &ip_iface, &best_ac, &best, &vpn)) { gboolean changed; changed = (priv->default_device4 != NULL); @@ -452,8 +446,6 @@ update_ip4_routing (NMPolicy *policy, gboolean force_update) if (!force_update && best && (best == priv->default_device4)) return; - gw_addr = nm_ip4_config_get_gateway (ip4_config); - if (best) { const GSList *connections, *iter; @@ -468,32 +460,9 @@ update_ip4_routing (NMPolicy *policy, gboolean force_update) } } - if (vpn) { - in_addr_t int_gw = nm_vpn_connection_get_ip4_internal_gateway (vpn); - int mss = nm_ip4_config_get_mss (ip4_config); - guint32 route_metric = nm_vpn_connection_get_ip4_route_metric (vpn); - - /* If no VPN interface, use the parent interface */ - if (ip_ifindex <= 0) - ip_ifindex = nm_device_get_ip_ifindex (nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn))); - - if (!nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, - 0, 0, int_gw, - route_metric, mss)) { - if (int_gw) { - (void) nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, - int_gw, 32, 0, - route_metric, mss); - if (!nm_platform_ip4_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, - 0, 0, int_gw, - route_metric, mss)) - nm_log_err (LOGD_IP4 | LOGD_VPN, "Failed to set IPv4 default route via VPN."); - } else - nm_log_err (LOGD_IP4 | LOGD_VPN, "Failed to set IPv4 default route via VPN."); - } - + if (vpn) default_device = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn)); - } else + else default_device = best; update_default_ac (policy, best_ac, nm_active_connection_set_default); @@ -512,7 +481,6 @@ static NMIP6Config * get_best_ip6_config (NMPolicy *self, gboolean ignore_never_default, const char **out_ip_iface, - int *out_ip_ifindex, NMActiveConnection **out_ac, NMDevice **out_device, NMVpnConnection **out_vpn) @@ -524,7 +492,6 @@ get_best_ip6_config (NMPolicy *self, ignore_never_default, priv->default_device6, out_ip_iface, - out_ip_ifindex, out_ac, out_device, out_vpn); @@ -538,7 +505,7 @@ update_ip6_dns (NMPolicy *policy, NMDnsManager *dns_mgr) NMVpnConnection *vpn = NULL; NMDnsIPConfigType dns_type = NM_DNS_IP_CONFIG_TYPE_BEST_DEVICE; - ip6_config = get_best_ip6_config (policy, TRUE, &ip_iface, NULL, NULL, NULL, &vpn); + ip6_config = get_best_ip6_config (policy, TRUE, &ip_iface, NULL, NULL, &vpn); if (ip6_config) { if (vpn) dns_type = NM_DNS_IP_CONFIG_TYPE_VPN; @@ -558,16 +525,12 @@ update_ip6_routing (NMPolicy *policy, gboolean force_update) NMConnection *connection = NULL; NMVpnConnection *vpn = NULL; NMActiveConnection *best_ac = NULL; - NMIP6Config *ip6_config = NULL; const char *ip_iface = NULL; - int ip_ifindex = -1; - const struct in6_addr *gw_addr; /* Note that we might have an IPv6 VPN tunneled over an IPv4-only device, * so we can get (vpn != NULL && best == NULL). */ - ip6_config = get_best_ip6_config (policy, FALSE, &ip_iface, &ip_ifindex, &best_ac, &best, &vpn); - if (!ip6_config) { + if (!get_best_ip6_config (policy, FALSE, &ip_iface, &best_ac, &best, &vpn)) { gboolean changed; changed = (priv->default_device6 != NULL); @@ -582,13 +545,6 @@ update_ip6_routing (NMPolicy *policy, gboolean force_update) if (!force_update && best && (best == priv->default_device6)) return; - /* If no better gateway is found, use ::; not all configurations will - * have a gateway, especially WWAN/Point-to-Point connections. - */ - gw_addr = nm_ip6_config_get_gateway (ip6_config); - if (!gw_addr) - gw_addr = &in6addr_any; - if (best) { const GSList *connections, *iter; @@ -603,35 +559,9 @@ update_ip6_routing (NMPolicy *policy, gboolean force_update) } } - if (vpn) { - const struct in6_addr *int_gw = nm_vpn_connection_get_ip6_internal_gateway (vpn); - int mss = nm_ip6_config_get_mss (ip6_config); - guint32 route_metric = nm_vpn_connection_get_ip6_route_metric (vpn); - - if (!int_gw) - int_gw = &in6addr_any; - - /* If no VPN interface, use the parent interface */ - if (ip_ifindex <= 0) - ip_ifindex = nm_device_get_ip_ifindex (nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn))); - - if (!nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, - in6addr_any, 0, *int_gw, - route_metric, mss)) { - if (!IN6_IS_ADDR_UNSPECIFIED (int_gw)) { - (void) nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, - *int_gw, 128, in6addr_any, - route_metric, mss); - if (!nm_platform_ip6_route_add (ip_ifindex, NM_IP_CONFIG_SOURCE_VPN, - in6addr_any, 0, *int_gw, - route_metric, mss)) - nm_log_err (LOGD_IP6 | LOGD_VPN, "Failed to set IPv6 default route via VPN."); - } else - nm_log_err (LOGD_IP6 | LOGD_VPN, "Failed to set IPv6 default route via VPN."); - } - + if (vpn) default_device6 = nm_active_connection_get_device (NM_ACTIVE_CONNECTION (vpn)); - } else + else default_device6 = best; update_default_ac (policy, best_ac, nm_active_connection_set_default6); -- cgit v1.2.1 From f22c72d29946ce25b87c3265edbd8a230f50fd55 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 4 Nov 2014 16:44:08 +0100 Subject: policy: improve get_best_device() to strictly adhering the sort order of the entries get_best_device() has two different modes depending on the @fully_activated argument. If @fully_activated, it only considers devices that are considered as active. Otherwise, it returns the best activating device (if that device is expected to be better then any of the already activated devices). Before, the check whether an activated device is considered best device also involved looking at the device state. This redundancy was harmful because part of NMDefaultRouteManager considered a device as fully activated, but get_best_device() might not return them. Split get_best_device() in two parts. The one part _ipx_get_best_activating_device() now checks for still activating devices. When inspecting devices with an entry, those devices are weighted according to _ipx_get_best_device(). That means that both functions now give a consistent result. Signed-off-by: Thomas Haller --- src/nm-default-route-manager.c | 128 ++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 60 deletions(-) diff --git a/src/nm-default-route-manager.c b/src/nm-default-route-manager.c index f5b671a32a..9bdb190720 100644 --- a/src/nm-default-route-manager.c +++ b/src/nm-default-route-manager.c @@ -670,6 +670,34 @@ nm_default_route_manager_ip6_connection_has_default_route (NMDefaultRouteManager /***********************************************************************************/ /** _ipx_get_best_device: + * @vtable: the virtual table + * @self: #NMDefaultRouteManager + **/ +static NMDevice * +_ipx_get_best_device (const VTableIP *vtable, NMDefaultRouteManager *self) +{ + NMDefaultRouteManagerPrivate *priv; + GPtrArray *entries; + guint i; + + g_return_val_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self), NULL); + + priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + entries = vtable->get_entries (priv); + + for (i = 0; i < entries->len; i++) { + Entry *entry = g_ptr_array_index (entries, i); + + if (!NM_IS_DEVICE (entry->source.pointer)) + continue; + + g_assert (!entry->never_default); + return entry->source.pointer; + } + return NULL; +} + +/** _ipx_get_best_activating_device: * @vtable: the virtual table * @self: #NMDefaultRouteManager * @devices: list of devices to be searched. Only devices from this list will be considered @@ -680,101 +708,81 @@ nm_default_route_manager_ip6_connection_has_default_route (NMDefaultRouteManager * the same priority. **/ static NMDevice * -_ipx_get_best_device (const VTableIP *vtable, NMDefaultRouteManager *self, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device) +_ipx_get_best_activating_device (const VTableIP *vtable, NMDefaultRouteManager *self, const GSList *devices, NMDevice *preferred_device) { NMDefaultRouteManagerPrivate *priv; const GSList *iter; NMDevice *best_device = NULL; guint32 best_prio = G_MAXUINT32; - guint best_idx = G_MAXUINT; + NMDevice *best_activated_device; g_return_val_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self), NULL); priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + best_activated_device = _ipx_get_best_device (vtable, self); + g_return_val_if_fail (!best_activated_device || g_slist_find ((GSList *) devices, best_activated_device), NULL); + for (iter = devices; iter; iter = g_slist_next (iter)) { NMDevice *device = NM_DEVICE (iter->data); - NMDeviceState state = nm_device_get_state (device); guint32 prio; - guint idx; Entry *entry; - if ( state <= NM_DEVICE_STATE_DISCONNECTED - || state >= NM_DEVICE_STATE_DEACTIVATING) - continue; + entry = _entry_find_by_source (vtable->get_entries (priv), device, NULL); - if (fully_activated && state < NM_DEVICE_STATE_SECONDARIES) - continue; + if (entry) { + /* of all the device that have an entry, we already know that best_activated_device + * is the best. entry cannot be better. */ + if (entry->source.device != best_activated_device) + continue; + prio = entry->effective_metric; + } else { + NMDeviceState state = nm_device_get_state (device); - if (!_ipx_connection_has_default_route (vtable, self, nm_device_get_connection (device))) - continue; + if ( state <= NM_DEVICE_STATE_DISCONNECTED + || state >= NM_DEVICE_STATE_DEACTIVATING) + continue; - entry = _entry_find_by_source (vtable->get_entries (priv), device, &idx); - if (fully_activated && !entry) - continue; + if (!_ipx_connection_has_default_route (vtable, self, nm_device_get_connection (device))) + continue; - if (entry) - prio = entry->effective_metric; - else prio = nm_device_get_ip4_route_metric (device); + } prio = vtable->route_metric_normalize (prio); - if (!best_device || prio < best_prio) - goto NEW_BEST_DEVICE; - - if (prio == best_prio) { - /* the devices have the same priority... which one to prefer? */ - - if (fully_activated) { - /* for @fully_activated, respect the order as from the sorted entries. */ - if (idx < best_idx) - goto NEW_BEST_DEVICE; - } else { - /* for !fully_activated, prefer @preferred_device. */ - if (preferred_device == device) - goto NEW_BEST_DEVICE; - if (preferred_device != best_device) { - /* if both devices are not @preferred_device, prefer - * the one with an entry (or the one with lower index, - * if both have an entry). - * - * The following is correct because if @best_device has no entry, @best_idx - * is G_MAXUINT. Also, if the currenty device has no entry, @idx is G_MAXUINT. */ - if (idx < best_idx) - goto NEW_BEST_DEVICE; - } - } + if ( !best_device + || prio < best_prio + || (prio == best_prio && preferred_device == device)) { + best_device = device; + best_prio = prio; } - - continue; -NEW_BEST_DEVICE: - best_device = device; - best_prio = prio; - best_idx = idx; - } - - if (best_device && !fully_activated) { - /* There's only a best activating device if the best device - * among all activating and already-activated devices is a - * still-activating one. - */ - if (nm_device_get_state (best_device) >= NM_DEVICE_STATE_SECONDARIES) - return NULL; } + /* There's only a best activating device if the best device + * among all activating and already-activated devices is a + * still-activating one. + */ + if (best_device && nm_device_get_state (best_device) >= NM_DEVICE_STATE_SECONDARIES) + return NULL; return best_device; } NMDevice * nm_default_route_manager_ip4_get_best_device (NMDefaultRouteManager *self, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device) { - return _ipx_get_best_device (&vtable_ip4, self, devices, fully_activated, preferred_device); + if (fully_activated) + return _ipx_get_best_device (&vtable_ip4, self); + else + return _ipx_get_best_activating_device (&vtable_ip4, self, devices, preferred_device); } NMDevice * nm_default_route_manager_ip6_get_best_device (NMDefaultRouteManager *self, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device) { - return _ipx_get_best_device (&vtable_ip6, self, devices, fully_activated, preferred_device); + if (fully_activated) + return _ipx_get_best_device (&vtable_ip6, self); + else + return _ipx_get_best_activating_device (&vtable_ip6, self, devices, preferred_device); } /***********************************************************************************/ @@ -864,7 +872,7 @@ _ipx_get_best_config (const VTableIP *vtable, /* If no VPN connections, we use the best device instead */ if (!config_result) { - device = _ipx_get_best_device (vtable, self, nm_manager_get_devices (manager), TRUE, preferred_device); + device = _ipx_get_best_device (vtable, self); if (device) { if (VTABLE_IS_IP4) config_result = nm_device_get_ip4_config (device); -- cgit v1.2.1 From 6e409ef91f2da4905118816bfd708e718bc4292e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 4 Nov 2014 17:28:22 +0100 Subject: policy: return best config based on the internal sorting of NMDefaultRouteManager Now that both VPN and devices are managed (and ordered) by NMDefaultRouteManager, refactor get_best_config() to use the priority accordingly. Before, we would first iterate over all VPN connections and returning the best one. Only if no suitable VPN connection was found, a best device would be returned. Modify get_best_config() to treat VPN and device the same and return the best one based on the route metric. With this change, get_best_config() gives consistent results together with get_best_device(). Also, you can configure that a device gets a higher priority then a VPN. Signed-off-by: Thomas Haller --- src/nm-default-route-manager.c | 85 ++++++++++++------------------------------ src/nm-default-route-manager.h | 4 -- src/nm-policy.c | 8 ---- 3 files changed, 24 insertions(+), 73 deletions(-) diff --git a/src/nm-default-route-manager.c b/src/nm-default-route-manager.c index 9bdb190720..0bf37f0522 100644 --- a/src/nm-default-route-manager.c +++ b/src/nm-default-route-manager.c @@ -790,18 +790,15 @@ nm_default_route_manager_ip6_get_best_device (NMDefaultRouteManager *self, const static gpointer _ipx_get_best_config (const VTableIP *vtable, NMDefaultRouteManager *self, - NMManager *manager, gboolean ignore_never_default, - NMDevice *preferred_device, const char **out_ip_iface, NMActiveConnection **out_ac, NMDevice **out_device, NMVpnConnection **out_vpn) { NMDefaultRouteManagerPrivate *priv; - const GSList *connections, *iter; - NMDevice *device; - NMActRequest *req = NULL; + GPtrArray *entries; + guint i; gpointer config_result = NULL; g_return_val_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self), NULL); @@ -817,63 +814,36 @@ _ipx_get_best_config (const VTableIP *vtable, priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); - /* If a VPN connection is active, it is preferred */ - connections = nm_manager_get_active_connections (manager); - for (iter = connections; iter; iter = g_slist_next (iter)) { - NMActiveConnection *active = NM_ACTIVE_CONNECTION (iter->data); - NMVpnConnection *candidate; - NMConnection *tmp; - NMVpnConnectionState vpn_state; - - if (!NM_IS_VPN_CONNECTION (active)) - continue; - - candidate = NM_VPN_CONNECTION (active); + g_return_val_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self), NULL); - tmp = nm_active_connection_get_connection (active); - g_assert (tmp); + priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); + entries = vtable->get_entries (priv); - vpn_state = nm_vpn_connection_get_vpn_state (candidate); - if (vpn_state != NM_VPN_CONNECTION_STATE_ACTIVATED) - continue; + for (i = 0; i < entries->len; i++) { + Entry *entry = g_ptr_array_index (entries, i); - if (VTABLE_IS_IP4) { - NMIP4Config *vpn_ip4; + if (!NM_IS_DEVICE (entry->source.pointer)) { + NMVpnConnection *vpn = NM_VPN_CONNECTION (entry->source.vpn); - vpn_ip4 = nm_vpn_connection_get_ip4_config (candidate); - if (!vpn_ip4) + if (entry->never_default && !ignore_never_default) continue; - if (!ignore_never_default && nm_ip4_config_get_never_default (vpn_ip4)) - continue; + if (VTABLE_IS_IP4) + config_result = nm_vpn_connection_get_ip4_config (vpn); + else + config_result = nm_vpn_connection_get_ip6_config (vpn); + g_assert (config_result); - config_result = vpn_ip4; + if (out_vpn) + *out_vpn = vpn; + if (out_ac) + *out_ac = NM_ACTIVE_CONNECTION (vpn); + if (out_ip_iface) + *out_ip_iface = nm_vpn_connection_get_ip_iface (vpn); } else { - NMIP6Config *vpn_ip6; - - vpn_ip6 = nm_vpn_connection_get_ip6_config (candidate); - if (!vpn_ip6) - continue; - - if (!ignore_never_default && nm_ip6_config_get_never_default (vpn_ip6)) - continue; + NMDevice *device = entry->source.device; + NMActRequest *req; - config_result = vpn_ip6; - } - - if (out_vpn) - *out_vpn = candidate; - if (out_ac) - *out_ac = active; - if (out_ip_iface) - *out_ip_iface = nm_vpn_connection_get_ip_iface (candidate); - break; - } - - /* If no VPN connections, we use the best device instead */ - if (!config_result) { - device = _ipx_get_best_device (vtable, self); - if (device) { if (VTABLE_IS_IP4) config_result = nm_device_get_ip4_config (device); else @@ -889,6 +859,7 @@ _ipx_get_best_config (const VTableIP *vtable, if (out_ip_iface) *out_ip_iface = nm_device_get_ip_iface (device); } + break; } return config_result; @@ -896,9 +867,7 @@ _ipx_get_best_config (const VTableIP *vtable, NMIP4Config * nm_default_route_manager_ip4_get_best_config (NMDefaultRouteManager *self, - NMManager *manager, gboolean ignore_never_default, - NMDevice *preferred_device, const char **out_ip_iface, NMActiveConnection **out_ac, NMDevice **out_device, @@ -906,9 +875,7 @@ nm_default_route_manager_ip4_get_best_config (NMDefaultRouteManager *self, { return _ipx_get_best_config (&vtable_ip4, self, - manager, ignore_never_default, - preferred_device, out_ip_iface, out_ac, out_device, @@ -917,9 +884,7 @@ nm_default_route_manager_ip4_get_best_config (NMDefaultRouteManager *self, NMIP6Config * nm_default_route_manager_ip6_get_best_config (NMDefaultRouteManager *self, - NMManager *manager, gboolean ignore_never_default, - NMDevice *preferred_device, const char **out_ip_iface, NMActiveConnection **out_ac, NMDevice **out_device, @@ -927,9 +892,7 @@ nm_default_route_manager_ip6_get_best_config (NMDefaultRouteManager *self, { return _ipx_get_best_config (&vtable_ip6, self, - manager, ignore_never_default, - preferred_device, out_ip_iface, out_ac, out_device, diff --git a/src/nm-default-route-manager.h b/src/nm-default-route-manager.h index 6f49d3130a..88fb59f0c9 100644 --- a/src/nm-default-route-manager.h +++ b/src/nm-default-route-manager.h @@ -61,17 +61,13 @@ NMDevice *nm_default_route_manager_ip4_get_best_device (NMDefaultRouteManager *m NMDevice *nm_default_route_manager_ip6_get_best_device (NMDefaultRouteManager *manager, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device); NMIP4Config *nm_default_route_manager_ip4_get_best_config (NMDefaultRouteManager *manager, - NMManager *nm_manager, gboolean ignore_never_default, - NMDevice *preferred_device, const char **out_ip_iface, NMActiveConnection **out_ac, NMDevice **out_device, NMVpnConnection **out_vpn); NMIP6Config *nm_default_route_manager_ip6_get_best_config (NMDefaultRouteManager *manager, - NMManager *nm_manager, gboolean ignore_never_default, - NMDevice *preferred_device, const char **out_ip_iface, NMActiveConnection **out_ac, NMDevice **out_device, diff --git a/src/nm-policy.c b/src/nm-policy.c index e31ba13e11..963be3099b 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -386,12 +386,8 @@ get_best_ip4_config (NMPolicy *self, NMDevice **out_device, NMVpnConnection **out_vpn) { - NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self); - return nm_default_route_manager_ip4_get_best_config (nm_default_route_manager_get (), - priv->manager, ignore_never_default, - priv->default_device4, out_ip_iface, out_ac, out_device, @@ -485,12 +481,8 @@ get_best_ip6_config (NMPolicy *self, NMDevice **out_device, NMVpnConnection **out_vpn) { - NMPolicyPrivate *priv = NM_POLICY_GET_PRIVATE (self); - return nm_default_route_manager_ip6_get_best_config (nm_default_route_manager_get (), - priv->manager, ignore_never_default, - priv->default_device6, out_ip_iface, out_ac, out_device, -- cgit v1.2.1 From 5dcd853bff5044edaa135333223c89c075faeeca Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Nov 2014 14:09:24 +0100 Subject: core: even vor MODEM devices don't add IPv6 default routes without gateway We'll ever have WWAN devices with a NULL gateway because the IPv6 over WWAN still uses router advertisements to get a prefix. Thus you'll always have a gateway if the device has real IPv6 connectivity. For the IPv4 case, we still allow default routes without gateway on WWAN. https://bugzilla.gnome.org/show_bug.cgi?id=735512 Signed-off-by: Thomas Haller --- src/devices/nm-device.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 3d7a34ba8a..9eb25c96aa 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -3355,11 +3355,10 @@ ip6_config_merge_and_apply (NMDevice *self, priv->default_route.v6_has = _device_get_default_route_from_platform (self, AF_INET, (NMPlatformIPRoute *) route); else { gateway = nm_ip6_config_get_gateway (composite); - if ( gateway - || nm_device_get_device_type (self) == NM_DEVICE_TYPE_MODEM) { + if (gateway) { memset (route, 0, sizeof (*route)); route->source = NM_IP_CONFIG_SOURCE_USER; - route->gateway = gateway ? *gateway : in6addr_any; + route->gateway = *gateway; route->metric = nm_device_get_ip6_route_metric (self); route->mss = nm_ip6_config_get_mss (composite); priv->default_route.v6_has = TRUE; -- cgit v1.2.1 From 2cfd1647d3723baa42ea48f50f0c8a97c3d96d2f Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Nov 2014 16:34:13 +0100 Subject: core: ensure generated connections has no gateway if it has no addresses Fixes: f17699f4e3dacb9358a8503c8b15efe3cb852b48 Signed-off-by: Thomas Haller --- src/nm-ip4-config.c | 6 +++++- src/nm-ip6-config.c | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 5fddc73798..34d613437d 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -430,7 +430,8 @@ nm_ip4_config_create_setting (const NMIP4Config *config) } /* Gateway */ - if (gateway) { + if ( gateway + && nm_setting_ip_config_get_num_addresses (s_ip4) > 0) { g_object_set (s_ip4, NM_SETTING_IP_CONFIG_GATEWAY, nm_utils_inet4_ntop (gateway, NULL), NULL); @@ -586,6 +587,9 @@ nm_ip4_config_subtract (NMIP4Config *dst, const NMIP4Config *src) if (nm_ip4_config_get_gateway (src) == nm_ip4_config_get_gateway (dst)) nm_ip4_config_set_gateway (dst, 0); + if (!nm_ip4_config_get_num_addresses (dst)) + nm_ip4_config_set_gateway (dst, 0); + /* routes */ for (i = 0; i < nm_ip4_config_get_num_routes (src); i++) { const NMPlatformIP4Route *src_route = nm_ip4_config_get_route (src, i); diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index 9423d8a0f9..5ad16b3249 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -538,7 +538,8 @@ nm_ip6_config_create_setting (const NMIP6Config *config) } /* Gateway */ - if (gateway) { + if ( gateway + && nm_setting_ip_config_get_num_addresses (s_ip6) > 0) { g_object_set (s_ip6, NM_SETTING_IP_CONFIG_GATEWAY, nm_utils_inet6_ntop (gateway, NULL), NULL); @@ -700,6 +701,9 @@ nm_ip6_config_subtract (NMIP6Config *dst, const NMIP6Config *src) if (src_tmp && dst_tmp && IN6_ARE_ADDR_EQUAL (src_tmp, dst_tmp)) nm_ip6_config_set_gateway (dst, NULL); + if (!nm_ip6_config_get_num_addresses (dst)) + nm_ip6_config_set_gateway (dst, NULL); + /* routes */ for (i = 0; i < nm_ip6_config_get_num_routes (src); i++) { const NMPlatformIP6Route *src_route = nm_ip6_config_get_route (src, i); -- cgit v1.2.1 From 99d0fe3006c1435a7f11011392a106b50e45a4bb Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 7 Nov 2014 10:55:58 -0500 Subject: core: fix new NMIP4Config and NMIP6Config properties The AddressData and RouteData marshalling code were still using the types from an earlier version of the branch. Fix that. --- include/nm-dbus-glib-types.h | 4 +- src/nm-ip4-config.c | 113 ++++++++++++++++++++++--------------------- src/nm-ip6-config.c | 98 ++++++++++++++++++------------------- 3 files changed, 108 insertions(+), 107 deletions(-) diff --git a/include/nm-dbus-glib-types.h b/include/nm-dbus-glib-types.h index 1b41c9ff18..ad11ab6812 100644 --- a/include/nm-dbus-glib-types.h +++ b/include/nm-dbus-glib-types.h @@ -37,9 +37,9 @@ #define DBUS_TYPE_G_IP6_ROUTE (dbus_g_type_get_struct ("GValueArray", DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT, DBUS_TYPE_G_UCHAR_ARRAY, G_TYPE_UINT, G_TYPE_INVALID)) #define DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_IP6_ROUTE)) -#define DBUS_TYPE_NM_IP_ADDRESS (dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_UINT, DBUS_TYPE_G_MAP_OF_STRING, G_TYPE_INVALID)) +#define DBUS_TYPE_NM_IP_ADDRESS DBUS_TYPE_G_MAP_OF_VARIANT #define DBUS_TYPE_NM_IP_ADDRESSES (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_NM_IP_ADDRESS)) -#define DBUS_TYPE_NM_IP_ROUTE (dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_UINT, DBUS_TYPE_G_MAP_OF_STRING, G_TYPE_INVALID)) +#define DBUS_TYPE_NM_IP_ROUTE DBUS_TYPE_G_MAP_OF_VARIANT #define DBUS_TYPE_NM_IP_ROUTES (dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_NM_IP_ROUTE)) #endif /* __NM_DBUS_GLIB_TYPES_H__ */ diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 34d613437d..72c1b0cd75 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -1728,6 +1728,15 @@ finalize (GObject *object) G_OBJECT_CLASS (nm_ip4_config_parent_class)->finalize (object); } +static void +gvalue_destroy (gpointer data) +{ + GValue *value = (GValue *) data; + + g_value_unset (value); + g_slice_free (GValue, value); +} + static void get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) @@ -1744,29 +1753,29 @@ get_property (GObject *object, guint prop_id, for (i = 0; i < naddr; i++) { const NMPlatformIP4Address *address = nm_ip4_config_get_address (config, i); - GValueArray *array = g_value_array_new (3); - GHashTable *attrs; - GValue val = { 0, }; - - g_value_init (&val, G_TYPE_STRING); - g_value_set_string (&val, nm_utils_inet4_ntop (address->address, NULL)); - g_value_array_append (array, &val); - g_value_unset (&val); - - g_value_init (&val, G_TYPE_UINT); - g_value_set_uint (&val, address->plen); - g_value_array_append (array, &val); - g_value_unset (&val); - - g_value_init (&val, DBUS_TYPE_G_MAP_OF_STRING); - attrs = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free); - if (*address->label) - g_hash_table_insert (attrs, "label", g_strdup (address->label)); - g_value_take_boxed (&val, attrs); - g_value_array_append (array, &val); - g_value_unset (&val); + GHashTable *addr_hash; + GValue *val; + + addr_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, gvalue_destroy); + + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_STRING); + g_value_set_string (val, nm_utils_inet4_ntop (address->address, NULL)); + g_hash_table_insert (addr_hash, "address", val); + + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_UINT); + g_value_set_uint (val, address->plen); + g_hash_table_insert (addr_hash, "prefix", val); + + if (*address->label) { + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_STRING); + g_value_set_string (val, address->label); + g_hash_table_insert (addr_hash, "label", val); + } - g_ptr_array_add (addresses, array); + g_ptr_array_add (addresses, addr_hash); } g_value_take_boxed (value, addresses); @@ -1801,40 +1810,34 @@ get_property (GObject *object, guint prop_id, for (i = 0; i < nroutes; i++) { const NMPlatformIP4Route *route = nm_ip4_config_get_route (config, i); - GValueArray *array = g_value_array_new (5); - GHashTable *attrs; - GValue val = { 0, }; - - g_value_init (&val, G_TYPE_STRING); - g_value_set_string (&val, nm_utils_inet4_ntop (route->network, NULL)); - g_value_array_append (array, &val); - g_value_unset (&val); - - g_value_init (&val, G_TYPE_UINT); - g_value_set_uint (&val, route->plen); - g_value_array_append (array, &val); - g_value_unset (&val); - - g_value_init (&val, G_TYPE_STRING); - if (route->gateway) - g_value_set_string (&val, nm_utils_inet4_ntop (route->gateway, NULL)); - else - g_value_set_string (&val, ""); - g_value_array_append (array, &val); - g_value_unset (&val); - - g_value_init (&val, G_TYPE_INT64); - g_value_set_int64 (&val, route->metric); - g_value_array_append (array, &val); - g_value_unset (&val); - - g_value_init (&val, DBUS_TYPE_G_MAP_OF_STRING); - attrs = g_hash_table_new (g_str_hash, g_str_equal); - g_value_take_boxed (&val, attrs); - g_value_array_append (array, &val); - g_value_unset (&val); + GHashTable *route_hash; + GValue *val; + + route_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, gvalue_destroy); + + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_STRING); + g_value_set_string (val, nm_utils_inet4_ntop (route->network, NULL)); + g_hash_table_insert (route_hash, "dest", val); + + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_UINT); + g_value_set_uint (val, route->plen); + g_hash_table_insert (route_hash, "prefix", val); + + if (route->gateway) { + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_STRING); + g_value_set_string (val, nm_utils_inet4_ntop (route->gateway, NULL)); + g_hash_table_insert (route_hash, "gateway", val); + } - g_ptr_array_add (routes, array); + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_UINT); + g_value_set_uint (val, route->metric); + g_hash_table_insert (route_hash, "metric", val); + + g_ptr_array_add (routes, route_hash); } g_value_take_boxed (value, routes); diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index 5ad16b3249..fe45f824d8 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -1609,6 +1609,15 @@ nameservers_to_gvalue (GArray *array, GValue *value) g_value_take_boxed (value, dns); } +static void +gvalue_destroy (gpointer data) +{ + GValue *value = (GValue *) data; + + g_value_unset (value); + g_slice_free (GValue, value); +} + static void get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) @@ -1625,27 +1634,22 @@ get_property (GObject *object, guint prop_id, for (i = 0; i < naddr; i++) { const NMPlatformIP6Address *address = nm_ip6_config_get_address (config, i); - GValueArray *array = g_value_array_new (3); - GHashTable *attrs; - GValue val = { 0, }; + GHashTable *addr_hash; + GValue *val; - g_value_init (&val, G_TYPE_STRING); - g_value_set_string (&val, nm_utils_inet6_ntop (&address->address, NULL)); - g_value_array_append (array, &val); - g_value_unset (&val); + addr_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, gvalue_destroy); - g_value_init (&val, G_TYPE_UINT); - g_value_set_uint (&val, address->plen); - g_value_array_append (array, &val); - g_value_unset (&val); + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_STRING); + g_value_set_string (val, nm_utils_inet6_ntop (&address->address, NULL)); + g_hash_table_insert (addr_hash, "address", val); - g_value_init (&val, DBUS_TYPE_G_MAP_OF_STRING); - attrs = g_hash_table_new (g_str_hash, g_str_equal); - g_value_take_boxed (&val, attrs); - g_value_array_append (array, &val); - g_value_unset (&val); + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_UINT); + g_value_set_uint (val, address->plen); + g_hash_table_insert (addr_hash, "prefix", val); - g_ptr_array_add (addresses, array); + g_ptr_array_add (addresses, addr_hash); } g_value_take_boxed (value, addresses); @@ -1701,40 +1705,34 @@ get_property (GObject *object, guint prop_id, for (i = 0; i < nroutes; i++) { const NMPlatformIP6Route *route = nm_ip6_config_get_route (config, i); - GValueArray *array = g_value_array_new (5); - GHashTable *attrs; - GValue val = { 0, }; - - g_value_init (&val, G_TYPE_STRING); - g_value_set_string (&val, nm_utils_inet6_ntop (&route->network, NULL)); - g_value_array_append (array, &val); - g_value_unset (&val); - - g_value_init (&val, G_TYPE_UINT); - g_value_set_uint (&val, route->plen); - g_value_array_append (array, &val); - g_value_unset (&val); - - g_value_init (&val, G_TYPE_STRING); - if (memcmp (&route->gateway, &in6addr_any, sizeof (struct in6_addr)) != 0) - g_value_set_string (&val, nm_utils_inet6_ntop (&route->gateway, NULL)); - else - g_value_set_string (&val, ""); - g_value_array_append (array, &val); - g_value_unset (&val); - - g_value_init (&val, G_TYPE_INT64); - g_value_set_int64 (&val, route->metric); - g_value_array_append (array, &val); - g_value_unset (&val); - - g_value_init (&val, DBUS_TYPE_G_MAP_OF_STRING); - attrs = g_hash_table_new (g_str_hash, g_str_equal); - g_value_take_boxed (&val, attrs); - g_value_array_append (array, &val); - g_value_unset (&val); + GHashTable *route_hash; + GValue *val; + + route_hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, gvalue_destroy); + + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_STRING); + g_value_set_string (val, nm_utils_inet6_ntop (&route->network, NULL)); + g_hash_table_insert (route_hash, "dest", val); + + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_UINT); + g_value_set_uint (val, route->plen); + g_hash_table_insert (route_hash, "prefix", val); + + if (!IN6_IS_ADDR_UNSPECIFIED (&route->gateway)) { + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_STRING); + g_value_set_string (val, nm_utils_inet6_ntop (&route->gateway, NULL)); + g_hash_table_insert (route_hash, "gateway", val); + } - g_ptr_array_add (routes, array); + val = g_slice_new0 (GValue); + g_value_init (val, G_TYPE_UINT); + g_value_set_uint (val, route->metric); + g_hash_table_insert (route_hash, "metric", val); + + g_ptr_array_add (routes, route_hash); } g_value_take_boxed (value, routes); -- cgit v1.2.1 From 9ed96e15eb57865660ce39763696352f8d33d78c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Fri, 7 Nov 2014 17:03:20 +0100 Subject: build: add the compatibility header for g_clear_pointer() Necessary for GLib < 2.34 --- src/devices/adsl/nm-atm-manager.c | 1 + src/nm-active-connection.c | 1 + src/settings/nm-settings-connection.c | 1 + 3 files changed, 3 insertions(+) diff --git a/src/devices/adsl/nm-atm-manager.c b/src/devices/adsl/nm-atm-manager.c index 37be69c6e4..1c8ea6b6b6 100644 --- a/src/devices/adsl/nm-atm-manager.c +++ b/src/devices/adsl/nm-atm-manager.c @@ -28,6 +28,7 @@ #include "nm-device-adsl.h" #include "nm-device-factory.h" #include "nm-logging.h" +#include "nm-glib-compat.h" typedef struct { GUdevClient *client; diff --git a/src/nm-active-connection.c b/src/nm-active-connection.c index 73925659df..ff70d802bf 100644 --- a/src/nm-active-connection.c +++ b/src/nm-active-connection.c @@ -33,6 +33,7 @@ #include "NetworkManagerUtils.h" #include "gsystem-local-alloc.h" #include "nm-active-connection-glue.h" +#include "nm-glib-compat.h" /* Base class for anything implementing the Connection.Active D-Bus interface */ G_DEFINE_ABSTRACT_TYPE (NMActiveConnection, nm_active_connection, G_TYPE_OBJECT) diff --git a/src/settings/nm-settings-connection.c b/src/settings/nm-settings-connection.c index 6a71c02036..78a830bad6 100644 --- a/src/settings/nm-settings-connection.c +++ b/src/settings/nm-settings-connection.c @@ -37,6 +37,7 @@ #include "NetworkManagerUtils.h" #include "nm-properties-changed-signal.h" #include "nm-core-internal.h" +#include "nm-glib-compat.h" #define SETTINGS_TIMESTAMPS_FILE NMSTATEDIR "/timestamps" #define SETTINGS_SEEN_BSSIDS_FILE NMSTATEDIR "/seen-bssids" -- cgit v1.2.1 From 065a3240fb22b004cb0a92df7b6cf787be8a2eb1 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Nov 2014 18:38:58 +0100 Subject: policy: fix get_best_device() to return only active devices from the list This fixes an assertion during shutdown. NMManager:dispose() calls remove_device(), which eventually hit the assertion in nm_default_route_manager_ip4_get_best_device(). Remove the assertion, but also make sure that the function only returns devices from the provided list. It is counter intuitive, that the function might return devices that are not in the provided list. Signed-off-by: Thomas Haller --- src/nm-default-route-manager.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/nm-default-route-manager.c b/src/nm-default-route-manager.c index 0bf37f0522..22a042a4af 100644 --- a/src/nm-default-route-manager.c +++ b/src/nm-default-route-manager.c @@ -669,12 +669,8 @@ nm_default_route_manager_ip6_connection_has_default_route (NMDefaultRouteManager /***********************************************************************************/ -/** _ipx_get_best_device: - * @vtable: the virtual table - * @self: #NMDefaultRouteManager - **/ static NMDevice * -_ipx_get_best_device (const VTableIP *vtable, NMDefaultRouteManager *self) +_ipx_get_best_device (const VTableIP *vtable, NMDefaultRouteManager *self, const GSList *devices) { NMDefaultRouteManagerPrivate *priv; GPtrArray *entries; @@ -682,6 +678,9 @@ _ipx_get_best_device (const VTableIP *vtable, NMDefaultRouteManager *self) g_return_val_if_fail (NM_IS_DEFAULT_ROUTE_MANAGER (self), NULL); + if (!devices) + return NULL; + priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); entries = vtable->get_entries (priv); @@ -692,7 +691,9 @@ _ipx_get_best_device (const VTableIP *vtable, NMDefaultRouteManager *self) continue; g_assert (!entry->never_default); - return entry->source.pointer; + + if (g_slist_find ((GSList *) devices, entry->source.device)) + return entry->source.pointer; } return NULL; } @@ -720,8 +721,7 @@ _ipx_get_best_activating_device (const VTableIP *vtable, NMDefaultRouteManager * priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); - best_activated_device = _ipx_get_best_device (vtable, self); - g_return_val_if_fail (!best_activated_device || g_slist_find ((GSList *) devices, best_activated_device), NULL); + best_activated_device = _ipx_get_best_device (vtable, self, devices); for (iter = devices; iter; iter = g_slist_next (iter)) { NMDevice *device = NM_DEVICE (iter->data); @@ -771,7 +771,7 @@ NMDevice * nm_default_route_manager_ip4_get_best_device (NMDefaultRouteManager *self, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device) { if (fully_activated) - return _ipx_get_best_device (&vtable_ip4, self); + return _ipx_get_best_device (&vtable_ip4, self, devices); else return _ipx_get_best_activating_device (&vtable_ip4, self, devices, preferred_device); } @@ -780,7 +780,7 @@ NMDevice * nm_default_route_manager_ip6_get_best_device (NMDefaultRouteManager *self, const GSList *devices, gboolean fully_activated, NMDevice *preferred_device) { if (fully_activated) - return _ipx_get_best_device (&vtable_ip6, self); + return _ipx_get_best_device (&vtable_ip6, self, devices); else return _ipx_get_best_activating_device (&vtable_ip6, self, devices, preferred_device); } -- cgit v1.2.1 From b5668f22be6e8b2d3e93d84473e3f9d6d0117404 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Thu, 22 May 2014 11:09:11 -0500 Subject: ifcfg-rh: (trivial) fix formatting --- src/settings/plugins/ifcfg-rh/shvar.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/settings/plugins/ifcfg-rh/shvar.c b/src/settings/plugins/ifcfg-rh/shvar.c index b4c911fb6d..9a4d1d1e3a 100644 --- a/src/settings/plugins/ifcfg-rh/shvar.c +++ b/src/settings/plugins/ifcfg-rh/shvar.c @@ -238,8 +238,7 @@ svEscape (const char *s) new[j++] = s[i]; } new[j++] = '"'; - new[j++] = '\0' -; + new[j++] = '\0'; g_assert (j == slen + mangle - newline + 3); return new; -- cgit v1.2.1 From 941897cc9721997d106e2ab7ac438dafe441fb4e Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Thu, 22 May 2014 11:09:36 -0500 Subject: ifcfg-rh: strip trailing whitespace from ifcfg files (rh #1100336) shvar.c has apparently never stripped trailing whitespace, but obviously a shell doesn't care. Unfortunately NM does. Strip trailing whitespace before unescaping, to preserve quoted whitespace. https://bugzilla.redhat.com/show_bug.cgi?id=1100336 --- src/settings/plugins/ifcfg-rh/shvar.c | 3 +- .../ifcfg-rh/tests/network-scripts/Makefile.am | 3 +- .../ifcfg-test-vlan-trailing-spaces | 11 +++++++ .../plugins/ifcfg-rh/tests/test-ifcfg-rh.c | 38 ++++++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-vlan-trailing-spaces diff --git a/src/settings/plugins/ifcfg-rh/shvar.c b/src/settings/plugins/ifcfg-rh/shvar.c index 9a4d1d1e3a..bdfd40a31f 100644 --- a/src/settings/plugins/ifcfg-rh/shvar.c +++ b/src/settings/plugins/ifcfg-rh/shvar.c @@ -265,7 +265,8 @@ svGetValue (shvarFile *s, const char *key, gboolean verbatim) for (s->current = s->lineList; s->current; s->current = s->current->next) { line = s->current->data; if (!strncmp (keyString, line, len)) { - value = g_strdup (line + len); + /* Strip trailing spaces before unescaping to preserve spaces quoted whitespace */ + value = g_strchomp (g_strdup (line + len)); if (!verbatim) svUnescape (value); break; diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/Makefile.am b/src/settings/plugins/ifcfg-rh/tests/network-scripts/Makefile.am index 9c9a3b13f8..8941da3a62 100644 --- a/src/settings/plugins/ifcfg-rh/tests/network-scripts/Makefile.am +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/Makefile.am @@ -119,7 +119,8 @@ EXTRA_DIST = \ ifcfg-test-fcoe-vn2vn \ ifcfg-test-team-master \ ifcfg-test-team-port \ - ifcfg-test-team-port-empty-config + ifcfg-test-team-port-empty-config \ + ifcfg-test-vlan-trailing-spaces # make target dependencies can't have colons in their names, which ends up # meaning that we can't add the alias files to EXTRA_DIST diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-vlan-trailing-spaces b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-vlan-trailing-spaces new file mode 100644 index 0000000000..4a31e475ae --- /dev/null +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-vlan-trailing-spaces @@ -0,0 +1,11 @@ +DEVICE="vlan201" +ONBOOT=yes +NETBOOT=yes +BOOTPROTO=none +IPADDR="10.130.70.7" +NETMASK="255.255.0.0" +TYPE=Vlan +NAME="vlan201" +VLAN=yes +PHYSDEV="enccw0.0.fb00" + diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index 9964254b71..fbb0b9686a 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -13452,6 +13452,43 @@ test_svUnescape (void) g_rand_free (r); } +static void +test_read_vlan_trailing_spaces (void) +{ + const char *testfile = TEST_IFCFG_DIR"/network-scripts/ifcfg-test-vlan-trailing-spaces"; + NMConnection *connection; + gboolean success; + GError *error = NULL; + NMSettingVlan *s_vlan; + char *contents = NULL; + + /* Ensure there is whitespace at the end of the VLAN interface name, + * to prevent the whitespace getting stripped off and committed mistakenly + * by something in the future. + */ + success = g_file_get_contents (testfile, &contents, NULL, &error); + g_assert_no_error (error); + g_assert (success); + g_assert (contents && contents[0]); + g_assert (strstr (contents, "DEVICE=\"vlan201\" \n")); + g_free (contents); + + connection = connection_from_file (testfile, NULL, TYPE_ETHERNET, NULL, + NULL, NULL, NULL, &error, NULL); + g_assert_no_error (error); + g_assert (connection != NULL); + + s_vlan = nm_connection_get_setting_vlan (connection); + g_assert (s_vlan); + + g_assert_cmpstr (nm_connection_get_interface_name (connection), ==, "vlan201"); + g_assert_cmpstr (nm_setting_vlan_get_parent (s_vlan), ==, "enccw0.0.fb00"); + g_assert_cmpint (nm_setting_vlan_get_id (s_vlan), ==, 201); + g_assert_cmpint (nm_setting_vlan_get_flags (s_vlan), ==, 0); + + g_object_unref (connection); +} + #define TEST_IFCFG_WIFI_OPEN_SSID_BAD_HEX TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wifi-open-ssid-bad-hex" #define TEST_IFCFG_WIFI_OPEN_SSID_LONG_QUOTED TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wifi-open-ssid-long-quoted" @@ -13477,6 +13514,7 @@ int main (int argc, char **argv) nmtst_init_assert_logging (&argc, &argv); g_test_add_func (TPATH "svUnescape", test_svUnescape); + g_test_add_func (TPATH "vlan-trailing-spaces", test_read_vlan_trailing_spaces); g_test_add_func (TPATH "unmanaged", test_read_unmanaged); g_test_add_func (TPATH "unmanaged-unrecognized", test_read_unmanaged_unrecognized); -- cgit v1.2.1 From cbabd135818fd4749ad0b827ba1f0bc444b92a97 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 7 Nov 2014 12:38:17 -0500 Subject: libnm, docs: docs fixes Update the docs build to include and exclude the correct files. Fill in some missing documentation, and fix problems in the existing docs. (In particular, "<" can't appear as a literal in documentation, so change it to "<". Also, "PKCS#12" has to be written as "PKCS#12", or gtk-doc will think "#12" is a reference to a type named "12".) --- docs/libnm/Makefile.am | 11 ++++--- docs/libnm/libnm-docs.xml | 10 ++++++ libnm-core/nm-connection.h | 7 ++++ libnm-core/nm-dbus-interface.h | 2 -- libnm-core/nm-errors.h | 12 +++---- libnm-core/nm-setting-8021x.c | 57 +++++++++++++++++---------------- libnm-core/nm-setting-8021x.h | 2 +- libnm-core/nm-setting-bluetooth.c | 3 +- libnm-core/nm-utils.c | 4 +-- libnm/nm-device.c | 3 +- libnm/nm-types.h | 2 -- libnm/nm-vpn-editor-plugin.h | 67 +++++++++++++++++++++++---------------- libnm/nm-wimax-nsp.h | 9 ++++++ 13 files changed, 114 insertions(+), 75 deletions(-) diff --git a/docs/libnm/Makefile.am b/docs/libnm/Makefile.am index 2d1200b485..e04d1722bf 100644 --- a/docs/libnm/Makefile.am +++ b/docs/libnm/Makefile.am @@ -30,9 +30,11 @@ CFILE_GLOB=$(top_srcdir)/libnm-core/*.c $(top_srcdir)/libnm/*.c # Header files to ignore when scanning. IGNORE_HFILES= \ + common.h \ crypto.h \ - nm-dbus-helpers-private.h \ + nm-dbus-helpers.h \ nm-core-internal.h \ + nm-core-types.h \ nm-device-private.h \ nm-dhcp4-config.h \ nm-dhcp6-config.h \ @@ -41,12 +43,13 @@ IGNORE_HFILES= \ nm-manager.h \ nm-object-cache.h \ nm-object-private.h \ - nm-param-spec-dbus.h \ + nm-property-compare.h \ nm-remote-connection-private.h \ nm-remote-settings.h \ nm-setting-private.h \ - nm-types-private.h \ - nm-utils-private.h + nm-types.h \ + nm-utils-private.h \ + nm-vpn-plugin-old.h # Images to copy into HTML directory. HTML_IMAGES = libnm.png diff --git a/docs/libnm/libnm-docs.xml b/docs/libnm/libnm-docs.xml index e722a30964..2bb9f37521 100644 --- a/docs/libnm/libnm-docs.xml +++ b/docs/libnm/libnm-docs.xml @@ -68,11 +68,14 @@ + + Connection and Setting API Reference + @@ -135,6 +138,13 @@ Utility API Reference + + + + + VPN Plugin API Reference + + diff --git a/libnm-core/nm-connection.h b/libnm-core/nm-connection.h index 2f9883c33d..c76ad60768 100644 --- a/libnm-core/nm-connection.h +++ b/libnm-core/nm-connection.h @@ -57,6 +57,13 @@ G_BEGIN_DECLS * client side, and #NMSettingsConnection on the daemon side. */ +/** + * NMConnectionInterface: + * @parent: the parent interace struct + * @secrets_updated: emitted when the connection's secrets are updated + * @secrets_cleared: emitted when the connection's secrets are cleared + * @changed: emitted when any change to the connection's settings occurs + */ typedef struct { GTypeInterface parent; diff --git a/libnm-core/nm-dbus-interface.h b/libnm-core/nm-dbus-interface.h index b4cbd1793e..ccf21ee14a 100644 --- a/libnm-core/nm-dbus-interface.h +++ b/libnm-core/nm-dbus-interface.h @@ -526,8 +526,6 @@ typedef enum { NM_DEVICE_STATE_REASON_MODEM_FAILED = 57, NM_DEVICE_STATE_REASON_MODEM_AVAILABLE = 58, NM_DEVICE_STATE_REASON_SIM_PIN_INCORRECT = 59, - - NM_DEVICE_STATE_REASON_LAST = 0xFFFF } NMDeviceStateReason; diff --git a/libnm-core/nm-errors.h b/libnm-core/nm-errors.h index fa6919c569..a9857445b7 100644 --- a/libnm-core/nm-errors.h +++ b/libnm-core/nm-errors.h @@ -63,21 +63,21 @@ GQuark nm_agent_manager_error_quark (void); * was attempted on a non-secret property * @NM_CONNECTION_ERROR_MISSING_SETTING: the #NMConnection object is missing an * #NMSetting which is required for its configuration. The error message will - * always be prefixed with ": ", where "" is the + * always be prefixed with "<setting-name>: ", where "<setting-name>" is the * name of the setting that is missing. * @NM_CONNECTION_ERROR_INVALID_SETTING: the #NMConnection object contains an * invalid or inappropriate #NMSetting. The error message will always be - * prefixed with ": ", where "" is the name of the + * prefixed with "<setting-name>: ", where "<setting-name>" is the name of the * setting that is invalid. * @NM_CONNECTION_ERROR_MISSING_PROPERTY: the #NMConnection object is invalid * because it is missing a required property. The error message will always be - * prefixed with ".: ", where "" is - * the name of the setting with the missing property, and "" is + * prefixed with "<setting-name>.<property-name>: ", where "<setting-name>" is + * the name of the setting with the missing property, and "<property-name>" is * the property that is missing. * @NM_CONNECTION_ERROR_INVALID_PROPERTY: the #NMConnection object is invalid * because a property has an invalid value. The error message will always be - * prefixed with ".: ", where "" is - * the name of the setting with the invalid property, and "" is + * prefixed with "<setting-name>.<property-name>: ", where "<setting-name>" is + * the name of the setting with the invalid property, and "<property-name>" is * the property that is invalid. * * Describes errors that may result from operations involving a #NMConnection diff --git a/libnm-core/nm-setting-8021x.c b/libnm-core/nm-setting-8021x.c index a1019060d1..6b202dd4ff 100644 --- a/libnm-core/nm-setting-8021x.c +++ b/libnm-core/nm-setting-8021x.c @@ -820,7 +820,7 @@ nm_setting_802_1x_get_client_cert_path (NMSetting8021x *setting) * @setting: the #NMSetting8021x * @cert_path: when @scheme is set to either %NM_SETTING_802_1X_CK_SCHEME_PATH * or %NM_SETTING_802_1X_CK_SCHEME_BLOB, pass the path of the client - * certificate file (PEM, DER, or PKCS#12 format). The path must be UTF-8 + * certificate file (PEM, DER, or PKCS#12 format). The path must be UTF-8 * encoded; use g_filename_to_utf8() to convert if needed. Passing %NULL with * any @scheme clears the client certificate. * @scheme: desired storage scheme for the certificate @@ -1403,7 +1403,7 @@ nm_setting_802_1x_get_phase2_client_cert_path (NMSetting8021x *setting) * @setting: the #NMSetting8021x * @cert_path: when @scheme is set to either %NM_SETTING_802_1X_CK_SCHEME_PATH * or %NM_SETTING_802_1X_CK_SCHEME_BLOB, pass the path of the "phase2" client - * certificate file (PEM, DER, or PKCS#12 format). The path must be UTF-8 + * certificate file (PEM, DER, or PKCS#12 format). The path must be UTF-8 * encoded; use g_filename_to_utf8() to convert if needed. Passing %NULL with * any @scheme clears the "phase2" client certificate. * @scheme: desired storage scheme for the certificate @@ -1687,7 +1687,7 @@ file_to_secure_bytes (const char *filename) * @setting: the #NMSetting8021x * @key_path: when @scheme is set to either %NM_SETTING_802_1X_CK_SCHEME_PATH or * %NM_SETTING_802_1X_CK_SCHEME_BLOB, pass the path of the private key file - * (PEM, DER, or PKCS#12 format). The path must be UTF-8 encoded; use + * (PEM, DER, or PKCS#12 format). The path must be UTF-8 encoded; use * g_filename_to_utf8() to convert if needed. Passing %NULL with any @scheme * clears the private key. * @password: password used to decrypt the private key, or %NULL if the password @@ -1997,7 +1997,7 @@ nm_setting_802_1x_get_phase2_private_key_path (NMSetting8021x *setting) * @setting: the #NMSetting8021x * @key_path: when @scheme is set to either %NM_SETTING_802_1X_CK_SCHEME_PATH or * %NM_SETTING_802_1X_CK_SCHEME_BLOB, pass the path of the "phase2" private - * key file (PEM, DER, or PKCS#12 format). The path must be UTF-8 encoded; + * key file (PEM, DER, or PKCS#12 format). The path must be UTF-8 encoded; * use g_filename_to_utf8() to convert if needed. Passing %NULL with any * @scheme clears the private key. * @password: password used to decrypt the private key, or %NULL if the password @@ -3502,15 +3502,16 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) * should be set to the key's encrypted PEM encoded data. When using private * keys with the path scheme, this property should be set to the full UTF-8 * encoded path of the key, prefixed with the string "file://" and ending - * with a terminating NUL byte. When using PKCS#12 format private keys and - * the blob scheme, this property should be set to the PKCS#12 data and the - * #NMSetting8021x:private-key-password property must be set to password - * used to decrypt the PKCS#12 certificate and key. When using PKCS#12 files - * and the path scheme, this property should be set to the full UTF-8 - * encoded path of the key, prefixed with the string "file://" and and - * ending with a terminating NUL byte, and as with the blob scheme the - * "private-key-password" property must be set to the password used to - * decode the PKCS#12 private key and certificate. + * with a terminating NUL byte. When using PKCS#12 format private + * keys and the blob scheme, this property should be set to the + * PKCS#12 data and the #NMSetting8021x:private-key-password + * property must be set to password used to decrypt the PKCS#12 + * certificate and key. When using PKCS#12 files and the path + * scheme, this property should be set to the full UTF-8 encoded path of the + * key, prefixed with the string "file://" and and ending with a terminating + * NUL byte, and as with the blob scheme the "private-key-password" property + * must be set to the password used to decode the PKCS#12 private + * key and certificate. * * Setting this property directly is discouraged; use the * nm_setting_802_1x_set_private_key() function instead. @@ -3537,7 +3538,7 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) * * The password used to decrypt the private key specified in the * #NMSetting8021x:private-key property when the private key either uses the - * path scheme, or if the private key is a PKCS#12 format key. Setting this + * path scheme, or if the private key is a PKCS#12 format key. Setting this * property directly is not generally necessary except when returning * secrets to NetworkManager; it is generally set automatically when setting * the private key by the nm_setting_802_1x_set_private_key() function. @@ -3576,15 +3577,16 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) * should be set to the key's encrypted PEM encoded data. When using private * keys with the path scheme, this property should be set to the full UTF-8 * encoded path of the key, prefixed with the string "file://" and ending - * with a terminating NUL byte. When using PKCS#12 format private keys and - * the blob scheme, this property should be set to the PKCS#12 data and the - * #NMSetting8021x:phase2-private-key-password property must be set to - * password used to decrypt the PKCS#12 certificate and key. When using - * PKCS#12 files and the path scheme, this property should be set to the - * full UTF-8 encoded path of the key, prefixed with the string "file://" - * and and ending with a terminating NUL byte, and as with the blob scheme - * the #NMSetting8021x:phase2-private-key-password property must be set to - * the password used to decode the PKCS#12 private key and certificate. + * with a terminating NUL byte. When using PKCS#12 format private + * keys and the blob scheme, this property should be set to the + * PKCS#12 data and the #NMSetting8021x:phase2-private-key-password + * property must be set to password used to decrypt the PKCS#12 + * certificate and key. When using PKCS#12 files and the path + * scheme, this property should be set to the full UTF-8 encoded path of the + * key, prefixed with the string "file://" and and ending with a terminating + * NUL byte, and as with the blob scheme the + * #NMSetting8021x:phase2-private-key-password property must be set to the + * password used to decode the PKCS#12 private key and certificate. * * Setting this property directly is discouraged; use the * nm_setting_802_1x_set_phase2_private_key() function instead. @@ -3605,10 +3607,11 @@ nm_setting_802_1x_class_init (NMSetting8021xClass *setting_class) * * The password used to decrypt the "phase 2" private key specified in the * #NMSetting8021x:phase2-private-key property when the private key either - * uses the path scheme, or is a PKCS#12 format key. Setting this property - * directly is not generally necessary except when returning secrets to - * NetworkManager; it is generally set automatically when setting the - * private key by the nm_setting_802_1x_set_phase2_private_key() function. + * uses the path scheme, or is a PKCS#12 format key. Setting this + * property directly is not generally necessary except when returning + * secrets to NetworkManager; it is generally set automatically when setting + * the private key by the nm_setting_802_1x_set_phase2_private_key() + * function. **/ g_object_class_install_property (object_class, PROP_PHASE2_PRIVATE_KEY_PASSWORD, diff --git a/libnm-core/nm-setting-8021x.h b/libnm-core/nm-setting-8021x.h index 10338de998..43885daaa3 100644 --- a/libnm-core/nm-setting-8021x.h +++ b/libnm-core/nm-setting-8021x.h @@ -37,7 +37,7 @@ G_BEGIN_DECLS * @NM_SETTING_802_1X_CK_FORMAT_X509: file contains an X.509 format certificate * @NM_SETTING_802_1X_CK_FORMAT_RAW_KEY: file contains an old-style OpenSSL PEM * or DER private key - * @NM_SETTING_802_1X_CK_FORMAT_PKCS12: file contains a PKCS#12 certificate + * @NM_SETTING_802_1X_CK_FORMAT_PKCS12: file contains a PKCS#12 certificate * and private key * * #NMSetting8021xCKFormat values indicate the general type of a certificate diff --git a/libnm-core/nm-setting-bluetooth.c b/libnm-core/nm-setting-bluetooth.c index 952dd61b51..6b169301ba 100644 --- a/libnm-core/nm-setting-bluetooth.c +++ b/libnm-core/nm-setting-bluetooth.c @@ -79,7 +79,8 @@ NMSetting *nm_setting_bluetooth_new (void) * Returns the connection method for communicating with the remote device (i.e. * either DUN to a DUN-capable device or PANU to a NAP-capable device). * - * Returns: the type, either %NM_SETTING_BLUETOOTH_PANU or %NM_SETTING_BLUETOOTH_DUN + * Returns: the type, either %NM_SETTING_BLUETOOTH_TYPE_PANU or + * %NM_SETTING_BLUETOOTH_TYPE_DUN **/ const char * nm_setting_bluetooth_get_connection_type (NMSettingBluetooth *setting) diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 5356399baa..12ad1f2d50 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -2233,9 +2233,9 @@ nm_utils_rsa_key_encrypt_aes (const guint8 *data, * nm_utils_file_is_pkcs12: * @filename: name of the file to test * - * Utility function to find out if the @filename is in PKCS#12 format. + * Utility function to find out if the @filename is in PKCS#12 format. * - * Returns: %TRUE if the file is PKCS#12, %FALSE if it is not + * Returns: %TRUE if the file is PKCS#12, %FALSE if it is not **/ gboolean nm_utils_file_is_pkcs12 (const char *filename) diff --git a/libnm/nm-device.c b/libnm/nm-device.c index 29f5db8d33..ba5dbacb85 100644 --- a/libnm/nm-device.c +++ b/libnm/nm-device.c @@ -1568,8 +1568,7 @@ get_short_vendor (NMDevice *device) * nm_device_get_description: * @device: an #NMDevice * - * Gets a description of @device, incorporating the results of - * nm_device_get_short_vendor() and nm_device_get_short_product(). + * Gets a description of @device, based on its vendor and product names. * * Returns: a description of @device. If either the vendor or the * product name is unknown, this returns the interface name. diff --git a/libnm/nm-types.h b/libnm/nm-types.h index 2e62ffdea3..3602878a6a 100644 --- a/libnm/nm-types.h +++ b/libnm/nm-types.h @@ -49,8 +49,6 @@ typedef struct _NMObject NMObject; typedef struct _NMRemoteConnection NMRemoteConnection; typedef struct _NMSecretAgent NMSecretAgent; typedef struct _NMVpnConnection NMVpnConnection; -typedef struct _NMVpnEditorPlugin NMVpnEditorPlugin; -typedef struct _NMVpnEditor NMVpnEditor; typedef struct _NMWimaxNsp NMWimaxNsp; #endif /* NM_TYPES_H */ diff --git a/libnm/nm-vpn-editor-plugin.h b/libnm/nm-vpn-editor-plugin.h index 76b365876e..5910aa3653 100644 --- a/libnm/nm-vpn-editor-plugin.h +++ b/libnm/nm-vpn-editor-plugin.h @@ -32,6 +32,9 @@ G_BEGIN_DECLS +typedef struct _NMVpnEditorPlugin NMVpnEditorPlugin; +typedef struct _NMVpnEditor NMVpnEditor; + /* Plugin's factory function that returns a GObject that implements * NMVpnEditorPlugin. */ @@ -75,41 +78,44 @@ typedef enum /*< flags >*/ { /* D-Bus service name of the plugin's VPN service */ #define NM_VPN_EDITOR_PLUGIN_SERVICE "service" +/** + * NMVpnEditorPluginInterface: + * @g_iface: the parent interface + * @get_editor: returns an #NMVpnEditor, pre-filled with values from @connection + * if non-%NULL. + * @get_capabilities: returns a bitmask of capabilities. + * @import_from_file: Try to import a connection from the specified path. On + * success, return a partial #NMConnection object. On error, return %NULL and + * set @error with additional information. Note that @error can be %NULL, in + * which case no additional error information should be provided. + * @export_to_file: Export the given connection to the specified path. Return + * %TRUE on success. On error, return %FALSE and set @error with additional + * error information. Note that @error can be %NULL, in which case no + * additional error information should be provided. + * @get_suggested_filename: For a given connection, return a suggested file + * name. Returned value will be %NULL or a suggested file name to be freed by + * the caller. + * + * Interface for VPN editor plugins. + */ typedef struct { GTypeInterface g_iface; - /* Returns an #NMVpnEditor, pre-filled with values from @connection if - * non-%NULL. - */ NMVpnEditor * (*get_editor) (NMVpnEditorPlugin *plugin, NMConnection *connection, GError **error); - /* Plugin's capabiltity function that returns a bitmask of capabilities. */ NMVpnEditorPluginCapability (*get_capabilities) (NMVpnEditorPlugin *plugin); - /* Try to import a connection from the specified path. On success, return a - * partial #NMConnection object. On error, return %NULL and set @error with - * additional information. Note that @error can be %NULL, in which case no - * additional error information should be provided. - */ NMConnection * (*import_from_file) (NMVpnEditorPlugin *plugin, const char *path, GError **error); - /* Export the given connection to the specified path. Return %TRUE on success. - * On error, return %FALSE and set @error with additional error information. - * Note that @error can be %NULL, in which case no additional error information - * should be provided. - */ gboolean (*export_to_file) (NMVpnEditorPlugin *plugin, const char *path, NMConnection *connection, GError **error); - /* For a given connection, return a suggested file name. Returned value - * will be %NULL or a suggested file name to be freed by the caller. - */ char * (*get_suggested_filename) (NMVpnEditorPlugin *plugin, NMConnection *connection); } NMVpnEditorPluginInterface; @@ -140,28 +146,33 @@ char *nm_vpn_editor_plugin_get_suggested_filename (NMVpnEditorPlugin *pl #define NM_IS_VPN_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_VPN_EDITOR)) #define NM_VPN_EDITOR_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), NM_TYPE_VPN_EDITOR, NMVpnEditorInterface)) +/** + * NMVpnEditorInterface: + * @g_iface: the parent interface + * @get_widget: return the #GtkWidget for the VPN editor's UI + * @placeholder: not currently used + * @update_connection: called to save the user-entered options to the connection + * object. Should return %FALSE and set @error if the current options are + * invalid. @error should contain enough information for the plugin to + * determine which UI widget is invalid at a later point in time. For + * example, creating unique error codes for what error occurred and populating + * the message field of @error with the name of the invalid property. + * @changed: emitted when the value of a UI widget changes. May trigger a + * validity check via @update_connection to write values to the connection. + * + * Interface for editing a specific #NMConnection + */ typedef struct { GTypeInterface g_iface; - /* Return the #GtkWidget for the VPN editor's UI */ GObject * (*get_widget) (NMVpnEditor *editor); void (*placeholder) (void); - /* Called to save the user-entered options to the connection object. Should - * return %FALSE and set @error if the current options are invalid. @error - * should contain enough information for the plugin to determine which UI - * widget is invalid at a later point in time. For example, creating unique - * error codes for what error occurred and populating the message field - * of @error with the name of the invalid property. - */ gboolean (*update_connection) (NMVpnEditor *editor, NMConnection *connection, GError **error); - /* Emitted when the value of a UI widget changes. May trigger a validity - * check via update_connection() to write values to the connection. - */ void (*changed) (NMVpnEditor *editor); } NMVpnEditorInterface; diff --git a/libnm/nm-wimax-nsp.h b/libnm/nm-wimax-nsp.h index a8f9105268..cdc7b23665 100644 --- a/libnm/nm-wimax-nsp.h +++ b/libnm/nm-wimax-nsp.h @@ -41,6 +41,15 @@ G_BEGIN_DECLS #define NM_WIMAX_NSP_SIGNAL_QUALITY "signal-quality" #define NM_WIMAX_NSP_NETWORK_TYPE "network-type" +/** + * NMWimaxNspNetworkType: + * @NM_WIMAX_NSP_NETWORK_TYPE_UNKNOWN: unknown network type + * @NM_WIMAX_NSP_NETWORK_TYPE_HOME: home network + * @NM_WIMAX_NSP_NETWORK_TYPE_PARTNER: partner network + * @NM_WIMAX_NSP_NETWORK_TYPE_ROAMING_PARTNER: roaming partner network + * + * WiMAX network type. + */ typedef enum { NM_WIMAX_NSP_NETWORK_TYPE_UNKNOWN = 0, NM_WIMAX_NSP_NETWORK_TYPE_HOME = 1, -- cgit v1.2.1 From 22762324e841df851219cec0a79bb063824c5dfc Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Thu, 6 Nov 2014 17:51:09 -0600 Subject: libnm,core: enhance nm_utils_hexstr2bin() Make the type return GBytes since most in-tree users want that. Allow the function to accept many more formats as valid hex, including bytes delimited by ':' and a leading '0x'. --- clients/cli/settings.c | 10 +-- libnm-core/nm-utils.c | 103 ++++++++++++------------- libnm-core/nm-utils.h | 5 +- libnm-core/tests/test-general.c | 37 +++++++++ libnm/libnm.ver | 1 - libnm/nm-device.c | 15 +++- src/dhcp-manager/nm-dhcp-utils.c | 41 ++-------- src/settings/plugins/ifcfg-rh/reader.c | 42 ++++------ src/settings/plugins/ifnet/connection_parser.c | 41 +++++----- src/supplicant-manager/nm-supplicant-config.c | 34 +++++--- 10 files changed, 176 insertions(+), 153 deletions(-) diff --git a/clients/cli/settings.c b/clients/cli/settings.c index 2c6be705dc..dcd4f1cc72 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -2343,7 +2343,7 @@ nmc_property_set_byte_array (NMSetting *setting, const char *prop, const char *v char *val_strip; const char *delimiters = " \t,"; long int val_int; - char *bin; + GBytes *bytes; GByteArray *array = NULL; gboolean success = TRUE; @@ -2352,11 +2352,9 @@ nmc_property_set_byte_array (NMSetting *setting, const char *prop, const char *v val_strip = g_strstrip (g_strdup (val)); /* First try hex string in the format of AAbbCCDd */ - bin = nm_utils_hexstr2bin (val_strip, strlen (val_strip)); - if (bin) { - array = g_byte_array_sized_new (strlen (val_strip)/2); - g_byte_array_append (array, (const guint8 *) bin, strlen (val_strip)/2); - g_free (bin); + bytes = nm_utils_hexstr2bin (val_strip); + if (bytes) { + array = g_bytes_unref_to_array (bytes); goto done; } diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 12ad1f2d50..37f9ca0cf4 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -33,6 +33,7 @@ #include "nm-glib-compat.h" #include "nm-setting-private.h" #include "crypto.h" +#include "gsystem-local-alloc.h" #include "nm-setting-bond.h" #include "nm-setting-bridge.h" @@ -2100,7 +2101,7 @@ nm_utils_rsa_key_encrypt_helper (const char *cipher, if (!in_password) { if (!crypto_randomize (pw_buf, sizeof (pw_buf), error)) return NULL; - in_password = tmp_password = nm_utils_bin2hexstr ((const char *) pw_buf, sizeof (pw_buf), -1); + in_password = tmp_password = nm_utils_bin2hexstr (pw_buf, sizeof (pw_buf), -1); } if (g_strcmp0 (cipher, CIPHER_AES_CBC) == 0) @@ -2124,7 +2125,7 @@ nm_utils_rsa_key_encrypt_helper (const char *cipher, g_string_append (pem, "Proc-Type: 4,ENCRYPTED\n"); /* Convert the salt to a hex string */ - tmp = nm_utils_bin2hexstr ((const char *) salt, salt_len, salt_len * 2); + tmp = nm_utils_bin2hexstr (salt, salt_len, salt_len * 2); g_string_append_printf (pem, "DEK-Info: %s,%s\n\n", cipher, tmp); g_free (tmp); @@ -2876,13 +2877,13 @@ _nm_utils_hwaddr_from_dbus (GVariant *dbus_value, /** * nm_utils_bin2hexstr: - * @bytes: an array of bytes + * @src: an array of bytes * @len: the length of the @bytes array * @final_len: an index where to cut off the returned string, or -1 * - * Converts a byte-array @bytes into a hexadecimal string. - * If @final_len is greater than -1, the returned string is terminated at - * that index (returned_string[final_len] == '\0'), + * Converts the byte array @src into a hexadecimal string. If @final_len is + * greater than -1, the returned string is terminated at that index + * (returned_string[final_len] == '\0'), * * Return value: (transfer full): the textual form of @bytes */ @@ -2891,9 +2892,10 @@ _nm_utils_hwaddr_from_dbus (GVariant *dbus_value, * copyright Red Hat, Inc. under terms of the LGPL. */ char * -nm_utils_bin2hexstr (const char *bytes, int len, int final_len) +nm_utils_bin2hexstr (gconstpointer src, gsize len, int final_len) { static char hex_digits[] = "0123456789abcdef"; + const guint8 *bytes = src; char *result; int i; gsize buflen = (len * 2) + 1; @@ -2918,64 +2920,61 @@ nm_utils_bin2hexstr (const char *bytes, int len, int final_len) return result; } -/* From hostap, Copyright (c) 2002-2005, Jouni Malinen */ /** - * nm_utils_hex2byte: - * @hex: a string representing a hex byte + * nm_utils_hexstr2bin: + * @hex: a string of hexadecimal characters with optional ':' separators * - * Converts a hex string (2 characters) into its byte representation. + * Converts a hexadecimal string @hex into an array of bytes. The optional + * separator ':' may be used between single or pairs of hexadecimal characters, + * eg "00:11" or "0:1". Any "0x" at the beginning of @hex is ignored. @hex + * may not start or end with ':'. * - * Return value: a byte, or -1 if @hex doesn't represent a hex byte + * Return value: (transfer full): the converted bytes, or %NULL on error */ -int -nm_utils_hex2byte (const char *hex) +GBytes * +nm_utils_hexstr2bin (const char *hex) { + guint i = 0, x = 0; + gs_free guint8 *c = NULL; int a, b; - a = g_ascii_xdigit_value (*hex++); - if (a < 0) - return -1; - b = g_ascii_xdigit_value (*hex++); - if (b < 0) - return -1; - return (a << 4) | b; -} + gboolean found_colon = FALSE; -/** - * nm_utils_hexstr2bin: - * @hex: an hex string - * @len: the length of the @hex string (it has to be even) - * - * Converts a hexadecimal string @hex into a byte-array. The returned array - * length is @len/2. - * - * Return value: (transfer full): a array of bytes, or %NULL on error - */ -char * -nm_utils_hexstr2bin (const char *hex, size_t len) -{ - size_t i; - int a; - const char * ipos = hex; - char * buf = NULL; - char * opos; + g_return_val_if_fail (hex != NULL, NULL); - /* Length must be a multiple of 2 */ - if ((len % 2) != 0) - return NULL; + if (strncasecmp (hex, "0x", 2) == 0) + hex += 2; + found_colon = !!strchr (hex, ':'); - opos = buf = g_malloc0 ((len / 2) + 1); - for (i = 0; i < len; i += 2) { - a = nm_utils_hex2byte (ipos); - if (a < 0) { - g_free (buf); + c = g_malloc (strlen (hex) / 2 + 1); + for (;;) { + a = g_ascii_xdigit_value (hex[i++]); + if (a < 0) + return NULL; + + if (hex[i] && hex[i] != ':') { + b = g_ascii_xdigit_value (hex[i++]); + if (b < 0) + return NULL; + c[x++] = ((guint) a << 4) | ((guint) b); + } else + c[x++] = (guint) a; + + if (!hex[i]) + break; + if (hex[i] == ':') { + if (!hex[i + 1]) { + /* trailing ':' is invalid */ + return NULL; + } + i++; + } else if (found_colon) { + /* If colons exist, they must delimit 1 or 2 hex chars */ return NULL; } - *opos++ = a; - ipos += 2; } - return buf; + + return g_bytes_new (c, x); } -/* End from hostap */ /** * nm_utils_iface_valid_name: diff --git a/libnm-core/nm-utils.h b/libnm-core/nm-utils.h index 25f09c45ea..a4253927be 100644 --- a/libnm-core/nm-utils.h +++ b/libnm-core/nm-utils.h @@ -167,9 +167,8 @@ gboolean nm_utils_hwaddr_matches (gconstpointer hwaddr1, gconstpointer hwaddr2, gssize hwaddr2_len); -char *nm_utils_bin2hexstr (const char *bytes, int len, int final_len); -int nm_utils_hex2byte (const char *hex); -char *nm_utils_hexstr2bin (const char *hex, size_t len); +char *nm_utils_bin2hexstr (gconstpointer src, gsize len, int final_len); +GBytes *nm_utils_hexstr2bin (const char *hex); gboolean nm_utils_iface_valid_name(const char *name); diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 2c77726654..522c6eb15f 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -3550,6 +3550,41 @@ test_setting_ip6_gateway (void) g_object_unref (conn); } +typedef struct { + const char *str; + const guint8 expected[20]; + const guint expected_len; +} HexItem; + +static void +test_hexstr2bin (void) +{ + static const HexItem items[] = { + { "aaBBCCddDD10496a", { 0xaa, 0xbb, 0xcc, 0xdd, 0xdd, 0x10, 0x49, 0x6a }, 8 }, + { "aa:bb:cc:dd:10:49:6a", { 0xaa, 0xbb, 0xcc, 0xdd, 0x10, 0x49, 0x6a }, 7 }, + { "0xccddeeff", { 0xcc, 0xdd, 0xee, 0xff }, 4 }, + { "1:2:66:77:80", { 0x01, 0x02, 0x66, 0x77, 0x80 }, 5 }, + { "e", { 0x0e }, 1 }, + { "aabb1199:" }, + { ":aabb1199" }, + { "aabb$$dd" }, + { "aab:ccc:ddd" }, + { "aab::ccc:ddd" }, + }; + GBytes *b; + guint i; + + for (i = 0; i < G_N_ELEMENTS (items); i++) { + b = nm_utils_hexstr2bin (items[i].str); + if (items[i].expected_len) { + g_assert (b); + g_assert_cmpint (g_bytes_get_size (b), ==, items[i].expected_len); + g_assert (memcmp (g_bytes_get_data (b, NULL), items[i].expected, g_bytes_get_size (b)) == 0); + } else + g_assert (b == NULL); + } +} + NMTST_DEFINE (); int main (int argc, char **argv) @@ -3641,6 +3676,8 @@ int main (int argc, char **argv) g_test_add_func ("/core/general/test_setting_ip4_gateway", test_setting_ip4_gateway); g_test_add_func ("/core/general/test_setting_ip6_gateway", test_setting_ip6_gateway); + g_test_add_func ("/core/general/hexstr2bin", test_hexstr2bin); + return g_test_run (); } diff --git a/libnm/libnm.ver b/libnm/libnm.ver index baf83ca548..d1957ececc 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -756,7 +756,6 @@ global: nm_utils_deinit; nm_utils_escape_ssid; nm_utils_file_is_pkcs12; - nm_utils_hex2byte; nm_utils_hexstr2bin; nm_utils_hwaddr_atoba; nm_utils_hwaddr_aton; diff --git a/libnm/nm-device.c b/libnm/nm-device.c index ba5dbacb85..a6d5fe3067 100644 --- a/libnm/nm-device.c +++ b/libnm/nm-device.c @@ -1249,6 +1249,19 @@ nm_device_get_available_connections (NMDevice *device) return NM_DEVICE_GET_PRIVATE (device)->available_connections; } +static inline guint8 +hex2byte (const char *hex) +{ + int a, b; + a = g_ascii_xdigit_value (*hex++); + if (a < 0) + return -1; + b = g_ascii_xdigit_value (*hex++); + if (b < 0) + return -1; + return (a << 4) | b; +} + static char * get_decoded_property (GUdevDevice *device, const char *property) { @@ -1264,7 +1277,7 @@ get_decoded_property (GUdevDevice *device, const char *property) n = unescaped = g_malloc0 (len + 1); while (*p) { if ((len >= 4) && (*p == '\\') && (*(p+1) == 'x')) { - *n++ = (char) nm_utils_hex2byte (p + 2); + *n++ = (char) hex2byte (p + 2); p += 4; len -= 4; } else { diff --git a/src/dhcp-manager/nm-dhcp-utils.c b/src/dhcp-manager/nm-dhcp-utils.c index d9eb85df86..4b8c44a452 100644 --- a/src/dhcp-manager/nm-dhcp-utils.c +++ b/src/dhcp-manager/nm-dhcp-utils.c @@ -709,46 +709,17 @@ GBytes * nm_dhcp_utils_client_id_string_to_bytes (const char *client_id) { GBytes *bytes = NULL; - guint i = 0, x = 0; guint len; char *c; - int a; g_return_val_if_fail (client_id && client_id[0], NULL); - /* Accept a binary client ID in hex digits with the ':' delimiter, - * otherwise treat it as a string. - */ - len = strlen (client_id); - c = g_malloc0 (len / 2 + 1); - while (client_id[i]) { - a = g_ascii_xdigit_value (client_id[i++]); - if (a >= 0) { - if (client_id[i] != ':') { - c[x] = ((guint8) a << 4); - a = g_ascii_xdigit_value (client_id[i++]); - } - if (a >= 0) - c[x++] |= (guint8) a; - } - if (client_id[i]) { - if (client_id[i] != ':' || !client_id[i + 1]) { - /* missing or trailing ':' is invalid for hex-format */ - a = -1; - } - i++; - } - - if (a < 0) { - g_clear_pointer (&c, g_free); - break; - } - } - - if (c) { - g_assert (x > 0); - bytes = g_bytes_new_take (c, x); - } else { + /* Try as hex encoded */ + if (strchr (client_id, ':')) + bytes = nm_utils_hexstr2bin (client_id); + if (!bytes) { + /* Fall back to string */ + len = strlen (client_id); c = g_malloc (len + 1); c[0] = 0; /* type: non-hardware address per RFC 2132 section 9.14 */ memcpy (c + 1, client_id, len); diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index 5243d77f18..f97c84c782 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -3224,7 +3224,6 @@ make_wireless_setting (shvarFile *ifcfg, GError **error) { NMSettingWireless *s_wireless; - GBytes *bytes = NULL; char *value = NULL; gint64 chan = 0; @@ -3256,19 +3255,19 @@ make_wireless_setting (shvarFile *ifcfg, value = svGetValue (ifcfg, "ESSID", TRUE); if (value) { - gsize ssid_len = 0, value_len = strlen (value); - char *p = value, *tmp; - char buf[33]; + GBytes *bytes = NULL; + gsize ssid_len = 0; + gsize value_len = strlen (value); - ssid_len = value_len; if ( (value_len >= 2) && (value[0] == '"') && (value[value_len - 1] == '"')) { /* Strip the quotes and unescape */ - p = value + 1; + char *p = value + 1; + value[value_len - 1] = '\0'; svUnescape (p); - ssid_len = strlen (p); + bytes = g_bytes_new (p, strlen (p)); } else if ((value_len > 2) && (strncmp (value, "0x", 2) == 0)) { /* Hex representation */ if (value_len % 2) { @@ -3279,34 +3278,27 @@ make_wireless_setting (shvarFile *ifcfg, goto error; } - p = value + 2; - while (*p) { - if (!g_ascii_isxdigit (*p)) { - g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid SSID '%s' character (looks like hex SSID but '%c' isn't a hex digit)", - value, *p); - g_free (value); - goto error; - } - p++; + bytes = nm_utils_hexstr2bin (value); + if (!bytes) { + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, + "Invalid SSID '%s' (looks like hex SSID but isn't)", + value); + g_free (value); + goto error; } + } else + bytes = g_bytes_new (value, value_len); - tmp = nm_utils_hexstr2bin (value + 2, value_len - 2); - ssid_len = (value_len - 2) / 2; - memcpy (buf, tmp, ssid_len); - p = &buf[0]; - g_free (tmp); - } - + ssid_len = g_bytes_get_size (bytes); if (ssid_len > 32 || ssid_len == 0) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid SSID '%s' (size %zu not between 1 and 32 inclusive)", value, ssid_len); + g_bytes_unref (bytes); g_free (value); goto error; } - bytes = g_bytes_new (p, ssid_len); g_object_set (s_wireless, NM_SETTING_WIRELESS_SSID, bytes, NULL); g_bytes_unref (bytes); g_free (value); diff --git a/src/settings/plugins/ifnet/connection_parser.c b/src/settings/plugins/ifnet/connection_parser.c index 5ffc0605e1..651cd8ea71 100644 --- a/src/settings/plugins/ifnet/connection_parser.c +++ b/src/settings/plugins/ifnet/connection_parser.c @@ -43,8 +43,16 @@ connection_id_from_ifnet_name (const char *conn_name) int name_len = strlen (conn_name); /* Convert a hex-encoded conn_name (only used for wifi SSIDs) to human-readable one */ - if ((name_len > 2) && (g_str_has_prefix (conn_name, "0x"))) - return nm_utils_hexstr2bin (conn_name + 2, name_len - 2); + if ((name_len > 2) && (g_str_has_prefix (conn_name, "0x"))) { + GBytes *bytes = nm_utils_hexstr2bin (conn_name); + char *buf; + + if (bytes) { + buf = g_strndup (g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes)); + g_bytes_unref (bytes); + return buf; + } + } return g_strdup (conn_name); } @@ -882,7 +890,6 @@ make_wireless_connection_setting (const char *conn_name, NMSetting8021x **s_8021x, GError **error) { - GBytes *bytes; const char *mac = NULL; NMSettingWireless *wireless_setting = NULL; gboolean adhoc = FALSE; @@ -913,9 +920,8 @@ make_wireless_connection_setting (const char *conn_name, /* handle ssid (hex and ascii) */ if (conn_name) { + GBytes *bytes; gsize ssid_len = 0, value_len = strlen (conn_name); - const char *p; - char *tmp, *converted = NULL; ssid_len = value_len; if ((value_len > 2) && (g_str_has_prefix (conn_name, "0x"))) { @@ -926,32 +932,27 @@ make_wireless_connection_setting (const char *conn_name, conn_name); goto error; } - // ignore "0x" - p = conn_name + 2; - if (!is_hex (p)) { + + bytes = nm_utils_hexstr2bin (conn_name); + if (!bytes) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, - "Invalid SSID '%s' character (looks like hex SSID but '%c' isn't a hex digit)", - conn_name, *p); + "Invalid SSID '%s' (looks like hex SSID but isn't)", + conn_name); goto error; - } - tmp = nm_utils_hexstr2bin (p, value_len - 2); - ssid_len = (value_len - 2) / 2; - converted = g_malloc0 (ssid_len + 1); - memcpy (converted, tmp, ssid_len); - g_free (tmp); - } + } else + bytes = g_bytes_new (conn_name, value_len); + ssid_len = g_bytes_get_size (bytes); if (ssid_len > 32 || ssid_len == 0) { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Invalid SSID '%s' (size %zu not between 1 and 32 inclusive)", conn_name, ssid_len); goto error; } - bytes = g_bytes_new (converted ? converted : conn_name, ssid_len); + g_object_set (wireless_setting, NM_SETTING_WIRELESS_SSID, bytes, NULL); g_bytes_unref (bytes); - g_free (converted); } else { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, "Missing SSID"); @@ -1095,7 +1096,7 @@ add_one_wep_key (const char *ssid, } - converted = nm_utils_bin2hexstr (tmp, strlen (tmp), strlen (tmp) * 2); + converted = nm_utils_bin2hexstr (tmp, strlen (tmp), -1); g_free (tmp); } else { g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, diff --git a/src/supplicant-manager/nm-supplicant-config.c b/src/supplicant-manager/nm-supplicant-config.c index f4ee4c3a2c..2b857517a3 100644 --- a/src/supplicant-manager/nm-supplicant-config.c +++ b/src/supplicant-manager/nm-supplicant-config.c @@ -542,8 +542,8 @@ add_wep_key (NMSupplicantConfig *self, const char *name, NMWepKeyType wep_type) { - char *value; - gboolean success; + GBytes *bytes; + gboolean success = FALSE; size_t key_len = key ? strlen (key) : 0; if (!key || !key_len) @@ -552,9 +552,15 @@ add_wep_key (NMSupplicantConfig *self, if ( (wep_type == NM_WEP_KEY_TYPE_UNKNOWN) || (wep_type == NM_WEP_KEY_TYPE_KEY)) { if ((key_len == 10) || (key_len == 26)) { - value = nm_utils_hexstr2bin (key, strlen (key)); - success = nm_supplicant_config_add_option (self, name, value, key_len / 2, TRUE); - g_free (value); + bytes = nm_utils_hexstr2bin (key); + if (bytes) { + success = nm_supplicant_config_add_option (self, + name, + g_bytes_get_data (bytes, NULL), + g_bytes_get_size (bytes), + TRUE); + g_bytes_unref (bytes); + } if (!success) { nm_log_warn (LOGD_SUPPLICANT, "Error adding %s to supplicant config.", name); return FALSE; @@ -590,8 +596,7 @@ nm_supplicant_config_add_setting_wireless_security (NMSupplicantConfig *self, NMSetting8021x *setting_8021x, const char *con_uuid) { - char *value; - gboolean success; + gboolean success = FALSE; const char *key_mgmt, *auth_alg; const char *psk; @@ -612,10 +617,18 @@ nm_supplicant_config_add_setting_wireless_security (NMSupplicantConfig *self, size_t psk_len = strlen (psk); if (psk_len == 64) { + GBytes *bytes; + /* Hex PSK */ - value = nm_utils_hexstr2bin (psk, psk_len); - success = nm_supplicant_config_add_option (self, "psk", value, psk_len / 2, TRUE); - g_free (value); + bytes = nm_utils_hexstr2bin (psk); + if (bytes) { + success = nm_supplicant_config_add_option (self, + "psk", + g_bytes_get_data (bytes, NULL), + g_bytes_get_size (bytes), + TRUE); + g_bytes_unref (bytes); + } if (!success) { nm_log_warn (LOGD_SUPPLICANT, "Error adding 'psk' to supplicant config."); return FALSE; @@ -653,6 +666,7 @@ nm_supplicant_config_add_setting_wireless_security (NMSupplicantConfig *self, const char *wep1 = nm_setting_wireless_security_get_wep_key (setting, 1); const char *wep2 = nm_setting_wireless_security_get_wep_key (setting, 2); const char *wep3 = nm_setting_wireless_security_get_wep_key (setting, 3); + char *value; if (!add_wep_key (self, wep0, "wep_key0", wep_type)) return FALSE; -- cgit v1.2.1 From ee255036362043c9b8fcf48a9f41abc2e2dd122d Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 29 Oct 2014 09:36:47 -0500 Subject: core: split signal/pidfile/option handling into separate source file We'll use this from more than one spot. --- po/POTFILES.in | 1 + src/Makefile.am | 2 + src/main-utils.c | 281 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main-utils.h | 39 ++++++++ src/main.c | 238 +++------------------------------------------- 5 files changed, 335 insertions(+), 226 deletions(-) create mode 100644 src/main-utils.c create mode 100644 src/main-utils.h diff --git a/po/POTFILES.in b/po/POTFILES.in index 5c44103240..b964b93f0e 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -126,6 +126,7 @@ libnm/nm-vpn-plugin-old.c policy/org.freedesktop.NetworkManager.policy.in.in src/NetworkManagerUtils.c src/main.c +src/main-utils.c src/dhcp-manager/nm-dhcp-dhclient.c src/dhcp-manager/nm-dhcp-dhclient-utils.c src/dhcp-manager/nm-dhcp-manager.c diff --git a/src/Makefile.am b/src/Makefile.am index a0447bdd42..5735a3f6f0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -124,6 +124,8 @@ sbin_PROGRAMS = NetworkManager NetworkManager_SOURCES = \ $(nm_device_sources) $(nm_device_headers) \ + main-utils.c \ + main-utils.h \ main.c NetworkManager_LDADD = libNetworkManager.la diff --git a/src/main-utils.c b/src/main-utils.c new file mode 100644 index 0000000000..55da4dfe41 --- /dev/null +++ b/src/main-utils.c @@ -0,0 +1,281 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2004 - 2012 Red Hat, Inc. + * Copyright (C) 2005 - 2008 Novell, Inc. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "main-utils.h" +#include "nm-posix-signals.h" +#include "nm-logging.h" + +static sigset_t signal_set; +static gboolean *quit_early = NULL; + +/* + * Thread function waiting for signals and processing them. + * Wait for signals in signal set. The semantics of sigwait() require that all + * threads (including the thread calling sigwait()) have the signal masked, for + * reliable operation. Otherwise, a signal that arrives while this thread is + * not blocked in sigwait() might be delivered to another thread. + */ +static void * +signal_handling_thread (void *arg) +{ + GMainLoop *main_loop = arg; + int signo; + + while (1) { + sigwait (&signal_set, &signo); + + switch (signo) { + case SIGINT: + case SIGTERM: + nm_log_info (LOGD_CORE, "caught signal %d, shutting down normally.", signo); + *quit_early = TRUE; /* for quitting before entering the main loop */ + g_main_loop_quit (main_loop); + break; + case SIGHUP: + /* Reread config stuff like system config files, VPN service files, etc */ + nm_log_info (LOGD_CORE, "caught signal %d, not supported yet.", signo); + break; + case SIGPIPE: + /* silently ignore signal */ + break; + default: + nm_log_err (LOGD_CORE, "caught unexpected signal %d", signo); + break; + } + } + return NULL; +} + +/** + * nm_main_utils_setup_signals: + * @main_loop: the #GMainLoop to quit when SIGINT or SIGTERM is received + * @quit_early: location of a variable that will be set to TRUE when + * SIGINT or SIGTERM is received + * + * Mask the signals we are interested in and create a signal handling thread. + * Because all threads inherit the signal mask from their creator, all threads + * in the process will have the signals masked. That's why setup_signals() has + * to be called before creating other threads. + * + * Returns: %TRUE on success + */ +gboolean +nm_main_utils_setup_signals (GMainLoop *main_loop, gboolean *quit_early_ptr) +{ + pthread_t signal_thread_id; + sigset_t old_sig_mask; + int status; + + g_return_val_if_fail (main_loop != NULL, FALSE); + g_return_val_if_fail (quit_early_ptr != NULL, FALSE); + + quit_early = quit_early_ptr; + + sigemptyset (&signal_set); + sigaddset (&signal_set, SIGHUP); + sigaddset (&signal_set, SIGINT); + sigaddset (&signal_set, SIGTERM); + sigaddset (&signal_set, SIGPIPE); + + /* Block all signals of interest. */ + status = pthread_sigmask (SIG_BLOCK, &signal_set, &old_sig_mask); + if (status != 0) { + fprintf (stderr, _("Failed to set signal mask: %d"), status); + return FALSE; + } + /* Save original mask so that we could use it for child processes. */ + nm_save_original_signal_mask (old_sig_mask); + + /* Create the signal handling thread. */ + status = pthread_create (&signal_thread_id, NULL, signal_handling_thread, main_loop); + if (status != 0) { + fprintf (stderr, _("Failed to create signal handling thread: %d"), status); + return FALSE; + } + + return TRUE; +} + +gboolean +nm_main_utils_write_pidfile (const char *pidfile) +{ + char pid[16]; + int fd; + gboolean success = FALSE; + + if ((fd = open (pidfile, O_CREAT|O_WRONLY|O_TRUNC, 00644)) < 0) { + fprintf (stderr, _("Opening %s failed: %s\n"), pidfile, strerror (errno)); + return FALSE; + } + + g_snprintf (pid, sizeof (pid), "%d", getpid ()); + if (write (fd, pid, strlen (pid)) < 0) + fprintf (stderr, _("Writing to %s failed: %s\n"), pidfile, strerror (errno)); + else + success = TRUE; + + if (close (fd)) + fprintf (stderr, _("Closing %s failed: %s\n"), pidfile, strerror (errno)); + + return success; +} + +/** + * nm_main_utils_check_pidfile: + * @pidfile: the pid file + * @name: the process name + * + * Checks whether the pidfile already exists and contains PID of a running + * process. + * + * Returns: %TRUE if the specified pidfile already exists and contains the PID + * of a running process named @name, or %FALSE if not + */ +gboolean +nm_main_utils_check_pidfile (const char *pidfile, const char *name) +{ + char *contents = NULL; + gsize len = 0; + glong pid; + char *proc_cmdline = NULL; + gboolean nm_running = FALSE; + const char *process_name; + + /* Setup runtime directory */ + if (g_mkdir_with_parents (NMRUNDIR, 0755) != 0) { + nm_log_err (LOGD_CORE, "Cannot create '%s': %s", NMRUNDIR, strerror (errno)); + exit (1); + } + + if (!g_file_get_contents (pidfile, &contents, &len, NULL)) + return FALSE; + + if (len <= 0) + goto done; + + errno = 0; + pid = strtol (contents, NULL, 10); + if (pid <= 0 || pid > 65536 || errno) + goto done; + + g_free (contents); + proc_cmdline = g_strdup_printf ("/proc/%ld/cmdline", pid); + if (!g_file_get_contents (proc_cmdline, &contents, &len, NULL)) + goto done; + + process_name = strrchr (contents, '/'); + if (process_name) + process_name++; + else + process_name = contents; + if (strcmp (process_name, name) == 0) { + /* Check that the process exists */ + if (kill (pid, 0) == 0) { + fprintf (stderr, _("%s is already running (pid %ld)\n"), name, pid); + nm_running = TRUE; + } + } + +done: + g_free (proc_cmdline); + g_free (contents); + return nm_running; +} + +gboolean +nm_main_utils_early_setup (const char *progname, + char **argv[], + int *argc, + GOptionEntry *options, + GOptionEntry *more_options, + const char *summary) +{ + GOptionContext *opt_ctx = NULL; + GError *error = NULL; + gboolean success = FALSE; + int i; + + /* Make GIO ignore the remote VFS service; otherwise it tries to use the + * session bus to contact the remote service, and NM shouldn't ever be + * talking on the session bus. See rh #588745 + */ + setenv ("GIO_USE_VFS", "local", 1); + + /* + * Set the umask to 0022, which results in 0666 & ~0022 = 0644. + * Otherwise, if root (or an su'ing user) has a wacky umask, we could + * write out an unreadable resolv.conf. + */ + umask (022); + + /* Ensure gettext() gets the right environment (bgo #666516) */ + setlocale (LC_ALL, ""); + + bindtextdomain (GETTEXT_PACKAGE, NMLOCALEDIR); + bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); + textdomain (GETTEXT_PACKAGE); + + if (getuid () != 0) { + fprintf (stderr, _("You must be root to run %s!\n"), progname); + exit (1); + } + + for (i = 0; options[i].long_name; i++) { + if (!strcmp (options[i].long_name, "log-level")) + options[i].description = g_strdup_printf (options[i].description, nm_logging_all_levels_to_string ()); + else if (!strcmp (options[i].long_name, "log-domains")) + options[i].description = g_strdup_printf (options[i].description, nm_logging_all_domains_to_string ()); + } + + /* Parse options */ + opt_ctx = g_option_context_new (NULL); + g_option_context_set_translation_domain (opt_ctx, GETTEXT_PACKAGE); + g_option_context_set_ignore_unknown_options (opt_ctx, FALSE); + g_option_context_set_help_enabled (opt_ctx, TRUE); + g_option_context_add_main_entries (opt_ctx, options, NULL); + if (more_options) + g_option_context_add_main_entries (opt_ctx, more_options, NULL); + g_option_context_set_summary (opt_ctx, summary); + + success = g_option_context_parse (opt_ctx, argc, argv, &error); + if (!success) { + fprintf (stderr, _("%s. Please use --help to see a list of valid options.\n"), + error->message); + g_clear_error (&error); + } + g_option_context_free (opt_ctx); + + return success; +} + diff --git a/src/main-utils.h b/src/main-utils.h new file mode 100644 index 0000000000..472fa5e70e --- /dev/null +++ b/src/main-utils.h @@ -0,0 +1,39 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2014 Red Hat, Inc. + */ + +#ifndef __MAIN_UTILS_H__ +#define __MAIN_UTILS_H__ + +#include + +gboolean nm_main_utils_setup_signals (GMainLoop *main_loop, gboolean *quit_early_ptr); + +gboolean nm_main_utils_write_pidfile (const char *pidfile); + +gboolean nm_main_utils_check_pidfile (const char *pidfile, const char *name); + +gboolean nm_main_utils_early_setup (const char *progname, + char **argv[], + int *argc, + GOptionEntry *options, + GOptionEntry *more_options, + const char *summary); + +#endif /* __MAIN_UTILS_H__ */ diff --git a/src/main.c b/src/main.c index f4f693d1e4..29cfd4bfd8 100644 --- a/src/main.c +++ b/src/main.c @@ -42,6 +42,7 @@ #include "gsystem-local-alloc.h" #include "nm-dbus-interface.h" #include "NetworkManagerUtils.h" +#include "main-utils.h" #include "nm-manager.h" #include "nm-linux-platform.h" #include "nm-dns-manager.h" @@ -65,161 +66,7 @@ #define NM_DEFAULT_PID_FILE NMRUNDIR "/NetworkManager.pid" #define NM_DEFAULT_SYSTEM_STATE_FILE NMSTATEDIR "/NetworkManager.state" -/* - * Globals - */ static GMainLoop *main_loop = NULL; -static gboolean quit_early = FALSE; -static sigset_t signal_set; - -void *signal_handling_thread (void *arg); -/* - * Thread function waiting for signals and processing them. - * Wait for signals in signal set. The semantics of sigwait() require that all - * threads (including the thread calling sigwait()) have the signal masked, for - * reliable operation. Otherwise, a signal that arrives while this thread is - * not blocked in sigwait() might be delivered to another thread. - */ -void * -signal_handling_thread (void *arg) -{ - int signo; - - while (1) { - sigwait (&signal_set, &signo); - - switch (signo) { - case SIGINT: - case SIGTERM: - nm_log_info (LOGD_CORE, "caught signal %d, shutting down normally.", signo); - quit_early = TRUE; /* for quitting before entering the main loop */ - g_main_loop_quit (main_loop); - break; - case SIGHUP: - /* Reread config stuff like system config files, VPN service files, etc */ - nm_log_info (LOGD_CORE, "caught signal %d, not supported yet.", signo); - break; - case SIGPIPE: - /* silently ignore signal */ - break; - default: - nm_log_err (LOGD_CORE, "caught unexpected signal %d", signo); - break; - } - } - return NULL; -} - -/* - * Mask the signals we are interested in and create a signal handling thread. - * Because all threads inherit the signal mask from their creator, all threads - * in the process will have the signals masked. That's why setup_signals() has - * to be called before creating other threads. - */ -static gboolean -setup_signals (void) -{ - pthread_t signal_thread_id; - sigset_t old_sig_mask; - int status; - - sigemptyset (&signal_set); - sigaddset (&signal_set, SIGHUP); - sigaddset (&signal_set, SIGINT); - sigaddset (&signal_set, SIGTERM); - sigaddset (&signal_set, SIGPIPE); - - /* Block all signals of interest. */ - status = pthread_sigmask (SIG_BLOCK, &signal_set, &old_sig_mask); - if (status != 0) { - fprintf (stderr, _("Failed to set signal mask: %d"), status); - return FALSE; - } - /* Save original mask so that we could use it for child processes. */ - nm_save_original_signal_mask (old_sig_mask); - - /* Create the signal handling thread. */ - status = pthread_create (&signal_thread_id, NULL, signal_handling_thread, NULL); - if (status != 0) { - fprintf (stderr, _("Failed to create signal handling thread: %d"), status); - return FALSE; - } - - return TRUE; -} - -static gboolean -write_pidfile (const char *pidfile) -{ - char pid[16]; - int fd; - gboolean success = FALSE; - - if ((fd = open (pidfile, O_CREAT|O_WRONLY|O_TRUNC, 00644)) < 0) { - fprintf (stderr, _("Opening %s failed: %s\n"), pidfile, strerror (errno)); - return FALSE; - } - - g_snprintf (pid, sizeof (pid), "%d", getpid ()); - if (write (fd, pid, strlen (pid)) < 0) - fprintf (stderr, _("Writing to %s failed: %s\n"), pidfile, strerror (errno)); - else - success = TRUE; - - if (close (fd)) - fprintf (stderr, _("Closing %s failed: %s\n"), pidfile, strerror (errno)); - - return success; -} - -/* Check whether the pidfile already exists and contains PID of a running NetworkManager - * Returns: FALSE - specified pidfile doesn't exist or doesn't contain PID of a running NM process - * TRUE - specified pidfile already exists and contains PID of a running NM process - */ -static gboolean -check_pidfile (const char *pidfile) -{ - char *contents = NULL; - gsize len = 0; - glong pid; - char *proc_cmdline = NULL; - gboolean nm_running = FALSE; - const char *process_name; - - if (!g_file_get_contents (pidfile, &contents, &len, NULL)) - return FALSE; - - if (len <= 0) - goto done; - - errno = 0; - pid = strtol (contents, NULL, 10); - if (pid <= 0 || pid > 65536 || errno) - goto done; - - g_free (contents); - proc_cmdline = g_strdup_printf ("/proc/%ld/cmdline", pid); - if (!g_file_get_contents (proc_cmdline, &contents, &len, NULL)) - goto done; - - process_name = strrchr (contents, '/'); - if (process_name) - process_name++; - else - process_name = contents; - if (strcmp (process_name, "NetworkManager") == 0) { - /* Check that the process exists */ - if (kill (pid, 0) == 0) { - fprintf (stderr, _("NetworkManager is already running (pid %ld)\n"), pid); - nm_running = TRUE; - } - } - -done: - g_free (proc_cmdline); - g_free (contents); - return nm_running; -} static gboolean parse_state_file (const char *filename, @@ -337,7 +184,6 @@ _init_nm_debug (const char *debug) int main (int argc, char *argv[]) { - GOptionContext *opt_ctx = NULL; char *opt_log_level = NULL; char *opt_log_domains = NULL; gboolean become_daemon = TRUE, run_from_build_dir = FALSE; @@ -347,7 +193,6 @@ main (int argc, char *argv[]) gs_free char *state_file = NULL; gboolean wifi_enabled = TRUE, net_enabled = TRUE, wwan_enabled = TRUE, wimax_enabled = TRUE; gboolean success, show_version = FALSE; - int i; NMManager *manager = NULL; gs_unref_object NMVpnManager *vpn_manager = NULL; gs_unref_object NMDnsManager *dns_mgr = NULL; @@ -361,6 +206,7 @@ main (int argc, char *argv[]) GError *error = NULL; gboolean wrote_pidfile = FALSE; char *bad_domains = NULL; + gboolean quit_early = FALSE; GOptionEntry options[] = { { "version", 'V', 0, G_OPTION_ARG_NONE, &show_version, N_("Print NetworkManager version and exit"), NULL }, @@ -377,66 +223,15 @@ main (int argc, char *argv[]) {NULL} }; - /* Make GIO ignore the remote VFS service; otherwise it tries to use the - * session bus to contact the remote service, and NM shouldn't ever be - * talking on the session bus. See rh #588745 - */ - setenv ("GIO_USE_VFS", "local", 1); - - /* - * Set the umask to 0022, which results in 0666 & ~0022 = 0644. - * Otherwise, if root (or an su'ing user) has a wacky umask, we could - * write out an unreadable resolv.conf. - */ - umask (022); - - /* Set locale to be able to use environment variables */ - setlocale (LC_ALL, ""); - - bindtextdomain (GETTEXT_PACKAGE, NMLOCALEDIR); - bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); - textdomain (GETTEXT_PACKAGE); - - if (!g_module_supported ()) { - fprintf (stderr, _("GModules are not supported on your platform!\n")); - exit (1); - } - - if (getuid () != 0) { - fprintf (stderr, _("You must be root to run NetworkManager!\n")); - exit (1); - } - - for (i = 0; options[i].long_name; i++) { - if (!strcmp (options[i].long_name, "log-level")) { - options[i].description = g_strdup_printf (options[i].description, - nm_logging_all_levels_to_string ()); - } else if (!strcmp (options[i].long_name, "log-domains")) { - options[i].description = g_strdup_printf (options[i].description, - nm_logging_all_domains_to_string ()); - } - } - - /* Parse options */ - opt_ctx = g_option_context_new (NULL); - g_option_context_set_translation_domain (opt_ctx, GETTEXT_PACKAGE); - g_option_context_set_ignore_unknown_options (opt_ctx, FALSE); - g_option_context_set_help_enabled (opt_ctx, TRUE); - g_option_context_add_main_entries (opt_ctx, options, NULL); - g_option_context_add_main_entries (opt_ctx, nm_config_get_options (), NULL); - - g_option_context_set_summary (opt_ctx, - _("NetworkManager monitors all network connections and automatically\nchooses the best connection to use. It also allows the user to\nspecify wireless access points which wireless cards in the computer\nshould associate with.")); - - success = g_option_context_parse (opt_ctx, &argc, &argv, &error); - g_option_context_free (opt_ctx); + main_loop = g_main_loop_new (NULL, FALSE); - if (!success) { - fprintf (stderr, _("%s. Please use --help to see a list of valid options.\n"), - error->message); - g_clear_error (&error); + if (!nm_main_utils_early_setup ("NetworkManager", + &argv, + &argc, + options, + nm_config_get_options (), + _("NetworkManager monitors all network connections and automatically\nchooses the best connection to use. It also allows the user to\nspecify wireless access points which wireless cards in the computer\nshould associate with."))) exit (1); - } if (show_version) { fprintf (stdout, NM_DIST_VERSION "\n"); @@ -482,12 +277,6 @@ main (int argc, char *argv[]) g_free (path); } - /* Setup runtime directory */ - if (g_mkdir_with_parents (NMRUNDIR, 0755) != 0) { - nm_log_err (LOGD_CORE, "Cannot create '%s': %s", NMRUNDIR, strerror (errno)); - exit (1); - } - /* Ensure state directory exists */ if (g_mkdir_with_parents (NMSTATEDIR, 0755) != 0) { nm_log_err (LOGD_CORE, "Cannot create '%s': %s", NMSTATEDIR, strerror (errno)); @@ -498,7 +287,7 @@ main (int argc, char *argv[]) state_file = state_file ? state_file : g_strdup (NM_DEFAULT_SYSTEM_STATE_FILE); /* check pid file */ - if (check_pidfile (pidfile)) + if (nm_main_utils_check_pidfile (pidfile, "NetworkManager")) exit (1); /* Read the config file and CLI overrides */ @@ -549,14 +338,13 @@ main (int argc, char *argv[]) saved_errno); exit (1); } - if (write_pidfile (pidfile)) - wrote_pidfile = TRUE; + wrote_pidfile = nm_main_utils_write_pidfile (pidfile); } _init_nm_debug (nm_config_get_debug (config)); /* Set up unix signal handling - before creating threads, but after daemonizing! */ - if (!setup_signals ()) + if (!nm_main_utils_setup_signals (main_loop, &quit_early)) exit (1); if (g_fatal_warnings) { @@ -592,8 +380,6 @@ main (int argc, char *argv[]) #endif ); - main_loop = g_main_loop_new (NULL, FALSE); - /* Set up platform interaction layer */ nm_linux_platform_setup (); -- cgit v1.2.1 From 0b98dc4387c52616aa39979ce61848e6ca8b06b7 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 29 Oct 2014 15:04:38 -0500 Subject: dhcp: move D-Bus DHCP listener into separate class This simplifies the manager and ensures that only the clients that use D-Bus-based DHCP helpers need to care about them. --- src/Makefile.am | 2 + src/dhcp-manager/nm-dhcp-client.c | 27 +++- src/dhcp-manager/nm-dhcp-client.h | 11 +- src/dhcp-manager/nm-dhcp-dhclient.c | 10 ++ src/dhcp-manager/nm-dhcp-dhcpcd.c | 9 ++ src/dhcp-manager/nm-dhcp-listener.c | 289 ++++++++++++++++++++++++++++++++++++ src/dhcp-manager/nm-dhcp-listener.h | 39 +++++ src/dhcp-manager/nm-dhcp-manager.c | 237 +---------------------------- 8 files changed, 377 insertions(+), 247 deletions(-) create mode 100644 src/dhcp-manager/nm-dhcp-listener.c create mode 100644 src/dhcp-manager/nm-dhcp-listener.h diff --git a/src/Makefile.am b/src/Makefile.am index 5735a3f6f0..92c8b58cf7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -172,6 +172,8 @@ nm_sources = \ dhcp-manager/nm-dhcp-client.h \ dhcp-manager/nm-dhcp-utils.c \ dhcp-manager/nm-dhcp-utils.h \ + dhcp-manager/nm-dhcp-listener.c \ + dhcp-manager/nm-dhcp-listener.h \ dhcp-manager/nm-dhcp-dhclient.c \ dhcp-manager/nm-dhcp-dhclient.h \ dhcp-manager/nm-dhcp-dhclient-utils.c \ diff --git a/src/dhcp-manager/nm-dhcp-client.c b/src/dhcp-manager/nm-dhcp-client.c index b6b571eb5f..d6e308b959 100644 --- a/src/dhcp-manager/nm-dhcp-client.c +++ b/src/dhcp-manager/nm-dhcp-client.c @@ -673,10 +673,13 @@ copy_option (const char * key, g_hash_table_insert (hash, g_strdup (key), str_value); } -void -nm_dhcp_client_new_options (NMDhcpClient *self, - GHashTable *options, - const char *reason) +gboolean +nm_dhcp_client_handle_event (gpointer unused, + const char *iface, + gint64 pid, + GHashTable *options, + const char *reason, + NMDhcpClient *self) { NMDhcpClientPrivate *priv; guint32 old_state; @@ -684,11 +687,19 @@ nm_dhcp_client_new_options (NMDhcpClient *self, GHashTable *str_options = NULL; GObject *ip_config = NULL; - g_return_if_fail (NM_IS_DHCP_CLIENT (self)); - g_return_if_fail (options != NULL); - g_return_if_fail (reason != NULL); + g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), FALSE); + g_return_val_if_fail (iface != NULL, FALSE); + g_return_val_if_fail (pid > 0, FALSE); + g_return_val_if_fail (options != NULL, FALSE); + g_return_val_if_fail (reason != NULL, FALSE); priv = NM_DHCP_CLIENT_GET_PRIVATE (self); + + if (g_strcmp0 (priv->iface, iface) != 0) + return FALSE; + if (priv->pid != pid) + return FALSE; + old_state = priv->state; new_state = reason_to_state (priv->iface, reason); @@ -719,6 +730,8 @@ nm_dhcp_client_new_options (NMDhcpClient *self, if (str_options) g_hash_table_destroy (str_options); g_clear_object (&ip_config); + + return TRUE; } /********************************************/ diff --git a/src/dhcp-manager/nm-dhcp-client.h b/src/dhcp-manager/nm-dhcp-client.h index 89fe821db5..ca0fdd391b 100644 --- a/src/dhcp-manager/nm-dhcp-client.h +++ b/src/dhcp-manager/nm-dhcp-client.h @@ -128,10 +128,6 @@ gboolean nm_dhcp_client_start_ip6 (NMDhcpClient *self, void nm_dhcp_client_stop (NMDhcpClient *self, gboolean release); -void nm_dhcp_client_new_options (NMDhcpClient *self, - GHashTable *options, - const char *reason); - /* Backend helpers for subclasses */ void nm_dhcp_client_stop_existing (const char *pid_file, const char *binary_name); @@ -144,5 +140,12 @@ void nm_dhcp_client_set_state (NMDhcpClient *self, GObject *ip_config, /* NMIP4Config or NMIP6Config */ GHashTable *options); /* str:str hash */ +gboolean nm_dhcp_client_handle_event (gpointer unused, + const char *iface, + gint64 pid, + GHashTable *options, + const char *reason, + NMDhcpClient *self); + #endif /* __NETWORKMANAGER_DHCP_CLIENT_H__ */ diff --git a/src/dhcp-manager/nm-dhcp-dhclient.c b/src/dhcp-manager/nm-dhcp-dhclient.c index e4b37377bc..58da218150 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient.c +++ b/src/dhcp-manager/nm-dhcp-dhclient.c @@ -43,6 +43,7 @@ #include "nm-dhcp-manager.h" #include "nm-posix-signals.h" #include "NetworkManagerUtils.h" +#include "nm-dhcp-listener.h" G_DEFINE_TYPE (NMDhcpDhclient, nm_dhcp_dhclient, NM_TYPE_DHCP_CLIENT) @@ -612,6 +613,11 @@ nm_dhcp_dhclient_init (NMDhcpDhclient *self) /* Fallback option */ if (!priv->def_leasefile) priv->def_leasefile = SYSCONFDIR "/dhclient6.leases"; + + g_signal_connect (nm_dhcp_listener_get (), + NM_DHCP_LISTENER_EVENT, + G_CALLBACK (nm_dhcp_client_handle_event), + self); } static void @@ -619,6 +625,10 @@ dispose (GObject *object) { NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (object); + g_signal_handlers_disconnect_by_func (nm_dhcp_listener_get (), + G_CALLBACK (nm_dhcp_client_handle_event), + NM_DHCP_DHCLIENT (object)); + g_free (priv->pid_file); g_free (priv->conf_file); g_free (priv->lease_file); diff --git a/src/dhcp-manager/nm-dhcp-dhcpcd.c b/src/dhcp-manager/nm-dhcp-dhcpcd.c index a018c4cd42..0313462d7e 100644 --- a/src/dhcp-manager/nm-dhcp-dhcpcd.c +++ b/src/dhcp-manager/nm-dhcp-dhcpcd.c @@ -38,6 +38,7 @@ #include "nm-logging.h" #include "nm-posix-signals.h" #include "NetworkManagerUtils.h" +#include "nm-dhcp-listener.h" G_DEFINE_TYPE (NMDhcpDhcpcd, nm_dhcp_dhcpcd, NM_TYPE_DHCP_CLIENT) @@ -190,6 +191,10 @@ stop (NMDhcpClient *client, gboolean release, const GByteArray *duid) static void nm_dhcp_dhcpcd_init (NMDhcpDhcpcd *self) { + g_signal_connect (nm_dhcp_listener_get (), + NM_DHCP_LISTENER_EVENT, + G_CALLBACK (nm_dhcp_client_handle_event), + self); } static void @@ -197,6 +202,10 @@ dispose (GObject *object) { NMDhcpDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (object); + g_signal_handlers_disconnect_by_func (nm_dhcp_listener_get (), + G_CALLBACK (nm_dhcp_client_handle_event), + NM_DHCP_DHCPCD (object)); + g_free (priv->pid_file); G_OBJECT_CLASS (nm_dhcp_dhcpcd_parent_class)->dispose (object); diff --git a/src/dhcp-manager/nm-dhcp-listener.c b/src/dhcp-manager/nm-dhcp-listener.c new file mode 100644 index 0000000000..ce6dfafad4 --- /dev/null +++ b/src/dhcp-manager/nm-dhcp-listener.c @@ -0,0 +1,289 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright 2014 Red Hat, Inc. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "nm-dhcp-listener.h" +#include "nm-logging.h" +#include "nm-dbus-manager.h" +#include "nm-dbus-glib-types.h" +#include "nm-glib-compat.h" +#include "NetworkManagerUtils.h" + +#define NM_DHCP_CLIENT_DBUS_IFACE "org.freedesktop.nm_dhcp_client" +#define PRIV_SOCK_PATH NMRUNDIR "/private-dhcp" +#define PRIV_SOCK_TAG "dhcp" + +typedef struct { + NMDBusManager * dbus_mgr; + guint new_conn_id; + guint dis_conn_id; + GHashTable * proxies; + DBusGProxy * proxy; +} NMDhcpListenerPrivate; + +#define NM_DHCP_LISTENER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_LISTENER, NMDhcpListenerPrivate)) + +G_DEFINE_TYPE (NMDhcpListener, nm_dhcp_listener, G_TYPE_OBJECT) + +enum { + EVENT, + LAST_SIGNAL +}; +static guint signals[LAST_SIGNAL] = { 0 }; + +/***************************************************/ + +static char * +garray_to_string (GArray *array, const char *key) +{ + GString *str; + int i; + unsigned char c; + char *converted = NULL; + + g_return_val_if_fail (array != NULL, NULL); + + /* Since the DHCP options come through environment variables, they should + * already be UTF-8 safe, but just make sure. + */ + str = g_string_sized_new (array->len); + for (i = 0; i < array->len; i++) { + c = array->data[i]; + + /* Convert NULLs to spaces and non-ASCII characters to ? */ + if (c == '\0') + c = ' '; + else if (c > 127) + c = '?'; + str = g_string_append_c (str, c); + } + str = g_string_append_c (str, '\0'); + + converted = str->str; + if (!g_utf8_validate (converted, -1, NULL)) + nm_log_warn (LOGD_DHCP, "DHCP option '%s' couldn't be converted to UTF-8", key); + g_string_free (str, FALSE); + return converted; +} + +static char * +get_option (GHashTable *hash, const char *key) +{ + GValue *value; + + value = g_hash_table_lookup (hash, key); + if (value == NULL) + return NULL; + + if (G_VALUE_TYPE (value) != DBUS_TYPE_G_UCHAR_ARRAY) { + nm_log_warn (LOGD_DHCP, "unexpected key %s value type was not " + "DBUS_TYPE_G_UCHAR_ARRAY", + (char *) key); + return NULL; + } + + return garray_to_string ((GArray *) g_value_get_boxed (value), key); +} + +static void +handle_event (DBusGProxy *proxy, + GHashTable *options, + gpointer user_data) +{ + NMDhcpListener *self = NM_DHCP_LISTENER (user_data); + char *iface = NULL; + char *pid_str = NULL; + char *reason = NULL; + gint32 pid; + gboolean handled = FALSE; + + iface = get_option (options, "interface"); + if (iface == NULL) { + nm_log_warn (LOGD_DHCP, "DHCP event: didn't have associated interface."); + goto out; + } + + pid_str = get_option (options, "pid"); + pid = (gint32) nm_utils_ascii_str_to_int64 (pid_str, 10, 0, G_MAXINT32, -1); + if (pid == -1 || pid != (GPid) pid) { + nm_log_warn (LOGD_DHCP, "DHCP event: couldn't convert PID '%s' to an integer", pid_str ? pid_str : "(null)"); + goto out; + } + + reason = get_option (options, "reason"); + if (reason == NULL) { + nm_log_warn (LOGD_DHCP, "(pid %d) DHCP event didn't have a reason", pid); + goto out; + } + + g_signal_emit (self, signals[EVENT], 0, iface, pid, options, reason, &handled); + if (!handled) { + if (g_ascii_strcasecmp (reason, "RELEASE") == 0) { + /* Ignore event when the dhcp client gets killed and we receive its last message */ + nm_log_dbg (LOGD_DHCP, "(pid %d) unhandled RELEASE DHCP event for interface %s", pid, iface); + } else + nm_log_warn (LOGD_DHCP, "(pid %d) unhandled DHCP event for interface %s", pid, iface); + } + +out: + g_free (iface); + g_free (pid_str); + g_free (reason); +} + +#if HAVE_DBUS_GLIB_100 +static void +new_connection_cb (NMDBusManager *mgr, + DBusGConnection *connection, + NMDhcpListener *self) +{ + DBusGProxy *proxy; + + /* Create a new proxy for the client */ + proxy = dbus_g_proxy_new_for_peer (connection, "/", NM_DHCP_CLIENT_DBUS_IFACE); + dbus_g_proxy_add_signal (proxy, "Event", DBUS_TYPE_G_MAP_OF_VARIANT, G_TYPE_INVALID); + dbus_g_proxy_connect_signal (proxy, "Event", G_CALLBACK (handle_event), self, NULL); + + g_hash_table_insert (NM_DHCP_LISTENER_GET_PRIVATE (self)->proxies, connection, proxy); +} + +static void +dis_connection_cb (NMDBusManager *mgr, + DBusGConnection *connection, + NMDhcpListener *self) +{ + NMDhcpListenerPrivate *priv = NM_DHCP_LISTENER_GET_PRIVATE (self); + DBusGProxy *proxy; + + proxy = g_hash_table_lookup (priv->proxies, connection); + if (proxy) { + dbus_g_proxy_disconnect_signal (proxy, "Event", G_CALLBACK (handle_event), self); + g_hash_table_remove (priv->proxies, connection); + } +} +#endif + +/***************************************************/ + +NMDhcpListener * +nm_dhcp_listener_get (void) +{ + static NMDhcpListener *singleton = NULL; + + if (G_UNLIKELY (singleton == NULL)) + singleton = g_object_new (NM_TYPE_DHCP_LISTENER, NULL); + g_assert (singleton); + return singleton; +} + +static void +nm_dhcp_listener_init (NMDhcpListener *self) +{ + NMDhcpListenerPrivate *priv = NM_DHCP_LISTENER_GET_PRIVATE (self); +#if !HAVE_DBUS_GLIB_100 + DBusGConnection *g_connection; +#endif + + /* Maps DBusGConnection :: DBusGProxy */ + priv->proxies = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref); + + priv->dbus_mgr = nm_dbus_manager_get (); + +#if HAVE_DBUS_GLIB_100 + /* Register the socket our DHCP clients will return lease info on */ + nm_dbus_manager_private_server_register (priv->dbus_mgr, PRIV_SOCK_PATH, PRIV_SOCK_TAG); + priv->new_conn_id = g_signal_connect (priv->dbus_mgr, + NM_DBUS_MANAGER_PRIVATE_CONNECTION_NEW "::" PRIV_SOCK_TAG, + G_CALLBACK (new_connection_cb), + self); + priv->dis_conn_id = g_signal_connect (priv->dbus_mgr, + NM_DBUS_MANAGER_PRIVATE_CONNECTION_DISCONNECTED "::" PRIV_SOCK_TAG, + G_CALLBACK (dis_connection_cb), + self); +#else + g_connection = nm_dbus_manager_get_connection (priv->dbus_mgr); + priv->proxy = dbus_g_proxy_new_for_name (g_connection, + "org.freedesktop.nm_dhcp_client", + "/", + NM_DHCP_CLIENT_DBUS_IFACE); + g_assert (priv->proxy); + dbus_g_proxy_add_signal (priv->proxy, "Event", DBUS_TYPE_G_MAP_OF_VARIANT, G_TYPE_INVALID); + dbus_g_proxy_connect_signal (priv->proxy, "Event", G_CALLBACK (handle_event), self, NULL); +#endif +} + +static void +dispose (GObject *object) +{ + NMDhcpListenerPrivate *priv = NM_DHCP_LISTENER_GET_PRIVATE (object); + + if (priv->new_conn_id) { + g_signal_handler_disconnect (priv->dbus_mgr, priv->new_conn_id); + priv->new_conn_id = 0; + } + if (priv->dis_conn_id) { + g_signal_handler_disconnect (priv->dbus_mgr, priv->dis_conn_id); + priv->dis_conn_id = 0; + } + priv->dbus_mgr = NULL; + + if (priv->proxies) { + g_hash_table_destroy (priv->proxies); + priv->proxies = NULL; + } + g_clear_object (&priv->proxy); + + G_OBJECT_CLASS (nm_dhcp_listener_parent_class)->dispose (object); +} + +static void +nm_dhcp_listener_class_init (NMDhcpListenerClass *listener_class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (listener_class); + + g_type_class_add_private (listener_class, sizeof (NMDhcpListenerPrivate)); + + /* virtual methods */ + object_class->dispose = dispose; + + /* signals */ + signals[EVENT] = + g_signal_new (NM_DHCP_LISTENER_EVENT, + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, 0, + g_signal_accumulator_true_handled, + NULL, NULL, + G_TYPE_BOOLEAN, /* listeners return TRUE if handled */ + 4, + G_TYPE_STRING, /* iface */ + G_TYPE_INT64, /* pid */ + G_TYPE_HASH_TABLE, /* options */ + G_TYPE_STRING); /* reason */ +} diff --git a/src/dhcp-manager/nm-dhcp-listener.h b/src/dhcp-manager/nm-dhcp-listener.h new file mode 100644 index 0000000000..15ec053109 --- /dev/null +++ b/src/dhcp-manager/nm-dhcp-listener.h @@ -0,0 +1,39 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright 2014 Red Hat, Inc. + */ + +#ifndef __NETWORKMANAGER_DHCP_LISTENER_H__ +#define __NETWORKMANAGER_DHCP_LISTENER_H__ + +#include +#include + +#define NM_TYPE_DHCP_LISTENER (nm_dhcp_listener_get_type ()) +#define NM_DHCP_LISTENER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DHCP_LISTENER, NMDhcpListener)) +#define NM_IS_DHCP_LISTENER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DHCP_LISTENER)) +#define NM_DHCP_LISTENER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DHCP_LISTENER, NMDhcpListenerClass)) + +#define NM_DHCP_LISTENER_EVENT "event" + +typedef GObject NMDhcpListener; +typedef GObjectClass NMDhcpListenerClass; + +GType nm_dhcp_listener_get_type (void); + +NMDhcpListener *nm_dhcp_listener_get (void); + +#endif /* __NETWORKMANAGER_DHCP_LISTENER_H__ */ diff --git a/src/dhcp-manager/nm-dhcp-manager.c b/src/dhcp-manager/nm-dhcp-manager.c index 5c139b8ca2..beb9c94c65 100644 --- a/src/dhcp-manager/nm-dhcp-manager.c +++ b/src/dhcp-manager/nm-dhcp-manager.c @@ -23,7 +23,6 @@ #include "config.h" #include #include -#include #include #include #include @@ -39,19 +38,13 @@ #include "nm-dhcp-dhcpcd.h" #include "nm-dhcp-systemd.h" #include "nm-logging.h" -#include "nm-dbus-manager.h" #include "nm-config.h" #include "nm-dbus-glib-types.h" #include "nm-glib-compat.h" #include "NetworkManagerUtils.h" -#define NM_DHCP_CLIENT_DBUS_IFACE "org.freedesktop.nm_dhcp_client" - #define DHCP_TIMEOUT 45 /* default DHCP timeout, in seconds */ -#define PRIV_SOCK_PATH NMRUNDIR "/private-dhcp" -#define PRIV_SOCK_TAG "dhcp" - /* default to installed helper, but can be modified for testing */ const char *nm_dhcp_helper_path = LIBEXECDIR "/nm-dhcp-helper"; @@ -60,76 +53,15 @@ typedef GSList * (*GetLeaseConfigFunc) (const char *iface, const char *uuid, gbo typedef struct { GType client_type; GetLeaseConfigFunc get_lease_ip_configs_func; - - NMDBusManager * dbus_mgr; - guint new_conn_id; - guint dis_conn_id; - GHashTable * proxies; - GHashTable * clients; - DBusGProxy * proxy; char * default_hostname; } NMDhcpManagerPrivate; - #define NM_DHCP_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_MANAGER, NMDhcpManagerPrivate)) G_DEFINE_TYPE (NMDhcpManager, nm_dhcp_manager, G_TYPE_OBJECT) -static char * -garray_to_string (GArray *array, const char *key) -{ - GString *str; - int i; - unsigned char c; - char *converted = NULL; - - g_return_val_if_fail (array != NULL, NULL); - - /* Since the DHCP options come through environment variables, they should - * already be UTF-8 safe, but just make sure. - */ - str = g_string_sized_new (array->len); - for (i = 0; i < array->len; i++) { - c = array->data[i]; - - /* Convert NULLs to spaces and non-ASCII characters to ? */ - if (c == '\0') - c = ' '; - else if (c > 127) - c = '?'; - str = g_string_append_c (str, c); - } - str = g_string_append_c (str, '\0'); - - converted = str->str; - if (!g_utf8_validate (converted, -1, NULL)) - nm_log_warn (LOGD_DHCP, "DHCP option '%s' couldn't be converted to UTF-8", key); - g_string_free (str, FALSE); - return converted; -} - -static NMDhcpClient * -get_client_for_pid (NMDhcpManager *manager, GPid pid) -{ - NMDhcpManagerPrivate *priv; - GHashTableIter iter; - gpointer value; - - g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), NULL); - - priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); - - g_hash_table_iter_init (&iter, priv->clients); - while (g_hash_table_iter_next (&iter, NULL, &value)) { - NMDhcpClient *candidate = NM_DHCP_CLIENT (value); - - if (nm_dhcp_client_get_pid (candidate) == pid) - return candidate; - } - - return NULL; -} +/***************************************************/ static NMDhcpClient * get_client_for_ifindex (NMDhcpManager *manager, int ifindex, gboolean ip6) @@ -155,123 +87,6 @@ get_client_for_ifindex (NMDhcpManager *manager, int ifindex, gboolean ip6) return NULL; } -static char * -get_option (GHashTable *hash, const char *key) -{ - GValue *value; - - value = g_hash_table_lookup (hash, key); - if (value == NULL) - return NULL; - - if (G_VALUE_TYPE (value) != DBUS_TYPE_G_UCHAR_ARRAY) { - nm_log_warn (LOGD_DHCP, "unexpected key %s value type was not " - "DBUS_TYPE_G_UCHAR_ARRAY", - (char *) key); - return NULL; - } - - return garray_to_string ((GArray *) g_value_get_boxed (value), key); -} - -static void -nm_dhcp_manager_handle_event (DBusGProxy *proxy, - GHashTable *options, - gpointer user_data) -{ - NMDhcpManager *manager = NM_DHCP_MANAGER (user_data); - NMDhcpClient *client; - char *iface = NULL; - char *pid_str = NULL; - char *reason = NULL; - long pid; - - iface = get_option (options, "interface"); - if (iface == NULL) { - nm_log_warn (LOGD_DHCP, "DHCP event: didn't have associated interface."); - goto out; - } - - pid_str = get_option (options, "pid"); - pid = nm_utils_ascii_str_to_int64 (pid_str, 10, 0, LONG_MAX, -1); - if (pid == -1 || pid != (GPid)pid) { - nm_log_warn (LOGD_DHCP, "DHCP event: couldn't convert PID '%s' to an integer", pid_str ? pid_str : "(null)"); - goto out; - } - - reason = get_option (options, "reason"); - client = get_client_for_pid (manager, (GPid) pid); - if (client == NULL) { - if (reason && g_ascii_strcasecmp (reason, "RELEASE") == 0) { - /* This happens regularly, when the dhcp client gets killed and we receive its last message. - * Don't log a warning in this case. */ - nm_log_dbg (LOGD_DHCP, "(pid %ld) unhandled RELEASE DHCP event for interface %s", pid, iface); - } else - nm_log_warn (LOGD_DHCP, "(pid %ld) unhandled DHCP event for interface %s", pid, iface); - goto out; - } - - if (strcmp (iface, nm_dhcp_client_get_iface (client))) { - nm_log_warn (LOGD_DHCP, "(pid %ld) received DHCP event from unexpected interface '%s' (expected '%s')", - pid, iface, nm_dhcp_client_get_iface (client)); - goto out; - } - - if (reason == NULL) { - nm_log_warn (LOGD_DHCP, "(pid %ld) DHCP event didn't have a reason", pid); - goto out; - } - - nm_dhcp_client_new_options (client, options, reason); - -out: - g_free (iface); - g_free (pid_str); - g_free (reason); -} - -#if HAVE_DBUS_GLIB_100 -static void -new_connection_cb (NMDBusManager *mgr, - DBusGConnection *connection, - NMDhcpManager *self) -{ - NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self); - DBusGProxy *proxy; - - /* Create a new proxy for the client */ - proxy = dbus_g_proxy_new_for_peer (connection, "/", NM_DHCP_CLIENT_DBUS_IFACE); - dbus_g_proxy_add_signal (proxy, - "Event", - DBUS_TYPE_G_MAP_OF_VARIANT, - G_TYPE_INVALID); - dbus_g_proxy_connect_signal (proxy, - "Event", - G_CALLBACK (nm_dhcp_manager_handle_event), - self, - NULL); - g_hash_table_insert (priv->proxies, connection, proxy); -} - -static void -dis_connection_cb (NMDBusManager *mgr, - DBusGConnection *connection, - NMDhcpManager *self) -{ - NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self); - DBusGProxy *proxy; - - proxy = g_hash_table_lookup (priv->proxies, connection); - if (proxy) { - dbus_g_proxy_disconnect_signal (proxy, - "Event", - G_CALLBACK (nm_dhcp_manager_handle_event), - self); - g_hash_table_remove (priv->proxies, connection); - } -} -#endif - static GType get_client_type (const char *client, GError **error) { @@ -529,12 +344,6 @@ nm_dhcp_manager_init (NMDhcpManager *self) NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self); const char *client; GError *error = NULL; -#if !HAVE_DBUS_GLIB_100 - DBusGConnection *g_connection; -#endif - - /* Maps DBusGConnection :: DBusGProxy */ - priv->proxies = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref); /* Client-specific setup */ client = nm_config_get_dhcp_client (nm_config_get ()); @@ -554,33 +363,6 @@ nm_dhcp_manager_init (NMDhcpManager *self) NULL, (GDestroyNotify) g_object_unref); g_assert (priv->clients); - - priv->dbus_mgr = nm_dbus_manager_get (); - -#if HAVE_DBUS_GLIB_100 - /* Register the socket our DHCP clients will return lease info on */ - nm_dbus_manager_private_server_register (priv->dbus_mgr, PRIV_SOCK_PATH, PRIV_SOCK_TAG); - priv->new_conn_id = g_signal_connect (priv->dbus_mgr, - NM_DBUS_MANAGER_PRIVATE_CONNECTION_NEW "::" PRIV_SOCK_TAG, - (GCallback) new_connection_cb, - self); - priv->dis_conn_id = g_signal_connect (priv->dbus_mgr, - NM_DBUS_MANAGER_PRIVATE_CONNECTION_DISCONNECTED "::" PRIV_SOCK_TAG, - (GCallback) dis_connection_cb, - self); -#else - g_connection = nm_dbus_manager_get_connection (priv->dbus_mgr); - priv->proxy = dbus_g_proxy_new_for_name (g_connection, - "org.freedesktop.nm_dhcp_client", - "/", - NM_DHCP_CLIENT_DBUS_IFACE); - g_assert (priv->proxy); - dbus_g_proxy_add_signal (priv->proxy, "Event", DBUS_TYPE_G_MAP_OF_VARIANT, G_TYPE_INVALID); - dbus_g_proxy_connect_signal (priv->proxy, "Event", - G_CALLBACK (nm_dhcp_manager_handle_event), - self, - NULL); -#endif } static void @@ -596,23 +378,6 @@ dispose (GObject *object) g_list_free (values); } - if (priv->new_conn_id) { - g_signal_handler_disconnect (priv->dbus_mgr, priv->new_conn_id); - priv->new_conn_id = 0; - } - if (priv->dis_conn_id) { - g_signal_handler_disconnect (priv->dbus_mgr, priv->dis_conn_id); - priv->dis_conn_id = 0; - } - priv->dbus_mgr = NULL; - - if (priv->proxies) { - g_hash_table_destroy (priv->proxies); - priv->proxies = NULL; - } - if (priv->proxy) - g_object_unref (priv->proxy); - G_OBJECT_CLASS (nm_dhcp_manager_parent_class)->dispose (object); } -- cgit v1.2.1 From 318a8c2d727bee5c401f2d6b7f28f706ecb4dfb2 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 29 Oct 2014 17:12:46 -0500 Subject: dhcp: move client-specific knowledge out of the manager --- src/Makefile.am | 22 ++++-- src/dhcp-manager/nm-dhcp-client.h | 11 +++ src/dhcp-manager/nm-dhcp-dhclient.c | 14 +++- src/dhcp-manager/nm-dhcp-dhclient.h | 6 -- src/dhcp-manager/nm-dhcp-dhcpcd.c | 12 ++- src/dhcp-manager/nm-dhcp-dhcpcd.h | 2 - src/dhcp-manager/nm-dhcp-manager.c | 149 ++++++++++++++++++++++-------------- src/dhcp-manager/nm-dhcp-systemd.c | 12 ++- src/dhcp-manager/nm-dhcp-systemd.h | 4 - src/dhcp-manager/tests/Makefile.am | 2 + 10 files changed, 154 insertions(+), 80 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 92c8b58cf7..e186632548 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -124,6 +124,7 @@ sbin_PROGRAMS = NetworkManager NetworkManager_SOURCES = \ $(nm_device_sources) $(nm_device_headers) \ + $(nm_dhcp_client_sources) $(nm_dhcp_client_headers) \ main-utils.c \ main-utils.h \ main.c @@ -155,8 +156,21 @@ nm_device_headers = \ devices/nm-device-vlan.h \ devices/nm-device-vxlan.h +nm_dhcp_client_sources = \ + dhcp-manager/nm-dhcp-dhclient.c \ + dhcp-manager/nm-dhcp-dhclient-utils.c \ + dhcp-manager/nm-dhcp-dhcpcd.c \ + dhcp-manager/nm-dhcp-systemd.c + +nm_dhcp_client_headers = \ + dhcp-manager/nm-dhcp-dhclient.h \ + dhcp-manager/nm-dhcp-dhclient-utils.h \ + dhcp-manager/nm-dhcp-dhcpcd.h \ + dhcp-manager/nm-dhcp-systemd.h + nm_sources = \ $(nm_device_headers) \ + $(nm_dhcp_client_headers) \ devices/nm-device.c \ devices/nm-device.h \ devices/nm-device-ethernet-utils.c \ @@ -174,14 +188,6 @@ nm_sources = \ dhcp-manager/nm-dhcp-utils.h \ dhcp-manager/nm-dhcp-listener.c \ dhcp-manager/nm-dhcp-listener.h \ - dhcp-manager/nm-dhcp-dhclient.c \ - dhcp-manager/nm-dhcp-dhclient.h \ - dhcp-manager/nm-dhcp-dhclient-utils.c \ - dhcp-manager/nm-dhcp-dhclient-utils.h \ - dhcp-manager/nm-dhcp-dhcpcd.c \ - dhcp-manager/nm-dhcp-dhcpcd.h \ - dhcp-manager/nm-dhcp-systemd.h \ - dhcp-manager/nm-dhcp-systemd.c \ dhcp-manager/nm-dhcp-manager.c \ dhcp-manager/nm-dhcp-manager.h \ \ diff --git a/src/dhcp-manager/nm-dhcp-client.h b/src/dhcp-manager/nm-dhcp-client.h index ca0fdd391b..a9ce9bec37 100644 --- a/src/dhcp-manager/nm-dhcp-client.h +++ b/src/dhcp-manager/nm-dhcp-client.h @@ -99,6 +99,17 @@ typedef struct { GType nm_dhcp_client_get_type (void); +typedef const char *(*NMDhcpClientGetPathFunc) (void); + +typedef GSList * (*NMDhcpClientGetLeaseConfigsFunc) (const char *iface, + const char *uuid, + gboolean ipv6); + +void _nm_dhcp_client_register (GType gtype, + const char *name, + NMDhcpClientGetPathFunc get_path_func, + NMDhcpClientGetLeaseConfigsFunc get_lease_configs_func); + pid_t nm_dhcp_client_get_pid (NMDhcpClient *self); const char *nm_dhcp_client_get_iface (NMDhcpClient *self); diff --git a/src/dhcp-manager/nm-dhcp-dhclient.c b/src/dhcp-manager/nm-dhcp-dhclient.c index 58da218150..8f1718f8a6 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient.c +++ b/src/dhcp-manager/nm-dhcp-dhclient.c @@ -56,7 +56,7 @@ typedef struct { char *pid_file; } NMDhcpDhclientPrivate; -const char * +static const char * nm_dhcp_dhclient_get_path (void) { const char *path = NULL; @@ -123,7 +123,7 @@ get_dhclient_leasefile (const char *iface, return NULL; } -GSList * +static GSList * nm_dhcp_dhclient_get_lease_ip_configs (const char *iface, const char *uuid, gboolean ipv6) @@ -653,3 +653,13 @@ nm_dhcp_dhclient_class_init (NMDhcpDhclientClass *dhclient_class) client_class->get_duid = get_duid; } +static void __attribute__((constructor)) +register_dhcp_dhclient (void) +{ + g_type_init (); + _nm_dhcp_client_register (NM_TYPE_DHCP_DHCLIENT, + "dhclient", + nm_dhcp_dhclient_get_path, + nm_dhcp_dhclient_get_lease_ip_configs); +} + diff --git a/src/dhcp-manager/nm-dhcp-dhclient.h b/src/dhcp-manager/nm-dhcp-dhclient.h index 6c58826241..5abcc08ee3 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient.h +++ b/src/dhcp-manager/nm-dhcp-dhclient.h @@ -41,11 +41,5 @@ typedef struct { GType nm_dhcp_dhclient_get_type (void); -GSList *nm_dhcp_dhclient_get_lease_ip_configs (const char *iface, - const char *uuid, - gboolean ipv6); - -const char *nm_dhcp_dhclient_get_path (void); - #endif /* __NETWORKMANAGER_DHCP_DHCLIENT_H__ */ diff --git a/src/dhcp-manager/nm-dhcp-dhcpcd.c b/src/dhcp-manager/nm-dhcp-dhcpcd.c index 0313462d7e..c4d91b1247 100644 --- a/src/dhcp-manager/nm-dhcp-dhcpcd.c +++ b/src/dhcp-manager/nm-dhcp-dhcpcd.c @@ -48,7 +48,7 @@ typedef struct { char *pid_file; } NMDhcpDhcpcdPrivate; -const char * +static const char * nm_dhcp_dhcpcd_get_path (void) { const char *path = NULL; @@ -227,3 +227,13 @@ nm_dhcp_dhcpcd_class_init (NMDhcpDhcpcdClass *dhcpcd_class) client_class->stop = stop; } +static void __attribute__((constructor)) +register_dhcp_dhclient (void) +{ + g_type_init (); + _nm_dhcp_client_register (NM_TYPE_DHCP_DHCPCD, + "dhcpcd", + nm_dhcp_dhcpcd_get_path, + NULL); +} + diff --git a/src/dhcp-manager/nm-dhcp-dhcpcd.h b/src/dhcp-manager/nm-dhcp-dhcpcd.h index 4e3502ab5a..deed70e0f9 100644 --- a/src/dhcp-manager/nm-dhcp-dhcpcd.h +++ b/src/dhcp-manager/nm-dhcp-dhcpcd.h @@ -41,7 +41,5 @@ typedef struct { GType nm_dhcp_dhcpcd_get_type (void); -const char *nm_dhcp_dhcpcd_get_path (void); - #endif /* __NETWORKMANAGER_DHCP_DHCPCD_H__ */ diff --git a/src/dhcp-manager/nm-dhcp-manager.c b/src/dhcp-manager/nm-dhcp-manager.c index beb9c94c65..dbc0684dbc 100644 --- a/src/dhcp-manager/nm-dhcp-manager.c +++ b/src/dhcp-manager/nm-dhcp-manager.c @@ -52,7 +52,6 @@ typedef GSList * (*GetLeaseConfigFunc) (const char *iface, const char *uuid, gbo typedef struct { GType client_type; - GetLeaseConfigFunc get_lease_ip_configs_func; GHashTable * clients; char * default_hostname; } NMDhcpManagerPrivate; @@ -63,6 +62,79 @@ G_DEFINE_TYPE (NMDhcpManager, nm_dhcp_manager, G_TYPE_OBJECT) /***************************************************/ +typedef struct { + GType gtype; + const char *name; + NMDhcpClientGetPathFunc get_path_func; + NMDhcpClientGetLeaseConfigsFunc get_lease_configs_func; +} ClientDesc; + +static GSList *client_descs = NULL; + +void +_nm_dhcp_client_register (GType gtype, + const char *name, + NMDhcpClientGetPathFunc get_path_func, + NMDhcpClientGetLeaseConfigsFunc get_lease_configs_func) +{ + ClientDesc *desc; + GSList *iter; + + g_return_if_fail (gtype != G_TYPE_INVALID); + g_return_if_fail (name != NULL); + + for (iter = client_descs; iter; iter = iter->next) { + desc = iter->data; + g_return_if_fail (desc->gtype != gtype); + g_return_if_fail (strcmp (desc->name, name) != 0); + } + + desc = g_slice_new0 (ClientDesc); + desc->gtype = gtype; + desc->name = name; + desc->get_path_func = get_path_func; + desc->get_lease_configs_func = get_lease_configs_func; + client_descs = g_slist_prepend (client_descs, desc); + + nm_log_info (LOGD_DHCP, "Registered DHCP client '%s'", name); +} + +static ClientDesc * +find_client_desc (const char *name, GType gtype) +{ + GSList *iter; + + g_return_val_if_fail (name || gtype, NULL); + + for (iter = client_descs; iter; iter = iter->next) { + ClientDesc *desc = iter->data; + + if (name && strcmp (desc->name, name) != 0) + continue; + if (gtype && desc->name != 0) + continue; + return desc; + } + return NULL; +} + +static GType +is_client_enabled (const char *name, GError **error) +{ + ClientDesc *desc; + + desc = find_client_desc (name, G_TYPE_INVALID); + if (desc && (!desc->get_path_func || desc->get_path_func())) + return desc->gtype; + + g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED, + _("'%s' support not found or not enabled."), + name); + return G_TYPE_INVALID; +} + +/***************************************************/ + static NMDhcpClient * get_client_for_ifindex (NMDhcpManager *manager, int ifindex, gboolean ip6) { @@ -90,52 +162,23 @@ get_client_for_ifindex (NMDhcpManager *manager, int ifindex, gboolean ip6) static GType get_client_type (const char *client, GError **error) { - gboolean use_dhclient, use_dhcpcd; - - /* If a client was disabled at build-time, these will return FALSE */ - use_dhclient = !!nm_dhcp_dhclient_get_path (); - use_dhcpcd = !!nm_dhcp_dhcpcd_get_path (); - - if (!client) { - if (use_dhclient) - return NM_TYPE_DHCP_DHCLIENT; - else if (use_dhcpcd) - return NM_TYPE_DHCP_DHCPCD; - else { - g_set_error_literal (error, - NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED, - _("no usable DHCP client could be found.")); - return G_TYPE_INVALID; - } - } - - if (!strcmp (client, "dhclient")) { - if (!use_dhclient) { - g_set_error_literal (error, - NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED, - _("'dhclient' could not be found or was disabled.")); - return G_TYPE_INVALID; + GType client_gtype; + + if (client) + client_gtype = is_client_enabled (client, error); + else { + /* Fallbacks */ + client_gtype = is_client_enabled ("dhclient", NULL); + if (client_gtype == G_TYPE_INVALID) + client_gtype = is_client_enabled ("dhcpcd", NULL); + if (client_gtype == G_TYPE_INVALID) + client_gtype = is_client_enabled ("internal", NULL); + if (client_gtype == G_TYPE_INVALID) { + g_set_error_literal (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED, + _("no usable DHCP client could be found.")); } - return NM_TYPE_DHCP_DHCLIENT; } - - if (!strcmp (client, "dhcpcd")) { - if (!use_dhcpcd) { - g_set_error_literal (error, - NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED, - _("'dhcpcd' could not be found or was disabled.")); - return G_TYPE_INVALID; - } - return NM_TYPE_DHCP_DHCPCD; - } - - if (!strcmp (client, "internal")) - return NM_TYPE_DHCP_SYSTEMD; - - g_set_error (error, - NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED, - _("unsupported DHCP client '%s'"), client); - return G_TYPE_INVALID; + return client_gtype; } static void client_state_changed (NMDhcpClient *client, @@ -312,16 +355,15 @@ nm_dhcp_manager_get_lease_ip_configs (NMDhcpManager *self, const char *uuid, gboolean ipv6) { - NMDhcpManagerPrivate *priv; + ClientDesc *desc; g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL); g_return_val_if_fail (iface != NULL, NULL); g_return_val_if_fail (uuid != NULL, NULL); - priv = NM_DHCP_MANAGER_GET_PRIVATE (self); - - if (priv->get_lease_ip_configs_func) - return priv->get_lease_ip_configs_func (iface, uuid, ipv6); + desc = find_client_desc (NULL, NM_DHCP_MANAGER_GET_PRIVATE (self)->client_type); + if (desc && desc->get_lease_configs_func) + return desc->get_lease_configs_func (iface, uuid, ipv6); return NULL; } @@ -348,12 +390,7 @@ nm_dhcp_manager_init (NMDhcpManager *self) /* Client-specific setup */ client = nm_config_get_dhcp_client (nm_config_get ()); priv->client_type = get_client_type (client, &error); - - if (priv->client_type == NM_TYPE_DHCP_DHCLIENT) - priv->get_lease_ip_configs_func = nm_dhcp_dhclient_get_lease_ip_configs; - else if (priv->client_type == NM_TYPE_DHCP_SYSTEMD) - priv->get_lease_ip_configs_func = nm_dhcp_systemd_get_lease_ip_configs; - else if (priv->client_type == G_TYPE_INVALID) { + if (priv->client_type == G_TYPE_INVALID) { nm_log_warn (LOGD_DHCP, "No usable DHCP client found (%s)! DHCP configurations will fail.", error->message); } diff --git a/src/dhcp-manager/nm-dhcp-systemd.c b/src/dhcp-manager/nm-dhcp-systemd.c index 18b7b32180..6b06c45288 100644 --- a/src/dhcp-manager/nm-dhcp-systemd.c +++ b/src/dhcp-manager/nm-dhcp-systemd.c @@ -374,7 +374,7 @@ get_leasefile_path (const char *iface, const char *uuid, gboolean ipv6) iface); } -GSList * +static GSList * nm_dhcp_systemd_get_lease_ip_configs (const char *iface, const char *uuid, gboolean ipv6) @@ -808,3 +808,13 @@ nm_dhcp_systemd_class_init (NMDhcpSystemdClass *sdhcp_class) client_class->stop = stop; } +static void __attribute__((constructor)) +register_dhcp_dhclient (void) +{ + g_type_init (); + _nm_dhcp_client_register (NM_TYPE_DHCP_SYSTEMD, + "internal", + NULL, + nm_dhcp_systemd_get_lease_ip_configs); +} + diff --git a/src/dhcp-manager/nm-dhcp-systemd.h b/src/dhcp-manager/nm-dhcp-systemd.h index 05e8e8da33..2a7a463eb5 100644 --- a/src/dhcp-manager/nm-dhcp-systemd.h +++ b/src/dhcp-manager/nm-dhcp-systemd.h @@ -41,9 +41,5 @@ typedef struct { GType nm_dhcp_systemd_get_type (void); -GSList *nm_dhcp_systemd_get_lease_ip_configs (const char *iface, - const char *uuid, - gboolean ipv6); - #endif /* NM_DHCP_SYSTEMD_H */ diff --git a/src/dhcp-manager/tests/Makefile.am b/src/dhcp-manager/tests/Makefile.am index 0292db2b7a..1eb02d6323 100644 --- a/src/dhcp-manager/tests/Makefile.am +++ b/src/dhcp-manager/tests/Makefile.am @@ -18,6 +18,8 @@ noinst_PROGRAMS = \ ####### dhclient leases test ####### test_dhcp_dhclient_SOURCES = \ + $(top_srcdir)/src/dhcp-manager/nm-dhcp-dhclient-utils.h \ + $(top_srcdir)/src/dhcp-manager/nm-dhcp-dhclient-utils.c \ test-dhcp-dhclient.c test_dhcp_dhclient_LDADD = \ -- cgit v1.2.1 From e43174f368f4cb319897626207afea30f5896147 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 3 Nov 2014 14:39:25 -0600 Subject: dhcp: preserve DHCPv4 client ID for later use If we can, read the existing client ID from the leasefile and preserve it for later use. --- src/dhcp-manager/nm-dhcp-client.c | 28 +++++- src/dhcp-manager/nm-dhcp-client.h | 5 +- src/dhcp-manager/nm-dhcp-dhclient-utils.c | 134 ++++++++++++++++++++-------- src/dhcp-manager/nm-dhcp-dhclient-utils.h | 7 +- src/dhcp-manager/nm-dhcp-dhclient.c | 51 ++++++++--- src/dhcp-manager/nm-dhcp-dhcpcd.c | 1 - src/dhcp-manager/nm-dhcp-systemd.c | 58 +++++++++--- src/dhcp-manager/tests/Makefile.am | 2 + src/dhcp-manager/tests/test-dhcp-dhclient.c | 124 ++++++++++++++++++++++--- 9 files changed, 327 insertions(+), 83 deletions(-) diff --git a/src/dhcp-manager/nm-dhcp-client.c b/src/dhcp-manager/nm-dhcp-client.c index d6e308b959..f455efa732 100644 --- a/src/dhcp-manager/nm-dhcp-client.c +++ b/src/dhcp-manager/nm-dhcp-client.c @@ -45,6 +45,7 @@ typedef struct { guint32 priority; guint32 timeout; GByteArray * duid; + GBytes * client_id; NMDhcpState state; pid_t pid; @@ -143,6 +144,29 @@ nm_dhcp_client_get_priority (NMDhcpClient *self) return NM_DHCP_CLIENT_GET_PRIVATE (self)->priority; } +GBytes * +nm_dhcp_client_get_client_id (NMDhcpClient *self) +{ + g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL); + + return NM_DHCP_CLIENT_GET_PRIVATE (self)->client_id; +} + +void +nm_dhcp_client_set_client_id (NMDhcpClient *self, GBytes *client_id) +{ + NMDhcpClientPrivate *priv; + + g_return_if_fail (NM_IS_DHCP_CLIENT (self)); + + priv = NM_DHCP_CLIENT_GET_PRIVATE (self); + + if (priv->client_id && client_id && g_bytes_equal (priv->client_id, client_id)) + return; + g_clear_pointer (&priv->client_id, g_bytes_unref); + priv->client_id = client_id ? g_bytes_ref (client_id) : NULL; +} + /********************************************/ static const char *state_table[NM_DHCP_STATE_MAX + 1] = { @@ -374,7 +398,9 @@ nm_dhcp_client_start_ip4 (NMDhcpClient *self, nm_log_info (LOGD_DHCP, "Activation (%s) Beginning DHCPv4 transaction (timeout in %d seconds)", priv->iface, priv->timeout); - return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, dhcp_client_id, dhcp_anycast_addr, hostname); + nm_dhcp_client_set_client_id (self, dhcp_client_id ? nm_dhcp_utils_client_id_string_to_bytes (dhcp_client_id) : NULL); + + return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, dhcp_anycast_addr, hostname); } /* uuid_parse does not work for machine-id, so we use our own converter */ diff --git a/src/dhcp-manager/nm-dhcp-client.h b/src/dhcp-manager/nm-dhcp-client.h index a9ce9bec37..fda14cf4a5 100644 --- a/src/dhcp-manager/nm-dhcp-client.h +++ b/src/dhcp-manager/nm-dhcp-client.h @@ -64,7 +64,6 @@ typedef struct { /* Methods */ gboolean (*ip4_start) (NMDhcpClient *self, - const char *dhcp_client_id, const char *anycast_addr, const char *hostname); @@ -126,6 +125,8 @@ const GByteArray *nm_dhcp_client_get_hw_addr (NMDhcpClient *self); guint32 nm_dhcp_client_get_priority (NMDhcpClient *self); +GBytes *nm_dhcp_client_get_client_id (NMDhcpClient *self); + gboolean nm_dhcp_client_start_ip4 (NMDhcpClient *self, const char *dhcp_client_id, const char *dhcp_anycast_addr, @@ -158,5 +159,7 @@ gboolean nm_dhcp_client_handle_event (gpointer unused, const char *reason, NMDhcpClient *self); +void nm_dhcp_client_set_client_id (NMDhcpClient *self, GBytes *client_id); + #endif /* __NETWORKMANAGER_DHCP_CLIENT_H__ */ diff --git a/src/dhcp-manager/nm-dhcp-dhclient-utils.c b/src/dhcp-manager/nm-dhcp-dhclient-utils.c index a53e95b009..3c3c42c84a 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient-utils.c +++ b/src/dhcp-manager/nm-dhcp-dhclient-utils.c @@ -26,14 +26,14 @@ #include #include "nm-dhcp-dhclient-utils.h" +#include "nm-dhcp-utils.h" #include "nm-ip4-config.h" #include "nm-utils.h" #include "nm-platform.h" #include "NetworkManagerUtils.h" +#include "gsystem-local-alloc.h" #define CLIENTID_TAG "send dhcp-client-identifier" -#define CLIENTID_FORMAT CLIENTID_TAG " \"%s\"; # added by NetworkManager" -#define CLIENTID_FORMAT_OCTETS CLIENTID_TAG " %s; # added by NetworkManager" #define HOSTNAME4_TAG "send host-name" #define HOSTNAME4_FORMAT HOSTNAME4_TAG " \"%s\"; # added by NetworkManager" @@ -73,40 +73,39 @@ add_hostname (GString *str, const char *format, const char *hostname) } static void -add_ip4_config (GString *str, const char *dhcp_client_id, const char *hostname) +add_ip4_config (GString *str, GBytes *client_id, const char *hostname) { - if (dhcp_client_id && *dhcp_client_id) { - gboolean is_octets = TRUE; - int i = 0; + if (client_id) { + const char *p; + gsize l; + guint i; - while (dhcp_client_id[i]) { - if (!g_ascii_isxdigit (dhcp_client_id[i])) { - is_octets = FALSE; - break; - } - i++; - if (!dhcp_client_id[i]) - break; - if (g_ascii_isxdigit (dhcp_client_id[i])) { - i++; - if (!dhcp_client_id[i]) - break; - } - if (dhcp_client_id[i] != ':') { - is_octets = FALSE; + p = g_bytes_get_data (client_id, &l); + g_assert (p); + + /* Allow type 0 (non-hardware address) to be represented as a string + * as long as all the characters are printable. + */ + for (i = 1; (p[0] == 0) && i < l; i++) { + if (!g_ascii_isprint (p[i])) break; - } - i++; } - /* 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", dhcp_client_id); - else - g_string_append_printf (str, CLIENTID_FORMAT "\n", dhcp_client_id); + g_string_append (str, CLIENTID_TAG " "); + if (i < l) { + /* Unprintable; convert to a hex string */ + for (i = 0; i < l; i++) { + if (i > 0) + g_string_append_c (str, ':'); + g_string_append_printf (str, "%02x", (guint8) p[i]); + } + } else { + /* Printable; just add to the line minus the 'type' */ + g_string_append_c (str, '"'); + g_string_append_len (str, p + 1, l - 1); + g_string_append_c (str, '"'); + } + g_string_append (str, "; # added by NetworkManager\n"); } add_hostname (str, HOSTNAME4_FORMAT "\n", hostname); @@ -134,14 +133,67 @@ add_ip6_config (GString *str, const char *hostname) "send fqdn.server-update on;\n"); } +static GBytes * +read_client_id (const char *str) +{ + gs_free char *s = NULL; + char *p; + + g_assert (!strncmp (str, CLIENTID_TAG, STRLEN (CLIENTID_TAG))); + + str += STRLEN (CLIENTID_TAG); + while (g_ascii_isspace (*str)) + str++; + + if (*str == '"') { + s = g_strdup (str + 1); + p = strrchr (s, '"'); + if (p) + *p = '\0'; + else + return NULL; + } else + s = g_strdup (str); + + g_strchomp (s); + if (s[strlen (s) - 1] == ';') + s[strlen (s) - 1] = '\0'; + + return nm_dhcp_utils_client_id_string_to_bytes (s); +} + +GBytes * +nm_dhcp_dhclient_get_client_id_from_config_file (const char *path) +{ + gs_free char *contents = NULL; + gs_strfreev char **lines = NULL; + char **line; + + g_return_val_if_fail (path != NULL, NULL); + + if (!g_file_test (path, G_FILE_TEST_EXISTS)) + return NULL; + + if (!g_file_get_contents (path, &contents, NULL, NULL)) + return NULL; + + lines = g_strsplit_set (contents, "\n\r", 0); + for (line = lines; lines && *line; line++) { + if (!strncmp (*line, CLIENTID_TAG, STRLEN (CLIENTID_TAG))) + return read_client_id (*line); + } + return NULL; +} + char * nm_dhcp_dhclient_create_config (const char *interface, gboolean is_ip6, - const char *dhcp_client_id, + GBytes *client_id, const char *anycast_addr, const char *hostname, const char *orig_path, - const char *orig_contents) + const char *orig_contents, + GBytes **out_new_client_id) { GString *new_contents; GPtrArray *alsoreq; @@ -165,11 +217,15 @@ nm_dhcp_dhclient_create_config (const char *interface, if (!strlen (g_strstrip (p))) continue; - /* Override config file "dhcp-client-id" and use one from the - * connection. - */ - if (dhcp_client_id && !strncmp (p, CLIENTID_TAG, strlen (CLIENTID_TAG))) - continue; + if (!strncmp (p, CLIENTID_TAG, strlen (CLIENTID_TAG))) { + /* Override config file "dhcp-client-id" and use one from the connection */ + if (client_id) + continue; + + /* Otherwise capture and return the existing client id */ + if (out_new_client_id) + *out_new_client_id = read_client_id (p); + } /* Override config file hostname and use one from the connection */ if (hostname) { @@ -238,7 +294,7 @@ nm_dhcp_dhclient_create_config (const char *interface, add_also_request (alsoreq, "dhcp6.domain-search"); add_also_request (alsoreq, "dhcp6.client-id"); } else { - add_ip4_config (new_contents, dhcp_client_id, hostname); + add_ip4_config (new_contents, client_id, hostname); add_also_request (alsoreq, "rfc3442-classless-static-routes"); add_also_request (alsoreq, "ms-classless-static-routes"); add_also_request (alsoreq, "static-routes"); diff --git a/src/dhcp-manager/nm-dhcp-dhclient-utils.h b/src/dhcp-manager/nm-dhcp-dhclient-utils.h index 3b786a65a6..a1828add4a 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient-utils.h +++ b/src/dhcp-manager/nm-dhcp-dhclient-utils.h @@ -27,11 +27,12 @@ char *nm_dhcp_dhclient_create_config (const char *interface, gboolean is_ip6, - const char *dhcp_client_id, + GBytes *client_id, const char *anycast_addr, const char *hostname, const char *orig_path, - const char *orig_contents); + const char *orig_contents, + GBytes **out_new_client_id); char *nm_dhcp_dhclient_escape_duid (const GByteArray *duid); @@ -48,5 +49,7 @@ GSList *nm_dhcp_dhclient_read_lease_ip_configs (const char *iface, gboolean ipv6, GDateTime *now); +GBytes *nm_dhcp_dhclient_get_client_id_from_config_file (const char *path); + #endif /* __NETWORKMANAGER_DHCP_DHCLIENT_UTILS_H__ */ diff --git a/src/dhcp-manager/nm-dhcp-dhclient.c b/src/dhcp-manager/nm-dhcp-dhclient.c index 8f1718f8a6..76e8d68c2c 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient.c +++ b/src/dhcp-manager/nm-dhcp-dhclient.c @@ -44,6 +44,7 @@ #include "nm-posix-signals.h" #include "NetworkManagerUtils.h" #include "nm-dhcp-listener.h" +#include "gsystem-local-alloc.h" G_DEFINE_TYPE (NMDhcpDhclient, nm_dhcp_dhclient, NM_TYPE_DHCP_CLIENT) @@ -152,10 +153,11 @@ static gboolean merge_dhclient_config (const char *iface, const char *conf_file, gboolean is_ip6, - const char *dhcp_client_id, + GBytes *client_id, const char *anycast_addr, const char *hostname, const char *orig_path, + GBytes **out_new_client_id, GError **error) { char *orig = NULL, *new; @@ -174,7 +176,7 @@ merge_dhclient_config (const char *iface, } } - new = nm_dhcp_dhclient_create_config (iface, is_ip6, dhcp_client_id, anycast_addr, hostname, orig_path, orig); + new = nm_dhcp_dhclient_create_config (iface, is_ip6, client_id, anycast_addr, hostname, orig_path, orig, out_new_client_id); g_assert (new); success = g_file_set_contents (conf_file, new, -1, error); g_free (new); @@ -258,9 +260,10 @@ static char * create_dhclient_config (const char *iface, gboolean is_ip6, const char *uuid, - const char *dhcp_client_id, + GBytes *client_id, const char *dhcp_anycast_addr, - const char *hostname) + const char *hostname, + GBytes **out_new_client_id) { char *orig = NULL, *new = NULL; GError *error = NULL; @@ -285,7 +288,7 @@ create_dhclient_config (const char *iface, } error = NULL; - success = merge_dhclient_config (iface, new, is_ip6, dhcp_client_id, dhcp_anycast_addr, hostname, orig, &error); + success = merge_dhclient_config (iface, new, is_ip6, client_id, dhcp_anycast_addr, hostname, orig, out_new_client_id, &error); if (!success) { nm_log_warn (LOGD_DHCP, "(%s): error creating dhclient%s configuration: %s", iface, is_ip6 ? "6" : "", error->message); @@ -475,23 +478,28 @@ dhclient_start (NMDhcpClient *client, static gboolean ip4_start (NMDhcpClient *client, - const char *dhcp_client_id, const char *dhcp_anycast_addr, const char *hostname) { NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client); + GBytes *client_id; + gs_unref_bytes GBytes *new_client_id = NULL; const char *iface, *uuid; + gboolean success = FALSE; iface = nm_dhcp_client_get_iface (client); uuid = nm_dhcp_client_get_uuid (client); + client_id = nm_dhcp_client_get_client_id (client); - priv->conf_file = create_dhclient_config (iface, FALSE, uuid, dhcp_client_id, dhcp_anycast_addr, hostname); - if (!priv->conf_file) { + priv->conf_file = create_dhclient_config (iface, FALSE, uuid, client_id, dhcp_anycast_addr, hostname, &new_client_id); + if (priv->conf_file) { + if (new_client_id) + nm_dhcp_client_set_client_id (client, new_client_id); + success = dhclient_start (client, NULL, NULL, FALSE, NULL); + } else nm_log_warn (LOGD_DHCP4, "(%s): error creating dhclient configuration file.", iface); - return FALSE; - } - return dhclient_start (client, NULL, NULL, FALSE, NULL); + return success; } static gboolean @@ -508,7 +516,7 @@ ip6_start (NMDhcpClient *client, iface = nm_dhcp_client_get_iface (client); uuid = nm_dhcp_client_get_uuid (client); - priv->conf_file = create_dhclient_config (iface, TRUE, uuid, NULL, dhcp_anycast_addr, hostname); + priv->conf_file = create_dhclient_config (iface, TRUE, uuid, NULL, dhcp_anycast_addr, hostname, NULL); if (!priv->conf_file) { nm_log_warn (LOGD_DHCP6, "(%s): error creating dhclient6 configuration file.", iface); return FALSE; @@ -545,6 +553,24 @@ stop (NMDhcpClient *client, gboolean release, const GByteArray *duid) } } +static void +state_changed (NMDhcpClient *client, + NMDhcpState state, + GObject *ip_config, + GHashTable *options) +{ + NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client); + gs_unref_bytes GBytes *client_id = NULL; + + if (nm_dhcp_client_get_client_id (client)) + return; + if (state != NM_DHCP_STATE_BOUND) + return; + + client_id = nm_dhcp_dhclient_get_client_id_from_config_file (priv->conf_file); + nm_dhcp_client_set_client_id (client, client_id); +} + static GByteArray * get_duid (NMDhcpClient *client) { @@ -651,6 +677,7 @@ nm_dhcp_dhclient_class_init (NMDhcpDhclientClass *dhclient_class) client_class->ip6_start = ip6_start; client_class->stop = stop; client_class->get_duid = get_duid; + client_class->state_changed = state_changed; } static void __attribute__((constructor)) diff --git a/src/dhcp-manager/nm-dhcp-dhcpcd.c b/src/dhcp-manager/nm-dhcp-dhcpcd.c index c4d91b1247..5a3aee3f4d 100644 --- a/src/dhcp-manager/nm-dhcp-dhcpcd.c +++ b/src/dhcp-manager/nm-dhcp-dhcpcd.c @@ -74,7 +74,6 @@ dhcpcd_child_setup (gpointer user_data G_GNUC_UNUSED) static gboolean ip4_start (NMDhcpClient *client, - const char *dhcp_client_id, const char *dhcp_anycast_addr, const char *hostname) { diff --git a/src/dhcp-manager/nm-dhcp-systemd.c b/src/dhcp-manager/nm-dhcp-systemd.c index 6b06c45288..35b08a1d3b 100644 --- a/src/dhcp-manager/nm-dhcp-systemd.c +++ b/src/dhcp-manager/nm-dhcp-systemd.c @@ -401,6 +401,28 @@ nm_dhcp_systemd_get_lease_ip_configs (const char *iface, /************************************************************/ +static void +_save_client_id (NMDhcpSystemd *self, + uint8_t type, + const uint8_t *client_id, + size_t len) +{ + gs_unref_bytes GBytes *b = NULL; + gs_free char *buf = NULL; + + g_return_if_fail (self != NULL); + g_return_if_fail (client_id != NULL); + g_return_if_fail (len > 0); + + if (!nm_dhcp_client_get_client_id (NM_DHCP_CLIENT (self))) { + buf = g_malloc (len + 1); + buf[0] = type; + memcpy (buf + 1, client_id, len); + b = g_bytes_new (buf, len + 1); + nm_dhcp_client_set_client_id (NM_DHCP_CLIENT (self), b); + } +} + static void bound4_handle (NMDhcpSystemd *self) { @@ -428,9 +450,17 @@ bound4_handle (NMDhcpSystemd *self) TRUE, &error); if (ip4_config) { + const uint8_t *client_id = NULL; + size_t client_id_len = 0; + uint8_t type = 0; + add_requests_to_options (options, dhcp4_requests); sd_dhcp_lease_save (lease, priv->lease_file); + client_id = sd_dhcp_client_get_client_id(priv->client4, &type, &client_id_len); + if (client_id) + _save_client_id (self, type, client_id, client_id_len); + nm_dhcp_client_set_state (NM_DHCP_CLIENT (self), NM_DHCP_STATE_BOUND, G_OBJECT (ip4_config), @@ -486,7 +516,6 @@ get_arp_type (const GByteArray *hwaddr) static gboolean ip4_start (NMDhcpClient *client, - const char *dhcp_client_id, const char *dhcp_anycast_addr, const char *hostname) { @@ -494,6 +523,7 @@ ip4_start (NMDhcpClient *client, const char *iface = nm_dhcp_client_get_iface (client); const GByteArray *hwaddr; sd_dhcp_lease *lease = NULL; + GBytes *override_client_id; const uint8_t *client_id = NULL; size_t client_id_len = 0; struct in_addr last_addr; @@ -560,25 +590,25 @@ ip4_start (NMDhcpClient *client, } } - if (dhcp_client_id) { - gs_unref_bytes GBytes *b = NULL; - - b = nm_dhcp_utils_client_id_string_to_bytes (dhcp_client_id); - if (b) { - client_id = (const guint8 *) g_bytes_get_data (b, &client_id_len); - g_assert (client_id && client_id_len); - sd_dhcp_client_set_client_id (priv->client4, - client_id[0], - client_id + 1, - client_id_len - 1); - } - } else { + override_client_id = nm_dhcp_client_get_client_id (client); + if (override_client_id) { + client_id = g_bytes_get_data (override_client_id, &client_id_len); + g_assert (client_id && client_id_len); + sd_dhcp_client_set_client_id (priv->client4, + client_id[0], + client_id + 1, + client_id_len - 1); + } else if (lease) { r = sd_dhcp_lease_get_client_id (lease, &client_id, &client_id_len); if (r == 0 && client_id_len) { sd_dhcp_client_set_client_id (priv->client4, client_id[0], client_id + 1, client_id_len - 1); + _save_client_id (NM_DHCP_SYSTEMD (client), + client_id[0], + client_id + 1, + client_id_len - 1); } } diff --git a/src/dhcp-manager/tests/Makefile.am b/src/dhcp-manager/tests/Makefile.am index 1eb02d6323..9dc5dcb13d 100644 --- a/src/dhcp-manager/tests/Makefile.am +++ b/src/dhcp-manager/tests/Makefile.am @@ -20,6 +20,8 @@ noinst_PROGRAMS = \ test_dhcp_dhclient_SOURCES = \ $(top_srcdir)/src/dhcp-manager/nm-dhcp-dhclient-utils.h \ $(top_srcdir)/src/dhcp-manager/nm-dhcp-dhclient-utils.c \ + $(top_srcdir)/src/dhcp-manager/nm-dhcp-utils.h \ + $(top_srcdir)/src/dhcp-manager/nm-dhcp-utils.c \ test-dhcp-dhclient.c test_dhcp_dhclient_LDADD = \ diff --git a/src/dhcp-manager/tests/test-dhcp-dhclient.c b/src/dhcp-manager/tests/test-dhcp-dhclient.c index 2ddf6b5078..544bd89efe 100644 --- a/src/dhcp-manager/tests/test-dhcp-dhclient.c +++ b/src/dhcp-manager/tests/test-dhcp-dhclient.c @@ -23,30 +23,42 @@ #include #include +#include "gsystem-local-alloc.h" +#include "NetworkManagerUtils.h" #include "nm-dhcp-dhclient-utils.h" +#include "nm-dhcp-utils.h" #include "nm-utils.h" #include "nm-ip4-config.h" #include "nm-platform.h" -#define DEBUG 0 +#define DEBUG 1 static void test_config (const char *orig, const char *expected, const char *hostname, const char *dhcp_client_id, + GBytes *expected_new_client_id, const char *iface, const char *anycast_addr) { - char *new; + gs_free char *new = NULL; + gs_unref_bytes GBytes *client_id = NULL; + gs_unref_bytes GBytes *new_client_id = NULL; + + if (dhcp_client_id) { + client_id = nm_dhcp_utils_client_id_string_to_bytes (dhcp_client_id); + g_assert (client_id); + } new = nm_dhcp_dhclient_create_config (iface, FALSE, - dhcp_client_id, + client_id, anycast_addr, hostname, "/path/to/dhclient.conf", - orig); + orig, + &new_client_id); g_assert (new != NULL); #if DEBUG @@ -60,9 +72,13 @@ test_config (const char *orig, new, expected); } #endif - g_assert (strlen (new) == strlen (expected)); - g_assert (strcmp (new, expected) == 0); - g_free (new); + g_assert_cmpstr (new, ==, expected); + + if (expected_new_client_id) { + g_assert (new_client_id); + g_assert (g_bytes_equal (new_client_id, expected_new_client_id)); + } else + g_assert (new_client_id == NULL); } /*******************************************/ @@ -84,11 +100,7 @@ static const char *orig_missing_expected = \ static void test_orig_missing (void) { - test_config (NULL, orig_missing_expected, - NULL, - NULL, - "eth0", - NULL); + test_config (NULL, orig_missing_expected, NULL, NULL, NULL, "eth0", NULL); } /*******************************************/ @@ -119,6 +131,7 @@ test_override_client_id (void) test_config (override_client_id_orig, override_client_id_expected, NULL, "11:22:33:44:55:66", + NULL, "eth0", NULL); } @@ -147,6 +160,7 @@ test_quote_client_id (void) test_config (NULL, quote_client_id_expected, NULL, "1234", + NULL, "eth0", NULL); } @@ -175,6 +189,7 @@ test_ascii_client_id (void) test_config (NULL, ascii_client_id_expected, NULL, "qb:cd:ef:12:34:56", + NULL, "eth0", NULL); } @@ -184,7 +199,7 @@ test_ascii_client_id (void) static const char *hex_single_client_id_expected = \ "# Created by NetworkManager\n" "\n" - "send dhcp-client-identifier ab:cd:e:12:34:56; # added by NetworkManager\n" + "send dhcp-client-identifier ab:cd:0e:12:34:56; # added by NetworkManager\n" "\n" "option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;\n" "option ms-classless-static-routes code 249 = array of unsigned integer 8;\n" @@ -203,6 +218,84 @@ test_hex_single_client_id (void) test_config (NULL, hex_single_client_id_expected, NULL, "ab:cd:e:12:34:56", + NULL, + "eth0", + NULL); +} + +/*******************************************/ + +static const char *existing_hex_client_id_orig = \ + "send dhcp-client-identifier 00:30:04:20:7A:08;\n"; + +static const char *existing_hex_client_id_expected = \ + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "send dhcp-client-identifier 00:30:04:20:7A:08;\n" + "\n" + "option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;\n" + "option ms-classless-static-routes code 249 = array of unsigned integer 8;\n" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "\n"; + +static void +test_existing_hex_client_id (void) +{ + gs_unref_bytes GBytes *new_client_id = NULL; + const guint8 bytes[] = { 0x00, 0x30, 0x04,0x20, 0x7A, 0x08 }; + + new_client_id = g_bytes_new (bytes, sizeof (bytes)); + test_config (existing_hex_client_id_orig, existing_hex_client_id_expected, + NULL, + NULL, + new_client_id, + "eth0", + NULL); +} + +/*******************************************/ + +#define EACID "qb:cd:ef:12:34:56" + +static const char *existing_ascii_client_id_orig = \ + "send dhcp-client-identifier \"" EACID "\";\n"; + +static const char *existing_ascii_client_id_expected = \ + "# Created by NetworkManager\n" + "# Merged from /path/to/dhclient.conf\n" + "\n" + "send dhcp-client-identifier \"" EACID "\";\n" + "\n" + "option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;\n" + "option ms-classless-static-routes code 249 = array of unsigned integer 8;\n" + "option wpad code 252 = string;\n" + "\n" + "also request rfc3442-classless-static-routes;\n" + "also request ms-classless-static-routes;\n" + "also request static-routes;\n" + "also request wpad;\n" + "also request ntp-servers;\n" + "\n"; + +static void +test_existing_ascii_client_id (void) +{ + gs_unref_bytes GBytes *new_client_id = NULL; + char buf[STRLEN (EACID) + 1] = { 0 }; + + memcpy (buf + 1, EACID, STRLEN (EACID)); + new_client_id = g_bytes_new (buf, sizeof (buf)); + test_config (existing_ascii_client_id_orig, existing_ascii_client_id_expected, + NULL, + NULL, + new_client_id, "eth0", NULL); } @@ -235,6 +328,7 @@ test_override_hostname (void) test_config (override_hostname_orig, override_hostname_expected, "blahblah", NULL, + NULL, "eth0", NULL); } @@ -267,6 +361,7 @@ static void test_existing_alsoreq (void) { test_config (existing_alsoreq_orig, existing_alsoreq_expected, + NULL, NULL, NULL, "eth0", @@ -305,6 +400,7 @@ static void test_existing_multiline_alsoreq (void) { test_config (existing_multiline_alsoreq_orig, existing_multiline_alsoreq_expected, + NULL, NULL, NULL, "eth0", @@ -616,6 +712,8 @@ main (int argc, char **argv) g_test_add_func ("/dhcp/dhclient/quote_client_id", test_quote_client_id); g_test_add_func ("/dhcp/dhclient/ascii_client_id", test_ascii_client_id); g_test_add_func ("/dhcp/dhclient/hex_single_client_id", test_hex_single_client_id); + g_test_add_func ("/dhcp/dhclient/existing-hex-client-id", test_existing_hex_client_id); + g_test_add_func ("/dhcp/dhclient/existing-ascii-client-id", test_existing_ascii_client_id); g_test_add_func ("/dhcp/dhclient/override_hostname", test_override_hostname); g_test_add_func ("/dhcp/dhclient/existing_alsoreq", test_existing_alsoreq); g_test_add_func ("/dhcp/dhclient/existing_multiline_alsoreq", test_existing_multiline_alsoreq); -- cgit v1.2.1 From 034917e1299513ebcdd3ecbfb9d4c0adb5a641f5 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 3 Nov 2014 18:12:25 -0600 Subject: dhcp: preserve hostname for later use --- src/dhcp-manager/nm-dhcp-client.c | 18 ++++++++++++++++-- src/dhcp-manager/nm-dhcp-client.h | 6 +++--- src/dhcp-manager/nm-dhcp-dhclient.c | 11 +++++------ src/dhcp-manager/nm-dhcp-dhcpcd.c | 10 ++++------ src/dhcp-manager/nm-dhcp-systemd.c | 7 +++---- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/dhcp-manager/nm-dhcp-client.c b/src/dhcp-manager/nm-dhcp-client.c index f455efa732..69ed1120d6 100644 --- a/src/dhcp-manager/nm-dhcp-client.c +++ b/src/dhcp-manager/nm-dhcp-client.c @@ -46,6 +46,7 @@ typedef struct { guint32 timeout; GByteArray * duid; GBytes * client_id; + char * hostname; NMDhcpState state; pid_t pid; @@ -167,6 +168,14 @@ nm_dhcp_client_set_client_id (NMDhcpClient *self, GBytes *client_id) priv->client_id = client_id ? g_bytes_ref (client_id) : NULL; } +const char * +nm_dhcp_client_get_hostname (NMDhcpClient *self) +{ + g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL); + + return NM_DHCP_CLIENT_GET_PRIVATE (self)->hostname; +} + /********************************************/ static const char *state_table[NM_DHCP_STATE_MAX + 1] = { @@ -400,7 +409,10 @@ nm_dhcp_client_start_ip4 (NMDhcpClient *self, nm_dhcp_client_set_client_id (self, dhcp_client_id ? nm_dhcp_utils_client_id_string_to_bytes (dhcp_client_id) : NULL); - return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, dhcp_anycast_addr, hostname); + g_clear_pointer (&priv->hostname, g_free); + priv->hostname = g_strdup (hostname); + + return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, dhcp_anycast_addr); } /* uuid_parse does not work for machine-id, so we use our own converter */ @@ -544,6 +556,9 @@ nm_dhcp_client_start_ip6 (NMDhcpClient *self, g_free (str); } + g_clear_pointer (&priv->hostname, g_free); + priv->hostname = g_strdup (hostname); + priv->info_only = info_only; nm_log_info (LOGD_DHCP, "Activation (%s) Beginning DHCPv6 transaction (timeout in %d seconds)", @@ -551,7 +566,6 @@ nm_dhcp_client_start_ip6 (NMDhcpClient *self, return NM_DHCP_CLIENT_GET_CLASS (self)->ip6_start (self, dhcp_anycast_addr, - hostname, info_only, privacy, priv->duid); diff --git a/src/dhcp-manager/nm-dhcp-client.h b/src/dhcp-manager/nm-dhcp-client.h index fda14cf4a5..5651c52025 100644 --- a/src/dhcp-manager/nm-dhcp-client.h +++ b/src/dhcp-manager/nm-dhcp-client.h @@ -64,12 +64,10 @@ typedef struct { /* Methods */ gboolean (*ip4_start) (NMDhcpClient *self, - const char *anycast_addr, - const char *hostname); + const char *anycast_addr); gboolean (*ip6_start) (NMDhcpClient *self, const char *anycast_addr, - const char *hostname, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, const GByteArray *duid); @@ -127,6 +125,8 @@ guint32 nm_dhcp_client_get_priority (NMDhcpClient *self); GBytes *nm_dhcp_client_get_client_id (NMDhcpClient *self); +const char *nm_dhcp_client_get_hostname (NMDhcpClient *self); + gboolean nm_dhcp_client_start_ip4 (NMDhcpClient *self, const char *dhcp_client_id, const char *dhcp_anycast_addr, diff --git a/src/dhcp-manager/nm-dhcp-dhclient.c b/src/dhcp-manager/nm-dhcp-dhclient.c index 76e8d68c2c..c953c80d21 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient.c +++ b/src/dhcp-manager/nm-dhcp-dhclient.c @@ -477,19 +477,18 @@ dhclient_start (NMDhcpClient *client, } static gboolean -ip4_start (NMDhcpClient *client, - const char *dhcp_anycast_addr, - const char *hostname) +ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr) { NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client); GBytes *client_id; gs_unref_bytes GBytes *new_client_id = NULL; - const char *iface, *uuid; + const char *iface, *uuid, *hostname; gboolean success = FALSE; iface = nm_dhcp_client_get_iface (client); uuid = nm_dhcp_client_get_uuid (client); client_id = nm_dhcp_client_get_client_id (client); + hostname = nm_dhcp_client_get_hostname (client); priv->conf_file = create_dhclient_config (iface, FALSE, uuid, client_id, dhcp_anycast_addr, hostname, &new_client_id); if (priv->conf_file) { @@ -505,16 +504,16 @@ ip4_start (NMDhcpClient *client, static gboolean ip6_start (NMDhcpClient *client, const char *dhcp_anycast_addr, - const char *hostname, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, const GByteArray *duid) { NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client); - const char *iface, *uuid; + const char *iface, *uuid, *hostname; iface = nm_dhcp_client_get_iface (client); uuid = nm_dhcp_client_get_uuid (client); + hostname = nm_dhcp_client_get_hostname (client); priv->conf_file = create_dhclient_config (iface, TRUE, uuid, NULL, dhcp_anycast_addr, hostname, NULL); if (!priv->conf_file) { diff --git a/src/dhcp-manager/nm-dhcp-dhcpcd.c b/src/dhcp-manager/nm-dhcp-dhcpcd.c index 5a3aee3f4d..a39f953dbe 100644 --- a/src/dhcp-manager/nm-dhcp-dhcpcd.c +++ b/src/dhcp-manager/nm-dhcp-dhcpcd.c @@ -73,16 +73,14 @@ dhcpcd_child_setup (gpointer user_data G_GNUC_UNUSED) } static gboolean -ip4_start (NMDhcpClient *client, - const char *dhcp_anycast_addr, - const char *hostname) +ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr) { NMDhcpDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (client); GPtrArray *argv = NULL; pid_t pid = -1; GError *error = NULL; char *pid_contents = NULL, *binary_name, *cmd_str; - const char *iface, *dhcpcd_path = NULL; + const char *iface, *dhcpcd_path, *hostname; g_return_val_if_fail (priv->pid_file == NULL, FALSE); @@ -129,7 +127,8 @@ ip4_start (NMDhcpClient *client, g_ptr_array_add (argv, (gpointer) "-4"); #endif - if (hostname && strlen (hostname)) { + hostname = nm_dhcp_client_get_hostname (client); + if (hostname) { g_ptr_array_add (argv, (gpointer) "-h"); /* Send hostname to DHCP server */ g_ptr_array_add (argv, (gpointer) hostname ); } @@ -160,7 +159,6 @@ ip4_start (NMDhcpClient *client, static gboolean ip6_start (NMDhcpClient *client, const char *dhcp_anycast_addr, - const char *hostname, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, const GByteArray *duid) diff --git a/src/dhcp-manager/nm-dhcp-systemd.c b/src/dhcp-manager/nm-dhcp-systemd.c index 35b08a1d3b..6d5204c8d9 100644 --- a/src/dhcp-manager/nm-dhcp-systemd.c +++ b/src/dhcp-manager/nm-dhcp-systemd.c @@ -515,9 +515,7 @@ get_arp_type (const GByteArray *hwaddr) } static gboolean -ip4_start (NMDhcpClient *client, - const char *dhcp_anycast_addr, - const char *hostname) +ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr) { NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (client); const char *iface = nm_dhcp_client_get_iface (client); @@ -527,6 +525,7 @@ ip4_start (NMDhcpClient *client, const uint8_t *client_id = NULL; size_t client_id_len = 0; struct in_addr last_addr; + const char *hostname; int r, i; g_assert (priv->client4 == NULL); @@ -621,6 +620,7 @@ ip4_start (NMDhcpClient *client, sd_dhcp_client_set_request_option (priv->client4, dhcp4_requests[i].num); } + hostname = nm_dhcp_client_get_hostname (client); if (hostname) { r = sd_dhcp_client_set_hostname (priv->client4, hostname); if (r < 0) { @@ -683,7 +683,6 @@ dhcp6_event_cb (sd_dhcp6_client *client, int event, gpointer user_data) static gboolean ip6_start (NMDhcpClient *client, const char *dhcp_anycast_addr, - const char *hostname, gboolean info_only, NMSettingIP6ConfigPrivacy privacy, const GByteArray *duid) -- cgit v1.2.1 From 49cac9f32f5eb31698687216dd8e26b4b49629d8 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 3 Nov 2014 22:35:22 -0600 Subject: dhcp: track last IPv4 address on start for renewal Really only used by systemd because it doesn't have as good lease handling, but it's also necessary if we switch DHCP clients mid-stream (which we'll be doing later) since the new DHCP client won't have a lease file for the current IP address, and thus has nowhere to pull the current IP address from to request the same address from the DHCP server. --- src/devices/nm-device.c | 3 ++- src/dhcp-manager/nm-dhcp-client.c | 5 +++-- src/dhcp-manager/nm-dhcp-client.h | 6 ++++-- src/dhcp-manager/nm-dhcp-dhclient.c | 2 +- src/dhcp-manager/nm-dhcp-dhcpcd.c | 2 +- src/dhcp-manager/nm-dhcp-manager.c | 12 +++++++----- src/dhcp-manager/nm-dhcp-manager.h | 3 ++- src/dhcp-manager/nm-dhcp-systemd.c | 22 ++++++++++++---------- 8 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 9eb25c96aa..9d8932f18a 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -2975,7 +2975,8 @@ dhcp4_start (NMDevice *self, nm_setting_ip_config_get_dhcp_hostname (s_ip4), nm_setting_ip4_config_get_dhcp_client_id (NM_SETTING_IP4_CONFIG (s_ip4)), priv->dhcp_timeout, - priv->dhcp_anycast_address); + priv->dhcp_anycast_address, + NULL); if (tmp) g_byte_array_free (tmp, TRUE); diff --git a/src/dhcp-manager/nm-dhcp-client.c b/src/dhcp-manager/nm-dhcp-client.c index 69ed1120d6..0ffd573849 100644 --- a/src/dhcp-manager/nm-dhcp-client.c +++ b/src/dhcp-manager/nm-dhcp-client.c @@ -393,7 +393,8 @@ gboolean nm_dhcp_client_start_ip4 (NMDhcpClient *self, const char *dhcp_client_id, const char *dhcp_anycast_addr, - const char *hostname) + const char *hostname, + const char *last_ip4_address) { NMDhcpClientPrivate *priv; @@ -412,7 +413,7 @@ nm_dhcp_client_start_ip4 (NMDhcpClient *self, g_clear_pointer (&priv->hostname, g_free); priv->hostname = g_strdup (hostname); - return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, dhcp_anycast_addr); + return NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, dhcp_anycast_addr, last_ip4_address); } /* uuid_parse does not work for machine-id, so we use our own converter */ diff --git a/src/dhcp-manager/nm-dhcp-client.h b/src/dhcp-manager/nm-dhcp-client.h index 5651c52025..d732aa05ef 100644 --- a/src/dhcp-manager/nm-dhcp-client.h +++ b/src/dhcp-manager/nm-dhcp-client.h @@ -64,7 +64,8 @@ typedef struct { /* Methods */ gboolean (*ip4_start) (NMDhcpClient *self, - const char *anycast_addr); + const char *anycast_addr, + const char *last_ip4_address); gboolean (*ip6_start) (NMDhcpClient *self, const char *anycast_addr, @@ -130,7 +131,8 @@ const char *nm_dhcp_client_get_hostname (NMDhcpClient *self); gboolean nm_dhcp_client_start_ip4 (NMDhcpClient *self, const char *dhcp_client_id, const char *dhcp_anycast_addr, - const char *hostname); + const char *hostname, + const char *last_ip4_address); gboolean nm_dhcp_client_start_ip6 (NMDhcpClient *self, const char *dhcp_anycast_addr, diff --git a/src/dhcp-manager/nm-dhcp-dhclient.c b/src/dhcp-manager/nm-dhcp-dhclient.c index c953c80d21..4cabb2257b 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient.c +++ b/src/dhcp-manager/nm-dhcp-dhclient.c @@ -477,7 +477,7 @@ dhclient_start (NMDhcpClient *client, } static gboolean -ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr) +ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last_ip4_address) { NMDhcpDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client); GBytes *client_id; diff --git a/src/dhcp-manager/nm-dhcp-dhcpcd.c b/src/dhcp-manager/nm-dhcp-dhcpcd.c index a39f953dbe..9aacba7667 100644 --- a/src/dhcp-manager/nm-dhcp-dhcpcd.c +++ b/src/dhcp-manager/nm-dhcp-dhcpcd.c @@ -73,7 +73,7 @@ dhcpcd_child_setup (gpointer user_data G_GNUC_UNUSED) } static gboolean -ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr) +ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last_ip4_address) { NMDhcpDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (client); GPtrArray *argv = NULL; diff --git a/src/dhcp-manager/nm-dhcp-manager.c b/src/dhcp-manager/nm-dhcp-manager.c index dbc0684dbc..563b7ac91a 100644 --- a/src/dhcp-manager/nm-dhcp-manager.c +++ b/src/dhcp-manager/nm-dhcp-manager.c @@ -224,7 +224,8 @@ client_start (NMDhcpManager *self, const char *dhcp_anycast_addr, const char *hostname, gboolean info_only, - NMSettingIP6ConfigPrivacy privacy) + NMSettingIP6ConfigPrivacy privacy, + const char *last_ip4_address) { NMDhcpManagerPrivate *priv; NMDhcpClient *client; @@ -265,7 +266,7 @@ client_start (NMDhcpManager *self, if (ipv6) success = nm_dhcp_client_start_ip6 (client, dhcp_anycast_addr, hostname, info_only, privacy); else - success = nm_dhcp_client_start_ip4 (client, dhcp_client_id, dhcp_anycast_addr, hostname); + success = nm_dhcp_client_start_ip4 (client, dhcp_client_id, dhcp_anycast_addr, hostname, last_ip4_address); if (!success) { remove_client (self, client); @@ -296,7 +297,8 @@ nm_dhcp_manager_start_ip4 (NMDhcpManager *self, const char *dhcp_hostname, const char *dhcp_client_id, guint32 timeout, - const char *dhcp_anycast_addr) + const char *dhcp_anycast_addr, + const char *last_ip_address) { const char *hostname = NULL; @@ -306,7 +308,7 @@ nm_dhcp_manager_start_ip4 (NMDhcpManager *self, hostname = get_send_hostname (self, dhcp_hostname); return client_start (self, iface, ifindex, hwaddr, uuid, priority, FALSE, dhcp_client_id, timeout, dhcp_anycast_addr, hostname, - FALSE, 0); + FALSE, 0, last_ip_address); } /* Caller owns a reference to the NMDhcpClient on return */ @@ -332,7 +334,7 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self, hostname = get_send_hostname (self, dhcp_hostname); return client_start (self, iface, ifindex, hwaddr, uuid, priority, TRUE, NULL, timeout, dhcp_anycast_addr, hostname, info_only, - privacy); + privacy, NULL); } void diff --git a/src/dhcp-manager/nm-dhcp-manager.h b/src/dhcp-manager/nm-dhcp-manager.h index 0c3c3f2506..cf5519fbf8 100644 --- a/src/dhcp-manager/nm-dhcp-manager.h +++ b/src/dhcp-manager/nm-dhcp-manager.h @@ -61,7 +61,8 @@ NMDhcpClient * nm_dhcp_manager_start_ip4 (NMDhcpManager *manager, const char *dhcp_hostname, const char *dhcp_client_id, guint32 timeout, - const char *dhcp_anycast_addr); + const char *dhcp_anycast_addr, + const char *last_ip_address); NMDhcpClient * nm_dhcp_manager_start_ip6 (NMDhcpManager *manager, const char *iface, diff --git a/src/dhcp-manager/nm-dhcp-systemd.c b/src/dhcp-manager/nm-dhcp-systemd.c index 6d5204c8d9..3910339e5d 100644 --- a/src/dhcp-manager/nm-dhcp-systemd.c +++ b/src/dhcp-manager/nm-dhcp-systemd.c @@ -515,7 +515,7 @@ get_arp_type (const GByteArray *hwaddr) } static gboolean -ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr) +ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last_ip4_address) { NMDhcpSystemdPrivate *priv = NM_DHCP_SYSTEMD_GET_PRIVATE (client); const char *iface = nm_dhcp_client_get_iface (client); @@ -524,7 +524,7 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr) GBytes *override_client_id; const uint8_t *client_id = NULL; size_t client_id_len = 0; - struct in_addr last_addr; + struct in_addr last_addr = { 0 }; const char *hostname; int r, i; @@ -578,14 +578,16 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr) sd_dhcp_lease_load (priv->lease_file, &lease); - if (lease) { - r = sd_dhcp_lease_get_address (lease, &last_addr); - if (r == 0) { - r = sd_dhcp_client_set_request_address (priv->client4, &last_addr); - if (r < 0) { - nm_log_warn (LOGD_DHCP4, "(%s): failed to set last IPv4 address (%d)", iface, r); - goto error; - } + if (last_ip4_address) + inet_pton (AF_INET, last_ip4_address, &last_addr); + else if (lease) + sd_dhcp_lease_get_address (lease, &last_addr); + + if (last_addr.s_addr) { + r = sd_dhcp_client_set_request_address (priv->client4, &last_addr); + if (r < 0) { + nm_log_warn (LOGD_DHCP4, "(%s): failed to set last IPv4 address (%d)", iface, r); + goto error; } } -- cgit v1.2.1 From 7df18cba5bfae1ae1ccd87d574dfd2acd9c86e83 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Thu, 30 Oct 2014 18:59:10 -0500 Subject: core: add nm_utils_ip4_property_path() --- src/NetworkManagerUtils.c | 51 +++++++++++++++++++++++++++++++++++++---------- src/NetworkManagerUtils.h | 1 + 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 538295a10c..0dcd643bf3 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -2156,6 +2156,32 @@ out: } +#define IPV6_PROPERTY_DIR "/proc/sys/net/ipv6/conf/" +#define IPV4_PROPERTY_DIR "/proc/sys/net/ipv4/conf/" +G_STATIC_ASSERT (sizeof (IPV4_PROPERTY_DIR) == sizeof (IPV6_PROPERTY_DIR)); + +static const char * +_get_property_path (const char *ifname, + const char *property, + gboolean ipv6) +{ + static char path[sizeof (IPV6_PROPERTY_DIR) + IFNAMSIZ + 32]; + int len; + + ifname = ASSERT_VALID_PATH_COMPONENT (ifname); + property = ASSERT_VALID_PATH_COMPONENT (property); + + len = g_snprintf (path, + sizeof (path), + "%s%s/%s", + ipv6 ? IPV6_PROPERTY_DIR : IPV4_PROPERTY_DIR, + ifname, + property); + g_assert (len < sizeof (path) - 1); + + return path; +} + /** * nm_utils_ip6_property_path: * @ifname: an interface name @@ -2167,18 +2193,21 @@ out: const char * nm_utils_ip6_property_path (const char *ifname, const char *property) { -#define IPV6_PROPERTY_DIR "/proc/sys/net/ipv6/conf/" - static char path[sizeof (IPV6_PROPERTY_DIR) + IFNAMSIZ + 32]; - int len; - - ifname = ASSERT_VALID_PATH_COMPONENT (ifname); - property = ASSERT_VALID_PATH_COMPONENT (property); - - len = g_snprintf (path, sizeof (path), IPV6_PROPERTY_DIR "%s/%s", - ifname, property); - g_assert (len < sizeof (path) - 1); + return _get_property_path (ifname, property, TRUE); +} - return path; +/** + * nm_utils_ip4_property_path: + * @ifname: an interface name + * @property: a property name + * + * Returns the path to IPv4 property @property on @ifname. Note that + * this uses a static buffer. + */ +const char * +nm_utils_ip4_property_path (const char *ifname, const char *property) +{ + return _get_property_path (ifname, property, FALSE); } const char * diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h index 492edd133e..0ed7587a63 100644 --- a/src/NetworkManagerUtils.h +++ b/src/NetworkManagerUtils.h @@ -168,6 +168,7 @@ gint32 nm_utils_get_monotonic_timestamp_s (void); const char *ASSERT_VALID_PATH_COMPONENT (const char *name) G_GNUC_WARN_UNUSED_RESULT; const char *nm_utils_ip6_property_path (const char *ifname, const char *property); +const char *nm_utils_ip4_property_path (const char *ifname, const char *property); gboolean nm_utils_is_specific_hostname (const char *name); -- cgit v1.2.1 From a01e2ff91d3233526ce359d65be73c098573adfb Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 2 Apr 2014 12:41:04 -0500 Subject: core: add option to quit when startup is complete (rh #863515) (rh #1083683) Cloud setups often have a never-changing setup and since every cycle counts, they don't really want a management process running in the background after network setup is complete. Since it's likely a VM, it's not like links are going to go up/down very often. Add a new "configure-quit=true/false" config option which, when set to true, will quit NetworkManager after startup and initial configuration is complete. --- src/main.c | 19 +++++++++++++++++++ src/nm-config.c | 10 ++++++++++ src/nm-config.h | 1 + 3 files changed, 30 insertions(+) diff --git a/src/main.c b/src/main.c index 29cfd4bfd8..708545fd4c 100644 --- a/src/main.c +++ b/src/main.c @@ -177,6 +177,20 @@ _init_nm_debug (const char *debug) } } +static void +manager_startup_complete (NMManager *manager, GParamSpec *pspec, gpointer user_data) +{ + NMConfig *config = NM_CONFIG (user_data); + gboolean startup = FALSE; + + g_object_get (G_OBJECT (manager), NM_MANAGER_STARTUP, &startup, NULL); + + if (!startup && nm_config_get_configure_and_quit (config)) { + nm_log_info (LOGD_CORE, "quitting now that startup is complete"); + g_main_loop_quit (main_loop); + } +} + /* * main * @@ -448,6 +462,11 @@ main (int argc, char *argv[]) } } + g_signal_connect (manager, + "notify::" NM_MANAGER_STARTUP, + G_CALLBACK (manager_startup_complete), + config); + nm_manager_start (manager); /* Make sure the loopback interface is up. If interface is down, we bring diff --git a/src/nm-config.c b/src/nm-config.c index 1f41553355..26d5cb437a 100644 --- a/src/nm-config.c +++ b/src/nm-config.c @@ -61,6 +61,8 @@ typedef struct { char **no_auto_default; char **ignore_carrier; + + gboolean configure_and_quit; } NMConfigPrivate; static NMConfig *singleton = NULL; @@ -218,6 +220,12 @@ nm_config_get_connectivity_response (NMConfig *config) return NM_CONFIG_GET_PRIVATE (config)->connectivity_response; } +gboolean +nm_config_get_configure_and_quit (NMConfig *config) +{ + return NM_CONFIG_GET_PRIVATE (config)->configure_and_quit; +} + char * nm_config_get_value (NMConfig *config, const char *group, const char *key, GError **error) { @@ -623,6 +631,8 @@ nm_config_new (GError **error) priv->ignore_carrier = g_key_file_get_string_list (priv->keyfile, "main", "ignore-carrier", NULL, NULL); + priv->configure_and_quit = g_key_file_get_boolean (priv->keyfile, "main", "configure-and-quit", NULL); + return singleton; } diff --git a/src/nm-config.h b/src/nm-config.h index 27da4cbbc6..56f75fb5f6 100644 --- a/src/nm-config.h +++ b/src/nm-config.h @@ -61,6 +61,7 @@ const char *nm_config_get_debug (NMConfig *config); const char *nm_config_get_connectivity_uri (NMConfig *config); guint nm_config_get_connectivity_interval (NMConfig *config); const char *nm_config_get_connectivity_response (NMConfig *config); +gboolean nm_config_get_configure_and_quit (NMConfig *config); gboolean nm_config_get_ethernet_can_auto_default (NMConfig *config, NMDevice *device); void nm_config_set_ethernet_no_auto_default (NMConfig *config, NMDevice *device); -- cgit v1.2.1 From 9ff8c01d4aa0efe3ad943b1f10b61477d2658e0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Fri, 23 May 2014 13:24:15 +0200 Subject: man: document 'configure-and-quit' configuration option --- man/NetworkManager.conf.xml.in | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/man/NetworkManager.conf.xml.in b/man/NetworkManager.conf.xml.in index 67919cfb4f..4fb8f502c0 100644 --- a/man/NetworkManager.conf.xml.in +++ b/man/NetworkManager.conf.xml.in @@ -3,7 +3,7 @@ "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> @@ -199,6 +199,28 @@ Copyright (C) 2010 - 2013 Red Hat, Inc. + + configure-and-quit + + + When set to true, NetworkManager quits after + performing initial network configuration but spawns small helpers + to preserve DHCP leases and IPv6 addresses. This is useful in + environments where network setup is more or less static or it is + desirable to save process time but still handle some dynamic + configurations. When this option is true, + network configuration for WiFi, WWAN, Bluetooth, ADSL, and PPPoE + interfaces cannot be preserved due to their use of external + services, and these devices will be deconfigured when NetworkManager + quits even though other interface's configuration may be preserved. + The default value is false, meaning that + NetworkManager will continue running after initial network + configuration and continue responding to system and hardware events, + D-Bus requests, and user commands. + + + + dns Set the DNS (resolv.conf) processing mode. -- cgit v1.2.1 From 5149fd120d24e7622fb264fffaa4bed04eb579d6 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 29 Oct 2014 09:12:18 -0500 Subject: iface-helper: add nm-iface-helper for dynamic configure-then-quit support When quitting, the Manager asks each device to spawn the interface helper, which persists and manages dynamic address on the interface after NetworkManager is gone. If the dynamic address cannot be maintaned, the helper quits and the interface's address may be removed when their lifetime runs out. To keep the helper as simple as possible, NetworkManager passes most of the configuration on the command-line, including some properties of the device's current state, which are necessary for the helper to maintain DHCP leases or IPv6 SLAAC addresses. --- .gitignore | 1 + contrib/fedora/rpm/NetworkManager.spec | 1 + po/POTFILES.in | 1 + src/Makefile.am | 85 +++++- src/devices/nm-device.c | 167 +++++++++++ src/devices/nm-device.h | 2 + src/dhcp-manager/nm-dhcp-manager.c | 9 +- src/main.c | 24 +- src/nm-config.c | 80 ++--- src/nm-iface-helper.c | 529 +++++++++++++++++++++++++++++++++ src/nm-ip4-config.c | 4 + src/nm-ip6-config.c | 4 + src/nm-manager.c | 28 +- src/nm-manager.h | 2 + 14 files changed, 865 insertions(+), 72 deletions(-) create mode 100644 src/nm-iface-helper.c diff --git a/.gitignore b/.gitignore index d7c47e3899..8aa06cad5d 100644 --- a/.gitignore +++ b/.gitignore @@ -232,6 +232,7 @@ valgrind-*.log /src/dhcp-manager/tests/test-dhcp-options /src/dhcp-manager/tests/test-dhcp-utils /src/dnsmasq-manager/tests/test-dnsmasq-utils +/src/nm-iface-helper /src/settings/plugins/ibft/tests/test-ibft /src/settings/plugins/ifcfg-rh/tests/network-scripts/*-Test_Write_* /src/settings/plugins/ifcfg-rh/tests/network-scripts/Test_Write_* diff --git a/contrib/fedora/rpm/NetworkManager.spec b/contrib/fedora/rpm/NetworkManager.spec index 20d1a43a0a..b0c76790b9 100644 --- a/contrib/fedora/rpm/NetworkManager.spec +++ b/contrib/fedora/rpm/NetworkManager.spec @@ -518,6 +518,7 @@ fi %{_libexecdir}/nm-dhcp-helper %{_libexecdir}/nm-avahi-autoipd.action %{_libexecdir}/nm-dispatcher +%{_libexecdir}/nm-iface-helper %dir %{_libdir}/NetworkManager %{_libdir}/NetworkManager/libnm-settings-plugin*.so %{_mandir}/man1/* diff --git a/po/POTFILES.in b/po/POTFILES.in index b964b93f0e..0571f0e238 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -147,6 +147,7 @@ src/devices/wifi/nm-wifi-ap-utils.c src/devices/wimax/nm-device-wimax.c src/devices/wwan/nm-modem-broadband.c src/nm-config.c +src/nm-iface-helper.c src/nm-logging.c src/nm-manager.c src/nm-sleep-monitor-systemd.c diff --git a/src/Makefile.am b/src/Makefile.am index e186632548..eb5c64c089 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,7 +48,10 @@ AM_CPPFLAGS = \ # primarily for its side effect of removing duplicates. AM_CPPFLAGS += $(foreach d,$(sort $(dir $(libNetworkManager_la_SOURCES))),-I$(top_srcdir)/src/$d) -noinst_LTLIBRARIES = libNetworkManager.la libsystemd-dhcp.la +noinst_LTLIBRARIES = \ + libNetworkManager.la \ + libnm-iface-helper.la \ + libsystemd-dhcp.la ###################### # libsystemd-dhcp @@ -458,6 +461,86 @@ NetworkManager_LDFLAGS = -rdynamic ###################### +libnm_iface_helper_la_SOURCES = \ + dhcp-manager/nm-dhcp-client.c \ + dhcp-manager/nm-dhcp-client.h \ + dhcp-manager/nm-dhcp-utils.c \ + dhcp-manager/nm-dhcp-utils.h \ + dhcp-manager/nm-dhcp-manager.c \ + dhcp-manager/nm-dhcp-manager.h \ + \ + platform/nm-linux-platform.c \ + platform/nm-linux-platform.h \ + platform/nm-platform.c \ + platform/nm-platform.h \ + platform/wifi/wifi-utils-nl80211.c \ + platform/wifi/wifi-utils-nl80211.h \ + platform/wifi/wifi-utils-private.h \ + platform/wifi/wifi-utils.c \ + platform/wifi/wifi-utils.h \ + \ + rdisc/nm-fake-rdisc.c \ + rdisc/nm-fake-rdisc.h \ + rdisc/nm-lndp-rdisc.c \ + rdisc/nm-lndp-rdisc.h \ + rdisc/nm-rdisc.c \ + rdisc/nm-rdisc.h \ + \ + nm-ip4-config.c \ + nm-ip4-config.h \ + nm-ip6-config.c \ + nm-ip6-config.h \ + \ + nm-enum-types.c \ + nm-enum-types.h \ + nm-logging.c \ + nm-logging.h \ + nm-posix-signals.c \ + nm-posix-signals.h \ + NetworkManagerUtils.c \ + NetworkManagerUtils.h + +if WITH_WEXT +libnm_iface_helper_la_SOURCES += \ + platform/wifi/wifi-utils-wext.c \ + platform/wifi/wifi-utils-wext.h +endif + +libnm_iface_helper_la_LIBADD = \ + $(top_builddir)/libnm-core/libnm-core.la \ + libsystemd-dhcp.la \ + $(DBUS_LIBS) \ + $(GLIB_LIBS) \ + $(GUDEV_LIBS) \ + $(LIBNL_LIBS) \ + $(LIBNDP_LIBS) \ + $(LIBDL) \ + $(LIBM) + +libexec_PROGRAMS = nm-iface-helper + +nm_iface_helper_SOURCES = \ + dhcp-manager/nm-dhcp-systemd.h \ + dhcp-manager/nm-dhcp-systemd.c \ + nm-iface-helper.c \ + main-utils.c \ + main-utils.h + +nm_iface_helper_LDADD = \ + $(top_builddir)/libnm-core/libnm-core.la \ + libsystemd-dhcp.la \ + libnm-iface-helper.la \ + $(DBUS_LIBS) \ + $(GLIB_LIBS) \ + $(GUDEV_LIBS) \ + $(LIBNL_LIBS) \ + $(LIBNDP_LIBS) \ + $(LIBM) + +nm_iface_helper_LDFLAGS = -rdynamic + +###################### + dbusservicedir = $(DBUS_SYS_DIR) dbusservice_DATA = org.freedesktop.NetworkManager.conf diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 9d8932f18a..3390bf765e 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -6990,6 +6990,173 @@ nm_device_cleanup (NMDevice *self, NMDeviceStateReason reason) _cleanup_generic_post (self, TRUE); } +static char * +bin2hexstr (const char *bytes, gsize len) +{ + GString *str; + int i; + + g_return_val_if_fail (bytes != NULL, NULL); + g_return_val_if_fail (len > 0, NULL); + + str = g_string_sized_new (len * 2 + 1); + for (i = 0; i < len; i++) { + if (str->len) + g_string_append_c (str, ':'); + g_string_append_printf (str, "%02x", (guint8) bytes[i]); + } + return g_string_free (str, FALSE); +} + +static char * +find_dhcp4_address (NMDevice *self) +{ + NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); + guint i, n; + + if (!priv->ip4_config) + return NULL; + + n = nm_ip4_config_get_num_addresses (priv->ip4_config); + for (i = 0; i < n; i++) { + const NMPlatformIP4Address *a = nm_ip4_config_get_address (priv->ip4_config, i); + + if (a->source == NM_IP_CONFIG_SOURCE_DHCP) + return g_strdup (nm_utils_inet4_ntop (a->address, NULL)); + } + return NULL; +} + +void +nm_device_spawn_iface_helper (NMDevice *self) +{ + NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); + gboolean priority_set = FALSE, configured = FALSE; + NMConnection *connection; + GError *error = NULL; + const char *method; + GPtrArray *argv; + gs_free char *dhcp4_address = NULL; + + if (priv->state != NM_DEVICE_STATE_ACTIVATED) + return; + if (!nm_device_can_assume_connections (self)) + return; + + connection = nm_device_get_connection (self); + g_assert (connection); + + argv = g_ptr_array_sized_new (10); + g_ptr_array_set_free_func (argv, g_free); + + g_ptr_array_add (argv, g_strdup (LIBEXECDIR "/nm-iface-helper")); + g_ptr_array_add (argv, g_strdup ("--ifname")); + g_ptr_array_add (argv, g_strdup (nm_device_get_ip_iface (self))); + g_ptr_array_add (argv, g_strdup ("--uuid")); + g_ptr_array_add (argv, g_strdup (nm_connection_get_uuid (connection))); + + dhcp4_address = find_dhcp4_address (self); + + method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); + if ( priv->ip4_config + && priv->ip4_state == IP_DONE + && g_strcmp0 (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO) == 0 + && priv->dhcp4_client + && dhcp4_address) { + NMSettingIPConfig *s_ip4; + GBytes *client_id; + char *hex_client_id; + const char *hostname; + + s_ip4 = nm_connection_get_setting_ip4_config (connection); + g_assert (s_ip4); + + g_ptr_array_add (argv, g_strdup ("--priority")); + g_ptr_array_add (argv, g_strdup_printf ("%u", nm_dhcp_client_get_priority (priv->dhcp4_client))); + priority_set = TRUE; + + g_ptr_array_add (argv, g_strdup ("--dhcp4")); + g_ptr_array_add (argv, g_strdup (dhcp4_address)); + if (nm_setting_ip_config_get_may_fail (s_ip4) == FALSE) + g_ptr_array_add (argv, g_strdup ("--dhcp4-required")); + + client_id = nm_dhcp_client_get_client_id (priv->dhcp4_client); + if (client_id) { + g_ptr_array_add (argv, g_strdup ("--dhcp4-clientid")); + hex_client_id = bin2hexstr (g_bytes_get_data (client_id, NULL), + g_bytes_get_size (client_id)); + g_ptr_array_add (argv, hex_client_id); + } + + hostname = nm_dhcp_client_get_hostname (priv->dhcp4_client); + if (client_id) { + g_ptr_array_add (argv, g_strdup ("--dhcp4-hostname")); + g_ptr_array_add (argv, g_strdup (hostname)); + } + + configured = TRUE; + } + + method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP6_CONFIG); + if ( priv->ip6_config + && priv->ip6_state == IP_DONE + && g_strcmp0 (method, NM_SETTING_IP6_CONFIG_METHOD_AUTO) == 0 + && priv->rdisc + && priv->ac_ip6_config) { + NMSettingIPConfig *s_ip6; + char *hex_iid; + NMUtilsIPv6IfaceId iid = NM_UTILS_IPV6_IFACE_ID_INIT; + + s_ip6 = nm_connection_get_setting_ip6_config (connection); + g_assert (s_ip6); + + g_ptr_array_add (argv, g_strdup ("--slaac")); + + if (nm_setting_ip_config_get_may_fail (s_ip6) == FALSE) + g_ptr_array_add (argv, g_strdup ("--slaac-required")); + + g_ptr_array_add (argv, g_strdup ("--slaac-tempaddr")); + g_ptr_array_add (argv, g_strdup_printf ("%d", priv->rdisc_use_tempaddr)); + + if (nm_device_get_ip_iface_identifier (self, &iid)) { + g_ptr_array_add (argv, g_strdup ("--iid")); + hex_iid = bin2hexstr ((const char *) iid.id_u8, sizeof (NMUtilsIPv6IfaceId)); + g_ptr_array_add (argv, hex_iid); + } + + configured = TRUE; + } + + if (configured) { + GPid pid; + + if (!priority_set) { + g_ptr_array_add (argv, g_strdup ("--priority")); + g_ptr_array_add (argv, g_strdup_printf ("%u", nm_device_get_priority (self))); + } + + g_ptr_array_add (argv, NULL); + + if (nm_logging_enabled (LOGL_DEBUG, LOGD_DEVICE)) { + char *tmp; + + tmp = g_strjoinv (" ", (char **) argv->pdata); + _LOGD (LOGD_DEVICE, "running '%s'", tmp); + g_free (tmp); + } + + if (g_spawn_async (NULL, (char **) argv->pdata, NULL, + G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &error)) { + _LOGI (LOGD_DEVICE, "spawned helper PID %u", (guint) pid); + } else { + _LOGW (LOGD_DEVICE, "failed to spawn helper: %s", error->message); + g_error_free (error); + } + } + + g_ptr_array_unref (argv); +} + /***********************************************************/ static gboolean diff --git a/src/devices/nm-device.h b/src/devices/nm-device.h index f7b91e00cc..840a19c08b 100644 --- a/src/devices/nm-device.h +++ b/src/devices/nm-device.h @@ -365,6 +365,8 @@ NMConnection *nm_device_new_default_connection (NMDevice *self); const NMPlatformIP4Route *nm_device_get_ip4_default_route (NMDevice *self); const NMPlatformIP6Route *nm_device_get_ip6_default_route (NMDevice *self); +void nm_device_spawn_iface_helper (NMDevice *self); + G_END_DECLS /* For testing only */ diff --git a/src/dhcp-manager/nm-dhcp-manager.c b/src/dhcp-manager/nm-dhcp-manager.c index 563b7ac91a..3147980b4b 100644 --- a/src/dhcp-manager/nm-dhcp-manager.c +++ b/src/dhcp-manager/nm-dhcp-manager.c @@ -386,11 +386,18 @@ static void nm_dhcp_manager_init (NMDhcpManager *self) { NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self); + NMConfig *config = nm_config_get (); const char *client; GError *error = NULL; /* Client-specific setup */ - client = nm_config_get_dhcp_client (nm_config_get ()); + client = nm_config_get_dhcp_client (config); + if (nm_config_get_configure_and_quit (config)) { + if (g_strcmp0 (client, "internal") != 0) + nm_log_warn (LOGD_DHCP, "Using internal DHCP client since configure-and-quit is set."); + client = "internal"; + } + priv->client_type = get_client_type (client, &error); if (priv->client_type == G_TYPE_INVALID) { nm_log_warn (LOGD_DHCP, "No usable DHCP client found (%s)! DHCP configurations will fail.", diff --git a/src/main.c b/src/main.c index 708545fd4c..476d2a7711 100644 --- a/src/main.c +++ b/src/main.c @@ -178,17 +178,10 @@ _init_nm_debug (const char *debug) } static void -manager_startup_complete (NMManager *manager, GParamSpec *pspec, gpointer user_data) +manager_configure_quit (NMManager *manager, gpointer user_data) { - NMConfig *config = NM_CONFIG (user_data); - gboolean startup = FALSE; - - g_object_get (G_OBJECT (manager), NM_MANAGER_STARTUP, &startup, NULL); - - if (!startup && nm_config_get_configure_and_quit (config)) { - nm_log_info (LOGD_CORE, "quitting now that startup is complete"); - g_main_loop_quit (main_loop); - } + nm_log_info (LOGD_CORE, "quitting now that startup is complete"); + g_main_loop_quit (main_loop); } /* @@ -462,10 +455,7 @@ main (int argc, char *argv[]) } } - g_signal_connect (manager, - "notify::" NM_MANAGER_STARTUP, - G_CALLBACK (manager_startup_complete), - config); + g_signal_connect (manager, NM_MANAGER_CONFIGURE_QUIT, G_CALLBACK (manager_configure_quit), config); nm_manager_start (manager); @@ -485,10 +475,10 @@ main (int argc, char *argv[]) success = TRUE; /* Told to quit before getting to the mainloop by the signal handler */ - if (quit_early == TRUE) - goto done; + if (!quit_early) + g_main_loop_run (main_loop); - g_main_loop_run (main_loop); + nm_manager_stop (manager); done: g_clear_object (&manager); diff --git a/src/nm-config.c b/src/nm-config.c index 26d5cb437a..aba4fa1a1b 100644 --- a/src/nm-config.c +++ b/src/nm-config.c @@ -74,41 +74,36 @@ G_DEFINE_TYPE (NMConfig, nm_config, G_TYPE_OBJECT) /************************************************************************/ static gboolean -_parse_bool_str (const char *str, gboolean *out_value) +_get_bool_value (GKeyFile *keyfile, + const char *section, + const char *key, + gboolean default_value) { - gboolean value; - gsize len; - char *s = NULL; - - g_return_val_if_fail (str, FALSE); - - while (g_ascii_isspace (*str)) - str++; - - if (!*str) - return FALSE; - - len = strlen (str); - - if (g_ascii_isspace (str[len-1])) { - str = s = g_strdup (str); - g_strchomp (s); - } - - if (!g_ascii_strcasecmp (str, "true") || !g_ascii_strcasecmp (str, "yes") || !g_ascii_strcasecmp (str, "on") || !g_ascii_strcasecmp (str, "1")) - value = TRUE; - else if (!g_ascii_strcasecmp (str, "false") || !g_ascii_strcasecmp (str, "no") || !g_ascii_strcasecmp (str, "off") || !g_ascii_strcasecmp (str, "0")) - value = FALSE; - else { - g_free (s); - return FALSE; + gboolean value = default_value; + char *str; + + g_return_val_if_fail (keyfile != NULL, default_value); + g_return_val_if_fail (section != NULL, default_value); + g_return_val_if_fail (key != NULL, default_value); + + str = g_key_file_get_value (keyfile, section, key, NULL); + if (!str) + return default_value; + + g_strstrip (str); + if (str[0]) { + if (!g_ascii_strcasecmp (str, "true") || !g_ascii_strcasecmp (str, "yes") || !g_ascii_strcasecmp (str, "on") || !g_ascii_strcasecmp (str, "1")) + value = TRUE; + else if (!g_ascii_strcasecmp (str, "false") || !g_ascii_strcasecmp (str, "no") || !g_ascii_strcasecmp (str, "off") || !g_ascii_strcasecmp (str, "0")) + value = FALSE; + else { + nm_log_warn (LOGD_CORE, "Unrecognized value for %s.%s: '%s'. Assuming '%s'", + section, key, str, default_value ? "true" : "false"); + } } - if (out_value) - *out_value = value; - - g_free (s); - return TRUE; + g_free (str); + return value; } /************************************************************************/ @@ -521,7 +516,6 @@ nm_config_new (GError **error) GFileInfo *info; GPtrArray *confs; const char *name; - char *value; int i; GString *config_description; @@ -591,23 +585,9 @@ nm_config_new (GError **error) if (!priv->plugins && STRLEN (CONFIG_PLUGINS_DEFAULT) > 0) priv->plugins = g_strsplit (CONFIG_PLUGINS_DEFAULT, ",", -1); - value = g_key_file_get_value (priv->keyfile, "main", "monitor-connection-files", NULL); - priv->monitor_connection_files = FALSE; - if (value) { - if (!_parse_bool_str (value, &priv->monitor_connection_files)) - nm_log_warn (LOGD_CORE, "Unrecognized value for main.monitor-connection-files: %s. Assuming 'false'", value); - g_free (value); - } + priv->monitor_connection_files = _get_bool_value (priv->keyfile, "main", "monitor-connection-files", FALSE); - value = g_key_file_get_value (priv->keyfile, "main", "auth-polkit", NULL); - priv->auth_polkit = NM_CONFIG_DEFAULT_AUTH_POLKIT; - if (value) { - if (!_parse_bool_str (value, &priv->auth_polkit)) { - nm_log_warn (LOGD_CORE, "Unrecognized value for main.auth-polkit: %s. Assuming '%s'", value, - NM_CONFIG_DEFAULT_AUTH_POLKIT ? "true" : "false"); - } - g_free (value); - } + priv->auth_polkit = _get_bool_value (priv->keyfile, "main", "auth-polkit", NM_CONFIG_DEFAULT_AUTH_POLKIT); priv->dhcp_client = g_key_file_get_value (priv->keyfile, "main", "dhcp", NULL); priv->dns_mode = g_key_file_get_value (priv->keyfile, "main", "dns", NULL); @@ -631,7 +611,7 @@ nm_config_new (GError **error) priv->ignore_carrier = g_key_file_get_string_list (priv->keyfile, "main", "ignore-carrier", NULL, NULL); - priv->configure_and_quit = g_key_file_get_boolean (priv->keyfile, "main", "configure-and-quit", NULL); + priv->configure_and_quit = _get_bool_value (priv->keyfile, "main", "configure-and-quit", FALSE); return singleton; } diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c new file mode 100644 index 0000000000..62f1b2aba4 --- /dev/null +++ b/src/nm-iface-helper.c @@ -0,0 +1,529 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2014 Red Hat, Inc. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "gsystem-local-alloc.h" +#include "NetworkManagerUtils.h" +#include "nm-linux-platform.h" +#include "nm-dhcp-manager.h" +#include "nm-logging.h" +#include "main-utils.h" +#include "nm-rdisc.h" +#include "nm-lndp-rdisc.h" +#include "nm-utils.h" + +#if !defined(NM_DIST_VERSION) +# define NM_DIST_VERSION VERSION +#endif + +#define NMIH_PID_FILE_FMT NMRUNDIR "/nm-iface-helper-%d.pid" + +static GMainLoop *main_loop = NULL; +static char *ifname = NULL; +static int ifindex = -1; +static gboolean slaac_required = FALSE; +static gboolean dhcp4_required = FALSE; +static int tempaddr = NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN; +static int priority = -1; + +static void +dhcp4_state_changed (NMDhcpClient *client, + NMDhcpState state, + NMIP4Config *ip4_config, + GHashTable *options, + gpointer user_data) +{ + static NMIP4Config *last_config = NULL; + NMIP4Config *existing; + + g_return_if_fail (!ip4_config || NM_IS_IP4_CONFIG (ip4_config)); + + nm_log_dbg (LOGD_DHCP4, "(%s): new DHCPv4 client state %d", ifname, state); + + switch (state) { + case NM_DHCP_STATE_BOUND: + g_assert (ip4_config); + existing = nm_ip4_config_capture (ifindex, FALSE); + if (last_config) + nm_ip4_config_subtract (existing, last_config); + + nm_ip4_config_merge (existing, ip4_config); + if (!nm_ip4_config_commit (existing, ifindex)) + nm_log_warn (LOGD_DHCP4, "(%s): failed to apply DHCPv4 config", ifname); + + if (last_config) { + g_object_unref (last_config); + last_config = nm_ip4_config_new (); + nm_ip4_config_replace (last_config, ip4_config, NULL); + } + break; + case NM_DHCP_STATE_TIMEOUT: + case NM_DHCP_STATE_DONE: + case NM_DHCP_STATE_FAIL: + if (dhcp4_required) { + nm_log_warn (LOGD_DHCP4, "(%s): DHCPv4 timed out or failed, quitting...", ifname); + g_main_loop_quit (main_loop); + } else + nm_log_warn (LOGD_DHCP4, "(%s): DHCPv4 timed out or failed", ifname); + break; + default: + break; + } +} + +static void +rdisc_config_changed (NMRDisc *rdisc, NMRDiscConfigMap changed, gpointer user_data) +{ + static NMIP6Config *last_config = NULL; + NMIP6Config *existing; + NMIP6Config *ip6_config; + static int system_support = -1; + guint ifa_flags = 0x00; + int i; + + if (system_support == -1) { + /* + * Check, if both libnl and the kernel are recent enough, + * to help user space handling RA. If it's not supported, + * we have no ipv6-privacy and must add autoconf addresses + * as /128. The reason for the /128 is to prevent the kernel + * from adding a prefix route for this address. + **/ + system_support = nm_platform_check_support_libnl_extended_ifa_flags () && + nm_platform_check_support_kernel_extended_ifa_flags (); + } + + if (system_support) + ifa_flags = IFA_F_NOPREFIXROUTE; + if (tempaddr == NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR + || tempaddr == NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_PUBLIC_ADDR) + { + /* without system_support, this flag will be ignored. Still set it, doesn't seem to do any harm. */ + ifa_flags |= IFA_F_MANAGETEMPADDR; + } + + ip6_config = nm_ip6_config_new (); + + if (changed & NM_RDISC_CONFIG_GATEWAYS) { + /* Use the first gateway as ordered in router discovery cache. */ + if (rdisc->gateways->len) { + NMRDiscGateway *gateway = &g_array_index (rdisc->gateways, NMRDiscGateway, 0); + + nm_ip6_config_set_gateway (ip6_config, &gateway->address); + } else + nm_ip6_config_set_gateway (ip6_config, NULL); + } + + if (changed & NM_RDISC_CONFIG_ADDRESSES) { + /* Rebuild address list from router discovery cache. */ + nm_ip6_config_reset_addresses (ip6_config); + + /* rdisc->addresses contains at most max_addresses entries. + * This is different from what the kernel does, which + * also counts static and temporary addresses when checking + * max_addresses. + **/ + for (i = 0; i < rdisc->addresses->len; i++) { + NMRDiscAddress *discovered_address = &g_array_index (rdisc->addresses, NMRDiscAddress, i); + NMPlatformIP6Address address; + + memset (&address, 0, sizeof (address)); + address.address = discovered_address->address; + address.plen = system_support ? 64 : 128; + address.timestamp = discovered_address->timestamp; + address.lifetime = discovered_address->lifetime; + address.preferred = discovered_address->preferred; + if (address.preferred > address.lifetime) + address.preferred = address.lifetime; + address.source = NM_IP_CONFIG_SOURCE_RDISC; + address.flags = ifa_flags; + + nm_ip6_config_add_address (ip6_config, &address); + } + } + + if (changed & NM_RDISC_CONFIG_ROUTES) { + /* Rebuild route list from router discovery cache. */ + nm_ip6_config_reset_routes (ip6_config); + + for (i = 0; i < rdisc->routes->len; i++) { + NMRDiscRoute *discovered_route = &g_array_index (rdisc->routes, NMRDiscRoute, i); + NMPlatformIP6Route route; + + /* Only accept non-default routes. The router has no idea what the + * local configuration or user preferences are, so sending routes + * with a prefix length of 0 is quite rude and thus ignored. + */ + if (discovered_route->plen > 0) { + memset (&route, 0, sizeof (route)); + route.network = discovered_route->network; + route.plen = discovered_route->plen; + route.gateway = discovered_route->gateway; + route.source = NM_IP_CONFIG_SOURCE_RDISC; + route.metric = priority; + + nm_ip6_config_add_route (ip6_config, &route); + } + } + } + + if (changed & NM_RDISC_CONFIG_DHCP_LEVEL) { + /* Unsupported until systemd DHCPv6 is ready */ + } + + /* hop_limit == 0 is a special value "unspecified", so do not touch + * in this case */ + if (changed & NM_RDISC_CONFIG_HOP_LIMIT && rdisc->hop_limit > 0) { + char val[16]; + + g_snprintf (val, sizeof (val), "%d", rdisc->hop_limit); + nm_platform_sysctl_set (nm_utils_ip6_property_path (ifname, "hop_limit"), val); + } + + if (changed & NM_RDISC_CONFIG_MTU) { + char val[16]; + + g_snprintf (val, sizeof (val), "%d", rdisc->mtu); + nm_platform_sysctl_set (nm_utils_ip6_property_path (ifname, "mtu"), val); + } + + existing = nm_ip6_config_capture (ifindex, FALSE, tempaddr); + if (last_config) + nm_ip6_config_subtract (existing, last_config); + + nm_ip6_config_merge (existing, ip6_config); + if (!nm_ip6_config_commit (existing, ifindex)) + nm_log_warn (LOGD_IP6, "(%s): failed to apply IPv6 config", ifname); + + if (last_config) { + g_object_unref (last_config); + last_config = nm_ip6_config_new (); + nm_ip6_config_replace (last_config, ip6_config, NULL); + } +} + +static void +rdisc_ra_timeout (NMRDisc *rdisc, gpointer user_data) +{ + if (slaac_required) { + nm_log_warn (LOGD_IP6, "(%s): IPv6 timed out or failed, quitting...", ifname); + g_main_loop_quit (main_loop); + } else + nm_log_warn (LOGD_IP6, "(%s): IPv6 timed out or failed", ifname); +} + +static gboolean +quit_handler (gpointer user_data) +{ + gboolean *quit_early_ptr = user_data; + + *quit_early_ptr = TRUE; + g_main_loop_quit (main_loop); + return G_SOURCE_CONTINUE; +} + +static void +setup_signals (gboolean *quit_early_ptr) +{ + sigset_t sigmask; + + sigemptyset (&sigmask); + pthread_sigmask (SIG_SETMASK, &sigmask, NULL); + + signal (SIGPIPE, SIG_IGN); + g_unix_signal_add (SIGINT, quit_handler, quit_early_ptr); + g_unix_signal_add (SIGTERM, quit_handler, quit_early_ptr); +} + +int +main (int argc, char *argv[]) +{ + char *opt_log_level = NULL; + char *opt_log_domains = NULL; + gboolean debug = FALSE, g_fatal_warnings = FALSE, become_daemon = FALSE; + gboolean show_version = FALSE, slaac = FALSE; + char *bad_domains = NULL, *dhcp4_hostname = NULL, *uuid = NULL; + char *iid_str = NULL, *dhcp4_clientid = NULL, *dhcp4_address = NULL; + gs_unref_object NMDhcpManager *dhcp_mgr = NULL; + GError *error = NULL; + gboolean wrote_pidfile = FALSE; + gs_free char *pidfile = NULL; + gboolean quit_early = FALSE; + gs_unref_object NMDhcpClient *dhcp4_client = NULL; + gs_unref_object NMRDisc *rdisc = NULL; + GByteArray *hwaddr = NULL; + size_t hwaddr_len = 0; + gconstpointer tmp; + gs_free NMUtilsIPv6IfaceId *iid = NULL; + + GOptionEntry options[] = { + /* Interface/IP config */ + { "ifname", 'i', 0, G_OPTION_ARG_STRING, &ifname, N_("The interface to manage"), N_("eth0") }, + { "uuid", 'u', 0, G_OPTION_ARG_STRING, &uuid, N_("Connection UUID"), N_("661e8cd0-b618-46b8-9dc9-31a52baaa16b") }, + { "slaac", 's', 0, G_OPTION_ARG_NONE, &slaac, N_("Whether to manage IPv6 SLAAC"), NULL }, + { "slaac-required", '6', 0, G_OPTION_ARG_NONE, &slaac_required, N_("Whether SLAAC must be successful"), NULL }, + { "slaac-tempaddr", 't', 0, G_OPTION_ARG_INT, &tempaddr, N_("Use an IPv6 temporary privacy address"), NULL }, + { "dhcp4", 'd', 0, G_OPTION_ARG_STRING, &dhcp4_address, N_("Current DHCPv4 address"), NULL }, + { "dhcp4-required", '4', 0, G_OPTION_ARG_NONE, &dhcp4_required, N_("Whether DHCPv4 must be successful"), NULL }, + { "dhcp4-clientid", 'c', 0, G_OPTION_ARG_STRING, &dhcp4_clientid, N_("Hex-encoded DHCPv4 client ID"), NULL }, + { "dhcp4-hostname", 'h', 0, G_OPTION_ARG_STRING, &dhcp4_hostname, N_("Hostname to send to DHCP server"), N_("barbar") }, + { "priority", 'p', 0, G_OPTION_ARG_INT, &priority, N_("Route priority"), N_("10") }, + { "iid", 'e', 0, G_OPTION_ARG_STRING, &iid_str, N_("Hex-encoded Interface Identifier"), N_("") }, + + /* Logging/debugging */ + { "version", 'V', 0, G_OPTION_ARG_NONE, &show_version, N_("Print NetworkManager version and exit"), NULL }, + { "no-daemon", 'n', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &become_daemon, N_("Don't become a daemon"), NULL }, + { "debug", 'b', 0, G_OPTION_ARG_NONE, &debug, N_("Don't become a daemon, and log to stderr"), NULL }, + { "log-level", 0, 0, G_OPTION_ARG_STRING, &opt_log_level, N_("Log level: one of [%s]"), "INFO" }, + { "log-domains", 0, 0, G_OPTION_ARG_STRING, &opt_log_domains, + N_("Log domains separated by ',': any combination of [%s]"), + "PLATFORM,RFKILL,WIFI" }, + { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, N_("Make all warnings fatal"), NULL }, + {NULL} + }; + + setpgid (getpid (), getpid ()); + + if (!nm_main_utils_early_setup ("nm-iface-helper", + &argv, + &argc, + options, + NULL, + _("nm-iface-helper is a small, standalone process that manages a single network interface."))) + exit (1); + + if (show_version) { + fprintf (stdout, NM_DIST_VERSION "\n"); + exit (0); + } + + if (!ifname || !uuid) { + fprintf (stderr, _("An interface name and UUID are required\n")); + exit (1); + } + + if (!nm_logging_setup (opt_log_level, + opt_log_domains, + &bad_domains, + &error)) { + fprintf (stderr, + _("%s. Please use --help to see a list of valid options.\n"), + error->message); + exit (1); + } else if (bad_domains) { + fprintf (stderr, + _("Ignoring unrecognized log domain(s) '%s' passed on command line.\n"), + bad_domains); + g_clear_pointer (&bad_domains, g_free); + } + + pidfile = g_strdup_printf (NMIH_PID_FILE_FMT, ifindex); + g_assert (pidfile); + + /* check pid file */ + if (nm_main_utils_check_pidfile (pidfile, "nm-iface-helper")) + exit (1); + + if (become_daemon && !debug) { + if (daemon (0, 0) < 0) { + int saved_errno; + + saved_errno = errno; + fprintf (stderr, _("Could not daemonize: %s [error %u]\n"), + g_strerror (saved_errno), + saved_errno); + exit (1); + } + if (nm_main_utils_write_pidfile (pidfile)) + wrote_pidfile = TRUE; + } + + /* Set up unix signal handling - before creating threads, but after daemonizing! */ + main_loop = g_main_loop_new (NULL, FALSE); + setup_signals (&quit_early); + + if (g_fatal_warnings) { + GLogLevelFlags fatal_mask; + + fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); + fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; + g_log_set_always_fatal (fatal_mask); + } + + nm_logging_syslog_openlog (debug); + +#if !GLIB_CHECK_VERSION (2, 35, 0) + g_type_init (); +#endif + + nm_log_info (LOGD_CORE, "nm-iface-helper (version " NM_DIST_VERSION ") is starting..."); + + /* Set up platform interaction layer */ + nm_linux_platform_setup (); + + ifindex = nm_platform_link_get_ifindex (ifname); + if (ifindex <= 0) { + fprintf (stderr, _("Failed to find interface index for %s\n"), ifname); + exit (1); + } + + tmp = nm_platform_link_get_address (ifindex, &hwaddr_len); + if (tmp) { + hwaddr = g_byte_array_sized_new (hwaddr_len); + g_byte_array_append (hwaddr, tmp, hwaddr_len); + } + + if (iid_str) { + GBytes *bytes; + gsize ignored = 0; + + bytes = nm_utils_hexstr2bin (iid_str); + if (!bytes || g_bytes_get_size (bytes) != sizeof (*iid)) { + fprintf (stderr, _("(%s): Invalid IID %s\n"), ifname, iid_str); + exit (1); + } + iid = g_bytes_unref_to_data (bytes, &ignored); + } + + priority = MAX (0, priority); + + if (dhcp4_address) { + nm_platform_sysctl_set (nm_utils_ip4_property_path (ifname, "promote_secondaries"), "1"); + + /* Initialize DHCP manager */ + dhcp_mgr = nm_dhcp_manager_get (); + g_assert (dhcp_mgr != NULL); + + dhcp4_client = nm_dhcp_manager_start_ip4 (dhcp_mgr, + ifname, + ifindex, + hwaddr, + uuid, + priority, + !!dhcp4_hostname, + dhcp4_hostname, + dhcp4_clientid, + 45, + NULL, + dhcp4_address); + g_assert (dhcp4_client); + g_signal_connect (dhcp4_client, + NM_DHCP_CLIENT_SIGNAL_STATE_CHANGED, + G_CALLBACK (dhcp4_state_changed), + NULL); + } + + if (slaac) { + nm_platform_link_set_user_ipv6ll_enabled (ifindex, TRUE); + + rdisc = nm_lndp_rdisc_new (ifindex, ifname); + g_assert (rdisc); + + if (iid) + nm_rdisc_set_iid (rdisc, *iid); + + nm_platform_sysctl_set (nm_utils_ip6_property_path (ifname, "accept_ra"), "1"); + nm_platform_sysctl_set (nm_utils_ip6_property_path (ifname, "accept_ra_defrtr"), "0"); + nm_platform_sysctl_set (nm_utils_ip6_property_path (ifname, "accept_ra_pinfo"), "0"); + nm_platform_sysctl_set (nm_utils_ip6_property_path (ifname, "accept_ra_rtr_pref"), "0"); + + g_signal_connect (rdisc, + NM_RDISC_CONFIG_CHANGED, + G_CALLBACK (rdisc_config_changed), + NULL); + g_signal_connect (rdisc, + NM_RDISC_RA_TIMEOUT, + G_CALLBACK (rdisc_ra_timeout), + NULL); + nm_rdisc_start (rdisc); + } + + if (!quit_early) + g_main_loop_run (main_loop); + + g_clear_pointer (&hwaddr, g_byte_array_unref); + + nm_logging_syslog_closelog (); + + if (pidfile && wrote_pidfile) + unlink (pidfile); + + nm_log_info (LOGD_CORE, "exiting"); + exit (0); +} + +/*******************************************************/ +/* Stub functions */ + +gconstpointer nm_config_get (void); +const char *nm_config_get_dhcp_client (gpointer unused); +gboolean nm_config_get_configure_and_quit (gpointer unused); +gconstpointer nm_dbus_manager_get (void); +void nm_dbus_manager_register_exported_type (gpointer unused, GType gtype, gconstpointer unused2); +void nm_dbus_manager_register_object (gpointer unused, const char *path, gpointer object); + +gconstpointer +nm_config_get (void) +{ + return GUINT_TO_POINTER (1); +} + +const char * +nm_config_get_dhcp_client (gpointer unused) +{ + return "internal"; +} + +gboolean +nm_config_get_configure_and_quit (gpointer unused) +{ + return TRUE; +} + +gconstpointer +nm_dbus_manager_get (void) +{ + return GUINT_TO_POINTER (1); +} + +void +nm_dbus_manager_register_exported_type (gpointer unused, GType gtype, gconstpointer unused2) +{ +} + +void +nm_dbus_manager_register_object (gpointer unused, const char *path, gpointer object) +{ +} + diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 72c1b0cd75..7548daded0 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -83,6 +83,7 @@ nm_ip4_config_new (void) } +#ifndef NM_IFACE_HELPER void nm_ip4_config_export (NMIP4Config *config) { @@ -94,6 +95,7 @@ nm_ip4_config_export (NMIP4Config *config) nm_dbus_manager_register_object (nm_dbus_manager_get (), priv->path, config); } } +#endif const char * nm_ip4_config_get_dbus_path (const NMIP4Config *config) @@ -1952,7 +1954,9 @@ nm_ip4_config_class_init (NMIP4ConfigClass *config_class) g_object_class_install_properties (object_class, LAST_PROP, obj_properties); +#ifndef NM_IFACE_HELPER nm_dbus_manager_register_exported_type (nm_dbus_manager_get (), G_TYPE_FROM_CLASS (config_class), &dbus_glib_nm_ip4_config_object_info); +#endif } diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index fe45f824d8..f0930b7453 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -73,6 +73,7 @@ nm_ip6_config_new (void) return (NMIP6Config *) g_object_new (NM_TYPE_IP6_CONFIG, NULL); } +#ifndef NM_IFACE_HELPER void nm_ip6_config_export (NMIP6Config *config) { @@ -84,6 +85,7 @@ nm_ip6_config_export (NMIP6Config *config) nm_dbus_manager_register_object (nm_dbus_manager_get (), priv->path, config); } } +#endif const char * nm_ip6_config_get_dbus_path (const NMIP6Config *config) @@ -1863,7 +1865,9 @@ nm_ip6_config_class_init (NMIP6ConfigClass *config_class) g_object_class_install_properties (object_class, LAST_PROP, obj_properties); +#ifndef NM_IFACE_HELPER nm_dbus_manager_register_exported_type (nm_dbus_manager_get (), G_TYPE_FROM_CLASS (config_class), &dbus_glib_nm_ip6_config_object_info); +#endif } diff --git a/src/nm-manager.c b/src/nm-manager.c index f936235c78..d8973b7337 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -59,6 +59,7 @@ #include "nm-session-monitor.h" #include "nm-activation-request.h" #include "nm-core-internal.h" +#include "nm-config.h" #define NM_AUTOIP_DBUS_SERVICE "org.freedesktop.nm_avahi_autoipd" #define NM_AUTOIP_DBUS_IFACE "org.freedesktop.nm_avahi_autoipd" @@ -214,6 +215,7 @@ enum { USER_PERMISSIONS_CHANGED, ACTIVE_CONNECTION_ADDED, ACTIVE_CONNECTION_REMOVED, + CONFIGURE_QUIT, LAST_SIGNAL }; @@ -706,6 +708,9 @@ check_if_startup_complete (NMManager *self) g_signal_handlers_disconnect_by_func (dev, G_CALLBACK (device_has_pending_action_changed), self); } + + if (nm_config_get_configure_and_quit (nm_config_get ())) + g_signal_emit (self, signals[CONFIGURE_QUIT], 0); } static void @@ -745,6 +750,8 @@ remove_device (NMManager *manager, nm_device_set_unmanaged_quitting (device); else nm_device_set_unmanaged (device, NM_UNMANAGED_INTERNAL, TRUE, NM_DEVICE_STATE_REASON_REMOVED); + } else if (quitting && nm_config_get_configure_and_quit (nm_config_get ())) { + nm_device_spawn_iface_helper (device); } } @@ -4167,6 +4174,16 @@ nm_manager_start (NMManager *self) check_if_startup_complete (self); } +void +nm_manager_stop (NMManager *self) +{ + NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self); + + /* Remove all devices */ + while (priv->devices) + remove_device (self, NM_DEVICE (priv->devices->data), TRUE, TRUE); +} + static gboolean handle_firmware_changed (gpointer user_data) { @@ -4990,9 +5007,7 @@ dispose (GObject *object) G_CALLBACK (authority_changed_cb), manager); - /* Remove all devices */ - while (priv->devices) - remove_device (manager, NM_DEVICE (priv->devices->data), TRUE, TRUE); + g_assert (priv->devices == NULL); if (priv->ac_cleanup_id) { g_source_remove (priv->ac_cleanup_id); @@ -5258,6 +5273,13 @@ nm_manager_class_init (NMManagerClass *manager_class) 0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_OBJECT); + signals[CONFIGURE_QUIT] = + g_signal_new (NM_MANAGER_CONFIGURE_QUIT, + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_FIRST, + 0, NULL, NULL, NULL, + G_TYPE_NONE, 0); + nm_dbus_manager_register_exported_type (nm_dbus_manager_get (), G_TYPE_FROM_CLASS (manager_class), &dbus_glib_nm_manager_object_info); diff --git a/src/nm-manager.h b/src/nm-manager.h index f623516159..3b00e8053a 100644 --- a/src/nm-manager.h +++ b/src/nm-manager.h @@ -59,6 +59,7 @@ /* Internal signals */ #define NM_MANAGER_ACTIVE_CONNECTION_ADDED "active-connection-added" #define NM_MANAGER_ACTIVE_CONNECTION_REMOVED "active-connection-removed" +#define NM_MANAGER_CONFIGURE_QUIT "configure-quit" struct _NMManager { @@ -88,6 +89,7 @@ NMManager * nm_manager_new (NMSettings *settings, NMManager * nm_manager_get (void); void nm_manager_start (NMManager *manager); +void nm_manager_stop (NMManager *manager); NMState nm_manager_get_state (NMManager *manager); const GSList *nm_manager_get_active_connections (NMManager *manager); GSList * nm_manager_get_activatable_connections (NMManager *manager); -- cgit v1.2.1 From 3e3e54a56c3b6af48afadc9e494214fb135492d0 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 7 Nov 2014 13:56:49 -0500 Subject: cli: fix an activation bug Add a missing g_clear_error() that, for circuitous reasons, made trying to activate a virtual connection fail silently. --- clients/cli/connections.c | 1 + 1 file changed, 1 insertion(+) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 65d900b9b7..5327634926 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -2128,6 +2128,7 @@ nmc_activate_connection (NmCli *nmc, g_clear_error (&local); return FALSE; } + g_clear_error (&local); } else if (ifname) { device = nm_client_get_device_by_iface (nmc->client, ifname); if (!device) { -- cgit v1.2.1 From b1e26735a05c09905d11a79ecb60d81d042d251a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Nov 2014 21:21:49 +0100 Subject: contrib/rpm: add option to only building SRPM Set environment variable BUILDTYPE=SRPM or call `build_clean.sh --srpm`. Signed-off-by: Thomas Haller --- contrib/fedora/rpm/build.sh | 13 +++++++++++-- contrib/fedora/rpm/build_clean.sh | 4 ++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/contrib/fedora/rpm/build.sh b/contrib/fedora/rpm/build.sh index e70eb21491..e3c7cd5c10 100755 --- a/contrib/fedora/rpm/build.sh +++ b/contrib/fedora/rpm/build.sh @@ -116,7 +116,16 @@ sed -e "/^__CHANGELOG__$/ \ d }" > "$TEMPSPEC" || die "Error reading spec file" -rpmbuild --define "_topdir $TEMP" -ba "$TEMPSPEC" || die "ERROR: rpmbuild FAILED" +case "$BUILDTYPE" in + "SRPM") + RPM_BUILD_OPTION=-bs + ;; + *) + RPM_BUILD_OPTION=-ba + ;; +esac + +rpmbuild --define "_topdir $TEMP" $RPM_BUILD_OPTION "$TEMPSPEC" || die "ERROR: rpmbuild FAILED" ln -snf "$TEMPBASE" ./latest TEMP_LATEST="$(readlink -f .)"/latest @@ -128,6 +137,6 @@ LOG LOG "See \"$TEMP_LATEST/\" which symlinks to \"$TEMPBASE\"" LOG LOG "Result:" -ls -dla "$TEMP_LATEST" "$(dirname "$TEMP_LATEST")/$TEMPBASE/" "$TEMP_LATEST"/RPMS/*/ "$TEMP_LATEST"/RPMS/*/*.rpm "$TEMP_LATEST"/SRPMS/ "$TEMP_LATEST"/SRPMS/*.rpm | sed 's/^/ /' +ls -dla "$TEMP_LATEST" "$(dirname "$TEMP_LATEST")/$TEMPBASE/" "$TEMP_LATEST"/RPMS/*/ "$TEMP_LATEST"/RPMS/*/*.rpm "$TEMP_LATEST"/SRPMS/ "$TEMP_LATEST"/SRPMS/*.rpm 2>/dev/null | sed 's/^/ /' diff --git a/contrib/fedora/rpm/build_clean.sh b/contrib/fedora/rpm/build_clean.sh index 433bd59d5a..c624d2a1d5 100755 --- a/contrib/fedora/rpm/build_clean.sh +++ b/contrib/fedora/rpm/build_clean.sh @@ -15,6 +15,7 @@ usage() { echo " --force: force build, even if working directory is not clean and has local modifications" echo " --clean: run \`git-clean -fdx :/\` before build" echo " --quick: only run \`make dist\` instead of \`make distcheck\`" + echo " --srpm: only build the SRPM" } @@ -46,6 +47,9 @@ for A; do -Q|--quick) QUICK=1 ;; + -S|--srpm) + BUILDTYPE=SRPM + ;; *) usage die "Unexpected argument \"$A\"" -- cgit v1.2.1 From 2728b5724046e2229fdd8236be2bd89ace4ce18c Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 7 Nov 2014 15:46:33 -0500 Subject: docs: fix libnm docs out-of-tree build --- docs/libnm/Makefile.am | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/libnm/Makefile.am b/docs/libnm/Makefile.am index e04d1722bf..28938f89ff 100644 --- a/docs/libnm/Makefile.am +++ b/docs/libnm/Makefile.am @@ -10,7 +10,11 @@ DOC_MAIN_SGML_FILE=$(DOC_MODULE)-docs.xml # The directory containing the source code. Relative to $(srcdir). # gtk-doc will search all .c & .h files beneath here for inline comments # documenting functions and macros. -DOC_SOURCE_DIR=$(top_srcdir)/libnm-core $(top_srcdir)/libnm +DOC_SOURCE_DIR= \ + $(top_srcdir)/libnm-core \ + $(top_builddir)/libnm-core \ + $(top_srcdir)/libnm \ + $(top_builddir)/libnm # Extra options to supply to gtkdoc-scan. SCAN_OPTIONS=--rebuild-types --rebuild-sections -- cgit v1.2.1 From 1f61cb82de73e750e4c5f515a492c2acde23153e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Nov 2014 21:49:12 +0100 Subject: po/test: add contrib/rpm directory to POTFILES.skip Add contrib/fedora/rpm/ directory to POTFILES.skip. Otherwise, after building an RPM, make check would fail due to the source files from the rpmbuild. Signed-off-by: Thomas Haller --- po/POTFILES.skip | 1 + 1 file changed, 1 insertion(+) diff --git a/po/POTFILES.skip b/po/POTFILES.skip index 2a7f3518a1..f611e4ce29 100644 --- a/po/POTFILES.skip +++ b/po/POTFILES.skip @@ -5,3 +5,4 @@ policy/org.freedesktop.NetworkManager.policy.in vpn-daemons/openvpn vpn-daemons/pptp vpn-daemons/vpnc +contrib/fedora/rpm/ -- cgit v1.2.1 From a5eb556d8cbde3414075aa1fe981e897d7441b3e Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 7 Nov 2014 14:50:46 -0600 Subject: dhcp: demote DHCP client registration message to 'debug' level Was supposed to be that level originally. --- src/dhcp-manager/nm-dhcp-manager.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/dhcp-manager/nm-dhcp-manager.c b/src/dhcp-manager/nm-dhcp-manager.c index 3147980b4b..419bdafbcb 100644 --- a/src/dhcp-manager/nm-dhcp-manager.c +++ b/src/dhcp-manager/nm-dhcp-manager.c @@ -95,8 +95,6 @@ _nm_dhcp_client_register (GType gtype, desc->get_path_func = get_path_func; desc->get_lease_configs_func = get_lease_configs_func; client_descs = g_slist_prepend (client_descs, desc); - - nm_log_info (LOGD_DHCP, "Registered DHCP client '%s'", name); } static ClientDesc * @@ -389,6 +387,7 @@ nm_dhcp_manager_init (NMDhcpManager *self) NMConfig *config = nm_config_get (); const char *client; GError *error = NULL; + GSList *iter; /* Client-specific setup */ client = nm_config_get_dhcp_client (config); @@ -409,6 +408,13 @@ nm_dhcp_manager_init (NMDhcpManager *self) NULL, (GDestroyNotify) g_object_unref); g_assert (priv->clients); + + for (iter = client_descs; iter; iter = iter->next) { + ClientDesc *desc = iter->data; + + nm_log_dbg (LOGD_DHCP, "Registered DHCP client '%s' (%s)", + desc->name, g_type_name (desc->gtype)); + } } static void -- cgit v1.2.1 From a1a338f9b0a8f70729cab8e5394ec563963ba11a Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 7 Nov 2014 17:00:39 -0500 Subject: libnm: fix a crash after unreffing NMClient Make NMClient disconnect from the NMManager and NMRemoteSettings signals when disposing, in case they outlive the NMClient (which shouldn't happen, but...) --- libnm/nm-client.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libnm/nm-client.c b/libnm/nm-client.c index f6a2256709..b84fce5f95 100644 --- a/libnm/nm-client.c +++ b/libnm/nm-client.c @@ -1825,7 +1825,9 @@ dispose (GObject *object) { NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (object); + g_signal_handlers_disconnect_by_data (priv->manager, object); g_clear_object (&priv->manager); + g_signal_handlers_disconnect_by_data (priv->settings, object); g_clear_object (&priv->settings); G_OBJECT_CLASS (nm_client_parent_class)->dispose (object); -- cgit v1.2.1 From dde8e703f86d2e0aebd0840135ca43f0be822007 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 7 Nov 2014 23:08:13 +0100 Subject: libnm: make dispose() of NMClient reentrant Signed-off-by: Thomas Haller --- libnm/nm-client.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/libnm/nm-client.c b/libnm/nm-client.c index b84fce5f95..4e4f194f5c 100644 --- a/libnm/nm-client.c +++ b/libnm/nm-client.c @@ -1825,10 +1825,14 @@ dispose (GObject *object) { NMClientPrivate *priv = NM_CLIENT_GET_PRIVATE (object); - g_signal_handlers_disconnect_by_data (priv->manager, object); - g_clear_object (&priv->manager); - g_signal_handlers_disconnect_by_data (priv->settings, object); - g_clear_object (&priv->settings); + if (priv->manager) { + g_signal_handlers_disconnect_by_data (priv->manager, object); + g_clear_object (&priv->manager); + } + if (priv->settings) { + g_signal_handlers_disconnect_by_data (priv->settings, object); + g_clear_object (&priv->settings); + } G_OBJECT_CLASS (nm_client_parent_class)->dispose (object); } -- cgit v1.2.1 From 14537c71d8a28f9805bd410f2df475db8b4aa6f6 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 7 Nov 2014 16:47:13 -0600 Subject: libnm-core: emit added/removed signals before property change notifications Because internal objects do some processing/setup in the various _added class signal handlers, they need to be emitted before property change notifications. Otherwise it leads to situations where external objects that listen to NMClient property changes will be called before internal processing has been completed. Specifically, NMRemoteSettings uses connection_added() to check connection visibility and assign the new connection to one of two internal arrays. If a client got a property notification for NMClient::connections before NMRemoteSettings can process the new connection, then nm_client_get_connections() will return an empty array because NMRemoteSettings hasn't had the chance to add the new connection to priv->visible yet, which is done in NMRemoteSettings::connection_added(). Fixes:Beaker:NetworkManager_Test240_nmtui_general_realtime_refresh_edit_screen --- libnm/nm-object.c | 21 ++++++++++++--------- libnm/tests/test-nm-client.c | 4 ++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/libnm/nm-object.c b/libnm/nm-object.c index 2023a74fa0..4bc82b8b1e 100644 --- a/libnm/nm-object.c +++ b/libnm/nm-object.c @@ -209,15 +209,9 @@ deferred_notify_cb (gpointer data) g_object_ref (object); - /* Emit property change notifications first */ - for (iter = props; iter; iter = g_slist_next (iter)) { - NotifyItem *item = iter->data; - - if (item->property) - g_object_notify (G_OBJECT (object), item->property); - } - - /* And added/removed signals second */ + /* Emit added/removed signals first since some of our internal objects + * use the added/removed signals for new object processing. + */ for (iter = props; iter; iter = g_slist_next (iter)) { NotifyItem *item = iter->data; char buf[50]; @@ -243,6 +237,15 @@ deferred_notify_cb (gpointer data) g_signal_emit_by_name (object, buf, item->changed); } } + + /* Emit property change notifications second */ + for (iter = props; iter; iter = g_slist_next (iter)) { + NotifyItem *item = iter->data; + + if (item->property) + g_object_notify (G_OBJECT (object), item->property); + } + g_object_unref (object); g_slist_free_full (props, (GDestroyNotify) notify_item_free); diff --git a/libnm/tests/test-nm-client.c b/libnm/tests/test-nm-client.c index 867a27933b..ada1f6f4a7 100644 --- a/libnm/tests/test-nm-client.c +++ b/libnm/tests/test-nm-client.c @@ -192,8 +192,8 @@ test_device_added_signal_after_init (void) g_signal_handlers_disconnect_by_func (client, device_sai_added_cb, &result); g_signal_handlers_disconnect_by_func (client, devices_sai_notify_cb, &result); - g_assert ((result & NOTIFY_MASK) == NOTIFY_FIRST); - g_assert ((result & SIGNAL_MASK) == SIGNAL_SECOND); + g_assert ((result & SIGNAL_MASK) == SIGNAL_FIRST); + g_assert ((result & NOTIFY_MASK) == NOTIFY_SECOND); devices = nm_client_get_devices (client); g_assert (devices); -- cgit v1.2.1 From ee0c1cf0bd68f4a8c6a4ae40018d7d0a827e8e20 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 10 Nov 2014 12:30:39 +0100 Subject: policy: fix using wrong loop counter in _platform_route_sync_flush() Fixes: e8824f6a5205ffcf761abd3e0897a22b254c7797 Signed-off-by: Thomas Haller --- src/nm-default-route-manager.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/nm-default-route-manager.c b/src/nm-default-route-manager.c index 22a042a4af..ec92d2d52c 100644 --- a/src/nm-default-route-manager.c +++ b/src/nm-default-route-manager.c @@ -204,7 +204,7 @@ _platform_route_sync_flush (const VTableIP *vtable, NMDefaultRouteManager *self) NMDefaultRouteManagerPrivate *priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); GPtrArray *entries = vtable->get_entries (priv); GArray *routes; - guint i; + guint i, j; /* prune all other default routes from this device. */ if (VTABLE_IS_IP4) @@ -224,8 +224,8 @@ _platform_route_sync_flush (const VTableIP *vtable, NMDefaultRouteManager *self) /* look at all entires and see if the route for this ifindex pair is * a known entry. */ - for (i = 0; i < entries->len; i++) { - Entry *e = g_ptr_array_index (entries, i); + for (j = 0; j < entries->len; j++) { + Entry *e = g_ptr_array_index (entries, j); if (e->never_default) continue; -- cgit v1.2.1 From ba7f17a02b5b79e78acad9551373cbc243c08c95 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Sun, 9 Nov 2014 22:51:03 +0100 Subject: libnm: Fix type mismatch in test Causes the info->remaining to be counted incorrectly possibly resulting in g_main_loop_quit() being called twice: Program received signal SIGSEGV, Segmentation fault. 0x00007ffff5a03155 in g_mutex_lock (mutex=0x7ffff7b764b4) at gthread-posix.c:1331 1331 if G_UNLIKELY (g_atomic_int_add (&mutex->i[0], 1) != 0) Missing separate debuginfos, use: debuginfo-install libgcc-4.9.2-1.fc21.x86_64 libgudev1-216-8.fc21.x86_64 libselinux-2.3-5.fc21.x86_64 libuuid-2.25.2-1.fc21.x86_64 ncurses-libs-5.9-16.20140323.fc21.x86_64 nspr-4.10.7-1.fc21.x86_64 nss-3.17.2-1.fc21.x86_64 nss-softokn-3.17.2-1.fc21.x86_64 nss-softokn-freebl-3.17.2-1.fc21.x86_64 nss-util-3.17.2-1.fc21.x86_64 sqlite-3.8.7-1.fc21.x86_64 systemd-libs-216-8.fc21.x86_64 (gdb) bt #0 0x00007ffff5a03155 in g_mutex_lock (mutex=0x7ffff7b764b4) at gthread-posix.c:1331 #1 0x00007ffff59bf258 in g_main_loop_quit (loop=0x7fffffffd130) at gmain.c:4000 #5 0x00007ffff5edc3bf in (instance=, signal_id=, detail=) at gsignal.c:3365 #2 0x00007ffff5ec1d35 in g_closure_invoke (closure=0x5555557b3da0, return_value=return_value@entry=0x0, n_param_values=2, param_values=param_values@entry=0x7fffffffd760, invocation_hint=invocation_hint@entry=0x7fffffffd700) at gclosure.c:768 #3 0x00007ffff5ed3a52 in signal_emit_unlocked_R (node=node@entry=0x555555787040, detail=detail@entry=341, instance=instance@entry=0x5555557bd180, emission_return=emission_return@entry=0x0, instance_and_params=instance_and_params@entry=0x7fffffffd760) at gsignal.c:3553 #4 0x00007ffff5edc191 in g_signal_emit_valist (instance=, signal_id=, detail=, var_args=var_args@entry=0x7fffffffd8f0) at gsignal.c:3309 #6 0x00007ffff5ec6465 in g_object_dispatch_properties_changed (object=0x7ffff7b764b4, n_pspecs=1434087776, pspecs=0x5555557ba880) at gobject.c:1056 #7 0x00007ffff5ec88c1 in g_object_notify (pspec=, object=0x5555557bd180 [NMDeviceVlan]) at gobject.c:1149 #8 0x00007ffff5ec88c1 in g_object_notify (object=0x5555557bd180 [NMDeviceVlan], property_name=property_name@entry=0x7ffff7b772f6 "active-connection") at gobject.c:1197 #9 0x00007ffff7ae57d3 in deferred_notify_cb (data=) at nm-object.c:246 #10 0x00007ffff59beafb in g_main_context_dispatch (context=0x555555784ac0) at gmain.c:3111 #11 0x00007ffff59beafb in g_main_context_dispatch (context=context@entry=0x555555784ac0) at gmain.c:3710 #12 0x00007ffff59bee98 in g_main_context_iterate (context=0x555555784ac0, block=block@entry=1, dispatch=dispatch@entry=1, self=) at gmain.c:3781 #13 0x00007ffff59bf1c2 in g_main_loop_run (loop=0x5555557699e0) at gmain.c:3975 #14 0x000055555555811d in test_activate_virtual () at test-nm-client.c:1103 #15 0x00007ffff59e4243 in g_test_run_suite_internal (tc=0x555555769a30) at gtestutils.c:2059 #16 0x00007ffff59e4243 in g_test_run_suite_internal (suite=suite@entry=0x555555766640, path=path@entry=0x7ffff5a6355e "") at gtestutils.c:2120 #17 0x00007ffff59e4412 in g_test_run_suite_internal (suite=suite@entry=0x555555766620, path=, path@entry=0x7ffff5a6355e "") at gtestutils.c:2131 #18 0x00007ffff59e477b in g_test_run_suite (suite=0x555555766620) at gtestutils.c:2184 #19 0x00007ffff59e47b1 in g_test_run () at gtestutils.c:1488 #20 0x0000555555556c01 in main (argc=1, argv=0x7fffffffe028) at test-nm-client.c:1189 https://bugzilla.gnome.org/show_bug.cgi?id=739861 --- libnm/tests/test-nm-client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnm/tests/test-nm-client.c b/libnm/tests/test-nm-client.c index ada1f6f4a7..35836e6bd3 100644 --- a/libnm/tests/test-nm-client.c +++ b/libnm/tests/test-nm-client.c @@ -1007,7 +1007,7 @@ client_devices_changed_cb (GObject *client, info->remaining--; else { g_signal_connect (device, "notify::" NM_DEVICE_ACTIVE_CONNECTION, - G_CALLBACK (device_ac_changed_cb), &info); + G_CALLBACK (device_ac_changed_cb), info); } info->remaining--; -- cgit v1.2.1 From a14bc5f67c5ed414520a64c8684a21696d35fa81 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Sun, 9 Nov 2014 22:18:52 +0100 Subject: dhcp-manager: Keep size of PID consistent Things explode on i386 when marshalling a 32-bit value when a 64-bit one is expected: Program received signal SIGSEGV, Segmentation fault. __memset_sse2 () at ../sysdeps/i386/i686/multiarch/memset-sse2.S:242 242 movdqu %xmm0, (%edx) Missing separate debuginfos, use: debuginfo-install nss-mdns-0.10-15.fc21.i686 (gdb) bt #0 0xffffffff in __memset_sse2 () at ../sysdeps/i386/i686/multiarch/memset-sse2.S:242 #1 0xffffffff in g_hash_table_remove_all_nodes (__len=, __ch=0, __dest=) at /usr/include/bits/string3.h:84 #2 0xffffffff in g_hash_table_remove_all_nodes (hash_table=hash_table@entry=0x82ee250, notify=notify@entry=1) at ghash.c:481 #3 0xffffffff in g_hash_table_unref (hash_table=0x82ee250) at ghash.c:1042 #4 0xffffffff in _g_type_boxed_free (type=136861824, value=0x82ee250) at gtype.c:4262 #5 0xffffffff in boxed_proxy_value_free (value=0xbfffe8ec) at gboxed.c:209 #6 0xffffffff in g_value_unset (value=value@entry=0xbfffe8ec) at gvalue.c:272 #7 0xffffffff in g_signal_emit_valist (instance=instance@entry=0x82492b8, signal_id=signal_id@entry=125, detail=detail@entry=0, var_args=, var_args@entry=0xbfffea4c "\030\342.\bL#") at gsignal.c:3338 #8 0xffffffff in g_signal_emit (instance=0x82492b8, signal_id=125, detail=0) at gsignal.c:3365 #9 0x0809c05d in handle_event (proxy=0xb5d012e8 [DBusGProxy], options=0x82eb640 = {...}, user_data=0x82492b8) at dhcp-manager/nm-dhcp-listener.c:146 #10 0xffffffff in g_cclosure_marshal_VOID__BOXED (closure=0x82bf270, return_value=0x0, n_param_values=2, param_values=0x82c60c0, invocation_hint=0xbfffec68, marshal_data=0x0) at gmarshal.c:1120 #11 0xffffffff in marshal_dbus_message_to_g_marshaller () at /lib/libdbus-glib-1.so.2 #15 0xffffffff in (instance=0xb5d012e8, signal_id=19, detail=915) at gsignal.c:3365 #12 0xffffffff in g_closure_invoke (closure=0x82bf270, return_value=return_value@entry=0x0, n_param_values=n_param_values@entry=3, param_values=param_values@entry=0xbfffecc0, invocation_hint=invocation_hint@entry=0xbfffec68) at gclosure.c:768 #13 0xffffffff in signal_emit_unlocked_R (node=node@entry=0x8263660, detail=detail@entry=915, instance=0xb5d012e8, emission_return=emission_return@entry=0x0, instance_and_params=0xbfffecc0) at gsignal.c:3553 #14 0xffffffff in g_signal_emit_valist (instance=instance@entry=0xb5d012e8, signal_id=signal_id@entry=19, detail=detail@entry=915, var_args=0xbfffee34 "\340\370.\b\004", var_args@entry=0xbfffee2c "\340\370.\b\300\303/\b\340\370.\b\004") at gsignal.c:3309 #16 0xffffffff in dbus_g_proxy_manager_filter () at /lib/libdbus-glib-1.so.2 #17 0xffffffff in dbus_connection_dispatch () at /lib/libdbus-1.so.3 #18 0xffffffff in message_queue_dispatch () at /lib/libdbus-glib-1.so.2 #19 0xffffffff in g_main_context_dispatch (context=0x8246720) at gmain.c:3111 #20 0xffffffff in g_main_context_dispatch (context=context@entry=0x8246720) at gmain.c:3710 #21 0xffffffff in g_main_context_iterate (context=0x8246720, block=block@entry=1, dispatch=dispatch@entry=1, self=) at gmain.c:3781 #22 0xffffffff in g_main_loop_run (loop=0x8246798) at gmain.c:3975 #23 0x08070c09 in main (argc=1, argv=0xbffff2b4) at main.c:479 (gdb) PIDs use native word width, a gint seems more suitable than gint32 or gint64. https://bugzilla.gnome.org/show_bug.cgi?id=739861 --- src/dhcp-manager/nm-dhcp-client.c | 2 +- src/dhcp-manager/nm-dhcp-client.h | 2 +- src/dhcp-manager/nm-dhcp-listener.c | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/dhcp-manager/nm-dhcp-client.c b/src/dhcp-manager/nm-dhcp-client.c index 0ffd573849..28959da3df 100644 --- a/src/dhcp-manager/nm-dhcp-client.c +++ b/src/dhcp-manager/nm-dhcp-client.c @@ -717,7 +717,7 @@ copy_option (const char * key, gboolean nm_dhcp_client_handle_event (gpointer unused, const char *iface, - gint64 pid, + gint pid, GHashTable *options, const char *reason, NMDhcpClient *self) diff --git a/src/dhcp-manager/nm-dhcp-client.h b/src/dhcp-manager/nm-dhcp-client.h index d732aa05ef..3329fe38f3 100644 --- a/src/dhcp-manager/nm-dhcp-client.h +++ b/src/dhcp-manager/nm-dhcp-client.h @@ -156,7 +156,7 @@ void nm_dhcp_client_set_state (NMDhcpClient *self, gboolean nm_dhcp_client_handle_event (gpointer unused, const char *iface, - gint64 pid, + gint pid, GHashTable *options, const char *reason, NMDhcpClient *self); diff --git a/src/dhcp-manager/nm-dhcp-listener.c b/src/dhcp-manager/nm-dhcp-listener.c index ce6dfafad4..07c153e72a 100644 --- a/src/dhcp-manager/nm-dhcp-listener.c +++ b/src/dhcp-manager/nm-dhcp-listener.c @@ -121,7 +121,7 @@ handle_event (DBusGProxy *proxy, char *iface = NULL; char *pid_str = NULL; char *reason = NULL; - gint32 pid; + gint pid; gboolean handled = FALSE; iface = get_option (options, "interface"); @@ -131,8 +131,8 @@ handle_event (DBusGProxy *proxy, } pid_str = get_option (options, "pid"); - pid = (gint32) nm_utils_ascii_str_to_int64 (pid_str, 10, 0, G_MAXINT32, -1); - if (pid == -1 || pid != (GPid) pid) { + pid = nm_utils_ascii_str_to_int64 (pid_str, 10, 0, G_MAXINT32, -1); + if (pid == -1) { nm_log_warn (LOGD_DHCP, "DHCP event: couldn't convert PID '%s' to an integer", pid_str ? pid_str : "(null)"); goto out; } @@ -283,7 +283,7 @@ nm_dhcp_listener_class_init (NMDhcpListenerClass *listener_class) G_TYPE_BOOLEAN, /* listeners return TRUE if handled */ 4, G_TYPE_STRING, /* iface */ - G_TYPE_INT64, /* pid */ + G_TYPE_INT, /* pid */ G_TYPE_HASH_TABLE, /* options */ G_TYPE_STRING); /* reason */ } -- cgit v1.2.1 From b75dfc62e899098e1178fddf991dce2772c51eec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Mon, 10 Nov 2014 16:56:34 +0100 Subject: cli: fix nmcli timeout when disconnecting a device $ nmcli dev disconnect nm-bond Error: Timeout 10 sec expired. When a software device is disconnected, it will be removed. And it may not go to NM_DEVICE_STATE_DISCONNECTED state before that. So we need to listen to "device-removed" signal. Fixes:Beaker:test_log-NetworkManager_Test189_bond_activate --- clients/cli/devices.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/clients/cli/devices.c b/clients/cli/devices.c index ae8664e987..3703743957 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -1598,17 +1598,28 @@ error: static void disconnect_state_cb (NMDevice *device, GParamSpec *pspec, gpointer user_data) { - NmCli *nmc = (NmCli *) user_data; NMDeviceState state; state = nm_device_get_state (device); if (state == NM_DEVICE_STATE_DISCONNECTED) { - g_string_printf (nmc->return_text, _("Success: Device '%s' successfully disconnected."), nm_device_get_iface (device)); + g_signal_handlers_disconnect_by_data (device, user_data); + g_print (_("Device '%s' successfully disconnected.\n"), + nm_device_get_iface (device)); quit (); } } +static void +device_removed_cb (NMClient *client, NMDevice *device, gpointer user_data) +{ + /* Success: device has been removed. It happens when disconnecting a software device. */ + g_signal_handlers_disconnect_by_data (client, user_data); + g_print (_("Device '%s' successfully disconnected.\n"), + nm_device_get_iface (device)); + quit (); +} + static void disconnect_device_cb (GObject *object, GAsyncResult *result, gpointer user_data) { @@ -1638,6 +1649,7 @@ disconnect_device_cb (GObject *object, GAsyncResult *result, gpointer user_data) quit (); } else { g_signal_connect (device, "notify::state", G_CALLBACK (disconnect_state_cb), nmc); + g_signal_connect (nmc->client, NM_CLIENT_DEVICE_REMOVED, G_CALLBACK (device_removed_cb), nmc); /* Start timer not to loop forever if "notify::state" signal is not issued */ g_timeout_add_seconds (nmc->timeout, timeout_cb, nmc); } -- cgit v1.2.1 From 5f017e96b34d46675af76f84e6b3e1df15fa16dc Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 11 Nov 2014 12:21:14 +0100 Subject: device: fix fetching the IPv6 default route for assumed devices Signed-off-by: Thomas Haller --- src/devices/nm-device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 3390bf765e..263e1f64d3 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -3353,7 +3353,7 @@ ip6_config_merge_and_apply (NMDevice *self, NMPlatformIP6Route *route = &priv->default_route.v6; if (assumed) - priv->default_route.v6_has = _device_get_default_route_from_platform (self, AF_INET, (NMPlatformIPRoute *) route); + priv->default_route.v6_has = _device_get_default_route_from_platform (self, AF_INET6, (NMPlatformIPRoute *) route); else { gateway = nm_ip6_config_get_gateway (composite); if (gateway) { -- cgit v1.2.1 From 714f50dafca1f336ff83b647da28e438d4a607d4 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 24 Jun 2014 19:48:48 +0200 Subject: core: don't pass NULL for "%s" format to g_message Signed-off-by: Thomas Haller --- src/nm-ip4-config.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index 7548daded0..f24fb9a790 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -945,7 +945,7 @@ nm_ip4_config_dump (const NMIP4Config *config, const char *detail) g_message (" nis: %s", nm_utils_inet4_ntop (tmp, NULL)); } - g_message (" nisdmn: %s", nm_ip4_config_get_nis_domain (config)); + g_message (" nisdmn: %s", str_if_set (nm_ip4_config_get_nis_domain (config), "(none)")); /* WINS */ for (i = 0; i < nm_ip4_config_get_num_wins (config); i++) { -- cgit v1.2.1 From 75a0ac080f8ea6124020d02a2e0a67affd55ee65 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 11 Nov 2014 13:26:11 +0100 Subject: core: fix integer type of mtu variable Signed-off-by: Thomas Haller --- src/nm-ip4-config.c | 6 +++--- src/nm-ip6-config.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c index f24fb9a790..7b59160a36 100644 --- a/src/nm-ip4-config.c +++ b/src/nm-ip4-config.c @@ -254,7 +254,7 @@ gboolean nm_ip4_config_commit (const NMIP4Config *config, int ifindex) { NMIP4ConfigPrivate *priv = NM_IP4_CONFIG_GET_PRIVATE (config); - int mtu = nm_ip4_config_get_mtu (config); + guint32 mtu = nm_ip4_config_get_mtu (config); int i; g_return_val_if_fail (ifindex > 0, FALSE); @@ -936,8 +936,8 @@ nm_ip4_config_dump (const NMIP4Config *config, const char *detail) for (i = 0; i < nm_ip4_config_get_num_searches (config); i++) g_message (" search: %s", nm_ip4_config_get_search (config, i)); - g_message (" mss: %u", nm_ip4_config_get_mss (config)); - g_message (" mtu: %u", nm_ip4_config_get_mtu (config)); + g_message (" mss: %"G_GUINT32_FORMAT, nm_ip4_config_get_mss (config)); + g_message (" mtu: %"G_GUINT32_FORMAT, nm_ip4_config_get_mtu (config)); /* NIS */ for (i = 0; i < nm_ip4_config_get_num_nis_servers (config); i++) { diff --git a/src/nm-ip6-config.c b/src/nm-ip6-config.c index f0930b7453..60ed7672dc 100644 --- a/src/nm-ip6-config.c +++ b/src/nm-ip6-config.c @@ -968,7 +968,7 @@ nm_ip6_config_dump (const NMIP6Config *config, const char *detail) for (i = 0; i < nm_ip6_config_get_num_searches (config); i++) g_message (" search: %s", nm_ip6_config_get_search (config, i)); - g_message (" mss: %u", nm_ip6_config_get_mss (config)); + g_message (" mss: %"G_GUINT32_FORMAT, nm_ip6_config_get_mss (config)); g_message (" n-dflt: %d", nm_ip6_config_get_never_default (config)); } -- cgit v1.2.1 From 1e8b681d4f10c2b5e9478ec4972cad64aa1f8e7c Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 5 Nov 2014 23:38:19 +0100 Subject: man: add manual page for nmtui(1) https://bugzilla.gnome.org/show_bug.cgi?id=739710 Branch: th/bgo739710_man_nmtui Signed-off-by: Thomas Haller --- configure.ac | 1 + contrib/fedora/rpm/NetworkManager.spec | 4 +++ man/Makefile.am | 9 +++++ man/nmtui.1.in | 66 ++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 man/nmtui.1.in diff --git a/configure.ac b/configure.ac index 61f8656871..2aecae72b7 100644 --- a/configure.ac +++ b/configure.ac @@ -975,6 +975,7 @@ man/NetworkManager.conf.xml man/nm-system-settings.conf.5 man/nm-online.1 man/nmcli.1 +man/nmtui.1 po/Makefile.in policy/Makefile policy/org.freedesktop.NetworkManager.policy.in diff --git a/contrib/fedora/rpm/NetworkManager.spec b/contrib/fedora/rpm/NetworkManager.spec index b0c76790b9..b19159bde8 100644 --- a/contrib/fedora/rpm/NetworkManager.spec +++ b/contrib/fedora/rpm/NetworkManager.spec @@ -521,6 +521,9 @@ fi %{_libexecdir}/nm-iface-helper %dir %{_libdir}/NetworkManager %{_libdir}/NetworkManager/libnm-settings-plugin*.so +%if 0%{?with_nmtui} +%exclude %{_mandir}/man1/nmtui* +%endif %{_mandir}/man1/* %{_mandir}/man5/* %{_mandir}/man8/* @@ -651,6 +654,7 @@ fi %{_bindir}/nmtui-edit %{_bindir}/nmtui-connect %{_bindir}/nmtui-hostname +%{_mandir}/man1/nmtui* %endif %changelog diff --git a/man/Makefile.am b/man/Makefile.am index 376a8b42ac..66f479dd53 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -52,6 +52,7 @@ endif configure_generated_man_pages = \ nmcli.1 \ + nmtui.1 \ nm-online.1 \ nm-system-settings.conf.5 @@ -82,6 +83,14 @@ DISTCLEANFILES = \ man_MANS += $(configure_generated_man_pages) +links = nmtui-edit nmtui-connect nmtui-hostname + +install-data-hook: + for link in $(links); do \ + ln -f $(DESTDIR)$(mandir)/man1/nmtui.1 $(DESTDIR)$(mandir)/man1/$$link.1; \ + done + + if ENABLE_GTK_DOC man_MANS += $(docbook_generated_man_pages) CLEANFILES += $(docbook_generated_man_pages) diff --git a/man/nmtui.1.in b/man/nmtui.1.in new file mode 100644 index 0000000000..2aa99eb96a --- /dev/null +++ b/man/nmtui.1.in @@ -0,0 +1,66 @@ +.\" nmtui (1) manual page +.\" +.\" This is free documentation; you can redistribute it and/or +.\" modify it under the terms of the GNU General Public License as +.\" published by the Free Software Foundation; either version 2 of +.\" the License, or (at your option) any later version. +.\" +.\" The GNU General Public License's references to "object code" +.\" and "executables" are to be interpreted as the output of any +.\" document formatting or typesetting system, including +.\" intermediate and printed output. +.\" +.\" This manual is distributed in the hope that it will be useful, +.\" but WITHOUT ANY WARRANTY; without even the implied warranty of +.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +.\" GNU General Public License for more details. +.\" +.\" You should have received a copy of the GNU General Public Licence along +.\" with this manual; if not, write to the Free Software Foundation, Inc., +.\" 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +.\" +.\" Copyright (C) 2014 Red Hat, Inc. +.\" +.TH NMTUI "1" "6 November 2014" + +.SH NAME +nmtui \- Text User Interface for controlling NetworkManager +.SH SYNOPSIS +.B nmtui +.RI " [ " edit " | " connect " | " hostname " ] [ ... ] " +.P +.B nmtui\-edit +.RI " [ " connection-id " | " connection-name " ] " +.P +.B nmtui\-connect +.RI " [ " connection-name " | " connection-uuid " | " device-name " | " Wi-Fi-SSID " ] " +.P +.B nmtui\-hostname + +.SH DESCRIPTION +.B nmtui +is a curses\(hybased TUI application for interacting with \fINetworkManager\fP. +.P +When starting \fInmtui\fP, the user is prompted to choose the activity to perform +unless it was specified as first argument. +.P +The supported activities are: +.IP \(em 4 +\fIedit\fP: show a connection editor that supports adding, modifying, viewing and deleting. +It provides similar functionality as \fInm\-connection\-editor\fP. +.IP \(em 4 +\fIconnect\fP: show a list of available connections, with the option to activate or deactivate +them. It provides similar functionality as \fInm\-applet\fP. +.IP \(em 4 +\fIhostname\fP: set the system hostname. + +.P +Corresponding to above activities, \fInmtui\fP also comes with binaries named +\fInmtui\-edit\fP, \fInmtui\-connect\fP, and \fInmtui-hostname\fP to skip the selection +of the actvities. + +.SH SEE ALSO +.BR NetworkManager(8), +.BR nmcli(1). +.BR nm\-applet(1). +.BR nm\-connection\-editor(1). -- cgit v1.2.1 From f2097ca482853aef7a80674cb24cb0c5cc2f9775 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 11 Nov 2014 16:09:08 +0100 Subject: keyfile: don't assert when parsing invalid prefix Signed-off-by: Thomas Haller --- src/settings/plugins/keyfile/reader.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c index 19149e0723..c4a2a5d9bc 100644 --- a/src/settings/plugins/keyfile/reader.c +++ b/src/settings/plugins/keyfile/reader.c @@ -324,15 +324,17 @@ read_one_ip_address_or_route (GKeyFile *file, } } +#define DEFAULT_PREFIX(for_route, for_ipv6) ( (for_route) ? ( (for_ipv6) ? 128 : 24 ) : ( (for_ipv6) ? 64 : 24 ) ) + /* parse plen, fallback to defaults */ - if (plen_str) - g_return_val_if_fail (get_one_int (plen_str, ipv6 ? 128 : 32, - key_name, &plen), NULL); - else { - if (route) - plen = ipv6 ? 128 : 24; - else - plen = ipv6 ? 64 : 24; + if (plen_str) { + if (!get_one_int (plen_str, ipv6 ? 128 : 32, key_name, &plen)) { + plen = DEFAULT_PREFIX (route, ipv6); + nm_log_warn (LOGD_SETTINGS, "keyfile: invalid prefix length '%s' in '%s.%s', defaulting to %d", + plen_str, setting_name, key_name, plen); + } + } else { + plen = DEFAULT_PREFIX (route, ipv6); nm_log_warn (LOGD_SETTINGS, "keyfile: Missing prefix length in '%s.%s', defaulting to %d", setting_name, key_name, plen); } -- cgit v1.2.1 From 142dbf213e536e34759bd3f2371289dc6c1d54cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Tue, 11 Nov 2014 12:12:03 +0100 Subject: cli: show gateway as a separate item in active connection data (bgo #739958) Example (for active connection ethernet-13) $ nmcli -f active con show ethernet-13 ... IP4.ADDRESS[1]: 10.34.25.205/23 IP4.GATEWAY: 10.34.25.254 IP4.ROUTE[1]: dst = 10.38.5.26/32, nh = 0.0.0.0, mt = 20 ... https://bugzilla.gnome.org/show_bug.cgi?id=739958 --- clients/cli/common.c | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/clients/cli/common.c b/clients/cli/common.c index 8539af28d8..aa8f80610e 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -39,13 +39,14 @@ NmcOutputField nmc_fields_ip4_config[] = { {"GROUP", N_("GROUP"), 15}, /* 0 */ {"ADDRESS", N_("ADDRESS"), 68}, /* 1 */ - {"ROUTE", N_("ROUTE"), 68}, /* 2 */ - {"DNS", N_("DNS"), 35}, /* 3 */ - {"DOMAIN", N_("DOMAIN"), 35}, /* 4 */ - {"WINS", N_("WINS"), 20}, /* 5 */ + {"GATEWAY", N_("GATEWAY"), 0}, /* 2 */ + {"ROUTE", N_("ROUTE"), 68}, /* 3 */ + {"DNS", N_("DNS"), 35}, /* 4 */ + {"DOMAIN", N_("DOMAIN"), 35}, /* 5 */ + {"WINS", N_("WINS"), 20}, /* 6 */ {NULL, NULL, 0} }; -#define NMC_FIELDS_IP4_CONFIG_ALL "GROUP,ADDRESS,ROUTE,DNS,DOMAIN,WINS" +#define NMC_FIELDS_IP4_CONFIG_ALL "GROUP,ADDRESS,GATEWAY,ROUTE,DNS,DOMAIN,WINS" /* Available fields for DHCPv4 group */ NmcOutputField nmc_fields_dhcp4_config[] = { @@ -59,12 +60,13 @@ NmcOutputField nmc_fields_dhcp4_config[] = { NmcOutputField nmc_fields_ip6_config[] = { {"GROUP", N_("GROUP"), 15}, /* 0 */ {"ADDRESS", N_("ADDRESS"), 95}, /* 1 */ - {"ROUTE", N_("ROUTE"), 95}, /* 2 */ - {"DNS", N_("DNS"), 60}, /* 3 */ - {"DOMAIN", N_("DOMAIN"), 35}, /* 4 */ + {"GATEWAY", N_("GATEWAY"), 0}, /* 2 */ + {"ROUTE", N_("ROUTE"), 95}, /* 3 */ + {"DNS", N_("DNS"), 60}, /* 4 */ + {"DOMAIN", N_("DOMAIN"), 35}, /* 5 */ {NULL, NULL, 0} }; -#define NMC_FIELDS_IP6_CONFIG_ALL "GROUP,ADDRESS,ROUTE,DNS,DOMAIN" +#define NMC_FIELDS_IP6_CONFIG_ALL "GROUP,ADDRESS,GATEWAY,ROUTE,DNS,DOMAIN" /* Available fields for DHCPv6 group */ NmcOutputField nmc_fields_dhcp6_config[] = { @@ -82,7 +84,6 @@ print_ip4_config (NMIPConfig *cfg4, const char *one_field) { GPtrArray *ptr_array; - const char *gw; char **addr_arr = NULL; char **route_arr = NULL; char **dns_arr = NULL; @@ -104,16 +105,14 @@ print_ip4_config (NMIPConfig *cfg4, /* addresses */ ptr_array = nm_ip_config_get_addresses (cfg4); - gw = nm_ip_config_get_gateway (cfg4); if (ptr_array) { addr_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { NMIPAddress *addr = (NMIPAddress *) g_ptr_array_index (ptr_array, i); - addr_arr[i] = g_strdup_printf ("ip = %s/%u, gw = %s", + addr_arr[i] = g_strdup_printf ("%s/%u", nm_ip_address_get_address (addr), - nm_ip_address_get_prefix (addr), - (i == 0) && gw ? gw : "0.0.0.0"); + nm_ip_address_get_prefix (addr)); } addr_arr[i] = NULL; } @@ -152,10 +151,11 @@ print_ip4_config (NMIPConfig *cfg4, arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, group_prefix); set_val_arr (arr, 1, addr_arr); - set_val_arr (arr, 2, route_arr); - set_val_arr (arr, 3, dns_arr); - set_val_arr (arr, 4, domain_arr); - set_val_arr (arr, 5, wins_arr); + set_val_strc (arr, 2, nm_ip_config_get_gateway (cfg4)); + set_val_arr (arr, 3, route_arr); + set_val_arr (arr, 4, dns_arr); + set_val_arr (arr, 5, domain_arr); + set_val_arr (arr, 6, wins_arr); g_ptr_array_add (nmc->output_data, arr); print_data (nmc); /* Print all data */ @@ -173,7 +173,6 @@ print_ip6_config (NMIPConfig *cfg6, const char *one_field) { GPtrArray *ptr_array; - const char *gw; char **addr_arr = NULL; char **route_arr = NULL; char **dns_arr = NULL; @@ -194,16 +193,14 @@ print_ip6_config (NMIPConfig *cfg6, /* addresses */ ptr_array = nm_ip_config_get_addresses (cfg6); - gw = nm_ip_config_get_gateway (cfg6); if (ptr_array) { addr_arr = g_new (char *, ptr_array->len + 1); for (i = 0; i < ptr_array->len; i++) { NMIPAddress *addr = (NMIPAddress *) g_ptr_array_index (ptr_array, i); - addr_arr[i] = g_strdup_printf ("ip = %s/%u, gw = %s", + addr_arr[i] = g_strdup_printf ("%s/%u", nm_ip_address_get_address (addr), - nm_ip_address_get_prefix (addr), - (i == 0) && gw ? gw : "::"); + nm_ip_address_get_prefix (addr)); } addr_arr[i] = NULL; } @@ -239,9 +236,10 @@ print_ip6_config (NMIPConfig *cfg6, arr = nmc_dup_fields_array (tmpl, tmpl_len, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, group_prefix); set_val_arr (arr, 1, addr_arr); - set_val_arr (arr, 2, route_arr); - set_val_arr (arr, 3, dns_arr); - set_val_arr (arr, 4, domain_arr); + set_val_strc (arr, 2, nm_ip_config_get_gateway (cfg6)); + set_val_arr (arr, 3, route_arr); + set_val_arr (arr, 4, dns_arr); + set_val_arr (arr, 5, domain_arr); g_ptr_array_add (nmc->output_data, arr); print_data (nmc); /* Print all data */ -- cgit v1.2.1 From 900763ce0f3613890ae265969d34163c3c8f43db Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 11 Nov 2014 22:26:00 +0100 Subject: dhcp: fix adapter for sd logging macros to show file location Previously we were logging: [1415715440.639086] [__FILE__:__LINE__] client_set_lease_timeouts(): DHCP CLIENT (0xcf221f86): T1 expires in 11h 59min 53.566147s Signed-off-by: Thomas Haller --- src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h b/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h index 2823acb0cf..5bd1f81f8b 100644 --- a/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h +++ b/src/dhcp-manager/systemd-dhcp/nm-sd-adapt.h @@ -54,8 +54,11 @@ _slog_level_to_nm (int slevel) #define log_meta(level, file, line, func, format, ...) \ G_STMT_START { \ guint32 _l = _slog_level_to_nm ((level)); \ - if (nm_logging_enabled (_l, LOGD_DHCP)) \ - _nm_log (#file ":" #line, func, _l, LOGD_DHCP, format, ## __VA_ARGS__); \ + if (nm_logging_enabled (_l, LOGD_DHCP)) { \ + const char *_location = strrchr ((file ":" G_STRINGIFY(line)), '/'); \ + \ + _nm_log (_location ? _location + 1 : (file ":" G_STRINGIFY(line)), func, _l, LOGD_DHCP, format, ## __VA_ARGS__); \ + } \ } G_STMT_END #define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__) -- cgit v1.2.1 From b27f87c714d6536f53c49cd8285ba5a110b48754 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 11 Nov 2014 16:41:22 -0500 Subject: tui: fix generated new connection names Now that "i" is being used in the first loop over all connections, we need to reset it before using it again in the loop to find an available name. --- clients/tui/nm-editor-utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/tui/nm-editor-utils.c b/clients/tui/nm-editor-utils.c index a98278bc09..f936c95943 100644 --- a/clients/tui/nm-editor-utils.c +++ b/clients/tui/nm-editor-utils.c @@ -272,7 +272,7 @@ get_available_connection_name (const char *format, } /* Find the next available unique connection name */ - while (!cname && (i++ < 10000)) { + for (i = 1; !cname && i < 10000; i++) { char *temp; gboolean found = FALSE; -- cgit v1.2.1 From 38b6037f3373b2c2ed52a22f6e2f02655c06bdfd Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Tue, 11 Nov 2014 16:10:37 -0600 Subject: vpn: update DefaultRouteManager before sending state change signal The DRM now affects DNS too, since it determines the "best" IPv4 and IPv6 configs based on it's idea of the default route. The Policy is also still updating DNS from a state-change handler for VPN connections. This led to a situation where the Policy would remove the VPN's IP config from the DNS manager in vpn_connection_deactivated() and call update_ip4_dns(), whereupon get_best_ip4_config() returned the just-removed VPN IPv4 config as "best" because the VPN connection hadn't yet told the DefaultRouteManager to remove it. Which meant VPN nameservers stuck around in resolv.conf for a long time after the VPN was disconnected. Fixes: a39a3ae4cd72d695f1b5d10eaa79544f2020a54e --- src/vpn-manager/nm-vpn-connection.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/vpn-manager/nm-vpn-connection.c b/src/vpn-manager/nm-vpn-connection.c index fe5d812791..c581874880 100644 --- a/src/vpn-manager/nm-vpn-connection.c +++ b/src/vpn-manager/nm-vpn-connection.c @@ -325,6 +325,11 @@ _set_vpn_state (NMVpnConnection *connection, dispatcher_cleanup (connection); + if (vpn_state >= STATE_DISCONNECTED && vpn_state <= STATE_FAILED) { + nm_default_route_manager_ip4_remove_default_route (nm_default_route_manager_get (), connection); + nm_default_route_manager_ip6_remove_default_route (nm_default_route_manager_get (), connection); + } + /* The connection gets destroyed by the VPN manager when it enters the * disconnected/failed state, but we need to keep it around for a bit * to send out signals and handle the dispatcher. So ref it. @@ -342,9 +347,6 @@ _set_vpn_state (NMVpnConnection *connection, g_object_notify (G_OBJECT (connection), NM_VPN_CONNECTION_VPN_STATE); } - nm_default_route_manager_ip4_update_default_route (nm_default_route_manager_get (), connection); - nm_default_route_manager_ip6_update_default_route (nm_default_route_manager_get (), connection); - switch (vpn_state) { case STATE_NEED_AUTH: /* Do nothing; not part of 'default' because we don't want to touch -- cgit v1.2.1 From 12f2f09e5535ffc30cc5baeac186999269a3137e Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Tue, 11 Nov 2014 17:28:06 -0600 Subject: core: remove unused variable --- src/nm-default-route-manager.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/nm-default-route-manager.c b/src/nm-default-route-manager.c index ec92d2d52c..ebe60c4df0 100644 --- a/src/nm-default-route-manager.c +++ b/src/nm-default-route-manager.c @@ -364,7 +364,6 @@ _entry_at_idx_update (const VTableIP *vtable, NMDefaultRouteManager *self, guint { NMDefaultRouteManagerPrivate *priv = NM_DEFAULT_ROUTE_MANAGER_GET_PRIVATE (self); Entry *entry; - NMDevice *device = NULL; GPtrArray *entries; entries = vtable->get_entries (priv); @@ -372,9 +371,6 @@ _entry_at_idx_update (const VTableIP *vtable, NMDefaultRouteManager *self, guint entry = g_ptr_array_index (entries, entry_idx); - if (NM_IS_DEVICE (entry->source.pointer)) - device = entry->source.device; - g_assert ( !old_entry || (entry->source.pointer == old_entry->source.pointer && entry->route.ifindex == old_entry->route.ifindex)); -- cgit v1.2.1 From dcb25a37a599c2f269228bc0060d2284a8d511ff Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 12 Nov 2014 10:54:37 +0100 Subject: glib-compat: sync local definition of g_clear_pointer() with upstream glib and remove atomic operations Upstream glib changed g_clear_pointer() not to use atomic functions. Update or local definition to b1dd594a22e3499caafdeccd7fa223a032b9e177 glib/gmem.h (glib 2.41.3). (fixup whitespace to match our style). See also the related bug https://bugzilla.gnome.org/show_bug.cgi?id=733969 from glib. Signed-off-by: Thomas Haller --- include/nm-glib-compat.h | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/include/nm-glib-compat.h b/include/nm-glib-compat.h index 7a9bb3160a..1dc939feac 100644 --- a/include/nm-glib-compat.h +++ b/include/nm-glib-compat.h @@ -59,23 +59,22 @@ __g_type_ensure (GType type) #if !GLIB_CHECK_VERSION(2,34,0) -#define g_clear_pointer(pp, destroy) \ - G_STMT_START { \ - G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \ - /* Only one access, please */ \ - gpointer *_pp = (gpointer *) (pp); \ - gpointer _p; \ - /* This assignment is needed to avoid a gcc warning */ \ - GDestroyNotify _destroy = (GDestroyNotify) (destroy); \ - \ - (void) (0 ? (gpointer) *(pp) : 0); \ - do \ - _p = g_atomic_pointer_get (_pp); \ - while G_UNLIKELY (!g_atomic_pointer_compare_and_exchange (_pp, _p, NULL)); \ - \ - if (_p) \ - _destroy (_p); \ - } G_STMT_END +#define g_clear_pointer(pp, destroy) \ + G_STMT_START { \ + G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \ + /* Only one access, please */ \ + gpointer *_pp = (gpointer *) (pp); \ + gpointer _p; \ + /* This assignment is needed to avoid a gcc warning */ \ + GDestroyNotify _destroy = (GDestroyNotify) (destroy); \ + \ + _p = *_pp; \ + if (_p) \ + { \ + *_pp = NULL; \ + _destroy (_p); \ + } \ + } G_STMT_END /* These are used to clean up the output of test programs; we can just let * them no-op in older glib. -- cgit v1.2.1 From a928ce89ef90ce2e6f66e025d30e7d174dd46042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Tue, 11 Nov 2014 13:15:19 +0100 Subject: clients: only handle secret requests for connection being explicitly activated When a connection is being activated, nmcli could ask for secrets for another connection, which might confuse users. We check the request now and only ask for secrets of connection being activated. Test case: $ nmcli con up my-ethernet0 Passwords or encryption keys are required to access the wireless network 'Red Hat'. Warning: password for '802-1x.identity' not given in 'passwd-file' and nmcli cannot ask without '--ask' option. --- clients/cli/agent.c | 2 +- clients/cli/connections.c | 3 ++- clients/common/nm-secret-agent-simple.c | 27 +++++++++++++++++++++++---- clients/common/nm-secret-agent-simple.h | 2 +- clients/tui/nmtui-connect.c | 2 +- 5 files changed, 28 insertions(+), 8 deletions(-) diff --git a/clients/cli/agent.c b/clients/cli/agent.c index 9d4869b11e..2b606a7fca 100644 --- a/clients/cli/agent.c +++ b/clients/cli/agent.c @@ -142,7 +142,7 @@ static NMCResultCode do_agent_secret (NmCli *nmc, int argc, char **argv) { /* Create secret agent */ - nmc->secret_agent = nm_secret_agent_simple_new ("nmcli-agent"); + nmc->secret_agent = nm_secret_agent_simple_new ("nmcli-agent", NULL); if (nmc->secret_agent) { /* We keep running */ nmc->should_wait = TRUE; diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 5327634926..ae1250aab2 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -2109,6 +2109,7 @@ nmc_activate_connection (NmCli *nmc, GError **error) { ActivateConnectionInfo *info; + GHashTable *pwds_hash; NMDevice *device = NULL; const char *spec_object = NULL; @@ -2153,7 +2154,7 @@ nmc_activate_connection (NmCli *nmc, nmc->pwds_hash = pwds_hash; /* Create secret agent */ - nmc->secret_agent = nm_secret_agent_simple_new ("nmcli-connect"); + nmc->secret_agent = nm_secret_agent_simple_new ("nmcli-connect", nm_object_get_path (NM_OBJECT (connection))); if (nmc->secret_agent) g_signal_connect (nmc->secret_agent, "request-secrets", G_CALLBACK (secrets_requested), nmc); diff --git a/clients/common/nm-secret-agent-simple.c b/clients/common/nm-secret-agent-simple.c index cb1f086016..3848bf9302 100644 --- a/clients/common/nm-secret-agent-simple.c +++ b/clients/common/nm-secret-agent-simple.c @@ -61,6 +61,8 @@ typedef struct { typedef struct { /* */ GHashTable *requests; + + char *path; } NMSecretAgentSimplePrivate; static void @@ -110,6 +112,8 @@ nm_secret_agent_simple_finalize (GObject *object) g_hash_table_destroy (priv->requests); g_error_free (error); + g_free (priv->path); + G_OBJECT_CLASS (nm_secret_agent_simple_parent_class)->finalize (object); } @@ -447,6 +451,14 @@ nm_secret_agent_simple_get_secrets (NMSecretAgent *agent, return; } + if (priv->path && g_strcmp0 (priv->path, connection_path) != 0) { + /* We only handle requests for connection with @path if set. */ + error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_FAILED, + "Request for %s secrets doesn't match path %s", + request_id, priv->path); + goto nope; + } + s_con = nm_connection_get_setting_connection (connection); connection_type = nm_setting_connection_get_connection_type (s_con); @@ -627,15 +639,22 @@ nm_secret_agent_simple_class_init (NMSecretAgentSimpleClass *klass) /** * nm_secret_agent_simple_new: * @name: the identifier of secret agent + * @path: (allow-none): the path of the connection the agent handle secrets for, + * or %NULL to handle requests for all connections * * Creates a new #NMSecretAgentSimple. * * Returns: a new #NMSecretAgentSimple */ NMSecretAgent * -nm_secret_agent_simple_new (const char *name) +nm_secret_agent_simple_new (const char *name, const char *path) { - return g_initable_new (NM_TYPE_SECRET_AGENT_SIMPLE, NULL, NULL, - NM_SECRET_AGENT_IDENTIFIER, name, - NULL); + NMSecretAgent *agent; + + agent = g_initable_new (NM_TYPE_SECRET_AGENT_SIMPLE, NULL, NULL, + NM_SECRET_AGENT_IDENTIFIER, name, + NULL); + NM_SECRET_AGENT_SIMPLE_GET_PRIVATE (agent)->path = g_strdup (path); + + return agent; } diff --git a/clients/common/nm-secret-agent-simple.h b/clients/common/nm-secret-agent-simple.h index 7e11d2a5c0..b1cc304492 100644 --- a/clients/common/nm-secret-agent-simple.h +++ b/clients/common/nm-secret-agent-simple.h @@ -47,7 +47,7 @@ typedef struct { GType nm_secret_agent_simple_get_type (void); -NMSecretAgent *nm_secret_agent_simple_new (const char *name); +NMSecretAgent *nm_secret_agent_simple_new (const char *name, const char *path); void nm_secret_agent_simple_response (NMSecretAgentSimple *self, const char *request_id, GPtrArray *secrets); diff --git a/clients/tui/nmtui-connect.c b/clients/tui/nmtui-connect.c index e2ffeb6495..26f7296a66 100644 --- a/clients/tui/nmtui-connect.c +++ b/clients/tui/nmtui-connect.c @@ -145,7 +145,7 @@ activate_connection (NMConnection *connection, label = nmt_newt_label_new (_("Connecting...")); nmt_newt_form_set_content (form, label); - agent = nm_secret_agent_simple_new ("nmtui"); + agent = nm_secret_agent_simple_new ("nmtui", nm_object_get_path (NM_OBJECT (connection))); g_signal_connect (agent, "request-secrets", G_CALLBACK (secrets_requested), NULL); specific_object_path = specific_object ? nm_object_get_path (specific_object) : NULL; -- cgit v1.2.1 From 0391c8b161a029150c0a0b6f3b9803be4e3724b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Wed, 12 Nov 2014 15:28:33 +0100 Subject: trivial: fix description of route-metric --- libnm-core/nm-setting-ip-config.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libnm-core/nm-setting-ip-config.c b/libnm-core/nm-setting-ip-config.c index 0a781798fe..8db1d603c0 100644 --- a/libnm-core/nm-setting-ip-config.c +++ b/libnm-core/nm-setting-ip-config.c @@ -2209,8 +2209,8 @@ nm_setting_ip_config_class_init (NMSettingIPConfigClass *setting_class) * The metric applies to dynamic routes, manual (static) routes that * don't have an explicit metric setting, address prefix routes, and * the default route. - * Note that for IPv6, the kernel accepts accepts zero (0) but coerces - * it to 1024 (user default). Hence, setting this value to zero effectively + * Note that for IPv6, the kernel accepts zero (0) but coerces it to + * 1024 (user default). Hence, setting this property to zero effectively * mean setting it to 1024. * For IPv4, zero is a regular value for the metric. **/ -- cgit v1.2.1 From 66b05c94c0f5f6dcf72c3256ff7f4f7d7845eb68 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 12 Nov 2014 13:37:30 +0100 Subject: platform: assert against expected lifetime values of NMPlatformIPAddress Signed-off-by: Thomas Haller --- src/platform/nm-platform.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 8edf46f75c..b4d09ca073 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -1704,6 +1704,11 @@ _address_get_lifetime (const NMPlatformIPAddress *address, guint32 now, guint32 if (address->lifetime == 0) { *out_lifetime = NM_PLATFORM_LIFETIME_PERMANENT; *out_preferred = NM_PLATFORM_LIFETIME_PERMANENT; + + /* We treat lifetime==0 as permanent addresses to allow easy creation of such addresses + * (without requiring to set the lifetime fields to NM_PLATFORM_LIFETIME_PERMANENT). + * In that case we also expect that the other fields (timestamp and preferred) are left unset. */ + g_return_val_if_fail (address->timestamp == 0 && address->preferred == 0, TRUE); } else { lifetime = _rebase_relative_time_on_now (address->timestamp, address->lifetime, now, padding); if (!lifetime) -- cgit v1.2.1 From 751b52e50be049b53a0b998638a22d4e28a59135 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Wed, 12 Nov 2014 15:24:06 +0100 Subject: bluetooth: Don't call into bluez5 DUN code when it's not enabled It is conditionally compiled depending on presence of bluez-libs. Results in undefined symbols: NetworkManager[19346]: (/libnm-device-plugin-bluetooth.so): failed to load plugin: /usr/lib64/NetworkManager/libnm-device-plugin-bluetooth.so: undefined symbol: nm_bluez5_dun_cleanup --- src/devices/bluetooth/nm-bluez-device.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/devices/bluetooth/nm-bluez-device.c b/src/devices/bluetooth/nm-bluez-device.c index 40fb31819b..a941685b97 100644 --- a/src/devices/bluetooth/nm-bluez-device.c +++ b/src/devices/bluetooth/nm-bluez-device.c @@ -427,7 +427,11 @@ nm_bluez_device_disconnect (NMBluezDevice *self) args = g_variant_new ("(s)", priv->b4_iface), dbus_iface = BLUEZ4_SERIAL_INTERFACE; } else if (priv->bluez_version == 5) { +#if WITH_BLUEZ5_DUN nm_bluez5_dun_cleanup (priv->b5_dun_context); +#else + g_assert_not_reached (); +#endif priv->connected = FALSE; goto out; } @@ -541,9 +545,13 @@ nm_bluez_device_connect_async (NMBluezDevice *self, if (priv->bluez_version == 4) dbus_iface = BLUEZ4_SERIAL_INTERFACE; else if (priv->bluez_version == 5) { +#if WITH_BLUEZ5_DUN if (priv->b5_dun_context == NULL) priv->b5_dun_context = nm_bluez5_dun_new (priv->adapter_address, priv->address); nm_bluez5_dun_connect (priv->b5_dun_context, bluez5_dun_connect_cb, simple); +#else + g_assert_not_reached (); +#endif return; } } else @@ -1090,7 +1098,11 @@ dispose (GObject *object) } if (priv->b5_dun_context) { +#if WITH_BLUEZ5_DUN nm_bluez5_dun_free (priv->b5_dun_context); +#else + g_assert_not_reached (); +#endif priv->b5_dun_context = NULL; } -- cgit v1.2.1 From e40fc7bb17f76aefdbe86d3f8ba4b9e4fed23945 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 12 Nov 2014 16:48:08 +0100 Subject: bluez: fix build without bluez5-dun make[5]: Entering directory `./NetworkManager/_build/src/devices/bluetooth' CC nm-bluez-device.lo ../../../../src/devices/bluetooth/nm-bluez-device.c: In function 'nm_bluez_device_disconnect': ../../../../src/devices/bluetooth/nm-bluez-device.c:430:5: error: "WITH_BLUEZ5_DUN" is not defined [-Werror=undef] #if WITH_BLUEZ5_DUN Fixes: f1c9595311f52d8b79e8d2032e006005613a8fb1 Fixes: 751b52e50be049b53a0b998638a22d4e28a59135 Signed-off-by: Thomas Haller --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 2aecae72b7..b5dbc0d250 100644 --- a/configure.ac +++ b/configure.ac @@ -658,7 +658,7 @@ if (test "${enable_bluez5_dun}" = "yes"); then fi AC_DEFINE(WITH_BLUEZ5_DUN, 1, [Define if you have Bluez 5 libraries]) else - AC_DEFINE(HAVE_BLUEZ5_DUN, 0, [Define if you have Bluez 5 libraries]) + AC_DEFINE(WITH_BLUEZ5_DUN, 0, [Define if you have Bluez 5 libraries]) fi AM_CONDITIONAL(WITH_BLUEZ5_DUN, test "${enable_bluez5_dun}" = "yes") -- cgit v1.2.1 From 4edab14e738fdca5c749e92429216763fc49efa0 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Wed, 12 Nov 2014 17:06:35 +0100 Subject: bluez: Another bluez5 build fix Fixes the "unused declaration" warning with -Werror and no bluez-libs. Fixes: f1c9595311f52d8b79e8d2032e006005613a8fb1 Fixes: 751b52e50be049b53a0b998638a22d4e28a59135 --- src/devices/bluetooth/nm-bluez-device.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/devices/bluetooth/nm-bluez-device.c b/src/devices/bluetooth/nm-bluez-device.c index a941685b97..6e80708d87 100644 --- a/src/devices/bluetooth/nm-bluez-device.c +++ b/src/devices/bluetooth/nm-bluez-device.c @@ -30,9 +30,12 @@ #include "nm-bluez-device.h" #include "nm-logging.h" #include "nm-settings-connection.h" -#include "nm-bluez5-dun.h" #include "NetworkManagerUtils.h" +#if WITH_BLUEZ5_DUN +#include "nm-bluez5-dun.h" +#endif + G_DEFINE_TYPE (NMBluezDevice, nm_bluez_device, G_TYPE_OBJECT) #define NM_BLUEZ_DEVICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_BLUEZ_DEVICE, NMBluezDevicePrivate)) @@ -59,7 +62,9 @@ typedef struct { gboolean connected; char *b4_iface; +#if WITH_BLUEZ5_DUN NMBluez5DunContext *b5_dun_context; +#endif NMConnectionProvider *provider; GSList *connections; @@ -495,6 +500,7 @@ bluez_connect_cb (GDBusConnection *dbus_connection, g_object_unref (result_object); } +#if WITH_BLUEZ5_DUN static void bluez5_dun_connect_cb (NMBluez5DunContext *context, const char *device, @@ -514,6 +520,7 @@ bluez5_dun_connect_cb (NMBluez5DunContext *context, g_simple_async_result_complete (result); g_object_unref (result); } +#endif void nm_bluez_device_connect_async (NMBluezDevice *self, @@ -1097,14 +1104,12 @@ dispose (GObject *object) priv->pan_connection = NULL; } - if (priv->b5_dun_context) { #if WITH_BLUEZ5_DUN + if (priv->b5_dun_context) { nm_bluez5_dun_free (priv->b5_dun_context); -#else - g_assert_not_reached (); -#endif priv->b5_dun_context = NULL; } +#endif g_signal_handlers_disconnect_by_func (priv->provider, cp_connection_added, self); g_signal_handlers_disconnect_by_func (priv->provider, cp_connection_removed, self); -- cgit v1.2.1 From dd9bb5f376e1367a54b307c26bf4d888976c2d20 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 12 Nov 2014 17:40:05 +0100 Subject: man: fix `make uninstall` to remove the nmtui manual pages Fixes: 1e8b681d4f10c2b5e9478ec4972cad64aa1f8e7c Signed-off-by: Thomas Haller --- man/Makefile.am | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/man/Makefile.am b/man/Makefile.am index 66f479dd53..5d0386ff38 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -90,6 +90,10 @@ install-data-hook: ln -f $(DESTDIR)$(mandir)/man1/nmtui.1 $(DESTDIR)$(mandir)/man1/$$link.1; \ done +uninstall-hook: + for link in $(links); do \ + rm -f $(DESTDIR)$(mandir)/man1/$$link.1; \ + done if ENABLE_GTK_DOC man_MANS += $(docbook_generated_man_pages) -- cgit v1.2.1