GNUnet 0.28.0-dev.2-27-gc87478450
 
Loading...
Searching...
No Matches
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
93
94
102static int
104 enum StunMethods method)
105{
106 return ((msg_class & 1) << 4) | ((msg_class & 2) << 7)
107 | (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800)
108 << 2);
109}
110
111
117static void
119{
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 (UINT32_MAX);
123}
124
125
133static void
135 const struct sockaddr *addr,
136 socklen_t addrlen)
137{
138 struct GNUNET_NAT_STUN_Handle *rh = cls;
139 struct stun_header req;
140 struct sockaddr_in server;
141
142 if (NULL == addr)
143 {
144 rh->dns_active = NULL;
145 if (GNUNET_NO == rh->dns_success)
146 {
148 "Error resolving host %s\n",
149 rh->stun_server);
150 rh->cb (rh->cb_cls,
152 }
153 else if (GNUNET_SYSERR == rh->dns_success)
154 {
155 rh->cb (rh->cb_cls,
157 }
158 else
159 {
160 rh->cb (rh->cb_cls,
162 }
164 return;
165 }
166
168 memset (&server, 0, sizeof(server));
169 server.sin_family = AF_INET;
170 server.sin_addr = ((struct sockaddr_in *) addr)->sin_addr;
171 server.sin_port = htons (rh->stun_port);
172#if HAVE_SOCKADDR_IN_SIN_LEN
173 server.sin_len = (u_char) sizeof(struct sockaddr_in);
174#endif
175
176 /* Craft the simplest possible STUN packet. A request binding */
177 generate_request_id (&req);
178 req.msglen = htons (0);
179 req.msgtype = htons (encode_message (STUN_REQUEST,
180 STUN_BINDING));
181
182 /* Send the packet */
183 if (-1 ==
185 &req,
186 sizeof(req),
187 (const struct sockaddr *) &server,
188 sizeof(server)))
189 {
191 "sendto");
193 return;
194 }
195}
196
197
211 uint16_t port,
214 void *cb_cls)
215{
216 struct GNUNET_NAT_STUN_Handle *rh;
217
219 rh->sock = sock;
220 rh->cb = cb;
221 rh->cb_cls = cb_cls;
222 rh->stun_server = GNUNET_strdup (server);
223 rh->stun_port = port;
226 AF_INET,
227 TIMEOUT,
229 rh);
230 if (NULL == rh->dns_active)
231 {
233 return NULL;
234 }
235 return rh;
236}
237
238
245void
247{
248 if (NULL != rh->dns_active)
249 {
251 rh->dns_active = NULL;
252 }
254 GNUNET_free (rh);
255}
256
257
258/* end of nat_stun.c */
static uint16_t port
Port number.
Definition gnunet-bcd.c:146
Functions related to doing DNS lookups.
uint32_t GNUNET_CRYPTO_random_u32(uint32_t i)
Produce a random value.
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
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.
void(* GNUNET_NAT_TestCallback)(void *cls, enum GNUNET_NAT_StatusCode result)
Function called to report success or failure for NAT configuration test.
void GNUNET_NAT_stun_make_request_cancel(struct GNUNET_NAT_STUN_Handle *rh)
Cancel active STUN request.
@ GNUNET_NAT_ERROR_NOT_ONLINE
detected that we are offline
@ GNUNET_NAT_ERROR_SUCCESS
Just the default.
@ GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR
Failure in network subsystem, check permissions.
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:771
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.
void GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.
static void stun_dns_callback(void *cls, const struct sockaddr *addr, socklen_t addrlen)
Try to establish a connection given the specified address.
#define TIMEOUT
#define LOG(kind,...)
static int encode_message(enum StunClasses msg_class, enum StunMethods method)
Encode a class and method to a compatible STUN format.
static void generate_request_id(struct stun_header *req)
Fill the stun_header with a random request_id.
Message types for STUN server resolution.
#define STUN_MAGIC_COOKIE
Definition nat_stun.h:34
StunMethods
Definition nat_stun.h:95
@ STUN_BINDING
Definition nat_stun.h:97
StunClasses
STUN message classes.
Definition nat_stun.h:86
@ STUN_REQUEST
Definition nat_stun.h:88
Handle to a request given to the resolver.
int dns_success
Do we got a DNS resolution successfully?
GNUNET_NAT_TestCallback cb
Function to call when a error occurs.
struct GNUNET_RESOLVER_RequestHandle * dns_active
Handle to a pending DNS lookup request.
void * cb_cls
Closure for cb.
char * stun_server
Stun server address.
uint16_t stun_port
STUN port.
struct GNUNET_NETWORK_Handle * sock
Handle to the listen socket.
handle to a socket
Definition network.c:53
struct sockaddr * addr
Address we were bound to, or NULL.
Definition network.c:74
socklen_t addrlen
Number of bytes in addr.
Definition network.c:69
Handle to a request given to the resolver.
uint16_t msglen
Definition nat_stun.h:45
uint16_t msgtype
Definition nat_stun.h:44
uint32_t magic
Definition nat_stun.h:46
stun_trans_id id
Definition nat_stun.h:47
uint32_t id[3]
Definition nat_stun.h:38