Finish merging, i suck at git
[cascardo/gnio.git] / gnio / ginet4address.c
index a7473f8..61f1226 100644 (file)
@@ -1,6 +1,6 @@
 /* GNIO - GLib Network Layer of GIO
  * 
- * Copyright (C) 2008 Christian Kellner 
+ * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  * Boston, MA 02111-1307, USA.
  *
- * Author: Christian Kellner <gicmo@gnome.org>
+ * Authors: Christian Kellner <gicmo@gnome.org>
+ *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
  */
 
 #include <config.h>
 #include <glib.h>
 
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#ifndef G_OS_WIN32
+# include <netinet/in.h>
+# include <arpa/inet.h>
+#else
+# include <winsock2.h>
+# define IN_LOOPBACKNET 127
+#endif
 
 #include "ginet4address.h"
 
 G_DEFINE_TYPE (GInet4Address, g_inet4_address, G_TYPE_INET_ADDRESS);
 
-struct _GInet4AddressPrivate {
+struct _GInet4AddressPrivate
+{
   union {
     guint8  u4_addr8[4];
     guint32 u4_addr32;
   } addr;
 };
 
+static gchar *
+g_inet4_address_to_string (GInetAddress *address)
+{
+  guint8 *addr;
+
+  g_return_val_if_fail (G_IS_INET4_ADDRESS (address), NULL);
+
+  addr = G_INET4_ADDRESS (address)->priv->addr.u4_addr8;
+
+  return g_strdup_printf ("%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
+}
+
 static gboolean
 g_inet4_address_is_any (GInetAddress *address)
 {
@@ -150,6 +169,7 @@ g_inet4_address_class_init (GInet4AddressClass *klass)
   gobject_class->finalize = g_inet4_address_finalize;
   gobject_class->dispose = g_inet4_address_dispose;
 
+  ginetaddress_class->to_string = g_inet4_address_to_string;
   ginetaddress_class->is_any = g_inet4_address_is_any;
   ginetaddress_class->is_linklocal = g_inet4_address_is_linklocal;
   ginetaddress_class->is_loopback = g_inet4_address_is_loopback;
@@ -178,27 +198,24 @@ g_inet4_address_from_string (const gchar *string)
 {
   struct in_addr addr;
 
+#ifndef G_OS_WIN32
   if (!inet_aton (string, &addr))
     {
       g_warning ("Could not parse IP address %s", string);
       return NULL;
     }
+#else
+  addr.s_addr = inet_addr (string);
+  if (addr.s_addr == INADDR_NONE)
+    {
+      g_warning ("Could not parse IP address %s", string);
+      return NULL;
+    }
+#endif
 
   return g_inet4_address_from_bytes ((guint8 *) &(addr.s_addr));
 }
 
-char *
-g_inet4_address_to_string (GInet4Address *address)
-{
-  guint8 *addr;
-
-  g_return_val_if_fail (G_IS_INET4_ADDRESS (address), NULL);
-
-  addr = address->priv->addr.u4_addr8;
-
-  return g_strdup_printf ("%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);
-}
-
 GInet4Address *
 g_inet4_address_from_bytes (guint8 bytes[])
 {
@@ -225,7 +242,7 @@ g_inet4_address_to_bytes (GInet4Address *address)
 GInet4Address *
 g_inet4_address_new_loopback (void)
 {
-  guint8 addr[8] = {127, 0, 0, 1};
+  guint8 addr[4] = {127, 0, 0, 1};
 
   return g_inet4_address_from_bytes (addr);
 }
@@ -233,7 +250,7 @@ g_inet4_address_new_loopback (void)
 GInet4Address *
 g_inet4_address_new_any (void)
 {
-  guint8 addr[8] = {0, 0, 0, 0};
+  guint8 addr[4] = {0, 0, 0, 0};
 
   return g_inet4_address_from_bytes (addr);
 }