Update the docs for the new network APIs
[platform/upstream/glib.git] / gio / gunixsocketaddress.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 "gunixsocketaddress.h"
29 #include "glibintl.h"
30 #include "gnetworkingprivate.h"
31
32 #include "gioalias.h"
33
34 /**
35  * SECTION:gunixsocketaddress
36  * @short_description: Unix #GSocketAddress
37  *
38  * Support for UNIX-domain (aka local) sockets.
39  **/
40
41 /**
42  * GUnixSocketAddress:
43  *
44  * A UNIX-domain (local) socket address, corresponding to a
45  * <type>struct sockaddr_un</type>.
46  **/
47 G_DEFINE_TYPE (GUnixSocketAddress, g_unix_socket_address, G_TYPE_SOCKET_ADDRESS);
48
49 enum
50 {
51   PROP_0,
52   PROP_PATH,
53 };
54
55 struct _GUnixSocketAddressPrivate
56 {
57   char *path;
58 };
59
60 static void
61 g_unix_socket_address_finalize (GObject *object)
62 {
63   GUnixSocketAddress *address G_GNUC_UNUSED = G_UNIX_SOCKET_ADDRESS (object);
64
65   g_free (address->priv->path);
66
67   if (G_OBJECT_CLASS (g_unix_socket_address_parent_class)->finalize)
68     (G_OBJECT_CLASS (g_unix_socket_address_parent_class)->finalize) (object);
69 }
70
71 static void
72 g_unix_socket_address_dispose (GObject *object)
73 {
74   GUnixSocketAddress *address G_GNUC_UNUSED = G_UNIX_SOCKET_ADDRESS (object);
75
76   if (G_OBJECT_CLASS (g_unix_socket_address_parent_class)->dispose)
77     (*G_OBJECT_CLASS (g_unix_socket_address_parent_class)->dispose) (object);
78 }
79
80 static void
81 g_unix_socket_address_get_property (GObject    *object,
82                                     guint       prop_id,
83                                     GValue     *value,
84                                     GParamSpec *pspec)
85 {
86   GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
87
88   switch (prop_id)
89     {
90       case PROP_PATH:
91         g_value_set_string (value, address->priv->path);
92         break;
93
94       default:
95         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
96     }
97 }
98
99 static GSocketFamily
100 g_unix_socket_address_get_family (GSocketAddress *address)
101 {
102   g_assert (PF_UNIX == G_SOCKET_FAMILY_UNIX);
103
104   return G_SOCKET_FAMILY_UNIX;
105 }
106
107 static void
108 g_unix_socket_address_set_property (GObject      *object,
109                                     guint         prop_id,
110                                     const GValue *value,
111                                     GParamSpec   *pspec)
112 {
113   GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
114
115   switch (prop_id)
116     {
117       case PROP_PATH:
118         g_free (address->priv->path);
119         address->priv->path = g_value_dup_string (value);
120         break;
121
122       default:
123         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124     }
125 }
126
127 static gssize
128 g_unix_socket_address_get_native_size (GSocketAddress *address)
129 {
130   return sizeof (struct sockaddr_un);
131 }
132
133 static gboolean
134 g_unix_socket_address_to_native (GSocketAddress *address,
135                                  gpointer        dest,
136                                  gsize           destlen)
137 {
138   GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
139   struct sockaddr_un *sock;
140
141   if (destlen < sizeof (*sock))
142     return FALSE;
143
144   sock = (struct sockaddr_un *) dest;
145   sock->sun_family = AF_UNIX;
146   g_strlcpy (sock->sun_path, addr->priv->path, sizeof (sock->sun_path));
147
148   return TRUE;
149 }
150
151 static void
152 g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
153 {
154   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
155   GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
156
157   g_type_class_add_private (klass, sizeof (GUnixSocketAddressPrivate));
158
159   gobject_class->finalize = g_unix_socket_address_finalize;
160   gobject_class->dispose = g_unix_socket_address_dispose;
161   gobject_class->set_property = g_unix_socket_address_set_property;
162   gobject_class->get_property = g_unix_socket_address_get_property;
163
164   gsocketaddress_class->get_family = g_unix_socket_address_get_family;
165   gsocketaddress_class->to_native = g_unix_socket_address_to_native;
166   gsocketaddress_class->get_native_size = g_unix_socket_address_get_native_size;
167
168   g_object_class_install_property (gobject_class,
169                                    PROP_PATH,
170                                    g_param_spec_string ("path",
171                                                         P_("Path"),
172                                                         P_("UNIX socket path"),
173                                                         NULL,
174                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
175 }
176
177 static void
178 g_unix_socket_address_init (GUnixSocketAddress *address)
179 {
180   address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
181                                                G_TYPE_UNIX_SOCKET_ADDRESS,
182                                                GUnixSocketAddressPrivate);
183
184   address->priv->path = NULL;
185 }
186
187 /**
188  * g_unix_socket_address_new:
189  * @path: the socket path
190  *
191  * Creates a new #GUnixSocketAddress for @path.
192  *
193  * Returns: a new #GUnixSocketAddress
194  *
195  * Since: 2.22
196  */
197 GSocketAddress *
198 g_unix_socket_address_new (const gchar *path)
199 {
200   return g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
201                        "path", path,
202                        NULL);
203 }
204
205 #define __G_UNIX_SOCKET_ADDRESS_C__
206 #include "gioaliasdef.c"