test_transport.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Copyright (C) 2014 Mamadou DIOP.
  2. *
  3. * This file is part of Open Source Doubango Framework.
  4. *
  5. * DOUBANGO is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * DOUBANGO is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with DOUBANGO.
  17. *
  18. */
  19. #ifndef TNET_TEST_TRANSPORT_H
  20. #define TNET_TEST_TRANSPORT_H
  21. //#define REMOTE_IP4 "proxy.sipthor.net"//"192.168.0.15"
  22. #define REMOTE_IP4 "192.168.0.13"
  23. #define REMOTE_IP6 "2a01:e35:8632:7050:6122:2706:2124:32cb"
  24. #define REMOTE_IP REMOTE_IP4
  25. #define REMOTE_PORT 5083
  26. #if defined(ANDROID) /* FIXME */
  27. # define LOCAL_IP4 "10.0.2.15"
  28. #else
  29. # define LOCAL_IP4 TNET_SOCKET_HOST_ANY
  30. #endif
  31. #define LOCAL_IP6 TNET_SOCKET_HOST_ANY
  32. #if defined(ANDROID)
  33. # define LOCAL_PORT 5060
  34. #else
  35. # define LOCAL_PORT TNET_SOCKET_PORT_ANY
  36. #endif
  37. #define SIP_MESSAGE \
  38. "REGISTER sip:micromethod.com SIP/2.0\r\n" \
  39. "Via: SIP/2.0/%s %s:%d;rport;branch=z9hG4bK1245420841406%d\r\n" \
  40. "From: <sip:mamadou@micromethod.com>;tag=29358\r\n" \
  41. "To: <sip:mamadou@micromethod.com>\r\n" \
  42. "Call-ID: M-fa53180346f7f55ceb8d8670f9223dbb\r\n" \
  43. "CSeq: 201 REGISTER\r\n" \
  44. "Max-Forwards: 70\r\n" \
  45. "Contact: <sip:mamadou@%s:%d;transport=%s>\r\n" \
  46. "Expires: 10\r\n" \
  47. "\r\n"
  48. static int tnet_tcp_cb(const tnet_transport_event_t* e)
  49. {
  50. switch(e->type) {
  51. case event_data: {
  52. TSK_DEBUG_INFO("--- TCP ---\n%s\n", (const char*)e->data);
  53. break;
  54. }
  55. case event_closed:
  56. case event_connected:
  57. default: {
  58. break;
  59. }
  60. }
  61. return 0;
  62. }
  63. static int tnet_udp_cb(const tnet_transport_event_t* e)
  64. {
  65. switch(e->type) {
  66. case event_data: {
  67. TSK_DEBUG_INFO("--- UDP ---\n%s\n", (const char*)e->data);
  68. break;
  69. }
  70. case event_closed:
  71. case event_connected:
  72. default:
  73. break;
  74. }
  75. return 0;
  76. }
  77. void test_transport_tcp_ipv4(tnet_transport_handle_t *transport)
  78. {
  79. //tnet_socket_type_t type = tnet_socket_type_tcp_ipv4;
  80. tnet_ip_t ip;
  81. tnet_port_t port;
  82. tnet_fd_t fd = TNET_INVALID_FD;
  83. /* Set our callback function */
  84. tnet_transport_set_callback(transport, tnet_tcp_cb, "callbackdata");
  85. if(tnet_transport_start(transport)) {
  86. TSK_DEBUG_ERROR("Failed to create %s.", tnet_transport_get_description(transport));
  87. return;
  88. }
  89. /* Connect to the SIP Registrar */
  90. if((fd = tnet_transport_connectto_2(transport, REMOTE_IP, REMOTE_PORT)) == TNET_INVALID_FD) {
  91. TSK_DEBUG_ERROR("Failed to connect %s.", tnet_transport_get_description(transport));
  92. return;
  93. }
  94. if(tnet_sockfd_waitUntilWritable(fd, TNET_CONNECT_TIMEOUT)) {
  95. TSK_DEBUG_ERROR("%d milliseconds elapsed and the socket is still not connected.", TNET_CONNECT_TIMEOUT);
  96. tnet_transport_remove_socket(transport, &fd);
  97. return;
  98. }
  99. /* Send our SIP message */
  100. {
  101. char* message = 0;
  102. tnet_transport_get_ip_n_port(transport, fd, &ip, &port);
  103. tsk_sprintf(&message, SIP_MESSAGE, "TCP", ip, port, port, ip, port, "tcp");
  104. if(!tnet_transport_send(transport, fd, message, strlen(message))) {
  105. TSK_DEBUG_ERROR("Failed to send data using %s.", tnet_transport_get_description(transport));
  106. TSK_FREE(message);
  107. return;
  108. }
  109. TSK_FREE(message);
  110. }
  111. }
  112. int test_transport_udp_ipv4(tnet_transport_handle_t *transport)
  113. {
  114. //tnet_socket_type_t type = tnet_socket_type_udp_ipv4;
  115. tnet_ip_t ip;
  116. tnet_port_t port;
  117. tnet_fd_t fd = TNET_INVALID_FD;
  118. /* Set our callback function */
  119. tnet_transport_set_callback(transport, tnet_udp_cb, "callbackdata");
  120. if(tnet_transport_start(transport)) {
  121. TSK_DEBUG_ERROR("Failed to create %s.", tnet_transport_get_description(transport));
  122. return -1;
  123. }
  124. /* Connect to our SIP REGISTRAR */
  125. if((fd = tnet_transport_connectto_2(transport, REMOTE_IP, REMOTE_PORT)) == TNET_INVALID_FD) {
  126. TSK_DEBUG_ERROR("Failed to connect %s.", tnet_transport_get_description(transport));
  127. //tnet_transport_shutdown(transport);
  128. return -2;
  129. }
  130. if(tnet_sockfd_waitUntilWritable(fd, TNET_CONNECT_TIMEOUT)) {
  131. TSK_DEBUG_ERROR("%d milliseconds elapsed and the socket is still not connected.", TNET_CONNECT_TIMEOUT);
  132. tnet_transport_remove_socket(transport, &fd);
  133. return -3;
  134. }
  135. //tsk_thread_sleep(2000);
  136. /* Send our SIP message */
  137. /*while(1)*/{
  138. char* message = 0;
  139. tnet_transport_get_ip_n_port(transport, fd, &ip, &port);
  140. //memset(ip, 0, sizeof(ip));
  141. //memcpy(ip, "192.168.0.12", 12);
  142. tsk_sprintf(&message, SIP_MESSAGE, "UDP", ip, port, port, ip, port, "udp");
  143. if(!tnet_transport_send(transport, fd, message, strlen(message))) {
  144. TSK_DEBUG_ERROR("Failed to send data using %s.", tnet_transport_get_description(transport));
  145. //tnet_transport_shutdown(transport);
  146. TSK_FREE(message);
  147. return -4;
  148. }
  149. TSK_FREE(message);
  150. }
  151. return 0;
  152. }
  153. void test_transport()
  154. {
  155. #define TEST_TCP 1
  156. #define TEST_UDP 0
  157. #if TEST_UDP
  158. tnet_transport_handle_t *udp = tnet_transport_create(LOCAL_IP4, LOCAL_PORT, tnet_socket_type_udp_ipv4, "UDP/IPV4 TRANSPORT");
  159. test_transport_udp_ipv4(udp);
  160. #endif
  161. #if TEST_TCP
  162. tnet_transport_handle_t *tcp = tnet_transport_create(LOCAL_IP4, LOCAL_PORT, tnet_socket_type_tcp_ipv4, "TCP/IPV4 TRANSPORT");
  163. test_transport_tcp_ipv4(tcp);
  164. #endif
  165. //#if defined(ANDROID)
  166. tsk_thread_sleep(1000000);
  167. //#else
  168. getchar();
  169. //#endif
  170. #if TEST_UDP
  171. TSK_OBJECT_SAFE_FREE(udp);
  172. #endif
  173. #if TEST_TCP
  174. TSK_OBJECT_SAFE_FREE(tcp);
  175. #endif
  176. }
  177. #endif /* TNET_TEST_TRANSPORT_H*/