IpTunnel.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef IpTunnel_H
  16. #define IpTunnel_H
  17. #include "admin/angel/Hermes.h"
  18. #include "crypto/random/Random.h"
  19. #include "interface/Interface.h"
  20. #include "memory/Allocator.h"
  21. #include "util/log/Log.h"
  22. #include "util/events/EventBase.h"
  23. #include "util/platform/Sockaddr.h"
  24. #include "util/Linker.h"
  25. Linker_require("tunnel/IpTunnel.c")
  26. #include <stdint.h>
  27. /** This header shall be on all messages sent in and out of the nodeInterface. */
  28. struct IpTunnel_PacketInfoHeader
  29. {
  30. /**
  31. * When the IpTunnel sends a message out the nodeInterface, this is the Ip6 of the node
  32. * which the message should be sent to. When the IpTunnel receives a message from the
  33. * nodeInterface this is the address of the node from which it came.
  34. */
  35. uint8_t nodeIp6Addr[16];
  36. /** The full 32 byte key which corrisponds to the above Ip6 address. */
  37. uint8_t nodeKey[32];
  38. };
  39. #define IpTunnel_PacketInfoHeader_SIZE 48
  40. Assert_compileTime(sizeof(struct IpTunnel_PacketInfoHeader) == IpTunnel_PacketInfoHeader_SIZE);
  41. struct IpTunnel_Connection
  42. {
  43. /** The header which is used for this connection. */
  44. struct IpTunnel_PacketInfoHeader header;
  45. /** The IPv6 address used for this connection or all zeros if none was assigned. */
  46. uint8_t connectionIp6[16];
  47. /** The IPv4 address used for this connection or all zeros if none was assigned. */
  48. uint8_t connectionIp4[4];
  49. /** non-zero if the connection was made using IpTunnel_connectTo(). */
  50. int isOutgoing : 1;
  51. /** The number of the connection so it can be identified when removing. */
  52. int number : 31;
  53. };
  54. struct IpTunnel
  55. {
  56. /** The interface used to send and receive messages to the TUN device. */
  57. struct Interface tunInterface;
  58. /**
  59. * The interface used to send and receive messages to other nodes.
  60. * All messages sent on this interface shall be preceeded with the IpTunnel_PacketInfoHeader.
  61. */
  62. struct Interface nodeInterface;
  63. /**
  64. * The list of registered connections, do not modify manually.
  65. * Will be reorganized from time to time so pointers are ephimeral.
  66. */
  67. struct {
  68. uint32_t count;
  69. struct IpTunnel_Connection* connections;
  70. } connectionList;
  71. };
  72. /**
  73. * Create a new IpTunnel structure.
  74. *
  75. * @param logger a logger or NULL.
  76. * @param eventBase the event base.
  77. * @param alloc an allocator.
  78. * @param rand a random generator.
  79. * @param hermes the Hermes admin connector.
  80. */
  81. struct IpTunnel* IpTunnel_new(struct Log* logger,
  82. struct EventBase* eventBase,
  83. struct Allocator* alloc,
  84. struct Random* rand,
  85. struct Hermes* hermes);
  86. /**
  87. * Allow another node to tunnel IPv4 and/or ICANN IPv6 through this node.
  88. *
  89. * @param publicKeyOfAuthorizedNode the key for the node which will be allowed to connect.
  90. * @param ip6Addr the IPv6 address which the node will be issued or NULL.
  91. * @param ip4Addr the IPv4 address which the node will be issued or NULL.
  92. * @param tunnel the IpTunnel.
  93. * @return an connection number which is usable with IpTunnel_remove().
  94. */
  95. int IpTunnel_allowConnection(uint8_t publicKeyOfAuthorizedNode[32],
  96. struct Sockaddr* ip6Addr,
  97. struct Sockaddr* ip4Addr,
  98. struct IpTunnel* tunnel);
  99. /**
  100. * Connect to another node and get IPv4 and/or IPv6 addresses from it.
  101. *
  102. * @param publicKeyOfNodeToConnectTo the key for the node to connect to.
  103. * @param tunnel the IpTunnel.
  104. * @return an connection number which is usable with IpTunnel_remove().
  105. */
  106. int IpTunnel_connectTo(uint8_t publicKeyOfNodeToConnectTo[32], struct IpTunnel* tunnel);
  107. /**
  108. * Disconnect from a node or remove authorization to connect.
  109. *
  110. * @param connectionNumber the number of the connection to remove.
  111. * @param tunnel the IpTunnel.
  112. * @return 0 if the connection was successfully removed
  113. * IpTunnel_remove_NOT_FOUND if the connection was not found.
  114. */
  115. #define IpTunnel_removeConnection_NOT_FOUND -1
  116. int IpTunnel_removeConnection(int connectionNumber, struct IpTunnel* tunnel);
  117. void IpTunnel_setTunName(char* interfaceName, struct IpTunnel* ipTun);
  118. #endif