IpTunnel.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef IpTunnel_H
  16. #define IpTunnel_H
  17. #include "crypto/random/Random.h"
  18. #include "memory/Allocator.h"
  19. #include "interface/Iface.h"
  20. #include "util/log/Log.h"
  21. #include "util/events/EventBase.h"
  22. #include "util/platform/Sockaddr.h"
  23. #include "wire/RouteHeader.h"
  24. #include "tunnel/RouteGen.h"
  25. #include "util/Linker.h"
  26. Linker_require("tunnel/IpTunnel.c");
  27. struct IpTunnel_Connection
  28. {
  29. /** The header for routing to this node. */
  30. struct RouteHeader routeHeader;
  31. /** The IPv6 address used for this connection or all zeros if none was assigned. */
  32. uint8_t connectionIp6[16];
  33. /** The IPv4 address used for this connection or all zeros if none was assigned. */
  34. uint8_t connectionIp4[4];
  35. /** The IPv6 netmask/prefix length, in bits. Defaults to 128 if none was assigned. */
  36. uint8_t connectionIp6Prefix;
  37. /** The IPv6 prefix length in, in bits, defining netmask. 0xff if not used. */
  38. uint8_t connectionIp6Alloc;
  39. /** The IPv4 address prefix length, in bits. Defaults to 32 if none was assigned. */
  40. uint8_t connectionIp4Prefix;
  41. /** The IPv6 prefix length in, in bits, defining netmask. 0xff if not used. */
  42. uint8_t connectionIp4Alloc;
  43. /** non-zero if the connection was made using IpTunnel_connectTo(). */
  44. int isOutgoing : 1;
  45. /** The number of the connection so it can be identified when removing. */
  46. int number : 31;
  47. };
  48. struct IpTunnel
  49. {
  50. /** The interface used to send and receive messages to the TUN device. */
  51. struct Iface tunInterface;
  52. /**
  53. * The interface used to send and receive messages to other nodes.
  54. * All messages sent on this interface shall be preceeded with the RouterHeader and DataHeader.
  55. */
  56. struct Iface nodeInterface;
  57. /**
  58. * The list of registered connections, do not modify manually.
  59. * Will be reorganized from time to time so pointers are ephemeral.
  60. */
  61. struct {
  62. uint32_t count;
  63. struct IpTunnel_Connection* connections;
  64. } connectionList;
  65. };
  66. /**
  67. * Create a new IpTunnel structure.
  68. *
  69. * @param logger a logger or NULL.
  70. * @param eventBase the event base.
  71. * @param alloc an allocator.
  72. * @param rand a random generator.
  73. */
  74. struct IpTunnel* IpTunnel_new(struct Log* logger,
  75. struct EventBase* eventBase,
  76. struct Allocator* alloc,
  77. struct Random* rand,
  78. struct RouteGen* rg);
  79. /**
  80. * Allow another node to tunnel IPv4 and/or ICANN IPv6 through this node.
  81. *
  82. * @param publicKeyOfAuthorizedNode the key for the node which will be allowed to connect.
  83. * @param ip6Addr the IPv6 address which the node will be issued or NULL.
  84. * @param ip6Prefix the IPv6 netmask/prefix length.
  85. * @param ip6Alloc the address block to allocate to the client (v6) 0 is illegal
  86. * @param ip4Addr the IPv4 address which the node will be issued or NULL.
  87. * @param ip4Prefix the IPv4 netmask/prefix length.
  88. * @param ip4Alloc the address block size to allocate to the client (v4) 0 is illegal
  89. * @param tunnel the IpTunnel.
  90. * @return an connection number which is usable with IpTunnel_remove().
  91. */
  92. int IpTunnel_allowConnection(uint8_t publicKeyOfAuthorizedNode[32],
  93. struct Sockaddr* ip6Addr,
  94. uint8_t ip6Prefix, uint8_t ip6Alloc,
  95. struct Sockaddr* ip4Addr,
  96. uint8_t ip4Prefix, uint8_t ip4Alloc,
  97. struct IpTunnel* tunnel);
  98. /**
  99. * Connect to another node and get IPv4 and/or IPv6 addresses from it.
  100. *
  101. * @param publicKeyOfNodeToConnectTo the key for the node to connect to.
  102. * @param tunnel the IpTunnel.
  103. * @return an connection number which is usable with IpTunnel_remove().
  104. */
  105. int IpTunnel_connectTo(uint8_t publicKeyOfNodeToConnectTo[32], struct IpTunnel* tunnel);
  106. /**
  107. * Disconnect from a node or remove authorization to connect.
  108. *
  109. * @param connectionNumber the number of the connection to remove.
  110. * @param tunnel the IpTunnel.
  111. * @return 0 if the connection was successfully removed
  112. * IpTunnel_remove_NOT_FOUND if the connection was not found.
  113. */
  114. #define IpTunnel_removeConnection_NOT_FOUND -1
  115. int IpTunnel_removeConnection(int connectionNumber, struct IpTunnel* tunnel);
  116. void IpTunnel_setTunName(char* interfaceName, struct IpTunnel* ipTun);
  117. #endif