2 This file is part of GNUnet.
3 Copyright (C) 2009, 2015, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * This code provides some support for doing STUN transactions.
22 * We send simplest possible packet ia REQUEST with BIND to a STUN server.
24 * All STUN packets start with a simple header made of a type,
25 * length (excluding the header) and a 16-byte random transaction id.
26 * Following the header we may have zero or more attributes, each
27 * structured as a type, length and a value (whose format depends
28 * on the type, but often contains addresses).
29 * Of course all fields are in network format.
31 * This code was based on ministun.c.
33 * @file nat/nat_api_stun.c
34 * @brief Functions for STUN functionality
35 * @author Bruno Souza Cabral
39 #include "gnunet_util_lib.h"
40 #include "gnunet_resolver_service.h"
41 #include "gnunet_nat_service.h"
46 #define LOG(kind, ...) GNUNET_log_from (kind, "stun", __VA_ARGS__)
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
52 * Handle to a request given to the resolver. Can be used to cancel
53 * the request prior to the timeout or successful execution. Also
54 * used to track our internal state for the request.
56 struct GNUNET_NAT_STUN_Handle
59 * Handle to a pending DNS lookup request.
61 struct GNUNET_RESOLVER_RequestHandle *dns_active;
64 * Handle to the listen socket
66 struct GNUNET_NETWORK_Handle *sock;
74 * Function to call when a error occours
76 GNUNET_NAT_TestCallback cb;
84 * Do we got a DNS resolution successfully?
96 * Encode a class and method to a compatible STUN format
98 * @param msg_class class to be converted
99 * @param method method to be converted
100 * @return message in a STUN compatible format
103 encode_message (enum StunClasses msg_class,
104 enum StunMethods method)
106 return ((msg_class & 1) << 4) | ((msg_class & 2) << 7)
107 | (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800)
113 * Fill the stun_header with a random request_id
115 * @param req, stun header to be filled
118 generate_request_id (struct stun_header *req)
120 req->magic = htonl (STUN_MAGIC_COOKIE);
121 for (unsigned int x = 0; x < 3; x++)
122 req->id.id[x] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
128 * Try to establish a connection given the specified address.
130 * @param cls our `struct GNUNET_NAT_STUN_Handle *`
131 * @param addr address to try, NULL for "last call"
132 * @param addrlen length of @a addr
135 stun_dns_callback (void *cls,
136 const struct sockaddr *addr,
139 struct GNUNET_NAT_STUN_Handle *rh = cls;
140 struct stun_header req;
141 struct sockaddr_in server;
145 rh->dns_active = NULL;
146 if (GNUNET_NO == rh->dns_success)
148 LOG (GNUNET_ERROR_TYPE_INFO,
149 "Error resolving host %s\n",
152 GNUNET_NAT_ERROR_NOT_ONLINE);
154 else if (GNUNET_SYSERR == rh->dns_success)
157 GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR);
162 GNUNET_NAT_ERROR_SUCCESS);
164 GNUNET_NAT_stun_make_request_cancel (rh);
168 rh->dns_success = GNUNET_YES;
169 memset (&server, 0, sizeof(server));
170 server.sin_family = AF_INET;
171 server.sin_addr = ((struct sockaddr_in *) addr)->sin_addr;
172 server.sin_port = htons (rh->stun_port);
173 #if HAVE_SOCKADDR_IN_SIN_LEN
174 server.sin_len = (u_char) sizeof(struct sockaddr_in);
177 /* Craft the simplest possible STUN packet. A request binding */
178 generate_request_id (&req);
179 req.msglen = htons (0);
180 req.msgtype = htons (encode_message (STUN_REQUEST,
183 /* Send the packet */
185 GNUNET_NETWORK_socket_sendto (rh->sock,
188 (const struct sockaddr *) &server,
191 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
193 rh->dns_success = GNUNET_SYSERR;
200 * Make Generic STUN request. Sends a generic stun request to the
201 * server specified using the specified socket.
203 * @param server the address of the stun server
204 * @param port port of the stun server, in host byte order
205 * @param sock the socket used to send the request
206 * @param cb callback in case of error
207 * @param cb_cls closure for @a cb
208 * @return NULL on error
210 struct GNUNET_NAT_STUN_Handle *
211 GNUNET_NAT_stun_make_request (const char *server,
213 struct GNUNET_NETWORK_Handle *sock,
214 GNUNET_NAT_TestCallback cb,
217 struct GNUNET_NAT_STUN_Handle *rh;
219 rh = GNUNET_new (struct GNUNET_NAT_STUN_Handle);
223 rh->stun_server = GNUNET_strdup (server);
224 rh->stun_port = port;
225 rh->dns_success = GNUNET_NO;
226 rh->dns_active = GNUNET_RESOLVER_ip_get (rh->stun_server,
231 if (NULL == rh->dns_active)
233 GNUNET_NAT_stun_make_request_cancel (rh);
241 * Cancel active STUN request. Frees associated resources
242 * and ensures that the callback is no longer invoked.
244 * @param rh request to cancel
247 GNUNET_NAT_stun_make_request_cancel (struct GNUNET_NAT_STUN_Handle *rh)
249 if (NULL != rh->dns_active)
251 GNUNET_RESOLVER_request_cancel (rh->dns_active);
252 rh->dns_active = NULL;
254 GNUNET_free (rh->stun_server);
259 /* end of nat_stun.c */