Fix crashes and memory leaks.
[oweals/gnunet.git] / src / nat / nat_stun.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2015 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * 
23  * This code provides some support for doing STUN transactions.
24  * We send simplest possible packet ia REQUEST with BIND to a STUN server.
25  *
26  * All STUN packets start with a simple header made of a type,
27  * length (excluding the header) and a 16-byte random transaction id.
28  * Following the header we may have zero or more attributes, each
29  * structured as a type, length and a value (whose format depends
30  * on the type, but often contains addresses).
31  * Of course all fields are in network format.
32  * 
33  * This code was based on ministun.c.
34  *
35  *
36  * @file nat/nat_stun.c
37  * @brief Functions for STUN functionality
38  * @author Bruno Souza Cabral
39  */
40
41 #include "platform.h"
42 #include "gnunet_util_lib.h"
43 #include "gnunet_resolver_service.h"
44 #include "gnunet_nat_lib.h"
45
46
47 #include "nat_stun.h"
48
49 #define LOG(kind,...) GNUNET_log_from (kind, "stun", __VA_ARGS__)
50
51 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
52
53
54 /**
55  * Handle to a request given to the resolver.  Can be used to cancel
56  * the request prior to the timeout or successful execution.  Also
57  * used to track our internal state for the request.
58  */
59 struct GNUNET_NAT_STUN_Handle {
60
61     /**
62     * Handle to a pending DNS lookup request.
63     */
64     struct GNUNET_RESOLVER_RequestHandle *dns_active;
65
66
67     /**
68     * Handle to the listen socket
69     */
70     struct GNUNET_NETWORK_Handle * sock;
71
72     /**
73     * Stun server address
74     */
75     char *stun_server ;
76
77     /**
78     * STUN port
79     */
80     int stun_port;
81
82     /**
83      * Function to call when a error occours
84     */
85     GNUNET_NAT_stun_RequestCallback cb;
86
87     /**
88      * Closure for @e cb.
89      */
90     void *cb_cls;
91
92     /**
93      * Do we got a DNS resolution successfully ?
94      */
95     int dns_success;
96
97
98 };
99
100
101
102 /* here we store credentials extracted from a message */
103 struct StunState {
104     uint16_t attr;
105 };
106
107
108 /**
109  * Convert a message to a StunClass
110  *
111  * @param msg the received message
112  * @return the converted StunClass
113  */
114 static int
115 decode_class(int msg)
116 {
117     /* Sorry for the magic, but this maps the class according to rfc5245 */
118     return ((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7);
119 }
120
121 /**
122  * Convert a message to a StunMethod
123  *
124  * @param msg the received message
125  * @return the converted StunMethod
126  */
127 static int
128 decode_method(int msg)
129 {
130     return (msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2);
131 }
132
133 /**
134  * Encode a class and method to a compatible STUN format
135  *
136  * @param msg_class class to be converted
137  * @param method method to be converted
138  * @return message in a STUN compatible format
139  */
140 static int
141 encode_message(StunClasses msg_class, StunMethods method)
142 {
143     return ((msg_class & 1) << 4) | ((msg_class & 2) << 7) |
144            (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800) << 2);
145 }
146
147 /**
148  * Print a class and method from a STUN message
149  *
150  * @param msg
151  * @return string with the message class and method
152  */
153 static const char *
154 stun_msg2str(int msg)
155 {
156
157     const struct { enum StunClasses value; const char *name; } classes[] = {
158             { STUN_REQUEST, "Request" },
159             { STUN_INDICATION, "Indication" },
160             { STUN_RESPONSE, "Response" },
161             { STUN_ERROR_RESPONSE, "Error Response" },
162             { 0, NULL }
163     };
164
165     const struct { enum StunMethods value; const char *name; } methods[] = {
166             { STUN_BINDING, "Binding" },
167             { 0, NULL }
168     };
169
170     static char result[32];
171     const char *msg_class = NULL;
172     const char *method = NULL;
173     int i;
174     int value;
175
176     value = decode_class(msg);
177     for (i = 0; classes[i].name; i++) {
178         msg_class = classes[i].name;
179         if (classes[i].value == value)
180             break;
181     }
182     value = decode_method(msg);
183     for (i = 0; methods[i].name; i++) {
184         method = methods[i].name;
185         if (methods[i].value == value)
186             break;
187     }
188     GNUNET_snprintf(result, sizeof(result), "%s %s",
189              method ? : "Unknown Method",
190              msg_class ? : "Unknown Class Message");
191     return result;
192 }
193
194 /**
195  * Print attribute name
196  *
197  * @param msg with a attribute type
198  * @return string with the attribute name
199  */
200 static const char *
201 stun_attr2str(int msg)
202 {
203     const struct { enum StunAttributes value; const char *name; } attrs[] = {
204             { STUN_MAPPED_ADDRESS, "Mapped Address" },
205             { STUN_RESPONSE_ADDRESS, "Response Address" },
206             { STUN_CHANGE_ADDRESS, "Change Address" },
207             { STUN_SOURCE_ADDRESS, "Source Address" },
208             { STUN_CHANGED_ADDRESS, "Changed Address" },
209             { STUN_USERNAME, "Username" },
210             { STUN_PASSWORD, "Password" },
211             { STUN_MESSAGE_INTEGRITY, "Message Integrity" },
212             { STUN_ERROR_CODE, "Error Code" },
213             { STUN_UNKNOWN_ATTRIBUTES, "Unknown Attributes" },
214             { STUN_REFLECTED_FROM, "Reflected From" },
215             { STUN_REALM, "Realm" },
216             { STUN_NONCE, "Nonce" },
217             { STUN_XOR_MAPPED_ADDRESS, "XOR Mapped Address" },
218             { STUN_MS_VERSION, "MS Version" },
219             { STUN_MS_XOR_MAPPED_ADDRESS, "MS XOR Mapped Address" },
220             { STUN_SOFTWARE, "Software" },
221             { STUN_ALTERNATE_SERVER, "Alternate Server" },
222             { STUN_FINGERPRINT, "Fingerprint" },
223             { 0, NULL }
224     };
225     int i;
226
227     for (i = 0; attrs[i].name; i++) {
228         if (attrs[i].value == msg)
229             return attrs[i].name;
230     }
231     return "Unknown Attribute";
232 }
233
234
235 /**
236  * Fill the stun_header with a random request_id
237  *
238  * @param state, STUN attribute type
239  * @param attr , the actual attribute
240  *
241  * @param req, stun header to be filled
242  */
243 static int
244 stun_process_attr(struct StunState *state, struct stun_attr *attr)
245 {
246     LOG (GNUNET_ERROR_TYPE_INFO,
247          "Found STUN Attribute %s (%04x), length %d\n",
248          stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
249
250     switch (ntohs(attr->attr)) {
251         case STUN_MAPPED_ADDRESS:
252         case STUN_XOR_MAPPED_ADDRESS:
253         case STUN_MS_XOR_MAPPED_ADDRESS:
254             break;
255         default:
256             LOG (GNUNET_ERROR_TYPE_INFO,
257                  "Ignoring STUN Attribute %s (%04x), length %d\n",
258                  stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
259
260     }
261     return 0;
262 }
263
264
265
266 /**
267  * Fill the stun_header with a random request_id
268  *
269  * @param req, stun header to be filled
270  */
271 static void
272 generate_request_id(struct stun_header *req)
273 {
274     unsigned int x;
275     req->magic = htonl(STUN_MAGIC_COOKIE);
276     for (x = 0; x < 3; x++)
277         req->id.id[x] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
278                                                   UINT32_MAX);
279 }
280
281
282 /**
283  * Extract the STUN_MAPPED_ADDRESS from the stun response.
284  * This is used as a callback for stun_handle_response
285  * when called from stun_request.
286  *
287  * @param st, pointer where we will set the type
288  * @param attr , received stun attribute
289  * @param arg , pointer to a sockaddr_in where we will set the reported IP and port
290  * @param magic , Magic cookie
291  *
292  * @return 0 on success, other value otherwise
293  */
294 static int
295 stun_get_mapped(struct StunState *st, struct stun_attr *attr,struct sockaddr_in *arg, unsigned int magic)
296 {
297     struct stun_addr *returned_addr = (struct stun_addr *)(attr + 1);
298     struct sockaddr_in *sa = (struct sockaddr_in *)arg;
299     unsigned short type = ntohs(attr->attr);
300
301     switch (type) {
302         case STUN_MAPPED_ADDRESS:
303             if (st->attr == STUN_XOR_MAPPED_ADDRESS ||
304                 st->attr == STUN_MS_XOR_MAPPED_ADDRESS)
305                 return 1;
306             magic = 0;
307             break;
308         case STUN_MS_XOR_MAPPED_ADDRESS:
309             if (st->attr == STUN_XOR_MAPPED_ADDRESS)
310                 return 1;
311             break;
312         case STUN_XOR_MAPPED_ADDRESS:
313             break;
314         default:
315             return 1;
316     }
317     if (ntohs(attr->len) < 8 && returned_addr->family != 1) {
318         return 1;
319     }
320
321     st->attr = type;
322
323     sa->sin_family = AF_INET;
324     sa->sin_port = returned_addr->port ^ htons(ntohl(magic) >> 16);
325     sa->sin_addr.s_addr = returned_addr->addr ^ magic;
326     return 0;
327 }
328
329
330 /**
331  * Handle an incoming STUN message, Do some basic sanity checks on packet size and content,
332  * try to extract a bit of information, and possibly reply.
333  * At the moment this only processes BIND requests, and returns
334  * the externally visible address of the request.
335  * If a callback is specified, invoke it with the attribute.
336  *
337  * @param data, the packet
338  * @param len, the length of the packet
339  * @param arg, sockaddr_in where we will set our discovered packet
340  *
341  * @return, #GNUNET_OK on OK, #GNUNET_NO if the packet is invalid ( not a stun packet)
342  */
343 int
344 GNUNET_NAT_stun_handle_packet(const void *data, size_t len, struct sockaddr_in *arg)
345 {
346     const struct stun_header *hdr = (const struct stun_header *)data;
347     struct stun_attr *attr;
348     struct StunState st;
349     int ret = GNUNET_OK;
350
351     uint32_t advertised_message_size;
352     uint32_t message_magic_cookie;
353
354
355     /* On entry, 'len' is the length of the udp payload. After the
356          * initial checks it becomes the size of unprocessed options,
357          * while 'data' is advanced accordingly.
358          */
359     if (len < sizeof(struct stun_header)) {
360         LOG (GNUNET_ERROR_TYPE_INFO,
361              "STUN packet too short (only %d, wanting at least %d)\n", (int) len, (int) sizeof(struct stun_header));
362         GNUNET_break_op (0);
363         return GNUNET_NO;
364     }
365     /* Skip header as it is already in hdr */
366     len -= sizeof(struct stun_header);
367     data += sizeof(struct stun_header);
368
369     /* len as advertised in the message */
370     advertised_message_size = ntohs(hdr->msglen);
371
372     message_magic_cookie = ntohl(hdr->magic);
373     /* Compare if the cookie match */
374     if(STUN_MAGIC_COOKIE != message_magic_cookie){
375         LOG (GNUNET_ERROR_TYPE_INFO,
376              "Invalid magic cookie \n");
377         return GNUNET_NO;
378     }
379
380
381     LOG (GNUNET_ERROR_TYPE_INFO, "STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr->msgtype)),
382          ntohs(hdr->msgtype),
383          advertised_message_size);
384
385
386     if (advertised_message_size > len) {
387         LOG (GNUNET_ERROR_TYPE_INFO, "Scrambled STUN packet length (got %d, expecting %d)\n", advertised_message_size,
388              (int)len);
389         return GNUNET_NO;
390     } else {
391         len = advertised_message_size;
392     }
393
394     memset(&st,0, sizeof(st));
395
396     while (len > 0) {
397         if (len < sizeof(struct stun_attr)) {
398             LOG (GNUNET_ERROR_TYPE_INFO, "Attribute too short (got %d, expecting %d)\n", (int)len,
399                  (int) sizeof(struct stun_attr));
400             break;
401         }
402         attr = (struct stun_attr *)data;
403
404         /* compute total attribute length */
405         advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr);
406
407         /* Check if we still have space in our buffer */
408         if (advertised_message_size > len ) {
409             LOG (GNUNET_ERROR_TYPE_INFO, "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", advertised_message_size,
410                  (int)len);
411             break;
412         }
413
414
415         stun_get_mapped(&st, attr, arg, hdr->magic);
416
417         if (stun_process_attr(&st, attr)) {
418             LOG (GNUNET_ERROR_TYPE_INFO, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr->attr)),
419                  ntohs(attr->attr));
420             break;
421         }
422         /** Clear attribute id: in case previous entry was a string,
423          * this will act as the terminator for the string.
424          **/
425         attr->attr = 0;
426         data += advertised_message_size;
427         len -= advertised_message_size;
428         ret = GNUNET_OK;
429     }
430
431     return ret;
432 }
433
434
435
436 /**
437  * Clean-up used memory
438  *
439  * @param cls our `struct GNUNET_NAT_STUN_Handle *`
440  */
441 static void
442 clean(struct GNUNET_NAT_STUN_Handle * handle)
443 {
444     GNUNET_free(handle->stun_server);
445     GNUNET_free(handle);
446
447 }
448
449
450
451 /**
452  * Try to establish a connection given the specified address.
453  *
454  * @param cls our `struct GNUNET_NAT_STUN_Handle *`
455  * @param addr address to try, NULL for "last call"
456  * @param addrlen length of @a addr
457  */
458 static void
459 stun_dns_callback (void *cls,
460                    const struct sockaddr *addr,
461                    socklen_t addrlen) {
462
463
464     struct GNUNET_NAT_STUN_Handle *request = cls;
465
466     struct stun_header *req;
467     uint8_t reqdata[1024];
468     int reqlen;
469     struct sockaddr_in server;
470
471
472     if(NULL == request) {
473
474         if( GNUNET_NO == request->dns_success){
475             LOG (GNUNET_ERROR_TYPE_INFO, "Empty request\n");
476             request->cb(request->cb_cls, GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR);
477             clean(request);
478
479         }
480         return;
481     }
482
483     if (NULL == addr) {
484         request->dns_active = NULL;
485
486         if( GNUNET_NO == request->dns_success){
487             LOG (GNUNET_ERROR_TYPE_INFO, "Error resolving host %s\n", request->stun_server);
488             request->cb(request->cb_cls, GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR);
489             clean(request);
490
491         }
492
493         return;
494     }
495
496
497     request->dns_success= GNUNET_YES;
498     memset(&server,0, sizeof(server));
499     server.sin_family = AF_INET;
500     server.sin_addr = ((struct sockaddr_in *)addr)->sin_addr;
501     server.sin_port = htons(request->stun_port);
502
503
504     /*Craft the simplest possible STUN packet. A request binding*/
505     req = (struct stun_header *)reqdata;
506     generate_request_id(req);
507     reqlen = 0;
508     req->msgtype = 0;
509     req->msglen = 0;
510     req->msglen = htons(reqlen);
511     req->msgtype = htons(encode_message(STUN_REQUEST, STUN_BINDING));
512
513     /* Send the packet */
514     if (-1 == GNUNET_NETWORK_socket_sendto (request->sock, req, ntohs(req->msglen) + sizeof(*req),
515                                             (const struct sockaddr *) &server, sizeof (server)))
516     {
517         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "Fail to sendto");
518         request->cb(request->cb_cls, GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR);
519         clean(request);
520         return;
521     }
522
523
524 }
525
526
527
528
529
530 /**
531  * Make Generic STUN request and
532  * Send a generic stun request to the server specified using the specified socket.
533  * possibly waiting for a reply and filling the 'reply' field with
534  * the externally visible address.
535  *
536  * @param server, the address of the stun server
537  * @param port, port of the stun server
538  * @param sock the socket used to send the request
539  * @return #GNUNET_OK success, #GNUNET_NO on error.
540  */
541 int
542 GNUNET_NAT_stun_make_request(char * server, int port,
543                              struct GNUNET_NETWORK_Handle * sock,GNUNET_NAT_stun_RequestCallback cb,
544                              void *cb_cls)
545 {
546
547     struct GNUNET_NAT_STUN_Handle *rh;
548
549     rh = GNUNET_malloc (sizeof (struct GNUNET_NAT_STUN_Handle));
550     rh->sock = sock;
551
552     char * server_copy = GNUNET_strdup (server);
553
554     rh->cb = cb;
555     rh->cb_cls = cb_cls;
556     rh->stun_server = server_copy;
557     rh->stun_port = port;
558     rh->dns_success = GNUNET_NO;
559
560     rh->dns_active = GNUNET_RESOLVER_ip_get (server_copy, AF_INET,
561                                              TIMEOUT,
562                                              &stun_dns_callback, rh);
563
564     if(rh->dns_active == NULL)
565     {
566         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "Failed DNS");
567         GNUNET_free(rh);
568         return GNUNET_NO;
569     }
570
571
572     return GNUNET_OK;
573 }