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
40 * An IPv4 or IPv6 socket address; that is, the combination of a
41 * #GInetAddress and a port number.
47 * An IPv4 or IPv6 socket address, corresponding to a <type>struct
48 * sockaddr_in</type> or <type>struct sockaddr_in6</type>.
51 struct _GInetSocketAddressPrivate
53 GInetAddress *address;
59 G_DEFINE_TYPE_WITH_PRIVATE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS)
70 g_inet_socket_address_dispose (GObject *object)
72 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
74 g_clear_object (&(address->priv->address));
76 G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose (object);
80 g_inet_socket_address_get_property (GObject *object,
85 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
90 g_value_set_object (value, address->priv->address);
94 g_value_set_uint (value, address->priv->port);
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);
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);
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 g_inet_socket_address_set_property (GObject *object,
118 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
123 address->priv->address = g_object_ref (g_value_get_object (value));
127 address->priv->port = (guint16) g_value_get_uint (value);
131 /* We can't test that address->priv->address is IPv6 here,
132 * since this property might get set before PROP_ADDRESS.
134 address->priv->flowinfo = g_value_get_uint (value);
138 address->priv->scope_id = g_value_get_uint (value);
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147 g_inet_socket_address_get_family (GSocketAddress *address)
149 GInetSocketAddress *addr;
151 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
153 addr = G_INET_SOCKET_ADDRESS (address);
155 return g_inet_address_get_family (addr->priv->address);
159 g_inet_socket_address_get_native_size (GSocketAddress *address)
161 GInetSocketAddress *addr;
162 GSocketFamily family;
164 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
166 addr = G_INET_SOCKET_ADDRESS (address);
167 family = g_inet_address_get_family (addr->priv->address);
169 if (family == AF_INET)
170 return sizeof (struct sockaddr_in);
171 else if (family == AF_INET6)
172 return sizeof (struct sockaddr_in6);
178 g_inet_socket_address_to_native (GSocketAddress *address,
183 GInetSocketAddress *addr;
184 GSocketFamily family;
186 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), FALSE);
188 addr = G_INET_SOCKET_ADDRESS (address);
189 family = g_inet_address_get_family (addr->priv->address);
191 if (family == AF_INET)
193 struct sockaddr_in *sock = (struct sockaddr_in *) dest;
195 if (destlen < sizeof (*sock))
197 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
198 _("Not enough space for socket address"));
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));
208 else if (family == AF_INET6)
210 struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
212 if (destlen < sizeof (*sock))
214 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
215 _("Not enough space for socket address"));
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));
229 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
230 _("Unsupported socket address"));
236 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
238 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
239 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
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;
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;
249 g_object_class_install_property (gobject_class, PROP_ADDRESS,
250 g_param_spec_object ("address",
254 G_PARAM_CONSTRUCT_ONLY |
256 G_PARAM_STATIC_STRINGS));
258 g_object_class_install_property (gobject_class, PROP_PORT,
259 g_param_spec_uint ("port",
265 G_PARAM_CONSTRUCT_ONLY |
267 G_PARAM_STATIC_STRINGS));
270 * GInetSocketAddress:flowinfo:
272 * The <literal>sin6_flowinfo</literal> field, for IPv6 addresses.
276 g_object_class_install_property (gobject_class, PROP_FLOWINFO,
277 g_param_spec_uint ("flowinfo",
279 P_("IPv6 flow info"),
283 G_PARAM_CONSTRUCT_ONLY |
285 G_PARAM_STATIC_STRINGS));
288 * GInetSocketAddress:scope_id:
290 * The <literal>sin6_scope_id</literal> field, for IPv6 addresses.
294 g_object_class_install_property (gobject_class, PROP_SCOPE_ID,
295 g_param_spec_uint ("scope-id",
301 G_PARAM_CONSTRUCT_ONLY |
303 G_PARAM_STATIC_STRINGS));
307 g_inet_socket_address_init (GInetSocketAddress *address)
309 address->priv = g_inet_socket_address_get_instance_private (address);
313 * g_inet_socket_address_new:
314 * @address: a #GInetAddress
315 * @port: a port number
317 * Creates a new #GInetSocketAddress for @address and @port.
319 * Returns: a new #GInetSocketAddress
324 g_inet_socket_address_new (GInetAddress *address,
327 return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
334 * g_inet_socket_address_get_address:
335 * @address: a #GInetSocketAddress
337 * Gets @address's #GInetAddress.
339 * Returns: (transfer none): the #GInetAddress for @address, which must be
340 * g_object_ref()'d if it will be stored
345 g_inet_socket_address_get_address (GInetSocketAddress *address)
347 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
349 return address->priv->address;
353 * g_inet_socket_address_get_port:
354 * @address: a #GInetSocketAddress
356 * Gets @address's port.
358 * Returns: the port for @address
363 g_inet_socket_address_get_port (GInetSocketAddress *address)
365 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
367 return address->priv->port;
372 * g_inet_socket_address_get_flowinfo:
373 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetSocketAddress
375 * Gets the <literal>sin6_flowinfo</literal> field from @address,
376 * which must be an IPv6 address.
378 * Return value: the flowinfo field
383 g_inet_socket_address_get_flowinfo (GInetSocketAddress *address)
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);
388 return address->priv->flowinfo;
392 * g_inet_socket_address_get_scope_id:
393 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetAddress
395 * Gets the <literal>sin6_scope_id</literal> field from @address,
396 * which must be an IPv6 address.
398 * Return value: the scope id field
403 g_inet_socket_address_get_scope_id (GInetSocketAddress *address)
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);
408 return address->priv->scope_id;