test.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Copyright (C) 2013-2014 Mamadou DIOP
  2. * Copyright (C) 2013-2014 Doubango Telecom <http://www.doubango.org>
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with DOUBANGO.
  18. */
  19. #include "stdafx.h"
  20. #include "tipsec.h"
  21. #include "tsk_plugin.h"
  22. #include "tsk_debug.h"
  23. static tipsec_ipproto_t __ipproto = tipsec_ipproto_udp;
  24. static tsk_bool_t __use_ipv6 = tsk_false;
  25. static tipsec_mode_t __mode = tipsec_mode_trans;
  26. static tipsec_ealg_t __ealg = tipsec_ealg_aes;
  27. static tipsec_alg_t __alg = tipsec_alg_hmac_md5_96;
  28. static tipsec_proto_t __proto = tipsec_proto_esp;
  29. static const char* __addr_local = "192.168.0.37"; // overrided using command line args
  30. static const char* __addr_remote = "192.168.0.31"; // overrided using command line args
  31. static tipsec_port_t __port_local_in = 5064; // PORT_US
  32. static tipsec_port_t __port_local_out = 5062; // PORT_UC
  33. static tipsec_port_t __port_remote_in = 5068; // PORT_PS
  34. static tipsec_port_t __port_remote_out = 5066; // PORT_PC
  35. static tipsec_spi_t __spi_remote_out = 3333; // SPI_PC
  36. static tipsec_spi_t __spi_remote_in = 4444; // SPI_PS
  37. static tipsec_lifetime_t __lifetime = 1800; /* always set it to the maximum value. (Not possible to update the value after REGISTER 200OK. ) */
  38. static const char* __key_ik = "1234567890123456";
  39. static const char* __key_ck = "1234567890121234";
  40. #if TIPSEC_UNDER_WINDOWS
  41. #include <Windows.h>
  42. static tsk_bool_t test_ipsec_is_winvista_or_later()
  43. {
  44. /*
  45. Version Number Description
  46. 6.1 Windows 7 / Windows 2008 R2
  47. 6.0 Windows Vista / Windows 2008
  48. 5.2 Windows 2003
  49. 5.1 Windows XP
  50. 5.0 Windows 2000
  51. */
  52. static DWORD dwMajorVersion = -1;
  53. static DWORD dwMinorVersion = -1;
  54. if(dwMajorVersion == -1 || dwMinorVersion == -1) {
  55. OSVERSIONINFO osvi;
  56. ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
  57. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  58. GetVersionEx(&osvi);
  59. dwMajorVersion = osvi.dwMajorVersion;
  60. dwMinorVersion = osvi.dwMinorVersion;
  61. }
  62. return (dwMajorVersion >= 6);
  63. }
  64. #else
  65. static tsk_bool_t test_ipsec_is_winvista_or_later()
  66. {
  67. return 0;
  68. }
  69. #endif
  70. #if defined(_WIN32_WCE)
  71. int _tmain()
  72. #else
  73. int main(int argc, const char* argv[])
  74. #endif
  75. {
  76. int err = 0;
  77. tsk_size_t i;
  78. struct tsk_plugin_s* p_plugin = tsk_null;
  79. tipsec_ctx_t* p_ctx = tsk_null;
  80. static const char* __plugins_path[] = /* Visual Studio requires "Debugging" -> "Working Directory" = "$(OutDir)" */
  81. {
  82. "pluginWinIPSecVista.DLL",
  83. "pluginWinIPSecXP.DLL",
  84. "pluginLinIPsecTools.so"
  85. };
  86. static const tsk_size_t __plugins_count = sizeof(__plugins_path)/sizeof(__plugins_path[0]);
  87. #define BUF_SIZE 1024
  88. char buffer[BUF_SIZE];
  89. /* Set debug level to INFO */
  90. tsk_debug_set_level(DEBUG_LEVEL_INFO);
  91. // Command line "local_ip" "local_port_in" "local_port_out" "remote_ip" "remote_port_in" "remote_port_out"
  92. if (argc == (6 + 1)) {
  93. __addr_local = argv[1];
  94. __port_local_in = atoi(argv[2]);
  95. __port_local_out = atoi(argv[3]);
  96. __addr_remote = argv[4];
  97. __port_remote_in = atoi(argv[5]);
  98. __port_remote_out = atoi(argv[6]);
  99. }
  100. TSK_DEBUG_INFO("Local node=%s:%d/%d, remote node=%s:%d/%d",
  101. __addr_local, __port_local_in, __port_local_out,
  102. __addr_remote, __port_remote_in, __port_remote_out);
  103. /* Create the plugin */
  104. for (i = 0; i < __plugins_count; ++i) {
  105. if (tsk_plugin_file_exist(__plugins_path[i])) {
  106. tipsec_plugin_register_file(__plugins_path[i], &p_plugin);
  107. if (p_plugin) {
  108. break;
  109. }
  110. }
  111. }
  112. if (!p_plugin) {
  113. TSK_DEBUG_ERROR("Failed to create IPSec plugin");
  114. err = -1;
  115. goto bail;
  116. }
  117. /* Create the context */
  118. err = tipsec_ctx_create(__ipproto, __use_ipv6, __mode, __ealg, __alg, __proto, &p_ctx);
  119. if (err) {
  120. goto bail;
  121. }
  122. /* Set local */
  123. err = tipsec_ctx_set_local(p_ctx, __addr_local, __addr_remote, __port_local_out, __port_local_in);
  124. if (err) {
  125. goto bail;
  126. }
  127. // Dump SPIs created by the OS after calling set_local()
  128. TSK_DEBUG_INFO("SPI-UC=%u, SPI-US=%u", p_ctx->spi_uc, p_ctx->spi_us);
  129. // Enter Remote SPI in
  130. TSK_DEBUG_INFO("Enter remote SPI in:");
  131. if (fgets(buffer, BUF_SIZE, stdin)) {
  132. if (buffer[0] != 10 && buffer[1] != 0) {
  133. __spi_remote_in = strtoul (buffer, NULL, 0);
  134. }
  135. }
  136. // Enter Remote SPI out
  137. TSK_DEBUG_INFO("Enter remote SPI out:");
  138. if (fgets(buffer, BUF_SIZE, stdin)) {
  139. if (buffer[0] != 10 && buffer[1] != 0) {
  140. __spi_remote_out = strtoul (buffer, NULL, 0);
  141. }
  142. }
  143. TSK_DEBUG_INFO("SPI remote %u/%u", __spi_remote_in, __spi_remote_out);
  144. /* Set remote */
  145. err = tipsec_ctx_set_remote(p_ctx, __spi_remote_out, __spi_remote_in, __port_remote_out, __port_remote_in, __lifetime);
  146. if (err) {
  147. goto bail;
  148. }
  149. /* Set Integrity (IK) and Confidentiality (CK) keys */
  150. err = tipsec_ctx_set_keys(p_ctx, __key_ik, __key_ck);
  151. if (err) {
  152. goto bail;
  153. }
  154. /* Start (Setup) the SAs */
  155. err = tipsec_ctx_start(p_ctx);
  156. if (err) {
  157. goto bail;
  158. }
  159. TSK_DEBUG_INFO("!!! IPSec SAs started (Press any key to stop) !!!");
  160. /* Wait */
  161. getchar();
  162. bail:
  163. // Stop the SAs, cleanup and destroy the context
  164. TSK_OBJECT_SAFE_FREE(p_ctx); // must destroy the contect before unloading the plugin (*.DLL or *.SO)
  165. // Unregister the plugin and close the file handle
  166. if (p_plugin) {
  167. tipsec_plugin_unregister_file(p_plugin);
  168. TSK_OBJECT_SAFE_FREE(p_plugin);
  169. }
  170. return err;
  171. }