Changeset 2041 for pjproject/trunk/pjnath/src/pjnath-test/stun.c
- Timestamp:
- Jun 21, 2008 12:36:56 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/src/pjnath-test/stun.c
r1877 r2041 164 164 "\x00\x01\x00\x08\x21\x12\xa4\x42" 165 165 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" 166 "\x80\xff\x00\x0 4\x00\x00\x00\x00",166 "\x80\xff\x00\x03\x00\x00\x00\x00", 167 167 28, 168 168 NULL, … … 370 370 static int verify2(pj_stun_msg *msg) 371 371 { 372 if (msg->attr_count != 0) { 373 PJ_LOG(1,(THIS_FILE, " expecting zero attribute count")); 372 pj_stun_binary_attr *bin_attr; 373 374 if (msg->attr_count != 1) { 375 PJ_LOG(1,(THIS_FILE, " expecting one attribute count")); 374 376 return -200; 375 377 } 378 379 bin_attr = (pj_stun_binary_attr*)msg->attr[0]; 380 if (bin_attr->hdr.type != 0x80ff) { 381 PJ_LOG(1,(THIS_FILE, " expecting attribute type 0x80ff")); 382 return -210; 383 } 384 if (bin_attr->hdr.length != 3) { 385 PJ_LOG(1,(THIS_FILE, " expecting attribute length = 4")); 386 return -220; 387 } 388 if (bin_attr->magic != PJ_STUN_MAGIC) { 389 PJ_LOG(1,(THIS_FILE, " expecting PJ_STUN_MAGIC for unknown attr")); 390 return -230; 391 } 392 if (bin_attr->length != 3) { 393 PJ_LOG(1,(THIS_FILE, " expecting data length 4")); 394 return -240; 395 } 396 376 397 return 0; 377 398 } … … 689 710 690 711 712 /* Compare two messages */ 713 static int cmp_msg(const pj_stun_msg *msg1, const pj_stun_msg *msg2) 714 { 715 unsigned i; 716 717 if (msg1->hdr.type != msg2->hdr.type) 718 return -10; 719 if (msg1->hdr.length != msg2->hdr.length) 720 return -20; 721 if (msg1->hdr.magic != msg2->hdr.magic) 722 return -30; 723 if (pj_memcmp(msg1->hdr.tsx_id, msg2->hdr.tsx_id, sizeof(msg1->hdr.tsx_id))) 724 return -40; 725 if (msg1->attr_count != msg2->attr_count) 726 return -50; 727 728 for (i=0; i<msg1->attr_count; ++i) { 729 const pj_stun_attr_hdr *a1 = msg1->attr[i]; 730 const pj_stun_attr_hdr *a2 = msg2->attr[i]; 731 732 if (a1->type != a2->type) 733 return -60; 734 if (a1->length != a2->length) 735 return -70; 736 } 737 738 return 0; 739 } 740 741 /* Decode and authenticate message with unknown non-mandatory attribute */ 742 static int handle_unknown_non_mandatory(void) 743 { 744 pj_pool_t *pool = pj_pool_create(mem, NULL, 1000, 1000, NULL); 745 pj_stun_msg *msg0, *msg1, *msg2; 746 pj_uint8_t data[] = { 1, 2, 3, 4, 5, 6}; 747 pj_uint8_t packet[500]; 748 pj_stun_auth_cred cred; 749 unsigned len; 750 pj_status_t rc; 751 752 PJ_LOG(3,(THIS_FILE, " handling unknown non-mandatory attr")); 753 754 PJ_LOG(3,(THIS_FILE, " encoding")); 755 rc = pj_stun_msg_create(pool, PJ_STUN_BINDING_REQUEST, PJ_STUN_MAGIC, NULL, &msg0); 756 rc += pj_stun_msg_add_string_attr(pool, msg0, PJ_STUN_ATTR_USERNAME, &USERNAME); 757 rc += pj_stun_msg_add_binary_attr(pool, msg0, 0x80ff, data, sizeof(data)); 758 rc += pj_stun_msg_add_msgint_attr(pool, msg0); 759 rc += pj_stun_msg_encode(msg0, packet, sizeof(packet), 0, &PASSWORD, &len); 760 761 #if 0 762 if (1) { 763 unsigned i; 764 puts(""); 765 printf("{ "); 766 for (i=0; i<len; ++i) printf("0x%02x, ", packet[i]); 767 puts(" }"); 768 } 769 #endif 770 771 PJ_LOG(3,(THIS_FILE, " decoding")); 772 rc += pj_stun_msg_decode(pool, packet, len, PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET, 773 &msg1, NULL, NULL); 774 775 rc += cmp_msg(msg0, msg1); 776 777 pj_bzero(&cred, sizeof(cred)); 778 cred.type = PJ_STUN_AUTH_CRED_STATIC; 779 cred.data.static_cred.username = USERNAME; 780 cred.data.static_cred.data_type = PJ_STUN_PASSWD_PLAIN; 781 cred.data.static_cred.data = PASSWORD; 782 783 PJ_LOG(3,(THIS_FILE, " authenticating")); 784 rc += pj_stun_authenticate_request(packet, len, msg1, &cred, pool, NULL, NULL); 785 786 PJ_LOG(3,(THIS_FILE, " clone")); 787 msg2 = pj_stun_msg_clone(pool, msg1); 788 rc += cmp_msg(msg0, msg2); 789 790 pj_pool_release(pool); 791 792 return rc==0 ? 0 : -4410; 793 } 794 795 691 796 int stun_test(void) 692 797 { … … 707 812 goto on_return; 708 813 814 rc = handle_unknown_non_mandatory(); 815 if (rc != 0) 816 goto on_return; 817 709 818 on_return: 710 819 pj_stun_set_padding_char(pad);
Note: See TracChangeset
for help on using the changeset viewer.