media/mtransport/third_party/nICEr/src/stun/stun_hint.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 Copyright (c) 2007, Adobe Systems, Incorporated
michael@0 3 All rights reserved.
michael@0 4
michael@0 5 Redistribution and use in source and binary forms, with or without
michael@0 6 modification, are permitted provided that the following conditions are
michael@0 7 met:
michael@0 8
michael@0 9 * Redistributions of source code must retain the above copyright
michael@0 10 notice, this list of conditions and the following disclaimer.
michael@0 11
michael@0 12 * Redistributions in binary form must reproduce the above copyright
michael@0 13 notice, this list of conditions and the following disclaimer in the
michael@0 14 documentation and/or other materials provided with the distribution.
michael@0 15
michael@0 16 * Neither the name of Adobe Systems, Network Resonance nor the names of its
michael@0 17 contributors may be used to endorse or promote products derived from
michael@0 18 this software without specific prior written permission.
michael@0 19
michael@0 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31 */
michael@0 32
michael@0 33
michael@0 34 static char *RCSSTRING __UNUSED__="$Id: stun_hint.c,v 1.2 2008/04/28 18:21:30 ekr Exp $";
michael@0 35
michael@0 36
michael@0 37 #include <errno.h>
michael@0 38 #include <csi_platform.h>
michael@0 39
michael@0 40 #ifdef WIN32
michael@0 41 #include <winsock2.h>
michael@0 42 #include <stdlib.h>
michael@0 43 #include <io.h>
michael@0 44 #include <time.h>
michael@0 45 #else /* UNIX */
michael@0 46 #include <string.h>
michael@0 47 #endif /* end UNIX */
michael@0 48 #include <assert.h>
michael@0 49
michael@0 50 #include "stun.h"
michael@0 51
michael@0 52
michael@0 53 /* returns 0 if it's not a STUN message
michael@0 54 * 1 if it's likely to be a STUN message
michael@0 55 * 2 if it's super likely to be a STUN message
michael@0 56 * 3 if it really is a STUN message */
michael@0 57 int
michael@0 58 nr_is_stun_message(UCHAR *buf, int len)
michael@0 59 {
michael@0 60 const UINT4 cookie = htonl(NR_STUN_MAGIC_COOKIE);
michael@0 61 const UINT4 cookie2 = htonl(NR_STUN_MAGIC_COOKIE2);
michael@0 62 #if 0
michael@0 63 nr_stun_message msg;
michael@0 64 #endif
michael@0 65 UINT2 type;
michael@0 66 nr_stun_encoded_attribute* attr;
michael@0 67 unsigned int attrLen;
michael@0 68 int atrType;
michael@0 69
michael@0 70 if (sizeof(nr_stun_message_header) > len)
michael@0 71 return 0;
michael@0 72
michael@0 73 if ((buf[0] & (0x80|0x40)) != 0)
michael@0 74 return 0;
michael@0 75
michael@0 76 memcpy(&type, buf, 2);
michael@0 77 type = ntohs(type);
michael@0 78
michael@0 79 switch (type) {
michael@0 80 case NR_STUN_MSG_BINDING_REQUEST:
michael@0 81 case NR_STUN_MSG_BINDING_INDICATION:
michael@0 82 case NR_STUN_MSG_BINDING_RESPONSE:
michael@0 83 case NR_STUN_MSG_BINDING_ERROR_RESPONSE:
michael@0 84
michael@0 85 #ifdef USE_TURN
michael@0 86 case NR_STUN_MSG_ALLOCATE_REQUEST:
michael@0 87 case NR_STUN_MSG_ALLOCATE_RESPONSE:
michael@0 88 case NR_STUN_MSG_ALLOCATE_ERROR_RESPONSE:
michael@0 89 case NR_STUN_MSG_REFRESH_REQUEST:
michael@0 90 case NR_STUN_MSG_REFRESH_RESPONSE:
michael@0 91 case NR_STUN_MSG_REFRESH_ERROR_RESPONSE:
michael@0 92 case NR_STUN_MSG_PERMISSION_REQUEST:
michael@0 93 case NR_STUN_MSG_PERMISSION_RESPONSE:
michael@0 94 case NR_STUN_MSG_PERMISSION_ERROR_RESPONSE:
michael@0 95 case NR_STUN_MSG_CHANNEL_BIND_REQUEST:
michael@0 96 case NR_STUN_MSG_CHANNEL_BIND_RESPONSE:
michael@0 97 case NR_STUN_MSG_CHANNEL_BIND_ERROR_RESPONSE:
michael@0 98 case NR_STUN_MSG_SEND_INDICATION:
michael@0 99 case NR_STUN_MSG_DATA_INDICATION:
michael@0 100 #ifdef NR_STUN_MSG_CONNECT_REQUEST
michael@0 101 case NR_STUN_MSG_CONNECT_REQUEST:
michael@0 102 #endif
michael@0 103 #ifdef NR_STUN_MSG_CONNECT_RESPONSE
michael@0 104 case NR_STUN_MSG_CONNECT_RESPONSE:
michael@0 105 #endif
michael@0 106 #ifdef NR_STUN_MSG_CONNECT_ERROR_RESPONSE
michael@0 107 case NR_STUN_MSG_CONNECT_ERROR_RESPONSE:
michael@0 108 #endif
michael@0 109 #ifdef NR_STUN_MSG_CONNECT_STATUS_INDICATION
michael@0 110 case NR_STUN_MSG_CONNECT_STATUS_INDICATION:
michael@0 111 #endif
michael@0 112 #endif /* USE_TURN */
michael@0 113
michael@0 114 /* ok so far, continue */
michael@0 115 break;
michael@0 116 default:
michael@0 117 return 0;
michael@0 118 break;
michael@0 119 }
michael@0 120
michael@0 121 if (!memcmp(&cookie2, &buf[4], sizeof(UINT4))) {
michael@0 122 /* return here because if it's an old-style message then there will
michael@0 123 * not be a fingerprint in the message */
michael@0 124 return 1;
michael@0 125 }
michael@0 126
michael@0 127 if (memcmp(&cookie, &buf[4], sizeof(UINT4)))
michael@0 128 return 0;
michael@0 129
michael@0 130 /* the magic cookie was right, so it's pretty darn likely that what we've
michael@0 131 * got here is a STUN message */
michael@0 132
michael@0 133 attr = (nr_stun_encoded_attribute*)(buf + (len - 8));
michael@0 134 attrLen = ntohs(attr->length);
michael@0 135 atrType = ntohs(attr->type);
michael@0 136
michael@0 137 if (atrType != NR_STUN_ATTR_FINGERPRINT || attrLen != 4)
michael@0 138 return 1;
michael@0 139
michael@0 140 /* the fingerprint is in the right place and looks sane, so we can be quite
michael@0 141 * sure we've got a STUN message */
michael@0 142
michael@0 143 #if 0
michael@0 144 /* nevermind this check ... there's a reasonable chance that a NAT has modified
michael@0 145 * the message (and thus the fingerprint check will fail), but it's still an
michael@0 146 * otherwise-perfectly-good STUN message, so skip the check since we're going
michael@0 147 * to return "true" whether the check succeeds or fails */
michael@0 148
michael@0 149 if (nr_stun_parse_attr_UINT4(buf + (len - 4), attrLen, &msg.fingerprint))
michael@0 150 return 2;
michael@0 151
michael@0 152
michael@0 153 if (nr_stun_compute_fingerprint(buf, len - 8, &computedFingerprint))
michael@0 154 return 2;
michael@0 155
michael@0 156 if (msg.fingerprint.number != computedFingerprint)
michael@0 157 return 2;
michael@0 158
michael@0 159 /* and the fingerprint is good, so it's gotta be a STUN message */
michael@0 160 #endif
michael@0 161
michael@0 162 return 3;
michael@0 163 }
michael@0 164
michael@0 165 int
michael@0 166 nr_is_stun_request_message(UCHAR *buf, int len)
michael@0 167 {
michael@0 168 UINT2 type;
michael@0 169
michael@0 170 if (sizeof(nr_stun_message_header) > len)
michael@0 171 return 0;
michael@0 172
michael@0 173 if (!nr_is_stun_message(buf, len))
michael@0 174 return 0;
michael@0 175
michael@0 176 memcpy(&type, buf, 2);
michael@0 177 type = ntohs(type);
michael@0 178
michael@0 179 return NR_STUN_GET_TYPE_CLASS(type) == NR_CLASS_REQUEST;
michael@0 180 }
michael@0 181
michael@0 182 int
michael@0 183 nr_is_stun_indication_message(UCHAR *buf, int len)
michael@0 184 {
michael@0 185 UINT2 type;
michael@0 186
michael@0 187 if (sizeof(nr_stun_message_header) > len)
michael@0 188 return 0;
michael@0 189
michael@0 190 if (!nr_is_stun_message(buf, len))
michael@0 191 return 0;
michael@0 192
michael@0 193 memcpy(&type, buf, 2);
michael@0 194 type = ntohs(type);
michael@0 195
michael@0 196 return NR_STUN_GET_TYPE_CLASS(type) == NR_CLASS_INDICATION;
michael@0 197 }
michael@0 198
michael@0 199 int
michael@0 200 nr_is_stun_response_message(UCHAR *buf, int len)
michael@0 201 {
michael@0 202 UINT2 type;
michael@0 203
michael@0 204 if (sizeof(nr_stun_message_header) > len)
michael@0 205 return 0;
michael@0 206
michael@0 207 if (!nr_is_stun_message(buf, len))
michael@0 208 return 0;
michael@0 209
michael@0 210 memcpy(&type, buf, 2);
michael@0 211 type = ntohs(type);
michael@0 212
michael@0 213 return NR_STUN_GET_TYPE_CLASS(type) == NR_CLASS_RESPONSE
michael@0 214 || NR_STUN_GET_TYPE_CLASS(type) == NR_CLASS_ERROR_RESPONSE;
michael@0 215 }
michael@0 216
michael@0 217 int
michael@0 218 nr_has_stun_cookie(UCHAR *buf, int len)
michael@0 219 {
michael@0 220 static UINT4 cookie;
michael@0 221
michael@0 222 cookie = htonl(NR_STUN_MAGIC_COOKIE);
michael@0 223
michael@0 224 if (sizeof(nr_stun_message_header) > len)
michael@0 225 return 0;
michael@0 226
michael@0 227 if (memcmp(&cookie, &buf[4], sizeof(UINT4)))
michael@0 228 return 0;
michael@0 229
michael@0 230 return 1;
michael@0 231 }
michael@0 232
michael@0 233 int
michael@0 234 nr_stun_message_length(UCHAR *buf, int buf_len, int *msg_len)
michael@0 235 {
michael@0 236 nr_stun_message_header *hdr;
michael@0 237
michael@0 238 if (!nr_is_stun_message(buf, buf_len))
michael@0 239 return(R_BAD_DATA);
michael@0 240
michael@0 241 hdr = (nr_stun_message_header *)buf;
michael@0 242
michael@0 243 *msg_len = ntohs(hdr->length);
michael@0 244
michael@0 245 return(0);
michael@0 246 }
michael@0 247
michael@0 248
michael@0 249

mercurial