Program.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Copyright (C) 2010-2014 Mamadou DIOP
  2. * Copyright (C) 2011-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. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text;
  23. using org.doubango.ipsecWRAP;
  24. using System.Diagnostics;
  25. using err = org.doubango.ipsecWRAP.tipsec_error_t;
  26. using System.Runtime.InteropServices;
  27. namespace ipsec
  28. {
  29. class Program
  30. {
  31. static tipsec_ipproto_t __ipproto = tipsec_ipproto_t.tipsec_ipproto_udp;
  32. static bool __use_ipv6 = false;
  33. static tipsec_mode_t __mode = tipsec_mode_t.tipsec_mode_trans;
  34. static tipsec_ealg_t __ealg = tipsec_ealg_t.tipsec_ealg_des_ede3_cbc;
  35. static tipsec_alg_t __alg = tipsec_alg_t.tipsec_alg_hmac_md5_96;
  36. static tipsec_proto_t __proto = tipsec_proto_t.tipsec_proto_esp;
  37. static String __addr_local = "0.0.0.0";
  38. static String __addr_remote = "192.168.0.34";
  39. static ushort __port_local_out = 5062; // PORT_UC
  40. static ushort __port_local_in = 5064; // PORT_US
  41. static ushort __port_remote_out = 5066; // PORT_PC
  42. static ushort __port_remote_in = 5068; // PORT_PS
  43. static UInt32 __spi_remote_out = 3333; // SPI_PC
  44. static UInt32 __spi_remote_in = 4444; // SPI_PS
  45. static UInt64 __lifetime = 1800; /* always set it to the maximum value. (Not possible to update the value after REGISTER 200OK. ) */
  46. static String __key_ik = "1234567890123456";
  47. static String __key_ck = "1234567890121234";
  48. static void Main(string[] args)
  49. {
  50. /* Create the context */
  51. IPSecCtx ipsecCtx = new IPSecCtx(__ipproto, __use_ipv6, __mode, __ealg, __alg, __proto);
  52. /* Set local */
  53. Debug.Assert(ipsecCtx.setLocal(__addr_local, __addr_remote, __port_local_out, __port_local_in) == err.tipsec_error_success);
  54. /* Dump SPIs created by the OS after calling set_local() */
  55. Console.WriteLine("SPI-UC={0}, SPI-US={1}", ipsecCtx.getSpiUC(), ipsecCtx.getSpiUS());
  56. /* Set remote */
  57. Debug.Assert(ipsecCtx.setRemote(__spi_remote_out, __spi_remote_in, __port_remote_out, __port_remote_in, __lifetime) == err.tipsec_error_success);
  58. /* Set Integrity (IK) and Confidentiality (CK) keys */
  59. IntPtr keyIK = Marshal.StringToHGlobalAnsi(__key_ik);
  60. IntPtr keyCK = Marshal.StringToHGlobalAnsi(__key_ck);
  61. Debug.Assert(ipsecCtx.setKeys(keyIK, keyCK) == err.tipsec_error_success);
  62. Marshal.FreeHGlobal(keyIK);
  63. Marshal.FreeHGlobal(keyCK);
  64. /* Start (Setup) the SAs */
  65. Debug.Assert(ipsecCtx.start() == err.tipsec_error_success);
  66. Console.WriteLine("!!! IPSec SAs started (Press any key to stop) !!!");
  67. Console.ReadLine();
  68. ipsecCtx.Dispose(); // Not required. GC will collect it when refCount reach zero.
  69. Console.ReadLine();
  70. }
  71. }
  72. }