Debian Codegraph @

src/libostree/ostree-repo-finder-avahi-parser.c

raw symbols SCIP
1 /*
2 * Copyright © 2016 Kinvolk GmbH
3 * Copyright © 2017 Endless Mobile, Inc.
4 *
5 * SPDX-License-Identifier: LGPL-2.0+
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <https://www.gnu.org/licenses/>.
19 *
20 * Authors:
21 * - Krzesimir Nowak <[email protected]>
22 * - Philip Withnall <[email protected]>
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 /* Reference: RFC 6763, §6. */
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 /* RFC 6763, §6.1. */
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 /* Key character. */
63 *key_len = *key_len + 1;
64 continue;
65 }
66 else if (*key_len > 0 && txt[i] == '=')
67 {
68 /* Separator. */
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 /* The entire TXT record is the key; there is no ‘=’ or value. */
80 *value = NULL;
81 *value_len = 0;
82
83 return (*key_len > 0);
84 }
85
86 /* TODO: Docs. Return value is only valid as long as @txt is. Reference: RFC 6763, §6. */
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 /* Distinguish between the case where the entire record is the key
123 * (value == NULL) and the case where the record is the key + ‘=’ and the
124 * value is empty (value != NULL && value_len == 0). */
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 }