diff options
Diffstat (limited to 'tinyNET/test')
-rwxr-xr-x | tinyNET/test/targetver.h | 4 | ||||
-rwxr-xr-x | tinyNET/test/test.c | 44 | ||||
-rwxr-xr-x | tinyNET/test/test_dhcp.h | 133 | ||||
-rwxr-xr-x | tinyNET/test/test_dhcp6.h | 24 | ||||
-rwxr-xr-x | tinyNET/test/test_dns.h | 306 | ||||
-rwxr-xr-x | tinyNET/test/test_ice.h | 315 | ||||
-rwxr-xr-x | tinyNET/test/test_ifaces.h | 102 | ||||
-rwxr-xr-x | tinyNET/test/test_nat.h | 80 | ||||
-rwxr-xr-x | tinyNET/test/test_sockets.h | 69 | ||||
-rwxr-xr-x | tinyNET/test/test_stun.h | 290 | ||||
-rwxr-xr-x | tinyNET/test/test_tls.h | 113 | ||||
-rwxr-xr-x | tinyNET/test/test_transport.h | 242 |
12 files changed, 843 insertions, 879 deletions
diff --git a/tinyNET/test/targetver.h b/tinyNET/test/targetver.h index 21af608..5972193 100755 --- a/tinyNET/test/targetver.h +++ b/tinyNET/test/targetver.h @@ -20,8 +20,8 @@ #define TNET_TEST_TARGETVER_H // The following macros define the minimum required platform. The minimum required platform -// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run -// your application. The macros work by enabling all features available on platform versions up to and +// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run +// your application. The macros work by enabling all features available on platform versions up to and // including the version specified. // Modify the following defines if you have to target a platform prior to the ones specified below. diff --git a/tinyNET/test/test.c b/tinyNET/test/test.c index 64e0349..d8af3a8 100755 --- a/tinyNET/test/test.c +++ b/tinyNET/test/test.c @@ -57,65 +57,65 @@ int _tmain(int argc, _TCHAR* argv[]) int main() #endif { - /* Startup the network stack. */ - if(tnet_startup()){ - return -1; - } + /* Startup the network stack. */ + if(tnet_startup()) { + return -1; + } #if RUN_TEST_LOOP - for(;;) + for(;;) #endif - { - + { + #if RUN_TEST_ALL || RUN_TEST_SOCKETS - test_sockets(); + test_sockets(); #endif #if RUN_TEST_ALL || RUN_TEST_TRANSPORT - test_transport(); + test_transport(); #endif #if RUN_TEST_ALL || RUN_TEST_AUTH - test_auth(); + test_auth(); #endif #if RUN_TEST_ALL || RUN_TEST_STUN - test_stun(); + test_stun(); #endif #if RUN_TEST_ALL || RUN_TEST_ICE - test_ice(); + test_ice(); #endif #if RUN_TEST_ALL || RUN_TEST_NAT - test_nat(); + test_nat(); #endif #if RUN_TEST_ALL || RUN_TEST_IFACES - test_ifaces(); + test_ifaces(); #endif #if RUN_TEST_ALL || RUN_TEST_DNS - test_dns(); + test_dns(); #endif #if RUN_TEST_ALL || RUN_TEST_DHCP - test_dhcp(); + test_dhcp(); #endif #if RUN_TEST_ALL || RUN_TEST_DHCP6 - test_dhcp6(); + test_dhcp6(); #endif #if RUN_TEST_ALL || RUN_TEST_TLS - test_tls(); + test_tls(); #endif - } + } - /* Cleanup the network stack */ - tnet_cleanup(); + /* Cleanup the network stack */ + tnet_cleanup(); - return 0; + return 0; } diff --git a/tinyNET/test/test_dhcp.h b/tinyNET/test/test_dhcp.h index c74e99c..ee6e767 100755 --- a/tinyNET/test/test_dhcp.h +++ b/tinyNET/test/test_dhcp.h @@ -35,84 +35,75 @@ void test_dhcp_request(tnet_dhcp_ctx_t *ctx) void test_dhcp_inform(tnet_dhcp_ctx_t *ctx) { - tnet_dhcp_params_t *params = tsk_null; - tnet_dhcp_reply_t *reply = tsk_null; - - params = tnet_dhcp_params_create(); - tnet_dhcp_params_add_code(params, dhcp_code_SIP_Servers_DHCP_Option); /* SIP Servers */ - tnet_dhcp_params_add_code(params, dhcp_code_Domain_Server); /* DNS Server */ - - reply = tnet_dhcp_query_inform(ctx, params); - - if(reply && !TNET_DHCP_MESSAGE_IS_REPLY(reply)){ - TSK_DEBUG_ERROR("DHCP request is not expected in response to a request."); - goto bail; - } - - if(reply){ - switch(reply->type) - { - case dhcp_type_ack: - { - tsk_list_item_t *item; - TSK_DEBUG_INFO("DHCP response type ==> ACK."); - - tsk_list_foreach(item, reply->options) - { - const tnet_dhcp_option_t *option = item->data; - - /* SIP SERVERS */ - if(option->code == dhcp_code_SIP_Servers_DHCP_Option) - { - tsk_list_item_t *item2; - const tnet_dhcp_option_sip_t *option_sip4 = (const tnet_dhcp_option_sip_t*)option;; - tsk_list_foreach(item2, option_sip4->servers) - { - const tsk_string_t *str = item2->data; - TSK_DEBUG_INFO("DHCP-SIP_SERVER ==>%s", str->value); - } - } - - /* DNS SERVERS */ - if(option->code == dhcp_code_Domain_Server) - { - tsk_list_item_t *item2; - const tnet_dhcp_option_dns_t *option_dns = (const tnet_dhcp_option_dns_t*)option;; - tsk_list_foreach(item2, option_dns->servers) - { - const tsk_string_t *str = item2->data; - TSK_DEBUG_INFO("DHCP-DNS_SERVER ==>%s", str->value); - } - } - } - break; - } - - default: - { - break; - } - } - } - else - { - TSK_DEBUG_ERROR("DHCP reply is NULL."); - goto bail; - } - + tnet_dhcp_params_t *params = tsk_null; + tnet_dhcp_reply_t *reply = tsk_null; + + params = tnet_dhcp_params_create(); + tnet_dhcp_params_add_code(params, dhcp_code_SIP_Servers_DHCP_Option); /* SIP Servers */ + tnet_dhcp_params_add_code(params, dhcp_code_Domain_Server); /* DNS Server */ + + reply = tnet_dhcp_query_inform(ctx, params); + + if(reply && !TNET_DHCP_MESSAGE_IS_REPLY(reply)) { + TSK_DEBUG_ERROR("DHCP request is not expected in response to a request."); + goto bail; + } + + if(reply) { + switch(reply->type) { + case dhcp_type_ack: { + tsk_list_item_t *item; + TSK_DEBUG_INFO("DHCP response type ==> ACK."); + + tsk_list_foreach(item, reply->options) { + const tnet_dhcp_option_t *option = item->data; + + /* SIP SERVERS */ + if(option->code == dhcp_code_SIP_Servers_DHCP_Option) { + tsk_list_item_t *item2; + const tnet_dhcp_option_sip_t *option_sip4 = (const tnet_dhcp_option_sip_t*)option;; + tsk_list_foreach(item2, option_sip4->servers) { + const tsk_string_t *str = item2->data; + TSK_DEBUG_INFO("DHCP-SIP_SERVER ==>%s", str->value); + } + } + + /* DNS SERVERS */ + if(option->code == dhcp_code_Domain_Server) { + tsk_list_item_t *item2; + const tnet_dhcp_option_dns_t *option_dns = (const tnet_dhcp_option_dns_t*)option;; + tsk_list_foreach(item2, option_dns->servers) { + const tsk_string_t *str = item2->data; + TSK_DEBUG_INFO("DHCP-DNS_SERVER ==>%s", str->value); + } + } + } + break; + } + + default: { + break; + } + } + } + else { + TSK_DEBUG_ERROR("DHCP reply is NULL."); + goto bail; + } + bail: - TSK_OBJECT_SAFE_FREE(reply); - TSK_OBJECT_SAFE_FREE(params); + TSK_OBJECT_SAFE_FREE(reply); + TSK_OBJECT_SAFE_FREE(params); - //tsk_thread_sleep(1000); + //tsk_thread_sleep(1000); } void test_dhcp() { - tnet_dhcp_ctx_t *ctx = tnet_dhcp_ctx_create(); - test_dhcp_inform(ctx); + tnet_dhcp_ctx_t *ctx = tnet_dhcp_ctx_create(); + test_dhcp_inform(ctx); - TSK_OBJECT_SAFE_FREE(ctx); + TSK_OBJECT_SAFE_FREE(ctx); } #endif /* TNET_TEST_DHCP_H */ diff --git a/tinyNET/test/test_dhcp6.h b/tinyNET/test/test_dhcp6.h index c96fd96..1844dcc 100755 --- a/tinyNET/test/test_dhcp6.h +++ b/tinyNET/test/test_dhcp6.h @@ -23,26 +23,26 @@ void test_dhcp6_requestinfo(tnet_dhcp6_ctx_t *ctx) { - tnet_dhcp6_option_orequest_t *orequest = tnet_dhcp6_option_orequest_create_null(); - tnet_dhcp6_reply_t* reply = 0; + tnet_dhcp6_option_orequest_t *orequest = tnet_dhcp6_option_orequest_create_null(); + tnet_dhcp6_reply_t* reply = 0; - tnet_dhcp6_option_orequest_add_code(orequest, 24); - tnet_dhcp6_option_orequest_add_code(orequest, 23); - tnet_dhcp6_option_orequest_add_code(orequest, 21); /* SIP Servers Domain Name List */ - tnet_dhcp6_option_orequest_add_code(orequest, 22); /* SIP Servers IPv6 Address List */ + tnet_dhcp6_option_orequest_add_code(orequest, 24); + tnet_dhcp6_option_orequest_add_code(orequest, 23); + tnet_dhcp6_option_orequest_add_code(orequest, 21); /* SIP Servers Domain Name List */ + tnet_dhcp6_option_orequest_add_code(orequest, 22); /* SIP Servers IPv6 Address List */ - reply = tnet_dhcp6_requestinfo(ctx, orequest); + reply = tnet_dhcp6_requestinfo(ctx, orequest); - TSK_OBJECT_SAFE_FREE(orequest); - TSK_OBJECT_SAFE_FREE(reply); + TSK_OBJECT_SAFE_FREE(orequest); + TSK_OBJECT_SAFE_FREE(reply); } void test_dhcp6() { - tnet_dhcp6_ctx_t *ctx = tnet_dhcp6_ctx_create(); - test_dhcp6_requestinfo(ctx); + tnet_dhcp6_ctx_t *ctx = tnet_dhcp6_ctx_create(); + test_dhcp6_requestinfo(ctx); - TSK_OBJECT_SAFE_FREE(ctx); + TSK_OBJECT_SAFE_FREE(ctx); } #endif /* TNET_TEST_DHCP6_H */ diff --git a/tinyNET/test/test_dns.h b/tinyNET/test/test_dns.h index f2ab145..83c8002 100755 --- a/tinyNET/test/test_dns.h +++ b/tinyNET/test/test_dns.h @@ -23,201 +23,199 @@ void test_dns_query() { - tnet_dns_ctx_t *ctx = tnet_dns_ctx_create(); - tnet_dns_response_t *response = tsk_null; - const tsk_list_item_t* item; - const tnet_dns_rr_t* rr; - - //if((response = tnet_dns_resolve(ctx, "_sip._udp.sip2sip.info", qclass_in, qtype_srv))) - if((response = tnet_dns_resolve(ctx, "sip2sip.info", qclass_in, qtype_naptr))) - { - if(TNET_DNS_RESPONSE_IS_SUCCESS(response)){ - TSK_DEBUG_INFO("We got a success response from the DNS server."); - // loop through the answers - tsk_list_foreach(item, response->Answers){ - rr = item->data; - if(rr->qtype == qtype_naptr){ - const tnet_dns_naptr_t *naptr = (const tnet_dns_naptr_t*)rr; - - TSK_DEBUG_INFO("order=%u pref=%u flags=%s services=%s regexp=%s replacement=%s", - naptr->order, - naptr->preference, - naptr->flags, - naptr->services, - naptr->regexp, - naptr->replacement); - } - } - } - else{ - TSK_DEBUG_ERROR("We got an error response from the DNS server. Erro code: %u", response->Header.RCODE); - } - } - - tnet_dns_cache_clear(ctx); - - TSK_OBJECT_SAFE_FREE(response); - TSK_OBJECT_SAFE_FREE(ctx); - - - tsk_thread_sleep(2000); + tnet_dns_ctx_t *ctx = tnet_dns_ctx_create(); + tnet_dns_response_t *response = tsk_null; + const tsk_list_item_t* item; + const tnet_dns_rr_t* rr; + + //if((response = tnet_dns_resolve(ctx, "_sip._udp.sip2sip.info", qclass_in, qtype_srv))) + if((response = tnet_dns_resolve(ctx, "sip2sip.info", qclass_in, qtype_naptr))) { + if(TNET_DNS_RESPONSE_IS_SUCCESS(response)) { + TSK_DEBUG_INFO("We got a success response from the DNS server."); + // loop through the answers + tsk_list_foreach(item, response->Answers) { + rr = item->data; + if(rr->qtype == qtype_naptr) { + const tnet_dns_naptr_t *naptr = (const tnet_dns_naptr_t*)rr; + + TSK_DEBUG_INFO("order=%u pref=%u flags=%s services=%s regexp=%s replacement=%s", + naptr->order, + naptr->preference, + naptr->flags, + naptr->services, + naptr->regexp, + naptr->replacement); + } + } + } + else { + TSK_DEBUG_ERROR("We got an error response from the DNS server. Erro code: %u", response->Header.RCODE); + } + } + + tnet_dns_cache_clear(ctx); + + TSK_OBJECT_SAFE_FREE(response); + TSK_OBJECT_SAFE_FREE(ctx); + + + tsk_thread_sleep(2000); } void test_dns_srv() { - tnet_dns_ctx_t *ctx = tnet_dns_ctx_create(); - char* hostname = 0; - tnet_port_t port = 0; + tnet_dns_ctx_t *ctx = tnet_dns_ctx_create(); + char* hostname = 0; + tnet_port_t port = 0; - if(!tnet_dns_query_srv(ctx, "_sip._udp.sip2sip.info", &hostname, &port)){ - TSK_DEBUG_INFO("DNS SRV succeed ==> hostname=%s and port=%u", hostname, port); - } + if(!tnet_dns_query_srv(ctx, "_sip._udp.sip2sip.info", &hostname, &port)) { + TSK_DEBUG_INFO("DNS SRV succeed ==> hostname=%s and port=%u", hostname, port); + } - TSK_FREE(hostname); - TSK_OBJECT_SAFE_FREE(ctx); + TSK_FREE(hostname); + TSK_OBJECT_SAFE_FREE(ctx); - tsk_thread_sleep(2000); + tsk_thread_sleep(2000); } void test_dns_naptr_srv() { - tnet_dns_ctx_t *ctx = tnet_dns_ctx_create(); - char* hostname = tsk_null; - tnet_port_t port = 0; + tnet_dns_ctx_t *ctx = tnet_dns_ctx_create(); + char* hostname = tsk_null; + tnet_port_t port = 0; - if(!tnet_dns_query_naptr_srv(ctx, "sip2sip.info", "SIP+D2U", &hostname, &port)){ - TSK_DEBUG_INFO("DNS NAPTR+SRV succeed ==> hostname=%s and port=%u", hostname, port); - } + if(!tnet_dns_query_naptr_srv(ctx, "sip2sip.info", "SIP+D2U", &hostname, &port)) { + TSK_DEBUG_INFO("DNS NAPTR+SRV succeed ==> hostname=%s and port=%u", hostname, port); + } - TSK_FREE(hostname); - TSK_OBJECT_SAFE_FREE(ctx); + TSK_FREE(hostname); + TSK_OBJECT_SAFE_FREE(ctx); - tsk_thread_sleep(2000); + tsk_thread_sleep(2000); } void test_enum() { - tnet_dns_ctx_t *ctx = tnet_dns_ctx_create(); - tnet_dns_response_t* response = tsk_null; + tnet_dns_ctx_t *ctx = tnet_dns_ctx_create(); + tnet_dns_response_t* response = tsk_null; // const tsk_list_item_t* item; // const tnet_dns_naptr_t* record; - char* uri = tsk_null; - const char* e164num = "+1-800-555-5555"; - //const char* e164num = "+33660188661"; - - //tnet_dns_add_server(ctx, "192.168.16.9"); - - //if((uri = tnet_dns_enum_2(ctx, "E2U+SIP", e164num, "e164.org"))){ - if((uri = tnet_dns_enum_2(ctx, "E2U+SIP", e164num, "e164.org"))){ - TSK_DEBUG_INFO("URI=%s", uri); - TSK_FREE(uri); - } - else{ - TSK_DEBUG_ERROR("ENUM(%s) failed", e164num); - } - - /*if((response = tnet_dns_enum(ctx, "+1-800-555-5555", "e164.org"))){ - if(TNET_DNS_RESPONSE_IS_SUCCESS(response)){ - TSK_DEBUG_INFO("We got a success response from the DNS server."); - // loop through the answers - tsk_list_foreach(item, response->Answers){ - record = item->data; - - TSK_DEBUG_INFO("order=%u pref=%u flags=%s services=%s regexp=%s replacement=%s", - record->order, - record->preference, - record->flags, - record->services, - record->regexp, - record->replacement); - } - } - else{ - TSK_DEBUG_ERROR("We got an error response from the DNS server. Erro code: %u", response->Header.RCODE); - } - }*/ - - - TSK_OBJECT_SAFE_FREE(response); - TSK_OBJECT_SAFE_FREE(ctx); - - tsk_thread_sleep(2000); + char* uri = tsk_null; + const char* e164num = "+1-800-555-5555"; + //const char* e164num = "+33660188661"; + + //tnet_dns_add_server(ctx, "192.168.16.9"); + + //if((uri = tnet_dns_enum_2(ctx, "E2U+SIP", e164num, "e164.org"))){ + if((uri = tnet_dns_enum_2(ctx, "E2U+SIP", e164num, "e164.org"))) { + TSK_DEBUG_INFO("URI=%s", uri); + TSK_FREE(uri); + } + else { + TSK_DEBUG_ERROR("ENUM(%s) failed", e164num); + } + + /*if((response = tnet_dns_enum(ctx, "+1-800-555-5555", "e164.org"))){ + if(TNET_DNS_RESPONSE_IS_SUCCESS(response)){ + TSK_DEBUG_INFO("We got a success response from the DNS server."); + // loop through the answers + tsk_list_foreach(item, response->Answers){ + record = item->data; + + TSK_DEBUG_INFO("order=%u pref=%u flags=%s services=%s regexp=%s replacement=%s", + record->order, + record->preference, + record->flags, + record->services, + record->regexp, + record->replacement); + } + } + else{ + TSK_DEBUG_ERROR("We got an error response from the DNS server. Erro code: %u", response->Header.RCODE); + } + }*/ + + + TSK_OBJECT_SAFE_FREE(response); + TSK_OBJECT_SAFE_FREE(ctx); + + tsk_thread_sleep(2000); } -typedef struct regexp_test_s{ - const char* e164num; - const char* regexp; - const char* xres; +typedef struct regexp_test_s { + const char* e164num; + const char* regexp; + const char* xres; } regexp_test_t; regexp_test_t regexp_tests[] = { - "+18005551234", "!^.*$!sip:customer-service@example.com!i", "sip:customer-service@example.com", - "+18005551234", "!^.*$!mailto:information@example.com!i", "mailto:information@example.com", + "+18005551234", "!^.*$!sip:customer-service@example.com!i", "sip:customer-service@example.com", + "+18005551234", "!^.*$!mailto:information@example.com!i", "mailto:information@example.com", - "+18005555555", "!^\\+1800(.*)$!sip:1641641800\\1@tollfree.sip-happens.com!", "sip:16416418005555555@tollfree.sip-happens.com", - "+18005555555", "!^\\+1800(.*)$!sip:1641641800\\1@sip.tollfreegateway.com!", "sip:16416418005555555@sip.tollfreegateway.com", - - "+468971234", "!^+46(.*)$!ldap://ldap.telco.se/cn=0\\1!", "ldap://ldap.telco.se/cn=08971234", - "+468971234", "!^+46(.*)$!mailto:spam@paf.se!", "mailto:spam@paf.se", + "+18005555555", "!^\\+1800(.*)$!sip:1641641800\\1@tollfree.sip-happens.com!", "sip:16416418005555555@tollfree.sip-happens.com", + "+18005555555", "!^\\+1800(.*)$!sip:1641641800\\1@sip.tollfreegateway.com!", "sip:16416418005555555@sip.tollfreegateway.com", - "urn:cid:199606121851.1@bar.example.com", "!!^urn:cid:.+@([^\\.]+\\.)(.*)$!\\2!i", "example.com", + "+468971234", "!^+46(.*)$!ldap://ldap.telco.se/cn=0\\1!", "ldap://ldap.telco.se/cn=08971234", + "+468971234", "!^+46(.*)$!mailto:spam@paf.se!", "mailto:spam@paf.se", + + "urn:cid:199606121851.1@bar.example.com", "!!^urn:cid:.+@([^\\.]+\\.)(.*)$!\\2!i", "example.com", }; void test_regex() { - char* ret; - size_t i; - - for(i=0; i< sizeof(regexp_tests)/sizeof(regexp_test_t); i++) - { - if((ret = tnet_dns_regex_parse(regexp_tests[i].e164num, regexp_tests[i].regexp))){ - TSK_DEBUG_INFO("ENUM(%s) = %s", regexp_tests[i].e164num, ret); - if(!tsk_strequals(ret, regexp_tests[i].xres)){ - TSK_DEBUG_ERROR("Failed to match ENUM(%s)", regexp_tests[i].e164num); - } - TSK_FREE(ret); - } - else{ - TSK_DEBUG_ERROR("Failed to parse ENUM(%s)", regexp_tests[i].e164num); - } - - TSK_DEBUG_INFO("---------"); - } + char* ret; + size_t i; + + for(i=0; i< sizeof(regexp_tests)/sizeof(regexp_test_t); i++) { + if((ret = tnet_dns_regex_parse(regexp_tests[i].e164num, regexp_tests[i].regexp))) { + TSK_DEBUG_INFO("ENUM(%s) = %s", regexp_tests[i].e164num, ret); + if(!tsk_strequals(ret, regexp_tests[i].xres)) { + TSK_DEBUG_ERROR("Failed to match ENUM(%s)", regexp_tests[i].e164num); + } + TSK_FREE(ret); + } + else { + TSK_DEBUG_ERROR("Failed to parse ENUM(%s)", regexp_tests[i].e164num); + } + + TSK_DEBUG_INFO("---------"); + } } void test_resolvconf() { - tnet_addresses_L_t * servers; - const tnet_address_t* address; - const tsk_list_item_t* item; - const char* path = "C:\\tmp\\resolv32.conf"; - //const char* path = "C:\\tmp\\resolv.conf"; - //const char* path = "/etc/resolv.conf"; - - if((servers = tnet_dns_resolvconf_parse(path))){ - tsk_list_foreach(item, servers){ - address = item->data; - - TSK_DEBUG_INFO("DNS Server host=%s Family=%d", address->ip, address->family); - } - - TSK_OBJECT_SAFE_FREE(servers); - } - else{ - TSK_DEBUG_ERROR("Failed to parse DNS servers from %s.", path); - } + tnet_addresses_L_t * servers; + const tnet_address_t* address; + const tsk_list_item_t* item; + const char* path = "C:\\tmp\\resolv32.conf"; + //const char* path = "C:\\tmp\\resolv.conf"; + //const char* path = "/etc/resolv.conf"; + + if((servers = tnet_dns_resolvconf_parse(path))) { + tsk_list_foreach(item, servers) { + address = item->data; + + TSK_DEBUG_INFO("DNS Server host=%s Family=%d", address->ip, address->family); + } + + TSK_OBJECT_SAFE_FREE(servers); + } + else { + TSK_DEBUG_ERROR("Failed to parse DNS servers from %s.", path); + } } void test_dns() { - test_dns_naptr_srv(); - //test_dns_srv(); - //test_dns_query(); - //test_enum(); - //test_regex(); - //test_resolvconf(); + test_dns_naptr_srv(); + //test_dns_srv(); + //test_dns_query(); + //test_enum(); + //test_regex(); + //test_resolvconf(); } diff --git a/tinyNET/test/test_ice.h b/tinyNET/test/test_ice.h index 023a2c2..c7341b2 100755 --- a/tinyNET/test/test_ice.h +++ b/tinyNET/test/test_ice.h @@ -44,193 +44,190 @@ static struct tnet_ice_ctx_s *p_ice_ctx2 = tsk_null; static void test_ice_print_local_candidates(const struct tnet_ice_ctx_s *pc_ctx) { - tsk_size_t index = 0; - const tnet_ice_candidate_t* candidate; - char* p_str = tsk_null; - - while ((candidate = tnet_ice_ctx_get_local_candidate_at(pc_ctx, index++))) { - tsk_strcat_2(&p_str, "%s\r\n", tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate)); - } - TSK_DEBUG_INFO("ICE LOCAL CANDIDATES:\n%s", p_str); - TSK_FREE(p_str); + tsk_size_t index = 0; + const tnet_ice_candidate_t* candidate; + char* p_str = tsk_null; + + while ((candidate = tnet_ice_ctx_get_local_candidate_at(pc_ctx, index++))) { + tsk_strcat_2(&p_str, "%s\r\n", tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate)); + } + TSK_DEBUG_INFO("ICE LOCAL CANDIDATES:\n%s", p_str); + TSK_FREE(p_str); } static char* test_ice_get_local_candidates(const struct tnet_ice_ctx_s *pc_ctx) { - tsk_size_t index = 0; - const tnet_ice_candidate_t* candidate; - char* p_str = tsk_null; - - while ((candidate = tnet_ice_ctx_get_local_candidate_at(pc_ctx, index++))) { - if (kSkipHosts && candidate->type_e == tnet_ice_cand_type_host) { - continue; - } - if (kSkipReflexives && candidate->type_e == tnet_ice_cand_type_srflx) { - continue; - } - if (kSkipRelays && candidate->type_e == tnet_ice_cand_type_relay) { - continue; - } - if (kSkipPeers && candidate->type_e == tnet_ice_cand_type_prflx) { - continue; - } - tsk_strcat_2(&p_str, "%s\r\n", tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate)); - } - return p_str; + tsk_size_t index = 0; + const tnet_ice_candidate_t* candidate; + char* p_str = tsk_null; + + while ((candidate = tnet_ice_ctx_get_local_candidate_at(pc_ctx, index++))) { + if (kSkipHosts && candidate->type_e == tnet_ice_cand_type_host) { + continue; + } + if (kSkipReflexives && candidate->type_e == tnet_ice_cand_type_srflx) { + continue; + } + if (kSkipRelays && candidate->type_e == tnet_ice_cand_type_relay) { + continue; + } + if (kSkipPeers && candidate->type_e == tnet_ice_cand_type_prflx) { + continue; + } + tsk_strcat_2(&p_str, "%s\r\n", tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate)); + } + return p_str; } static int test_ice_rtp_callback(const void* callback_data, const uint8_t* data_ptr, tsk_size_t data_size, tnet_fd_t local_fd, const struct sockaddr_storage* remote_addr) { - struct tnet_ice_ctx_s *p_ice_ctx = (struct tnet_ice_ctx_s *)callback_data; + struct tnet_ice_ctx_s *p_ice_ctx = (struct tnet_ice_ctx_s *)callback_data; - TSK_DEBUG_INFO("\n\nICE rtp callback (incoming data): %.*s\n\n", data_size, data_ptr); + TSK_DEBUG_INFO("\n\nICE rtp callback (incoming data): %.*s\n\n", data_size, data_ptr); #if 0 - tnet_ice_ctx_send_turn_rtp(p_ice_ctx, data_ptr, data_size); + tnet_ice_ctx_send_turn_rtp(p_ice_ctx, data_ptr, data_size); #endif - return 0; + return 0; } static int test_ice_state_callback(const tnet_ice_event_t *e) { - struct tnet_ice_ctx_s *p_ice_ctx = (struct tnet_ice_ctx_s *)e->ctx; - int ret = 0; - - TSK_DEBUG_INFO("ICE state callback: %s", e->phrase); - - switch(e->type) - { - case tnet_ice_event_type_gathering_completed: - { - test_ice_print_local_candidates(p_ice_ctx); - if (p_ice_ctx == p_ice_ctx1) { - if ((ret = tnet_ice_ctx_start(p_ice_ctx2))) { - goto bail; - } - } - else { - const tnet_ice_candidate_t* candidate; - char* p_cand; - - p_cand = test_ice_get_local_candidates(p_ice_ctx2); - candidate = tnet_ice_ctx_get_local_candidate_first(p_ice_ctx2); - ret = tnet_ice_ctx_set_remote_candidates(p_ice_ctx1, p_cand, candidate->ufrag, candidate->pwd, tsk_true, tsk_false); - if (ret == 0) { - TSK_FREE(p_cand); - p_cand = test_ice_get_local_candidates(p_ice_ctx1); - candidate = tnet_ice_ctx_get_local_candidate_first(p_ice_ctx1); - ret = tnet_ice_ctx_set_remote_candidates(p_ice_ctx2, p_cand, candidate->ufrag, candidate->pwd, tsk_false, tsk_false); - TSK_FREE(p_cand); - } - } - break; - } - - case tnet_ice_event_type_conncheck_succeed: - { - const char kTurnData[] = "TURN data to send for testing"; - const tnet_ice_candidate_t *candidate_offer, *candidate_answer_src, *candidate_answer_dest; - - // === RTP === // - ret = tnet_ice_ctx_get_nominated_symetric_candidates(p_ice_ctx, TNET_ICE_CANDIDATE_COMPID_RTP, &candidate_offer, &candidate_answer_src, &candidate_answer_dest); - if (ret == 0) { - TSK_DEBUG_INFO("Nominated candidate(RTP): Offer=[[%s]], AnswerSrc=[[%s]], AnswerDest=[[%s]]", - tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate_offer), - tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate_answer_src), - tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate_answer_dest)); - - if (tnet_ice_ctx_is_turn_rtp_active(p_ice_ctx)) { - tnet_ice_ctx_send_turn_rtp(p_ice_ctx, kTurnData, sizeof(kTurnData)); - } - } - // === RTCP === // - if (use_rtcp) { - ret = tnet_ice_ctx_get_nominated_symetric_candidates(p_ice_ctx, TNET_ICE_CANDIDATE_COMPID_RTCP, &candidate_offer, &candidate_answer_src, &candidate_answer_dest); - if (ret == 0) { - TSK_DEBUG_INFO("Nominated candidate(RTCP): Offer=[[%s]], AnswerSrc=[[%s]], AnswerDest=[[%s]]", - tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate_offer), - tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate_answer_src), - tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate_answer_dest)); - if (tnet_ice_ctx_is_turn_rtcp_active(p_ice_ctx)) { - tnet_ice_ctx_send_turn_rtcp(p_ice_ctx, kTurnData, sizeof(kTurnData)); - } - } - } - break; - } - } + struct tnet_ice_ctx_s *p_ice_ctx = (struct tnet_ice_ctx_s *)e->ctx; + int ret = 0; + + TSK_DEBUG_INFO("ICE state callback: %s", e->phrase); + + switch(e->type) { + case tnet_ice_event_type_gathering_completed: { + test_ice_print_local_candidates(p_ice_ctx); + if (p_ice_ctx == p_ice_ctx1) { + if ((ret = tnet_ice_ctx_start(p_ice_ctx2))) { + goto bail; + } + } + else { + const tnet_ice_candidate_t* candidate; + char* p_cand; + + p_cand = test_ice_get_local_candidates(p_ice_ctx2); + candidate = tnet_ice_ctx_get_local_candidate_first(p_ice_ctx2); + ret = tnet_ice_ctx_set_remote_candidates(p_ice_ctx1, p_cand, candidate->ufrag, candidate->pwd, tsk_true, tsk_false); + if (ret == 0) { + TSK_FREE(p_cand); + p_cand = test_ice_get_local_candidates(p_ice_ctx1); + candidate = tnet_ice_ctx_get_local_candidate_first(p_ice_ctx1); + ret = tnet_ice_ctx_set_remote_candidates(p_ice_ctx2, p_cand, candidate->ufrag, candidate->pwd, tsk_false, tsk_false); + TSK_FREE(p_cand); + } + } + break; + } + + case tnet_ice_event_type_conncheck_succeed: { + const char kTurnData[] = "TURN data to send for testing"; + const tnet_ice_candidate_t *candidate_offer, *candidate_answer_src, *candidate_answer_dest; + + // === RTP === // + ret = tnet_ice_ctx_get_nominated_symetric_candidates(p_ice_ctx, TNET_ICE_CANDIDATE_COMPID_RTP, &candidate_offer, &candidate_answer_src, &candidate_answer_dest); + if (ret == 0) { + TSK_DEBUG_INFO("Nominated candidate(RTP): Offer=[[%s]], AnswerSrc=[[%s]], AnswerDest=[[%s]]", + tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate_offer), + tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate_answer_src), + tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate_answer_dest)); + + if (tnet_ice_ctx_is_turn_rtp_active(p_ice_ctx)) { + tnet_ice_ctx_send_turn_rtp(p_ice_ctx, kTurnData, sizeof(kTurnData)); + } + } + // === RTCP === // + if (use_rtcp) { + ret = tnet_ice_ctx_get_nominated_symetric_candidates(p_ice_ctx, TNET_ICE_CANDIDATE_COMPID_RTCP, &candidate_offer, &candidate_answer_src, &candidate_answer_dest); + if (ret == 0) { + TSK_DEBUG_INFO("Nominated candidate(RTCP): Offer=[[%s]], AnswerSrc=[[%s]], AnswerDest=[[%s]]", + tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate_offer), + tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate_answer_src), + tnet_ice_candidate_tostring((tnet_ice_candidate_t*)candidate_answer_dest)); + if (tnet_ice_ctx_is_turn_rtcp_active(p_ice_ctx)) { + tnet_ice_ctx_send_turn_rtcp(p_ice_ctx, kTurnData, sizeof(kTurnData)); + } + } + } + break; + } + } bail: - return ret; + return ret; } void test_ice() { - int ret; - static const tsk_bool_t use_ipv6 = tsk_false; - static const tsk_bool_t use_ice_jingle = tsk_false; - static const tsk_bool_t use_video = tsk_false; - - if (!(p_ice_ctx1 = tnet_ice_ctx_create(use_ice_jingle, use_ipv6, use_rtcp, use_video, test_ice_state_callback, tsk_null))) { - goto bail; - } - if (!(p_ice_ctx2 = tnet_ice_ctx_create(use_ice_jingle, use_ipv6, use_rtcp, use_video, test_ice_state_callback, tsk_null))) { - goto bail; - } - if ((ret = tnet_ice_ctx_set_turn_enabled(p_ice_ctx1, 1))) { - goto bail; - } - if ((ret = tnet_ice_ctx_set_turn_enabled(p_ice_ctx2, 1))) { - goto bail; - } - if ((ret = tnet_ice_ctx_set_stun_enabled(p_ice_ctx1, 1))) { - goto bail; - } - if ((ret = tnet_ice_ctx_set_stun_enabled(p_ice_ctx2, 1))) { - goto bail; - } - if ((ret = tnet_ice_ctx_set_userdata(p_ice_ctx1, p_ice_ctx1))) { - goto bail; - } - if ((ret = tnet_ice_ctx_set_userdata(p_ice_ctx2, p_ice_ctx2))) { - goto bail; - } - if ((ret = tnet_ice_ctx_rtp_callback(p_ice_ctx1, test_ice_rtp_callback, p_ice_ctx1))) { - goto bail; - } - if ((ret = tnet_ice_ctx_rtp_callback(p_ice_ctx2, test_ice_rtp_callback, p_ice_ctx2))) { - goto bail; - } + int ret; + static const tsk_bool_t use_ipv6 = tsk_false; + static const tsk_bool_t use_ice_jingle = tsk_false; + static const tsk_bool_t use_video = tsk_false; + + if (!(p_ice_ctx1 = tnet_ice_ctx_create(use_ice_jingle, use_ipv6, use_rtcp, use_video, test_ice_state_callback, tsk_null))) { + goto bail; + } + if (!(p_ice_ctx2 = tnet_ice_ctx_create(use_ice_jingle, use_ipv6, use_rtcp, use_video, test_ice_state_callback, tsk_null))) { + goto bail; + } + if ((ret = tnet_ice_ctx_set_turn_enabled(p_ice_ctx1, 1))) { + goto bail; + } + if ((ret = tnet_ice_ctx_set_turn_enabled(p_ice_ctx2, 1))) { + goto bail; + } + if ((ret = tnet_ice_ctx_set_stun_enabled(p_ice_ctx1, 1))) { + goto bail; + } + if ((ret = tnet_ice_ctx_set_stun_enabled(p_ice_ctx2, 1))) { + goto bail; + } + if ((ret = tnet_ice_ctx_set_userdata(p_ice_ctx1, p_ice_ctx1))) { + goto bail; + } + if ((ret = tnet_ice_ctx_set_userdata(p_ice_ctx2, p_ice_ctx2))) { + goto bail; + } + if ((ret = tnet_ice_ctx_rtp_callback(p_ice_ctx1, test_ice_rtp_callback, p_ice_ctx1))) { + goto bail; + } + if ((ret = tnet_ice_ctx_rtp_callback(p_ice_ctx2, test_ice_rtp_callback, p_ice_ctx2))) { + goto bail; + } #if 0 //@deprecated - if ((ret = tnet_ice_ctx_set_stun(p_ice_ctx1, kStunServerIP, 3478, kStunSoftware, kStunUsrName, kStunPwd))) { - goto bail; - } - if ((ret = tnet_ice_ctx_set_stun(p_ice_ctx2, kStunServerIP, 3478, kStunSoftware, kStunUsrName, kStunPwd))) { - goto bail; - } + if ((ret = tnet_ice_ctx_set_stun(p_ice_ctx1, kStunServerIP, 3478, kStunSoftware, kStunUsrName, kStunPwd))) { + goto bail; + } + if ((ret = tnet_ice_ctx_set_stun(p_ice_ctx2, kStunServerIP, 3478, kStunSoftware, kStunUsrName, kStunPwd))) { + goto bail; + } #else - tnet_ice_ctx_add_server(p_ice_ctx1, "udp", kStunServerIP, 3478, kTurnFalse, kStunTrue, kStunUsrName, kStunPwd); // STUN-UDP - tnet_ice_ctx_add_server(p_ice_ctx1, "tcp", kStunServerIP, 3478, kTurnTrue, kStunFalse, kStunUsrName, kStunPwd); // TURN-TCP - tnet_ice_ctx_add_server(p_ice_ctx2, "udp", kStunServerIP, 3478, kTurnFalse, kStunTrue, kStunUsrName, kStunPwd); // STUN-UDP - tnet_ice_ctx_add_server(p_ice_ctx2, "tcp", kStunServerIP, 3478, kTurnTrue, kStunFalse, kStunUsrName, kStunPwd); // TURN-TCP + tnet_ice_ctx_add_server(p_ice_ctx1, "udp", kStunServerIP, 3478, kTurnFalse, kStunTrue, kStunUsrName, kStunPwd); // STUN-UDP + tnet_ice_ctx_add_server(p_ice_ctx1, "tcp", kStunServerIP, 3478, kTurnTrue, kStunFalse, kStunUsrName, kStunPwd); // TURN-TCP + tnet_ice_ctx_add_server(p_ice_ctx2, "udp", kStunServerIP, 3478, kTurnFalse, kStunTrue, kStunUsrName, kStunPwd); // STUN-UDP + tnet_ice_ctx_add_server(p_ice_ctx2, "tcp", kStunServerIP, 3478, kTurnTrue, kStunFalse, kStunUsrName, kStunPwd); // TURN-TCP #endif - if ((ret = tnet_ice_ctx_start(p_ice_ctx1))) { - goto bail; - } - // start ctx2 when we finish gathering ctx1's candidates - //if ((ret = tnet_ice_ctx_start(p_ice_ctx2))) { - // goto bail; - //} - - getchar(); - - // ret = tnet_ice_ctx_stop(p_ice_ctx1); - // ret = tnet_ice_ctx_stop(p_ice_ctx2); + if ((ret = tnet_ice_ctx_start(p_ice_ctx1))) { + goto bail; + } + // start ctx2 when we finish gathering ctx1's candidates + //if ((ret = tnet_ice_ctx_start(p_ice_ctx2))) { + // goto bail; + //} + + getchar(); + + // ret = tnet_ice_ctx_stop(p_ice_ctx1); + // ret = tnet_ice_ctx_stop(p_ice_ctx2); bail: - TSK_OBJECT_SAFE_FREE(p_ice_ctx1); - TSK_OBJECT_SAFE_FREE(p_ice_ctx2); + TSK_OBJECT_SAFE_FREE(p_ice_ctx1); + TSK_OBJECT_SAFE_FREE(p_ice_ctx2); } diff --git a/tinyNET/test/test_ifaces.h b/tinyNET/test/test_ifaces.h index 2fd4e43..a1cf920 100755 --- a/tinyNET/test/test_ifaces.h +++ b/tinyNET/test/test_ifaces.h @@ -21,79 +21,73 @@ void test_faces_bestsource() { - tnet_ip_t source; + tnet_ip_t source; // IPv6 - if(!tnet_getbestsource("fe80::fe4c:3ea1", 5060, tnet_socket_type_udp_ipv6, &source)){ - TSK_DEBUG_INFO("Best IPv6 source is [%s]", source); - } - else{ - TSK_DEBUG_ERROR("Failed to get best IPv6 source."); - } + if(!tnet_getbestsource("fe80::fe4c:3ea1", 5060, tnet_socket_type_udp_ipv6, &source)) { + TSK_DEBUG_INFO("Best IPv6 source is [%s]", source); + } + else { + TSK_DEBUG_ERROR("Failed to get best IPv6 source."); + } // IPv6 - if(!tnet_getbestsource("2a01:e35:8b32:7050:212:f0ff:fe4c:3ea1", 5060, tnet_socket_type_udp_ipv6, &source)){ - TSK_DEBUG_INFO("Best IPv6 source is [%s]", source); - } - else{ - TSK_DEBUG_ERROR("Failed to get best IPv6 source."); - } - // IPv4 - if(!tnet_getbestsource("192.168.0.11", 5060, tnet_socket_type_udp_ipv4, &source)){ - TSK_DEBUG_INFO("Best IPv4 source is [%s]", source); - } - else{ - TSK_DEBUG_ERROR("Failed to get best IPv4 source."); - } + if(!tnet_getbestsource("2a01:e35:8b32:7050:212:f0ff:fe4c:3ea1", 5060, tnet_socket_type_udp_ipv6, &source)) { + TSK_DEBUG_INFO("Best IPv6 source is [%s]", source); + } + else { + TSK_DEBUG_ERROR("Failed to get best IPv6 source."); + } + // IPv4 + if(!tnet_getbestsource("192.168.0.11", 5060, tnet_socket_type_udp_ipv4, &source)) { + TSK_DEBUG_INFO("Best IPv4 source is [%s]", source); + } + else { + TSK_DEBUG_ERROR("Failed to get best IPv4 source."); + } } void test_ifaces_dump_ifaces() { - tnet_interfaces_L_t* ifaces = tnet_get_interfaces(); - tsk_list_item_t *item; + tnet_interfaces_L_t* ifaces = tnet_get_interfaces(); + tsk_list_item_t *item; - tsk_list_foreach(item, ifaces) - { - const tnet_interface_t *iface = item->data; - TSK_DEBUG_INFO("Interface: %s", iface->description); - } + tsk_list_foreach(item, ifaces) { + const tnet_interface_t *iface = item->data; + TSK_DEBUG_INFO("Interface: %s", iface->description); + } - TSK_OBJECT_SAFE_FREE(ifaces); + TSK_OBJECT_SAFE_FREE(ifaces); } void test_ifaces_dump_addresses() { - tnet_addresses_L_t* addresses = tnet_get_addresses_all(); - tsk_list_item_t *item; + tnet_addresses_L_t* addresses = tnet_get_addresses_all(); + tsk_list_item_t *item; - tsk_list_foreach(item, addresses) - { - const tnet_address_t *address = item->data; - if(address->anycast) - { - TSK_DEBUG_INFO("ANYCAST address: %s", address->ip); - } - else if(address->unicast) - { - TSK_DEBUG_INFO("UNICAST address: %s", address->ip); - } - else if(address->multicast) - { - TSK_DEBUG_INFO("MULTICAST address: %s", address->ip); - } - else if(address->dnsserver) - { - TSK_DEBUG_INFO("DNSSERVER address: %s", address->ip); - } - } + tsk_list_foreach(item, addresses) { + const tnet_address_t *address = item->data; + if(address->anycast) { + TSK_DEBUG_INFO("ANYCAST address: %s", address->ip); + } + else if(address->unicast) { + TSK_DEBUG_INFO("UNICAST address: %s", address->ip); + } + else if(address->multicast) { + TSK_DEBUG_INFO("MULTICAST address: %s", address->ip); + } + else if(address->dnsserver) { + TSK_DEBUG_INFO("DNSSERVER address: %s", address->ip); + } + } - TSK_OBJECT_SAFE_FREE(addresses); + TSK_OBJECT_SAFE_FREE(addresses); } void test_ifaces() { - test_faces_bestsource(); - test_ifaces_dump_ifaces(); - test_ifaces_dump_addresses(); + test_faces_bestsource(); + test_ifaces_dump_ifaces(); + test_ifaces_dump_addresses(); } #endif /* TNET_TEST_IFACES_H */ diff --git a/tinyNET/test/test_nat.h b/tinyNET/test/test_nat.h index 1a9bd68..c9723d0 100755 --- a/tinyNET/test/test_nat.h +++ b/tinyNET/test/test_nat.h @@ -27,55 +27,55 @@ void test_nat_stun() { - tnet_socket_t *socket1 = 0, *socket2 = 0; - struct tnet_nat_ctx_s *context = 0; + tnet_socket_t *socket1 = 0, *socket2 = 0; + struct tnet_nat_ctx_s *context = 0; - tnet_stun_binding_id_t bind_id1, bind_id2; + tnet_stun_binding_id_t bind_id1, bind_id2; - char* public_ip1 = 0, *public_ip2 = 0; - tnet_port_t public_port1 = 0, public_port2 = 0; - - if(!(socket1 = tnet_socket_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, STUN_SERVER_PROTO)) - || !(socket2 = tnet_socket_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, STUN_SERVER_PROTO))) { - goto bail; - } + char* public_ip1 = 0, *public_ip2 = 0; + tnet_port_t public_port1 = 0, public_port2 = 0; - context = tnet_nat_context_create(STUN_SERVER_PROTO, STUN_USERNAME, STUN_PASSWORD); + if(!(socket1 = tnet_socket_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, STUN_SERVER_PROTO)) + || !(socket2 = tnet_socket_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, STUN_SERVER_PROTO))) { + goto bail; + } - if(tnet_nat_set_server_address(context, STUN_SERVER_IP)) { - TSK_DEBUG_ERROR("Failed to set STUN/TURN address."); - goto bail; - } + context = tnet_nat_context_create(STUN_SERVER_PROTO, STUN_USERNAME, STUN_PASSWORD); - // == BIND == // - bind_id1 = tnet_nat_stun_bind(context, socket1->fd); - bind_id2 = tnet_nat_stun_bind(context, socket2->fd); + if(tnet_nat_set_server_address(context, STUN_SERVER_IP)) { + TSK_DEBUG_ERROR("Failed to set STUN/TURN address."); + goto bail; + } - if(!bind_id1 || !bind_id2) { - TSK_DEBUG_ERROR("Failed to get public IP/port using stun"); - goto bail; - } + // == BIND == // + bind_id1 = tnet_nat_stun_bind(context, socket1->fd); + bind_id2 = tnet_nat_stun_bind(context, socket2->fd); - if (tnet_nat_stun_get_reflexive_address(context, bind_id1, &public_ip1, &public_port1) == 0) { - TSK_DEBUG_INFO("Public IP1/Port1 ==> %s:%u", public_ip1, public_port1); - } + if(!bind_id1 || !bind_id2) { + TSK_DEBUG_ERROR("Failed to get public IP/port using stun"); + goto bail; + } - if (tnet_nat_stun_get_reflexive_address(context, bind_id2, &public_ip2, &public_port2) == 0) { - TSK_DEBUG_INFO("Public IP2/Port2 ==> %s:%u", public_ip2, public_port2); - } + if (tnet_nat_stun_get_reflexive_address(context, bind_id1, &public_ip1, &public_port1) == 0) { + TSK_DEBUG_INFO("Public IP1/Port1 ==> %s:%u", public_ip1, public_port1); + } - // == UNBIND == // - tnet_nat_stun_unbind(context, bind_id1); - tnet_nat_stun_unbind(context, bind_id2); + if (tnet_nat_stun_get_reflexive_address(context, bind_id2, &public_ip2, &public_port2) == 0) { + TSK_DEBUG_INFO("Public IP2/Port2 ==> %s:%u", public_ip2, public_port2); + } + + // == UNBIND == // + tnet_nat_stun_unbind(context, bind_id1); + tnet_nat_stun_unbind(context, bind_id2); bail: - TSK_OBJECT_SAFE_FREE(socket1); - TSK_OBJECT_SAFE_FREE(socket2); + TSK_OBJECT_SAFE_FREE(socket1); + TSK_OBJECT_SAFE_FREE(socket2); - TSK_FREE(public_ip1); - TSK_FREE(public_ip1); + TSK_FREE(public_ip1); + TSK_FREE(public_ip1); - TSK_OBJECT_SAFE_FREE(context); + TSK_OBJECT_SAFE_FREE(context); } void test_nat_turn() @@ -90,7 +90,7 @@ void test_nat_turn() // tnet_turn_channel_binding_id_t channel_id; // // int ret; -// +// // if(!(socket1 = tnet_socket_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, STUN_SERVER_PROTO)) // || !(socket2 = tnet_socket_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, STUN_SERVER_PROTO))) // { @@ -201,9 +201,9 @@ void test_nat_turn() void test_nat() { - test_nat_stun(); - //test_nat_turn(); - //tsk_thread_sleep(1000); + test_nat_stun(); + //test_nat_turn(); + //tsk_thread_sleep(1000); } diff --git a/tinyNET/test/test_sockets.h b/tinyNET/test/test_sockets.h index 3edd1bd..857ce6b 100755 --- a/tinyNET/test/test_sockets.h +++ b/tinyNET/test/test_sockets.h @@ -21,41 +21,40 @@ void test_sockets() { - int test; - tnet_socket_tcp_t * tcp_socket; - tnet_socket_type_t type = tnet_socket_type_udp_ipv4; - struct sockaddr_storage to; - - TNET_SOCKET_TYPE_SET_IPV4(type); - TNET_SOCKET_TYPE_SET_IPV6(type); - TNET_SOCKET_TYPE_SET_IPV4Only(type); - TNET_SOCKET_TYPE_SET_IPV6Only(type); - TNET_SOCKET_TYPE_SET_IPV6Only(type); - TNET_SOCKET_TYPE_SET_IPV4(type); - TNET_SOCKET_TYPE_SET_IPV6(type); - - TNET_SOCKET_TYPE_SET_TLS(type); - TNET_SOCKET_TYPE_SET_UDP(type); - TNET_SOCKET_TYPE_SET_SCTP(type); - TNET_SOCKET_TYPE_SET_TCP(type); - - tcp_socket = tnet_socket_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, type); - - if(!TNET_SOCKET_IS_VALID(tcp_socket)) - { - TSK_OBJECT_SAFE_FREE(tcp_socket); - return; - } - - //if(!(test = tnet_sockaddr_init("www.google.com", 80, type, &to))){ - // test = tnet_sockfd_connetto(tcp_socket->fd, (const struct sockaddr_storage *)&to); - //} - - if(!(test = tnet_sockaddr_init("ipv6.google.com", 80, type, &to))){ - test = tnet_sockfd_connectto(tcp_socket->fd, (const struct sockaddr_storage *)&to); - } - - TSK_OBJECT_SAFE_FREE(tcp_socket); + int test; + tnet_socket_tcp_t * tcp_socket; + tnet_socket_type_t type = tnet_socket_type_udp_ipv4; + struct sockaddr_storage to; + + TNET_SOCKET_TYPE_SET_IPV4(type); + TNET_SOCKET_TYPE_SET_IPV6(type); + TNET_SOCKET_TYPE_SET_IPV4Only(type); + TNET_SOCKET_TYPE_SET_IPV6Only(type); + TNET_SOCKET_TYPE_SET_IPV6Only(type); + TNET_SOCKET_TYPE_SET_IPV4(type); + TNET_SOCKET_TYPE_SET_IPV6(type); + + TNET_SOCKET_TYPE_SET_TLS(type); + TNET_SOCKET_TYPE_SET_UDP(type); + TNET_SOCKET_TYPE_SET_SCTP(type); + TNET_SOCKET_TYPE_SET_TCP(type); + + tcp_socket = tnet_socket_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, type); + + if(!TNET_SOCKET_IS_VALID(tcp_socket)) { + TSK_OBJECT_SAFE_FREE(tcp_socket); + return; + } + + //if(!(test = tnet_sockaddr_init("www.google.com", 80, type, &to))){ + // test = tnet_sockfd_connetto(tcp_socket->fd, (const struct sockaddr_storage *)&to); + //} + + if(!(test = tnet_sockaddr_init("ipv6.google.com", 80, type, &to))) { + test = tnet_sockfd_connectto(tcp_socket->fd, (const struct sockaddr_storage *)&to); + } + + TSK_OBJECT_SAFE_FREE(tcp_socket); } #endif /* TNET_TEST_SOCKETS_H */ diff --git a/tinyNET/test/test_stun.h b/tinyNET/test/test_stun.h index 58fc9b1..cc9033b 100755 --- a/tinyNET/test/test_stun.h +++ b/tinyNET/test/test_stun.h @@ -65,79 +65,79 @@ static int test_stun_buff_cmp(const uint8_t* pc_buf1_ptr, tsk_size_t n_buff1_siz static void test_stun_parser() { - tnet_stun_pkt_t* p_pkt = tsk_null; - tsk_size_t n_written_bytes, n_read_bytes; - static const char* __pc_mapped_addr_ipv4 = "192.168.0.37"; - static const char* __pc_mapped_addr_ipv6 = "fdf8:f53b:82e4::53"; - static const uint16_t __u_mapped_addr_port = 5060; - static const char __pc_username[] = "Mamadou DIOP"; - static const uint16_t __u_username = sizeof(__pc_username); - static const char __pc_integrity[TSK_SHA1_DIGEST_SIZE] = { 0 }; - static const uint16_t __u_integrity = sizeof(__pc_integrity); - static const uint32_t __u_fingerprint = 19831983; - static const uint8_t __u_error_class = 4; // (4 * 100) = 404 - static const uint8_t __u_error_number = 4; // + 4 = 404 - static const char* __pc_error_reason = "Not Found"; - tnet_stun_addr_t addr_ipv4, addr_ipv6; - static const char __pc_realm[] = "doubango.org"; - static const uint16_t __u_realm = sizeof(__pc_realm); - static const char __pc_nonce[128] = { 0 }; - static const uint16_t __u_nonce = sizeof(__pc_nonce); - static const char __pc_software[] = "tinyNET 2.0"; - static const uint16_t __u_software = sizeof(__pc_software); - static const uint32_t __u_life_time = 600; - static const uint32_t __u_req_transport = 17; // UDP + tnet_stun_pkt_t* p_pkt = tsk_null; + tsk_size_t n_written_bytes, n_read_bytes; + static const char* __pc_mapped_addr_ipv4 = "192.168.0.37"; + static const char* __pc_mapped_addr_ipv6 = "fdf8:f53b:82e4::53"; + static const uint16_t __u_mapped_addr_port = 5060; + static const char __pc_username[] = "Mamadou DIOP"; + static const uint16_t __u_username = sizeof(__pc_username); + static const char __pc_integrity[TSK_SHA1_DIGEST_SIZE] = { 0 }; + static const uint16_t __u_integrity = sizeof(__pc_integrity); + static const uint32_t __u_fingerprint = 19831983; + static const uint8_t __u_error_class = 4; // (4 * 100) = 404 + static const uint8_t __u_error_number = 4; // + 4 = 404 + static const char* __pc_error_reason = "Not Found"; + tnet_stun_addr_t addr_ipv4, addr_ipv6; + static const char __pc_realm[] = "doubango.org"; + static const uint16_t __u_realm = sizeof(__pc_realm); + static const char __pc_nonce[128] = { 0 }; + static const uint16_t __u_nonce = sizeof(__pc_nonce); + static const char __pc_software[] = "tinyNET 2.0"; + static const uint16_t __u_software = sizeof(__pc_software); + static const uint32_t __u_life_time = 600; + static const uint32_t __u_req_transport = 17; // UDP (n_read_bytes); - BAIL_IF_ERR(tnet_stun_pkt_create_empty(tnet_stun_pkt_type_binding_request, &p_pkt)); - BAIL_IF_ERR(tnet_stun_utils_inet_pton_v4(__pc_mapped_addr_ipv4, &addr_ipv4)); - BAIL_IF_ERR(tnet_stun_utils_inet_pton_v6(__pc_mapped_addr_ipv6, &addr_ipv6)); - BAIL_IF_ERR(tnet_stun_pkt_attrs_add(p_pkt, - TNET_STUN_PKT_ATTR_ADD_MAPPED_ADDRESS_V4(__u_mapped_addr_port, &addr_ipv4), - TNET_STUN_PKT_ATTR_ADD_MAPPED_ADDRESS_V6(__u_mapped_addr_port, &addr_ipv6), - TNET_STUN_PKT_ATTR_ADD_XOR_MAPPED_ADDRESS_V4(__u_mapped_addr_port, &addr_ipv4), - TNET_STUN_PKT_ATTR_ADD_XOR_MAPPED_ADDRESS_V6(__u_mapped_addr_port, &addr_ipv6), - TNET_STUN_PKT_ATTR_ADD_USERNAME_ZT(__pc_username), - TNET_STUN_PKT_ATTR_ADD_MESSAGE_INTEGRITY(__pc_integrity, __u_integrity), - TNET_STUN_PKT_ATTR_ADD_ERROR_CODE(__u_error_class, __u_error_number, __pc_error_reason), - TNET_STUN_PKT_ATTR_ADD_ERROR_CODE_TRY_ALTERNATE(), - TNET_STUN_PKT_ATTR_ADD_ERROR_CODE_BAD_REQUEST(), - TNET_STUN_PKT_ATTR_ADD_ERROR_CODE_UNAUTHORIZED(), - TNET_STUN_PKT_ATTR_ADD_ERROR_CODE_UNKNOWN_ATTRIBUTE(), - TNET_STUN_PKT_ATTR_ADD_ERROR_CODE_STALE_NONCE(), - TNET_STUN_PKT_ATTR_ADD_ERROR_CODE_SERVER_ERROR(), - TNET_STUN_PKT_ATTR_ADD_REALM_ZT(__pc_realm), - TNET_STUN_PKT_ATTR_ADD_NONCE(__pc_nonce, __u_nonce), - - TNET_STUN_PKT_ATTR_ADD_UNKNOWN_ATTRS( - TNET_STUN_PKT_ATTR_ADD_UNKNOWN_ATTRS_VAL(0x0001), // MAPPED-ADDRESS - TNET_STUN_PKT_ATTR_ADD_UNKNOWN_ATTRS_VAL(0x0006), // USERNAME - TNET_STUN_PKT_ATTR_ADD_UNKNOWN_ATTRS_VAL(0x0007), // PASSWORD - TNET_STUN_PKT_ATTR_ADD_NULL()), - - TNET_STUN_PKT_ATTR_ADD_SOFTWARE_ZT(__pc_software), - TNET_STUN_PKT_ATTR_ADD_ALTERNATE_SERVER_V4(__u_mapped_addr_port, &addr_ipv4), - TNET_STUN_PKT_ATTR_ADD_ALTERNATE_SERVER_V6(__u_mapped_addr_port, &addr_ipv6), - TNET_STUN_PKT_ATTR_ADD_LIFETIME(__u_life_time), - TNET_STUN_PKT_ATTR_ADD_REQUESTED_TRANSPORT(__u_req_transport), - TNET_STUN_PKT_ATTR_ADD_DONT_FRAGMENT(), - - TNET_STUN_PKT_ATTR_ADD_FINGERPRINT(__u_fingerprint), - TNET_STUN_PKT_ATTR_ADD_NULL())); - BAIL_IF_ERR(tnet_stun_pkt_write_with_padding(p_pkt, __parse_buff_write_ptr, __parse_buff_write_size, &n_written_bytes)); - TNET_TEST_STUN_SEND_BUFF(__parse_buff_write_ptr, n_written_bytes); - - TSK_OBJECT_SAFE_FREE(p_pkt); - BAIL_IF_ERR(tnet_stun_pkt_read(__parse_buff_write_ptr, n_written_bytes, &p_pkt)); - BAIL_IF_ERR(tnet_stun_pkt_write_with_padding(p_pkt, __parse_buff_read_ptr, __parse_buff_read_size, &n_read_bytes)); - //TNET_TEST_STUN_SEND_BUFF(__parse_buff_read_ptr, n_read_bytes); - - BAIL_IF_ERR(test_stun_buff_cmp(__parse_buff_write_ptr, n_written_bytes, __parse_buff_read_ptr, n_read_bytes)); + BAIL_IF_ERR(tnet_stun_pkt_create_empty(tnet_stun_pkt_type_binding_request, &p_pkt)); + BAIL_IF_ERR(tnet_stun_utils_inet_pton_v4(__pc_mapped_addr_ipv4, &addr_ipv4)); + BAIL_IF_ERR(tnet_stun_utils_inet_pton_v6(__pc_mapped_addr_ipv6, &addr_ipv6)); + BAIL_IF_ERR(tnet_stun_pkt_attrs_add(p_pkt, + TNET_STUN_PKT_ATTR_ADD_MAPPED_ADDRESS_V4(__u_mapped_addr_port, &addr_ipv4), + TNET_STUN_PKT_ATTR_ADD_MAPPED_ADDRESS_V6(__u_mapped_addr_port, &addr_ipv6), + TNET_STUN_PKT_ATTR_ADD_XOR_MAPPED_ADDRESS_V4(__u_mapped_addr_port, &addr_ipv4), + TNET_STUN_PKT_ATTR_ADD_XOR_MAPPED_ADDRESS_V6(__u_mapped_addr_port, &addr_ipv6), + TNET_STUN_PKT_ATTR_ADD_USERNAME_ZT(__pc_username), + TNET_STUN_PKT_ATTR_ADD_MESSAGE_INTEGRITY(__pc_integrity, __u_integrity), + TNET_STUN_PKT_ATTR_ADD_ERROR_CODE(__u_error_class, __u_error_number, __pc_error_reason), + TNET_STUN_PKT_ATTR_ADD_ERROR_CODE_TRY_ALTERNATE(), + TNET_STUN_PKT_ATTR_ADD_ERROR_CODE_BAD_REQUEST(), + TNET_STUN_PKT_ATTR_ADD_ERROR_CODE_UNAUTHORIZED(), + TNET_STUN_PKT_ATTR_ADD_ERROR_CODE_UNKNOWN_ATTRIBUTE(), + TNET_STUN_PKT_ATTR_ADD_ERROR_CODE_STALE_NONCE(), + TNET_STUN_PKT_ATTR_ADD_ERROR_CODE_SERVER_ERROR(), + TNET_STUN_PKT_ATTR_ADD_REALM_ZT(__pc_realm), + TNET_STUN_PKT_ATTR_ADD_NONCE(__pc_nonce, __u_nonce), + + TNET_STUN_PKT_ATTR_ADD_UNKNOWN_ATTRS( + TNET_STUN_PKT_ATTR_ADD_UNKNOWN_ATTRS_VAL(0x0001), // MAPPED-ADDRESS + TNET_STUN_PKT_ATTR_ADD_UNKNOWN_ATTRS_VAL(0x0006), // USERNAME + TNET_STUN_PKT_ATTR_ADD_UNKNOWN_ATTRS_VAL(0x0007), // PASSWORD + TNET_STUN_PKT_ATTR_ADD_NULL()), + + TNET_STUN_PKT_ATTR_ADD_SOFTWARE_ZT(__pc_software), + TNET_STUN_PKT_ATTR_ADD_ALTERNATE_SERVER_V4(__u_mapped_addr_port, &addr_ipv4), + TNET_STUN_PKT_ATTR_ADD_ALTERNATE_SERVER_V6(__u_mapped_addr_port, &addr_ipv6), + TNET_STUN_PKT_ATTR_ADD_LIFETIME(__u_life_time), + TNET_STUN_PKT_ATTR_ADD_REQUESTED_TRANSPORT(__u_req_transport), + TNET_STUN_PKT_ATTR_ADD_DONT_FRAGMENT(), + + TNET_STUN_PKT_ATTR_ADD_FINGERPRINT(__u_fingerprint), + TNET_STUN_PKT_ATTR_ADD_NULL())); + BAIL_IF_ERR(tnet_stun_pkt_write_with_padding(p_pkt, __parse_buff_write_ptr, __parse_buff_write_size, &n_written_bytes)); + TNET_TEST_STUN_SEND_BUFF(__parse_buff_write_ptr, n_written_bytes); + + TSK_OBJECT_SAFE_FREE(p_pkt); + BAIL_IF_ERR(tnet_stun_pkt_read(__parse_buff_write_ptr, n_written_bytes, &p_pkt)); + BAIL_IF_ERR(tnet_stun_pkt_write_with_padding(p_pkt, __parse_buff_read_ptr, __parse_buff_read_size, &n_read_bytes)); + //TNET_TEST_STUN_SEND_BUFF(__parse_buff_read_ptr, n_read_bytes); + + BAIL_IF_ERR(test_stun_buff_cmp(__parse_buff_write_ptr, n_written_bytes, __parse_buff_read_ptr, n_read_bytes)); TSK_DEBUG_INFO("test_stun_parser...OK"); bail: - TSK_OBJECT_SAFE_FREE(p_pkt); + TSK_OBJECT_SAFE_FREE(p_pkt); } static struct tnet_turn_session_s* __pc_ss1 = tsk_null; @@ -153,103 +153,95 @@ static tnet_turn_peer_id_t __u_peer_id2 = kTurnPeerIdInvalid; static int _test_turn_session_callback(const struct tnet_turn_session_event_xs *e) { - const struct tnet_turn_session_s* pc_ss = (const struct tnet_turn_session_s*)e->pc_usr_data; - int ret = 0; - switch (e->e_type) { - case tnet_turn_session_event_type_alloc_ok: - { - uint16_t *pu_port = (pc_ss == __pc_ss2) ? &__u_rel_port_ss1 : &__u_rel_port_ss2; - char** pp_ip = (pc_ss == __pc_ss2) ? &__p_rel_ip_ss1 : &__p_rel_ip_ss2; - tsk_bool_t *pb_ipv6 = (pc_ss == __pc_ss2) ? &__b_rel_ipv6_ss1 : &__b_rel_ipv6_ss2; - tnet_turn_peer_id_t *pu_peer_id = (pc_ss == __pc_ss2) ? &__u_peer_id2 : &__u_peer_id1; - - BAIL_IF_ERR(tnet_turn_session_get_relayed_addr(pc_ss, pp_ip, pu_port, pb_ipv6)); - // BAIL_IF_ERR(tnet_turn_session_get_srflx_addr(pc_ss, pu_port, &u_port, &b_ipv6)); // get my own server reflexive address (in order to send data to myself) - BAIL_IF_ERR(tnet_turn_session_createpermission((struct tnet_turn_session_s*)pc_ss, *pp_ip, *pu_port, pu_peer_id)); // Input = ADDR(remote.candidate.relay) - break; - } - case tnet_turn_session_event_type_alloc_nok: - { - TSK_DEBUG_INFO("*** TURN ALLOC NOK ***"); - break; - } - case tnet_turn_session_event_type_perm_ok: - { - static const char __pc_data[] = { "TURN Sample Data (Send Indication)" }; - int i; - tnet_turn_peer_id_t u_peer_id = (pc_ss == __pc_ss2) ? __u_peer_id2 : __u_peer_id1; - // Bind a channel (not required). If succeed, will be used to save data. - tnet_turn_session_chanbind((struct tnet_turn_session_s*)pc_ss, u_peer_id); - // Send data (will use channel if one is active. Otherwise (no channel), SendIndications will be used) - for (i = 0; i < 10; ++i) { - BAIL_IF_ERR(tnet_turn_session_send_data((struct tnet_turn_session_s*)pc_ss, u_peer_id, __pc_data, sizeof(__pc_data))); - } - break; - } - case tnet_turn_session_event_type_perm_nok: - { - TSK_DEBUG_INFO("*** TURN PERM NOK ***"); - break; - } - case tnet_turn_session_event_type_chanbind_ok: - { - static const char __pc_data[] = { "TURN Sample Data (ChannelData)" }; - int i; - tnet_turn_peer_id_t u_peer_id = (pc_ss == __pc_ss2) ? __u_peer_id2 : __u_peer_id1; - for (i = 0; i < 10; ++i) { - BAIL_IF_ERR(tnet_turn_session_send_data((struct tnet_turn_session_s*)pc_ss, u_peer_id, __pc_data, sizeof(__pc_data))); - } - break; - } - case tnet_turn_session_event_type_chanbind_nok: - { - TSK_DEBUG_INFO("*** TURN CHANBIND NOK ***"); - break; - } - case tnet_turn_session_event_type_recv_data: - { - TSK_DEBUG_INFO("RECV DATA:%.*s", e->data.u_data_size, (const char*)e->data.pc_data_ptr); - break; - } - default: - { - break; - } - } + const struct tnet_turn_session_s* pc_ss = (const struct tnet_turn_session_s*)e->pc_usr_data; + int ret = 0; + switch (e->e_type) { + case tnet_turn_session_event_type_alloc_ok: { + uint16_t *pu_port = (pc_ss == __pc_ss2) ? &__u_rel_port_ss1 : &__u_rel_port_ss2; + char** pp_ip = (pc_ss == __pc_ss2) ? &__p_rel_ip_ss1 : &__p_rel_ip_ss2; + tsk_bool_t *pb_ipv6 = (pc_ss == __pc_ss2) ? &__b_rel_ipv6_ss1 : &__b_rel_ipv6_ss2; + tnet_turn_peer_id_t *pu_peer_id = (pc_ss == __pc_ss2) ? &__u_peer_id2 : &__u_peer_id1; + + BAIL_IF_ERR(tnet_turn_session_get_relayed_addr(pc_ss, pp_ip, pu_port, pb_ipv6)); + // BAIL_IF_ERR(tnet_turn_session_get_srflx_addr(pc_ss, pu_port, &u_port, &b_ipv6)); // get my own server reflexive address (in order to send data to myself) + BAIL_IF_ERR(tnet_turn_session_createpermission((struct tnet_turn_session_s*)pc_ss, *pp_ip, *pu_port, pu_peer_id)); // Input = ADDR(remote.candidate.relay) + break; + } + case tnet_turn_session_event_type_alloc_nok: { + TSK_DEBUG_INFO("*** TURN ALLOC NOK ***"); + break; + } + case tnet_turn_session_event_type_perm_ok: { + static const char __pc_data[] = { "TURN Sample Data (Send Indication)" }; + int i; + tnet_turn_peer_id_t u_peer_id = (pc_ss == __pc_ss2) ? __u_peer_id2 : __u_peer_id1; + // Bind a channel (not required). If succeed, will be used to save data. + tnet_turn_session_chanbind((struct tnet_turn_session_s*)pc_ss, u_peer_id); + // Send data (will use channel if one is active. Otherwise (no channel), SendIndications will be used) + for (i = 0; i < 10; ++i) { + BAIL_IF_ERR(tnet_turn_session_send_data((struct tnet_turn_session_s*)pc_ss, u_peer_id, __pc_data, sizeof(__pc_data))); + } + break; + } + case tnet_turn_session_event_type_perm_nok: { + TSK_DEBUG_INFO("*** TURN PERM NOK ***"); + break; + } + case tnet_turn_session_event_type_chanbind_ok: { + static const char __pc_data[] = { "TURN Sample Data (ChannelData)" }; + int i; + tnet_turn_peer_id_t u_peer_id = (pc_ss == __pc_ss2) ? __u_peer_id2 : __u_peer_id1; + for (i = 0; i < 10; ++i) { + BAIL_IF_ERR(tnet_turn_session_send_data((struct tnet_turn_session_s*)pc_ss, u_peer_id, __pc_data, sizeof(__pc_data))); + } + break; + } + case tnet_turn_session_event_type_chanbind_nok: { + TSK_DEBUG_INFO("*** TURN CHANBIND NOK ***"); + break; + } + case tnet_turn_session_event_type_recv_data: { + TSK_DEBUG_INFO("RECV DATA:%.*s", e->data.u_data_size, (const char*)e->data.pc_data_ptr); + break; + } + default: { + break; + } + } bail: - return ret; + return ret; } static void test_turn_session() { - BAIL_IF_ERR(tnet_turn_session_create_udp_ipv4(tnet_turn_transport_udp, kStunServerIP, kStunServerPort, &__pc_ss1)); - BAIL_IF_ERR(tnet_turn_session_set_callback(__pc_ss1, _test_turn_session_callback, __pc_ss1)); - BAIL_IF_ERR(tnet_turn_session_set_cred(__pc_ss1, kStunUsrName, kStunPwd)); - BAIL_IF_ERR(tnet_turn_session_prepare(__pc_ss1)); - BAIL_IF_ERR(tnet_turn_session_start(__pc_ss1)); + BAIL_IF_ERR(tnet_turn_session_create_udp_ipv4(tnet_turn_transport_udp, kStunServerIP, kStunServerPort, &__pc_ss1)); + BAIL_IF_ERR(tnet_turn_session_set_callback(__pc_ss1, _test_turn_session_callback, __pc_ss1)); + BAIL_IF_ERR(tnet_turn_session_set_cred(__pc_ss1, kStunUsrName, kStunPwd)); + BAIL_IF_ERR(tnet_turn_session_prepare(__pc_ss1)); + BAIL_IF_ERR(tnet_turn_session_start(__pc_ss1)); - BAIL_IF_ERR(tnet_turn_session_create_udp_ipv4(tnet_turn_transport_udp, kStunServerIP, kStunServerPort, &__pc_ss2)); - BAIL_IF_ERR(tnet_turn_session_set_callback(__pc_ss2, _test_turn_session_callback, __pc_ss2)); - BAIL_IF_ERR(tnet_turn_session_set_cred(__pc_ss2, kStunUsrName, kStunPwd)); - BAIL_IF_ERR(tnet_turn_session_prepare(__pc_ss2)); - BAIL_IF_ERR(tnet_turn_session_start(__pc_ss2)); + BAIL_IF_ERR(tnet_turn_session_create_udp_ipv4(tnet_turn_transport_udp, kStunServerIP, kStunServerPort, &__pc_ss2)); + BAIL_IF_ERR(tnet_turn_session_set_callback(__pc_ss2, _test_turn_session_callback, __pc_ss2)); + BAIL_IF_ERR(tnet_turn_session_set_cred(__pc_ss2, kStunUsrName, kStunPwd)); + BAIL_IF_ERR(tnet_turn_session_prepare(__pc_ss2)); + BAIL_IF_ERR(tnet_turn_session_start(__pc_ss2)); - BAIL_IF_ERR(tnet_turn_session_allocate(__pc_ss1)); - BAIL_IF_ERR(tnet_turn_session_allocate(__pc_ss2)); + BAIL_IF_ERR(tnet_turn_session_allocate(__pc_ss1)); + BAIL_IF_ERR(tnet_turn_session_allocate(__pc_ss2)); - TSK_DEBUG_INFO("*** Press ENTER to continue ***"); - getchar(); + TSK_DEBUG_INFO("*** Press ENTER to continue ***"); + getchar(); bail: - TSK_OBJECT_SAFE_FREE(__pc_ss1); - TSK_OBJECT_SAFE_FREE(__pc_ss2); + TSK_OBJECT_SAFE_FREE(__pc_ss1); + TSK_OBJECT_SAFE_FREE(__pc_ss2); } static void test_stun() { - //test_stun_parser(); - test_turn_session(); + //test_stun_parser(); + test_turn_session(); } #endif /* TNET_TEST_STUN_H */ diff --git a/tinyNET/test/test_tls.h b/tinyNET/test/test_tls.h index 48ae865..5b1661e 100755 --- a/tinyNET/test/test_tls.h +++ b/tinyNET/test/test_tls.h @@ -36,69 +36,66 @@ static int tnet_tls_cb(const tnet_transport_event_t* e) { - switch(e->type){ - case event_data: - { - TSK_DEBUG_INFO("--- TLS ---\n%s\n", (const char*)e->data); - break; - } - case event_closed: - case event_connected: - default: - { - break; - } - } - return 0; + switch(e->type) { + case event_data: { + TSK_DEBUG_INFO("--- TLS ---\n%s\n", (const char*)e->data); + break; + } + case event_closed: + case event_connected: + default: { + break; + } + } + return 0; } void test_tls() { - tnet_transport_handle_t *transport = tnet_transport_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, tnet_socket_type_tls_ipv4, "TLS/IPV4 TRANSPORT"); - - tnet_ip_t ip; - tnet_port_t port; - tnet_fd_t fd = TNET_INVALID_FD; - - if(tnet_transport_start(transport)){ - TSK_DEBUG_ERROR("Failed to create %s.", tnet_transport_get_description(transport)); - return; - } - - /* Set our callback function */ - tnet_transport_set_callback(transport, tnet_tls_cb, "callbackdata"); - - - - /* Connect to the SIP Registrar */ - if((fd = tnet_transport_connectto_2(transport, TEST_TLS_REMOTE_IP, TEST_TLS_REMOTE_PORT)) == TNET_INVALID_FD){ - TSK_DEBUG_ERROR("Failed to connect %s.", tnet_transport_get_description(transport)); - return; - } - - if(tnet_sockfd_waitUntilWritable(fd, TNET_CONNECT_TIMEOUT)){ - TSK_DEBUG_ERROR("%d milliseconds elapsed and the socket is still not connected.", TNET_CONNECT_TIMEOUT); - tnet_transport_remove_socket(transport, &fd); - return; - } - - /* Send our SIP message */ - { - char* message = 0; - tnet_transport_get_ip_n_port(transport, fd, &ip, &port); - tsk_sprintf(&message, TLS_TEST_SIP_MESSAGE, "TLS", ip, port, port, ip, port, "tls"); - - if(!tnet_transport_send(transport, fd, message, strlen(message))) - { - TSK_DEBUG_ERROR("Failed to send data using TCP/IPv4 transport."); - TSK_FREE(message); - return; - } - TSK_FREE(message); - } - - TSK_OBJECT_SAFE_FREE(transport); + tnet_transport_handle_t *transport = tnet_transport_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, tnet_socket_type_tls_ipv4, "TLS/IPV4 TRANSPORT"); + + tnet_ip_t ip; + tnet_port_t port; + tnet_fd_t fd = TNET_INVALID_FD; + + if(tnet_transport_start(transport)) { + TSK_DEBUG_ERROR("Failed to create %s.", tnet_transport_get_description(transport)); + return; + } + + /* Set our callback function */ + tnet_transport_set_callback(transport, tnet_tls_cb, "callbackdata"); + + + + /* Connect to the SIP Registrar */ + if((fd = tnet_transport_connectto_2(transport, TEST_TLS_REMOTE_IP, TEST_TLS_REMOTE_PORT)) == TNET_INVALID_FD) { + TSK_DEBUG_ERROR("Failed to connect %s.", tnet_transport_get_description(transport)); + return; + } + + if(tnet_sockfd_waitUntilWritable(fd, TNET_CONNECT_TIMEOUT)) { + TSK_DEBUG_ERROR("%d milliseconds elapsed and the socket is still not connected.", TNET_CONNECT_TIMEOUT); + tnet_transport_remove_socket(transport, &fd); + return; + } + + /* Send our SIP message */ + { + char* message = 0; + tnet_transport_get_ip_n_port(transport, fd, &ip, &port); + tsk_sprintf(&message, TLS_TEST_SIP_MESSAGE, "TLS", ip, port, port, ip, port, "tls"); + + if(!tnet_transport_send(transport, fd, message, strlen(message))) { + TSK_DEBUG_ERROR("Failed to send data using TCP/IPv4 transport."); + TSK_FREE(message); + return; + } + TSK_FREE(message); + } + + TSK_OBJECT_SAFE_FREE(transport); } #endif /* TNET_TEST_TLS_H */ diff --git a/tinyNET/test/test_transport.h b/tinyNET/test/test_transport.h index b52b554..0384546 100755 --- a/tinyNET/test/test_transport.h +++ b/tinyNET/test/test_transport.h @@ -53,133 +53,129 @@ static int tnet_tcp_cb(const tnet_transport_event_t* e) { - switch(e->type){ - case event_data: - { - TSK_DEBUG_INFO("--- TCP ---\n%s\n", (const char*)e->data); - break; - } - case event_closed: - case event_connected: - default: - { - break; - } - } - return 0; + switch(e->type) { + case event_data: { + TSK_DEBUG_INFO("--- TCP ---\n%s\n", (const char*)e->data); + break; + } + case event_closed: + case event_connected: + default: { + break; + } + } + return 0; } static int tnet_udp_cb(const tnet_transport_event_t* e) { - switch(e->type){ - case event_data: - { - TSK_DEBUG_INFO("--- UDP ---\n%s\n", (const char*)e->data); - break; - } - case event_closed: - case event_connected: - default: break; - - } - return 0; + switch(e->type) { + case event_data: { + TSK_DEBUG_INFO("--- UDP ---\n%s\n", (const char*)e->data); + break; + } + case event_closed: + case event_connected: + default: + break; + + } + return 0; } void test_transport_tcp_ipv4(tnet_transport_handle_t *transport) { - //tnet_socket_type_t type = tnet_socket_type_tcp_ipv4; - tnet_ip_t ip; - tnet_port_t port; - tnet_fd_t fd = TNET_INVALID_FD; - - /* Set our callback function */ - tnet_transport_set_callback(transport, tnet_tcp_cb, "callbackdata"); - - if(tnet_transport_start(transport)){ - TSK_DEBUG_ERROR("Failed to create %s.", tnet_transport_get_description(transport)); - return; - } - - /* Connect to the SIP Registrar */ - if((fd = tnet_transport_connectto_2(transport, REMOTE_IP, REMOTE_PORT)) == TNET_INVALID_FD){ - TSK_DEBUG_ERROR("Failed to connect %s.", tnet_transport_get_description(transport)); - return; - } - - if(tnet_sockfd_waitUntilWritable(fd, TNET_CONNECT_TIMEOUT)){ - TSK_DEBUG_ERROR("%d milliseconds elapsed and the socket is still not connected.", TNET_CONNECT_TIMEOUT); - tnet_transport_remove_socket(transport, &fd); - return; - } - - - /* Send our SIP message */ - { - char* message = 0; - tnet_transport_get_ip_n_port(transport, fd, &ip, &port); - tsk_sprintf(&message, SIP_MESSAGE, "TCP", ip, port, port, ip, port, "tcp"); - - if(!tnet_transport_send(transport, fd, message, strlen(message))) - { - TSK_DEBUG_ERROR("Failed to send data using %s.", tnet_transport_get_description(transport)); - TSK_FREE(message); - return; - } - TSK_FREE(message); - } - + //tnet_socket_type_t type = tnet_socket_type_tcp_ipv4; + tnet_ip_t ip; + tnet_port_t port; + tnet_fd_t fd = TNET_INVALID_FD; + + /* Set our callback function */ + tnet_transport_set_callback(transport, tnet_tcp_cb, "callbackdata"); + + if(tnet_transport_start(transport)) { + TSK_DEBUG_ERROR("Failed to create %s.", tnet_transport_get_description(transport)); + return; + } + + /* Connect to the SIP Registrar */ + if((fd = tnet_transport_connectto_2(transport, REMOTE_IP, REMOTE_PORT)) == TNET_INVALID_FD) { + TSK_DEBUG_ERROR("Failed to connect %s.", tnet_transport_get_description(transport)); + return; + } + + if(tnet_sockfd_waitUntilWritable(fd, TNET_CONNECT_TIMEOUT)) { + TSK_DEBUG_ERROR("%d milliseconds elapsed and the socket is still not connected.", TNET_CONNECT_TIMEOUT); + tnet_transport_remove_socket(transport, &fd); + return; + } + + + /* Send our SIP message */ + { + char* message = 0; + tnet_transport_get_ip_n_port(transport, fd, &ip, &port); + tsk_sprintf(&message, SIP_MESSAGE, "TCP", ip, port, port, ip, port, "tcp"); + + if(!tnet_transport_send(transport, fd, message, strlen(message))) { + TSK_DEBUG_ERROR("Failed to send data using %s.", tnet_transport_get_description(transport)); + TSK_FREE(message); + return; + } + TSK_FREE(message); + } + } int test_transport_udp_ipv4(tnet_transport_handle_t *transport) { - //tnet_socket_type_t type = tnet_socket_type_udp_ipv4; - tnet_ip_t ip; - tnet_port_t port; - tnet_fd_t fd = TNET_INVALID_FD; - - /* Set our callback function */ - tnet_transport_set_callback(transport, tnet_udp_cb, "callbackdata"); - - if(tnet_transport_start(transport)){ - TSK_DEBUG_ERROR("Failed to create %s.", tnet_transport_get_description(transport)); - return -1; - } - - /* Connect to our SIP REGISTRAR */ - if((fd = tnet_transport_connectto_2(transport, REMOTE_IP, REMOTE_PORT)) == TNET_INVALID_FD){ - TSK_DEBUG_ERROR("Failed to connect %s.", tnet_transport_get_description(transport)); - //tnet_transport_shutdown(transport); - return -2; - } - - if(tnet_sockfd_waitUntilWritable(fd, TNET_CONNECT_TIMEOUT)){ - TSK_DEBUG_ERROR("%d milliseconds elapsed and the socket is still not connected.", TNET_CONNECT_TIMEOUT); - tnet_transport_remove_socket(transport, &fd); - return -3; - } - - //tsk_thread_sleep(2000); - - /* Send our SIP message */ - /*while(1)*/{ - char* message = 0; - tnet_transport_get_ip_n_port(transport, fd, &ip, &port); - //memset(ip, 0, sizeof(ip)); - //memcpy(ip, "192.168.0.12", 12); - tsk_sprintf(&message, SIP_MESSAGE, "UDP", ip, port, port, ip, port, "udp"); - - if(!tnet_transport_send(transport, fd, message, strlen(message))) - { - TSK_DEBUG_ERROR("Failed to send data using %s.", tnet_transport_get_description(transport)); - //tnet_transport_shutdown(transport); - TSK_FREE(message); - return -4; - } - TSK_FREE(message); - } - - return 0; + //tnet_socket_type_t type = tnet_socket_type_udp_ipv4; + tnet_ip_t ip; + tnet_port_t port; + tnet_fd_t fd = TNET_INVALID_FD; + + /* Set our callback function */ + tnet_transport_set_callback(transport, tnet_udp_cb, "callbackdata"); + + if(tnet_transport_start(transport)) { + TSK_DEBUG_ERROR("Failed to create %s.", tnet_transport_get_description(transport)); + return -1; + } + + /* Connect to our SIP REGISTRAR */ + if((fd = tnet_transport_connectto_2(transport, REMOTE_IP, REMOTE_PORT)) == TNET_INVALID_FD) { + TSK_DEBUG_ERROR("Failed to connect %s.", tnet_transport_get_description(transport)); + //tnet_transport_shutdown(transport); + return -2; + } + + if(tnet_sockfd_waitUntilWritable(fd, TNET_CONNECT_TIMEOUT)) { + TSK_DEBUG_ERROR("%d milliseconds elapsed and the socket is still not connected.", TNET_CONNECT_TIMEOUT); + tnet_transport_remove_socket(transport, &fd); + return -3; + } + + //tsk_thread_sleep(2000); + + /* Send our SIP message */ + /*while(1)*/{ + char* message = 0; + tnet_transport_get_ip_n_port(transport, fd, &ip, &port); + //memset(ip, 0, sizeof(ip)); + //memcpy(ip, "192.168.0.12", 12); + tsk_sprintf(&message, SIP_MESSAGE, "UDP", ip, port, port, ip, port, "udp"); + + if(!tnet_transport_send(transport, fd, message, strlen(message))) { + TSK_DEBUG_ERROR("Failed to send data using %s.", tnet_transport_get_description(transport)); + //tnet_transport_shutdown(transport); + TSK_FREE(message); + return -4; + } + TSK_FREE(message); + } + + return 0; } void test_transport() @@ -189,27 +185,27 @@ void test_transport() #if TEST_UDP - tnet_transport_handle_t *udp = tnet_transport_create(LOCAL_IP4, LOCAL_PORT, tnet_socket_type_udp_ipv4, "UDP/IPV4 TRANSPORT"); - test_transport_udp_ipv4(udp); + tnet_transport_handle_t *udp = tnet_transport_create(LOCAL_IP4, LOCAL_PORT, tnet_socket_type_udp_ipv4, "UDP/IPV4 TRANSPORT"); + test_transport_udp_ipv4(udp); #endif #if TEST_TCP - tnet_transport_handle_t *tcp = tnet_transport_create(LOCAL_IP4, LOCAL_PORT, tnet_socket_type_tcp_ipv4, "TCP/IPV4 TRANSPORT"); - test_transport_tcp_ipv4(tcp); -#endif + tnet_transport_handle_t *tcp = tnet_transport_create(LOCAL_IP4, LOCAL_PORT, tnet_socket_type_tcp_ipv4, "TCP/IPV4 TRANSPORT"); + test_transport_tcp_ipv4(tcp); +#endif //#if defined(ANDROID) - tsk_thread_sleep(1000000); + tsk_thread_sleep(1000000); //#else - getchar(); + getchar(); //#endif #if TEST_UDP - TSK_OBJECT_SAFE_FREE(udp); + TSK_OBJECT_SAFE_FREE(udp); #endif #if TEST_TCP - TSK_OBJECT_SAFE_FREE(tcp); + TSK_OBJECT_SAFE_FREE(tcp); #endif } |