Add win32 directory to EXTRA_DIST
[sipe-libnice.git] / stun / utils.c
blobd3fb391bbca12583390fcc73a41f0e7a8c17f7b3
1 /*
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
17 * License.
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.
24 * Contributors:
25 * Youness Alaoui, Collabora Ltd.
26 * Olivier Crete, Collabora Ltd.
27 * Kai Vehmanen, Nokia
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.
40 #ifdef HAVE_CONFIG_H
41 # include <config.h>
42 #endif
44 #include <string.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <stdarg.h>
49 #include "utils.h"
51 size_t stun_padding (size_t l)
53 return (4 - (l & 3)) & 3;
56 size_t stun_align (size_t l)
58 return (l + 3) & ~3;
62 uint16_t stun_getw (const uint8_t *ptr)
64 return ((ptr)[0] << 8) | ptr[1];
68 void *stun_setw (uint8_t *ptr, uint16_t value)
70 *ptr++ = value >> 8;
71 *ptr++ = value & 0xff;
72 return ptr;
76 void stun_set_type (uint8_t *h, StunClass c, StunMethod m)
78 /* assert (c < 4); */
79 /* assert (m < (1 << 12)); */
81 h[0] = (c >> 1) | ((m >> 6) & 0x3e);
82 h[1] = ((c << 4) & 0x10) | ((m << 1) & 0xe0) | (m & 0x0f);
84 /* assert (stun_getw (h) < (1 << 14)); */
85 /* assert (stun_get_class (h) == c); */
86 /* assert (stun_get_method (h) == m); */
90 StunMessageReturn stun_xor_address (const StunMessage *msg,
91 struct sockaddr *addr, socklen_t addrlen,
92 uint32_t magic_cookie)
94 switch (addr->sa_family)
96 case AF_INET:
98 struct sockaddr_in *ip4 = (struct sockaddr_in *)addr;
99 if ((size_t) addrlen < sizeof (*ip4))
100 return STUN_MESSAGE_RETURN_INVALID;
102 ip4->sin_port ^= htons (magic_cookie >> 16);
103 ip4->sin_addr.s_addr ^= htonl (magic_cookie);
104 return STUN_MESSAGE_RETURN_SUCCESS;
107 case AF_INET6:
109 struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)addr;
110 unsigned short i;
112 if ((size_t) addrlen < sizeof (*ip6))
113 return STUN_MESSAGE_RETURN_INVALID;
115 ip6->sin6_port ^= htons (magic_cookie >> 16);
116 for (i = 0; i < 16; i++)
117 ip6->sin6_addr.s6_addr[i] ^= msg->buffer[4 + i];
118 return STUN_MESSAGE_RETURN_SUCCESS;
121 return STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS;