InterfaceController.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 login an identity to provide to the other node with the password,
  116. * if null then authtype 1 will be used.
  117. * @param displayName the username to assign the other node in the CryptoAuth session. May be null.
  118. * @param alloc the peer will be dropped if this is freed.
  119. *
  120. * @return 0 if all goes well.
  121. * InterfaceController_bootstrapPeer_BAD_IFNUM if there is no such interface for this num.
  122. * InterfaceController_bootstrapPeer_OUT_OF_SPACE if there is no space to store the peer.
  123. * InterfaceController_bootstrapPeer_BAD_KEY the provided herPublicKey is not valid.
  124. * InterfaceController_bootstrapPeer_INTERNAL unspecified error.
  125. */
  126. #define InterfaceController_bootstrapPeer_BAD_IFNUM -1
  127. #define InterfaceController_bootstrapPeer_BAD_KEY -2
  128. #define InterfaceController_bootstrapPeer_OUT_OF_SPACE -3
  129. #define InterfaceController_bootstrapPeer_INTERNAL -4
  130. int InterfaceController_bootstrapPeer(struct InterfaceController* ifc,
  131. int interfaceNumber,
  132. uint8_t* herPublicKey,
  133. const struct Sockaddr* lladdr,
  134. String* password,
  135. String* login,
  136. String* displayName,
  137. struct Allocator* alloc);
  138. #define InterfaceController_beaconState_newState_OFF 0
  139. #define InterfaceController_beaconState_newState_ACCEPT 1
  140. #define InterfaceController_beaconState_newState_SEND 2
  141. #define InterfaceController_beaconState_NO_SUCH_IFACE -1
  142. #define InterfaceController_beaconState_INVALID_STATE -2
  143. int InterfaceController_beaconState(struct InterfaceController* ifc,
  144. int interfaceNumber,
  145. int newState);
  146. /**
  147. * Disconnect a previously registered peer.
  148. *
  149. * @param ic the if controller
  150. * @param herPublicKey the public key of the foreign node
  151. * @retrun 0 if all goes well.
  152. * InterfaceController_disconnectPeer_NOTFOUND if no peer with herPublicKey is found.
  153. */
  154. #define InterfaceController_disconnectPeer_NOTFOUND -1
  155. int InterfaceController_disconnectPeer(struct InterfaceController* ifc, uint8_t herPublicKey[32]);
  156. /**
  157. * Get stats for the connected peers.
  158. *
  159. * @params ic the if controller
  160. * @params alloc the Allocator to use for the peerStats array in statsOut
  161. * @params statsOut pointer to the InterfaceController_peerStats array
  162. * @return the number of InterfaceController_peerStats in statsOut
  163. */
  164. int InterfaceController_getPeerStats(struct InterfaceController* ic,
  165. struct Allocator* alloc,
  166. struct InterfaceController_PeerStats** statsOut);
  167. struct InterfaceController* InterfaceController_new(struct CryptoAuth* ca,
  168. struct SwitchCore* switchCore,
  169. struct Log* logger,
  170. struct EventBase* eventBase,
  171. struct SwitchPinger* switchPinger,
  172. struct Random* rand,
  173. struct Allocator* allocator,
  174. struct EventEmitter* ee);
  175. #endif