diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-07-07 15:32:31 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-07-07 15:32:31 +0000 |
commit | 1c5ace41aa144afd32221db565c6f627765afdd0 (patch) | |
tree | 7c0fe425bd18b00d1f6c9b81cf97730e68c6778c | |
parent | e6b9f7bb2febff33140dd3cb0ea96bed09a05346 (diff) | |
download | gnunet-1c5ace41aa144afd32221db565c6f627765afdd0.tar.gz gnunet-1c5ace41aa144afd32221db565c6f627765afdd0.zip |
adding code to get external IP from miniupnp tool
-rw-r--r-- | src/include/gnunet_nat_lib.h | 15 | ||||
-rw-r--r-- | src/nat/Makefile.am | 4 | ||||
-rw-r--r-- | src/nat/nat_mini.c | 89 |
3 files changed, 107 insertions, 1 deletions
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 | |||
186 | GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst); | 186 | GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst); |
187 | 187 | ||
188 | 188 | ||
189 | |||
190 | /** | ||
191 | * Try to get the external IPv4 address of this peer. | ||
192 | * Note: calling this function may block this process | ||
193 | * for a few seconds (!). | ||
194 | * | ||
195 | * @param addr address to set | ||
196 | * @return GNUNET_OK on success, | ||
197 | * GNUNET_NO if the result is questionable, | ||
198 | * GNUNET_SYSERR on error | ||
199 | */ | ||
200 | int | ||
201 | GNUNET_NAT_mini_get_external_ipv4 (struct in_addr *addr); | ||
202 | |||
203 | |||
189 | #endif | 204 | #endif |
190 | 205 | ||
191 | /* end of gnunet_nat_lib.h */ | 206 | /* 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 | |||
43 | lib_LTLIBRARIES = libgnunetnat.la | 43 | lib_LTLIBRARIES = libgnunetnat.la |
44 | 44 | ||
45 | libgnunetnat_la_SOURCES = \ | 45 | libgnunetnat_la_SOURCES = \ |
46 | nat.c nat_test.c nat.h | 46 | nat.c nat.h \ |
47 | nat_test.c \ | ||
48 | nat_mini.c | ||
47 | 49 | ||
48 | libgnunetnat_la_LIBADD = \ | 50 | libgnunetnat_la_LIBADD = \ |
49 | $(top_builddir)/src/util/libgnunetutil.la \ | 51 | $(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 @@ | |||
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 */ | ||