1
0

InterfaceController.h 7.6 KB

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