InterfaceController.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 InterfaceController_H
  16. #define InterfaceController_H
  17. #include "benc/String.h"
  18. #include "interface/Interface.h"
  19. #include "wire/Headers.h"
  20. #include "crypto/CryptoAuth.h"
  21. #include "dht/dhtcore/Router.h"
  22. #include "dht/dhtcore/RumorMill.h"
  23. #include "dht/Address.h"
  24. #include "interface/Interface.h"
  25. #include "memory/Allocator.h"
  26. #include "switch/SwitchCore.h"
  27. #include "net/SwitchPinger.h"
  28. #include "util/platform/Sockaddr.h"
  29. #include "util/log/Log.h"
  30. #include "util/Linker.h"
  31. Linker_require("interface/InterfaceController.c")
  32. #include <stdint.h>
  33. #include <stdbool.h>
  34. enum InterfaceController_PeerState
  35. {
  36. /**
  37. * In state >= NEW, a valid packet has been received but it could still be a replay.
  38. * Or it's an outgoing connection so we don't care about authentication.
  39. */
  40. InterfaceController_PeerState_NEW = CryptoAuth_NEW,
  41. InterfaceController_PeerState_HANDSHAKE1 = CryptoAuth_HANDSHAKE1,
  42. InterfaceController_PeerState_HANDSHAKE2 = CryptoAuth_HANDSHAKE2,
  43. InterfaceController_PeerState_HANDSHAKE3 = CryptoAuth_HANDSHAKE3,
  44. /** In state == ESTABLISHED, we know the node at the other end is authentic. */
  45. InterfaceController_PeerState_ESTABLISHED = CryptoAuth_ESTABLISHED,
  46. /** If state == UNRESPONSIVE, the peer has not responded to pings in the required timeframe. */
  47. InterfaceController_PeerState_UNRESPONSIVE = -1,
  48. /** If state is UNAUTHENTICATED, the other node has not sent a single valid packet. */
  49. InterfaceController_PeerState_UNAUTHENTICATED = -2,
  50. };
  51. Assert_compileTime(CryptoAuth_STATE_COUNT == 5);
  52. static inline char* InterfaceController_stateString(enum InterfaceController_PeerState ps)
  53. {
  54. switch (ps) {
  55. case InterfaceController_PeerState_NEW: return "NEW";
  56. case InterfaceController_PeerState_HANDSHAKE1: return "HANDSHAKE1";
  57. case InterfaceController_PeerState_HANDSHAKE2: return "HANDSHAKE2";
  58. case InterfaceController_PeerState_HANDSHAKE3: return "HANDSHAKE3";
  59. case InterfaceController_PeerState_ESTABLISHED: return "ESTABLISHED";
  60. case InterfaceController_PeerState_UNRESPONSIVE: return "UNRESPONSIVE";
  61. case InterfaceController_PeerState_UNAUTHENTICATED: return "UNAUTHENTICATED";
  62. default: return "INVALID";
  63. }
  64. }
  65. /**
  66. * Stats about a peer
  67. */
  68. struct InterfaceController_PeerStats
  69. {
  70. struct Address addr;
  71. int state;
  72. uint64_t timeOfLastMessage;
  73. uint64_t bytesOut;
  74. uint64_t bytesIn;
  75. bool isIncomingConnection;
  76. String* user;
  77. /** Packet loss/duplication statistics. see: ReplayProtector */
  78. uint32_t duplicates;
  79. uint32_t lostPackets;
  80. uint32_t receivedOutOfRange;
  81. };
  82. struct InterfaceController
  83. {
  84. int unused;
  85. };
  86. /**
  87. * Register an Ethernet-like interface.
  88. * Ethernet-like means the interface is capable of sending messages to one or more nodes
  89. * and differentiates between them using an address.
  90. *
  91. * @param ifc the interface controller
  92. * @param addrIface the interface
  93. * @param addrLen the size in bytes of the addresses used by this Addressable Interface.
  94. * @param name a name for the interface, must be globally unique
  95. * @param alloc an allocator, the interface will be removed when this is freed.
  96. * @return the number of the interface in the interface table.
  97. */
  98. int InterfaceController_regIface(struct InterfaceController* ifc,
  99. struct Interface* addrIface,
  100. String* name,
  101. struct Allocator* alloc);
  102. /**
  103. * Add a new peer.
  104. * Called from the network interface when it is asked to make a connection or it autoconnects.
  105. * If the peer which is connected to becomes unresponsive, IC will *not* remove it but will
  106. * set it's state to UNRESPONSIVE and it is the job of the caller to remove the peer by freeing
  107. * the allocator which is provided with iface.
  108. *
  109. * @param ifc the interface controller.
  110. * @param interfaceNumber a number for the interface to use, see regIface.
  111. * @param herPublicKey the public key of the foreign node, NULL if unknown.
  112. * @param lladdr the link level address, must be the size given by the interface for interfaceNumber
  113. * @param password the password for authenticating with the other node.
  114. * @param alloc the peer will be dropped if this is freed.
  115. *
  116. * @return 0 if all goes well.
  117. * InterfaceController_bootstrapPeer_BAD_IFNUM if there is no such interface for this num.
  118. * InterfaceController_bootstrapPeer_OUT_OF_SPACE if there is no space to store the peer.
  119. * InterfaceController_bootstrapPeer_BAD_KEY the provided herPublicKey is not valid.
  120. * InterfaceController_bootstrapPeer_INTERNAL unspecified error.
  121. */
  122. #define InterfaceController_bootstrapPeer_BAD_IFNUM -1
  123. #define InterfaceController_bootstrapPeer_BAD_KEY -2
  124. #define InterfaceController_bootstrapPeer_OUT_OF_SPACE -3
  125. #define InterfaceController_bootstrapPeer_INTERNAL -4
  126. int InterfaceController_bootstrapPeer(struct InterfaceController* ifc,
  127. int interfaceNumber,
  128. uint8_t* herPublicKey,
  129. const struct Sockaddr* lladdr,
  130. String* password,
  131. struct Allocator* alloc);
  132. #define InterfaceController_beaconState_newState_OFF 0
  133. #define InterfaceController_beaconState_newState_ACCEPT 1
  134. #define InterfaceController_beaconState_newState_SEND 2
  135. #define InterfaceController_beaconState_NO_SUCH_IFACE -1
  136. #define InterfaceController_beaconState_INVALID_STATE -2
  137. int InterfaceController_beaconState(struct InterfaceController* ifc,
  138. int interfaceNumber,
  139. int newState);
  140. /**
  141. * Disconnect a previously registered peer.
  142. *
  143. * @param ic the if controller
  144. * @param herPublicKey the public key of the foreign node
  145. * @retrun 0 if all goes well.
  146. * InterfaceController_disconnectPeer_NOTFOUND if no peer with herPublicKey is found.
  147. */
  148. #define InterfaceController_disconnectPeer_NOTFOUND -1
  149. int InterfaceController_disconnectPeer(struct InterfaceController* ifController,
  150. uint8_t herPublicKey[32]);
  151. /**
  152. * Get stats for the connected peers.
  153. *
  154. * @params ic the if controller
  155. * @params alloc the Allocator to use for the peerStats array in statsOut
  156. * @params statsOut pointer to the InterfaceController_peerStats array
  157. * @return the number of InterfaceController_peerStats in statsOut
  158. */
  159. int InterfaceController_getPeerStats(struct InterfaceController* ic,
  160. struct Allocator* alloc,
  161. struct InterfaceController_PeerStats** statsOut);
  162. struct InterfaceController* InterfaceController_new(struct CryptoAuth* ca,
  163. struct SwitchCore* switchCore,
  164. struct Router* router,
  165. struct RumorMill* rumorMill,
  166. struct Log* logger,
  167. struct EventBase* eventBase,
  168. struct SwitchPinger* switchPinger,
  169. struct Random* rand,
  170. struct Allocator* allocator);
  171. #endif