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 "gsocketaddress.h"
31 #include "ginetsocketaddress.h"
32 #include "ginet4address.h"
34 G_DEFINE_ABSTRACT_TYPE (GSocketAddress, g_socket_address, G_TYPE_INITIALLY_UNOWNED);
37 g_socket_address_class_init (GSocketAddressClass *klass)
43 g_socket_address_init (GSocketAddress *address)
49 g_socket_address_native_size (GSocketAddress *address)
51 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), -1);
53 return G_SOCKET_ADDRESS_GET_CLASS (address)->native_size (address);
57 g_socket_address_to_native (GSocketAddress *address, gpointer dest)
59 g_return_val_if_fail (G_IS_SOCKET_ADDRESS (address), FALSE);
61 return G_SOCKET_ADDRESS_GET_CLASS (address)->to_native (address, dest);
65 g_socket_address_from_native (gpointer native, gsize len)
69 if (len < sizeof (gshort))
72 family = ((struct sockaddr *) native)->sa_family;
74 if (family == AF_UNSPEC)
77 if (family == AF_INET)
79 struct sockaddr_in *addr = (struct sockaddr_in *) native;
81 return G_SOCKET_ADDRESS (g_inet_socket_address_new (G_INET_ADDRESS (g_inet4_address_from_bytes ((guint8 *) &(addr->sin_addr))), g_ntohs (addr->sin_port)));
83 // TODO: handle AF_INET6 and AF_UNIX