1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
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.1 of the License, or (at your option) any later version.
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.
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/>.
18 * Authors: Christian Kellner <gicmo@gnome.org>
19 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
26 #include "gnativesocketaddress.h"
27 #include "gnetworkingprivate.h"
33 * SECTION:gnativesocketaddress
34 * @short_description: Native GSocketAddress
37 * An socket address of some unknown native type.
41 * GNativeSocketAddress:
43 * An socket address, corresponding to a general struct
44 * sockadd address of a type not otherwise handled by glib.
47 struct _GNativeSocketAddressPrivate
49 struct sockaddr
*sockaddr
;
51 struct sockaddr_storage storage
;
57 G_DEFINE_TYPE_WITH_PRIVATE (GNativeSocketAddress
, g_native_socket_address
, G_TYPE_SOCKET_ADDRESS
)
60 g_native_socket_address_dispose (GObject
*object
)
62 GNativeSocketAddress
*address
= G_NATIVE_SOCKET_ADDRESS (object
);
64 if (address
->priv
->sockaddr
!= &address
->priv
->storage
.sa
)
65 g_free (address
->priv
->sockaddr
);
67 G_OBJECT_CLASS (g_native_socket_address_parent_class
)->dispose (object
);
71 g_native_socket_address_get_family (GSocketAddress
*address
)
73 GNativeSocketAddress
*addr
;
75 g_return_val_if_fail (G_IS_NATIVE_SOCKET_ADDRESS (address
), 0);
77 addr
= G_NATIVE_SOCKET_ADDRESS (address
);
79 return addr
->priv
->sockaddr
->sa_family
;
83 g_native_socket_address_get_native_size (GSocketAddress
*address
)
85 GNativeSocketAddress
*addr
;
87 g_return_val_if_fail (G_IS_NATIVE_SOCKET_ADDRESS (address
), 0);
89 addr
= G_NATIVE_SOCKET_ADDRESS (address
);
91 return addr
->priv
->sockaddr_len
;
95 g_native_socket_address_to_native (GSocketAddress
*address
,
100 GNativeSocketAddress
*addr
;
102 g_return_val_if_fail (G_IS_NATIVE_SOCKET_ADDRESS (address
), FALSE
);
104 addr
= G_NATIVE_SOCKET_ADDRESS (address
);
106 if (destlen
< addr
->priv
->sockaddr_len
)
108 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NO_SPACE
,
109 _("Not enough space for socket address"));
113 memcpy (dest
, addr
->priv
->sockaddr
, addr
->priv
->sockaddr_len
);
118 g_native_socket_address_class_init (GNativeSocketAddressClass
*klass
)
120 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
121 GSocketAddressClass
*gsocketaddress_class
= G_SOCKET_ADDRESS_CLASS (klass
);
123 gobject_class
->dispose
= g_native_socket_address_dispose
;
125 gsocketaddress_class
->get_family
= g_native_socket_address_get_family
;
126 gsocketaddress_class
->to_native
= g_native_socket_address_to_native
;
127 gsocketaddress_class
->get_native_size
= g_native_socket_address_get_native_size
;
131 g_native_socket_address_init (GNativeSocketAddress
*address
)
133 address
->priv
= g_native_socket_address_get_instance_private (address
);
137 * g_native_socket_address_new:
138 * @native: a native address object
139 * @len: the length of @native, in bytes
141 * Creates a new #GNativeSocketAddress for @native and @len.
143 * Returns: a new #GNativeSocketAddress
148 g_native_socket_address_new (gpointer native
,
151 GNativeSocketAddress
*addr
;
153 addr
= g_object_new (G_TYPE_NATIVE_SOCKET_ADDRESS
, NULL
);
155 if (len
<= sizeof(addr
->priv
->storage
))
156 addr
->priv
->sockaddr
= &addr
->priv
->storage
.sa
;
158 addr
->priv
->sockaddr
= g_malloc (len
);
160 memcpy (addr
->priv
->sockaddr
, native
, len
);
161 addr
->priv
->sockaddr_len
= len
;
162 return G_SOCKET_ADDRESS (addr
);