gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r15877 - in gnunet/src: include nat


From: gnunet
Subject: [GNUnet-SVN] r15877 - in gnunet/src: include nat
Date: Thu, 7 Jul 2011 17:32:31 +0200

Author: grothoff
Date: 2011-07-07 17:32:31 +0200 (Thu, 07 Jul 2011)
New Revision: 15877

Added:
   gnunet/src/nat/nat_mini.c
Modified:
   gnunet/src/include/gnunet_nat_lib.h
   gnunet/src/nat/Makefile.am
Log:
adding code to get external IP from miniupnp tool

Modified: gnunet/src/include/gnunet_nat_lib.h
===================================================================
--- gnunet/src/include/gnunet_nat_lib.h 2011-07-07 15:06:33 UTC (rev 15876)
+++ gnunet/src/include/gnunet_nat_lib.h 2011-07-07 15:32:31 UTC (rev 15877)
@@ -186,6 +186,21 @@
 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst);
 
 
+
+/**
+ * Try to get the external IPv4 address of this peer.
+ * Note: calling this function may block this process
+ * for a few seconds (!).
+ *
+ * @param addr address to set
+ * @return GNUNET_OK on success,
+ *         GNUNET_NO if the result is questionable,
+ *         GNUNET_SYSERR on error
+ */
+int
+GNUNET_NAT_mini_get_external_ipv4 (struct in_addr *addr);
+
+
 #endif 
 
 /* end of gnunet_nat_lib.h */

Modified: gnunet/src/nat/Makefile.am
===================================================================
--- gnunet/src/nat/Makefile.am  2011-07-07 15:06:33 UTC (rev 15876)
+++ gnunet/src/nat/Makefile.am  2011-07-07 15:32:31 UTC (rev 15877)
@@ -43,7 +43,9 @@
 lib_LTLIBRARIES = libgnunetnat.la
 
 libgnunetnat_la_SOURCES = \
-  nat.c nat_test.c nat.h
+  nat.c nat.h \
+  nat_test.c \
+  nat_mini.c
 
 libgnunetnat_la_LIBADD = \
   $(top_builddir)/src/util/libgnunetutil.la \

Added: gnunet/src/nat/nat_mini.c
===================================================================
--- gnunet/src/nat/nat_mini.c                           (rev 0)
+++ gnunet/src/nat/nat_mini.c   2011-07-07 15:32:31 UTC (rev 15877)
@@ -0,0 +1,89 @@
+/*
+     This file is part of GNUnet.
+     (C) 2011 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 3, or (at your
+     option) any later version.
+
+     GNUnet 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
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file nat/nat_mini.c
+ * @brief functions for interaction with miniupnp
+ * @author Christian Grothoff
+ */
+#include "platform.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_nat_lib.h"
+#include "nat.h"
+
+
+/**
+ * Try to get the external IPv4 address of this peer.
+ * Note: calling this function may block this process
+ * for a few seconds (!).
+ *
+ * @param addr address to set
+ * @return GNUNET_OK on success,
+ *         GNUNET_NO if the result is questionable,
+ *         GNUNET_SYSERR on error
+ */
+int
+GNUNET_NAT_mini_get_external_ipv4 (struct in_addr *addr)
+{
+  struct GNUNET_OS_Process *eip;
+  struct GNUNET_DISK_PipeHandle *opipe;
+  const struct GNUNET_DISK_FileHandle *r;
+  size_t off;
+  char buf[17];
+  ssize_t ret;
+  int iret;
+
+  opipe = GNUNET_DISK_pipe (GNUNET_YES,
+                           GNUNET_NO,
+                           GNUNET_YES);
+  eip = GNUNET_OS_start_process (NULL,
+                                opipe,
+                                "external-ip",
+                                "external-ip", NULL);
+  if (NULL == eip)
+    {
+      GNUNET_DISK_pipe_close (opipe);
+      return GNUNET_SYSERR;
+    }
+  GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
+  iret = GNUNET_SYSERR;
+  r = GNUNET_DISK_pipe_handle (opipe,
+                              GNUNET_DISK_PIPE_END_READ);
+  off = 0;
+  while (0 < (ret = GNUNET_DISK_file_read (r, &buf[off], sizeof (buf)-off)))
+    off += ret;
+  if ( (off > 7) &&    
+       (buf[off-1] == '\n') )    
+    {
+      buf[off-1] = '\0';
+      if (1 == inet_pton (AF_INET, buf, addr))
+       {
+         if (addr->s_addr == 0)
+           iret = GNUNET_NO; /* got 0.0.0.0 */
+         iret = GNUNET_OK;
+       }
+    }
+  (void) GNUNET_OS_process_kill (eip, SIGKILL);
+  GNUNET_OS_process_close (eip);
+  GNUNET_DISK_pipe_close (opipe);
+  return iret; 
+}
+
+/* end of nat_mini.c */




reply via email to

[Prev in Thread] Current Thread [Next in Thread]