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>.
50 struct _GInetSocketAddressPrivate
52 GInetAddress *address;
58 G_DEFINE_TYPE_WITH_PRIVATE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS)
69 g_inet_socket_address_dispose (GObject *object)
71 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
73 g_clear_object (&(address->priv->address));
75 G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose (object);
79 g_inet_socket_address_get_property (GObject *object,
84 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
89 g_value_set_object (value, address->priv->address);
93 g_value_set_uint (value, address->priv->port);
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);
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);
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112 g_inet_socket_address_set_property (GObject *object,
117 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
122 address->priv->address = g_object_ref (g_value_get_object (value));
126 address->priv->port = (guint16) g_value_get_uint (value);
130 /* We can't test that address->priv->address is IPv6 here,
131 * since this property might get set before PROP_ADDRESS.
133 address->priv->flowinfo = g_value_get_uint (value);
137 address->priv->scope_id = g_value_get_uint (value);
141 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146 g_inet_socket_address_get_family (GSocketAddress *address)
148 GInetSocketAddress *addr;
150 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
152 addr = G_INET_SOCKET_ADDRESS (address);
154 return g_inet_address_get_family (addr->priv->address);
158 g_inet_socket_address_get_native_size (GSocketAddress *address)
160 GInetSocketAddress *addr;
161 GSocketFamily family;
163 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
165 addr = G_INET_SOCKET_ADDRESS (address);
166 family = g_inet_address_get_family (addr->priv->address);
168 if (family == AF_INET)
169 return sizeof (struct sockaddr_in);
170 else if (family == AF_INET6)
171 return sizeof (struct sockaddr_in6);
177 g_inet_socket_address_to_native (GSocketAddress *address,
182 GInetSocketAddress *addr;
183 GSocketFamily family;
185 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), FALSE);
187 addr = G_INET_SOCKET_ADDRESS (address);
188 family = g_inet_address_get_family (addr->priv->address);
190 if (family == AF_INET)
192 struct sockaddr_in *sock = (struct sockaddr_in *) dest;
194 if (destlen < sizeof (*sock))
196 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
197 _("Not enough space for socket address"));
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));
207 else if (family == AF_INET6)
209 struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
211 if (destlen < sizeof (*sock))
213 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
214 _("Not enough space for socket address"));
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));
228 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
229 _("Unsupported socket address"));
235 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
237 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
238 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
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;
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;
248 g_object_class_install_property (gobject_class, PROP_ADDRESS,
249 g_param_spec_object ("address",
253 G_PARAM_CONSTRUCT_ONLY |
255 G_PARAM_STATIC_STRINGS));
257 g_object_class_install_property (gobject_class, PROP_PORT,
258 g_param_spec_uint ("port",
264 G_PARAM_CONSTRUCT_ONLY |
266 G_PARAM_STATIC_STRINGS));
269 * GInetSocketAddress:flowinfo:
271 * The <literal>sin6_flowinfo</literal> field, for IPv6 addresses.
275 g_object_class_install_property (gobject_class, PROP_FLOWINFO,
276 g_param_spec_uint ("flowinfo",
278 P_("IPv6 flow info"),
282 G_PARAM_CONSTRUCT_ONLY |
284 G_PARAM_STATIC_STRINGS));
287 * GInetSocketAddress:scope_id:
289 * The <literal>sin6_scope_id</literal> field, for IPv6 addresses.
293 g_object_class_install_property (gobject_class, PROP_SCOPE_ID,
294 g_param_spec_uint ("scope-id",
300 G_PARAM_CONSTRUCT_ONLY |
302 G_PARAM_STATIC_STRINGS));
306 g_inet_socket_address_init (GInetSocketAddress *address)
308 address->priv = g_inet_socket_address_get_instance_private (address);
312 * g_inet_socket_address_new:
313 * @address: a #GInetAddress
314 * @port: a port number
316 * Creates a new #GInetSocketAddress for @address and @port.
318 * Returns: a new #GInetSocketAddress
323 g_inet_socket_address_new (GInetAddress *address,
326 return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
333 * g_inet_socket_address_get_address:
334 * @address: a #GInetSocketAddress
336 * Gets @address's #GInetAddress.
338 * Returns: (transfer none): the #GInetAddress for @address, which must be
339 * g_object_ref()'d if it will be stored
344 g_inet_socket_address_get_address (GInetSocketAddress *address)
346 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
348 return address->priv->address;
352 * g_inet_socket_address_get_port:
353 * @address: a #GInetSocketAddress
355 * Gets @address's port.
357 * Returns: the port for @address
362 g_inet_socket_address_get_port (GInetSocketAddress *address)
364 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
366 return address->priv->port;
371 * g_inet_socket_address_get_flowinfo:
372 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetSocketAddress
374 * Gets the <literal>sin6_flowinfo</literal> field from @address,
375 * which must be an IPv6 address.
377 * Return value: the flowinfo field
382 g_inet_socket_address_get_flowinfo (GInetSocketAddress *address)
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);
387 return address->priv->flowinfo;
391 * g_inet_socket_address_get_scope_id:
392 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetAddress
394 * Gets the <literal>sin6_scope_id</literal> field from @address,
395 * which must be an IPv6 address.
397 * Return value: the scope id field
402 g_inet_socket_address_get_scope_id (GInetSocketAddress *address)
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);
407 return address->priv->scope_id;