GNUnet  0.10.x
nat_api_stun.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009, 2015, 2016 GNUnet e.V.
4 
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.
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  Affero General Public License for more details.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
38 #include "platform.h"
39 #include "gnunet_util_lib.h"
41 #include "gnunet_nat_service.h"
42 
43 
44 #include "nat_stun.h"
45 
46 #define LOG(kind,...) GNUNET_log_from (kind, "stun", __VA_ARGS__)
47 
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
49 
50 
57 {
58 
63 
68 
72  char *stun_server;
73 
78 
82  void *cb_cls;
83 
88 
92  uint16_t stun_port;
93 
94 };
95 
96 
104 static int
105 encode_message (enum StunClasses msg_class,
106  enum StunMethods method)
107 {
108  return ((msg_class & 1) << 4) | ((msg_class & 2) << 7) |
109  (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800) << 2);
110 }
111 
112 
118 static void
120 {
121  req->magic = htonl(STUN_MAGIC_COOKIE);
122  for (unsigned int x = 0; x < 3; x++)
124  UINT32_MAX);
125 }
126 
127 
135 static void
136 stun_dns_callback (void *cls,
137  const struct sockaddr *addr,
138  socklen_t addrlen)
139 {
140  struct GNUNET_NAT_STUN_Handle *rh = cls;
141  struct stun_header req;
142  struct sockaddr_in server;
143 
144  if (NULL == addr)
145  {
146  rh->dns_active = NULL;
147  if (GNUNET_NO == rh->dns_success)
148  {
150  "Error resolving host %s\n",
151  rh->stun_server);
152  rh->cb (rh->cb_cls,
154  }
155  else if (GNUNET_SYSERR == rh->dns_success)
156  {
157  rh->cb (rh->cb_cls,
159  }
160  else
161  {
162  rh->cb (rh->cb_cls,
164  }
166  return;
167  }
168 
169  rh->dns_success = GNUNET_YES;
170  memset (&server, 0, sizeof(server));
171  server.sin_family = AF_INET;
172  server.sin_addr = ((struct sockaddr_in *)addr)->sin_addr;
173  server.sin_port = htons (rh->stun_port);
174 #if HAVE_SOCKADDR_IN_SIN_LEN
175  server.sin_len = (u_char) sizeof (struct sockaddr_in);
176 #endif
177 
178  /* Craft the simplest possible STUN packet. A request binding */
179  generate_request_id (&req);
180  req.msglen = htons (0);
181  req.msgtype = htons (encode_message (STUN_REQUEST,
182  STUN_BINDING));
183 
184  /* Send the packet */
185  if (-1 ==
187  &req,
188  sizeof (req),
189  (const struct sockaddr *) &server,
190  sizeof (server)))
191  {
193  "sendto");
195  return;
196  }
197 }
198 
199 
211 struct GNUNET_NAT_STUN_Handle *
212 GNUNET_NAT_stun_make_request (const char *server,
213  uint16_t port,
214  struct GNUNET_NETWORK_Handle *sock,
216  void *cb_cls)
217 {
218  struct GNUNET_NAT_STUN_Handle *rh;
219 
220  rh = GNUNET_new (struct GNUNET_NAT_STUN_Handle);
221  rh->sock = sock;
222  rh->cb = cb;
223  rh->cb_cls = cb_cls;
224  rh->stun_server = GNUNET_strdup (server);
225  rh->stun_port = port;
226  rh->dns_success = GNUNET_NO;
228  AF_INET,
229  TIMEOUT,
231  rh);
232  if (NULL == rh->dns_active)
233  {
235  return NULL;
236  }
237  return rh;
238 }
239 
240 
247 void
249 {
250  if (NULL != rh->dns_active)
251  {
253  rh->dns_active = NULL;
254  }
255  GNUNET_free (rh->stun_server);
256  GNUNET_free (rh);
257 }
258 
259 
260 /* end of nat_stun.c */
void GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.
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)
Make Generic STUN request.
Definition: nat_api_stun.c:212
detected that we are offline
void GNUNET_NAT_stun_make_request_cancel(struct GNUNET_NAT_STUN_Handle *rh)
Cancel active STUN request.
Definition: nat_api_stun.c:248
void * cb_cls
Closure for cb.
Definition: nat_api_stun.c:82
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
Handle to a request given to the resolver.
Definition: nat_api_stun.c:56
Message types for STUN server resolution.
stun_trans_id id
Definition: nat_stun.h:46
void(* GNUNET_NAT_TestCallback)(void *cls, enum GNUNET_NAT_StatusCode result)
Function called to report success or failure for NAT configuration test.
#define GNUNET_NO
Definition: gnunet_common.h:81
struct GNUNET_RESOLVER_RequestHandle * GNUNET_RESOLVER_ip_get(const char *hostname, int af, struct GNUNET_TIME_Relative timeout, GNUNET_RESOLVER_AddressCallback callback, void *callback_cls)
Convert a string to one or more IP addresses.
Definition: resolver_api.c:927
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint16_t msglen
Definition: nat_stun.h:44
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
StunMethods
Definition: nat_stun.h:92
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
#define LOG(kind,...)
Definition: nat_api_stun.c:46
ssize_t GNUNET_NETWORK_socket_sendto(const struct GNUNET_NETWORK_Handle *desc, const void *message, size_t length, const struct sockaddr *dest_addr, socklen_t dest_len)
Send data to a particular destination (always non-blocking).
Definition: network.c:968
Randomness for IVs etc.
uint16_t msgtype
Definition: nat_stun.h:43
struct GNUNET_NETWORK_Handle * sock
Handle to the listen socket.
Definition: nat_api_stun.c:67
Failure in network subsystem, check permissions.
char * stun_server
Stun server address.
Definition: nat_api_stun.c:72
struct sockaddr * addr
Address we were bound to, or NULL.
Definition: network.c:72
#define TIMEOUT
Definition: nat_api_stun.c:48
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
GNUNET_NAT_TestCallback cb
Function to call when a error occours.
Definition: nat_api_stun.c:77
Handle to a request given to the resolver.
Definition: resolver_api.c:100
static int encode_message(enum StunClasses msg_class, enum StunMethods method)
Encode a class and method to a compatible STUN format.
Definition: nat_api_stun.c:105
StunClasses
STUN message classes.
Definition: nat_stun.h:84
uint16_t stun_port
STUN port.
Definition: nat_api_stun.c:92
int dns_success
Do we got a DNS resolution successfully?
Definition: nat_api_stun.c:87
static uint16_t port
Port number.
Definition: gnunet-bcd.c:79
socklen_t addrlen
Number of bytes in addr.
Definition: network.c:67
#define STUN_MAGIC_COOKIE
Definition: nat_stun.h:34
static void stun_dns_callback(void *cls, const struct sockaddr *addr, socklen_t addrlen)
Try to establish a connection given the specified address.
Definition: nat_api_stun.c:136
handle to a socket
Definition: network.c:46
uint32_t magic
Definition: nat_stun.h:45
#define GNUNET_YES
Definition: gnunet_common.h:80
uint32_t id[3]
Definition: nat_stun.h:37
static void generate_request_id(struct stun_header *req)
Fill the stun_header with a random request_id.
Definition: nat_api_stun.c:119
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_RESOLVER_RequestHandle * dns_active
Handle to a pending DNS lookup request.
Definition: nat_api_stun.c:62