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"
35 * SECTION:ginetsocketaddress
36 * @short_description: Internet socket addresses
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 <type>struct
46 * sockaddr_in</type> or <type>struct sockaddr_in6</type>.
48 G_DEFINE_TYPE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS);
56 struct _GInetSocketAddressPrivate
58 GInetAddress *address;
63 g_inet_socket_address_finalize (GObject *object)
65 GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
67 if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize)
68 (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize) (object);
72 g_inet_socket_address_dispose (GObject *object)
74 GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
76 g_object_unref (address->priv->address);
78 if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose)
79 (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose) (object);
83 g_inet_socket_address_get_property (GObject *object,
88 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
93 g_value_set_object (value, address->priv->address);
97 g_value_set_uint (value, address->priv->port);
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106 g_inet_socket_address_set_property (GObject *object,
111 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
116 address->priv->address = g_object_ref (g_value_get_object (value));
120 address->priv->port = (guint16) g_value_get_uint (value);
124 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129 g_inet_socket_address_get_family (GSocketAddress *address)
131 GInetSocketAddress *addr;
133 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
135 addr = G_INET_SOCKET_ADDRESS (address);
137 return g_inet_address_get_family (addr->priv->address);
141 g_inet_socket_address_get_native_size (GSocketAddress *address)
143 GInetSocketAddress *addr;
144 GSocketFamily family;
146 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
148 addr = G_INET_SOCKET_ADDRESS (address);
149 family = g_inet_address_get_family (addr->priv->address);
151 if (family == AF_INET)
152 return sizeof (struct sockaddr_in);
153 else if (family == AF_INET6)
154 return sizeof (struct sockaddr_in6);
160 g_inet_socket_address_to_native (GSocketAddress *address,
164 GInetSocketAddress *addr;
165 GSocketFamily family;
167 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
169 addr = G_INET_SOCKET_ADDRESS (address);
170 family = g_inet_address_get_family (addr->priv->address);
172 if (family == AF_INET)
174 struct sockaddr_in *sock = (struct sockaddr_in *) dest;
176 if (destlen < sizeof (*sock))
179 sock->sin_family = AF_INET;
180 sock->sin_port = g_htons (addr->priv->port);
181 memcpy (&(sock->sin_addr.s_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin_addr));
182 memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
185 else if (family == AF_INET6)
187 struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
189 if (destlen < sizeof (*sock))
192 memset (sock, 0, sizeof (sock));
193 sock->sin6_family = AF_INET6;
194 sock->sin6_port = g_htons (addr->priv->port);
195 memcpy (&(sock->sin6_addr.s6_addr), g_inet_address_to_bytes (addr->priv->address), sizeof (sock->sin6_addr));
203 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
205 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
206 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
208 g_type_class_add_private (klass, sizeof (GInetSocketAddressPrivate));
210 gobject_class->finalize = g_inet_socket_address_finalize;
211 gobject_class->dispose = g_inet_socket_address_dispose;
212 gobject_class->set_property = g_inet_socket_address_set_property;
213 gobject_class->get_property = g_inet_socket_address_get_property;
215 gsocketaddress_class->get_family = g_inet_socket_address_get_family;
216 gsocketaddress_class->to_native = g_inet_socket_address_to_native;
217 gsocketaddress_class->get_native_size = g_inet_socket_address_get_native_size;
219 g_object_class_install_property (gobject_class, PROP_ADDRESS,
220 g_param_spec_object ("address",
224 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
226 g_object_class_install_property (gobject_class, PROP_PORT,
227 g_param_spec_uint ("port",
233 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
237 g_inet_socket_address_init (GInetSocketAddress *address)
239 address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
240 G_TYPE_INET_SOCKET_ADDRESS,
241 GInetSocketAddressPrivate);
243 address->priv->address = NULL;
244 address->priv->port = 0;
248 * g_inet_socket_address_new:
249 * @address: a #GInetAddress
250 * @port: a port number
252 * Creates a new #GInetSocketAddress for @address and @port.
254 * Returns: a new #GInetSocketAddress
259 g_inet_socket_address_new (GInetAddress *address,
262 return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
269 * g_inet_socket_address_get_address:
270 * @address: a #GInetSocketAddress
272 * Gets @address's #GInetAddress.
274 * Returns: the #GInetAddress for @address, which must be
275 * g_object_ref()'d if it will be stored
280 g_inet_socket_address_get_address (GInetSocketAddress *address)
282 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
284 return address->priv->address;
288 * g_inet_socket_address_get_port:
289 * @address: a #GInetSocketAddress
291 * Gets @address's port.
293 * Returns: the port for @address
298 g_inet_socket_address_get_port (GInetSocketAddress *address)
300 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
302 return address->priv->port;
305 #define __G_INET_SOCKET_ADDRESS_C__
306 #include "gioaliasdef.c"