bd6ea209519ee314afb92ae99a3e5ce285a4c201
[platform/upstream/libnice.git] / stun / utils.c
1 /*
2  * This file is part of the Nice GLib ICE library.
3  *
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
8  *
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/
13  *
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
17  * License.
18  *
19  * The Original Code is the Nice GLib ICE library.
20  *
21  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22  * Corporation. All Rights Reserved.
23  *
24  * Contributors:
25  *   Youness Alaoui, Collabora Ltd.
26  *   Olivier Crete, Collabora Ltd.
27  *   Kai Vehmanen, Nokia
28  *
29  * Alternatively, the contents of this file may be used under the terms of the
30  * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
31  * case the provisions of LGPL are applicable instead of those above. If you
32  * wish to allow use of your version of this file only under the terms of the
33  * LGPL and not to allow others to use your version of this file under the
34  * MPL, indicate your decision by deleting the provisions above and replace
35  * them with the notice and other provisions required by the LGPL. If you do
36  * not delete the provisions above, a recipient may use your version of this
37  * file under either the MPL or the LGPL.
38  */
39
40 #ifdef HAVE_CONFIG_H
41 # include <config.h>
42 #endif
43
44 #include <string.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <stdarg.h>
48
49 #include "utils.h"
50
51 size_t stun_padding (size_t l)
52 {
53   return (4 - (l & 3)) & 3;
54 }
55
56 size_t stun_align (size_t l)
57 {
58   return (l + 3) & ~3;
59 }
60
61
62 uint16_t stun_getw (const uint8_t *ptr)
63 {
64   return ((ptr)[0] << 8) | ptr[1];
65 }
66
67
68 void *stun_setw (uint8_t *ptr, uint16_t value)
69 {
70   *ptr++ = value >> 8;
71   *ptr++ = value & 0xff;
72   return ptr;
73 }
74
75
76 void stun_set_type (uint8_t *h, StunClass c, StunMethod m)
77 {
78 /*   assert (c < 4); */
79 /*   assert (m < (1 << 12)); */
80
81   h[0] = (c >> 1) | ((m >> 6) & 0x3e);
82   h[1] = ((c << 4) & 0x10) | ((m << 1) & 0xe0) | (m & 0x0f);
83
84 /*   assert (stun_getw (h) < (1 << 14)); */
85 /*   assert (stun_get_class (h) == c); */
86 /*   assert (stun_get_method (h) == m); */
87 }
88
89
90 StunMessageReturn stun_xor_address (const StunMessage *msg,
91     struct sockaddr_storage *addr, socklen_t addrlen,
92     uint32_t magic_cookie)
93 {
94   union {
95     struct sockaddr_storage *addr;
96     struct sockaddr_in *in;
97     struct sockaddr_in6 *in6;
98   } addr_ptr;
99
100   addr_ptr.addr = addr;
101
102   switch (addr->ss_family)
103   {
104     case AF_INET:
105     {
106       struct sockaddr_in *ip4 = addr_ptr.in;
107       if ((size_t) addrlen < sizeof (*ip4))
108         return STUN_MESSAGE_RETURN_INVALID;
109
110       ip4->sin_port ^= htons (magic_cookie >> 16);
111       ip4->sin_addr.s_addr ^= htonl (magic_cookie);
112       return STUN_MESSAGE_RETURN_SUCCESS;
113     }
114
115     case AF_INET6:
116     {
117       struct sockaddr_in6 *ip6 = addr_ptr.in6;
118       unsigned short i;
119
120       if ((size_t) addrlen < sizeof (*ip6))
121         return STUN_MESSAGE_RETURN_INVALID;
122
123       ip6->sin6_port ^= htons (magic_cookie >> 16);
124       for (i = 0; i < 16; i++)
125         ip6->sin6_addr.s6_addr[i] ^= msg->buffer[4 + i];
126       return STUN_MESSAGE_RETURN_SUCCESS;
127     }
128
129     default:
130       return STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS;
131   }
132 }