summaryrefslogtreecommitdiff
path: root/common/gvfsdnssdutils.c
blob: a17d07b1ad779da0abfd12628bdbd643fb82f518 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright (C) 2008 Red Hat, Inc.
 *
 * 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, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

#include <config.h>
#include <string.h>
#include <glib/gi18n-lib.h>

#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
#include <avahi-common/error.h>
#include <avahi-common/timeval.h>
#include <avahi-glib/glib-watch.h>
#include <avahi-glib/glib-malloc.h>

#include "gvfsdnssdutils.h"

static gchar *
escape_service_name (const gchar *service_name)
{
  GString *s;
  char *res;
  const gchar *p;

  g_return_val_if_fail (service_name != NULL, NULL);

  s = g_string_new (NULL);

  p = service_name;
  while (*p != '\0')
    {
      if (*p == '\\')
        g_string_append (s, "\\\\");
      else if (*p == '.')
        g_string_append (s, "\\.");
      else if (*p == '/')
        g_string_append (s, "\\s");
      else
        g_string_append_c (s, *p);
      p++;
    }

  res = g_uri_escape_string (s->str, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, FALSE);
  g_string_free (s, TRUE);
  return res;
}

static gchar *
escape_service_name2 (const gchar *service_name)
{
  GString *s;
  const gchar *p;

  g_return_val_if_fail (service_name != NULL, NULL);

  s = g_string_new (NULL);

  p = service_name;
  while (*p != '\0')
    {
      if (*p == '.')
        g_string_append (s, "%2e");
      else
        g_string_append_c (s, *p);
      p++;
    }

  return g_string_free (s, FALSE);
}

/**
 * g_vfs_get_dns_sd_uri_for_triple:
 * @service_name: DNS-SD service name.
 * @service_type: DNS-SD service type.
 * @domain: DNS-SD domain.
 *
 * Creates an URI for a file on the GVfs <literal>dns-sd</literal>
 * virtual file system that provides live data for resolving the given
 * DNS-SD service.
 *
 * The URI is of the form
 * <literal>dns-sd://domain/service_name.service_type<literal> with
 * suitable encoding added.
 *
 * Note that there may not exist a file at the returned URI, the
 * resource providing the DNS-SD service will have to be available for
 * the file to exist.
 *
 * Returns: An URI. Free with g_free().
 **/
gchar *
g_vfs_get_dns_sd_uri_for_triple (const gchar *service_name,
                                 const gchar *service_type,
                                 const gchar *domain)
{
  gchar *escaped_service_name;
  gchar *ret;

  g_return_val_if_fail (service_name != NULL, NULL);
  g_return_val_if_fail (service_type != NULL, NULL);
  g_return_val_if_fail (domain != NULL, NULL);

  escaped_service_name = escape_service_name (service_name);
  
  ret = g_strdup_printf ("dns-sd://%s/%s.%s",
                         domain,
                         escaped_service_name,
                         service_type);
  g_free (escaped_service_name);

  return ret;
}

/**
 * g_vfs_encode_dns_sd_triple:
 * @service_name: DNS-SD service name.
 * @service_type: DNS-SD service type.
 * @domain: DNS-SD domain.
 *
 * Creates an encoded triple representing a DNS-SD service. The triple
 * will be of the form
 * <literal>service_name.service_type.domain</literal> with suitable
 * encoding.
 *
 * Use g_vfs_decode_dns_sd_triple() to decode the returned string.
 *
 * Returns: A string representing the triple, free with g_free().
 **/
gchar *
g_vfs_encode_dns_sd_triple (const gchar *service_name,
                            const gchar *service_type,
                            const gchar *domain)
{
  char *dot_escaped_service_name;
  char *escaped_service_name;
  char *escaped_service_type;
  char *escaped_domain;
  char *s;

  escaped_service_name = g_uri_escape_string (service_name, NULL, FALSE);
  dot_escaped_service_name = escape_service_name2 (escaped_service_name);
  escaped_service_type = g_uri_escape_string (service_type, NULL, FALSE);
  escaped_domain = g_uri_escape_string (domain, NULL, FALSE);
  s = g_strdup_printf ("%s.%s.%s",
                       dot_escaped_service_name,
                       escaped_service_type,
                       escaped_domain);
  g_free (dot_escaped_service_name);
  g_free (escaped_service_name);
  g_free (escaped_service_type);
  g_free (escaped_domain);
  return s;
}

/**
 * g_vfs_decode_dns_sd_triple:
 * @encoded_triple: A string obtained from g_vfs_encode_dns_sd_triple().
 * @out_service_name: %NULL or return location for the service name.
 * @out_service_type: %NULL or return location for the service type.
 * @out_domain: %NULL or return location for the domain.
 * @error: Return location for error or %NULL.
 *
 * Constructs a DNS-SD triple by decoding a string generated from
 * g_vfs_encode_dns_sd_triple(). This can fail if @encoded_triple is
 * malformed.
 *
 * Returns: %TRUE unless @error is set.
 **/
gboolean
g_vfs_decode_dns_sd_triple (const gchar *encoded_triple,
                            gchar      **out_service_name,
                            gchar      **out_service_type,
                            gchar      **out_domain,
                            GError     **error)
{
  gboolean ret;
  int n;
  int m;
  int service_type_pos;
  char *escaped_service_name;
  char *escaped_service_type;
  char *escaped_domain;

  g_return_val_if_fail (encoded_triple != NULL, FALSE);


  escaped_service_name = NULL;
  escaped_service_type = NULL;
  escaped_domain = NULL;
  ret = FALSE;

  if (out_service_name != NULL)
    *out_service_name = NULL;

  if (out_service_type != NULL)
    *out_service_type = NULL;

  if (out_domain != NULL)
    *out_domain = NULL;

  /* Find first '.' followed by an underscore. */
  for (n = 0; encoded_triple[n] != '\0'; n++)
    {
      if (encoded_triple[n] == '.')
        {
          if (encoded_triple[n + 1] == '_')
            break;
        }
    }
  if (encoded_triple[n] == '\0')
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_INVALID_ARGUMENT,
                   _("Malformed DNS-SD encoded_triple “%s”"),
                   encoded_triple);
      goto out;
    }

  escaped_service_name = g_strndup (encoded_triple, n);
  if (escaped_service_name == NULL)
    goto out;

  if (out_service_name != NULL)
    *out_service_name = g_uri_unescape_string (escaped_service_name, NULL);

  /* skip dot between service name and service type */
  n += 1;

  service_type_pos = n;

  /* skip next two dots */
  for (m = 0; m < 2; m++)
    {
      for (; encoded_triple[n] != '\0'; n++)
        {
          if (encoded_triple[n] == '.')
            break;
        }
      if (encoded_triple[n] == '\0')
        {
          g_set_error (error,
                       G_IO_ERROR,
                       G_IO_ERROR_INVALID_ARGUMENT,
                       _("Malformed DNS-SD encoded_triple “%s”"),
                       encoded_triple);
          goto out;
        }
      n++;
    }

  escaped_service_type = g_strndup (encoded_triple + service_type_pos, n - service_type_pos - 1);
  if (out_service_type != NULL)
    *out_service_type = g_uri_unescape_string (escaped_service_type, NULL);

  /* the domain is the rest */
  if (encoded_triple[n] == '\0')
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_INVALID_ARGUMENT,
                   _("Malformed DNS-SD encoded_triple “%s”"),
                   encoded_triple);
      goto out;
    }

  escaped_domain = g_strdup (encoded_triple + n);
  if (out_domain != NULL)
    *out_domain = g_uri_unescape_string (escaped_domain, NULL);

  ret = TRUE;

 out:
  g_free (escaped_service_name);
  g_free (escaped_service_type);
  g_free (escaped_domain);
  return ret;
}

gchar *
g_vfs_normalize_encoded_dns_sd_triple (const gchar *encoded_triple)
{
  char *service_name;
  char *service_type;
  char *domain;
  char *ret;

  ret = NULL;

  if (!g_vfs_decode_dns_sd_triple (encoded_triple,
                                   &service_name,
                                   &service_type,
                                   &domain,
                                   NULL))
    goto out;

  ret = g_vfs_encode_dns_sd_triple (service_name, service_type, domain);
  g_free (service_name);
  g_free (service_type);
  g_free (domain);

 out:
  return ret;
}