nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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, see <http://www.gnu.org/licenses/>. |
||
17 | * |
||
18 | * Authors: Christian Kellner <gicmo@gnome.org> |
||
19 | * Samuel Cormier-Iijima <sciyoshi@gmail.com> |
||
20 | */ |
||
21 | |||
22 | #include <config.h> |
||
23 | #include <glib.h> |
||
24 | #include <string.h> |
||
25 | |||
26 | #include "gnativesocketaddress.h" |
||
27 | #include "gnetworkingprivate.h" |
||
28 | #include "gioerror.h" |
||
29 | #include "glibintl.h" |
||
30 | |||
31 | |||
32 | /** |
||
33 | * SECTION:gnativesocketaddress |
||
34 | * @short_description: Native GSocketAddress |
||
35 | * @include: gio/gio.h |
||
36 | * |
||
37 | * An socket address of some unknown native type. |
||
38 | */ |
||
39 | |||
40 | /** |
||
41 | * GNativeSocketAddress: |
||
42 | * |
||
43 | * An socket address, corresponding to a general struct |
||
44 | * sockadd address of a type not otherwise handled by glib. |
||
45 | */ |
||
46 | |||
47 | struct _GNativeSocketAddressPrivate |
||
48 | { |
||
49 | struct sockaddr *sockaddr; |
||
50 | struct sockaddr_storage storage; |
||
51 | gsize sockaddr_len; |
||
52 | }; |
||
53 | |||
54 | G_DEFINE_TYPE_WITH_PRIVATE (GNativeSocketAddress, g_native_socket_address, G_TYPE_SOCKET_ADDRESS) |
||
55 | |||
56 | static void |
||
57 | g_native_socket_address_dispose (GObject *object) |
||
58 | { |
||
59 | GNativeSocketAddress *address = G_NATIVE_SOCKET_ADDRESS (object); |
||
60 | |||
61 | if (address->priv->sockaddr != (struct sockaddr *)&address->priv->storage) |
||
62 | g_free (address->priv->sockaddr); |
||
63 | |||
64 | G_OBJECT_CLASS (g_native_socket_address_parent_class)->dispose (object); |
||
65 | } |
||
66 | |||
67 | static GSocketFamily |
||
68 | g_native_socket_address_get_family (GSocketAddress *address) |
||
69 | { |
||
70 | GNativeSocketAddress *addr; |
||
71 | |||
72 | g_return_val_if_fail (G_IS_NATIVE_SOCKET_ADDRESS (address), 0); |
||
73 | |||
74 | addr = G_NATIVE_SOCKET_ADDRESS (address); |
||
75 | |||
76 | return addr->priv->sockaddr->sa_family; |
||
77 | } |
||
78 | |||
79 | static gssize |
||
80 | g_native_socket_address_get_native_size (GSocketAddress *address) |
||
81 | { |
||
82 | GNativeSocketAddress *addr; |
||
83 | |||
84 | g_return_val_if_fail (G_IS_NATIVE_SOCKET_ADDRESS (address), 0); |
||
85 | |||
86 | addr = G_NATIVE_SOCKET_ADDRESS (address); |
||
87 | |||
88 | return addr->priv->sockaddr_len; |
||
89 | } |
||
90 | |||
91 | static gboolean |
||
92 | g_native_socket_address_to_native (GSocketAddress *address, |
||
93 | gpointer dest, |
||
94 | gsize destlen, |
||
95 | GError **error) |
||
96 | { |
||
97 | GNativeSocketAddress *addr; |
||
98 | |||
99 | g_return_val_if_fail (G_IS_NATIVE_SOCKET_ADDRESS (address), FALSE); |
||
100 | |||
101 | addr = G_NATIVE_SOCKET_ADDRESS (address); |
||
102 | |||
103 | if (destlen < addr->priv->sockaddr_len) |
||
104 | { |
||
105 | g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE, |
||
106 | _("Not enough space for socket address")); |
||
107 | return FALSE; |
||
108 | } |
||
109 | |||
110 | memcpy (dest, addr->priv->sockaddr, addr->priv->sockaddr_len); |
||
111 | return TRUE; |
||
112 | } |
||
113 | |||
114 | static void |
||
115 | g_native_socket_address_class_init (GNativeSocketAddressClass *klass) |
||
116 | { |
||
117 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
||
118 | GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass); |
||
119 | |||
120 | gobject_class->dispose = g_native_socket_address_dispose; |
||
121 | |||
122 | gsocketaddress_class->get_family = g_native_socket_address_get_family; |
||
123 | gsocketaddress_class->to_native = g_native_socket_address_to_native; |
||
124 | gsocketaddress_class->get_native_size = g_native_socket_address_get_native_size; |
||
125 | } |
||
126 | |||
127 | static void |
||
128 | g_native_socket_address_init (GNativeSocketAddress *address) |
||
129 | { |
||
130 | address->priv = g_native_socket_address_get_instance_private (address); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * g_native_socket_address_new: |
||
135 | * @native: a native address object |
||
136 | * @len: the length of @native, in bytes |
||
137 | * |
||
138 | * Creates a new #GNativeSocketAddress for @native and @len. |
||
139 | * |
||
140 | * Returns: a new #GNativeSocketAddress |
||
141 | * |
||
142 | * Since: 2.46 |
||
143 | */ |
||
144 | GSocketAddress * |
||
145 | g_native_socket_address_new (gpointer native, |
||
146 | gsize len) |
||
147 | { |
||
148 | GNativeSocketAddress *addr; |
||
149 | |||
150 | addr = g_object_new (G_TYPE_NATIVE_SOCKET_ADDRESS, NULL); |
||
151 | |||
152 | if (len <= sizeof(addr->priv->storage)) |
||
153 | addr->priv->sockaddr = (struct sockaddr*)&addr->priv->storage; |
||
154 | else |
||
155 | addr->priv->sockaddr = g_malloc (len); |
||
156 | |||
157 | memcpy (addr->priv->sockaddr, native, len); |
||
158 | addr->priv->sockaddr_len = len; |
||
159 | return G_SOCKET_ADDRESS (addr); |
||
160 | } |