Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / ginetsocketaddress.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Authors: Christian Kellner <gicmo@gnome.org>
21  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
22  */
23
24 #include <config.h>
25 #include <glib.h>
26 #include <string.h>
27
28 #include "ginetsocketaddress.h"
29 #include "ginetaddress.h"
30 #include "gnetworkingprivate.h"
31 #include "gioerror.h"
32 #include "glibintl.h"
33
34 #include "gioalias.h"
35
36 /**
37  * SECTION:ginetsocketaddress
38  * @short_description: Internet GSocketAddress
39  *
40  * An IPv4 or IPv6 socket address; that is, the combination of a
41  * #GInetAddress and a port number.
42  */
43
44 /**
45  * GInetSocketAddress:
46  *
47  * An IPv4 or IPv6 socket address, corresponding to a <type>struct
48  * sockaddr_in</type> or <type>struct sockaddr_in6</type>.
49  */
50 G_DEFINE_TYPE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS);
51
52 enum {
53   PROP_0,
54   PROP_ADDRESS,
55   PROP_PORT
56 };
57
58 struct _GInetSocketAddressPrivate
59 {
60   GInetAddress *address;
61   guint16       port;
62 };
63
64 static void
65 g_inet_socket_address_finalize (GObject *object)
66 {
67   GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
68
69   if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize)
70     (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize) (object);
71 }
72
73 static void
74 g_inet_socket_address_dispose (GObject *object)
75 {
76   GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
77
78   g_object_unref (address->priv->address);
79
80   if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose)
81     (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose) (object);
82 }
83
84 static void
85 g_inet_socket_address_get_property (GObject    *object,
86                                     guint       prop_id,
87                                     GValue     *value,
88                                     GParamSpec *pspec)
89 {
90   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
91
92   switch (prop_id)
93     {
94       case PROP_ADDRESS:
95         g_value_set_object (value, address->priv->address);
96         break;
97
98       case PROP_PORT:
99         g_value_set_uint (value, address->priv->port);
100         break;
101
102       default:
103         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
104     }
105 }
106
107 static void
108 g_inet_socket_address_set_property (GObject      *object,
109                                     guint         prop_id,
110                                     const GValue *value,
111                                     GParamSpec   *pspec)
112 {
113   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
114
115   switch (prop_id)
116     {
117       case PROP_ADDRESS:
118         address->priv->address = g_object_ref (g_value_get_object (value));
119         break;
120
121       case PROP_PORT:
122         address->priv->port = (guint16) g_value_get_uint (value);
123         break;
124
125       default:
126         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127     }
128 }
129
130 static GSocketFamily
131 g_inet_socket_address_get_family (GSocketAddress *address)
132 {
133   GInetSocketAddress *addr;
134
135   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
136
137   addr = G_INET_SOCKET_ADDRESS (address);
138
139   return g_inet_address_get_family (addr->priv->address);
140 }
141
142 static gssize
143 g_inet_socket_address_get_native_size (GSocketAddress *address)
144 {
145   GInetSocketAddress *addr;
146   GSocketFamily family;
147
148   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
149
150   addr = G_INET_SOCKET_ADDRESS (address);
151   family = g_inet_address_get_family (addr->priv->address);
152
153   if (family == AF_INET)
154     return sizeof (struct sockaddr_in);
155   else if (family == AF_INET6)
156     return sizeof (struct sockaddr_in6);
157   else
158     return -1;
159 }
160
161 static gboolean
162 g_inet_socket_address_to_native (GSocketAddress  *address,
163                                  gpointer         dest,
164                                  gsize            destlen,
165                                  GError         **error)
166 {
167   GInetSocketAddress *addr;
168   GSocketFamily family;
169
170   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
171
172   addr = G_INET_SOCKET_ADDRESS (address);
173   family = g_inet_address_get_family (addr->priv->address);
174
175   if (family == AF_INET)
176     {
177       struct sockaddr_in *sock = (struct sockaddr_in *) dest;
178
179       if (destlen < sizeof (*sock))
180         {
181           g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
182                                _("Not enough space for socket address"));
183           return FALSE;
184         }
185
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));
190       return TRUE;
191     }
192   else if (family == AF_INET6)
193     {
194       struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
195
196       if (destlen < sizeof (*sock))
197         {
198           g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
199                                _("Not enough space for socket address"));
200           return FALSE;
201         }
202
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));
207       return TRUE;
208     }
209   else
210     {
211       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
212                            _("Unsupported socket address"));
213       return FALSE;
214     }
215 }
216
217 static void
218 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
219 {
220   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
221   GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
222
223   g_type_class_add_private (klass, sizeof (GInetSocketAddressPrivate));
224
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;
229
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;
233
234   g_object_class_install_property (gobject_class, PROP_ADDRESS,
235                                    g_param_spec_object ("address",
236                                                         P_("Address"),
237                                                         P_("The address"),
238                                                         G_TYPE_INET_ADDRESS,
239                                                         G_PARAM_CONSTRUCT_ONLY |
240                                                         G_PARAM_READWRITE |
241                                                         G_PARAM_STATIC_STRINGS));
242
243   g_object_class_install_property (gobject_class, PROP_PORT,
244                                    g_param_spec_uint ("port",
245                                                       P_("Port"),
246                                                       P_("The port"),
247                                                       0,
248                                                       65535,
249                                                       0,
250                                                       G_PARAM_CONSTRUCT_ONLY |
251                                                       G_PARAM_READWRITE |
252                                                       G_PARAM_STATIC_STRINGS));
253 }
254
255 static void
256 g_inet_socket_address_init (GInetSocketAddress *address)
257 {
258   address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
259                                                G_TYPE_INET_SOCKET_ADDRESS,
260                                                GInetSocketAddressPrivate);
261
262   address->priv->address = NULL;
263   address->priv->port = 0;
264 }
265
266 /**
267  * g_inet_socket_address_new:
268  * @address: a #GInetAddress
269  * @port: a port number
270  *
271  * Creates a new #GInetSocketAddress for @address and @port.
272  *
273  * Returns: a new #GInetSocketAddress
274  *
275  * Since: 2.22
276  */
277 GSocketAddress *
278 g_inet_socket_address_new (GInetAddress *address,
279                            guint16       port)
280 {
281   return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
282                        "address", address,
283                        "port", port,
284                        NULL);
285 }
286
287 /**
288  * g_inet_socket_address_get_address:
289  * @address: a #GInetSocketAddress
290  *
291  * Gets @address's #GInetAddress.
292  *
293  * Returns: the #GInetAddress for @address, which must be
294  * g_object_ref()'d if it will be stored
295  *
296  * Since: 2.22
297  */
298 GInetAddress *
299 g_inet_socket_address_get_address (GInetSocketAddress *address)
300 {
301   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
302
303   return address->priv->address;
304 }
305
306 /**
307  * g_inet_socket_address_get_port:
308  * @address: a #GInetSocketAddress
309  *
310  * Gets @address's port.
311  *
312  * Returns: the port for @address
313  *
314  * Since: 2.22
315  */
316 guint16
317 g_inet_socket_address_get_port (GInetSocketAddress *address)
318 {
319   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
320
321   return address->priv->port;
322 }
323
324 #define __G_INET_SOCKET_ADDRESS_C__
325 #include "gioaliasdef.c"