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, see <http://www.gnu.org/licenses/>.
18 * Authors: Christian Kellner <gicmo@gnome.org>
19 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
26 #include "ginetsocketaddress.h"
27 #include "ginetaddress.h"
28 #include "gnetworkingprivate.h"
34 * SECTION:ginetsocketaddress
35 * @short_description: Internet GSocketAddress
38 * An IPv4 or IPv6 socket address; that is, the combination of a
39 * #GInetAddress and a port number.
45 * An IPv4 or IPv6 socket address, corresponding to a struct
46 * sockaddr_in or struct sockaddr_in6.
49 struct _GInetSocketAddressPrivate
51 GInetAddress *address;
57 G_DEFINE_TYPE_WITH_PRIVATE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS)
68 g_inet_socket_address_dispose (GObject *object)
70 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
72 g_clear_object (&(address->priv->address));
74 G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose (object);
78 g_inet_socket_address_get_property (GObject *object,
83 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
88 g_value_set_object (value, address->priv->address);
92 g_value_set_uint (value, address->priv->port);
96 g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
97 g_value_set_uint (value, address->priv->flowinfo);
101 g_return_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6);
102 g_value_set_uint (value, address->priv->scope_id);
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111 g_inet_socket_address_set_property (GObject *object,
116 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
121 address->priv->address = g_object_ref (g_value_get_object (value));
125 address->priv->port = (guint16) g_value_get_uint (value);
129 /* We can't test that address->priv->address is IPv6 here,
130 * since this property might get set before PROP_ADDRESS.
132 address->priv->flowinfo = g_value_get_uint (value);
136 address->priv->scope_id = g_value_get_uint (value);
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145 g_inet_socket_address_get_family (GSocketAddress *address)
147 GInetSocketAddress *addr;
149 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
151 addr = G_INET_SOCKET_ADDRESS (address);
153 return g_inet_address_get_family (addr->priv->address);
157 g_inet_socket_address_get_native_size (GSocketAddress *address)
159 GInetSocketAddress *addr;
160 GSocketFamily family;
162 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
164 addr = G_INET_SOCKET_ADDRESS (address);
165 family = g_inet_address_get_family (addr->priv->address);
167 if (family == AF_INET)
168 return sizeof (struct sockaddr_in);
169 else if (family == AF_INET6)
170 return sizeof (struct sockaddr_in6);
176 g_inet_socket_address_to_native (GSocketAddress *address,
181 GInetSocketAddress *addr;
182 GSocketFamily family;
184 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), FALSE);
186 addr = G_INET_SOCKET_ADDRESS (address);
187 family = g_inet_address_get_family (addr->priv->address);
189 if (family == AF_INET)
191 struct sockaddr_in *sock = (struct sockaddr_in *) dest;
193 if (destlen < sizeof (*sock))
195 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
196 _("Not enough space for socket address"));
200 sock->sin_family = AF_INET;
201 sock->sin_port = g_htons (addr->priv->port);
202 memcpy (&(sock->sin_addr.s_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin_addr));
203 memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
206 else if (family == AF_INET6)
208 struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
210 if (destlen < sizeof (*sock))
212 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
213 _("Not enough space for socket address"));
217 memset (sock, 0, sizeof (*sock));
218 sock->sin6_family = AF_INET6;
219 sock->sin6_port = g_htons (addr->priv->port);
220 sock->sin6_flowinfo = addr->priv->flowinfo;
221 sock->sin6_scope_id = addr->priv->scope_id;
222 memcpy (&(sock->sin6_addr.s6_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin6_addr));
227 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
228 _("Unsupported socket address"));
234 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
236 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
237 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
239 gobject_class->dispose = g_inet_socket_address_dispose;
240 gobject_class->set_property = g_inet_socket_address_set_property;
241 gobject_class->get_property = g_inet_socket_address_get_property;
243 gsocketaddress_class->get_family = g_inet_socket_address_get_family;
244 gsocketaddress_class->to_native = g_inet_socket_address_to_native;
245 gsocketaddress_class->get_native_size = g_inet_socket_address_get_native_size;
247 g_object_class_install_property (gobject_class, PROP_ADDRESS,
248 g_param_spec_object ("address",
252 G_PARAM_CONSTRUCT_ONLY |
254 G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (gobject_class, PROP_PORT,
257 g_param_spec_uint ("port",
263 G_PARAM_CONSTRUCT_ONLY |
265 G_PARAM_STATIC_STRINGS));
268 * GInetSocketAddress:flowinfo:
270 * The `sin6_flowinfo` field, for IPv6 addresses.
274 g_object_class_install_property (gobject_class, PROP_FLOWINFO,
275 g_param_spec_uint ("flowinfo",
277 P_("IPv6 flow info"),
281 G_PARAM_CONSTRUCT_ONLY |
283 G_PARAM_STATIC_STRINGS));
286 * GInetSocketAddress:scope_id:
288 * The `sin6_scope_id` field, for IPv6 addresses.
292 g_object_class_install_property (gobject_class, PROP_SCOPE_ID,
293 g_param_spec_uint ("scope-id",
299 G_PARAM_CONSTRUCT_ONLY |
301 G_PARAM_STATIC_STRINGS));
305 g_inet_socket_address_init (GInetSocketAddress *address)
307 address->priv = g_inet_socket_address_get_instance_private (address);
311 * g_inet_socket_address_new:
312 * @address: a #GInetAddress
313 * @port: a port number
315 * Creates a new #GInetSocketAddress for @address and @port.
317 * Returns: a new #GInetSocketAddress
322 g_inet_socket_address_new (GInetAddress *address,
325 return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
332 * g_inet_socket_address_new_from_string:
333 * @address: the string form of an IP address
334 * @port: a port number
336 * Creates a new #GInetSocketAddress for @address and @port.
338 * If @address is an IPv6 address, it can also contain a scope ID
339 * (separated from the address by a "<literal>%</literal>").
341 * Returns: a new #GInetSocketAddress, or %NULL if @address cannot be
347 g_inet_socket_address_new_from_string (const char *address,
350 static struct addrinfo *hints, hints_struct;
351 GSocketAddress *saddr;
353 struct addrinfo *res;
356 if (strchr (address, ':'))
358 /* IPv6 address (or it's invalid). We use getaddrinfo() because
359 * it will handle parsing a scope_id as well.
362 if (G_UNLIKELY (g_once_init_enter (&hints)))
364 hints_struct.ai_socktype = SOCK_STREAM;
365 hints_struct.ai_flags = AI_NUMERICHOST;
366 g_once_init_leave (&hints, &hints_struct);
369 status = getaddrinfo (address, NULL, hints, &res);
373 if (res->ai_family == AF_INET6 &&
374 res->ai_addrlen == sizeof (struct sockaddr_in6))
376 ((struct sockaddr_in6 *)res->ai_addr)->sin6_port = g_htons (port);
377 saddr = g_socket_address_new_from_native (res->ai_addr, res->ai_addrlen);
386 /* IPv4 (or invalid). We don't want to use getaddrinfo() here,
387 * because it accepts the stupid "IPv4 numbers-and-dots
388 * notation" addresses that are never used for anything except
389 * phishing. Since we don't have to worry about scope IDs for
390 * IPv4, we can just use g_inet_address_new_from_string().
392 iaddr = g_inet_address_new_from_string (address);
396 g_warn_if_fail (g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV4);
398 saddr = g_inet_socket_address_new (iaddr, port);
399 g_object_unref (iaddr);
406 * g_inet_socket_address_get_address:
407 * @address: a #GInetSocketAddress
409 * Gets @address's #GInetAddress.
411 * Returns: (transfer none): the #GInetAddress for @address, which must be
412 * g_object_ref()'d if it will be stored
417 g_inet_socket_address_get_address (GInetSocketAddress *address)
419 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
421 return address->priv->address;
425 * g_inet_socket_address_get_port:
426 * @address: a #GInetSocketAddress
428 * Gets @address's port.
430 * Returns: the port for @address
435 g_inet_socket_address_get_port (GInetSocketAddress *address)
437 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
439 return address->priv->port;
444 * g_inet_socket_address_get_flowinfo:
445 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetSocketAddress
447 * Gets the `sin6_flowinfo` field from @address,
448 * which must be an IPv6 address.
450 * Returns: the flowinfo field
455 g_inet_socket_address_get_flowinfo (GInetSocketAddress *address)
457 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
458 g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
460 return address->priv->flowinfo;
464 * g_inet_socket_address_get_scope_id:
465 * @address: a %G_SOCKET_FAMILY_IPV6 #GInetAddress
467 * Gets the `sin6_scope_id` field from @address,
468 * which must be an IPv6 address.
470 * Returns: the scope id field
475 g_inet_socket_address_get_scope_id (GInetSocketAddress *address)
477 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
478 g_return_val_if_fail (g_inet_address_get_family (address->priv->address) == G_SOCKET_FAMILY_IPV6, 0);
480 return address->priv->scope_id;