Update German translation
[glib.git] / gio / gnativesocketaddress.c
blobc1c175669d16455ca6819bca2bf2d9b81249e498
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 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>
22 #include <config.h>
23 #include <glib.h>
24 #include <string.h>
26 #include "gnativesocketaddress.h"
27 #include "gnetworkingprivate.h"
28 #include "gioerror.h"
29 #include "glibintl.h"
32 /**
33 * SECTION:gnativesocketaddress
34 * @short_description: Native GSocketAddress
35 * @include: gio/gio.h
37 * An socket address of some unknown native type.
40 /**
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;
50 struct sockaddr_storage storage;
51 gsize sockaddr_len;
54 G_DEFINE_TYPE_WITH_PRIVATE (GNativeSocketAddress, g_native_socket_address, G_TYPE_SOCKET_ADDRESS)
56 static void
57 g_native_socket_address_dispose (GObject *object)
59 GNativeSocketAddress *address = G_NATIVE_SOCKET_ADDRESS (object);
61 if (address->priv->sockaddr != (struct sockaddr *)&address->priv->storage)
62 g_free (address->priv->sockaddr);
64 G_OBJECT_CLASS (g_native_socket_address_parent_class)->dispose (object);
67 static GSocketFamily
68 g_native_socket_address_get_family (GSocketAddress *address)
70 GNativeSocketAddress *addr;
72 g_return_val_if_fail (G_IS_NATIVE_SOCKET_ADDRESS (address), 0);
74 addr = G_NATIVE_SOCKET_ADDRESS (address);
76 return addr->priv->sockaddr->sa_family;
79 static gssize
80 g_native_socket_address_get_native_size (GSocketAddress *address)
82 GNativeSocketAddress *addr;
84 g_return_val_if_fail (G_IS_NATIVE_SOCKET_ADDRESS (address), 0);
86 addr = G_NATIVE_SOCKET_ADDRESS (address);
88 return addr->priv->sockaddr_len;
91 static gboolean
92 g_native_socket_address_to_native (GSocketAddress *address,
93 gpointer dest,
94 gsize destlen,
95 GError **error)
97 GNativeSocketAddress *addr;
99 g_return_val_if_fail (G_IS_NATIVE_SOCKET_ADDRESS (address), FALSE);
101 addr = G_NATIVE_SOCKET_ADDRESS (address);
103 if (destlen < addr->priv->sockaddr_len)
105 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
106 _("Not enough space for socket address"));
107 return FALSE;
110 memcpy (dest, addr->priv->sockaddr, addr->priv->sockaddr_len);
111 return TRUE;
114 static void
115 g_native_socket_address_class_init (GNativeSocketAddressClass *klass)
117 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
118 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
120 gobject_class->dispose = g_native_socket_address_dispose;
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;
127 static void
128 g_native_socket_address_init (GNativeSocketAddress *address)
130 address->priv = g_native_socket_address_get_instance_private (address);
134 * g_native_socket_address_new:
135 * @native: a native address object
136 * @len: the length of @native, in bytes
138 * Creates a new #GNativeSocketAddress for @native and @len.
140 * Returns: a new #GNativeSocketAddress
142 * Since: 2.46
144 GSocketAddress *
145 g_native_socket_address_new (gpointer native,
146 gsize len)
148 GNativeSocketAddress *addr;
150 addr = g_object_new (G_TYPE_NATIVE_SOCKET_ADDRESS, NULL);
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);
157 memcpy (addr->priv->sockaddr, native, len);
158 addr->priv->sockaddr_len = len;
159 return G_SOCKET_ADDRESS (addr);