test_nat.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_NAT_H
  20. #define TNET_TEST_NAT_H
  21. #define STUN_SERVER_IP "ns313841.ovh.net" // "numb.viagenie.ca" /* stun.ekiga.net, */
  22. #define STUN_USERNAME "bossiel@yahoo.fr"
  23. #define STUN_PASSWORD "tinynet"
  24. #define STUN_SERVER_PORT TNET_STUN_TCP_UDP_DEFAULT_PORT
  25. #define STUN_SERVER_PROTO tnet_socket_type_udp_ipv4
  26. void test_nat_stun()
  27. {
  28. tnet_socket_t *socket1 = 0, *socket2 = 0;
  29. struct tnet_nat_ctx_s *context = 0;
  30. tnet_stun_binding_id_t bind_id1, bind_id2;
  31. char* public_ip1 = 0, *public_ip2 = 0;
  32. tnet_port_t public_port1 = 0, public_port2 = 0;
  33. if(!(socket1 = tnet_socket_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, STUN_SERVER_PROTO))
  34. || !(socket2 = tnet_socket_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, STUN_SERVER_PROTO))) {
  35. goto bail;
  36. }
  37. context = tnet_nat_context_create(STUN_SERVER_PROTO, STUN_USERNAME, STUN_PASSWORD);
  38. if(tnet_nat_set_server_address(context, STUN_SERVER_IP)) {
  39. TSK_DEBUG_ERROR("Failed to set STUN/TURN address.");
  40. goto bail;
  41. }
  42. // == BIND == //
  43. bind_id1 = tnet_nat_stun_bind(context, socket1->fd);
  44. bind_id2 = tnet_nat_stun_bind(context, socket2->fd);
  45. if(!bind_id1 || !bind_id2) {
  46. TSK_DEBUG_ERROR("Failed to get public IP/port using stun");
  47. goto bail;
  48. }
  49. if (tnet_nat_stun_get_reflexive_address(context, bind_id1, &public_ip1, &public_port1) == 0) {
  50. TSK_DEBUG_INFO("Public IP1/Port1 ==> %s:%u", public_ip1, public_port1);
  51. }
  52. if (tnet_nat_stun_get_reflexive_address(context, bind_id2, &public_ip2, &public_port2) == 0) {
  53. TSK_DEBUG_INFO("Public IP2/Port2 ==> %s:%u", public_ip2, public_port2);
  54. }
  55. // == UNBIND == //
  56. tnet_nat_stun_unbind(context, bind_id1);
  57. tnet_nat_stun_unbind(context, bind_id2);
  58. bail:
  59. TSK_OBJECT_SAFE_FREE(socket1);
  60. TSK_OBJECT_SAFE_FREE(socket2);
  61. TSK_FREE(public_ip1);
  62. TSK_FREE(public_ip1);
  63. TSK_OBJECT_SAFE_FREE(context);
  64. }
  65. void test_nat_turn()
  66. {
  67. // tnet_socket_t *socket1 = 0, *socket2 = 0;
  68. // struct tnet_nat_ctx_s *context = 0;
  69. // tnet_turn_allocation_id_t alloc_id1, alloc_id2;
  70. //
  71. // char* public_ip1 = 0, *public_ip2 = 0;
  72. // tnet_port_t public_port1 = 0, public_port2 = 0;
  73. //
  74. // tnet_turn_channel_binding_id_t channel_id;
  75. //
  76. // int ret;
  77. //
  78. // if(!(socket1 = tnet_socket_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, STUN_SERVER_PROTO))
  79. // || !(socket2 = tnet_socket_create(TNET_SOCKET_HOST_ANY, TNET_SOCKET_PORT_ANY, STUN_SERVER_PROTO)))
  80. // {
  81. // goto bail;
  82. // }
  83. //
  84. // context = tnet_nat_context_create(STUN_SERVER_PROTO, STUN_USERNAME, STUN_PASSWORD);
  85. // ((tnet_nat_context_t*)context)->enable_evenport = 0;
  86. // ((tnet_nat_context_t*)context)->enable_fingerprint = 0;
  87. // ((tnet_nat_context_t*)context)->enable_dontfrag = 0;
  88. // ((tnet_nat_context_t*)context)->enable_integrity = 0;
  89. //
  90. // if(tnet_nat_set_server_address(context, STUN_SERVER_IP))
  91. // {
  92. // TSK_DEBUG_ERROR("Failed to set STUN/TURN address.");
  93. // goto bail;
  94. // }
  95. //
  96. // /* == ALLOC
  97. // */
  98. // alloc_id1 = tnet_nat_turn_allocate(context, socket1->fd);
  99. // alloc_id2 = tnet_nat_turn_allocate(context, socket2->fd);
  100. // if(!TNET_TURN_IS_VALID_ALLOCATION_ID(alloc_id1) || !TNET_TURN_IS_VALID_ALLOCATION_ID(alloc_id2))
  101. // {
  102. // TSK_DEBUG_ERROR("TURN allocation failed.");
  103. // goto bail;
  104. // }
  105. // else
  106. // {
  107. // TSK_DEBUG_INFO("TURN allocation succeeded and id1=%llu and id2=%llu", alloc_id1, alloc_id2);
  108. // }
  109. //
  110. // tsk_thread_sleep(2000);
  111. //
  112. // /* == RETRIEVE STUN SERVER REFLEXIVE ADDRESSES
  113. // */
  114. // if(!tnet_nat_turn_get_reflexive_address(context, alloc_id1, &public_ip1, &public_port1))
  115. // {
  116. // TSK_DEBUG_INFO("Public IP1/Port1 ==> %s:%u", public_ip1, public_port1);
  117. // }
  118. //
  119. // if(!tnet_nat_turn_get_reflexive_address(context, alloc_id2, &public_ip2, &public_port2))
  120. // {
  121. // TSK_DEBUG_INFO("Public IP2/Port2 ==> %s:%u", public_ip2, public_port2);
  122. // }
  123. //
  124. // /* == CREATE PERMISSION
  125. // */
  126. // //tnet_nat_turn_add_permission(context, alloc_id1, "192.168.0.11", 300);
  127. //
  128. // /* == CHANNEL BINDING
  129. // */
  130. // {
  131. // /* Try to bind (channel binding) to the socket1 to socket2 */
  132. // struct sockaddr_storage peer;
  133. // if((ret = tnet_sockaddr_init(public_ip2, public_port2, STUN_SERVER_PROTO, &peer)))
  134. // {
  135. // TSK_DEBUG_ERROR("Failed to init peer with error code %d.", ret);
  136. // }
  137. // else
  138. // {
  139. // channel_id = tnet_nat_turn_channel_bind(context, alloc_id1,&peer);
  140. // if(TNET_TURN_IS_VALID_CHANNEL_BINDING_ID(channel_id))
  141. // {
  142. // TSK_DEBUG_INFO("TURN channel binding succeeded.");
  143. //
  144. // /* Try to send data using the newly create channel */
  145. // if(tnet_nat_turn_channel_senddata(context, channel_id, "Doubango", strlen("Doubango")))
  146. // {
  147. // TSK_DEBUG_ERROR("Failed to send data using channel id [%u].", channel_id);
  148. // }
  149. // else
  150. // {
  151. // TSK_DEBUG_INFO("Data successfuly sent using channel if[%u].", channel_id);
  152. // }
  153. // }
  154. // else
  155. // {
  156. // TSK_DEBUG_ERROR("TURN channel binding failed.");
  157. // }
  158. // }
  159. // }
  160. //
  161. // tsk_thread_sleep(2000);
  162. //
  163. // /* == UNALLOC
  164. // */
  165. // if((ret = tnet_nat_turn_unallocate(context, alloc_id1)) || (ret = tnet_nat_turn_unallocate(context, alloc_id2)))
  166. // {
  167. // TSK_DEBUG_ERROR("TURN unallocation failed with error code: %d.", ret);
  168. // goto bail;
  169. // }
  170. // else
  171. // {
  172. // TSK_DEBUG_INFO("TURN unallocation succeeded.");
  173. // }
  174. //
  175. //bail:
  176. // TSK_OBJECT_SAFE_FREE(socket1);
  177. // TSK_OBJECT_SAFE_FREE(socket2);
  178. //
  179. // TSK_FREE(public_ip1);
  180. // TSK_FREE(public_ip1);
  181. //
  182. // TSK_OBJECT_SAFE_FREE(context);
  183. }
  184. void test_nat()
  185. {
  186. test_nat_stun();
  187. //test_nat_turn();
  188. //tsk_thread_sleep(1000);
  189. }
  190. #endif /* TNET_TEST_NAT_H */