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"
37 * SECTION:ginetsocketaddress
38 * @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>.
50 G_DEFINE_TYPE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS);
58 struct _GInetSocketAddressPrivate
60 GInetAddress *address;
65 g_inet_socket_address_finalize (GObject *object)
67 GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
69 if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize)
70 (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize) (object);
74 g_inet_socket_address_dispose (GObject *object)
76 GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
78 g_object_unref (address->priv->address);
80 if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose)
81 (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose) (object);
85 g_inet_socket_address_get_property (GObject *object,
90 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
95 g_value_set_object (value, address->priv->address);
99 g_value_set_uint (value, address->priv->port);
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 g_inet_socket_address_set_property (GObject *object,
113 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
118 address->priv->address = g_object_ref (g_value_get_object (value));
122 address->priv->port = (guint16) g_value_get_uint (value);
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131 g_inet_socket_address_get_family (GSocketAddress *address)
133 GInetSocketAddress *addr;
135 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
137 addr = G_INET_SOCKET_ADDRESS (address);
139 return g_inet_address_get_family (addr->priv->address);
143 g_inet_socket_address_get_native_size (GSocketAddress *address)
145 GInetSocketAddress *addr;
146 GSocketFamily family;
148 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
150 addr = G_INET_SOCKET_ADDRESS (address);
151 family = g_inet_address_get_family (addr->priv->address);
153 if (family == AF_INET)
154 return sizeof (struct sockaddr_in);
155 else if (family == AF_INET6)
156 return sizeof (struct sockaddr_in6);
162 g_inet_socket_address_to_native (GSocketAddress *address,
167 GInetSocketAddress *addr;
168 GSocketFamily family;
170 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
172 addr = G_INET_SOCKET_ADDRESS (address);
173 family = g_inet_address_get_family (addr->priv->address);
175 if (family == AF_INET)
177 struct sockaddr_in *sock = (struct sockaddr_in *) dest;
179 if (destlen < sizeof (*sock))
181 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
182 _("Not enough space for socket address"));
186 sock->sin_family = AF_INET;
187 sock->sin_port = g_htons (addr->priv->port);
188 memcpy (&(sock->sin_addr.s_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin_addr));
189 memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
192 else if (family == AF_INET6)
194 struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
196 if (destlen < sizeof (*sock))
198 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
199 _("Not enough space for socket address"));
203 memset (sock, 0, sizeof (*sock));
204 sock->sin6_family = AF_INET6;
205 sock->sin6_port = g_htons (addr->priv->port);
206 memcpy (&(sock->sin6_addr.s6_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin6_addr));
211 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
212 _("Unsupported socket address"));
218 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
220 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
221 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
223 g_type_class_add_private (klass, sizeof (GInetSocketAddressPrivate));
225 gobject_class->finalize = g_inet_socket_address_finalize;
226 gobject_class->dispose = g_inet_socket_address_dispose;
227 gobject_class->set_property = g_inet_socket_address_set_property;
228 gobject_class->get_property = g_inet_socket_address_get_property;
230 gsocketaddress_class->get_family = g_inet_socket_address_get_family;
231 gsocketaddress_class->to_native = g_inet_socket_address_to_native;
232 gsocketaddress_class->get_native_size = g_inet_socket_address_get_native_size;
234 g_object_class_install_property (gobject_class, PROP_ADDRESS,
235 g_param_spec_object ("address",
239 G_PARAM_CONSTRUCT_ONLY |
241 G_PARAM_STATIC_STRINGS));
243 g_object_class_install_property (gobject_class, PROP_PORT,
244 g_param_spec_uint ("port",
250 G_PARAM_CONSTRUCT_ONLY |
252 G_PARAM_STATIC_STRINGS));
256 g_inet_socket_address_init (GInetSocketAddress *address)
258 address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
259 G_TYPE_INET_SOCKET_ADDRESS,
260 GInetSocketAddressPrivate);
262 address->priv->address = NULL;
263 address->priv->port = 0;
267 * g_inet_socket_address_new:
268 * @address: a #GInetAddress
269 * @port: a port number
271 * Creates a new #GInetSocketAddress for @address and @port.
273 * Returns: a new #GInetSocketAddress
278 g_inet_socket_address_new (GInetAddress *address,
281 return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
288 * g_inet_socket_address_get_address:
289 * @address: a #GInetSocketAddress
291 * Gets @address's #GInetAddress.
293 * Returns: the #GInetAddress for @address, which must be
294 * g_object_ref()'d if it will be stored
299 g_inet_socket_address_get_address (GInetSocketAddress *address)
301 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
303 return address->priv->address;
307 * g_inet_socket_address_get_port:
308 * @address: a #GInetSocketAddress
310 * Gets @address's port.
312 * Returns: the port for @address
317 g_inet_socket_address_get_port (GInetSocketAddress *address)
319 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
321 return address->priv->port;
324 #define __G_INET_SOCKET_ADDRESS_C__
325 #include "gioaliasdef.c"