summaryrefslogtreecommitdiff
path: root/src/libostree/ostree-repo-finder-avahi-parser.c
blob: 8eb1ce4c774667731c89f49a4d8df723d23f3e2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright © 2016 Kinvolk GmbH
 * Copyright © 2017 Endless Mobile, Inc.
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * 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, see <https://www.gnu.org/licenses/>.
 *
 * Authors:
 *  - Krzesimir Nowak <krzesimir@kinvolk.io>
 *  - Philip Withnall <withnall@endlessm.com>
 */

#include "config.h"

#include <avahi-common/strlst.h>
#include <glib.h>
#include <glib-object.h>
#include <libglnx.h>
#include <string.h>

#include "ostree-autocleanups.h"
#include "ostree-repo-finder-avahi.h"
#include "ostree-repo-finder-avahi-private.h"

/* Reference: RFC 6763, §6. */
static gboolean
parse_txt_record (const guint8  *txt,
                  gsize          txt_len,
                  const gchar  **key,
                  gsize         *key_len,
                  const guint8 **value,
                  gsize         *value_len)
{
  gsize i;

  g_return_val_if_fail (key != NULL, FALSE);
  g_return_val_if_fail (key_len != NULL, FALSE);
  g_return_val_if_fail (value != NULL, FALSE);
  g_return_val_if_fail (value_len != NULL, FALSE);

  /* RFC 6763, §6.1. */
  if (txt_len > 8900)
    return FALSE;

  *key = (const gchar *) txt;
  *key_len = 0;
  *value = NULL;
  *value_len = 0;

  for (i = 0; i < txt_len; i++)
    {
      if (txt[i] >= 0x20 && txt[i] <= 0x7e && txt[i] != '=')
        {
          /* Key character. */
          *key_len = *key_len + 1;
          continue;
        }
      else if (*key_len > 0 && txt[i] == '=')
        {
          /* Separator. */
          *value = txt + (i + 1);
          *value_len = txt_len - (i + 1);
          return TRUE;
        }
      else
        {
          return FALSE;
        }
    }

  /* The entire TXT record is the key; there is no ‘=’ or value. */
  *value = NULL;
  *value_len = 0;

  return (*key_len > 0);
}

/* TODO: Docs. Return value is only valid as long as @txt is. Reference: RFC 6763, §6. */
GHashTable *
_ostree_txt_records_parse (AvahiStringList *txt_list)
{
  AvahiStringList *l;
  g_autoptr(GHashTable) out = NULL;

  out = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_bytes_unref);

  for (l = txt_list; l != NULL; l = avahi_string_list_get_next (l))
    {
      const guint8 *txt;
      gsize txt_len;
      const gchar *key;
      const guint8 *value;
      gsize key_len, value_len;
      g_autofree gchar *key_allocated = NULL;
      g_autoptr(GBytes) value_allocated = NULL;

      txt = avahi_string_list_get_text (l);
      txt_len = avahi_string_list_get_size (l);

      if (!parse_txt_record (txt, txt_len, &key, &key_len, &value, &value_len))
        {
          g_debug ("Ignoring invalid TXT record of length %" G_GSIZE_FORMAT,
                   txt_len);
          continue;
        }

      key_allocated = g_ascii_strdown (key, key_len);

      if (g_hash_table_lookup_extended (out, key_allocated, NULL, NULL))
        {
          g_debug ("Ignoring duplicate TXT record ‘%s’", key_allocated);
          continue;
        }

      /* Distinguish between the case where the entire record is the key
       * (value == NULL) and the case where the record is the key + ‘=’ and the
       * value is empty (value != NULL && value_len == 0). */
      if (value != NULL)
        value_allocated = g_bytes_new_static (value, value_len);

      g_hash_table_insert (out, g_steal_pointer (&key_allocated), g_steal_pointer (&value_allocated));
    }

  return g_steal_pointer (&out);
}