GVariant: support serialising to GVariantVectors
[platform/upstream/glib.git] / gio / gnetworkservice.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4  *
5  * Copyright (C) 2008 Red Hat, Inc.
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
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "config.h"
22 #include <glib.h>
23 #include "glibintl.h"
24
25 #include "gnetworkservice.h"
26
27 #include "gcancellable.h"
28 #include "ginetaddress.h"
29 #include "ginetsocketaddress.h"
30 #include "gioerror.h"
31 #include "gnetworkaddress.h"
32 #include "gnetworkingprivate.h"
33 #include "gresolver.h"
34 #include "gtask.h"
35 #include "gsocketaddressenumerator.h"
36 #include "gsocketconnectable.h"
37 #include "gsrvtarget.h"
38
39 #include <stdlib.h>
40 #include <string.h>
41
42
43 /**
44  * SECTION:gnetworkservice
45  * @short_description: A GSocketConnectable for resolving SRV records
46  * @include: gio/gio.h
47  *
48  * Like #GNetworkAddress does with hostnames, #GNetworkService
49  * provides an easy way to resolve a SRV record, and then attempt to
50  * connect to one of the hosts that implements that service, handling
51  * service priority/weighting, multiple IP addresses, and multiple
52  * address families.
53  *
54  * See #GSrvTarget for more information about SRV records, and see
55  * #GSocketConnectable for and example of using the connectable
56  * interface.
57  */
58
59 /**
60  * GNetworkService:
61  *
62  * A #GSocketConnectable for resolving a SRV record and connecting to
63  * that service.
64  */
65
66 struct _GNetworkServicePrivate
67 {
68   gchar *service, *protocol, *domain, *scheme;
69   GList *targets;
70 };
71
72 enum {
73   PROP_0,
74   PROP_SERVICE,
75   PROP_PROTOCOL,
76   PROP_DOMAIN,
77   PROP_SCHEME
78 };
79
80 static void g_network_service_set_property (GObject      *object,
81                                             guint         prop_id,
82                                             const GValue *value,
83                                             GParamSpec   *pspec);
84 static void g_network_service_get_property (GObject      *object,
85                                             guint         prop_id,
86                                             GValue       *value,
87                                             GParamSpec   *pspec);
88
89 static void                      g_network_service_connectable_iface_init       (GSocketConnectableIface *iface);
90 static GSocketAddressEnumerator *g_network_service_connectable_enumerate        (GSocketConnectable      *connectable);
91 static GSocketAddressEnumerator *g_network_service_connectable_proxy_enumerate  (GSocketConnectable      *connectable);
92
93 G_DEFINE_TYPE_WITH_CODE (GNetworkService, g_network_service, G_TYPE_OBJECT,
94                          G_ADD_PRIVATE (GNetworkService)
95                          G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
96                                                 g_network_service_connectable_iface_init))
97
98 static void
99 g_network_service_finalize (GObject *object)
100 {
101   GNetworkService *srv = G_NETWORK_SERVICE (object);
102
103   g_free (srv->priv->service);
104   g_free (srv->priv->protocol);
105   g_free (srv->priv->domain);
106   g_free (srv->priv->scheme);
107
108   if (srv->priv->targets)
109     g_resolver_free_targets (srv->priv->targets);
110
111   G_OBJECT_CLASS (g_network_service_parent_class)->finalize (object);
112 }
113
114 static void
115 g_network_service_class_init (GNetworkServiceClass *klass)
116 {
117   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
118
119   gobject_class->set_property = g_network_service_set_property;
120   gobject_class->get_property = g_network_service_get_property;
121   gobject_class->finalize = g_network_service_finalize;
122
123   g_object_class_install_property (gobject_class, PROP_SERVICE,
124                                    g_param_spec_string ("service",
125                                                         P_("Service"),
126                                                         P_("Service name, eg \"ldap\""),
127                                                         NULL,
128                                                         G_PARAM_READWRITE |
129                                                         G_PARAM_CONSTRUCT_ONLY |
130                                                         G_PARAM_STATIC_STRINGS));
131   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
132                                    g_param_spec_string ("protocol",
133                                                         P_("Protocol"),
134                                                         P_("Network protocol, eg \"tcp\""),
135                                                         NULL,
136                                                         G_PARAM_READWRITE |
137                                                         G_PARAM_CONSTRUCT_ONLY |
138                                                         G_PARAM_STATIC_STRINGS));
139   g_object_class_install_property (gobject_class, PROP_DOMAIN,
140                                    g_param_spec_string ("domain",
141                                                         P_("Domain"),
142                                                         P_("Network domain, eg, \"example.com\""),
143                                                         NULL,
144                                                         G_PARAM_READWRITE |
145                                                         G_PARAM_CONSTRUCT_ONLY |
146                                                         G_PARAM_STATIC_STRINGS));
147   g_object_class_install_property (gobject_class, PROP_DOMAIN,
148                                    g_param_spec_string ("scheme",
149                                                         P_("Scheme"),
150                                                         P_("Network scheme (default is to use service)"),
151                                                         NULL,
152                                                         G_PARAM_READWRITE |
153                                                         G_PARAM_STATIC_STRINGS));
154
155 }
156
157 static void
158 g_network_service_connectable_iface_init (GSocketConnectableIface *connectable_iface)
159 {
160   connectable_iface->enumerate = g_network_service_connectable_enumerate;
161   connectable_iface->proxy_enumerate = g_network_service_connectable_proxy_enumerate;
162 }
163
164 static void
165 g_network_service_init (GNetworkService *srv)
166 {
167   srv->priv = g_network_service_get_instance_private (srv);
168 }
169
170 static void
171 g_network_service_set_property (GObject      *object,
172                                 guint         prop_id,
173                                 const GValue *value,
174                                 GParamSpec   *pspec)
175 {
176   GNetworkService *srv = G_NETWORK_SERVICE (object);
177
178   switch (prop_id)
179     {
180     case PROP_SERVICE:
181       srv->priv->service = g_value_dup_string (value);
182       break;
183
184     case PROP_PROTOCOL:
185       srv->priv->protocol = g_value_dup_string (value);
186       break;
187
188     case PROP_DOMAIN:
189       srv->priv->domain = g_value_dup_string (value);
190       break;
191
192     case PROP_SCHEME:
193       g_network_service_set_scheme (srv, g_value_get_string (value));
194       break;
195
196     default:
197       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198       break;
199     }
200 }
201
202 static void
203 g_network_service_get_property (GObject    *object,
204                                 guint       prop_id,
205                                 GValue     *value,
206                                 GParamSpec *pspec)
207 {
208   GNetworkService *srv = G_NETWORK_SERVICE (object);
209
210   switch (prop_id)
211     {
212     case PROP_SERVICE:
213       g_value_set_string (value, g_network_service_get_service (srv));
214       break;
215
216     case PROP_PROTOCOL:
217       g_value_set_string (value, g_network_service_get_protocol (srv));
218       break;
219
220     case PROP_DOMAIN:
221       g_value_set_string (value, g_network_service_get_domain (srv));
222       break;
223
224     case PROP_SCHEME:
225       g_value_set_string (value, g_network_service_get_scheme (srv));
226       break;
227
228     default:
229       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230       break;
231     }
232 }
233
234 /**
235  * g_network_service_new:
236  * @service: the service type to look up (eg, "ldap")
237  * @protocol: the networking protocol to use for @service (eg, "tcp")
238  * @domain: the DNS domain to look up the service in
239  *
240  * Creates a new #GNetworkService representing the given @service,
241  * @protocol, and @domain. This will initially be unresolved; use the
242  * #GSocketConnectable interface to resolve it.
243  *
244  * Returns: (transfer full) (type GNetworkService): a new #GNetworkService
245  *
246  * Since: 2.22
247  */
248 GSocketConnectable *
249 g_network_service_new (const gchar *service,
250                        const gchar *protocol,
251                        const gchar *domain)
252 {
253   return g_object_new (G_TYPE_NETWORK_SERVICE,
254                        "service", service,
255                        "protocol", protocol,
256                        "domain", domain,
257                        NULL);
258 }
259
260 /**
261  * g_network_service_get_service:
262  * @srv: a #GNetworkService
263  *
264  * Gets @srv's service name (eg, "ldap").
265  *
266  * Returns: @srv's service name
267  *
268  * Since: 2.22
269  */
270 const gchar *
271 g_network_service_get_service (GNetworkService *srv)
272 {
273   g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
274
275   return srv->priv->service;
276 }
277
278 /**
279  * g_network_service_get_protocol:
280  * @srv: a #GNetworkService
281  *
282  * Gets @srv's protocol name (eg, "tcp").
283  *
284  * Returns: @srv's protocol name
285  *
286  * Since: 2.22
287  */
288 const gchar *
289 g_network_service_get_protocol (GNetworkService *srv)
290 {
291   g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
292
293   return srv->priv->protocol;
294 }
295
296 /**
297  * g_network_service_get_domain:
298  * @srv: a #GNetworkService
299  *
300  * Gets the domain that @srv serves. This might be either UTF-8 or
301  * ASCII-encoded, depending on what @srv was created with.
302  *
303  * Returns: @srv's domain name
304  *
305  * Since: 2.22
306  */
307 const gchar *
308 g_network_service_get_domain (GNetworkService *srv)
309 {
310   g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
311
312   return srv->priv->domain;
313 }
314
315 /**
316  * g_network_service_get_scheme:
317  * @srv: a #GNetworkService
318  *
319  * Get's the URI scheme used to resolve proxies. By default, the service name
320  * is used as scheme.
321  *
322  * Returns: @srv's scheme name
323  *
324  * Since: 2.26
325  */
326 const gchar *
327 g_network_service_get_scheme (GNetworkService *srv)
328 {
329   g_return_val_if_fail (G_IS_NETWORK_SERVICE (srv), NULL);
330
331   if (srv->priv->scheme)
332     return srv->priv->scheme;
333   else
334     return srv->priv->service;
335 }
336
337 /**
338  * g_network_service_set_scheme:
339  * @srv: a #GNetworkService
340  * @scheme: a URI scheme
341  *
342  * Set's the URI scheme used to resolve proxies. By default, the service name
343  * is used as scheme.
344  *
345  * Since: 2.26
346  */
347 void
348 g_network_service_set_scheme (GNetworkService *srv,
349                               const gchar     *scheme)
350 {
351   g_return_if_fail (G_IS_NETWORK_SERVICE (srv));
352
353   g_free (srv->priv->scheme);
354   srv->priv->scheme = g_strdup (scheme);
355
356   g_object_notify (G_OBJECT (srv), "scheme");
357 }
358
359 static GList *
360 g_network_service_fallback_targets (GNetworkService *srv)
361 {
362   GSrvTarget *target;
363   struct servent *entry;
364   guint16 port;
365
366   entry = getservbyname (srv->priv->service, "tcp");
367   port = entry ? g_ntohs (entry->s_port) : 0;
368 #ifdef HAVE_ENDSERVENT
369   endservent ();
370 #endif
371
372   if (entry == NULL)
373       return NULL;
374
375   target = g_srv_target_new (srv->priv->domain, port, 0, 0);
376   return g_list_append (NULL, target);
377 }
378
379 #define G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR (_g_network_service_address_enumerator_get_type ())
380 #define G_NETWORK_SERVICE_ADDRESS_ENUMERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR, GNetworkServiceAddressEnumerator))
381
382 typedef struct {
383   GSocketAddressEnumerator parent_instance;
384
385   GResolver *resolver;
386   GNetworkService *srv;
387   GSocketAddressEnumerator *addr_enum;
388   GList *t;
389   gboolean use_proxy;
390
391   GError *error;
392
393 } GNetworkServiceAddressEnumerator;
394
395 typedef struct {
396   GSocketAddressEnumeratorClass parent_class;
397
398 } GNetworkServiceAddressEnumeratorClass;
399
400 static GType _g_network_service_address_enumerator_get_type (void);
401 G_DEFINE_TYPE (GNetworkServiceAddressEnumerator, _g_network_service_address_enumerator, G_TYPE_SOCKET_ADDRESS_ENUMERATOR)
402
403 static GSocketAddress *
404 g_network_service_address_enumerator_next (GSocketAddressEnumerator  *enumerator,
405                                            GCancellable              *cancellable,
406                                            GError                   **error)
407 {
408   GNetworkServiceAddressEnumerator *srv_enum =
409     G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
410   GSocketAddress *ret = NULL;
411
412   /* If we haven't yet resolved srv, do that */
413   if (!srv_enum->srv->priv->targets)
414     {
415       GList *targets;
416       GError *my_error = NULL;
417
418       targets = g_resolver_lookup_service (srv_enum->resolver,
419                                            srv_enum->srv->priv->service,
420                                            srv_enum->srv->priv->protocol,
421                                            srv_enum->srv->priv->domain,
422                                            cancellable, &my_error);
423       if (!targets && g_error_matches (my_error, G_RESOLVER_ERROR,
424                                        G_RESOLVER_ERROR_NOT_FOUND))
425         {
426           targets = g_network_service_fallback_targets (srv_enum->srv);
427           if (targets)
428             g_clear_error (&my_error);
429         }
430
431       if (my_error)
432         {
433           g_propagate_error (error, my_error);
434           return NULL;
435         }
436
437       srv_enum->srv->priv->targets = targets;
438       srv_enum->t = srv_enum->srv->priv->targets;
439     }
440
441   /* Delegate to GNetworkAddress */
442   do
443     {
444       if (srv_enum->addr_enum == NULL && srv_enum->t)
445         {
446           GError *error = NULL;
447           gchar *uri;
448           gchar *hostname;
449           GSocketConnectable *addr;
450           GSrvTarget *target = srv_enum->t->data;
451
452           srv_enum->t = g_list_next (srv_enum->t);
453
454           hostname = g_hostname_to_ascii (g_srv_target_get_hostname (target));
455
456           if (hostname == NULL)
457             {
458               if (srv_enum->error == NULL)
459                 srv_enum->error =
460                   g_error_new (G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
461                                "Received invalid hostname '%s' from GSrvTarget",
462                                g_srv_target_get_hostname (target));
463               continue;
464             }
465
466           uri = _g_uri_from_authority (g_network_service_get_scheme (srv_enum->srv),
467                                        hostname,
468                                        g_srv_target_get_port (target),
469                                        NULL);
470           g_free (hostname);
471
472           addr = g_network_address_parse_uri (uri,
473                                               g_srv_target_get_port (target),
474                                               &error);
475           g_free (uri);
476
477           if (addr == NULL)
478             {
479               if (srv_enum->error == NULL)
480                 srv_enum->error = error;
481               continue;
482             }
483
484           if (srv_enum->use_proxy)
485             srv_enum->addr_enum = g_socket_connectable_proxy_enumerate (addr);
486           else
487             srv_enum->addr_enum = g_socket_connectable_enumerate (addr);
488           g_object_unref (addr);
489         }
490
491       if (srv_enum->addr_enum)
492         {
493           GError *error = NULL;
494
495           ret = g_socket_address_enumerator_next (srv_enum->addr_enum,
496                                                   cancellable,
497                                                   &error);
498
499           if (error)
500             {
501               if (srv_enum->error == NULL)
502                 srv_enum->error = error;
503               else
504                 g_error_free (error);
505             }
506
507           if (!ret)
508             {
509               g_object_unref (srv_enum->addr_enum);
510               srv_enum->addr_enum = NULL;
511             }
512         }
513     }
514   while (srv_enum->addr_enum == NULL && srv_enum->t);
515
516   if (ret == NULL && srv_enum->error)
517     {
518       g_propagate_error (error, srv_enum->error);
519       srv_enum->error = NULL;
520     }
521
522   return ret;
523 }
524
525 static void next_async_resolved_targets   (GObject      *source_object,
526                                            GAsyncResult *result,
527                                            gpointer      user_data);
528 static void next_async_have_targets       (GTask        *srv_enum);
529 static void next_async_have_address       (GObject      *source_object,
530                                            GAsyncResult *result,
531                                            gpointer      user_data);
532
533 static void
534 g_network_service_address_enumerator_next_async (GSocketAddressEnumerator  *enumerator,
535                                                  GCancellable              *cancellable,
536                                                  GAsyncReadyCallback        callback,
537                                                  gpointer                   user_data)
538 {
539   GNetworkServiceAddressEnumerator *srv_enum =
540     G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
541   GTask *task;
542
543   task = g_task_new (enumerator, cancellable, callback, user_data);
544
545   /* If we haven't yet resolved srv, do that */
546   if (!srv_enum->srv->priv->targets)
547     {
548       g_resolver_lookup_service_async (srv_enum->resolver,
549                                        srv_enum->srv->priv->service,
550                                        srv_enum->srv->priv->protocol,
551                                        srv_enum->srv->priv->domain,
552                                        cancellable,
553                                        next_async_resolved_targets,
554                                        task);
555     }
556   else
557     next_async_have_targets (task);
558 }
559
560 static void
561 next_async_resolved_targets (GObject      *source_object,
562                              GAsyncResult *result,
563                              gpointer      user_data)
564 {
565   GTask *task = user_data;
566   GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
567   GError *error = NULL;
568   GList *targets;
569
570   targets = g_resolver_lookup_service_finish (srv_enum->resolver,
571                                               result, &error);
572
573   if (!targets && g_error_matches (error, G_RESOLVER_ERROR,
574                                    G_RESOLVER_ERROR_NOT_FOUND))
575     {
576       targets = g_network_service_fallback_targets (srv_enum->srv);
577       if (targets)
578         g_clear_error (&error);
579     }
580
581   if (error)
582     {
583       g_task_return_error (task, error);
584       g_object_unref (task);
585     }
586   else
587     {
588       srv_enum->t = srv_enum->srv->priv->targets = targets;
589       next_async_have_targets (task);
590     }
591 }
592
593 static void
594 next_async_have_targets (GTask *task)
595 {
596   GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
597
598   /* Delegate to GNetworkAddress */
599   if (srv_enum->addr_enum == NULL && srv_enum->t)
600     {
601       GSocketConnectable *addr;
602       GSrvTarget *target = srv_enum->t->data;
603
604       srv_enum->t = g_list_next (srv_enum->t);
605       addr = g_network_address_new (g_srv_target_get_hostname (target),
606                                     (guint16) g_srv_target_get_port (target));
607
608       if (srv_enum->use_proxy)
609         srv_enum->addr_enum = g_socket_connectable_proxy_enumerate (addr);
610       else
611         srv_enum->addr_enum = g_socket_connectable_enumerate (addr);
612
613       g_object_unref (addr);
614     }
615
616   if (srv_enum->addr_enum)
617     {
618       g_socket_address_enumerator_next_async (srv_enum->addr_enum,
619                                               g_task_get_cancellable (task),
620                                               next_async_have_address,
621                                               task);
622     }
623   else
624     {
625       if (srv_enum->error)
626         {
627           g_task_return_error (task, srv_enum->error);
628           srv_enum->error = NULL;
629         }
630       else
631         g_task_return_pointer (task, NULL, NULL);
632
633       g_object_unref (task);
634     }
635 }
636
637 static void
638 next_async_have_address (GObject      *source_object,
639                          GAsyncResult *result,
640                          gpointer      user_data)
641 {
642   GTask *task = user_data;
643   GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
644   GSocketAddress *address;
645   GError *error = NULL;
646   
647   address = g_socket_address_enumerator_next_finish (srv_enum->addr_enum,
648                                                      result,
649                                                      &error);
650
651   if (error)
652     {
653       if (srv_enum->error == NULL)
654         srv_enum->error = error;
655       else
656         g_error_free (error);
657     }
658
659   if (!address)
660     {
661       g_object_unref (srv_enum->addr_enum);
662       srv_enum->addr_enum = NULL;
663
664       next_async_have_targets (task);
665     }
666   else
667     {
668       g_task_return_pointer (task, address, g_object_unref);
669       g_object_unref (task);
670     }
671 }
672
673 static GSocketAddress *
674 g_network_service_address_enumerator_next_finish (GSocketAddressEnumerator  *enumerator,
675                                                   GAsyncResult              *result,
676                                                   GError                   **error)
677 {
678   return g_task_propagate_pointer (G_TASK (result), error);
679 }
680
681 static void
682 _g_network_service_address_enumerator_init (GNetworkServiceAddressEnumerator *enumerator)
683 {
684 }
685
686 static void
687 g_network_service_address_enumerator_finalize (GObject *object)
688 {
689   GNetworkServiceAddressEnumerator *srv_enum =
690     G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (object);
691
692   if (srv_enum->srv)
693     g_object_unref (srv_enum->srv);
694
695   if (srv_enum->addr_enum)
696     g_object_unref (srv_enum->addr_enum);
697
698   if (srv_enum->resolver)
699     g_object_unref (srv_enum->resolver);
700
701   if (srv_enum->error)
702     g_error_free (srv_enum->error);
703
704   G_OBJECT_CLASS (_g_network_service_address_enumerator_parent_class)->finalize (object);
705 }
706
707 static void
708 _g_network_service_address_enumerator_class_init (GNetworkServiceAddressEnumeratorClass *srvenum_class)
709 {
710   GObjectClass *object_class = G_OBJECT_CLASS (srvenum_class);
711   GSocketAddressEnumeratorClass *enumerator_class =
712     G_SOCKET_ADDRESS_ENUMERATOR_CLASS (srvenum_class);
713
714   enumerator_class->next        = g_network_service_address_enumerator_next;
715   enumerator_class->next_async  = g_network_service_address_enumerator_next_async;
716   enumerator_class->next_finish = g_network_service_address_enumerator_next_finish;
717
718   object_class->finalize = g_network_service_address_enumerator_finalize;
719 }
720
721 static GSocketAddressEnumerator *
722 g_network_service_connectable_enumerate (GSocketConnectable *connectable)
723 {
724   GNetworkServiceAddressEnumerator *srv_enum;
725
726   srv_enum = g_object_new (G_TYPE_NETWORK_SERVICE_ADDRESS_ENUMERATOR, NULL);
727   srv_enum->srv = g_object_ref (connectable);
728   srv_enum->resolver = g_resolver_get_default ();
729   srv_enum->use_proxy = FALSE;
730
731   return G_SOCKET_ADDRESS_ENUMERATOR (srv_enum);
732 }
733
734 static GSocketAddressEnumerator *
735 g_network_service_connectable_proxy_enumerate (GSocketConnectable *connectable)
736 {
737   GSocketAddressEnumerator *addr_enum;
738   GNetworkServiceAddressEnumerator *srv_enum;
739
740   addr_enum = g_network_service_connectable_enumerate (connectable);
741   srv_enum = G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (addr_enum);
742   srv_enum->use_proxy = TRUE;
743
744   return addr_enum;
745 }