Changeset 1877 for pjproject/trunk/pjnath/src/pjnath/stun_msg.c
- Timestamp:
- Mar 19, 2008 11:00:30 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/src/pjnath/stun_msg.c
r1852 r1877 21 21 #include <pjlib-util/crc32.h> 22 22 #include <pjlib-util/hmac_sha1.h> 23 #include <pjlib-util/md5.h>24 23 #include <pj/assert.h> 25 24 #include <pj/log.h> … … 91 90 pj_status_t (*encode_attr)(const void *a, pj_uint8_t *buf, 92 91 unsigned len, unsigned *printed); 92 void* (*clone_attr)(pj_pool_t *pool, const void *src); 93 93 }; 94 94 … … 102 102 unsigned len, 103 103 unsigned *printed); 104 static void* clone_sockaddr_attr(pj_pool_t *pool, const void *src); 104 105 static pj_status_t decode_string_attr(pj_pool_t *pool, 105 106 const pj_uint8_t *buf, … … 107 108 static pj_status_t encode_string_attr(const void *a, pj_uint8_t *buf, 108 109 unsigned len, unsigned *printed); 110 static void* clone_string_attr(pj_pool_t *pool, const void *src); 109 111 static pj_status_t decode_msgint_attr(pj_pool_t *pool, 110 112 const pj_uint8_t *buf, … … 112 114 static pj_status_t encode_msgint_attr(const void *a, pj_uint8_t *buf, 113 115 unsigned len, unsigned *printed); 116 static void* clone_msgint_attr(pj_pool_t *pool, const void *src); 114 117 static pj_status_t decode_errcode_attr(pj_pool_t *pool, 115 118 const pj_uint8_t *buf, … … 117 120 static pj_status_t encode_errcode_attr(const void *a, pj_uint8_t *buf, 118 121 unsigned len, unsigned *printed); 122 static void* clone_errcode_attr(pj_pool_t *pool, const void *src); 119 123 static pj_status_t decode_unknown_attr(pj_pool_t *pool, 120 124 const pj_uint8_t *buf, … … 122 126 static pj_status_t encode_unknown_attr(const void *a, pj_uint8_t *buf, 123 127 unsigned len, unsigned *printed); 128 static void* clone_unknown_attr(pj_pool_t *pool, const void *src); 124 129 static pj_status_t decode_uint_attr(pj_pool_t *pool, 125 130 const pj_uint8_t *buf, … … 127 132 static pj_status_t encode_uint_attr(const void *a, pj_uint8_t *buf, 128 133 unsigned len, unsigned *printed); 134 static void* clone_uint_attr(pj_pool_t *pool, const void *src); 129 135 static pj_status_t decode_uint64_attr(pj_pool_t *pool, 130 136 const pj_uint8_t *buf, … … 132 138 static pj_status_t encode_uint64_attr(const void *a, pj_uint8_t *buf, 133 139 unsigned len, unsigned *printed); 140 static void* clone_uint64_attr(pj_pool_t *pool, const void *src); 134 141 static pj_status_t decode_binary_attr(pj_pool_t *pool, 135 142 const pj_uint8_t *buf, … … 137 144 static pj_status_t encode_binary_attr(const void *a, pj_uint8_t *buf, 138 145 unsigned len, unsigned *printed); 146 static void* clone_binary_attr(pj_pool_t *pool, const void *src); 139 147 static pj_status_t decode_empty_attr(pj_pool_t *pool, 140 148 const pj_uint8_t *buf, … … 142 150 static pj_status_t encode_empty_attr(const void *a, pj_uint8_t *buf, 143 151 unsigned len, unsigned *printed); 144 152 static void* clone_empty_attr(pj_pool_t *pool, const void *src); 145 153 146 154 static struct attr_desc mandatory_attr_desc[] = … … 148 156 { 149 157 /* type zero */ 158 NULL, 150 159 NULL, 151 160 NULL, … … 156 165 "MAPPED-ADDRESS", 157 166 &decode_sockaddr_attr, 158 &encode_sockaddr_attr 167 &encode_sockaddr_attr, 168 &clone_sockaddr_attr 159 169 }, 160 170 { … … 162 172 "RESPONSE-ADDRESS", 163 173 &decode_sockaddr_attr, 164 &encode_sockaddr_attr 174 &encode_sockaddr_attr, 175 &clone_sockaddr_attr 165 176 }, 166 177 { … … 168 179 "CHANGE-REQUEST", 169 180 &decode_uint_attr, 170 &encode_uint_attr 181 &encode_uint_attr, 182 &clone_uint_attr 171 183 }, 172 184 { … … 174 186 "SOURCE-ADDRESS", 175 187 &decode_sockaddr_attr, 176 &encode_sockaddr_attr 188 &encode_sockaddr_attr, 189 &clone_sockaddr_attr 177 190 }, 178 191 { … … 180 193 "CHANGED-ADDRESS", 181 194 &decode_sockaddr_attr, 182 &encode_sockaddr_attr 195 &encode_sockaddr_attr, 196 &clone_sockaddr_attr 183 197 }, 184 198 { … … 186 200 "USERNAME", 187 201 &decode_string_attr, 188 &encode_string_attr 202 &encode_string_attr, 203 &clone_string_attr 189 204 }, 190 205 { … … 192 207 "PASSWORD", 193 208 &decode_string_attr, 194 &encode_string_attr 209 &encode_string_attr, 210 &clone_string_attr 195 211 }, 196 212 { … … 198 214 "MESSAGE-INTEGRITY", 199 215 &decode_msgint_attr, 200 &encode_msgint_attr 216 &encode_msgint_attr, 217 &clone_msgint_attr 201 218 }, 202 219 { … … 204 221 "ERROR-CODE", 205 222 &decode_errcode_attr, 206 &encode_errcode_attr 223 &encode_errcode_attr, 224 &clone_errcode_attr 207 225 }, 208 226 { … … 210 228 "UNKNOWN-ATTRIBUTES", 211 229 &decode_unknown_attr, 212 &encode_unknown_attr 230 &encode_unknown_attr, 231 &clone_unknown_attr 213 232 }, 214 233 { … … 216 235 "REFLECTED-FROM", 217 236 &decode_sockaddr_attr, 218 &encode_sockaddr_attr 237 &encode_sockaddr_attr, 238 &clone_sockaddr_attr 219 239 }, 220 240 { … … 222 242 "CHANNEL-NUMBER", 223 243 &decode_uint_attr, 224 &encode_uint_attr 244 &encode_uint_attr, 245 &clone_uint_attr 225 246 }, 226 247 { … … 228 249 "LIFETIME", 229 250 &decode_uint_attr, 230 &encode_uint_attr 251 &encode_uint_attr, 252 &clone_uint_attr 231 253 }, 232 254 { 233 255 /* ID 0x000E is not assigned */ 256 NULL, 234 257 NULL, 235 258 NULL, … … 240 263 "MAGIC-COOKIE", 241 264 &decode_uint_attr, 242 &encode_uint_attr 265 &encode_uint_attr, 266 &clone_uint_attr 243 267 }, 244 268 { … … 246 270 "BANDWIDTH", 247 271 &decode_uint_attr, 248 &encode_uint_attr 272 &encode_uint_attr, 273 &clone_uint_attr 249 274 }, 250 275 { 251 276 /* ID 0x0011 is not assigned */ 277 NULL, 252 278 NULL, 253 279 NULL, … … 258 284 "PEER-ADDRESS", 259 285 &decode_xored_sockaddr_attr, 260 &encode_sockaddr_attr 286 &encode_sockaddr_attr, 287 &clone_sockaddr_attr 261 288 }, 262 289 { … … 264 291 "DATA", 265 292 &decode_binary_attr, 266 &encode_binary_attr 293 &encode_binary_attr, 294 &clone_binary_attr 267 295 }, 268 296 { … … 270 298 "REALM", 271 299 &decode_string_attr, 272 &encode_string_attr 300 &encode_string_attr, 301 &clone_string_attr 273 302 }, 274 303 { … … 276 305 "NONCE", 277 306 &decode_string_attr, 278 &encode_string_attr 307 &encode_string_attr, 308 &clone_string_attr 279 309 }, 280 310 { … … 282 312 "RELAY-ADDRESS", 283 313 &decode_xored_sockaddr_attr, 284 &encode_sockaddr_attr 314 &encode_sockaddr_attr, 315 &clone_sockaddr_attr 285 316 }, 286 317 { … … 288 319 "REQUESTED-ADDRESS-TYPE", 289 320 &decode_uint_attr, 290 &encode_uint_attr 321 &encode_uint_attr, 322 &clone_uint_attr 291 323 }, 292 324 { … … 294 326 "REQUESTED-PROPS", 295 327 &decode_uint_attr, 296 &encode_uint_attr 328 &encode_uint_attr, 329 &clone_uint_attr 297 330 }, 298 331 { … … 300 333 "REQUESTED-TRANSPORT", 301 334 &decode_uint_attr, 302 &encode_uint_attr 335 &encode_uint_attr, 336 &clone_uint_attr 303 337 }, 304 338 { … … 306 340 NULL, 307 341 NULL, 342 NULL, 308 343 NULL 309 344 }, … … 312 347 NULL, 313 348 NULL, 349 NULL, 314 350 NULL 315 351 }, … … 318 354 NULL, 319 355 NULL, 356 NULL, 320 357 NULL 321 358 }, … … 324 361 NULL, 325 362 NULL, 363 NULL, 326 364 NULL 327 365 }, … … 330 368 NULL, 331 369 NULL, 370 NULL, 332 371 NULL 333 372 }, 334 373 { 335 374 /* ID 0x001F is not assigned */ 375 NULL, 336 376 NULL, 337 377 NULL, … … 342 382 "XOR-MAPPED-ADDRESS", 343 383 &decode_xored_sockaddr_attr, 344 &encode_sockaddr_attr 384 &encode_sockaddr_attr, 385 &clone_sockaddr_attr 345 386 }, 346 387 { … … 348 389 "TIMER-VAL", 349 390 &decode_uint_attr, 350 &encode_uint_attr 391 &encode_uint_attr, 392 &clone_uint_attr 351 393 }, 352 394 { … … 354 396 "RESERVATION-TOKEN", 355 397 &decode_uint64_attr, 356 &encode_uint64_attr 398 &encode_uint64_attr, 399 &clone_uint64_attr 357 400 }, 358 401 { … … 360 403 "XOR-REFLECTED-FROM", 361 404 &decode_xored_sockaddr_attr, 362 &encode_sockaddr_attr 405 &encode_sockaddr_attr, 406 &clone_sockaddr_attr 363 407 }, 364 408 { … … 366 410 "PRIORITY", 367 411 &decode_uint_attr, 368 &encode_uint_attr 412 &encode_uint_attr, 413 &clone_uint_attr 369 414 }, 370 415 { … … 372 417 "USE-CANDIDATE", 373 418 &decode_empty_attr, 374 &encode_empty_attr 419 &encode_empty_attr, 420 &clone_empty_attr 375 421 }, 376 422 { … … 378 424 "XOR-INTERNAL-ADDRESS", 379 425 &decode_xored_sockaddr_attr, 380 &encode_sockaddr_attr 426 &encode_sockaddr_attr, 427 &clone_sockaddr_attr 381 428 }, 382 429 … … 386 433 NULL, 387 434 NULL, 435 NULL, 388 436 NULL 389 437 } … … 394 442 { 395 443 /* ID 0x8021 is not assigned */ 444 NULL, 396 445 NULL, 397 446 NULL, … … 402 451 "SERVER", 403 452 &decode_string_attr, 404 &encode_string_attr 453 &encode_string_attr, 454 &clone_string_attr 405 455 }, 406 456 { … … 408 458 "ALTERNATE-SERVER", 409 459 &decode_sockaddr_attr, 410 &encode_sockaddr_attr 460 &encode_sockaddr_attr, 461 &clone_sockaddr_attr 411 462 }, 412 463 { … … 414 465 "REFRESH-INTERVAL", 415 466 &decode_uint_attr, 416 &encode_uint_attr 467 &encode_uint_attr, 468 &clone_uint_attr 417 469 }, 418 470 { … … 420 472 NULL, 421 473 NULL, 474 NULL, 422 475 NULL 423 476 }, … … 426 479 NULL, 427 480 NULL, 481 NULL, 428 482 NULL 429 483 }, 430 484 { 431 485 /* CACHE-TIMEOUT, 0x8027 */ 486 NULL, 432 487 NULL, 433 488 NULL, … … 438 493 "FINGERPRINT", 439 494 &decode_uint_attr, 440 &encode_uint_attr 495 &encode_uint_attr, 496 &clone_uint_attr 441 497 }, 442 498 { … … 444 500 "ICE-CONTROLLED", 445 501 &decode_uint64_attr, 446 &encode_uint64_attr 502 &encode_uint64_attr, 503 &clone_uint64_attr 447 504 }, 448 505 { … … 450 507 "ICE-CONTROLLING", 451 508 &decode_uint64_attr, 452 &encode_uint64_attr 509 &encode_uint64_attr, 510 &clone_uint64_attr 453 511 } 454 512 }; … … 827 885 828 886 887 static void* clone_sockaddr_attr(pj_pool_t *pool, const void *src) 888 { 889 pj_stun_sockaddr_attr *dst = PJ_POOL_ALLOC_T(pool, pj_stun_sockaddr_attr); 890 pj_memcpy(dst, src, sizeof(pj_stun_sockaddr_attr)); 891 return (void*)dst; 892 } 893 829 894 ////////////////////////////////////////////////////////////////////////////// 830 895 /* … … 935 1000 936 1001 1002 static void* clone_string_attr(pj_pool_t *pool, const void *src) 1003 { 1004 const pj_stun_string_attr *asrc = (const pj_stun_string_attr*)src; 1005 pj_stun_string_attr *dst = PJ_POOL_ALLOC_T(pool, pj_stun_string_attr); 1006 1007 pj_memcpy(dst, src, sizeof(pj_stun_attr_hdr)); 1008 pj_strdup(pool, &dst->value, &asrc->value); 1009 1010 return (void*)dst; 1011 } 1012 937 1013 ////////////////////////////////////////////////////////////////////////////// 938 1014 /* … … 1018 1094 } 1019 1095 1096 1097 static void* clone_empty_attr(pj_pool_t *pool, const void *src) 1098 { 1099 pj_stun_empty_attr *dst = PJ_POOL_ALLOC_T(pool, pj_stun_empty_attr); 1100 1101 pj_memcpy(dst, src, sizeof(pj_stun_empty_attr)); 1102 1103 return (void*) dst; 1104 } 1020 1105 1021 1106 ////////////////////////////////////////////////////////////////////////////// … … 1104 1189 } 1105 1190 1191 1192 static void* clone_uint_attr(pj_pool_t *pool, const void *src) 1193 { 1194 pj_stun_uint_attr *dst = PJ_POOL_ALLOC_T(pool, pj_stun_uint_attr); 1195 1196 pj_memcpy(dst, src, sizeof(pj_stun_uint_attr)); 1197 1198 return (void*)dst; 1199 } 1200 1106 1201 ////////////////////////////////////////////////////////////////////////////// 1107 1202 … … 1190 1285 1191 1286 1287 static void* clone_uint64_attr(pj_pool_t *pool, const void *src) 1288 { 1289 pj_stun_uint64_attr *dst = PJ_POOL_ALLOC_T(pool, pj_stun_uint64_attr); 1290 1291 pj_memcpy(dst, src, sizeof(pj_stun_uint64_attr)); 1292 1293 return (void*)dst; 1294 } 1295 1296 1192 1297 ////////////////////////////////////////////////////////////////////////////// 1193 1298 /* … … 1270 1375 1271 1376 return PJ_SUCCESS; 1377 } 1378 1379 1380 static void* clone_msgint_attr(pj_pool_t *pool, const void *src) 1381 { 1382 pj_stun_msgint_attr *dst = PJ_POOL_ALLOC_T(pool, pj_stun_msgint_attr); 1383 1384 pj_memcpy(dst, src, sizeof(pj_stun_msgint_attr)); 1385 1386 return (void*) dst; 1272 1387 } 1273 1388 … … 1382 1497 } 1383 1498 1499 1500 static void* clone_errcode_attr(pj_pool_t *pool, const void *src) 1501 { 1502 const pj_stun_errcode_attr *asrc = (const pj_stun_errcode_attr*)src; 1503 pj_stun_errcode_attr *dst = PJ_POOL_ALLOC_T(pool, pj_stun_errcode_attr); 1504 1505 pj_memcpy(dst, src, sizeof(pj_stun_errcode_attr)); 1506 pj_strdup(pool, &dst->reason, &asrc->reason); 1507 1508 return (void*)dst; 1509 } 1510 1384 1511 ////////////////////////////////////////////////////////////////////////////// 1385 1512 /* … … 1500 1627 1501 1628 1629 static void* clone_unknown_attr(pj_pool_t *pool, const void *src) 1630 { 1631 pj_stun_unknown_attr *dst = PJ_POOL_ALLOC_T(pool, pj_stun_unknown_attr); 1632 1633 pj_memcpy(dst, src, sizeof(pj_stun_unknown_attr)); 1634 1635 return (void*)dst; 1636 } 1637 1502 1638 ////////////////////////////////////////////////////////////////////////////// 1503 1639 /* … … 1597 1733 } 1598 1734 1735 1736 static void* clone_binary_attr(pj_pool_t *pool, const void *src) 1737 { 1738 const pj_stun_binary_attr *asrc = (const pj_stun_binary_attr*)src; 1739 pj_stun_binary_attr *dst = PJ_POOL_ALLOC_T(pool, pj_stun_binary_attr); 1740 1741 pj_memcpy(dst, src, sizeof(pj_stun_binary_attr)); 1742 1743 if (asrc->length) { 1744 dst->data = (pj_uint8_t*) pj_pool_alloc(pool, asrc->length); 1745 pj_memcpy(dst->data, asrc->data, asrc->length); 1746 } 1747 1748 return (void*)dst; 1749 } 1599 1750 1600 1751 ////////////////////////////////////////////////////////////////////////////// … … 1638 1789 1639 1790 return PJ_SUCCESS; 1791 } 1792 1793 1794 /* 1795 * Clone a STUN message with all of its attributes. 1796 */ 1797 PJ_DEF(pj_stun_msg*) pj_stun_msg_clone( pj_pool_t *pool, 1798 const pj_stun_msg *src) 1799 { 1800 pj_stun_msg *dst; 1801 unsigned i; 1802 1803 PJ_ASSERT_RETURN(pool && src, NULL); 1804 1805 dst = PJ_POOL_ZALLOC_T(pool, pj_stun_msg); 1806 pj_memcpy(dst, src, sizeof(pj_stun_msg)); 1807 1808 /* Duplicate the attributes */ 1809 for (i=0, dst->attr_count=0; i<src->attr_count; ++i) { 1810 dst->attr[dst->attr_count] = pj_stun_attr_clone(pool, src->attr[i]); 1811 if (dst->attr[dst->attr_count]) 1812 ++dst->attr_count; 1813 } 1814 1815 return dst; 1640 1816 } 1641 1817 … … 1982 2158 } 1983 2159 1984 /* Calculate HMAC-SHA1 key for long term credential, by getting1985 * MD5 digest of username, realm, and password.1986 */1987 static void calc_md5_key(pj_uint8_t digest[16],1988 const pj_str_t *realm,1989 const pj_str_t *username,1990 const pj_str_t *passwd)1991 {1992 /* The 16-byte key for MESSAGE-INTEGRITY HMAC is formed by taking1993 * the MD5 hash of the result of concatenating the following five1994 * fields: (1) The username, with any quotes and trailing nulls1995 * removed, (2) A single colon, (3) The realm, with any quotes and1996 * trailing nulls removed, (4) A single colon, and (5) The1997 * password, with any trailing nulls removed.1998 */1999 pj_md5_context ctx;2000 pj_str_t s;2001 2002 pj_md5_init(&ctx);2003 2004 #define REMOVE_QUOTE(s) if (s.slen && *s.ptr=='"') \2005 s.ptr++, s.slen--; \2006 if (s.slen && s.ptr[s.slen-1]=='"') \2007 s.slen--;2008 2009 /* Add username */2010 s = *username;2011 REMOVE_QUOTE(s);2012 pj_md5_update(&ctx, (pj_uint8_t*)s.ptr, s.slen);2013 2014 /* Add single colon */2015 pj_md5_update(&ctx, (pj_uint8_t*)":", 1);2016 2017 /* Add realm */2018 s = *realm;2019 REMOVE_QUOTE(s);2020 pj_md5_update(&ctx, (pj_uint8_t*)s.ptr, s.slen);2021 2022 #undef REMOVE_QUOTE2023 2024 /* Another colon */2025 pj_md5_update(&ctx, (pj_uint8_t*)":", 1);2026 2027 /* Add password */2028 pj_md5_update(&ctx, (pj_uint8_t*)passwd->ptr, passwd->slen);2029 2030 /* Done */2031 pj_md5_final(&ctx, digest);2032 }2033 2034 2035 /*2036 * Create authentication key to be used for encoding the message with2037 * MESSAGE-INTEGRITY.2038 */2039 PJ_DEF(void) pj_stun_create_key(pj_pool_t *pool,2040 pj_str_t *key,2041 const pj_str_t *realm,2042 const pj_str_t *username,2043 const pj_str_t *passwd)2044 {2045 PJ_ASSERT_ON_FAIL(pool && key && username && passwd, return);2046 2047 if (realm && realm->slen) {2048 key->ptr = (char*) pj_pool_alloc(pool, 16);2049 calc_md5_key((pj_uint8_t*)key->ptr, realm, username, passwd);2050 key->slen = 16;2051 } else {2052 pj_strdup(pool, key, passwd);2053 }2054 }2055 2056 2057 2160 /* 2058 2161 static char *print_binary(const pj_uint8_t *data, unsigned data_len) … … 2208 2311 * the last attribute before FINGERPRINT. 2209 2312 */ 2210 if ( i < msg->attr_count-2) {2313 if (msg->attr_count>1 && i < msg->attr_count-2) { 2211 2314 /* Should not happen for message generated by us */ 2212 2315 pj_assert(PJ_FALSE); … … 2299 2402 } 2300 2403 2404 2405 /* 2406 * Clone a STUN attribute. 2407 */ 2408 PJ_DEF(pj_stun_attr_hdr*) pj_stun_attr_clone( pj_pool_t *pool, 2409 const pj_stun_attr_hdr *attr) 2410 { 2411 const struct attr_desc *adesc; 2412 2413 /* Get the attribute descriptor */ 2414 adesc = find_attr_desc(attr->type); 2415 PJ_ASSERT_RETURN(adesc != NULL, NULL); 2416 2417 return (pj_stun_attr_hdr*) (*adesc->clone_attr)(pool, attr); 2418 } 2419 2420
Note: See TracChangeset
for help on using the changeset viewer.