aboutsummaryrefslogtreecommitdiff
path: root/src/nat/test_stun.c
diff options
context:
space:
mode:
authorBruno Cabral <bcabral@uw.edu>2015-06-25 02:21:23 +0000
committerBruno Cabral <bcabral@uw.edu>2015-06-25 02:21:23 +0000
commit26ad92551ed4afd96f3d7742179509b850a5cc7a (patch)
treeddcf6699873afc2431df036193b1e598c15e142c /src/nat/test_stun.c
parent20d8a41eaa19064d16e9a004dea2d9abcdbd9731 (diff)
downloadgnunet-26ad92551ed4afd96f3d7742179509b850a5cc7a.tar.gz
gnunet-26ad92551ed4afd96f3d7742179509b850a5cc7a.zip
Polish and simplyfy STUN code, move STUN code to GNUNET_NAT_
Diffstat (limited to 'src/nat/test_stun.c')
-rw-r--r--src/nat/test_stun.c825
1 files changed, 208 insertions, 617 deletions
diff --git a/src/nat/test_stun.c b/src/nat/test_stun.c
index 086789f27..c45714f4d 100644
--- a/src/nat/test_stun.c
+++ b/src/nat/test_stun.c
@@ -1,617 +1,208 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2009, 2015 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2009, 2015 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 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 7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version. 8 option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details. 13 General Public License for more details.
14 14
15 You should have received a copy of the GNU General Public License 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 16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/** 21/**
22 * Testcase for STUN server resolution 22 * Testcase for STUN server resolution
23 * 23 *
24 * @file nat/test_stun.c 24 * @file nat/test_stun.c
25 * @brief Testcase for STUN library 25 * @brief Testcase for STUN library
26 * @author Bruno Souza Cabral - Major rewrite. 26 * @author Bruno Souza Cabral - Major rewrite.
27 * @autor Mark Spencer (Original code - borrowed from Asterisk) 27
28 * 28 *
29 */ 29 */
30 30
31 31
32#include "platform.h" 32#include "platform.h"
33#include "gnunet_util_lib.h" 33#include "gnunet_util_lib.h"
34#include "gnunet_program_lib.h" 34#include "gnunet_program_lib.h"
35#include "gnunet_scheduler_lib.h" 35#include "gnunet_scheduler_lib.h"
36#include "gnunet_nat_lib.h" 36#include "gnunet_nat_lib.h"
37 37
38 38
39#include "test_stun.h" 39
40 40#define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__)
41#define LOG(kind,...) GNUNET_log_from (kind, "stun", __VA_ARGS__) 41
42 42/**
43/** 43 * The port the test service is running on (default 7895)
44 * The port the test service is running on (default 7895) 44 */
45 */ 45static unsigned long port = 7895;
46static unsigned long port = 7895; 46static int ret = 1;
47static int ret = 1; 47
48 48static char *stun_server = "stun.ekiga.net";
49static char *stun_server = STUN_SERVER; 49static int stun_port = 3478;
50static int stun_port = STUN_PORT; 50
51 51/**
52/** 52 * The listen socket of the service for IPv4
53 * The listen socket of the service for IPv4 53 */
54 */ 54static struct GNUNET_NETWORK_Handle *lsock4;
55static struct GNUNET_NETWORK_Handle *lsock4; 55
56 56
57 57/**
58/** 58 * The listen task ID for IPv4
59 * The listen task ID for IPv4 59 */
60 */ 60static struct GNUNET_SCHEDULER_Task * ltask4;
61static struct GNUNET_SCHEDULER_Task * ltask4; 61
62 62
63 63
64 64
65/** 65static void
66 * Handle to a request given to the resolver. Can be used to cancel 66print_answer(struct sockaddr_in* answer)
67 * the request prior to the timeout or successful execution. Also 67{
68 * used to track our internal state for the request. 68 printf("External IP is: %s , with port %d\n", inet_ntoa(answer->sin_addr), ntohs(answer->sin_port));
69 */ 69}
70struct GNUNET_NAT_StunRequestHandle { 70
71 71
72 /** 72/**
73 * Handle to a pending DNS lookup request. 73 * Activity on our incoming socket. Read data from the
74 */ 74 * incoming connection.
75 struct GNUNET_RESOLVER_RequestHandle *dns_active; 75 *
76 76 * @param cls
77 77 * @param tc scheduler context
78 /** 78 */
79 * Handle to the listen socket 79static void
80 */ 80do_udp_read (void *cls,
81 struct GNUNET_NETWORK_Handle * sock; 81 const struct GNUNET_SCHEDULER_TaskContext *tc)
82 82{
83}; 83 //struct GNUNET_NAT_Test *tst = cls;
84 84 unsigned char reply_buf[1024];
85 85 ssize_t rlen;
86 86 struct sockaddr_in answer;
87 87
88/* here we store credentials extracted from a message */ 88
89struct StunState { 89 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
90 uint16_t attr; 90 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
91}; 91 lsock4)))
92 92 {
93/* callback type to be invoked on stun responses. */ 93 rlen = GNUNET_NETWORK_socket_recv (lsock4, reply_buf, sizeof (reply_buf));
94typedef int (stun_cb_f)(struct StunState *st, struct stun_attr *attr, void *arg, unsigned int magic); 94 printf("Recivied something of size %d", rlen);
95 95
96 96 //Lets handle the packet
97 97 memset(&answer, 0, sizeof(struct sockaddr_in));
98/** 98 GNUNET_NAT_stun_handle_packet(reply_buf,rlen, &answer);
99 * Convert a message to a StunClass 99
100 * 100 //Print the anser
101 * @param msg the received message 101 //TODO: Delete the object
102 * @return the converted StunClass 102 ret = 0;
103 */ 103 print_answer(&answer);
104static int decode_class(int msg) 104
105{ 105
106 return ((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7); 106 }
107} 107}
108 108
109/** 109
110 * Convert a message to a StunMethod 110/**
111 * 111 * Create an IPv4 listen socket bound to our port.
112 * @param msg the received message 112 *
113 * @return the converted StunMethod 113 * @return NULL on error
114 */ 114 */
115static int decode_method(int msg) 115static struct GNUNET_NETWORK_Handle *
116{ 116bind_v4 ()
117 return (msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2); 117{
118} 118 struct GNUNET_NETWORK_Handle *ls;
119 119 struct sockaddr_in sa4;
120/** 120 int eno;
121 * Encode a class and method to a compatible STUN format 121
122 * 122 memset (&sa4, 0, sizeof (sa4));
123 * @param msg_class class to be converted 123 sa4.sin_family = AF_INET;
124 * @param method method to be converted 124 sa4.sin_port = htons (port);
125 * @return message in a STUN compatible format 125#if HAVE_SOCKADDR_IN_SIN_LEN
126 */ 126 sa4.sin_len = sizeof (sa4);
127static int encode_message(StunClasses msg_class, StunMethods method) 127#endif
128{ 128 ls = GNUNET_NETWORK_socket_create (AF_INET,
129 return ((msg_class & 1) << 4) | ((msg_class & 2) << 7) | 129 SOCK_DGRAM,
130 (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800) << 2); 130 0);
131} 131 if (NULL == ls)
132 132 return NULL;
133/* helper function to print message names */ 133 if (GNUNET_OK !=
134static const char *stun_msg2str(int msg) 134 GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
135{ 135 sizeof (sa4)))
136 136 {
137 const struct { enum StunClasses value; const char *name; } classes[] = { 137 eno = errno;
138 { STUN_REQUEST, "Request" }, 138 GNUNET_NETWORK_socket_close (ls);
139 { STUN_INDICATION, "Indication" }, 139 errno = eno;
140 { STUN_RESPONSE, "Response" }, 140 return NULL;
141 { STUN_ERROR_RESPONSE, "Error Response" }, 141 }
142 { 0, NULL } 142 return ls;
143 }; 143}
144 144
145 const struct { enum StunMethods value; const char *name; } methods[] = { 145
146 { STUN_BINDING, "Binding" }, 146
147 { 0, NULL } 147/**
148 }; 148 * Main function run with scheduler.
149 149 */
150 static char result[32]; 150
151 const char *msg_class = NULL; 151
152 const char *method = NULL; 152static void
153 int i; 153run (void *cls, char *const *args, const char *cfgfile,
154 int value; 154 const struct GNUNET_CONFIGURATION_Handle *cfg)
155 155{
156 value = decode_class(msg); 156
157 for (i = 0; classes[i].name; i++) { 157
158 msg_class = classes[i].name; 158 //Lets create the socket
159 if (classes[i].value == value) 159 lsock4 = bind_v4 ();
160 break; 160 if (NULL == lsock4)
161 } 161 {
162 value = decode_method(msg); 162 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
163 for (i = 0; methods[i].name; i++) { 163 }
164 method = methods[i].name; 164 else
165 if (methods[i].value == value) 165 {
166 break; 166 printf("Binded, now will call add_read\n");
167 } 167 //Lets call our function now when it accepts
168 snprintf(result, sizeof(result), "%s %s", 168 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
169 method ? : "Unknown Method", 169 lsock4, &do_udp_read, NULL);
170 msg_class ? : "Unknown Class Message"); 170
171 return result; 171 }
172} 172 if(NULL == lsock4 )
173 173 {
174/* helper function to print attribute names */ 174 GNUNET_SCHEDULER_shutdown ();
175static const char *stun_attr2str(int msg) 175 return;
176{ 176 }
177 const struct { enum StunAttributes value; const char *name; } attrs[] = { 177 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
178 { STUN_MAPPED_ADDRESS, "Mapped Address" }, 178 "Service listens on port %u\n",
179 { STUN_RESPONSE_ADDRESS, "Response Address" }, 179 port);
180 { STUN_CHANGE_ADDRESS, "Change Address" }, 180 printf("Start main event\n");
181 { STUN_SOURCE_ADDRESS, "Source Address" }, 181 GNUNET_NAT_stun_make_request(stun_server, stun_port, lsock4);
182 { STUN_CHANGED_ADDRESS, "Changed Address" }, 182 //Main event
183 { STUN_USERNAME, "Username" }, 183 //main_task = GNUNET_SCHEDULER_add_delayed (timeout, &do_timeout, nh);
184 { STUN_PASSWORD, "Password" }, 184
185 { STUN_MESSAGE_INTEGRITY, "Message Integrity" }, 185}
186 { STUN_ERROR_CODE, "Error Code" }, 186
187 { STUN_UNKNOWN_ATTRIBUTES, "Unknown Attributes" }, 187
188 { STUN_REFLECTED_FROM, "Reflected From" }, 188int
189 { STUN_REALM, "Realm" }, 189main (int argc, char *const argv[])
190 { STUN_NONCE, "Nonce" }, 190{
191 { STUN_XOR_MAPPED_ADDRESS, "XOR Mapped Address" }, 191 struct GNUNET_GETOPT_CommandLineOption options[] = {
192 { STUN_MS_VERSION, "MS Version" }, 192 GNUNET_GETOPT_OPTION_END
193 { STUN_MS_XOR_MAPPED_ADDRESS, "MS XOR Mapped Address" }, 193 };
194 { STUN_SOFTWARE, "Software" }, 194
195 { STUN_ALTERNATE_SERVER, "Alternate Server" }, 195 char *const argv_prog[] = {
196 { STUN_FINGERPRINT, "Fingerprint" }, 196 "test-stun",
197 { 0, NULL } 197 NULL
198 }; 198 };
199 int i; 199 GNUNET_log_setup ("test-stun",
200 200 "WARNING",
201 for (i = 0; attrs[i].name; i++) { 201 NULL);
202 if (attrs[i].value == msg) 202
203 return attrs[i].name; 203 GNUNET_PROGRAM_run (1, argv_prog, "test-stun", "nohelp", options, &run, NULL);
204 } 204
205 return "Unknown Attribute"; 205 return ret;
206} 206}
207 207
208 208/* end of test_nat.c */
209
210static int stun_process_attr(struct StunState *state, struct stun_attr *attr)
211{
212 LOG (GNUNET_ERROR_TYPE_INFO,
213 "Found STUN Attribute %s (%04x), length %d\n",
214 stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
215
216 switch (ntohs(attr->attr)) {
217 case STUN_MAPPED_ADDRESS:
218 case STUN_XOR_MAPPED_ADDRESS:
219 case STUN_MS_XOR_MAPPED_ADDRESS:
220 break;
221 default:
222 LOG (GNUNET_ERROR_TYPE_INFO,
223 "Ignoring STUN Attribute %s (%04x), length %d\n",
224 stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
225
226 }
227 return 0;
228}
229
230
231/* helper function to generate a random request id */
232static void
233generate_request_id(struct stun_header *req)
234{
235 int x;
236 req->magic = htonl(STUN_MAGIC_COOKIE);
237 for (x = 0; x < 3; x++)
238 req->id.id[x] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
239 UINT32_MAX);
240}
241
242
243/* handle an incoming STUN message.
244 *
245 * Do some basic sanity checks on packet size and content,
246 * try to extract a bit of information, and possibly reply.
247 * At the moment this only processes BIND requests, and returns
248 * the externally visible address of the request.
249 * If a callback is specified, invoke it with the attribute.
250 */
251static int
252stun_handle_packet(const uint8_t *data, size_t len, stun_cb_f *stun_cb, void *arg)
253{
254 struct stun_header *hdr = (struct stun_header *)data;
255 struct stun_attr *attr;
256 struct StunState st;
257 int ret = STUN_IGNORE;
258
259 uint32_t advertised_message_size;
260 uint32_t message_magic_cookie;
261
262
263 /* On entry, 'len' is the length of the udp payload. After the
264 * initial checks it becomes the size of unprocessed options,
265 * while 'data' is advanced accordingly.
266 */
267 if (len < sizeof(struct stun_header)) {
268 LOG (GNUNET_ERROR_TYPE_INFO,
269 "STUN packet too short (only %d, wanting at least %d)\n", (int) len, (int) sizeof(struct stun_header));
270 GNUNET_break_op (0);
271 return -1;
272 }
273 /* Skip header as it is already in hdr */
274 len -= sizeof(struct stun_header);
275 data += sizeof(struct stun_header);
276
277 /* len as advertised in the message */
278 advertised_message_size = ntohs(hdr->msglen);
279
280 message_magic_cookie = ntohl(hdr->magic);
281 /* Compare if the cookie match */
282 if(STUN_MAGIC_COOKIE != message_magic_cookie){
283 LOG (GNUNET_ERROR_TYPE_INFO,
284 "Invalid magic cookie \n");
285 GNUNET_break_op (0);
286 return -1;
287 }
288
289
290 LOG (GNUNET_ERROR_TYPE_INFO, "STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr->msgtype)), ntohs(hdr->msgtype), advertised_message_size);
291
292
293 if (advertised_message_size > len) {
294 LOG (GNUNET_ERROR_TYPE_INFO, "Scrambled STUN packet length (got %d, expecting %d)\n", advertised_message_size, (int)len);
295 GNUNET_break_op (0);
296 } else {
297 len = advertised_message_size;
298 }
299 /* Zero the struct */
300 memset(&st,0, sizeof(st));
301
302 while (len > 0) {
303 if (len < sizeof(struct stun_attr)) {
304 LOG (GNUNET_ERROR_TYPE_INFO, "Attribute too short (got %d, expecting %d)\n", (int)len, (int) sizeof(struct stun_attr));
305 GNUNET_break_op (0);
306 break;
307 }
308 attr = (struct stun_attr *)data;
309
310 /* compute total attribute length */
311 advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr);
312
313 /* Check if we still have space in our buffer */
314 if (advertised_message_size > len ) {
315 LOG (GNUNET_ERROR_TYPE_INFO, "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", advertised_message_size, (int)len);
316 GNUNET_break_op (0);
317 break;
318 }
319
320 if (stun_cb){
321 stun_cb(&st, attr, arg, hdr->magic);
322 }
323
324 if (stun_process_attr(&st, attr)) {
325 LOG (GNUNET_ERROR_TYPE_INFO, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr));
326 break;
327 }
328 /* Clear attribute id: in case previous entry was a string,
329 * this will act as the terminator for the string.
330 */
331 attr->attr = 0;
332 data += advertised_message_size;
333 len -= advertised_message_size;
334 }
335
336 return ret;
337}
338
339/* Extract the STUN_MAPPED_ADDRESS from the stun response.
340 * This is used as a callback for stun_handle_response
341 * when called from stun_request.
342 */
343static int
344stun_get_mapped(struct StunState *st, struct stun_attr *attr, void *arg, unsigned int magic)
345{
346 struct stun_addr *returned_addr = (struct stun_addr *)(attr + 1);
347 struct sockaddr_in *sa = (struct sockaddr_in *)arg;
348 unsigned short type = ntohs(attr->attr);
349
350 switch (type) {
351 case STUN_MAPPED_ADDRESS:
352 if (st->attr == STUN_XOR_MAPPED_ADDRESS ||
353 st->attr == STUN_MS_XOR_MAPPED_ADDRESS)
354 return 1;
355 magic = 0;
356 break;
357 case STUN_MS_XOR_MAPPED_ADDRESS:
358 if (st->attr == STUN_XOR_MAPPED_ADDRESS)
359 return 1;
360 break;
361 case STUN_XOR_MAPPED_ADDRESS:
362 break;
363 default:
364 return 1;
365 }
366 if (ntohs(attr->len) < 8 && returned_addr->family != 1) {
367 return 1;
368 }
369
370 st->attr = type;
371 sa->sin_port = returned_addr->port ^ htons(ntohl(magic) >> 16);
372 sa->sin_addr.s_addr = returned_addr->addr ^ magic;
373 return 0;
374}
375
376
377/**
378 * Try to establish a connection given the specified address.
379 * This function is called by the resolver once we have a DNS reply.
380 *
381 * @param cls our `struct GNUNET_CONNECTION_Handle *`
382 * @param addr address to try, NULL for "last call"
383 * @param addrlen length of @a addr
384 */
385static void
386try_connect_using_address (void *cls,
387 const struct sockaddr *addr,
388 socklen_t addrlen) {
389
390
391 struct GNUNET_NAT_StunRequestHandle *request = cls;
392
393 struct stun_header *req;
394 uint8_t reqdata[1024];
395 int reqlen;
396
397 if(NULL == request) {
398 LOG (GNUNET_ERROR_TYPE_INFO, "Empty request\n");
399 return;
400 }
401
402
403 if (NULL == addr) {
404 request->dns_active = NULL;
405 LOG (GNUNET_ERROR_TYPE_INFO, "Error resolving host %s\n", stun_server);
406 //BREAk op
407 return;
408 }
409
410
411
412 struct sockaddr_in server;
413
414
415 memset(&server,0, sizeof(server));
416 server.sin_family = AF_INET;
417
418 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
419
420 server.sin_addr = addr_in->sin_addr;
421 server.sin_port = htons(stun_port);
422
423
424 /*Craft the simplest possible STUN packet. A request binding*/
425 req = (struct stun_header *)reqdata;
426 generate_request_id(req);
427 reqlen = 0;
428 req->msgtype = 0;
429 req->msglen = 0;
430
431
432 req->msglen = htons(reqlen);
433 req->msgtype = htons(encode_message(STUN_REQUEST, STUN_BINDING));
434
435
436 if (-1 == GNUNET_NETWORK_socket_sendto (request->sock, req, ntohs(req->msglen) + sizeof(*req),
437 (const struct sockaddr *) &server, sizeof (server)))
438 {
439 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "Failt to sendto");
440 }
441
442}
443
444
445/* Generic STUN request
446 * Send a generic stun request to the server specified,
447 * possibly waiting for a reply and filling the 'reply' field with
448 * the externally visible address.
449
450 * \param s the socket used to send the request
451 * \return 0 on success, other values on error.
452 */
453struct GNUNET_NAT_StunRequestHandle *
454stun_request(struct GNUNET_NETWORK_Handle * sock)
455{
456
457
458 struct GNUNET_NAT_StunRequestHandle *rh;
459
460
461 rh = GNUNET_malloc (sizeof (struct GNUNET_NAT_StunRequestHandle));
462
463 rh->sock = sock;
464
465 rh->dns_active = GNUNET_RESOLVER_ip_get (stun_server, AF_INET,
466 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
467 &try_connect_using_address, rh);
468
469
470
471
472 return rh;
473}
474
475static void
476print_answer(struct sockaddr_in* answer)
477{
478 printf("External IP is: %s , with port %d\n", inet_ntoa(answer->sin_addr), ntohs(answer->sin_port));
479}
480
481
482/**
483 * Activity on our incoming socket. Read data from the
484 * incoming connection.
485 *
486 * @param cls
487 * @param tc scheduler context
488 */
489static void
490do_udp_read (void *cls,
491 const struct GNUNET_SCHEDULER_TaskContext *tc)
492{
493 //struct GNUNET_NAT_Test *tst = cls;
494 unsigned char reply_buf[1024];
495 ssize_t rlen;
496 struct sockaddr_in answer;
497
498
499 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
500 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
501 lsock4)))
502 {
503 rlen = GNUNET_NETWORK_socket_recv (lsock4, reply_buf, sizeof (reply_buf));
504 printf("Recivied something of size %d", rlen);
505
506 //Lets handle the packet
507 memset(&answer, 0, sizeof(struct sockaddr_in));
508 stun_handle_packet(reply_buf, rlen, stun_get_mapped, &answer);
509 //Print the anser
510 //TODO: Delete the object
511 ret = 0;
512 print_answer(&answer);
513
514
515 }
516}
517
518
519/**
520 * Create an IPv4 listen socket bound to our port.
521 *
522 * @return NULL on error
523 */
524static struct GNUNET_NETWORK_Handle *
525bind_v4 ()
526{
527 struct GNUNET_NETWORK_Handle *ls;
528 struct sockaddr_in sa4;
529 int eno;
530
531 memset (&sa4, 0, sizeof (sa4));
532 sa4.sin_family = AF_INET;
533 sa4.sin_port = htons (port);
534#if HAVE_SOCKADDR_IN_SIN_LEN
535 sa4.sin_len = sizeof (sa4);
536#endif
537 ls = GNUNET_NETWORK_socket_create (AF_INET,
538 SOCK_DGRAM,
539 0);
540 if (NULL == ls)
541 return NULL;
542 if (GNUNET_OK !=
543 GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
544 sizeof (sa4)))
545 {
546 eno = errno;
547 GNUNET_NETWORK_socket_close (ls);
548 errno = eno;
549 return NULL;
550 }
551 return ls;
552}
553
554
555
556/**
557 * Main function run with scheduler.
558 */
559
560
561static void
562run (void *cls, char *const *args, const char *cfgfile,
563 const struct GNUNET_CONFIGURATION_Handle *cfg)
564{
565
566
567 //Lets create the socket
568 lsock4 = bind_v4 ();
569 if (NULL == lsock4)
570 {
571 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
572 }
573 else
574 {
575 printf("Binded, now will call add_read\n");
576 //Lets call our function now when it accepts
577 ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
578 lsock4, &do_udp_read, NULL);
579
580 }
581 if(NULL == lsock4 )
582 {
583 GNUNET_SCHEDULER_shutdown ();
584 return;
585 }
586 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
587 "Service listens on port %u\n",
588 port);
589 printf("Start main event\n");
590 stun_request(lsock4);
591 //Main event
592 //main_task = GNUNET_SCHEDULER_add_delayed (timeout, &do_timeout, nh);
593
594}
595
596
597int
598main (int argc, char *const argv[])
599{
600 struct GNUNET_GETOPT_CommandLineOption options[] = {
601 GNUNET_GETOPT_OPTION_END
602 };
603
604 char *const argv_prog[] = {
605 "test-stun",
606 NULL
607 };
608 GNUNET_log_setup ("test-stun",
609 "WARNING",
610 NULL);
611
612 GNUNET_PROGRAM_run (1, argv_prog, "test-stun", "nohelp", options, &run, NULL);
613
614 return ret;
615}
616
617/* end of test_nat.c */