dhcp/nettools: cleanup nm_dhcp_lease_data_parse_search_list()

This commit is contained in:
Thomas Haller
2021-02-10 17:06:37 +01:00
parent 6e0d2e5850
commit ce72563a8c

View File

@@ -10,6 +10,7 @@
#include "nm-std-aux/unaligned.h" #include "nm-std-aux/unaligned.h"
#include "nm-glib-aux/nm-dedup-multi.h" #include "nm-glib-aux/nm-dedup-multi.h"
#include "nm-glib-aux/nm-str-buf.h"
#include "systemd/nm-sd-utils-shared.h" #include "systemd/nm-sd-utils-shared.h"
#include "nm-dhcp-utils.h" #include "nm-dhcp-utils.h"
@@ -968,7 +969,7 @@ nm_dhcp_lease_data_parse_in_addr(const guint8 *data, gsize n_data, in_addr_t *ou
/*****************************************************************************/ /*****************************************************************************/
static gboolean static gboolean
lease_option_print_label(GString *str, size_t n_label, const uint8_t **datap, size_t *n_datap) lease_option_print_label(NMStrBuf *sbuf, size_t n_label, const uint8_t **datap, size_t *n_datap)
{ {
gsize i; gsize i;
@@ -984,30 +985,31 @@ lease_option_print_label(GString *str, size_t n_label, const uint8_t **datap, si
case '0' ... '9': case '0' ... '9':
case '-': case '-':
case '_': case '_':
g_string_append_c(str, c); nm_str_buf_append_c(sbuf, c);
break; break;
case '.': case '.':
case '\\': case '\\':
g_string_append_printf(str, "\\%c", c); nm_str_buf_append_c2(sbuf, '\\', c);
break; break;
default: default:
g_string_append_printf(str, "\\%3d", c); nm_str_buf_append_printf(sbuf, "\\%3d", c);
} }
} }
return TRUE; return TRUE;
} }
static gboolean static char *
lease_option_print_domain_name(GString * str, lease_option_print_domain_name(const uint8_t * cache,
const uint8_t * cache,
size_t * n_cachep, size_t * n_cachep,
const uint8_t **datap, const uint8_t **datap,
size_t * n_datap) size_t * n_datap)
{ {
nm_auto_str_buf NMStrBuf sbuf = NM_STR_BUF_INIT(NM_UTILS_GET_NEXT_REALLOC_SIZE_40, FALSE);
const uint8_t * domain; const uint8_t * domain;
size_t n_domain, n_cache = *n_cachep; size_t n_domain;
const uint8_t **domainp = datap; size_t n_cache = *n_cachep;
const uint8_t ** domainp = datap;
size_t * n_domainp = n_datap; size_t * n_domainp = n_datap;
gboolean first = TRUE; gboolean first = TRUE;
uint8_t c; uint8_t c;
@@ -1029,11 +1031,11 @@ lease_option_print_domain_name(GString * str,
* cache shrinks, so this is guaranteed to terminate. * cache shrinks, so this is guaranteed to terminate.
*/ */
if (cache + n_cache != *datap) if (cache + n_cache != *datap)
return FALSE; return NULL;
for (;;) { for (;;) {
if (!nm_dhcp_lease_data_consume(domainp, n_domainp, &c, sizeof(c))) if (!nm_dhcp_lease_data_consume(domainp, n_domainp, &c, sizeof(c)))
return FALSE; return NULL;
switch (c & 0xC0) { switch (c & 0xC0) {
case 0x00: /* label length */ case 0x00: /* label length */
@@ -1046,16 +1048,16 @@ lease_option_print_domain_name(GString * str,
* the cache to include the consumed data, and return. * the cache to include the consumed data, and return.
*/ */
*n_cachep = *datap - cache; *n_cachep = *datap - cache;
return TRUE; return nm_str_buf_finalize(&sbuf, NULL);
} }
if (!first) if (!first)
g_string_append_c(str, '.'); nm_str_buf_append_c(&sbuf, '.');
else else
first = FALSE; first = FALSE;
if (!lease_option_print_label(str, n_label, domainp, n_domainp)) if (!lease_option_print_label(&sbuf, n_label, domainp, n_domainp))
return FALSE; return NULL;
break; break;
} }
@@ -1069,12 +1071,12 @@ lease_option_print_domain_name(GString * str,
*/ */
if (!nm_dhcp_lease_data_consume(domainp, n_domainp, &c, sizeof(c))) if (!nm_dhcp_lease_data_consume(domainp, n_domainp, &c, sizeof(c)))
return FALSE; return NULL;
offset += c; offset += c;
if (offset >= n_cache) if (offset >= n_cache)
return FALSE; return NULL;
domain = cache + offset; domain = cache + offset;
n_domain = n_cache - offset; n_domain = n_cache - offset;
@@ -1086,7 +1088,7 @@ lease_option_print_domain_name(GString * str,
break; break;
} }
default: default:
return FALSE; return NULL;
} }
} }
} }
@@ -1099,23 +1101,21 @@ nm_dhcp_lease_data_parse_search_list(const guint8 *data, gsize n_data)
gsize n_cache = 0; gsize n_cache = 0;
for (;;) { for (;;) {
nm_auto_free_gstring GString *domain = NULL; gs_free char *s = NULL;
nm_gstring_prepare(&domain); s = lease_option_print_domain_name(cache, &n_cache, &data, &n_data);
if (!s)
if (!lease_option_print_domain_name(domain, cache, &n_cache, &data, &n_data))
break; break;
if (!array) if (!array)
array = g_ptr_array_new(); array = g_ptr_array_new();
g_ptr_array_add(array, g_string_free(domain, FALSE)); g_ptr_array_add(array, g_steal_pointer(&s));
domain = NULL;
} }
if (array) { if (!array)
return NULL;
g_ptr_array_add(array, NULL); g_ptr_array_add(array, NULL);
return (char **) g_ptr_array_free(array, FALSE); return (char **) g_ptr_array_free(array, FALSE);
} else
return NULL;
} }