1 /* GNIO - GLib Network Layer of GIO
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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Christian Kellner <gicmo@gnome.org>
21 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
30 #include "ginetsocketaddress.h"
31 #include "ginetaddress.h"
32 #include "ginet4address.h"
33 #include "ginet6address.h"
35 G_DEFINE_TYPE (GInetSocketAddress, g_inet_socket_address, G_TYPE_SOCKET_ADDRESS);
43 struct _GInetSocketAddressPrivate
45 GInetAddress *address;
50 g_inet_socket_address_finalize (GObject *object)
52 GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
54 if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize)
55 (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->finalize) (object);
59 g_inet_socket_address_dispose (GObject *object)
61 GInetSocketAddress *address G_GNUC_UNUSED = G_INET_SOCKET_ADDRESS (object);
63 if (G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose)
64 (*G_OBJECT_CLASS (g_inet_socket_address_parent_class)->dispose) (object);
68 g_inet_socket_address_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
70 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
75 g_value_set_object (value, address->priv->address);
79 g_value_set_uint (value, address->priv->port);
83 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
88 g_inet_socket_address_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
90 GInetSocketAddress *address = G_INET_SOCKET_ADDRESS (object);
95 address->priv->address = G_INET_ADDRESS (g_value_get_object (value));
99 address->priv->port = (guint16) g_value_get_uint (value);
104 g_inet_socket_address_native_size (GSocketAddress *address)
106 GInetSocketAddress *addr;
108 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
110 addr = G_INET_SOCKET_ADDRESS (address);
112 if (G_IS_INET4_ADDRESS (addr->priv->address))
113 return sizeof (struct sockaddr_in);
114 else if (G_IS_INET6_ADDRESS (addr->priv->address))
115 return sizeof (struct sockaddr_in6);
121 g_inet_socket_address_to_native (GSocketAddress *address, gpointer dest)
123 GInetSocketAddress *addr;
125 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (address), 0);
127 addr = G_INET_SOCKET_ADDRESS (address);
129 if (G_IS_INET4_ADDRESS (addr->priv->address))
131 struct sockaddr_in *sock = (struct sockaddr_in *) dest;
133 sock->sin_family = AF_INET;
134 sock->sin_port = addr->priv->port;
135 memcpy (&(sock->sin_addr.s_addr), g_inet4_address_to_bytes (G_INET4_ADDRESS (addr->priv->address)), sizeof (sock->sin_addr));
136 memset (sock->sin_zero, 0, sizeof (sock->sin_zero));
139 else if (G_IS_INET6_ADDRESS (addr->priv->address))
148 g_inet_socket_address_class_init (GInetSocketAddressClass *klass)
150 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
151 GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
153 g_type_class_add_private (klass, sizeof (GInetSocketAddressPrivate));
155 gobject_class->finalize = g_inet_socket_address_finalize;
156 gobject_class->dispose = g_inet_socket_address_dispose;
157 gobject_class->set_property = g_inet_socket_address_set_property;
158 gobject_class->get_property = g_inet_socket_address_get_property;
160 gsocketaddress_class->to_native = g_inet_socket_address_to_native;
161 gsocketaddress_class->native_size = g_inet_socket_address_native_size;
163 g_object_class_install_property (gobject_class, PROP_ADDRESS,
164 g_param_spec_object ("address",
168 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
170 g_object_class_install_property (gobject_class, PROP_PORT,
171 g_param_spec_uint ("port",
177 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
181 g_inet_socket_address_init (GInetSocketAddress *address)
183 address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address,
184 G_TYPE_INET_SOCKET_ADDRESS,
185 GInetSocketAddressPrivate);
187 address->priv->address = NULL;
188 address->priv->port = 0;
193 g_inet_socket_address_new (GInetAddress *address, guint16 port)
200 g_inet_socket_address_get_address (GInetSocketAddress *sockaddr)
202 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (sockaddr), NULL);
204 return sockaddr->priv->address;
208 g_inet_socket_address_get_port (GInetSocketAddress *sockaddr)
210 g_return_val_if_fail (G_IS_INET_SOCKET_ADDRESS (sockaddr), 0);
212 return sockaddr->priv->port;