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