659857277af45dff21416fad772443151e1837e4
[cascardo/gnio.git] / gnio / ginet6address.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 <string.h>
26 #include <glib.h>
27
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30
31 #include "ginet6address.h"
32
33 G_DEFINE_TYPE (GInet6Address, g_inet6_address, G_TYPE_INET_ADDRESS);
34
35 struct _GInet6AddressPrivate
36 {
37   union {
38     guint8  u6_addr8[16];
39     guint16 u6_addr16[8];
40     guint32 u6_addr32[4];
41   } addr;
42 };
43
44 static gchar *
45 g_inet6_address_to_string (GInetAddress *address)
46 {
47   gchar *addr = g_malloc (48);
48
49   g_return_val_if_fail (G_IS_INET6_ADDRESS (address), NULL);
50
51   inet_ntop (AF_INET6, G_INET6_ADDRESS (address)->priv->addr.u6_addr8, addr, 48);
52
53   return addr;
54 }
55
56 static void
57 g_inet6_address_finalize (GObject *object)
58 {
59   GInet6Address *address G_GNUC_UNUSED = G_INET6_ADDRESS (object);
60
61   if (G_OBJECT_CLASS (g_inet6_address_parent_class)->finalize)
62     (*G_OBJECT_CLASS (g_inet6_address_parent_class)->finalize) (object);
63 }
64
65 static void
66 g_inet6_address_dispose (GObject *object)
67 {
68   GInet6Address *address G_GNUC_UNUSED = G_INET6_ADDRESS (object);
69
70   if (G_OBJECT_CLASS (g_inet6_address_parent_class)->dispose)
71     (*G_OBJECT_CLASS (g_inet6_address_parent_class)->dispose) (object);
72 }
73
74 static void
75 g_inet6_address_class_init (GInet6AddressClass *klass)
76 {
77   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
78   GInetAddressClass *ginetaddress_class = G_INET_ADDRESS_CLASS (klass);
79
80   g_type_class_add_private (klass, sizeof (GInet6AddressPrivate));
81
82   gobject_class->finalize = g_inet6_address_finalize;
83   gobject_class->dispose = g_inet6_address_dispose;
84   ginetaddress_class->to_string = g_inet6_address_to_string;
85 /*  ginetaddress_class->is_any = g_inet6_address_is_any;
86   ginetaddress_class->is_linklocal = g_inet6_address_is_linklocal;
87   ginetaddress_class->is_loopback = g_inet6_address_is_loopback;
88   ginetaddress_class->is_sitelocal = g_inet6_address_is_sitelocal;
89   ginetaddress_class->is_multicast = g_inet6_address_is_multicast;
90   ginetaddress_class->is_mc_global = g_inet6_address_is_mc_global;
91   ginetaddress_class->is_mc_linklocal = g_inet6_address_is_mc_linklocal;
92   ginetaddress_class->is_mc_nodelocal = g_inet6_address_is_mc_nodelocal;
93   ginetaddress_class->is_mc_orglocal = g_inet6_address_is_mc_orglocal;
94   ginetaddress_class->is_mc_sitelocal = g_inet6_address_is_mc_sitelocal;*/
95 }
96
97 static void
98 g_inet6_address_init (GInet6Address *address)
99 {
100   address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
101                                                G_TYPE_INET6_ADDRESS,
102                                                GInet6AddressPrivate);
103 }
104
105 /* ************************************************************************* */
106 /* Public Functions */
107
108 GInet6Address *
109 g_inet6_address_from_string (const char *string)
110 {
111   return NULL;
112 }
113
114 GInet6Address *
115 g_inet6_address_from_bytes (guint8 bytes[])
116 {
117   GInet6Address *address = G_INET6_ADDRESS (g_object_new (G_TYPE_INET6_ADDRESS, NULL));
118
119   g_memmove (address->priv->addr.u6_addr8, bytes, 16);
120
121   return address;
122 }
123
124 const guint8 *
125 g_inet6_address_to_bytes (GInet6Address *address)
126 {
127   return NULL;
128 }
129
130 GInet6Address *
131 g_inet6_address_new_loopback (void)
132 {
133   guint8 bytes[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
134
135   return g_inet6_address_from_bytes (bytes);
136 }
137
138 GInet6Address *
139 g_inet6_address_new_any (void)
140 {
141   return NULL; 
142 }