Add network address and socket types
[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
32 #include "gioalias.h"
33
34 /**
35  * SECTION:ginetsocketaddress
36  * @short_description: Internet socket addresses
37  *
38  * An IPv4 or IPv6 socket address; that is, the combination of a
39  * #GInetAddress and a port number.
40  **/
41
42 /**
43  * GInetSocketAddress:
44  *
45  * An IPv4 or IPv6 socket address, corresponding to a <type>struct
46  * sockaddr_in</type> or <type>struct sockaddr_in6</type>.
47  **/
48 G_DEFINE_TYPE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS);
49
50 enum {
51   PROP_0,
52   PROP_ADDRESS,
53   PROP_PORT
54 };
55
56 struct _GInetSocketAddressPrivate
57 {
58   GInetAddress *address;
59   guint16       port;
60 };
61
62 static void
63 g_inet_socket_address_finalize (GObject *object)
64 {
65   GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
66
67   if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize)
68     (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize) (object);
69 }
70
71 static void
72 g_inet_socket_address_dispose (GObject *object)
73 {
74   GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
75
76   g_object_unref (address->priv->address);
77
78   if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose)
79     (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose) (object);
80 }
81
82 static void
83 g_inet_socket_address_get_property (GObject    *object,
84                                     guint       prop_id,
85                                     GValue     *value,
86                                     GParamSpec *pspec)
87 {
88   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
89
90   switch (prop_id)
91     {
92       case PROP_ADDRESS:
93         g_value_set_object (value, address->priv->address);
94         break;
95
96       case PROP_PORT:
97         g_value_set_uint (value, address->priv->port);
98         break;
99
100       default:
101         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
102     }
103 }
104
105 static void
106 g_inet_socket_address_set_property (GObject      *object,
107                                     guint         prop_id,
108                                     const GValue *value,
109                                     GParamSpec   *pspec)
110 {
111   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
112
113   switch (prop_id)
114     {
115       case PROP_ADDRESS:
116         address->priv->address = g_object_ref (g_value_get_object (value));
117         break;
118
119       case PROP_PORT:
120         address->priv->port = (guint16) g_value_get_uint (value);
121         break;
122
123       default:
124         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125     }
126 }
127
128 static GSocketFamily
129 g_inet_socket_address_get_family (GSocketAddress *address)
130 {
131   GInetSocketAddress *addr;
132
133   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
134
135   addr = G_INET_SOCKET_ADDRESS (address);
136
137   return g_inet_address_get_family (addr->priv->address);
138 }
139
140 static gssize
141 g_inet_socket_address_get_native_size (GSocketAddress *address)
142 {
143   GInetSocketAddress *addr;
144   GSocketFamily family;
145
146   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
147
148   addr = G_INET_SOCKET_ADDRESS (address);
149   family = g_inet_address_get_family (addr->priv->address);
150
151   if (family == AF_INET)
152     return sizeof (struct sockaddr_in);
153   else if (family == AF_INET6)
154     return sizeof (struct sockaddr_in6);
155   else
156     return -1;
157 }
158
159 static gboolean
160 g_inet_socket_address_to_native (GSocketAddress *address,
161                                  gpointer        dest,
162                                  gsize           destlen)
163 {
164   GInetSocketAddress *addr;
165   GSocketFamily family;
166
167   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
168
169   addr = G_INET_SOCKET_ADDRESS (address);
170   family = g_inet_address_get_family (addr->priv->address);
171
172   if (family == AF_INET)
173     {
174       struct sockaddr_in *sock = (struct sockaddr_in *) dest;
175
176       if (destlen < sizeof (*sock))
177         return FALSE;
178
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));
183       return TRUE;
184     }
185   else if (family == AF_INET6)
186     {
187       struct sockaddr_in6 *sock = (struct sockaddr_in6 *) dest;
188
189       if (destlen < sizeof (*sock))
190         return FALSE;
191
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));
196       return TRUE;
197     }
198   else
199     return FALSE;
200 }
201
202 static void
203 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
204 {
205   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
206   GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
207
208   g_type_class_add_private (klass, sizeof (GInetSocketAddressPrivate));
209
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;
214
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;
218
219   g_object_class_install_property (gobject_class, PROP_ADDRESS,
220                                    g_param_spec_object ("address",
221                                                         "address",
222                                                         "address",
223                                                         G_TYPE_INET_ADDRESS,
224                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
225
226   g_object_class_install_property (gobject_class, PROP_PORT,
227                                    g_param_spec_uint ("port",
228                                                       "port",
229                                                       "port",
230                                                       0,
231                                                       65535,
232                                                       0,
233                                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
234 }
235
236 static void
237 g_inet_socket_address_init (GInetSocketAddress *address)
238 {
239   address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
240                                                G_TYPE_INET_SOCKET_ADDRESS,
241                                                GInetSocketAddressPrivate);
242
243   address->priv->address = NULL;
244   address->priv->port = 0;
245 }
246
247 /**
248  * g_inet_socket_address_new:
249  * @address: a #GInetAddress
250  * @port: a port number
251  *
252  * Creates a new #GInetSocketAddress for @address and @port.
253  *
254  * Returns: a new #GInetSocketAddress
255  *
256  * Since: 2.22
257  */
258 GSocketAddress *
259 g_inet_socket_address_new (GInetAddress *address,
260                            guint16       port)
261 {
262   return g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
263                        "address", address,
264                        "port", port,
265                        NULL);
266 }
267
268 /**
269  * g_inet_socket_address_get_address:
270  * @address: a #GInetSocketAddress
271  *
272  * Gets @address's #GInetAddress.
273  *
274  * Returns: the #GInetAddress for @address, which must be
275  * g_object_ref()'d if it will be stored
276  *
277  * Since: 2.22
278  */
279 GInetAddress *
280 g_inet_socket_address_get_address (GInetSocketAddress *address)
281 {
282   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), NULL);
283
284   return address->priv->address;
285 }
286
287 /**
288  * g_inet_socket_address_get_port:
289  * @address: a #GInetSocketAddress
290  *
291  * Gets @address's port.
292  *
293  * Returns: the port for @address
294  *
295  * Since: 2.22
296  */
297 guint16
298 g_inet_socket_address_get_port (GInetSocketAddress *address)
299 {
300   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
301
302   return address->priv->port;
303 }
304
305 #define __G_INET_SOCKET_ADDRESS_C__
306 #include "gioaliasdef.c"