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>.
49 G_DEFINE_TYPE (GInetSocketAddress
, g_inet_socket_address
, G_TYPE_SOCKET_ADDRESS
);
57 struct _GInetSocketAddressPrivate
59 GInetAddress
*address
;
64 g_inet_socket_address_finalize (GObject
*object
)
66 GInetSocketAddress
*address G_GNUC_UNUSED
= G_INET_SOCKET_ADDRESS (object
);
68 if (G_OBJECT_CLASS (g_inet_socket_address_parent_class
)->finalize
)
69 (*G_OBJECT_CLASS (g_inet_socket_address_parent_class
)->finalize
) (object
);
73 g_inet_socket_address_dispose (GObject
*object
)
75 GInetSocketAddress
*address G_GNUC_UNUSED
= G_INET_SOCKET_ADDRESS (object
);
77 g_object_unref (address
->priv
->address
);
79 if (G_OBJECT_CLASS (g_inet_socket_address_parent_class
)->dispose
)
80 (*G_OBJECT_CLASS (g_inet_socket_address_parent_class
)->dispose
) (object
);
84 g_inet_socket_address_get_property (GObject
*object
,
89 GInetSocketAddress
*address
= G_INET_SOCKET_ADDRESS (object
);
94 g_value_set_object (value
, address
->priv
->address
);
98 g_value_set_uint (value
, address
->priv
->port
);
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
107 g_inet_socket_address_set_property (GObject
*object
,
112 GInetSocketAddress
*address
= G_INET_SOCKET_ADDRESS (object
);
117 address
->priv
->address
= g_object_ref (g_value_get_object (value
));
121 address
->priv
->port
= (guint16
) g_value_get_uint (value
);
125 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
130 g_inet_socket_address_get_family (GSocketAddress
*address
)
132 GInetSocketAddress
*addr
;
134 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address
), 0);
136 addr
= G_INET_SOCKET_ADDRESS (address
);
138 return g_inet_address_get_family (addr
->priv
->address
);
142 g_inet_socket_address_get_native_size (GSocketAddress
*address
)
144 GInetSocketAddress
*addr
;
145 GSocketFamily family
;
147 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address
), 0);
149 addr
= G_INET_SOCKET_ADDRESS (address
);
150 family
= g_inet_address_get_family (addr
->priv
->address
);
152 if (family
== AF_INET
)
153 return sizeof (struct sockaddr_in
);
154 else if (family
== AF_INET6
)
155 return sizeof (struct sockaddr_in6
);
161 g_inet_socket_address_to_native (GSocketAddress
*address
,
166 GInetSocketAddress
*addr
;
167 GSocketFamily family
;
169 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address
), FALSE
);
171 addr
= G_INET_SOCKET_ADDRESS (address
);
172 family
= g_inet_address_get_family (addr
->priv
->address
);
174 if (family
== AF_INET
)
176 struct sockaddr_in
*sock
= (struct sockaddr_in
*) dest
;
178 if (destlen
< sizeof (*sock
))
180 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NO_SPACE
,
181 _("Not enough space for socket address"));
185 sock
->sin_family
= AF_INET
;
186 sock
->sin_port
= g_htons (addr
->priv
->port
);
187 memcpy (&(sock
->sin_addr
.s_addr
), g_inet_address_to_bytes (addr
->priv
->address
), sizeof (sock
->sin_addr
));
188 memset (sock
->sin_zero
, 0, sizeof (sock
->sin_zero
));
191 else if (family
== AF_INET6
)
193 struct sockaddr_in6
*sock
= (struct sockaddr_in6
*) 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 memset (sock
, 0, sizeof (*sock
));
203 sock
->sin6_family
= AF_INET6
;
204 sock
->sin6_port
= g_htons (addr
->priv
->port
);
205 memcpy (&(sock
->sin6_addr
.s6_addr
), g_inet_address_to_bytes (addr
->priv
->address
), sizeof (sock
->sin6_addr
));
210 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
211 _("Unsupported socket address"));
217 g_inet_socket_address_class_init (GInetSocketAddressClass
*klass
)
219 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
220 GSocketAddressClass
*gsocketaddress_class
= G_SOCKET_ADDRESS_CLASS (klass
);
222 g_type_class_add_private (klass
, sizeof (GInetSocketAddressPrivate
));
224 gobject_class
->finalize
= g_inet_socket_address_finalize
;
225 gobject_class
->dispose
= g_inet_socket_address_dispose
;
226 gobject_class
->set_property
= g_inet_socket_address_set_property
;
227 gobject_class
->get_property
= g_inet_socket_address_get_property
;
229 gsocketaddress_class
->get_family
= g_inet_socket_address_get_family
;
230 gsocketaddress_class
->to_native
= g_inet_socket_address_to_native
;
231 gsocketaddress_class
->get_native_size
= g_inet_socket_address_get_native_size
;
233 g_object_class_install_property (gobject_class
, PROP_ADDRESS
,
234 g_param_spec_object ("address",
238 G_PARAM_CONSTRUCT_ONLY
|
240 G_PARAM_STATIC_STRINGS
));
242 g_object_class_install_property (gobject_class
, PROP_PORT
,
243 g_param_spec_uint ("port",
249 G_PARAM_CONSTRUCT_ONLY
|
251 G_PARAM_STATIC_STRINGS
));
255 g_inet_socket_address_init (GInetSocketAddress
*address
)
257 address
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (address
,
258 G_TYPE_INET_SOCKET_ADDRESS
,
259 GInetSocketAddressPrivate
);
261 address
->priv
->address
= NULL
;
262 address
->priv
->port
= 0;
266 * g_inet_socket_address_new:
267 * @address: a #GInetAddress
268 * @port: a port number
270 * Creates a new #GInetSocketAddress for @address and @port.
272 * Returns: a new #GInetSocketAddress
277 g_inet_socket_address_new (GInetAddress
*address
,
280 return g_object_new (G_TYPE_INET_SOCKET_ADDRESS
,
287 * g_inet_socket_address_get_address:
288 * @address: a #GInetSocketAddress
290 * Gets @address's #GInetAddress.
292 * Returns: (transfer none): the #GInetAddress for @address, which must be
293 * g_object_ref()'d if it will be stored
298 g_inet_socket_address_get_address (GInetSocketAddress
*address
)
300 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address
), NULL
);
302 return address
->priv
->address
;
306 * g_inet_socket_address_get_port:
307 * @address: a #GInetSocketAddress
309 * Gets @address's port.
311 * Returns: the port for @address
316 g_inet_socket_address_get_port (GInetSocketAddress
*address
)
318 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address
), 0);
320 return address
->priv
->port
;