summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuo Jinghua <sunmoon1997@gmail.com>2009-11-04 23:33:57 +0800
committerLuo Jinghua <sunmoon1997@gmail.com>2009-11-04 23:34:25 +0800
commit11651652c794ad58174f824baf34d2b80489ca66 (patch)
tree9a4859b433dca1bc5babf1619d5d25fd9c7a7061
parent0946453cdf5809809952bbba08d45af299f3aaa1 (diff)
milkway: add socket address abstract layer
-rw-r--r--milkway/Makefile.am12
-rw-r--r--milkway/mw-inet-address.c209
-rw-r--r--milkway/mw-inet-address.h73
-rw-r--r--milkway/mw-inet-socket-address.c130
-rw-r--r--milkway/mw-inet-socket-address.h56
-rw-r--r--milkway/mw-network-private.h45
-rw-r--r--milkway/mw-socket-address.c87
-rw-r--r--milkway/mw-socket-address.h92
-rw-r--r--milkway/mw-socket-family.h30
9 files changed, 732 insertions, 2 deletions
diff --git a/milkway/Makefile.am b/milkway/Makefile.am
index 7a51f8e..0059fe7 100644
--- a/milkway/Makefile.am
+++ b/milkway/Makefile.am
@@ -34,7 +34,11 @@ milkway_headers = \
mw-main-context.h \
mw-main-loop.h \
mw-timeout-source.h \
- mw-idle-source.h
+ mw-idle-source.h \
+ mw-socket-family.h \
+ mw-socket-address.h \
+ mw-inet-address.h \
+ mw-inet-socket-address.h
libmilkway_la_LDFLAGS = -version-info $(LT_VERSION_INFO) -no-undefined
libmilkway_la_LIBADD = @DEP_LIBS@ $(PTHREAD_LIBS)
@@ -84,7 +88,11 @@ libmilkway_la_SOURCES = \
mw-main-context.c \
mw-main-loop.c \
mw-timeout-source.c \
- mw-idle-source.c
+ mw-idle-source.c \
+ mw-network-private.h \
+ mw-socket-address.c \
+ mw-inet-address.c \
+ mw-inet-socket-address.c
milkwayincludedir = $(includedir)/milkway/milkway
milkwayinclude_HEADERS = $(milkway_headers)
diff --git a/milkway/mw-inet-address.c b/milkway/mw-inet-address.c
new file mode 100644
index 0000000..6729162
--- /dev/null
+++ b/milkway/mw-inet-address.c
@@ -0,0 +1,209 @@
+/* Milkway
+ *
+ * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#include "milkwayint.h"
+#include "milkway/mw-byteorder.h"
+#include "milkway/mw-inet-address.h"
+#include "milkway/mw-network-private.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+struct _mw_inet_address_priv {
+ mw_socket_family_t family;
+
+ union {
+ struct in_addr inet;
+ struct in6_addr inet6;
+ } addr;
+};
+
+static mw_inet_address_t*
+mw_inet_address_init(mw_inet_address_t *self,
+ void *addr,
+ mw_socket_family_t family)
+{
+ mw_inet_address_priv_t *priv = MW_GET_PRIV(self, MW_INET_ADDRESS_TYPE);
+
+ if (!mw_object_init(&self->base))
+ return NULL;
+
+ switch (family) {
+ case MW_SOCKET_FAMILY_INET:
+ memcpy(&priv->addr.inet, addr, sizeof(priv->addr.inet));
+ priv->family = MW_SOCKET_FAMILY_INET;
+ break;
+ case MW_SOCKET_FAMILY_INET6:
+ memcpy(&priv->addr.inet6, addr, sizeof(priv->addr.inet6));
+ priv->family = MW_SOCKET_FAMILY_INET6;
+ break;
+ case MW_SOCKET_FAMILY_INVALID:
+ case MW_SOCKET_FAMILY_UNIX:
+ default:
+ memset(&priv->addr, 0, sizeof(priv->addr));
+ priv->family = MW_SOCKET_FAMILY_INVALID;
+ break;
+ }
+ self->priv = priv;
+ return self;
+}
+
+mw_inet_address_t*
+mw_inet_address_new(const char *string)
+{
+ mw_inet_address_t *self;
+
+ self = mw_object_alloc(MW_INET_ADDRESS_TYPE);
+ if (!self)
+ return NULL;
+
+#ifdef MW_OS_WINDOWS
+ {
+ struct sockaddr_storage sa;
+ struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
+ int len;
+
+ memset(&sa, 0, sizeof(sa));
+ if (!WSAStringToAddress((LPTSTR)string, AF_INET, NULL, (LPSOCKADDR)&sa, len))
+ return mw_inet_address_init(self, &sin->sin_addr, MW_SOCKET_FAMILY_INET);
+ else if (!WSAStringToAddress((LPTSTR)string, AF_INET6, NULL, (LPSOCKADDR)&sa, len))
+ return mw_inet_address_init(self, &sin6->sin6_addr, MW_SOCKET_FAMILY_INET6);
+ }
+#else
+ {
+ struct in_addr in_addr;
+ struct in6_addr in6_addr;
+
+ if (inet_pton (AF_INET, string, &in_addr) > 0)
+ return mw_inet_address_init (self, &in_addr, MW_SOCKET_FAMILY_INET);
+ else if (inet_pton (AF_INET6, string, &in6_addr) > 0)
+ return mw_inet_address_init (self, &in6_addr, MW_SOCKET_FAMILY_INET6);
+ }
+#endif
+ MW_SUPER_FINALIZE(self, MW_INET_ADDRESS_TYPE)((mw_object_t*)self);
+ return NULL;
+}
+
+mw_inet_address_t*
+mw_inet_address_new_from_native(void *addr,
+ mw_socket_family_t family)
+{
+ mw_inet_address_t *self;
+
+ self = mw_object_alloc(MW_INET_ADDRESS_TYPE);
+ if (!self)
+ return NULL;
+
+ return mw_inet_address_init (self, addr, family);
+}
+
+mw_inet_address_t*
+mw_inet_address_new_loopback(mw_socket_family_t family)
+{
+ if (family == MW_SOCKET_FAMILY_INET)
+ return mw_inet_address_new("127.0.0.1");
+ else if (family == MW_SOCKET_FAMILY_INET6)
+ return mw_inet_address_new("::1");
+ return NULL;
+}
+
+mw_inet_address_t*
+mw_inet_address_new_any(mw_socket_family_t family)
+{
+ if (family == MW_SOCKET_FAMILY_INET) {
+ return mw_inet_address_new("0.0.0.0");
+ } else if (family == MW_SOCKET_FAMILY_INET6) {
+ return mw_inet_address_new("::");
+ }
+ return NULL;
+}
+
+mw_socket_family_t
+mw_inet_address_get_family(mw_inet_address_t *self)
+{
+ return self->priv->family;
+}
+
+mw_bool_t
+mw_inet_address_is_any(mw_inet_address_t *self)
+{
+ if (self->priv->family == MW_SOCKET_FAMILY_INET) {
+ mw_uint8_t any[4] = {0, 0, 0, 0};
+ return memcpy(&self->priv->addr.inet, any, sizeof(any)) == 0;
+ } else if (self->priv->family == MW_SOCKET_FAMILY_INET6) {
+ return IN6_IS_ADDR_UNSPECIFIED(&self->priv->addr.inet6);
+ }
+
+ return MW_FALSE;
+}
+
+mw_bool_t
+mw_inet_address_is_loopback(mw_inet_address_t *self)
+{
+ if (self->priv->family == MW_SOCKET_FAMILY_INET) {
+ uint32_t addr = mw_ntohl(self->priv->addr.inet.s_addr);
+
+ return (addr & 0xff000000) == 0x7f000000;
+ } else if (self->priv->family == MW_SOCKET_FAMILY_INET6) {
+ return IN6_IS_ADDR_LOOPBACK(&self->priv->addr.inet6);
+ }
+
+ return MW_FALSE;
+}
+
+size_t
+mw_inet_address_get_native_size(mw_inet_address_t *self)
+{
+ if (self->priv->family == MW_SOCKET_FAMILY_INET) {
+ return sizeof(self->priv->addr.inet);
+ } else if (self->priv->family == MW_SOCKET_FAMILY_INET6) {
+ return sizeof(self->priv->addr.inet6);
+ }
+
+ return MW_FALSE;
+}
+
+void*
+mw_inet_address_get_native(mw_inet_address_t *self)
+{
+ if (self->priv->family == MW_SOCKET_FAMILY_INET) {
+ return &self->priv->addr.inet;
+ } else if (self->priv->family == MW_SOCKET_FAMILY_INET6) {
+ return &self->priv->addr.inet6;
+ }
+
+ return NULL;
+}
+
+static void
+mw_inet_address_finalize(mw_object_t *super)
+{
+ MW_SUPER_FINALIZE(super, MW_INET_ADDRESS_TYPE)(super);
+}
+
+static void
+mw_inet_address_type_init(mw_inet_address_type_t *self)
+{
+ self->base.finalize = mw_inet_address_finalize;
+}
+
+MW_DEFINE_GET_TYPE(mw_inet_address, mw_inet_address_type_t,
+ MW_OBJECT_TYPE, "MWInetAddress",
+ sizeof(mw_inet_address_priv_t));
diff --git a/milkway/mw-inet-address.h b/milkway/mw-inet-address.h
new file mode 100644
index 0000000..ca5b3b2
--- /dev/null
+++ b/milkway/mw-inet-address.h
@@ -0,0 +1,73 @@
+/* Milkway
+ *
+ * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#ifndef MW_INET_ADDRESS_H
+#define MW_INET_ADDRESS_H
+
+#include <milkway/mw-object.h>
+#include <milkway/mw-socket-family.h>
+
+typedef struct _mw_inet_address_type mw_inet_address_type_t;
+typedef struct _mw_inet_address mw_inet_address_t;
+typedef struct _mw_inet_address_priv mw_inet_address_priv_t;
+
+#define MW_INET_ADDRESS_TYPE mw_inet_address_get_type()
+
+struct _mw_inet_address_type {
+ mw_object_type_t base;
+};
+
+struct _mw_inet_address {
+ mw_object_t base;
+
+ mw_inet_address_priv_t *priv;
+};
+
+mw_public mw_inet_address_type_t*
+mw_inet_address_get_type(void);
+
+mw_public mw_inet_address_t*
+mw_inet_address_new(const char *string);
+
+mw_public mw_inet_address_t*
+mw_inet_address_new_from_native(void *addr,
+ mw_socket_family_t family);
+
+mw_public mw_inet_address_t*
+mw_inet_address_new_loopback(mw_socket_family_t family);
+
+mw_public mw_inet_address_t*
+mw_inet_address_new_any(mw_socket_family_t family);
+
+mw_public mw_socket_family_t
+mw_inet_address_get_family(mw_inet_address_t *self);
+
+mw_public size_t
+mw_inet_address_get_native_size(mw_inet_address_t *self);
+
+mw_public void*
+mw_inet_address_get_native(mw_inet_address_t *self);
+
+mw_public mw_bool_t
+mw_inet_address_is_any(mw_inet_address_t *self);
+
+mw_public mw_bool_t
+mw_inet_address_is_loopback(mw_inet_address_t *self);
+
+#endif
diff --git a/milkway/mw-inet-socket-address.c b/milkway/mw-inet-socket-address.c
new file mode 100644
index 0000000..00dc73c
--- /dev/null
+++ b/milkway/mw-inet-socket-address.c
@@ -0,0 +1,130 @@
+/* Milkway
+ *
+ * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#include "milkwayint.h"
+#include "milkway/mw-byteorder.h"
+#include "milkway/mw-inet-socket-address.h"
+#include "milkway/mw-network-private.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+struct _mw_inet_socket_address_priv {
+ mw_inet_address_t *addr;
+ mw_uint16_t port;
+};
+
+static mw_inet_socket_address_t*
+mw_inet_socket_address_init(mw_inet_socket_address_t *self,
+ mw_inet_address_t *addr,
+ mw_uint16_t port)
+{
+ mw_inet_socket_address_priv_t *priv = MW_GET_PRIV(self, MW_INET_SOCKET_ADDRESS_TYPE);
+
+ if (!mw_socket_address_init(&self->base))
+ return NULL;
+
+ priv->addr = mw_object_ref(addr);
+ priv->port = port;
+
+ self->priv = priv;
+ return self;
+}
+
+mw_inet_socket_address_t*
+mw_inet_socket_address_new(mw_inet_address_t *address,
+ mw_uint16_t port)
+{
+ mw_inet_socket_address_t *self;
+
+ self = mw_object_alloc(MW_INET_SOCKET_ADDRESS_TYPE);
+ if (!self)
+ return NULL;
+
+ return mw_inet_socket_address_init (self, address, port);
+}
+
+mw_inet_address_t*
+mw_inet_socket_address_get_address(mw_inet_socket_address_t *self)
+{
+ return self->priv->addr;
+}
+
+mw_uint16_t
+mw_inet_socket_address_get_port(mw_inet_socket_address_t *self)
+{
+ return self->priv->port;
+}
+
+static mw_socket_family_t
+mw_inet_socket_address_get_family(mw_socket_address_t *super)
+{
+ mw_inet_socket_address_t *self = (mw_inet_socket_address_t *)super;
+
+ return mw_inet_address_get_family(self->priv->addr);
+}
+
+static size_t
+mw_inet_socket_address_get_native_size(mw_socket_address_t *super)
+{
+ mw_inet_socket_address_t *self = (mw_inet_socket_address_t *)super;
+
+ return mw_inet_address_get_native_size(self->priv->addr);
+}
+
+static mw_bool_t
+mw_inet_socket_address_to_native(mw_socket_address_t *super,
+ void *native,
+ size_t size,
+ mw_error_t **reterr)
+{
+ mw_inet_socket_address_t *self = (mw_inet_socket_address_t *)super;
+ size_t native_size = mw_inet_address_get_native_size(self->priv->addr);
+
+ if (native_size > size) {
+ mw_error_set(reterr, "socket", MW_TOO_SMALL,
+ "socket address buffers is too small");
+ return MW_FALSE;
+ }
+
+ memcpy(native, mw_inet_address_get_native(self->priv->addr), native_size);
+ return MW_TRUE;
+}
+
+static void
+mw_inet_socket_address_finalize(mw_object_t *super)
+{
+ mw_inet_socket_address_t *self = (mw_inet_socket_address_t*)super;
+
+ mw_object_unref(self->priv->addr);
+ MW_SUPER_FINALIZE(super, MW_INET_SOCKET_ADDRESS_TYPE)(super);
+}
+
+static void
+mw_inet_socket_address_type_init(mw_socket_address_type_t *self)
+{
+ self->base.finalize = mw_inet_socket_address_finalize;
+ self->get_family = mw_inet_socket_address_get_family;
+ self->get_native_size = mw_inet_socket_address_get_native_size;
+ self->to_native = mw_inet_socket_address_to_native;
+}
+
+MW_DEFINE_GET_TYPE(mw_inet_socket_address, mw_inet_socket_address_type_t,
+ MW_SOCKET_ADDRESS_TYPE, "MWInetSocketAddress",
+ sizeof(mw_inet_socket_address_priv_t));
diff --git a/milkway/mw-inet-socket-address.h b/milkway/mw-inet-socket-address.h
new file mode 100644
index 0000000..1c942fe
--- /dev/null
+++ b/milkway/mw-inet-socket-address.h
@@ -0,0 +1,56 @@
+/* Milkway
+ *
+ * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#ifndef MW_INET_SOCKET_ADDRESS_H
+#define MW_INET_SOCKET_ADDRESS_H
+
+#include <milkway/mw-socket-address.h>
+#include <milkway/mw-socket-family.h>
+#include <milkway/mw-inet-address.h>
+
+typedef struct _mw_inet_socket_address_type mw_inet_socket_address_type_t;
+typedef struct _mw_inet_socket_address mw_inet_socket_address_t;
+typedef struct _mw_inet_socket_address_priv mw_inet_socket_address_priv_t;
+
+#define MW_INET_SOCKET_ADDRESS_TYPE mw_inet_socket_address_get_type()
+
+struct _mw_inet_socket_address_type {
+ mw_socket_address_type_t base;
+};
+
+struct _mw_inet_socket_address {
+ mw_socket_address_t base;
+
+ mw_inet_socket_address_priv_t *priv;
+};
+
+mw_public mw_inet_socket_address_type_t*
+mw_inet_socket_address_get_type(void);
+
+mw_public mw_inet_socket_address_t*
+mw_inet_socket_address_new(mw_inet_address_t *addr,
+ mw_uint16_t port);
+
+mw_public mw_inet_address_t*
+mw_inet_socket_address_get_address(mw_inet_socket_address_t *self);
+
+mw_public mw_uint16_t
+mw_inet_socket_address_get_port(mw_inet_socket_address_t *self);
+
+#endif
diff --git a/milkway/mw-network-private.h b/milkway/mw-network-private.h
new file mode 100644
index 0000000..8d905c0
--- /dev/null
+++ b/milkway/mw-network-private.h
@@ -0,0 +1,45 @@
+/* Milkway
+ *
+ * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#ifndef MW_NETWORK_PRIVATE_H
+#define MW_NETWORK_PRIVATE_H
+
+#ifdef MW_OS_WINDOWS
+
+#define _WIN32_WINNT 0x0501
+#include <winsock2.h>
+#undef interface
+#include <ws2tcpip.h>
+#include <windns.h>
+#include <mswsock.h>
+
+#else
+
+#include <sys/types.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <resolv.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#endif
+
+#endif
diff --git a/milkway/mw-socket-address.c b/milkway/mw-socket-address.c
new file mode 100644
index 0000000..d260d80
--- /dev/null
+++ b/milkway/mw-socket-address.c
@@ -0,0 +1,87 @@
+/* Milkway
+ *
+ * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#include "milkwayint.h"
+#include "milkway/mw-byteorder.h"
+#include "milkway/mw-network-private.h"
+#include "milkway/mw-socket-address.h"
+#include "milkway/mw-inet-socket-address.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+mw_socket_address_t*
+mw_socket_address_init(mw_socket_address_t *self)
+{
+ if (!mw_object_init(&self->base))
+ return NULL;
+ return self;
+}
+
+mw_socket_address_t*
+mw_socket_address_new(void *native,
+ size_t size)
+{
+ struct sockaddr *addr = native;
+ mw_socket_address_t *self = NULL;
+
+ if (!addr)
+ return NULL;
+
+ if (addr->sa_family == AF_INET) {
+ struct sockaddr_in *inaddr = native;
+ mw_inet_address_t *inetaddr =
+ mw_inet_address_new_from_native(&inaddr->sin_addr,
+ MW_SOCKET_FAMILY_INET);
+ if (!inetaddr)
+ return NULL;
+
+ self = (mw_socket_address_t*)
+ mw_inet_socket_address_new(inetaddr, mw_ntohs(inaddr->sin_port));
+ mw_object_unref(inetaddr);
+ } else if (addr->sa_family == AF_INET6) {
+ struct sockaddr_in6 *inaddr = native;
+ mw_inet_address_t *inetaddr =
+ mw_inet_address_new_from_native(&inaddr->sin6_addr,
+ MW_SOCKET_FAMILY_INET6);
+ if (!inetaddr)
+ return NULL;
+
+ self = (mw_socket_address_t*)
+ mw_inet_socket_address_new(inetaddr, mw_ntohs(inaddr->sin6_port));
+ mw_object_unref(inetaddr);
+ }
+
+ return self;
+}
+
+static void
+mw_socket_address_finalize(mw_object_t *super)
+{
+ MW_SUPER_FINALIZE(super, MW_SOCKET_ADDRESS_TYPE)(super);
+}
+
+static void
+mw_socket_address_type_init(mw_socket_address_type_t *self)
+{
+ self->base.finalize = mw_socket_address_finalize;
+}
+
+MW_DEFINE_GET_TYPE(mw_socket_address, mw_socket_address_type_t,
+ MW_OBJECT_TYPE, "MWSocketAddress", 0);
diff --git a/milkway/mw-socket-address.h b/milkway/mw-socket-address.h
new file mode 100644
index 0000000..d809bf6
--- /dev/null
+++ b/milkway/mw-socket-address.h
@@ -0,0 +1,92 @@
+/* Milkway
+ *
+ * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#ifndef MW_SOCKET_ADDRESS_H
+#define MW_SOCKET_ADDRESS_H
+
+#include <milkway/mw-socket-family.h>
+#include <milkway/mw-object.h>
+#include <milkway/mw-error.h>
+
+typedef struct _mw_socket_address_type mw_socket_address_type_t;
+typedef struct _mw_socket_address mw_socket_address_t;
+typedef struct _mw_socket_address_priv mw_socket_address_priv_t;
+
+#define MW_SOCKET_ADDRESS_TYPE mw_socket_address_get_type()
+
+struct _mw_socket_address_type {
+ mw_object_type_t base;
+
+ mw_socket_family_t
+ (*get_family)(mw_socket_address_t *self);
+
+ size_t
+ (*get_native_size)(mw_socket_address_t *self);
+
+ mw_bool_t
+ (*to_native)(mw_socket_address_t *self,
+ void *native,
+ size_t size,
+ mw_error_t **reterr);
+};
+
+struct _mw_socket_address {
+ mw_object_t base;
+
+ mw_socket_address_priv_t *priv;
+};
+
+mw_public mw_socket_address_type_t*
+mw_socket_address_get_type(void);
+
+mw_public mw_socket_address_t*
+mw_socket_address_init(mw_socket_address_t *self);
+
+mw_public mw_socket_address_t*
+mw_socket_address_new(void *addr,
+ size_t size);
+
+static mw_inline mw_socket_family_t
+mw_socket_address_get_family(mw_socket_address_t *self)
+{
+ mw_socket_address_type_t *type = (mw_socket_address_type_t *)MW_TYPE(self);
+
+ return type->get_family(self);
+}
+
+static mw_inline size_t
+mw_socket_address_get_native_size(mw_socket_address_t *self)
+{
+ mw_socket_address_type_t *type = (mw_socket_address_type_t *)MW_TYPE(self);
+
+ return type->get_native_size(self);
+}
+
+static mw_inline mw_bool_t
+mw_socket_address_to_native(mw_socket_address_t *self,
+ void *native,
+ size_t size,
+ mw_error_t **reterr)
+{
+ mw_socket_address_type_t *type = (mw_socket_address_type_t *)MW_TYPE(self);
+
+ return type->to_native(self, native, size, reterr);
+}
+
+#endif
diff --git a/milkway/mw-socket-family.h b/milkway/mw-socket-family.h
new file mode 100644
index 0000000..198dda6
--- /dev/null
+++ b/milkway/mw-socket-family.h
@@ -0,0 +1,30 @@
+/* Milkway
+ *
+ * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#ifndef MW_SOCKET_FAMILY_H
+#define MW_SOCKET_FAMILY_H
+
+typedef enum {
+ MW_SOCKET_FAMILY_INVALID,
+ MW_SOCKET_FAMILY_INET,
+ MW_SOCKET_FAMILY_INET6,
+ MW_SOCKET_FAMILY_UNIX
+} mw_socket_family_t;
+
+#endif