2 * This file is part of the Nice GLib ICE library.
4 * (C) 2008-2009 Collabora Ltd.
5 * Contact: Youness Alaoui
6 * (C) 2007-2009 Nokia Corporation. All rights reserved.
7 * Contact: Rémi Denis-Courmont
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
25 * Youness Alaoui, Collabora Ltd.
26 * Rémi Denis-Courmont, Nokia
28 * Alternatively, the contents of this file may be used under the terms of the
29 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
30 * case the provisions of LGPL are applicable instead of those above. If you
31 * wish to allow use of your version of this file only under the terms of the
32 * LGPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replace
34 * them with the notice and other provisions required by the LGPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the LGPL.
46 #include "win32_common.h"
47 #define close closesocket
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
67 #include "stun/stunagent.h"
78 # define SOL_IP IPPROTO_IP
82 # define SOL_IPV6 IPPROTO_IPV6
86 /** Non-blocking mode STUN binding discovery */
88 size_t stun_usage_bind_create (StunAgent *agent, StunMessage *msg,
89 uint8_t *buffer, size_t buffer_len)
91 stun_agent_init_request (agent, msg, buffer, buffer_len, STUN_BINDING);
93 return stun_agent_finish_message (agent, msg, NULL, 0);
96 StunUsageBindReturn stun_usage_bind_process (StunMessage *msg,
97 struct sockaddr *addr, socklen_t *addrlen,
98 struct sockaddr *alternate_server, socklen_t *alternate_server_len)
101 StunMessageReturn val;
104 struct sockaddr_storage *sas;
109 if (stun_message_get_method (msg) != STUN_BINDING)
110 return STUN_USAGE_BIND_RETURN_INVALID;
112 switch (stun_message_get_class (msg))
115 case STUN_INDICATION:
116 return STUN_USAGE_BIND_RETURN_INVALID;
122 if (stun_message_find_error (msg, &code) != STUN_MESSAGE_RETURN_SUCCESS) {
123 /* missing ERROR-CODE: ignore message */
124 return STUN_USAGE_BIND_RETURN_INVALID;
127 /* NOTE: currently we ignore unauthenticated messages if the context
128 * is authenticated, for security reasons. */
129 stun_debug (" STUN error message received (code: %d)", code);
131 /* ALTERNATE-SERVER mechanism */
132 if ((code / 100) == 3) {
135 struct sockaddr_storage *sas;
138 alternate_sa.sa = alternate_server;
139 if (alternate_server && alternate_server_len) {
140 if (stun_message_find_addr (msg, STUN_ATTRIBUTE_ALTERNATE_SERVER,
142 alternate_server_len) != STUN_MESSAGE_RETURN_SUCCESS) {
143 stun_debug (" Unexpectedly missing ALTERNATE-SERVER attribute");
144 return STUN_USAGE_BIND_RETURN_ERROR;
147 if (!stun_message_has_attribute (msg, STUN_ATTRIBUTE_ALTERNATE_SERVER)) {
148 stun_debug (" Unexpectedly missing ALTERNATE-SERVER attribute");
149 return STUN_USAGE_BIND_RETURN_ERROR;
153 stun_debug ("Found alternate server");
154 return STUN_USAGE_BIND_RETURN_ALTERNATE_SERVER;
157 return STUN_USAGE_BIND_RETURN_ERROR;
164 stun_debug ("Received %u-bytes STUN message", stun_message_length (msg));
166 val = stun_message_find_xor_addr (msg,
167 STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, sa.sas,
169 if (val != STUN_MESSAGE_RETURN_SUCCESS)
171 stun_debug (" No XOR-MAPPED-ADDRESS: %d", val);
172 val = stun_message_find_addr (msg,
173 STUN_ATTRIBUTE_MAPPED_ADDRESS, sa.sas,
175 if (val != STUN_MESSAGE_RETURN_SUCCESS)
177 stun_debug (" No MAPPED-ADDRESS: %d", val);
178 return STUN_USAGE_BIND_RETURN_ERROR;
182 stun_debug (" Mapped address found!");
183 return STUN_USAGE_BIND_RETURN_SUCCESS;
188 /** Binding keep-alive (Binding discovery indication!) */
191 stun_usage_bind_keepalive (StunAgent *agent, StunMessage *msg,
192 uint8_t *buf, size_t len)
195 stun_agent_init_indication (agent, msg,
196 buf, len, STUN_BINDING);
197 return stun_agent_finish_message (agent, msg, NULL, 0);
202 typedef struct stun_trans_s
208 struct sockaddr_storage dst;
213 STUN_USAGE_TRANS_RETURN_SUCCESS,
214 STUN_USAGE_TRANS_RETURN_ERROR,
215 STUN_USAGE_TRANS_RETURN_RETRY,
216 STUN_USAGE_TRANS_RETURN_INVALID_ADDRESS,
217 STUN_USAGE_TRANS_RETURN_UNSUPPORTED,
218 } StunUsageTransReturn;
223 static StunUsageTransReturn
224 stun_trans_init (StunTransport *tr, int fd,
225 const struct sockaddr *srv, socklen_t srvlen)
229 if ((size_t) srvlen > sizeof (tr->dst))
230 return STUN_USAGE_TRANS_RETURN_INVALID_ADDRESS;
236 memcpy (&tr->dst, srv, srvlen);
238 return STUN_USAGE_TRANS_RETURN_SUCCESS;
243 * Creates and connects a socket. This is useful when a socket is to be used
244 * for multiple consecutive transactions (e.g. TURN).
246 static int stun_socket (int family, int type, int proto)
249 unsigned long set_nonblock=1;
252 int fd = socket (family, type, proto);
257 fcntl (fd, F_SETFD, fcntl (fd, F_GETFD) | FD_CLOEXEC);
260 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL) | O_NONBLOCK);
262 ioctlsocket(fd, FIONBIO, &set_nonblock);
266 if (type == SOCK_DGRAM)
268 /* Linux specifics for ICMP errors on non-connected sockets */
273 setsockopt (fd, SOL_IP, IP_RECVERR, &yes, sizeof (yes));
276 setsockopt (fd, SOL_IPV6, IPV6_RECVERR, &yes, sizeof (yes));
289 static StunUsageTransReturn
290 stun_trans_create (StunTransport *tr, int type, int proto,
291 const struct sockaddr *srv, socklen_t srvlen)
293 StunUsageTransReturn val = STUN_USAGE_TRANS_RETURN_ERROR;
296 if ((size_t) srvlen < sizeof(*srv))
297 return STUN_USAGE_TRANS_RETURN_INVALID_ADDRESS;
299 fd = stun_socket (srv->sa_family, type, proto);
301 return STUN_USAGE_TRANS_RETURN_ERROR;
303 if (type != SOCK_DGRAM) {
304 if (connect (fd, srv, srvlen) &&
306 (WSAGetLastError () != WSAEINPROGRESS)) {
308 (errno != EINPROGRESS)) {
312 val = stun_trans_init (tr, fd, NULL, 0);
314 val = stun_trans_init (tr, fd, srv, srvlen);
321 return STUN_USAGE_TRANS_RETURN_SUCCESS;
329 static void stun_trans_deinit (StunTransport *tr)
333 assert (tr->fd != -1);
335 if (tr->own_fd != -1)
346 # define MSG_DONTWAIT 0
349 # define MSG_NOSIGNAL 0
353 static int stun_err_dequeue (int fd)
357 int saved_errno = errno, ret;
359 memset (&hdr, 0, sizeof (hdr));
360 ret = (recvmsg (fd, &hdr, MSG_ERRQUEUE) >= 0);
371 stun_trans_sendto (StunTransport *tr, const uint8_t *buf, size_t len,
372 const struct sockaddr *dst, socklen_t dstlen)
374 static const int flags = MSG_DONTWAIT | MSG_NOSIGNAL;
380 val = sendto (tr->fd, (void *)buf, len, flags, dst, dstlen);
382 val = send (tr->fd, (void *)buf, len, flags);
384 while ((val == -1) && stun_err_dequeue (tr->fd));
391 stun_trans_recvfrom (StunTransport *tr, uint8_t *buf, size_t maxlen,
392 struct sockaddr_storage * dst,
395 static const int flags = MSG_DONTWAIT | MSG_NOSIGNAL;
399 val = recvfrom (tr->fd, (void *)buf, maxlen, flags, (struct sockaddr *) dst,
402 val = recv (tr->fd, (void *)buf, maxlen, flags);
405 stun_err_dequeue (tr->fd);
412 stun_trans_send (StunTransport *tr, const uint8_t *buf, size_t len)
414 struct sockaddr *conv;
416 conv = (struct sockaddr *) &tr->dst;
418 return stun_trans_sendto (tr, buf, len, conv, tr->dstlen);
422 stun_trans_recv (StunTransport *tr, uint8_t *buf, size_t maxlen)
424 return stun_trans_recvfrom (tr, buf, maxlen, NULL, NULL);
429 static int stun_trans_fd (const StunTransport *tr)
438 * Waits for a response or timeout to occur.
440 * @return ETIMEDOUT if the transaction has timed out, or 0 if an incoming
441 * message needs to be processed.
443 static StunUsageTransReturn
444 stun_trans_poll (StunTransport *tr, unsigned int delay)
449 memset (&ufd, 0, sizeof (ufd));
450 ufd.fd = stun_trans_fd (tr);
452 ufd.events |= POLLIN;
454 if (poll (&ufd, 1, delay) <= 0) {
455 return STUN_USAGE_TRANS_RETURN_RETRY;
458 return STUN_USAGE_TRANS_RETURN_SUCCESS;
461 return STUN_USAGE_TRANS_RETURN_UNSUPPORTED;
467 /** Blocking mode STUN binding discovery */
468 StunUsageBindReturn stun_usage_bind_run (const struct sockaddr *srv,
469 socklen_t srvlen, struct sockaddr_storage *addr, socklen_t *addrlen)
475 uint8_t req_buf[STUN_MAX_MESSAGE_SIZE];
477 uint8_t buf[STUN_MAX_MESSAGE_SIZE];
478 StunValidationStatus valid;
480 StunUsageTransReturn ret;
482 struct sockaddr_storage alternate_server = { AF_UNSPEC } ;
483 socklen_t alternate_server_len = sizeof (alternate_server);
484 StunUsageBindReturn bind_ret;
488 stun_agent_init (&agent, STUN_ALL_KNOWN_ATTRIBUTES,
489 STUN_COMPATIBILITY_RFC3489, 0);
491 len = stun_usage_bind_create (&agent, &req, req_buf, sizeof(req_buf));
493 ret = stun_trans_create (&trans, SOCK_DGRAM, 0, srv, srvlen);
494 if (ret != STUN_USAGE_TRANS_RETURN_SUCCESS) {
495 stun_debug ("STUN transaction failed: couldn't create transport.");
496 bind_ret = STUN_USAGE_BIND_RETURN_ERROR;
500 val = stun_trans_send (&trans, req_buf, len);
502 stun_debug ("STUN transaction failed: couldn't send request.");
503 bind_ret = STUN_USAGE_BIND_RETURN_ERROR;
507 stun_timer_start (&timer, STUN_TIMER_DEFAULT_TIMEOUT,
508 STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
509 stun_debug ("STUN transaction started (timeout %dms).",
510 stun_timer_remainder (&timer));
515 unsigned delay = stun_timer_remainder (&timer);
516 ret = stun_trans_poll (&trans, delay);
517 if (ret == STUN_USAGE_TRANS_RETURN_RETRY) {
518 switch (stun_timer_refresh (&timer)) {
519 case STUN_USAGE_TIMER_RETURN_TIMEOUT:
520 stun_debug ("STUN transaction failed: time out.");
521 bind_ret = STUN_USAGE_BIND_RETURN_TIMEOUT; // fatal error!
523 case STUN_USAGE_TIMER_RETURN_RETRANSMIT:
524 stun_debug ("STUN transaction retransmitted (timeout %dms).",
525 stun_timer_remainder (&timer));
526 val = stun_trans_send (&trans, req_buf, len);
528 stun_debug ("STUN transaction failed: couldn't resend request.");
529 bind_ret = STUN_USAGE_BIND_RETURN_ERROR;
533 case STUN_USAGE_TIMER_RETURN_SUCCESS:
539 val = stun_trans_recv (&trans, buf, sizeof (buf));
545 valid = stun_agent_validate (&agent, &msg, buf, val, NULL, NULL);
546 if (valid == STUN_VALIDATION_UNKNOWN_ATTRIBUTE)
548 bind_ret = STUN_USAGE_BIND_RETURN_ERROR;
552 if (valid != STUN_VALIDATION_SUCCESS) {
553 ret = STUN_USAGE_TRANS_RETURN_RETRY;
555 bind_ret = stun_usage_bind_process (&msg, (struct sockaddr *) addr,
556 addrlen, (struct sockaddr *) &alternate_server, &alternate_server_len);
557 if (bind_ret == STUN_USAGE_BIND_RETURN_ALTERNATE_SERVER) {
558 stun_trans_deinit (&trans);
560 assert (alternate_server.ss_family != AF_UNSPEC);
562 ret = stun_trans_create (&trans, SOCK_DGRAM, 0,
563 (struct sockaddr *) &alternate_server, alternate_server_len);
565 if (ret != STUN_USAGE_TRANS_RETURN_SUCCESS) {
566 bind_ret = STUN_USAGE_BIND_RETURN_ERROR;
570 val = stun_trans_send (&trans, req_buf, len);
573 bind_ret = STUN_USAGE_BIND_RETURN_ERROR;
577 stun_timer_start (&timer, STUN_TIMER_DEFAULT_TIMEOUT,
578 STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
579 ret = STUN_USAGE_TRANS_RETURN_RETRY;
580 } else if (bind_ret == STUN_USAGE_BIND_RETURN_INVALID) {
581 ret = STUN_USAGE_TRANS_RETURN_RETRY;
587 while (ret == STUN_USAGE_TRANS_RETURN_RETRY);
591 stun_trans_deinit (&trans);