Starting on TcpClient, some formatting fixes
[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,
71                                     guint       prop_id,
72                                     GValue     *value,
73                                     GParamSpec *pspec)
74 {
75   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
76
77   switch (prop_id)
78     {
79       case PROP_ADDRESS:
80         g_value_set_object (value, address->priv->address);
81         break;
82
83       case PROP_PORT:
84         g_value_set_uint (value, address->priv->port);
85         break;
86
87       default:
88         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
89     }
90 }
91
92 static void
93 g_inet_socket_address_set_property (GObject      *object,
94                                     guint         prop_id,
95                                     const GValue *value,
96                                     GParamSpec   *pspec)
97 {
98   GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
99
100   switch (prop_id)
101     {
102       case PROP_ADDRESS:
103         address->priv->address = G_INET_ADDRESS (g_object_ref_sink (g_value_get_object (value)));
104         break;
105
106       case PROP_PORT:
107         address->priv->port = (guint16) g_value_get_uint (value);
108         break;
109
110       default:
111         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112     }
113 }
114
115 static gssize
116 g_inet_socket_address_native_size (GSocketAddress *address)
117 {
118   GInetSocketAddress *addr;
119
120   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
121
122   addr = G_INET_SOCKET_ADDRESS (address);
123
124   if (G_IS_INET4_ADDRESS (addr->priv->address))
125     return sizeof (struct sockaddr_in);
126   else if (G_IS_INET6_ADDRESS (addr->priv->address))
127     return sizeof (struct sockaddr_in6);
128   else
129     return -1;
130 }
131
132 static gboolean
133 g_inet_socket_address_to_native (GSocketAddress *address,
134                                  gpointer        dest)
135 {
136   GInetSocketAddress *addr;
137
138   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
139
140   addr = G_INET_SOCKET_ADDRESS (address);
141
142   if (G_IS_INET4_ADDRESS (addr->priv->address))
143     {
144       struct sockaddr_in *sock = (struct sockaddr_in *) dest;
145
146       sock->sin_family = AF_INET;
147       sock->sin_port = g_htons (addr->priv->port);
148       memcpy (&(sock->sin_addr.s_addr), g_inet4_address_to_bytes (G_INET4_ADDRESS (addr->priv->address)), sizeof (sock->sin_addr));
149       memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
150       return TRUE;
151     }
152   else if (G_IS_INET6_ADDRESS (addr->priv->address))
153     {
154       return FALSE;
155     }
156   else
157     return FALSE;
158 }
159
160 static void
161 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
162 {
163   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
164   GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
165
166   g_type_class_add_private (klass, sizeof (GInetSocketAddressPrivate));
167
168   gobject_class->finalize = g_inet_socket_address_finalize;
169   gobject_class->dispose = g_inet_socket_address_dispose;
170   gobject_class->set_property = g_inet_socket_address_set_property;
171   gobject_class->get_property = g_inet_socket_address_get_property;
172
173   gsocketaddress_class->to_native = g_inet_socket_address_to_native;
174   gsocketaddress_class->native_size = g_inet_socket_address_native_size;
175
176   g_object_class_install_property (gobject_class, PROP_ADDRESS,
177                                    g_param_spec_object ("address",
178                                                         "address",
179                                                         "address",
180                                                         G_TYPE_INET_ADDRESS,
181                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
182
183   g_object_class_install_property (gobject_class, PROP_PORT,
184                                    g_param_spec_uint ("port",
185                                                       "port",
186                                                       "port",
187                                                       0,
188                                                       65535,
189                                                       0,
190                                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
191 }
192
193 static void
194 g_inet_socket_address_init (GInetSocketAddress *address)
195 {
196   address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
197                                                G_TYPE_INET_SOCKET_ADDRESS,
198                                                GInetSocketAddressPrivate);
199
200   address->priv->address = NULL;
201   address->priv->port = 0;
202 }
203
204
205 GInetSocketAddress *
206 g_inet_socket_address_new (GInetAddress *address,
207                            guint16       port)
208 {
209   return G_INET_SOCKET_ADDRESS (g_object_new (G_TYPE_INET_SOCKET_ADDRESS, "address", address, "port", port, NULL));
210 }
211
212 GInetAddress *
213 g_inet_socket_address_get_address (GInetSocketAddress *sockaddr)
214 {
215   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (sockaddr), NULL);
216
217   return sockaddr->priv->address;
218 }
219
220 guint16 
221 g_inet_socket_address_get_port (GInetSocketAddress *sockaddr)
222 {
223   g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (sockaddr), 0);
224
225   return sockaddr->priv->port;
226 }
227