1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
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.
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.
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.
20 * Authors: Christian Kellner <gicmo@gnome.org>
21 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
28 #include "ginetsocketaddress.h"
29 #include "ginetaddress.h"
30 #include "gnetworkingprivate.h"
36 * SECTION:ginetsocketaddress
37 * @short_description: Internet GSocketAddress
39 * An IPv4 or IPv6 socket address; that is, the combination of a
40 * #GInetAddress and a port number.
46 * An IPv4 or IPv6 socket address, corresponding to a <type>struct
47 * sockaddr_in</type> or <type>struct sockaddr_in6</type>.
49 G_DEFINE_TYPE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS);
59 struct _GInetSocketAddressPrivate
61 GInetAddress *address;
68 g_inet_socket_address_finalize (GObject *object)
70 GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
72 if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize)
73 (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize) (object);
77 g_inet_socket_address_dispose (GObject *object)
79 GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
81 g_object_unref (address->priv->address);
83 if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose)
84 (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose) (object);
88 g_inet_socket_address_get_property (GObject *object,
93 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
98 g_value_set_object (value, address->priv->address);
102 g_value_set_uint (value, address->priv->port);
106 g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
107 g_value_set_uint (value, address->priv->flowinfo);
111 g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
112 g_value_set_uint (value, address->priv->scope_id);
116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121 g_inet_socket_address_set_property (GObject *object,
126 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
131 address->priv->address = g_object_ref (g_value_get_object (value));
135 address->priv->port = (guint16) g_value_get_uint (value);
139 /* We can't test that address->priv->address is IPv6 here,
140 * since this property might get set before PROP_ADDRESS.
142 address->priv->flowinfo = g_value_get_uint (value);
146 address->priv->scope_id = g_value_get_uint (value);
150 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
155 g_inet_socket_address_get_family (GSocketAddress *address)
157 GInetSocketAddress *addr;
159 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
161 addr = G_INET_SOCKET_ADDRESS (address);
163 return g_inet_address_get_family (addr->priv->address);
167 g_inet_socket_address_get_native_size (GSocketAddress *address)
169 GInetSocketAddress *addr;
170 GSocketFamily family;
172 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
174 addr = G_INET_SOCKET_ADDRESS (address);
175 family = g_inet_address_get_family (addr->priv->address);
177 if (family == AF_INET)
178 return sizeof (struct sockaddr_in);
179 else if (family == AF_INET6)
180 return sizeof (struct sockaddr_in6);
186 g_inet_socket_address_to_native (GSocketAddress *address,
191 GInetSocketAddress *addr;
192 GSocketFamily family;
194 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), FALSE);
196 addr = G_INET_SOCKET_ADDRESS (address);
197 family = g_inet_address_get_family (addr->priv->address);
199 if (family == AF_INET)
201 struct sockaddr_in *sock = (struct sockaddr_in *) dest;
203 if (destlen < sizeof (*sock))
205 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
206 _("Not enough space for socket address"));
210 sock->sin_family = AF_INET;
211 sock->sin_port = g_htons (addr->priv->port);
212 memcpy (&(sock->sin_addr.s_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin_addr));
213 memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
216 else if (family == AF_INET6)
218 struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
220 if (destlen < sizeof (*sock))
222 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
223 _("Not enough space for socket address"));
227 memset (sock, 0, sizeof (*sock));
228 sock->sin6_family = AF_INET6;
229 sock->sin6_port = g_htons (addr->priv->port);
230 sock->sin6_flowinfo = addr->priv->flowinfo;
231 sock->sin6_scope_id = addr->priv->scope_id;
232 memcpy (&(sock->sin6_addr.s6_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin6_addr));
237 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
238 _("Unsupported socket address"));
244 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
246 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
247 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
249 g_type_class_add_private (klass, sizeof (GInetSocketAddressPrivate));
251 gobject_class->finalize = g_inet_socket_address_finalize;
252 gobject_class->dispose = g_inet_socket_address_dispose;
253 gobject_class->set_property = g_inet_socket_address_set_property;
254 gobject_class->get_property = g_inet_socket_address_get_property;
256 gsocketaddress_class->get_family = g_inet_socket_address_get_family;
257 gsocketaddress_class->to_native = g_inet_socket_address_to_native;
258 gsocketaddress_class->get_native_size = g_inet_socket_address_get_native_size;
260 g_object_class_install_property (gobject_class, PROP_ADDRESS,
261 g_param_spec_object ("address",
265 G_PARAM_CONSTRUCT_ONLY |
267 G_PARAM_STATIC_STRINGS));
269 g_object_class_install_property (gobject_class, PROP_PORT,
270 g_param_spec_uint ("port",
276 G_PARAM_CONSTRUCT_ONLY |
278 G_PARAM_STATIC_STRINGS));
281 * GInetSocketAddress:flowinfo:
283 * The <literal>sin6_flowinfo</literal> field, for IPv6 addresses.
287 g_object_class_install_property (gobject_class, PROP_FLOWINFO,
288 g_param_spec_uint ("flowinfo",
290 P_("IPv6 flow info"),
294 G_PARAM_CONSTRUCT_ONLY |
296 G_PARAM_STATIC_STRINGS));
299 * GInetSocketAddress:scope_id:
301 * The <literal>sin6_scope_id</literal> field, for IPv6 addresses.
305 g_object_class_install_property (gobject_class, PROP_SCOPE_ID,
306 g_param_spec_uint ("scope-id",
312 G_PARAM_CONSTRUCT_ONLY |
314 G_PARAM_STATIC_STRINGS));
318 g_inet_socket_address_init (GInetSocketAddress *address)
320 address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
321 G_TYPE_INET_SOCKET_ADDRESS,
322 GInetSocketAddressPrivate);
326 * g_inet_socket_address_new:
327 * @address: a #GInetAddress
328 * @port: a port number
330 * Creates a new #GInetSocketAddress for @address and @port.
332 * Returns: a new #GInetSocketAddress
337 g_inet_socket_address_new (GInetAddress *address,
340 return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
347 * g_inet_socket_address_get_address:
348 * @address: a #GInetSocketAddress
350 * Gets @address's #GInetAddress.
352 * Returns: (transfer none): the #GInetAddress for @address, which must be
353 * g_object_ref()'d if it will be stored
358 g_inet_socket_address_get_address (GInetSocketAddress *address)
360 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
362 return address->priv->address;
366 * g_inet_socket_address_get_port:
367 * @address: a #GInetSocketAddress
369 * Gets @address's port.
371 * Returns: the port for @address
376 g_inet_socket_address_get_port (GInetSocketAddress *address)
378 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
380 return address->priv->port;
385 * g_inet_socket_address_get_flowinfo:
386 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetSocketAddress
388 * Gets the <literal>sin6_flowinfo</literal> field from @address,
389 * which must be an IPv6 address.
391 * Return value: the flowinfo field
396 g_inet_socket_address_get_flowinfo (GInetSocketAddress *address)
398 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
399 g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
401 return address->priv->flowinfo;
405 * g_inet_socket_address_get_scope_id:
406 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetAddress
408 * Gets the <literal>sin6_scope_id</literal> field from @address,
409 * which must be an IPv6 address.
411 * Return value: the scope id field
416 g_inet_socket_address_get_scope_id (GInetSocketAddress *address)
418 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
419 g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
421 return address->priv->scope_id;