gcredentials: add NetBSD support
[platform/upstream/glib.git] / gio / ginetsocketaddress.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Authors: Christian Kellner <gicmo@gnome.org>
19  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
20  */
21
22 #include <config.h>
23 #include <glib.h>
24 #include <string.h>
25
26 #include "ginetsocketaddress.h"
27 #include "ginetaddress.h"
28 #include "gnetworkingprivate.h"
29 #include "gioerror.h"
30 #include "glibintl.h"
31
32
33 /**
34  * SECTION:ginetsocketaddress
35  * @short_description: Internet GSocketAddress
36  * @include: gio/gio.h
37  *
38  * An IPv4 or IPv6 socket address; that is, the combination of a
39  * #GInetAddress and a port number.
40  */
41
42 /**
43  * GInetSocketAddress:
44  *
45  * An IPv4 or IPv6 socket address, corresponding to a struct
46  * sockaddr_in or struct sockaddr_in6.
47  */
48
49 struct _GInetSocketAddressPrivate
50 {
51   GInetAddress *address;
52   guint16       port;
53   guint32       flowinfo;
54   guint32       scope_id;
55 };
56
57 G_DEFINE_TYPE_WITH_PRIVATE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS)
58
59 enum {
60   PROP_0,
61   PROP_ADDRESS,
62   PROP_PORT,
63   PROP_FLOWINFO,
64   PROP_SCOPE_ID
65 };
66
67 static void
68 g_inet_socket_address_dispose (GObject *object)
69 {
70   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
71
72   g_clear_object (&(address->priv->address));
73
74   G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose (object);
75 }
76
77 static void
78 g_inet_socket_address_get_property (GObject    *object,
79                                     guint       prop_id,
80                                     GValue     *value,
81                                     GParamSpec *pspec)
82 {
83   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
84
85   switch (prop_id)
86     {
87       case PROP_ADDRESS:
88         g_value_set_object (value, address->priv->address);
89         break;
90
91       case PROP_PORT:
92         g_value_set_uint (value, address->priv->port);
93         break;
94
95       case PROP_FLOWINFO:
96         g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
97         g_value_set_uint (value, address->priv->flowinfo);
98         break;
99
100       case PROP_SCOPE_ID:
101         g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
102         g_value_set_uint (value, address->priv->scope_id);
103         break;
104
105       default:
106         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107     }
108 }
109
110 static void
111 g_inet_socket_address_set_property (GObject      *object,
112                                     guint         prop_id,
113                                     const GValue *value,
114                                     GParamSpec   *pspec)
115 {
116   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
117
118   switch (prop_id)
119     {
120       case PROP_ADDRESS:
121         address->priv->address = g_object_ref (g_value_get_object (value));
122         break;
123
124       case PROP_PORT:
125         address->priv->port = (guint16) g_value_get_uint (value);
126         break;
127
128       case PROP_FLOWINFO:
129         /* We can't test that address->priv->address is IPv6 here,
130          * since this property might get set before PROP_ADDRESS.
131          */
132         address->priv->flowinfo = g_value_get_uint (value);
133         break;
134
135       case PROP_SCOPE_ID:
136         address->priv->scope_id = g_value_get_uint (value);
137         break;
138
139       default:
140         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141     }
142 }
143
144 static GSocketFamily
145 g_inet_socket_address_get_family (GSocketAddress *address)
146 {
147   GInetSocketAddress *addr;
148
149   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
150
151   addr = G_INET_SOCKET_ADDRESS (address);
152
153   return g_inet_address_get_family (addr->priv->address);
154 }
155
156 static gssize
157 g_inet_socket_address_get_native_size (GSocketAddress *address)
158 {
159   GInetSocketAddress *addr;
160   GSocketFamily family;
161
162   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
163
164   addr = G_INET_SOCKET_ADDRESS (address);
165   family = g_inet_address_get_family (addr->priv->address);
166
167   if (family == AF_INET)
168     return sizeof (struct sockaddr_in);
169   else if (family == AF_INET6)
170     return sizeof (struct sockaddr_in6);
171   else
172     return -1;
173 }
174
175 static gboolean
176 g_inet_socket_address_to_native (GSocketAddress  *address,
177                                  gpointer         dest,
178                                  gsize            destlen,
179                                  GError         **error)
180 {
181   GInetSocketAddress *addr;
182   GSocketFamily family;
183
184   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), FALSE);
185
186   addr = G_INET_SOCKET_ADDRESS (address);
187   family = g_inet_address_get_family (addr->priv->address);
188
189   if (family == AF_INET)
190     {
191       struct sockaddr_in *sock = (struct sockaddr_in *) dest;
192
193       if (destlen < sizeof (*sock))
194         {
195           g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
196                                _("Not enough space for socket address"));
197           return FALSE;
198         }
199
200       sock->sin_family = AF_INET;
201       sock->sin_port = g_htons (addr->priv->port);
202       memcpy (&(sock->sin_addr.s_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin_addr));
203       memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
204       return TRUE;
205     }
206   else if (family == AF_INET6)
207     {
208       struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
209
210       if (destlen < sizeof (*sock))
211         {
212           g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
213                                _("Not enough space for socket address"));
214           return FALSE;
215         }
216
217       memset (sock, 0, sizeof (*sock));
218       sock->sin6_family = AF_INET6;
219       sock->sin6_port = g_htons (addr->priv->port);
220       sock->sin6_flowinfo = addr->priv->flowinfo;
221       sock->sin6_scope_id = addr->priv->scope_id;
222       memcpy (&(sock->sin6_addr.s6_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin6_addr));
223       return TRUE;
224     }
225   else
226     {
227       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
228                            _("Unsupported socket address"));
229       return FALSE;
230     }
231 }
232
233 static void
234 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
235 {
236   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
237   GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
238
239   gobject_class->dispose = g_inet_socket_address_dispose;
240   gobject_class->set_property = g_inet_socket_address_set_property;
241   gobject_class->get_property = g_inet_socket_address_get_property;
242
243   gsocketaddress_class->get_family = g_inet_socket_address_get_family;
244   gsocketaddress_class->to_native = g_inet_socket_address_to_native;
245   gsocketaddress_class->get_native_size = g_inet_socket_address_get_native_size;
246
247   g_object_class_install_property (gobject_class, PROP_ADDRESS,
248                                    g_param_spec_object ("address",
249                                                         P_("Address"),
250                                                         P_("The address"),
251                                                         G_TYPE_INET_ADDRESS,
252                                                         G_PARAM_CONSTRUCT_ONLY |
253                                                         G_PARAM_READWRITE |
254                                                         G_PARAM_STATIC_STRINGS));
255
256   g_object_class_install_property (gobject_class, PROP_PORT,
257                                    g_param_spec_uint ("port",
258                                                       P_("Port"),
259                                                       P_("The port"),
260                                                       0,
261                                                       65535,
262                                                       0,
263                                                       G_PARAM_CONSTRUCT_ONLY |
264                                                       G_PARAM_READWRITE |
265                                                       G_PARAM_STATIC_STRINGS));
266
267   /**
268    * GInetSocketAddress:flowinfo:
269    *
270    * The `sin6_flowinfo` field, for IPv6 addresses.
271    *
272    * Since: 2.32
273    */
274   g_object_class_install_property (gobject_class, PROP_FLOWINFO,
275                                    g_param_spec_uint ("flowinfo",
276                                                       P_("Flow info"),
277                                                       P_("IPv6 flow info"),
278                                                       0,
279                                                       G_MAXUINT32,
280                                                       0,
281                                                       G_PARAM_CONSTRUCT_ONLY |
282                                                       G_PARAM_READWRITE |
283                                                       G_PARAM_STATIC_STRINGS));
284
285   /**
286    * GInetSocketAddress:scope_id:
287    *
288    * The `sin6_scope_id` field, for IPv6 addresses.
289    *
290    * Since: 2.32
291    */
292   g_object_class_install_property (gobject_class, PROP_SCOPE_ID,
293                                    g_param_spec_uint ("scope-id",
294                                                       P_("Scope ID"),
295                                                       P_("IPv6 scope ID"),
296                                                       0,
297                                                       G_MAXUINT32,
298                                                       0,
299                                                       G_PARAM_CONSTRUCT_ONLY |
300                                                       G_PARAM_READWRITE |
301                                                       G_PARAM_STATIC_STRINGS));
302 }
303
304 static void
305 g_inet_socket_address_init (GInetSocketAddress *address)
306 {
307   address->priv = g_inet_socket_address_get_instance_private (address);
308 }
309
310 /**
311  * g_inet_socket_address_new:
312  * @address: a #GInetAddress
313  * @port: a port number
314  *
315  * Creates a new #GInetSocketAddress for @address and @port.
316  *
317  * Returns: a new #GInetSocketAddress
318  *
319  * Since: 2.22
320  */
321 GSocketAddress *
322 g_inet_socket_address_new (GInetAddress *address,
323                            guint16       port)
324 {
325   return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
326                        "address", address,
327                        "port", port,
328                        NULL);
329 }
330
331 /**
332  * g_inet_socket_address_new_from_string:
333  * @address: the string form of an IP address
334  * @port: a port number
335  *
336  * Creates a new #GInetSocketAddress for @address and @port.
337  *
338  * If @address is an IPv6 address, it can also contain a scope ID
339  * (separated from the address by a "<literal>%</literal>").
340  *
341  * Returns: a new #GInetSocketAddress, or %NULL if @address cannot be
342  * parsed.
343  *
344  * Since: 2.40
345  */
346 GSocketAddress *
347 g_inet_socket_address_new_from_string (const char *address,
348                                        guint       port)
349 {
350   static struct addrinfo *hints, hints_struct;
351   GSocketAddress *saddr;
352   GInetAddress *iaddr;
353   struct addrinfo *res;
354   gint status;
355
356   if (strchr (address, ':'))
357     {
358       /* IPv6 address (or it's invalid). We use getaddrinfo() because
359        * it will handle parsing a scope_id as well.
360        */
361
362       if (G_UNLIKELY (g_once_init_enter (&hints)))
363         {
364           hints_struct.ai_socktype = SOCK_STREAM;
365           hints_struct.ai_flags = AI_NUMERICHOST;
366           g_once_init_leave (&hints, &hints_struct);
367         }
368
369       status = getaddrinfo (address, NULL, hints, &res);
370       if (status != 0)
371         return NULL;
372
373       if (res->ai_family == AF_INET6 &&
374           res->ai_addrlen == sizeof (struct sockaddr_in6))
375         {
376           ((struct sockaddr_in6 *)res->ai_addr)->sin6_port = g_htons (port);
377           saddr = g_socket_address_new_from_native (res->ai_addr, res->ai_addrlen);
378         }
379       else
380         saddr = NULL;
381
382       freeaddrinfo (res);
383     }
384   else
385     {
386       /* IPv4 (or invalid). We don't want to use getaddrinfo() here,
387        * because it accepts the stupid "IPv4 numbers-and-dots
388        * notation" addresses that are never used for anything except
389        * phishing. Since we don't have to worry about scope IDs for
390        * IPv4, we can just use g_inet_address_new_from_string().
391        */
392       iaddr = g_inet_address_new_from_string (address);
393       if (!iaddr)
394         return NULL;
395
396       g_warn_if_fail (g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV4);
397
398       saddr = g_inet_socket_address_new (iaddr, port);
399       g_object_unref (iaddr);
400     }
401
402   return saddr;
403 }
404
405 /**
406  * g_inet_socket_address_get_address:
407  * @address: a #GInetSocketAddress
408  *
409  * Gets @address's #GInetAddress.
410  *
411  * Returns: (transfer none): the #GInetAddress for @address, which must be
412  * g_object_ref()'d if it will be stored
413  *
414  * Since: 2.22
415  */
416 GInetAddress *
417 g_inet_socket_address_get_address (GInetSocketAddress *address)
418 {
419   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
420
421   return address->priv->address;
422 }
423
424 /**
425  * g_inet_socket_address_get_port:
426  * @address: a #GInetSocketAddress
427  *
428  * Gets @address's port.
429  *
430  * Returns: the port for @address
431  *
432  * Since: 2.22
433  */
434 guint16
435 g_inet_socket_address_get_port (GInetSocketAddress *address)
436 {
437   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
438
439   return address->priv->port;
440 }
441
442
443 /**
444  * g_inet_socket_address_get_flowinfo:
445  * @address: a %G_SOCKET_FAMILY_IPV6 #GInetSocketAddress
446  *
447  * Gets the `sin6_flowinfo` field from @address,
448  * which must be an IPv6 address.
449  *
450  * Returns: the flowinfo field
451  *
452  * Since: 2.32
453  */
454 guint32
455 g_inet_socket_address_get_flowinfo (GInetSocketAddress *address)
456 {
457   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
458   g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
459
460   return address->priv->flowinfo;
461 }
462
463 /**
464  * g_inet_socket_address_get_scope_id:
465  * @address: a %G_SOCKET_FAMILY_IPV6 #GInetAddress
466  *
467  * Gets the `sin6_scope_id` field from @address,
468  * which must be an IPv6 address.
469  *
470  * Returns: the scope id field
471  *
472  * Since: 2.32
473  */
474 guint32
475 g_inet_socket_address_get_scope_id (GInetSocketAddress *address)
476 {
477   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
478   g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
479
480   return address->priv->scope_id;
481 }