aboutsummaryrefslogtreecommitdiff
path: root/src/nat/nat_api_stun.c
blob: 9332cfd987802ee7f97c1e393522c6a397904953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
     This file is part of GNUnet.
     Copyright (C) 2009, 2015, 2016 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.
    
     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * This code provides some support for doing STUN transactions.
 * We send simplest possible packet ia REQUEST with BIND to a STUN server.
 *
 * All STUN packets start with a simple header made of a type,
 * length (excluding the header) and a 16-byte random transaction id.
 * Following the header we may have zero or more attributes, each
 * structured as a type, length and a value (whose format depends
 * on the type, but often contains addresses).
 * Of course all fields are in network format.
 *
 * This code was based on ministun.c.
 *
 * @file nat/nat_api_stun.c
 * @brief Functions for STUN functionality
 * @author Bruno Souza Cabral
 */

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_resolver_service.h"
#include "gnunet_nat_service.h"


#include "nat_stun.h"

#define LOG(kind,...) GNUNET_log_from (kind, "stun", __VA_ARGS__)

#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)


/**
 * Handle to a request given to the resolver.  Can be used to cancel
 * the request prior to the timeout or successful execution.  Also
 * used to track our internal state for the request.
 */
struct GNUNET_NAT_STUN_Handle
{

  /**
   * Handle to a pending DNS lookup request.
   */
  struct GNUNET_RESOLVER_RequestHandle *dns_active;

  /**
   * Handle to the listen socket
   */
  struct GNUNET_NETWORK_Handle *sock;

  /**
   * Stun server address
   */
  char *stun_server;

  /**
   * Function to call when a error occours
   */
  GNUNET_NAT_TestCallback cb;

  /**
   * Closure for @e cb.
   */
  void *cb_cls;

  /**
   * Do we got a DNS resolution successfully?
   */
  int dns_success;

  /**
   * STUN port
   */
  uint16_t stun_port;

};


/**
 * Encode a class and method to a compatible STUN format
 *
 * @param msg_class class to be converted
 * @param method method to be converted
 * @return message in a STUN compatible format
 */
static int
encode_message (enum StunClasses msg_class,
                enum StunMethods method)
{
  return ((msg_class & 1) << 4) | ((msg_class & 2) << 7) |
    (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800) << 2);
}


/**
 * Fill the stun_header with a random request_id
 *
 * @param req, stun header to be filled
 */
static void
generate_request_id (struct stun_header *req)
{
  req->magic = htonl(STUN_MAGIC_COOKIE);
  for (unsigned int x = 0; x < 3; x++)
    req->id.id[x] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
                                              UINT32_MAX);
}


/**
 * Try to establish a connection given the specified address.
 *
 * @param cls our `struct GNUNET_NAT_STUN_Handle *`
 * @param addr address to try, NULL for "last call"
 * @param addrlen length of @a addr
 */
static void
stun_dns_callback (void *cls,
                   const struct sockaddr *addr,
                   socklen_t addrlen)
{
  struct GNUNET_NAT_STUN_Handle *rh = cls;
  struct stun_header req;
  struct sockaddr_in server;

  if (NULL == addr)
  {
    rh->dns_active = NULL;
    if (GNUNET_NO == rh->dns_success)
    {
      LOG (GNUNET_ERROR_TYPE_INFO,
           "Error resolving host %s\n",
           rh->stun_server);
      rh->cb (rh->cb_cls,
              GNUNET_NAT_ERROR_NOT_ONLINE);
    }
    else if (GNUNET_SYSERR == rh->dns_success)
    {
      rh->cb (rh->cb_cls,
	      GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR);
    }
    else
    {
      rh->cb (rh->cb_cls,
	      GNUNET_NAT_ERROR_SUCCESS);
    }
    GNUNET_NAT_stun_make_request_cancel (rh);
    return;
  }

  rh->dns_success = GNUNET_YES;
  memset (&server, 0, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_addr = ((struct sockaddr_in *)addr)->sin_addr;
  server.sin_port = htons (rh->stun_port);
#if HAVE_SOCKADDR_IN_SIN_LEN
  server.sin_len = (u_char) sizeof (struct sockaddr_in);
#endif

  /* Craft the simplest possible STUN packet. A request binding */
  generate_request_id (&req);
  req.msglen = htons (0);
  req.msgtype = htons (encode_message (STUN_REQUEST,
				       STUN_BINDING));

  /* Send the packet */
  if (-1 ==
      GNUNET_NETWORK_socket_sendto (rh->sock,
				    &req,
				    sizeof (req),
				    (const struct sockaddr *) &server,
				    sizeof (server)))
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
                         "sendto");
    rh->dns_success = GNUNET_SYSERR;
    return;
  }
}


/**
 * Make Generic STUN request. Sends a generic stun request to the
 * server specified using the specified socket.
 *
 * @param server the address of the stun server
 * @param port port of the stun server, in host byte order
 * @param sock the socket used to send the request
 * @param cb callback in case of error
 * @param cb_cls closure for @a cb
 * @return NULL on error
 */
struct GNUNET_NAT_STUN_Handle *
GNUNET_NAT_stun_make_request (const char *server,
                              uint16_t port,
                              struct GNUNET_NETWORK_Handle *sock,
                              GNUNET_NAT_TestCallback cb,
                              void *cb_cls)
{
  struct GNUNET_NAT_STUN_Handle *rh;

  rh = GNUNET_new (struct GNUNET_NAT_STUN_Handle);
  rh->sock = sock;
  rh->cb = cb;
  rh->cb_cls = cb_cls;
  rh->stun_server = GNUNET_strdup (server);
  rh->stun_port = port;
  rh->dns_success = GNUNET_NO;
  rh->dns_active = GNUNET_RESOLVER_ip_get (rh->stun_server,
                                           AF_INET,
                                           TIMEOUT,
                                           &stun_dns_callback,
					   rh);
  if (NULL == rh->dns_active)
  {
    GNUNET_NAT_stun_make_request_cancel (rh);
    return NULL;
  }
  return rh;
}


/**
 * Cancel active STUN request. Frees associated resources
 * and ensures that the callback is no longer invoked.
 *
 * @param rh request to cancel
 */
void
GNUNET_NAT_stun_make_request_cancel (struct GNUNET_NAT_STUN_Handle *rh)
{
  if (NULL != rh->dns_active)
  {
    GNUNET_RESOLVER_request_cancel (rh->dns_active);
    rh->dns_active = NULL;
  }
  GNUNET_free (rh->stun_server);
  GNUNET_free (rh);
}


/* end of nat_stun.c */