Some more stuff for GSockets
[cascardo/gnio.git] / gnio / gsocketaddress.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
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29
30 #include "gsocketaddress.h"
31 #include "ginetsocketaddress.h"
32 #include "ginet4address.h"
33
34 G_DEFINE_ABSTRACT_TYPE (GSocketAddress, g_socket_address, G_TYPE_INITIALLY_UNOWNED);
35
36 static void
37 g_socket_address_class_init (GSocketAddressClass *klass)
38 {
39
40 }
41
42 static void
43 g_socket_address_init (GSocketAddress *address)
44 {
45
46 }
47
48 gssize
49 g_socket_address_native_size (GSocketAddress *address)
50 {
51   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), -1);
52
53   return G_SOCKET_ADDRESS_GET_CLASS (address)->native_size (address);
54 }
55
56 gboolean
57 g_socket_address_to_native (GSocketAddress *address, gpointer dest)
58 {
59   g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
60
61   return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest);
62 }
63
64 GSocketAddress *
65 g_socket_address_from_native (gpointer native, gsize len)
66 {
67   gshort family;
68
69   if (len < sizeof (gshort))
70     return NULL;
71
72   family = ((struct sockaddr *) native)->sa_family;
73
74   if (family == AF_UNSPEC)
75     return NULL;
76
77   if (family == AF_INET)
78     {
79       struct sockaddr_in *addr = (struct sockaddr_in *) native;
80
81       return G_SOCKET_ADDRESS (g_inet_socket_address_new (G_INET_ADDRESS (g_inet4_address_from_bytes ((guint8 *) &(addr->sin_addr))), g_ntohs (addr->sin_port)));
82     }
83   // TODO: handle AF_INET6 and AF_UNIX
84
85   return NULL;
86 }