| 1 |
|
| 2 |
H |
| 3 |
. |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "config.h" |
| 26 |
|
| 27 |
#include <avahi-common/strlst.h> |
| 28 |
#include <glib-object.h> |
| 29 |
#include <glib.h> |
| 30 |
#include <libglnx.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#include "ostree-autocleanups.h" |
| 34 |
#include "ostree-repo-finder-avahi-private.h" |
| 35 |
#include "ostree-repo-finder-avahi.h" |
| 36 |
|
| 37 |
/ |
| 38 |
static gboolean |
| 39 |
parse_txt_record (const guint8 *txt, gsize txt_len, const gchar **key, gsize *key_len, |
| 40 |
const guint8 **value, gsize *value_len) |
| 41 |
{ |
| 42 |
gsize i; |
| 43 |
|
| 44 |
g_return_val_if_fail (key != NULL, FALSE); |
| 45 |
g_return_val_if_fail (key_len != NULL, FALSE); |
| 46 |
g_return_val_if_fail (value != NULL, FALSE); |
| 47 |
g_return_val_if_fail (value_len != NULL, FALSE); |
| 48 |
|
| 49 |
/ |
| 50 |
if (txt_len > 8900) |
| 51 |
return FALSE; |
| 52 |
|
| 53 |
*key = (const gchar *)txt; |
| 54 |
*key_len = 0; |
| 55 |
*value = NULL; |
| 56 |
*value_len = 0; |
| 57 |
|
| 58 |
for (i = 0; i < txt_len; i++) |
| 59 |
{ |
| 60 |
if (txt[i] >= 0x20 && txt[i] <= 0x7e && txt[i] != '=') |
| 61 |
{ |
| 62 |
|
| 63 |
*key_len = *key_len + 1; |
| 64 |
continue; |
| 65 |
} |
| 66 |
else if (*key_len > 0 && txt[i] == '=') |
| 67 |
{ |
| 68 |
|
| 69 |
*value = txt + (i + 1); |
| 70 |
*value_len = txt_len - (i + 1); |
| 71 |
return TRUE; |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
return FALSE; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
. */ |
| 80 |
*value = NULL; |
| 81 |
*value_len = 0; |
| 82 |
|
| 83 |
return (*key_len > 0); |
| 84 |
} |
| 85 |
|
| 86 |
/ |
| 87 |
GHashTable * |
| 88 |
_ostree_txt_records_parse (AvahiStringList *txt_list) |
| 89 |
{ |
| 90 |
AvahiStringList *l; |
| 91 |
g_autoptr (GHashTable) out = NULL; |
| 92 |
|
| 93 |
out = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_bytes_unref); |
| 94 |
|
| 95 |
for (l = txt_list; l != NULL; l = avahi_string_list_get_next (l)) |
| 96 |
{ |
| 97 |
const guint8 *txt; |
| 98 |
gsize txt_len; |
| 99 |
const gchar *key; |
| 100 |
const guint8 *value; |
| 101 |
gsize key_len, value_len; |
| 102 |
g_autofree gchar *key_allocated = NULL; |
| 103 |
g_autoptr (GBytes) value_allocated = NULL; |
| 104 |
|
| 105 |
txt = avahi_string_list_get_text (l); |
| 106 |
txt_len = avahi_string_list_get_size (l); |
| 107 |
|
| 108 |
if (!parse_txt_record (txt, txt_len, &key, &key_len, &value, &value_len)) |
| 109 |
{ |
| 110 |
g_debug ("Ignoring invalid TXT record of length %" G_GSIZE_FORMAT, txt_len); |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
key_allocated = g_ascii_strdown (key, key_len); |
| 115 |
|
| 116 |
if (g_hash_table_lookup_extended (out, key_allocated, NULL, NULL)) |
| 117 |
{ |
| 118 |
g_debug ("Ignoring duplicate TXT record ‘%s’", key_allocated); |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
the |
| 124 |
|
| 125 |
if (value != NULL) |
| 126 |
value_allocated = g_bytes_new_static (value, value_len); |
| 127 |
|
| 128 |
g_hash_table_insert (out, g_steal_pointer (&key_allocated), |
| 129 |
g_steal_pointer (&value_allocated)); |
| 130 |
} |
| 131 |
|
| 132 |
return g_steal_pointer (&out); |
| 133 |
} |