From: Christian Grothoff Date: Thu, 7 Jul 2011 15:32:31 +0000 (+0000) Subject: adding code to get external IP from miniupnp tool X-Git-Tag: initial-import-from-subversion-38251~17947 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=1c5ace41aa144afd32221db565c6f627765afdd0;p=oweals%2Fgnunet.git adding code to get external IP from miniupnp tool --- diff --git a/src/include/gnunet_nat_lib.h b/src/include/gnunet_nat_lib.h index 1eb8b5703..357758677 100644 --- a/src/include/gnunet_nat_lib.h +++ b/src/include/gnunet_nat_lib.h @@ -186,6 +186,21 @@ void 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 */ diff --git a/src/nat/Makefile.am b/src/nat/Makefile.am index 5aafdb240..769b68bd1 100644 --- a/src/nat/Makefile.am +++ b/src/nat/Makefile.am @@ -43,7 +43,9 @@ endif 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 \ diff --git a/src/nat/nat_mini.c b/src/nat/nat_mini.c new file mode 100644 index 000000000..b67718407 --- /dev/null +++ b/src/nat/nat_mini.c @@ -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 */