Some more stuff for GSockets
[cascardo/gnio.git] / gnio / ginetsocketaddress.c
1 /* GNIO - GLib Network Layer of GIO
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 #include <sys/socket.h>
28 #include <netinet/in.h>
29
30 #include "ginetsocketaddress.h"
31 #include "ginetaddress.h"
32 #include "ginet4address.h"
33 #include "ginet6address.h"
34
35 G_DEFINE_TYPE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS);
36
37 enum {
38   PROP_0,
39   PROP_ADDRESS,
40   PROP_PORT
41 };
42
43 struct _GInetSocketAddressPrivate
44 {
45   GInetAddress *address;
46   guint16       port;
47 };
48
49 static void
50 g_inet_socket_address_finalize (GObject *object)
51 {
52   GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
53
54   if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize)
55     (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize) (object);
56 }
57
58 static void
59 g_inet_socket_address_dispose (GObject *object)
60 {
61   GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
62
63   g_object_unref (address->priv->address);
64
65   if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose)
66     (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose) (object);
67 }
68
69 static void
70 g_inet_socket_address_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
71 {
72   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
73
74   switch (prop_id)
75     {
76       case PROP_ADDRESS:
77         g_value_set_object (value, address->priv->address);
78         break;
79
80       case PROP_PORT:
81         g_value_set_uint (value, address->priv->port);
82         break;
83
84       default:
85         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
86     }
87 }
88
89 static void
90 g_inet_socket_address_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
91 {
92   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
93
94   switch (prop_id)
95     {
96       case PROP_ADDRESS:
97         address->priv->address = G_INET_ADDRESS (g_object_ref_sink (g_value_get_object (value)));
98         break;
99
100       case PROP_PORT:
101         address->priv->port = (guint16) g_value_get_uint (value);
102         break;
103
104       default:
105         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106     }
107 }
108
109 static gssize
110 g_inet_socket_address_native_size (GSocketAddress *address)
111 {
112   GInetSocketAddress *addr;
113
114   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
115
116   addr = G_INET_SOCKET_ADDRESS (address);
117
118   if (G_IS_INET4_ADDRESS (addr->priv->address))
119     return sizeof (struct sockaddr_in);
120   else if (G_IS_INET6_ADDRESS (addr->priv->address))
121     return sizeof (struct sockaddr_in6);
122   else
123     return -1;
124 }
125
126 static gboolean
127 g_inet_socket_address_to_native (GSocketAddress *address, gpointer dest)
128 {
129   GInetSocketAddress *addr;
130
131   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
132
133   addr = G_INET_SOCKET_ADDRESS (address);
134
135   if (G_IS_INET4_ADDRESS (addr->priv->address))
136     {
137       struct sockaddr_in *sock = (struct sockaddr_in *) dest;
138
139       sock->sin_family = AF_INET;
140       sock->sin_port = g_htons (addr->priv->port);
141       memcpy (&(sock->sin_addr.s_addr), g_inet4_address_to_bytes (G_INET4_ADDRESS (addr->priv->address)), sizeof (sock->sin_addr));
142       memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
143       return TRUE;
144     }
145   else if (G_IS_INET6_ADDRESS (addr->priv->address))
146     {
147       return FALSE;
148     }
149   else
150     return FALSE;
151 }
152
153 static void
154 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
155 {
156   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
157   GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
158
159   g_type_class_add_private (klass, sizeof (GInetSocketAddressPrivate));
160
161   gobject_class->finalize = g_inet_socket_address_finalize;
162   gobject_class->dispose = g_inet_socket_address_dispose;
163   gobject_class->set_property = g_inet_socket_address_set_property;
164   gobject_class->get_property = g_inet_socket_address_get_property;
165
166   gsocketaddress_class->to_native = g_inet_socket_address_to_native;
167   gsocketaddress_class->native_size = g_inet_socket_address_native_size;
168
169   g_object_class_install_property (gobject_class, PROP_ADDRESS,
170                                    g_param_spec_object ("address",
171                                                         "address",
172                                                         "address",
173                                                         G_TYPE_INET_ADDRESS,
174                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
175
176   g_object_class_install_property (gobject_class, PROP_PORT,
177                                    g_param_spec_uint ("port",
178                                                       "port",
179                                                       "port",
180                                                       0,
181                                                       65535,
182                                                       0,
183                                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
184 }
185
186 static void
187 g_inet_socket_address_init (GInetSocketAddress *address)
188 {
189   address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
190                                                G_TYPE_INET_SOCKET_ADDRESS,
191                                                GInetSocketAddressPrivate);
192
193   address->priv->address = NULL;
194   address->priv->port = 0;
195 }
196
197
198 GInetSocketAddress *
199 g_inet_socket_address_new (GInetAddress *address, guint16 port)
200 {
201   return G_INET_SOCKET_ADDRESS (g_object_new (G_TYPE_INET_SOCKET_ADDRESS, "address", address, "port", port, NULL));
202 }
203
204 GInetAddress *
205 g_inet_socket_address_get_address (GInetSocketAddress *sockaddr)
206 {
207   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (sockaddr), NULL);
208
209   return sockaddr->priv->address;
210 }
211
212 guint16 
213 g_inet_socket_address_get_port (GInetSocketAddress *sockaddr)
214 {
215   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (sockaddr), 0);
216
217   return sockaddr->priv->port;
218 }
219