From 1ebf5a5fcda0c9154e22ed02404fd46525a7fd9f Mon Sep 17 00:00:00 2001 From: bossiel Date: Wed, 10 Aug 2011 22:59:15 +0000 Subject: Move deprecated v1.0 from trunk to branches --- tinyNET/test/droid-makefile | 28 +++ tinyNET/test/stdafx.c | 26 +++ tinyNET/test/stdafx.h | 38 ++++ tinyNET/test/targetver.h | 15 ++ tinyNET/test/test.c | 116 ++++++++++++ tinyNET/test/test.vcproj | 414 ++++++++++++++++++++++++++++++++++++++++++ tinyNET/test/test_auth.h | 31 ++++ tinyNET/test/test_dhcp.h | 121 ++++++++++++ tinyNET/test/test_dhcp6.h | 51 ++++++ tinyNET/test/test_dns.h | 227 +++++++++++++++++++++++ tinyNET/test/test_ifaces.h | 102 +++++++++++ tinyNET/test/test_nat.h | 219 ++++++++++++++++++++++ tinyNET/test/test_sockets.h | 64 +++++++ tinyNET/test/test_stun.h | 112 ++++++++++++ tinyNET/test/test_tls.h | 108 +++++++++++ tinyNET/test/test_transport.h | 222 ++++++++++++++++++++++ 16 files changed, 1894 insertions(+) create mode 100644 tinyNET/test/droid-makefile create mode 100644 tinyNET/test/stdafx.c create mode 100644 tinyNET/test/stdafx.h create mode 100644 tinyNET/test/targetver.h create mode 100644 tinyNET/test/test.c create mode 100644 tinyNET/test/test.vcproj create mode 100644 tinyNET/test/test_auth.h create mode 100644 tinyNET/test/test_dhcp.h create mode 100644 tinyNET/test/test_dhcp6.h create mode 100644 tinyNET/test/test_dns.h create mode 100644 tinyNET/test/test_ifaces.h create mode 100644 tinyNET/test/test_nat.h create mode 100644 tinyNET/test/test_sockets.h create mode 100644 tinyNET/test/test_stun.h create mode 100644 tinyNET/test/test_tls.h create mode 100644 tinyNET/test/test_transport.h (limited to 'tinyNET/test') diff --git a/tinyNET/test/droid-makefile b/tinyNET/test/droid-makefile new file mode 100644 index 0000000..26651a2 --- /dev/null +++ b/tinyNET/test/droid-makefile @@ -0,0 +1,28 @@ +APP := test + +CFLAGS := $(CFLAGS_COMMON) -I../src -I../../tinySAK/src +LDFLAGS := $(LDFLAGS_COMMON) -Wl,-Bsymbolic,--whole-archive -l$(PROJECT) -ltinySAK -Wl,--entry=main + +all: $(APP) + +OBJS += $(APP).o + +$(APP): $(OBJS) + $(CC) $(LDFLAGS) -o $@ $^ + +%.o: %.c + $(CC) -c $(INCLUDE) $(CFLAGS) $< -o $@ + +install: $(APP) + $(ANDROID_SDK_ROOT)/tools/adb push $(APP) $(EXEC_DIR)/$(APP) + $(ANDROID_SDK_ROOT)/tools/adb shell chmod 777 $(EXEC_DIR)/$(APP) + +run: + $(ANDROID_SDK_ROOT)/tools/adb shell $(EXEC_DIR)/$(APP) + +#dbg: +# $(MAKE) $(MAKEFILE) DEBUG="-g -DDEBUG" +# $(MAKE) $(MAKEFILE) install + +clean: + @rm -f $(OBJS) $(APP) \ No newline at end of file diff --git a/tinyNET/test/stdafx.c b/tinyNET/test/stdafx.c new file mode 100644 index 0000000..df71e1b --- /dev/null +++ b/tinyNET/test/stdafx.c @@ -0,0 +1,26 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ + +#include "stdafx.h" + +// TODO: reference any additional headers you need in STDAFX.H +// and not in this file diff --git a/tinyNET/test/stdafx.h b/tinyNET/test/stdafx.h new file mode 100644 index 0000000..b1200be --- /dev/null +++ b/tinyNET/test/stdafx.h @@ -0,0 +1,38 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ + +#ifndef TEST_TNET_STDAFX_H +#define TEST_TNET_STDAFX_H + +#ifdef WIN32 +#include "targetver.h" +#endif + +#include +#include +//#include + + + +// TODO: reference additional headers your program requires here + +#endif /* TEST_TNET_STDAFX_H */ diff --git a/tinyNET/test/targetver.h b/tinyNET/test/targetver.h new file mode 100644 index 0000000..08a10a4 --- /dev/null +++ b/tinyNET/test/targetver.h @@ -0,0 +1,15 @@ +#ifndef TNET_TEST_TARGETVER_H +#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 +// including the version specified. + +// Modify the following defines if you have to target a platform prior to the ones specified below. +// Refer to MSDN for the latest info on corresponding values for different platforms. +#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista. +#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows. +#endif + +#endif /* TNET_TEST_TARGETVER_H */ \ No newline at end of file diff --git a/tinyNET/test/test.c b/tinyNET/test/test.c new file mode 100644 index 0000000..f27e05c --- /dev/null +++ b/tinyNET/test/test.c @@ -0,0 +1,116 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ +#include "stdafx.h" + +#include "tsk.h" + +#include "tinynet.h" + +#include "test_sockets.h" +#include "test_transport.h" +#include "test_auth.h" +#include "test_stun.h" +#include "test_nat.h" +#include "test_ifaces.h" +#include "test_dns.h" +#include "test_dhcp.h" +#include "test_dhcp6.h" +#include "test_tls.h" + +#define RUN_TEST_LOOP 1 + +#define RUN_TEST_ALL 0 +#define RUN_TEST_SOCKETS 0 /* FIXME: Android */ +#define RUN_TEST_TRANSPORT 0 +#define RUN_TEST_AUTH 0 +#define RUN_TEST_STUN 0 +#define RUN_TEST_NAT 1 +#define RUN_TEST_IFACES 0 +#define RUN_TEST_DNS 0 +#define RUN_TEST_DHCP 0 +#define RUN_TEST_DHCP6 0 +#define RUN_TEST_TLS 0 + +#ifdef _WIN32_WCE +int _tmain(int argc, _TCHAR* argv[]) +#else +int main() +#endif +{ + /* Startup the network stack. */ + if(tnet_startup()){ + return -1; + } + +#if RUN_TEST_LOOP + for(;;) +#endif + { + +#if RUN_TEST_ALL || RUN_TEST_SOCKETS + test_sockets(); +#endif + +#if RUN_TEST_ALL || RUN_TEST_TRANSPORT + test_transport(); +#endif + +#if RUN_TEST_ALL || RUN_TEST_AUTH + test_auth(); +#endif + +#if RUN_TEST_ALL || RUN_TEST_STUN + test_stun(); +#endif + +#if RUN_TEST_ALL || RUN_TEST_NAT + test_nat(); +#endif + +#if RUN_TEST_ALL || RUN_TEST_IFACES + test_ifaces(); +#endif + +#if RUN_TEST_ALL || RUN_TEST_DNS + test_dns(); +#endif + +#if RUN_TEST_ALL || RUN_TEST_DHCP + test_dhcp(); +#endif + +#if RUN_TEST_ALL || RUN_TEST_DHCP6 + test_dhcp6(); +#endif + +#if RUN_TEST_ALL || RUN_TEST_TLS + test_tls(); +#endif + + } + + /* Cleanup the network stack */ + tnet_cleanup(); + + return 0; +} + diff --git a/tinyNET/test/test.vcproj b/tinyNET/test/test.vcproj new file mode 100644 index 0000000..fcf6413 --- /dev/null +++ b/tinyNET/test/test.vcproj @@ -0,0 +1,414 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tinyNET/test/test_auth.h b/tinyNET/test/test_auth.h new file mode 100644 index 0000000..20e96af --- /dev/null +++ b/tinyNET/test/test_auth.h @@ -0,0 +1,31 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ +#ifndef TNET_TEST_AUTH_H +#define TNET_TEST_AUTH_H + +void test_auth() +{ + +} + +#endif /* TNET_TEST_AUTH_H */ + diff --git a/tinyNET/test/test_dhcp.h b/tinyNET/test/test_dhcp.h new file mode 100644 index 0000000..6b976aa --- /dev/null +++ b/tinyNET/test/test_dhcp.h @@ -0,0 +1,121 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ +#ifndef TNET_TEST_DHCP_H +#define TNET_TEST_DHCP_H + + + +void test_dhcp_discover(tnet_dhcp_ctx_t *ctx) +{ +} + +void test_dhcp_offer(tnet_dhcp_ctx_t *ctx) +{ +} + +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; + } + +bail: + TSK_OBJECT_SAFE_FREE(reply); + TSK_OBJECT_SAFE_FREE(params); + + //tsk_thread_sleep(1000); +} + +void test_dhcp() +{ + tnet_dhcp_ctx_t *ctx = tnet_dhcp_ctx_create(); + test_dhcp_inform(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 new file mode 100644 index 0000000..a132cca --- /dev/null +++ b/tinyNET/test/test_dhcp6.h @@ -0,0 +1,51 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ +#ifndef TNET_TEST_DHCP6_H +#define TNET_TEST_DHCP6_H + + + +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_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); + + 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); + + TSK_OBJECT_SAFE_FREE(ctx); +} + +#endif /* TNET_TEST_DHCP6_H */ diff --git a/tinyNET/test/test_dns.h b/tinyNET/test/test_dns.h new file mode 100644 index 0000000..b7990b5 --- /dev/null +++ b/tinyNET/test/test_dns.h @@ -0,0 +1,227 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ +#ifndef TNET_TEST_DNS_H +#define TNET_TEST_DNS_H + +//#include "tnet_utils.h" /* tnet_address_t */ + +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); +} + +void test_dns_srv() +{ + 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); + } + + TSK_FREE(hostname); + TSK_OBJECT_SAFE_FREE(ctx); + + 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; + + 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_thread_sleep(2000); +} + +void test_enum() +{ + 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); +} + + +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", + + "+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", + + "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("---------"); + } +} + +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); + } +} + +void test_dns() +{ + //test_dns_naptr_srv(); + //test_dns_srv(); + //test_dns_query(); + test_enum(); + //test_regex(); + //test_resolvconf(); +} + + +#endif /* TNET_TEST_DNS_H */ diff --git a/tinyNET/test/test_ifaces.h b/tinyNET/test/test_ifaces.h new file mode 100644 index 0000000..a3bb411 --- /dev/null +++ b/tinyNET/test/test_ifaces.h @@ -0,0 +1,102 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ +#ifndef TNET_TEST_IFACES_H +#define TNET_TEST_IFACES_H + +void test_faces_bestsource() +{ + 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."); + } + // 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."); + } +} + +void test_ifaces_dump_ifaces() +{ + 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_OBJECT_SAFE_FREE(ifaces); +} + +void test_ifaces_dump_addresses() +{ + 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_OBJECT_SAFE_FREE(addresses); +} + +void test_ifaces() +{ + 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 new file mode 100644 index 0000000..ac1e4cb --- /dev/null +++ b/tinyNET/test/test_nat.h @@ -0,0 +1,219 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ +#ifndef TNET_TEST_NAT_H +#define TNET_TEST_NAT_H + +//stun.ekiga.net +//#define STUN_SERVER_IP "numb.viagenie.ca" +#define STUN_SERVER_IP "numb.viagenie.ca" +#define STUN_USERNAME "xxxxx" +#define STUN_PASSWORD "xxxxx" +#define STUN_SERVER_PORT TNET_STUN_TCP_UDP_DEFAULT_PORT +#define STUN_SERVER_PROTO tnet_socket_type_udp_ipv4 + +void test_nat_stun() +{ + tnet_socket_t *socket1 = 0, *socket2 = 0; + tnet_nat_context_handle_t *context = 0; + + 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; + } + + context = tnet_nat_context_create(STUN_SERVER_PROTO, STUN_USERNAME, STUN_PASSWORD); + + if(tnet_nat_set_server_address(context, STUN_SERVER_IP)) + { + TSK_DEBUG_ERROR("Failed to set STUN/TURN address."); + goto bail; + } + + /* == BIND + */ + bind_id1 = tnet_nat_stun_bind(context, socket1->fd); + bind_id2 = tnet_nat_stun_bind(context, socket2->fd); + + if(!TNET_STUN_IS_VALID_BINDING_ID(bind_id1) ||!TNET_STUN_IS_VALID_BINDING_ID(bind_id2)) + { + TSK_DEBUG_ERROR("Failed to get public IP/port using stun"); + goto bail; + } + + if(!tnet_nat_stun_get_reflexive_address(context, bind_id1, &public_ip1, &public_port1)) + { + TSK_DEBUG_INFO("Public IP1/Port1 ==> %s:%u", public_ip1, public_port1); + } + + if(!tnet_nat_stun_get_reflexive_address(context, bind_id2, &public_ip2, &public_port2)) + { + 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_FREE(public_ip1); + TSK_FREE(public_ip1); + + TSK_OBJECT_SAFE_FREE(context); +} + +void test_nat_turn() +{ + tnet_socket_t *socket1 = 0, *socket2 = 0; + tnet_nat_context_handle_t *context = 0; + tnet_turn_allocation_id_t alloc_id1, alloc_id2; + + char* public_ip1 = 0, *public_ip2 = 0; + tnet_port_t public_port1 = 0, public_port2 = 0; + + 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))) + { + goto bail; + } + + context = tnet_nat_context_create(STUN_SERVER_PROTO, STUN_USERNAME, STUN_PASSWORD); + + if(tnet_nat_set_server_address(context, STUN_SERVER_IP)) + { + TSK_DEBUG_ERROR("Failed to set STUN/TURN address."); + goto bail; + } + + /* == ALLOC + */ + alloc_id1 = tnet_nat_turn_allocate(context, socket1->fd); + alloc_id2 = tnet_nat_turn_allocate(context, socket2->fd); + if(!TNET_TURN_IS_VALID_ALLOCATION_ID(alloc_id1) || !TNET_TURN_IS_VALID_ALLOCATION_ID(alloc_id2)) + { + TSK_DEBUG_ERROR("TURN allocation failed."); + goto bail; + } + else + { + TSK_DEBUG_INFO("TURN allocation succeeded and id1=%llu and id2=%llu", alloc_id1, alloc_id2); + } + + tsk_thread_sleep(2000); + + /* == RETRIEVE STUN SERVER REFLEXIVE ADDRESSES + */ + if(!tnet_nat_turn_get_reflexive_address(context, alloc_id1, &public_ip1, &public_port1)) + { + TSK_DEBUG_INFO("Public IP1/Port1 ==> %s:%u", public_ip1, public_port1); + } + + if(!tnet_nat_turn_get_reflexive_address(context, alloc_id2, &public_ip2, &public_port2)) + { + TSK_DEBUG_INFO("Public IP2/Port2 ==> %s:%u", public_ip2, public_port2); + } + + /* == CREATE PERMISSION + */ + //tnet_nat_turn_add_permission(context, alloc_id1, "192.168.0.11", 300); + + /* == CHANNEL BINDING + */ + { + /* Try to bind (channel binding) to the socket1 to socket2 */ + struct sockaddr_storage peer; + if((ret = tnet_sockaddr_init(public_ip2, public_port2, STUN_SERVER_PROTO, &peer))) + { + TSK_DEBUG_ERROR("Failed to init peer with error code %d.", ret); + } + else + { + channel_id = tnet_nat_turn_channel_bind(context, alloc_id1,&peer); + if(TNET_TURN_IS_VALID_CHANNEL_BINDING_ID(channel_id)) + { + TSK_DEBUG_INFO("TURN channel binding succeeded."); + + /* Try to send data using the newly create channel */ + if(tnet_nat_turn_channel_senddata(context, channel_id, "Doubango", strlen("Doubango"))) + { + TSK_DEBUG_ERROR("Failed to send data using channel id [%u].", channel_id); + } + else + { + TSK_DEBUG_INFO("Data successfuly sent using channel if[%u].", channel_id); + } + } + else + { + TSK_DEBUG_ERROR("TURN channel binding failed."); + } + } + } + + tsk_thread_sleep(2000); + + /* == UNALLOC + */ + if((ret = tnet_nat_turn_unallocate(context, alloc_id1)) || (ret = tnet_nat_turn_unallocate(context, alloc_id2))) + { + TSK_DEBUG_ERROR("TURN unallocation failed with error code: %d.", ret); + goto bail; + } + else + { + TSK_DEBUG_INFO("TURN unallocation succeeded."); + } + +bail: + TSK_OBJECT_SAFE_FREE(socket1); + TSK_OBJECT_SAFE_FREE(socket2); + + TSK_FREE(public_ip1); + TSK_FREE(public_ip1); + + TSK_OBJECT_SAFE_FREE(context); +} + + +void test_nat() +{ + test_nat_stun(); + //test_nat_turn(); + //tsk_thread_sleep(1000); +} + + + +#endif /* TNET_TEST_NAT_H */ diff --git a/tinyNET/test/test_sockets.h b/tinyNET/test/test_sockets.h new file mode 100644 index 0000000..32e1cca --- /dev/null +++ b/tinyNET/test/test_sockets.h @@ -0,0 +1,64 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ +#ifndef TNET_TEST_SOCKETS_H +#define TNET_TEST_SOCKETS_H + +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); +} + +#endif /* TNET_TEST_SOCKETS_H */ diff --git a/tinyNET/test/test_stun.h b/tinyNET/test/test_stun.h new file mode 100644 index 0000000..4209f03 --- /dev/null +++ b/tinyNET/test/test_stun.h @@ -0,0 +1,112 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ +#ifndef TNET_TEST_STUN_H +#define TNET_TEST_STUN_H + +//#define STUN_SERVER_IP "numb.viagenie.ca" +//#define STUN_SERVER_PORT TNET_STUN_TCP_UDP_DEFAULT_PORT +//#define STUN_SERVER_PROTO tnet_socket_type_udp_ipv4 + +// http://tools.ietf.org/html/draft-ietf-behave-stun-test-vectors-04 + +void test_stun_dump_transacid(tnet_stun_transacid_t transcid) +{ + char transac_idstriing[TNET_STUN_TRANSACID_SIZE*2+1]; + tsk_str_from_hex(transcid, TNET_STUN_TRANSACID_SIZE, transac_idstriing); + + transac_idstriing[sizeof(transac_idstriing)-1] = '\0'; + + TSK_DEBUG_INFO("STUN transac id:%s", transac_idstriing); +} + +void test_sun_sendMessage() +{ + /*tnet_stun_message_handle_t *message = 0; + tsk_buffer_t *buffer = 0; + tnet_socket_t *socket = 0; + struct sockaddr_storage to; + + + message = TNET_STUN_MESSAGE_CREATE(); + tnet_stun_message_set_type(message, tsm_binding_request); + tnet_stun_message_set_random_transacid(message); + + if(!(buffer = tnet_stun_message_serialize(message))) + { + goto bail; + } + + // Create blocking socket and bind it + socket = tnet_socket_create_EX(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, STUN_SERVER_PROTO, 0); + if(!TNET_SOCKET_IS_VALID(socket)) + { + goto bail; + } + + // Create stun server's sockaddr structure + if(tnet_sockaddr_init(STUN_SERVER_IP, STUN_SERVER_PORT, STUN_SERVER_PROTO, &to)) + { + goto bail; + } + + tnet_socket_sendto(socket, (struct sockaddr*)&to, buffer->data, buffer->size); + +bail: + TSK_OBJECT_SAFE_FREE(message); + TSK_OBJECT_SAFE_FREE(socket); + TSK_OBJECT_SAFE_FREE(buffer);*/ +} + +void test_stun_context() +{ +// tnet_socket_t *localSocket = 0; +// tnet_stun_context_t *context = 0; +// +// /* Somewhere in Your application ==> Create and bind your local socket +// */ +// if(!(localSocket = tnet_socket_create_EX(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, STUN_SERVER_PROTO, 0))) +// { +// goto bail; +// } +// +// /* Create your STUN2 context +// */ +// if(!(context = TNET_STUN_CONTEXT_CREATE("myusername", "mypassword", localSocket->fd))) +// { +// goto bail; +// } +// +// tnet_stun_resolve(context, STUN_SERVER_IP, STUN_SERVER_PORT, STUN_SERVER_PROTO); +// +//bail: +// TNET_OBJECT_SAFE_FREE(localSocket); +// TNET_OBJECT_SAFE_FREE(context); +} + +void test_stun() +{ + //test_stun_transacid(); + //test_sun_sendMessage(); + test_stun_context(); +} + +#endif /* TNET_TEST_STUN_H */ diff --git a/tinyNET/test/test_tls.h b/tinyNET/test/test_tls.h new file mode 100644 index 0000000..ff86823 --- /dev/null +++ b/tinyNET/test/test_tls.h @@ -0,0 +1,108 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ +#ifndef TNET_TEST_TLS_H +#define TNET_TEST_TLS_H + +#define TEST_TLS_REMOTE_IP "192.168.16.225" +#define TEST_TLS_REMOTE_PORT 4061 + +#define TLS_TEST_SIP_MESSAGE \ + "REGISTER sip:micromethod.com SIP/2.0\r\n" \ + "Via: SIP/2.0/%s %s:%d;rport;branch=z9hG4bK1245420841406%d\r\n" \ + "From: ;tag=29358\r\n" \ + "To: \r\n" \ + "Call-ID: M-fa53180346f7f55ceb8d8670f9223dbb\r\n" \ + "CSeq: 201 REGISTER\r\n" \ + "Max-Forwards: 70\r\n" \ + "Contact: \r\n" \ + "Expires: 10\r\n" \ + "\r\n" + +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; +} + + +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); +} + +#endif /* TNET_TEST_TLS_H */ + diff --git a/tinyNET/test/test_transport.h b/tinyNET/test/test_transport.h new file mode 100644 index 0000000..870c8b5 --- /dev/null +++ b/tinyNET/test/test_transport.h @@ -0,0 +1,222 @@ +/* +* Copyright (C) 2009 Mamadou Diop. +* +* Contact: Mamadou Diop +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +* +*/ +#ifndef TNET_TEST_TRANSPORT_H +#define TNET_TEST_TRANSPORT_H + +//#define REMOTE_IP4 "proxy.sipthor.net"//"192.168.0.15" +#define REMOTE_IP4 "192.168.0.13" +#define REMOTE_IP6 "2a01:e35:8632:7050:6122:2706:2124:32cb" +#define REMOTE_IP REMOTE_IP4 +#define REMOTE_PORT 5083 + +#if defined(ANDROID) /* FIXME */ +# define LOCAL_IP4 "10.0.2.15" +#else +# define LOCAL_IP4 TNET_SOCKET_HOST_ANY +#endif +#define LOCAL_IP6 TNET_SOCKET_HOST_ANY + +#if defined(ANDROID) +# define LOCAL_PORT 5060 +#else +# define LOCAL_PORT TNET_SOCKET_PORT_ANY +#endif + +#define SIP_MESSAGE \ + "REGISTER sip:micromethod.com SIP/2.0\r\n" \ + "Via: SIP/2.0/%s %s:%d;rport;branch=z9hG4bK1245420841406%d\r\n" \ + "From: ;tag=29358\r\n" \ + "To: \r\n" \ + "Call-ID: M-fa53180346f7f55ceb8d8670f9223dbb\r\n" \ + "CSeq: 201 REGISTER\r\n" \ + "Max-Forwards: 70\r\n" \ + "Contact: \r\n" \ + "Expires: 10\r\n" \ + "\r\n" + + +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; +} + +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; +} + +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); + } + +} + + +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; +} + +void test_transport() +{ +#define TEST_TCP 1 +#define TEST_UDP 0 + + +#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); +#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 + +//#if defined(ANDROID) + tsk_thread_sleep(1000000); +//#else + getchar(); +//#endif + +#if TEST_UDP + TSK_OBJECT_SAFE_FREE(udp); +#endif + +#if TEST_TCP + TSK_OBJECT_SAFE_FREE(tcp); +#endif +} + + +#endif /* TNET_TEST_TRANSPORT_H*/ -- cgit v1.1