diff options
-rw-r--r-- | src/nat/Makefile.am | 16 | ||||
-rw-r--r-- | src/nat/gnunet-service-nat.c | 277 |
2 files changed, 290 insertions, 3 deletions
diff --git a/src/nat/Makefile.am b/src/nat/Makefile.am index e4b013916..d8d50e1a4 100644 --- a/src/nat/Makefile.am +++ b/src/nat/Makefile.am | |||
@@ -38,7 +38,9 @@ bin_PROGRAMS = \ | |||
38 | gnunet-nat | 38 | gnunet-nat |
39 | 39 | ||
40 | libexec_PROGRAMS = \ | 40 | libexec_PROGRAMS = \ |
41 | $(NATBIN) | 41 | $(NATBIN) \ |
42 | gnunet-service-nat | ||
43 | |||
42 | 44 | ||
43 | gnunet_nat_server_SOURCES = \ | 45 | gnunet_nat_server_SOURCES = \ |
44 | gnunet-nat-server.c nat.h | 46 | gnunet-nat-server.c nat.h |
@@ -54,7 +56,7 @@ gnunet_helper_nat_client_SOURCES = \ | |||
54 | 56 | ||
55 | 57 | ||
56 | gnunet_nat_SOURCES = \ | 58 | gnunet_nat_SOURCES = \ |
57 | gnunet-nat.c nat.h | 59 | gnunet-nat.c nat.h |
58 | gnunet_nat_LDADD = \ | 60 | gnunet_nat_LDADD = \ |
59 | libgnunetnat.la \ | 61 | libgnunetnat.la \ |
60 | $(top_builddir)/src/util/libgnunetutil.la | 62 | $(top_builddir)/src/util/libgnunetutil.la |
@@ -90,6 +92,15 @@ libgnunetnatnew_la_LDFLAGS = \ | |||
90 | $(GN_LIB_LDFLAGS) $(WINFLAGS) \ | 92 | $(GN_LIB_LDFLAGS) $(WINFLAGS) \ |
91 | -version-info 2:0:0 | 93 | -version-info 2:0:0 |
92 | 94 | ||
95 | gnunet_service_nat_SOURCES = \ | ||
96 | gnunet-service-nat.c | ||
97 | gnunet_service_nat_LDADD = \ | ||
98 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
99 | $(top_builddir)/src/statistics/libgnunetstatistics.la \ | ||
100 | $(LIBGCRYPT_LIBS) \ | ||
101 | -lgcrypt \ | ||
102 | $(GN_LIBINTL) | ||
103 | |||
93 | check_PROGRAMS = \ | 104 | check_PROGRAMS = \ |
94 | test_nat \ | 105 | test_nat \ |
95 | test_nat_mini \ | 106 | test_nat_mini \ |
@@ -113,7 +124,6 @@ test_nat_mini_LDADD = \ | |||
113 | libgnunetnat.la \ | 124 | libgnunetnat.la \ |
114 | $(top_builddir)/src/util/libgnunetutil.la | 125 | $(top_builddir)/src/util/libgnunetutil.la |
115 | 126 | ||
116 | |||
117 | test_nat_test_SOURCES = \ | 127 | test_nat_test_SOURCES = \ |
118 | test_nat_test.c | 128 | test_nat_test.c |
119 | test_nat_test_LDADD = \ | 129 | test_nat_test_LDADD = \ |
diff --git a/src/nat/gnunet-service-nat.c b/src/nat/gnunet-service-nat.c new file mode 100644 index 000000000..607797081 --- /dev/null +++ b/src/nat/gnunet-service-nat.c | |||
@@ -0,0 +1,277 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2016 GNUnet e.V. | ||
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., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file nat/gnunet-service-nat.c | ||
23 | * @brief network address translation traversal service | ||
24 | * @author Christian Grothoff | ||
25 | * | ||
26 | * The purpose of this service is to enable transports to | ||
27 | * traverse NAT routers, by providing traversal options and | ||
28 | * knowledge about the local network topology. | ||
29 | */ | ||
30 | #include "platform.h" | ||
31 | #include <math.h> | ||
32 | #include "gnunet_util_lib.h" | ||
33 | #include "gnunet_protocols.h" | ||
34 | #include "gnunet_signatures.h" | ||
35 | #include "gnunet_statistics_service.h" | ||
36 | #include "gnunet_nat_service.h" | ||
37 | #include "nat.h" | ||
38 | #include <gcrypt.h> | ||
39 | |||
40 | |||
41 | /** | ||
42 | * Internal data structure we track for each of our clients. | ||
43 | */ | ||
44 | struct ClientHandle | ||
45 | { | ||
46 | |||
47 | /** | ||
48 | * Kept in a DLL. | ||
49 | */ | ||
50 | struct ClientHandle *next; | ||
51 | |||
52 | /** | ||
53 | * Kept in a DLL. | ||
54 | */ | ||
55 | struct ClientHandle *prev; | ||
56 | |||
57 | /** | ||
58 | * Underlying handle for this client with the service. | ||
59 | */ | ||
60 | struct GNUNET_SERVICE_Client *client; | ||
61 | |||
62 | /** | ||
63 | * Message queue for communicating with the client. | ||
64 | */ | ||
65 | struct GNUNET_MQ_Handle *mq; | ||
66 | |||
67 | /** | ||
68 | * What does this client care about? | ||
69 | */ | ||
70 | enum GNUNET_NAT_RegisterFlags flags; | ||
71 | |||
72 | /** | ||
73 | * Client's IPPROTO, e.g. IPPROTO_UDP or IPPROTO_TCP. | ||
74 | */ | ||
75 | uint8_t proto; | ||
76 | |||
77 | /** | ||
78 | * Port we would like as we are configured to use this one for | ||
79 | * advertising (in addition to the one we are binding to). | ||
80 | */ | ||
81 | uint16_t adv_port; | ||
82 | |||
83 | /** | ||
84 | * Number of addresses that this service is bound to. | ||
85 | */ | ||
86 | uint16_t num_addrs; | ||
87 | |||
88 | /** | ||
89 | * Array of addresses used by the service. | ||
90 | */ | ||
91 | struct sockaddr **addrs; | ||
92 | |||
93 | }; | ||
94 | |||
95 | |||
96 | /** | ||
97 | * Handle to our current configuration. | ||
98 | */ | ||
99 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
100 | |||
101 | /** | ||
102 | * Handle to the statistics service. | ||
103 | */ | ||
104 | static struct GNUNET_STATISTICS_Handle *stats; | ||
105 | |||
106 | /** | ||
107 | * Task scheduled to periodically scan our network interfaces. | ||
108 | */ | ||
109 | static struct GNUNET_SCHEDULER_Task *scan_task; | ||
110 | |||
111 | /** | ||
112 | * Head of client DLL. | ||
113 | */ | ||
114 | static struct ClientHandle *ch_head; | ||
115 | |||
116 | /** | ||
117 | * Tail of client DLL. | ||
118 | */ | ||
119 | static struct ClientHandle *ch_tail; | ||
120 | |||
121 | |||
122 | /** | ||
123 | * Handler for #GNUNET_MESSAGE_TYPE_NAT_REGISTER message from client. | ||
124 | * We remember the client for updates upon future NAT events. | ||
125 | * | ||
126 | * @param cls client who sent the message | ||
127 | * @param message the message received | ||
128 | */ | ||
129 | static int | ||
130 | check_register (void *cls, | ||
131 | const struct GNUNET_NAT_RegisterMessage *message) | ||
132 | { | ||
133 | GNUNET_break (0); // not implemented | ||
134 | return GNUNET_SYSERR; | ||
135 | } | ||
136 | |||
137 | |||
138 | /** | ||
139 | * Handler for #GNUNET_MESSAGE_TYPE_NAT_REGISTER message from client. | ||
140 | * We remember the client for updates upon future NAT events. | ||
141 | * | ||
142 | * @param cls client who sent the message | ||
143 | * @param message the message received | ||
144 | */ | ||
145 | static void | ||
146 | handle_register (void *cls, | ||
147 | const struct GNUNET_NAT_RegisterMessage *message) | ||
148 | { | ||
149 | struct ClientHandle *ch = cls; | ||
150 | // struct GNUNET_MQ_Handle *mq; | ||
151 | |||
152 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
153 | "Received REGISTER message from client\n"); | ||
154 | GNUNET_SERVICE_client_continue (ch->client); | ||
155 | } | ||
156 | |||
157 | |||
158 | /** | ||
159 | * Task run during shutdown. | ||
160 | * | ||
161 | * @param cls unused | ||
162 | */ | ||
163 | static void | ||
164 | shutdown_task (void *cls) | ||
165 | { | ||
166 | if (NULL != scan_task) | ||
167 | { | ||
168 | GNUNET_SCHEDULER_cancel (scan_task); | ||
169 | scan_task = NULL; | ||
170 | } | ||
171 | if (NULL != stats) | ||
172 | { | ||
173 | GNUNET_STATISTICS_destroy (stats, GNUNET_NO); | ||
174 | stats = NULL; | ||
175 | } | ||
176 | } | ||
177 | |||
178 | |||
179 | /** | ||
180 | * Handle network size estimate clients. | ||
181 | * | ||
182 | * @param cls closure | ||
183 | * @param c configuration to use | ||
184 | * @param service the initialized service | ||
185 | */ | ||
186 | static void | ||
187 | run (void *cls, | ||
188 | const struct GNUNET_CONFIGURATION_Handle *c, | ||
189 | struct GNUNET_SERVICE_Handle *service) | ||
190 | { | ||
191 | cfg = c; | ||
192 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, | ||
193 | NULL); | ||
194 | stats = GNUNET_STATISTICS_create ("nat", | ||
195 | cfg); | ||
196 | } | ||
197 | |||
198 | |||
199 | /** | ||
200 | * Callback called when a client connects to the service. | ||
201 | * | ||
202 | * @param cls closure for the service | ||
203 | * @param c the new client that connected to the service | ||
204 | * @param mq the message queue used to send messages to the client | ||
205 | * @return a `struct ClientHandle` | ||
206 | */ | ||
207 | static void * | ||
208 | client_connect_cb (void *cls, | ||
209 | struct GNUNET_SERVICE_Client *c, | ||
210 | struct GNUNET_MQ_Handle *mq) | ||
211 | { | ||
212 | struct ClientHandle *ch; | ||
213 | |||
214 | ch = GNUNET_new (struct ClientHandle); | ||
215 | ch->mq = mq; | ||
216 | ch->client = c; | ||
217 | GNUNET_CONTAINER_DLL_insert (ch_head, | ||
218 | ch_tail, | ||
219 | ch); | ||
220 | return ch; | ||
221 | } | ||
222 | |||
223 | |||
224 | /** | ||
225 | * Callback called when a client disconnected from the service | ||
226 | * | ||
227 | * @param cls closure for the service | ||
228 | * @param c the client that disconnected | ||
229 | * @param internal_cls a `struct ClientHandle *` | ||
230 | */ | ||
231 | static void | ||
232 | client_disconnect_cb (void *cls, | ||
233 | struct GNUNET_SERVICE_Client *c, | ||
234 | void *internal_cls) | ||
235 | { | ||
236 | struct ClientHandle *ch = internal_cls; | ||
237 | |||
238 | GNUNET_CONTAINER_DLL_remove (ch_head, | ||
239 | ch_tail, | ||
240 | ch); | ||
241 | GNUNET_free (ch); | ||
242 | } | ||
243 | |||
244 | |||
245 | /** | ||
246 | * Define "main" method using service macro. | ||
247 | */ | ||
248 | GNUNET_SERVICE_MAIN | ||
249 | ("nat", | ||
250 | GNUNET_SERVICE_OPTION_NONE, | ||
251 | &run, | ||
252 | &client_connect_cb, | ||
253 | &client_disconnect_cb, | ||
254 | NULL, | ||
255 | GNUNET_MQ_hd_var_size (register, | ||
256 | GNUNET_MESSAGE_TYPE_NAT_REGISTER, | ||
257 | struct GNUNET_NAT_RegisterMessage, | ||
258 | NULL), | ||
259 | GNUNET_MQ_handler_end ()); | ||
260 | |||
261 | |||
262 | #if defined(LINUX) && defined(__GLIBC__) | ||
263 | #include <malloc.h> | ||
264 | |||
265 | /** | ||
266 | * MINIMIZE heap size (way below 128k) since this process doesn't need much. | ||
267 | */ | ||
268 | void __attribute__ ((constructor)) | ||
269 | GNUNET_ARM_memory_init () | ||
270 | { | ||
271 | mallopt (M_TRIM_THRESHOLD, 4 * 1024); | ||
272 | mallopt (M_TOP_PAD, 1 * 1024); | ||
273 | malloc_trim (0); | ||
274 | } | ||
275 | #endif | ||
276 | |||
277 | /* end of gnunet-service-nat.c */ | ||