adding code to get external IP from miniupnp tool
[oweals/gnunet.git] / src / nat / nat_mini.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file nat/nat_mini.c
23  * @brief functions for interaction with miniupnp
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_nat_lib.h"
29 #include "nat.h"
30
31
32 /**
33  * Try to get the external IPv4 address of this peer.
34  * Note: calling this function may block this process
35  * for a few seconds (!).
36  *
37  * @param addr address to set
38  * @return GNUNET_OK on success,
39  *         GNUNET_NO if the result is questionable,
40  *         GNUNET_SYSERR on error
41  */
42 int
43 GNUNET_NAT_mini_get_external_ipv4 (struct in_addr *addr)
44 {
45   struct GNUNET_OS_Process *eip;
46   struct GNUNET_DISK_PipeHandle *opipe;
47   const struct GNUNET_DISK_FileHandle *r;
48   size_t off;
49   char buf[17];
50   ssize_t ret;
51   int iret;
52
53   opipe = GNUNET_DISK_pipe (GNUNET_YES,
54                             GNUNET_NO,
55                             GNUNET_YES);
56   eip = GNUNET_OS_start_process (NULL,
57                                  opipe,
58                                  "external-ip",
59                                  "external-ip", NULL);
60   if (NULL == eip)
61     {
62       GNUNET_DISK_pipe_close (opipe);
63       return GNUNET_SYSERR;
64     }
65   GNUNET_DISK_pipe_close_end (opipe, GNUNET_DISK_PIPE_END_WRITE);
66   iret = GNUNET_SYSERR;
67   r = GNUNET_DISK_pipe_handle (opipe,
68                                GNUNET_DISK_PIPE_END_READ);
69   off = 0;
70   while (0 < (ret = GNUNET_DISK_file_read (r, &buf[off], sizeof (buf)-off)))
71     off += ret;
72   if ( (off > 7) &&    
73        (buf[off-1] == '\n') )    
74     {
75       buf[off-1] = '\0';
76       if (1 == inet_pton (AF_INET, buf, addr))
77         {
78           if (addr->s_addr == 0)
79             iret = GNUNET_NO; /* got 0.0.0.0 */
80           iret = GNUNET_OK;
81         }
82     }
83   (void) GNUNET_OS_process_kill (eip, SIGKILL);
84   GNUNET_OS_process_close (eip);
85   GNUNET_DISK_pipe_close (opipe);
86   return iret; 
87 }
88
89 /* end of nat_mini.c */