diff options
Diffstat (limited to 'src/dhcp')
| -rw-r--r-- | src/dhcp/meson.build | 2 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-client.c | 4 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-dhclient-utils.c | 181 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-dhclient.c | 2 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-manager.c | 2 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-systemd.c | 4 | ||||
| -rw-r--r-- | src/dhcp/nm-dhcp-utils.c | 103 | ||||
| -rw-r--r-- | src/dhcp/tests/test-dhcp-dhclient.c | 2 | ||||
| -rw-r--r-- | src/dhcp/tests/test-dhcp-utils.c | 2 |
9 files changed, 152 insertions, 150 deletions
diff --git a/src/dhcp/meson.build b/src/dhcp/meson.build index a5dd3151..c1f28be0 100644 --- a/src/dhcp/meson.build +++ b/src/dhcp/meson.build @@ -8,7 +8,7 @@ cflags = [ executable( name, name + '.c', - dependencies: nm_core_dep, + dependencies: libnm_core_dep, c_args: cflags, link_args: ldflags_linker_script_binary, link_depends: linker_script_binary, diff --git a/src/dhcp/nm-dhcp-client.c b/src/dhcp/nm-dhcp-client.c index a4fccce0..d494eff0 100644 --- a/src/dhcp/nm-dhcp-client.c +++ b/src/dhcp/nm-dhcp-client.c @@ -28,8 +28,8 @@ #include <stdlib.h> #include <linux/rtnetlink.h> -#include "nm-utils/nm-dedup-multi.h" -#include "nm-utils/nm-random-utils.h" +#include "nm-glib-aux/nm-dedup-multi.h" +#include "nm-glib-aux/nm-random-utils.h" #include "NetworkManagerUtils.h" #include "nm-utils.h" diff --git a/src/dhcp/nm-dhcp-dhclient-utils.c b/src/dhcp/nm-dhcp-dhclient-utils.c index cbd706fa..85ca3704 100644 --- a/src/dhcp/nm-dhcp-dhclient-utils.c +++ b/src/dhcp/nm-dhcp-dhclient-utils.c @@ -25,7 +25,7 @@ #include <arpa/inet.h> #include <net/if.h> -#include "nm-utils/nm-dedup-multi.h" +#include "nm-glib-aux/nm-dedup-multi.h" #include "nm-dhcp-utils.h" #include "nm-ip4-config.h" @@ -50,10 +50,10 @@ static void add_request (GPtrArray *array, const char *item) { - int i; + guint i; for (i = 0; i < array->len; i++) { - if (!strcmp (g_ptr_array_index (array, i), item)) + if (nm_streq (array->pdata[i], item)) return; } g_ptr_array_add (array, g_strdup (item)); @@ -62,37 +62,48 @@ add_request (GPtrArray *array, const char *item) static gboolean grab_request_options (GPtrArray *store, const char* line) { - char **areq, **aiter; - gboolean end = FALSE; + gs_free const char **line_v = NULL; + gsize i; /* Grab each 'request' or 'also request' option and save for later */ - areq = g_strsplit_set (line, "\t ,", -1); - for (aiter = areq; aiter && *aiter; aiter++) { - if (!strlen (g_strstrip (*aiter))) - continue; + line_v = nm_utils_strsplit_set (line, "\t ,"); + for (i = 0; line_v && line_v[i]; i++) { + const char *ss = nm_str_skip_leading_spaces (line_v[i]); + gsize l; + gboolean end = FALSE; - if (*aiter[0] == ';') { + if (!ss[0]) + continue; + if (ss[0] == ';') { /* all done */ - end = TRUE; - break; + return TRUE; } - if (!g_ascii_isalnum ((*aiter)[0])) + if (!g_ascii_isalnum (ss[0])) continue; - if ((*aiter)[strlen (*aiter) - 1] == ';') { + l = strlen (ss); + + while ( l > 0 + && g_ascii_isspace (ss[l - 1])) { + ((char *) ss)[l - 1] = '\0'; + l--; + } + if ( l > 0 + && ss[l - 1] == ';') { /* Remove the EOL marker */ - (*aiter)[strlen (*aiter) - 1] = '\0'; + ((char *) ss)[l - 1] = '\0'; end = TRUE; } - add_request (store, *aiter); - } + if (ss[0]) + add_request (store, ss); - if (areq) - g_strfreev (areq); + if (end) + return TRUE; + } - return end; + return FALSE; } static void @@ -278,8 +289,9 @@ nm_dhcp_dhclient_create_config (const char *interface, const char *orig_contents, GBytes **out_new_client_id) { - GString *new_contents; - GPtrArray *fqdn_opts, *reqs; + nm_auto_free_gstring GString *new_contents = NULL; + gs_unref_ptrarray GPtrArray *fqdn_opts = NULL; + gs_unref_ptrarray GPtrArray *reqs = NULL; gboolean reset_reqlist = FALSE; int i; @@ -288,11 +300,11 @@ nm_dhcp_dhclient_create_config (const char *interface, nm_assert (!out_new_client_id || !*out_new_client_id); new_contents = g_string_new (_("# Created by NetworkManager\n")); - fqdn_opts = g_ptr_array_sized_new (5); reqs = g_ptr_array_new_full (5, g_free); if (orig_contents) { - char **lines, **line; + gs_free const char **lines = NULL; + gsize line_i; int nest = 0; gboolean in_alsoreq = FALSE; gboolean in_req = FALSE; @@ -301,19 +313,23 @@ nm_dhcp_dhclient_create_config (const char *interface, g_string_append_printf (new_contents, _("# Merged from %s\n\n"), orig_path); intf[0] = '\0'; - lines = g_strsplit_set (orig_contents, "\n\r", 0); - for (line = lines; lines && *line; line++) { - char *p = *line; + lines = nm_utils_strsplit_set (orig_contents, "\n\r"); + for (line_i = 0; lines && lines[line_i]; line_i++) { + const char *line = nm_str_skip_leading_spaces (lines[line_i]); + const char *p; - if (!strlen (g_strstrip (p))) + if (line[0] == '\0') continue; + g_strchomp ((char *) line); + + p = line; if (in_req) { /* pass */ } else if (strchr (p, '{')) { nest++; if ( !intf[0] - && g_str_has_prefix (p, "interface")) + && NM_STR_HAS_PREFIX (p, "interface")) if (read_interface (p, intf, sizeof (intf))) continue; } else if (strchr (p, '}')) { @@ -363,6 +379,8 @@ nm_dhcp_dhclient_create_config (const char *interface, * default ones set by NM, add them later */ if (!strncmp (p, FQDN_TAG_PREFIX, NM_STRLEN (FQDN_TAG_PREFIX))) { + if (!fqdn_opts) + fqdn_opts = g_ptr_array_new_full (5, g_free); g_ptr_array_add (fqdn_opts, g_strdup (p + NM_STRLEN (FQDN_TAG_PREFIX))); continue; } @@ -397,12 +415,9 @@ nm_dhcp_dhclient_create_config (const char *interface, } /* Existing configuration line is OK, add it to new configuration */ - g_string_append (new_contents, *line); + g_string_append (new_contents, line); g_string_append_c (new_contents, '\n'); } - - if (lines) - g_strfreev (lines); } else g_string_append_c (new_contents, '\n'); @@ -436,17 +451,16 @@ nm_dhcp_dhclient_create_config (const char *interface, /* And add it to the dhclient configuration */ for (i = 0; i < reqs->len; i++) g_string_append_printf (new_contents, "also request %s;\n", (char *) reqs->pdata[i]); - g_ptr_array_free (reqs, TRUE); - for (i = 0; i < fqdn_opts->len; i++) { - char *t = g_ptr_array_index (fqdn_opts, i); + if (fqdn_opts) { + for (i = 0; i < fqdn_opts->len; i++) { + const char *t = fqdn_opts->pdata[i]; - if (i == 0) - g_string_append_printf (new_contents, "\n# FQDN options from %s\n", orig_path); - g_string_append_printf (new_contents, FQDN_TAG_PREFIX "%s\n", t); - g_free (t); + if (i == 0) + g_string_append_printf (new_contents, "\n# FQDN options from %s\n", orig_path); + g_string_append_printf (new_contents, FQDN_TAG_PREFIX "%s\n", t); + } } - g_ptr_array_free (fqdn_opts, TRUE); g_string_append_c (new_contents, '\n'); @@ -458,7 +472,7 @@ nm_dhcp_dhclient_create_config (const char *interface, interface, anycast_addr); } - return g_string_free (new_contents, FALSE); + return g_string_free (g_steal_pointer (&new_contents), FALSE); } /* Roughly follow what dhclient's quotify_buf() and pretty_escape() functions do */ @@ -553,9 +567,9 @@ error: GBytes * nm_dhcp_dhclient_read_duid (const char *leasefile, GError **error) { - GBytes *duid = NULL; - char *contents; - char **line, **split, *p, *e; + gs_free char *contents = NULL; + gs_free const char **contents_v = NULL; + gsize i; if (!g_file_test (leasefile, G_FILE_TEST_EXISTS)) return NULL; @@ -563,25 +577,29 @@ nm_dhcp_dhclient_read_duid (const char *leasefile, GError **error) if (!g_file_get_contents (leasefile, &contents, NULL, error)) return NULL; - split = g_strsplit_set (contents, "\n\r", -1); - for (line = split; line && *line && (duid == NULL); line++) { - p = g_strstrip (*line); - if (g_str_has_prefix (p, DUID_PREFIX)) { - p += strlen (DUID_PREFIX); + contents_v = nm_utils_strsplit_set (contents, "\n\r"); + for (i = 0; contents_v && contents_v[i]; i++) { + const char *p = nm_str_skip_leading_spaces (contents_v[i]); + GBytes *duid; - /* look for trailing "; */ - e = p + strlen (p) - 2; - if (strcmp (e, "\";") != 0) - continue; - *e = '\0'; + if (!NM_STR_HAS_PREFIX (p, DUID_PREFIX)) + continue; - duid = nm_dhcp_dhclient_unescape_duid (p); - } + p += NM_STRLEN (DUID_PREFIX); + + g_strchomp ((char *) p); + + if (!NM_STR_HAS_SUFFIX (p, "\";")) + continue; + + ((char *) p)[strlen (p) - 2] = '\0'; + + duid = nm_dhcp_dhclient_unescape_duid (p); + if (duid) + return duid; } - g_free (contents); - g_strfreev (split); - return duid; + return NULL; } gboolean @@ -590,14 +608,12 @@ nm_dhcp_dhclient_save_duid (const char *leasefile, GError **error) { gs_free char *escaped_duid = NULL; - gs_strfreev char **lines = NULL; - char **iter, *l; - GString *s; - gboolean success; + gs_free const char **lines = NULL; + nm_auto_free_gstring GString *s = NULL; + const char *const*iter; gsize len = 0; g_return_val_if_fail (leasefile != NULL, FALSE); - if (!duid) { nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "missing duid"); @@ -605,19 +621,17 @@ nm_dhcp_dhclient_save_duid (const char *leasefile, } escaped_duid = nm_dhcp_dhclient_escape_duid (duid); - g_return_val_if_fail (escaped_duid != NULL, FALSE); + nm_assert (escaped_duid); if (g_file_test (leasefile, G_FILE_TEST_EXISTS)) { - char *contents = NULL; + gs_free char *contents = NULL; if (!g_file_get_contents (leasefile, &contents, &len, error)) { g_prefix_error (error, "failed to read lease file %s: ", leasefile); return FALSE; } - g_assert (contents); - lines = g_strsplit_set (contents, "\n\r", -1); - g_free (contents); + lines = nm_utils_strsplit_set_with_empty (contents, "\n\r"); } s = g_string_sized_new (len + 50); @@ -625,37 +639,38 @@ nm_dhcp_dhclient_save_duid (const char *leasefile, /* Preserve existing leasefile contents */ if (lines) { - for (iter = lines; iter && *iter; iter++) { - l = *iter; - while (g_ascii_isspace (*l)) - l++; + for (iter = lines; *iter; iter++) { + const char *str = *iter; + const char *l; + /* If we find an uncommented DUID in the file, check if * equal to the one we are going to write: if so, no need * to update the lease file, otherwise skip the old DUID. */ + l = nm_str_skip_leading_spaces (str); if (g_str_has_prefix (l, DUID_PREFIX)) { gs_strfreev char **split = NULL; split = g_strsplit (l, "\"", -1); - if (nm_streq0 (split[1], escaped_duid)) { - g_string_free (s, TRUE); + if ( split[0] + && nm_streq0 (split[1], escaped_duid)) return TRUE; - } + continue; } - if (*iter[0]) - g_string_append (s, *iter); + if (str) + g_string_append (s, str); /* avoid to add an extra '\n' at the end of file */ if ((iter[1]) != NULL) g_string_append_c (s, '\n'); } } - success = g_file_set_contents (leasefile, s->str, -1, error); - if (!success) + if (!g_file_set_contents (leasefile, s->str, -1, error)) { g_prefix_error (error, "failed to set DUID in lease file %s: ", leasefile); + return FALSE; + } - g_string_free (s, TRUE); - return success; + return TRUE; } diff --git a/src/dhcp/nm-dhcp-dhclient.c b/src/dhcp/nm-dhcp-dhclient.c index af702cb4..b655a1eb 100644 --- a/src/dhcp/nm-dhcp-dhclient.c +++ b/src/dhcp/nm-dhcp-dhclient.c @@ -36,7 +36,7 @@ #include <arpa/inet.h> #include <ctype.h> -#include "nm-utils/nm-dedup-multi.h" +#include "nm-glib-aux/nm-dedup-multi.h" #include "nm-utils.h" #include "nm-config.h" diff --git a/src/dhcp/nm-dhcp-manager.c b/src/dhcp/nm-dhcp-manager.c index 7063c82c..fe843a2c 100644 --- a/src/dhcp/nm-dhcp-manager.c +++ b/src/dhcp/nm-dhcp-manager.c @@ -32,7 +32,7 @@ #include <fcntl.h> #include <stdio.h> -#include "nm-utils/nm-dedup-multi.h" +#include "nm-glib-aux/nm-dedup-multi.h" #include "nm-config.h" #include "NetworkManagerUtils.h" diff --git a/src/dhcp/nm-dhcp-systemd.c b/src/dhcp/nm-dhcp-systemd.c index 70ed8715..1cd5ba27 100644 --- a/src/dhcp/nm-dhcp-systemd.c +++ b/src/dhcp/nm-dhcp-systemd.c @@ -26,8 +26,8 @@ #include <ctype.h> #include <net/if_arp.h> -#include "nm-utils/nm-dedup-multi.h" -#include "nm-utils/unaligned.h" +#include "nm-glib-aux/nm-dedup-multi.h" +#include "nm-std-aux/unaligned.h" #include "nm-utils.h" #include "nm-config.h" diff --git a/src/dhcp/nm-dhcp-utils.c b/src/dhcp/nm-dhcp-utils.c index 5227eea7..7aa867c0 100644 --- a/src/dhcp/nm-dhcp-utils.c +++ b/src/dhcp/nm-dhcp-utils.c @@ -22,7 +22,7 @@ #include <unistd.h> #include <arpa/inet.h> -#include "nm-utils/nm-dedup-multi.h" +#include "nm-glib-aux/nm-dedup-multi.h" #include "nm-dhcp-utils.h" #include "nm-utils.h" @@ -100,67 +100,56 @@ out: return have_routes; } -static const char ** -process_dhclient_rfc3442_route (const char **octets, - NMPlatformIP4Route *route, - gboolean *success) +static gboolean +process_dhclient_rfc3442_route (const char *const**p_octets, + NMPlatformIP4Route *route) { - const char **o = octets; - int addr_len = 0, i = 0; - long int tmp; - char *next_hop; - guint32 tmp_addr; - - *success = FALSE; - - if (!*o) - return o; /* no prefix */ - - tmp = strtol (*o, NULL, 10); - if (tmp < 0 || tmp > 32) /* 32 == max IP4 prefix length */ - return o; - - memset (route, 0, sizeof (*route)); - route->plen = tmp; + const char *const*o = *p_octets; + gs_free char *next_hop = NULL; + int addr_len; + int v_plen; + in_addr_t tmp_addr; + in_addr_t v_network = 0; + + v_plen = _nm_utils_ascii_str_to_int64 (*o, 10, 0, 32, -1); + if (v_plen == -1) + return FALSE; o++; - if (tmp > 0) - addr_len = ((tmp - 1) / 8) + 1; + addr_len = v_plen > 0 + ? ((v_plen - 1) / 8) + 1 + : 0; /* ensure there's at least the address + next hop left */ - if (g_strv_length ((char **) o) < addr_len + 4) - goto error; + if (NM_PTRARRAY_LEN (o) < addr_len + 4) + return FALSE; - if (tmp) { + if (v_plen > 0) { const char *addr[4] = { "0", "0", "0", "0" }; - char *str_addr; + gs_free char *str_addr = NULL; + int i; for (i = 0; i < addr_len; i++) addr[i] = *o++; str_addr = g_strjoin (".", addr[0], addr[1], addr[2], addr[3], NULL); - if (inet_pton (AF_INET, str_addr, &tmp_addr) <= 0) { - g_free (str_addr); - goto error; - } - g_free (str_addr); - route->network = nm_utils_ip4_address_clear_host_address (tmp_addr, tmp); + if (inet_pton (AF_INET, str_addr, &tmp_addr) <= 0) + return FALSE; + v_network = nm_utils_ip4_address_clear_host_address (tmp_addr, v_plen); } - /* Handle next hop */ next_hop = g_strjoin (".", o[0], o[1], o[2], o[3], NULL); - if (inet_pton (AF_INET, next_hop, &tmp_addr) <= 0) { - g_free (next_hop); - goto error; - } - route->gateway = tmp_addr; - g_free (next_hop); - - *success = TRUE; - return o + 4; /* advance to past the next hop */ + o += 4; + if (inet_pton (AF_INET, next_hop, &tmp_addr) <= 0) + return FALSE; -error: - return o; + *route = (NMPlatformIP4Route) { + .network = v_network, + .plen = v_plen, + .gateway = tmp_addr, + }; + *p_octets = o; + return TRUE; } static gboolean @@ -171,23 +160,23 @@ ip4_process_dhclient_rfc3442_routes (const char *iface, NMIP4Config *ip4_config, guint32 *gwaddr) { - char **octets, **o; + gs_free const char **octets = NULL; + const char *const*o; gboolean have_routes = FALSE; - NMPlatformIP4Route route; - gboolean success; - o = octets = g_strsplit_set (str, " .", 0); - if (g_strv_length (octets) < 5) { + octets = nm_utils_strsplit_set_with_empty (str, " ."); + if (NM_PTRARRAY_LEN (octets) < 5) { _LOG2W (LOGD_DHCP4, iface, "ignoring invalid classless static routes '%s'", str); - goto out; + return FALSE; } + o = octets; while (*o) { - memset (&route, 0, sizeof (route)); - o = (char **) process_dhclient_rfc3442_route ((const char **) o, &route, &success); - if (!success) { + NMPlatformIP4Route route; + + if (!process_dhclient_rfc3442_route (&o, &route)) { _LOG2W (LOGD_DHCP4, iface, "ignoring invalid classless static routes"); - break; + return have_routes; } have_routes = TRUE; @@ -211,8 +200,6 @@ ip4_process_dhclient_rfc3442_routes (const char *iface, } } -out: - g_strfreev (octets); return have_routes; } diff --git a/src/dhcp/tests/test-dhcp-dhclient.c b/src/dhcp/tests/test-dhcp-dhclient.c index 55d712b0..1eac3643 100644 --- a/src/dhcp/tests/test-dhcp-dhclient.c +++ b/src/dhcp/tests/test-dhcp-dhclient.c @@ -24,7 +24,7 @@ #include <arpa/inet.h> #include <linux/rtnetlink.h> -#include "nm-utils/nm-dedup-multi.h" +#include "nm-glib-aux/nm-dedup-multi.h" #include "NetworkManagerUtils.h" #include "dhcp/nm-dhcp-dhclient-utils.h" diff --git a/src/dhcp/tests/test-dhcp-utils.c b/src/dhcp/tests/test-dhcp-utils.c index 240d868c..118082a5 100644 --- a/src/dhcp/tests/test-dhcp-utils.c +++ b/src/dhcp/tests/test-dhcp-utils.c @@ -23,7 +23,7 @@ #include <arpa/inet.h> #include <linux/rtnetlink.h> -#include "nm-utils/nm-dedup-multi.h" +#include "nm-glib-aux/nm-dedup-multi.h" #include "nm-utils.h" #include "dhcp/nm-dhcp-utils.h" |