136531dd7e4b879bee36b5484d04a67d50a7aedb
[platform/upstream/libnice.git] / stun / stun5389.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 Nokia Corporation. All rights reserved.
7  *
8  * The contents of this file are subject to the Mozilla Public License Version
9  * 1.1 (the "License"); you may not use this file except in compliance with
10  * the License. You may obtain a copy of the License at
11  * http://www.mozilla.org/MPL/
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15  * for the specific language governing rights and limitations under the
16  * License.
17  *
18  * The Original Code is the Nice GLib ICE library.
19  *
20  * The Initial Developers of the Original Code are Collabora Ltd and Nokia
21  * Corporation. All Rights Reserved.
22  *
23  * Contributors:
24  *   Youness Alaoui, Collabora Ltd.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
28  * case the provisions of LGPL are applicable instead of those above. If you
29  * wish to allow use of your version of this file only under the terms of the
30  * LGPL and not to allow others to use your version of this file under the
31  * MPL, indicate your decision by deleting the provisions above and replace
32  * them with the notice and other provisions required by the LGPL. If you do
33  * not delete the provisions above, a recipient may use your version of this
34  * file under either the MPL or the LGPL.
35  */
36
37 #ifdef HAVE_CONFIG_H
38 # include <config.h>
39 #endif
40
41 #ifdef _WIN32
42 #include <winsock2.h>
43 #else
44 #include <sys/socket.h>
45 #include <netinet/in.h> /* htons() */
46 #endif
47
48 #include <string.h>
49 #include <stdlib.h>
50
51 #include "stun5389.h"
52 #include "stuncrc32.h"
53 #include "stunmessage.h"
54
55
56 static const char utf8_skip_data[256] = {
57   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
58   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
59   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
60   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
61   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
62   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
63   2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
64   3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
65 };
66
67 #define next_utf8_char(p) (char *)((p) + \
68       utf8_skip_data[*(const unsigned char *)(p)])
69
70 uint32_t stun_fingerprint (const uint8_t *msg, size_t len,
71     bool wlm2009_stupid_crc32_typo)
72 {
73   crc_data data[3];
74   uint16_t fakelen = htons (len - 20u);
75
76   // assert (len >= 28u);
77
78   data[0].buf = (void *)msg;
79   data[0].len = 2;
80   data[1].buf = (uint8_t *)&fakelen;
81   data[1].len = 2;
82   data[2].buf = (void *)(msg + 4);
83   /* first 4 bytes done, last 8 bytes not summed */
84   data[2].len = len - 12u;
85
86   return htonl (stun_crc32 (data, 3, wlm2009_stupid_crc32_typo) ^ 0x5354554e);
87 }
88
89 bool stun_message_has_cookie (const StunMessage *msg)
90 {
91   StunTransactionId id;
92   uint32_t cookie = htonl (STUN_MAGIC_COOKIE);
93   stun_message_id (msg, id);
94   return memcmp (id, &cookie, sizeof (cookie)) == 0;
95 }
96
97
98 StunMessageReturn stun_message_append_software (StunMessage *msg,
99     const char *software)
100 {
101   int len = 0;
102   const char *ptr = NULL;
103
104   if (software == NULL)
105     software = PACKAGE_STRING;
106
107   ptr = software;
108   while (*ptr && len < 128) {
109     ptr = next_utf8_char (ptr);
110     len++;
111   }
112
113   return stun_message_append_bytes (msg, STUN_ATTRIBUTE_SOFTWARE, software,
114       ptr - software);
115 }