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 * Olivier Crete, Collabora Ltd.
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.
51 size_t stun_padding (size_t l
)
53 return (4 - (l
& 3)) & 3;
56 size_t stun_align (size_t l
)
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
)
71 *ptr
++ = value
& 0xff;
76 void stun_set_type (uint8_t *h
, StunClass c
, StunMethod m
)
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
)
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
;
109 struct sockaddr_in6
*ip6
= (struct sockaddr_in6
*)addr
;
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
;