Docs: don't use the type tag
[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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Christian Kellner <gicmo@gnome.org>
21  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
22  */
23
24 #include <config.h>
25 #include <glib.h>
26 #include <string.h>
27
28 #include "ginetsocketaddress.h"
29 #include "ginetaddress.h"
30 #include "gnetworkingprivate.h"
31 #include "gioerror.h"
32 #include "glibintl.h"
33
34
35 /**
36  * SECTION:ginetsocketaddress
37  * @short_description: Internet GSocketAddress
38  * @include: gio/gio.h
39  *
40  * An IPv4 or IPv6 socket address; that is, the combination of a
41  * #GInetAddress and a port number.
42  */
43
44 /**
45  * GInetSocketAddress:
46  *
47  * An IPv4 or IPv6 socket address, corresponding to a struct
48  * sockaddr_in or struct sockaddr_in6.
49  */
50
51 struct _GInetSocketAddressPrivate
52 {
53   GInetAddress *address;
54   guint16       port;
55   guint32       flowinfo;
56   guint32       scope_id;
57 };
58
59 G_DEFINE_TYPE_WITH_PRIVATE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS)
60
61 enum {
62   PROP_0,
63   PROP_ADDRESS,
64   PROP_PORT,
65   PROP_FLOWINFO,
66   PROP_SCOPE_ID
67 };
68
69 static void
70 g_inet_socket_address_dispose (GObject *object)
71 {
72   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
73
74   g_clear_object (&(address->priv->address));
75
76   G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose (object);
77 }
78
79 static void
80 g_inet_socket_address_get_property (GObject    *object,
81                                     guint       prop_id,
82                                     GValue     *value,
83                                     GParamSpec *pspec)
84 {
85   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
86
87   switch (prop_id)
88     {
89       case PROP_ADDRESS:
90         g_value_set_object (value, address->priv->address);
91         break;
92
93       case PROP_PORT:
94         g_value_set_uint (value, address->priv->port);
95         break;
96
97       case PROP_FLOWINFO:
98         g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
99         g_value_set_uint (value, address->priv->flowinfo);
100         break;
101
102       case PROP_SCOPE_ID:
103         g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
104         g_value_set_uint (value, address->priv->scope_id);
105         break;
106
107       default:
108         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109     }
110 }
111
112 static void
113 g_inet_socket_address_set_property (GObject      *object,
114                                     guint         prop_id,
115                                     const GValue *value,
116                                     GParamSpec   *pspec)
117 {
118   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
119
120   switch (prop_id)
121     {
122       case PROP_ADDRESS:
123         address->priv->address = g_object_ref (g_value_get_object (value));
124         break;
125
126       case PROP_PORT:
127         address->priv->port = (guint16) g_value_get_uint (value);
128         break;
129
130       case PROP_FLOWINFO:
131         /* We can't test that address->priv->address is IPv6 here,
132          * since this property might get set before PROP_ADDRESS.
133          */
134         address->priv->flowinfo = g_value_get_uint (value);
135         break;
136
137       case PROP_SCOPE_ID:
138         address->priv->scope_id = g_value_get_uint (value);
139         break;
140
141       default:
142         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143     }
144 }
145
146 static GSocketFamily
147 g_inet_socket_address_get_family (GSocketAddress *address)
148 {
149   GInetSocketAddress *addr;
150
151   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
152
153   addr = G_INET_SOCKET_ADDRESS (address);
154
155   return g_inet_address_get_family (addr->priv->address);
156 }
157
158 static gssize
159 g_inet_socket_address_get_native_size (GSocketAddress *address)
160 {
161   GInetSocketAddress *addr;
162   GSocketFamily family;
163
164   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
165
166   addr = G_INET_SOCKET_ADDRESS (address);
167   family = g_inet_address_get_family (addr->priv->address);
168
169   if (family == AF_INET)
170     return sizeof (struct sockaddr_in);
171   else if (family == AF_INET6)
172     return sizeof (struct sockaddr_in6);
173   else
174     return -1;
175 }
176
177 static gboolean
178 g_inet_socket_address_to_native (GSocketAddress  *address,
179                                  gpointer         dest,
180                                  gsize            destlen,
181                                  GError         **error)
182 {
183   GInetSocketAddress *addr;
184   GSocketFamily family;
185
186   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), FALSE);
187
188   addr = G_INET_SOCKET_ADDRESS (address);
189   family = g_inet_address_get_family (addr->priv->address);
190
191   if (family == AF_INET)
192     {
193       struct sockaddr_in *sock = (struct sockaddr_in *) dest;
194
195       if (destlen < sizeof (*sock))
196         {
197           g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
198                                _("Not enough space for socket address"));
199           return FALSE;
200         }
201
202       sock->sin_family = AF_INET;
203       sock->sin_port = g_htons (addr->priv->port);
204       memcpy (&(sock->sin_addr.s_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin_addr));
205       memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
206       return TRUE;
207     }
208   else if (family == AF_INET6)
209     {
210       struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
211
212       if (destlen < sizeof (*sock))
213         {
214           g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
215                                _("Not enough space for socket address"));
216           return FALSE;
217         }
218
219       memset (sock, 0, sizeof (*sock));
220       sock->sin6_family = AF_INET6;
221       sock->sin6_port = g_htons (addr->priv->port);
222       sock->sin6_flowinfo = addr->priv->flowinfo;
223       sock->sin6_scope_id = addr->priv->scope_id;
224       memcpy (&(sock->sin6_addr.s6_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin6_addr));
225       return TRUE;
226     }
227   else
228     {
229       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
230                            _("Unsupported socket address"));
231       return FALSE;
232     }
233 }
234
235 static void
236 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
237 {
238   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
239   GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
240
241   gobject_class->dispose = g_inet_socket_address_dispose;
242   gobject_class->set_property = g_inet_socket_address_set_property;
243   gobject_class->get_property = g_inet_socket_address_get_property;
244
245   gsocketaddress_class->get_family = g_inet_socket_address_get_family;
246   gsocketaddress_class->to_native = g_inet_socket_address_to_native;
247   gsocketaddress_class->get_native_size = g_inet_socket_address_get_native_size;
248
249   g_object_class_install_property (gobject_class, PROP_ADDRESS,
250                                    g_param_spec_object ("address",
251                                                         P_("Address"),
252                                                         P_("The address"),
253                                                         G_TYPE_INET_ADDRESS,
254                                                         G_PARAM_CONSTRUCT_ONLY |
255                                                         G_PARAM_READWRITE |
256                                                         G_PARAM_STATIC_STRINGS));
257
258   g_object_class_install_property (gobject_class, PROP_PORT,
259                                    g_param_spec_uint ("port",
260                                                       P_("Port"),
261                                                       P_("The port"),
262                                                       0,
263                                                       65535,
264                                                       0,
265                                                       G_PARAM_CONSTRUCT_ONLY |
266                                                       G_PARAM_READWRITE |
267                                                       G_PARAM_STATIC_STRINGS));
268
269   /**
270    * GInetSocketAddress:flowinfo:
271    *
272    * The <literal>sin6_flowinfo</literal> field, for IPv6 addresses.
273    *
274    * Since: 2.32
275    */
276   g_object_class_install_property (gobject_class, PROP_FLOWINFO,
277                                    g_param_spec_uint ("flowinfo",
278                                                       P_("Flow info"),
279                                                       P_("IPv6 flow info"),
280                                                       0,
281                                                       G_MAXUINT32,
282                                                       0,
283                                                       G_PARAM_CONSTRUCT_ONLY |
284                                                       G_PARAM_READWRITE |
285                                                       G_PARAM_STATIC_STRINGS));
286
287   /**
288    * GInetSocketAddress:scope_id:
289    *
290    * The <literal>sin6_scope_id</literal> field, for IPv6 addresses.
291    *
292    * Since: 2.32
293    */
294   g_object_class_install_property (gobject_class, PROP_SCOPE_ID,
295                                    g_param_spec_uint ("scope-id",
296                                                       P_("Scope ID"),
297                                                       P_("IPv6 scope ID"),
298                                                       0,
299                                                       G_MAXUINT32,
300                                                       0,
301                                                       G_PARAM_CONSTRUCT_ONLY |
302                                                       G_PARAM_READWRITE |
303                                                       G_PARAM_STATIC_STRINGS));
304 }
305
306 static void
307 g_inet_socket_address_init (GInetSocketAddress *address)
308 {
309   address->priv = g_inet_socket_address_get_instance_private (address);
310 }
311
312 /**
313  * g_inet_socket_address_new:
314  * @address: a #GInetAddress
315  * @port: a port number
316  *
317  * Creates a new #GInetSocketAddress for @address and @port.
318  *
319  * Returns: a new #GInetSocketAddress
320  *
321  * Since: 2.22
322  */
323 GSocketAddress *
324 g_inet_socket_address_new (GInetAddress *address,
325                            guint16       port)
326 {
327   return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
328                        "address", address,
329                        "port", port,
330                        NULL);
331 }
332
333 /**
334  * g_inet_socket_address_get_address:
335  * @address: a #GInetSocketAddress
336  *
337  * Gets @address's #GInetAddress.
338  *
339  * Returns: (transfer none): the #GInetAddress for @address, which must be
340  * g_object_ref()'d if it will be stored
341  *
342  * Since: 2.22
343  */
344 GInetAddress *
345 g_inet_socket_address_get_address (GInetSocketAddress *address)
346 {
347   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
348
349   return address->priv->address;
350 }
351
352 /**
353  * g_inet_socket_address_get_port:
354  * @address: a #GInetSocketAddress
355  *
356  * Gets @address's port.
357  *
358  * Returns: the port for @address
359  *
360  * Since: 2.22
361  */
362 guint16
363 g_inet_socket_address_get_port (GInetSocketAddress *address)
364 {
365   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
366
367   return address->priv->port;
368 }
369
370
371 /**
372  * g_inet_socket_address_get_flowinfo:
373  * @address: a %G_SOCKET_FAMILY_IPV6 #GInetSocketAddress
374  *
375  * Gets the <literal>sin6_flowinfo</literal> field from @address,
376  * which must be an IPv6 address.
377  *
378  * Return value: the flowinfo field
379  *
380  * Since: 2.32
381  */
382 guint32
383 g_inet_socket_address_get_flowinfo (GInetSocketAddress *address)
384 {
385   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
386   g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
387
388   return address->priv->flowinfo;
389 }
390
391 /**
392  * g_inet_socket_address_get_scope_id:
393  * @address: a %G_SOCKET_FAMILY_IPV6 #GInetAddress
394  *
395  * Gets the <literal>sin6_scope_id</literal> field from @address,
396  * which must be an IPv6 address.
397  *
398  * Return value: the scope id field
399  *
400  * Since: 2.32
401  */
402 guint32
403 g_inet_socket_address_get_scope_id (GInetSocketAddress *address)
404 {
405   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
406   g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
407
408   return address->priv->scope_id;
409 }