1
0

InterfaceController.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 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_INIT = CryptoAuth_State_INIT,
  38. InterfaceController_PeerState_SENT_HELLO = CryptoAuth_State_SENT_HELLO,
  39. InterfaceController_PeerState_RECEIVED_HELLO = CryptoAuth_State_RECEIVED_HELLO,
  40. InterfaceController_PeerState_SENT_KEY = CryptoAuth_State_SENT_KEY,
  41. InterfaceController_PeerState_RECEIVED_KEY = CryptoAuth_State_RECEIVED_KEY,
  42. /** In state == ESTABLISHED, we know the node at the other end is authentic. */
  43. InterfaceController_PeerState_ESTABLISHED = CryptoAuth_State_ESTABLISHED,
  44. /** If state == UNRESPONSIVE, the peer has not responded to pings in the required timeframe. */
  45. InterfaceController_PeerState_UNRESPONSIVE = -1,
  46. /** If state is UNAUTHENTICATED, the other node has not sent a single valid packet. */
  47. InterfaceController_PeerState_UNAUTHENTICATED = -2,
  48. };
  49. static inline char* InterfaceController_stateString(enum InterfaceController_PeerState ps)
  50. {
  51. switch (ps) {
  52. case InterfaceController_PeerState_INIT: return "INIT";
  53. case InterfaceController_PeerState_SENT_HELLO: return "SENT_HELLO";
  54. case InterfaceController_PeerState_RECEIVED_HELLO: return "RECEIVED_HELLO";
  55. case InterfaceController_PeerState_SENT_KEY: return "SENT_KEY";
  56. case InterfaceController_PeerState_RECEIVED_KEY: return "RECEIVED_KEY";
  57. case InterfaceController_PeerState_ESTABLISHED: return "ESTABLISHED";
  58. case InterfaceController_PeerState_UNRESPONSIVE: return "UNRESPONSIVE";
  59. case InterfaceController_PeerState_UNAUTHENTICATED: return "UNAUTHENTICATED";
  60. default: return "INVALID";
  61. }
  62. }
  63. /**
  64. * Stats about a peer
  65. */
  66. struct InterfaceController_PeerStats
  67. {
  68. struct Address addr;
  69. int state;
  70. uint64_t timeOfLastMessage;
  71. uint64_t bytesOut;
  72. uint64_t bytesIn;
  73. bool isIncomingConnection;
  74. String* user;
  75. /** Packet loss/duplication statistics. see: ReplayProtector */
  76. uint32_t duplicates;
  77. uint32_t lostPackets;
  78. uint32_t receivedOutOfRange;
  79. uint32_t sendKbps;
  80. uint32_t recvKbps;
  81. };
  82. struct InterfaceController
  83. {
  84. int unused;
  85. };
  86. struct InterfaceController_Iface
  87. {
  88. struct Iface addrIf;
  89. /** Interface number within InterfaceController. */
  90. int ifNum;
  91. };
  92. /**
  93. * Register an Ethernet-like interface.
  94. * Ethernet-like means the interface is capable of sending messages to one or more nodes
  95. * and differentiates between them using an address.
  96. *
  97. * @param ifc the interface controller
  98. * @param name a string which will identify this interface
  99. * @param alloc an allocator, the interface will be removed when this is freed.
  100. */
  101. struct InterfaceController_Iface* InterfaceController_newIface(struct InterfaceController* ifc,
  102. String* name,
  103. struct Allocator* alloc);
  104. /**
  105. * Add a new peer.
  106. * Called from the network interface when it is asked to make a connection or it autoconnects.
  107. * If the peer which is connected to becomes unresponsive, IC will *not* remove it but will
  108. * set it's state to UNRESPONSIVE and it is the job of the caller to remove the peer by freeing
  109. * the allocator which is provided with iface.
  110. *
  111. * @param ifc the interface controller.
  112. * @param interfaceNumber a number for the interface to use, see regIface.
  113. * @param herPublicKey the public key of the foreign node, NULL if unknown.
  114. * @param lladdr the link level address, must be the size given by the interface for interfaceNumber
  115. * @param password the password for authenticating with the other node.
  116. * @param login an identity to provide to the other node with the password,
  117. * if null then authtype 1 will be used.
  118. * @param displayName the username to assign the other node in the CryptoAuth session. May be null.
  119. * @param alloc the peer will be dropped if this is freed.
  120. *
  121. * @return 0 if all goes well.
  122. * InterfaceController_bootstrapPeer_BAD_IFNUM if there is no such interface for this num.
  123. * InterfaceController_bootstrapPeer_OUT_OF_SPACE if there is no space to store the peer.
  124. * InterfaceController_bootstrapPeer_BAD_KEY the provided herPublicKey is not valid.
  125. * InterfaceController_bootstrapPeer_INTERNAL unspecified error.
  126. */
  127. #define InterfaceController_bootstrapPeer_BAD_IFNUM -1
  128. #define InterfaceController_bootstrapPeer_BAD_KEY -2
  129. #define InterfaceController_bootstrapPeer_OUT_OF_SPACE -3
  130. #define InterfaceController_bootstrapPeer_INTERNAL -4
  131. int InterfaceController_bootstrapPeer(struct InterfaceController* ifc,
  132. int interfaceNumber,
  133. uint8_t* herPublicKey,
  134. const struct Sockaddr* lladdr,
  135. String* password,
  136. String* login,
  137. String* displayName,
  138. struct Allocator* alloc);
  139. #define InterfaceController_beaconState_newState_OFF 0
  140. #define InterfaceController_beaconState_newState_ACCEPT 1
  141. #define InterfaceController_beaconState_newState_SEND 2
  142. #define InterfaceController_beaconState_NO_SUCH_IFACE -1
  143. #define InterfaceController_beaconState_INVALID_STATE -2
  144. int InterfaceController_beaconState(struct InterfaceController* ifc,
  145. int interfaceNumber,
  146. int newState);
  147. /**
  148. * CryptoAuth_reset() a peer to reestablish the connection.
  149. *
  150. * @param ic the if controller
  151. * @param herPublicKey the public key of the foreign node or NULL for all peers
  152. * @return void
  153. */
  154. void InterfaceController_resetPeering(struct InterfaceController* ifController,
  155. uint8_t herPublicKey[32]);
  156. /**
  157. * Disconnect a previously registered peer.
  158. *
  159. * @param ic the if controller
  160. * @param herPublicKey the public key of the foreign node
  161. * @return 0 if all goes well.
  162. * InterfaceController_disconnectPeer_NOTFOUND if no peer with herPublicKey is found.
  163. */
  164. #define InterfaceController_disconnectPeer_NOTFOUND -1
  165. int InterfaceController_disconnectPeer(struct InterfaceController* ifc, uint8_t herPublicKey[32]);
  166. /**
  167. * Get stats for the connected peers.
  168. *
  169. * @params ic the if controller
  170. * @params alloc the Allocator to use for the peerStats array in statsOut
  171. * @params statsOut pointer to the InterfaceController_peerStats array
  172. * @return the number of InterfaceController_peerStats in statsOut
  173. */
  174. int InterfaceController_getPeerStats(struct InterfaceController* ic,
  175. struct Allocator* alloc,
  176. struct InterfaceController_PeerStats** statsOut);
  177. struct InterfaceController* InterfaceController_new(struct CryptoAuth* ca,
  178. struct SwitchCore* switchCore,
  179. struct Log* logger,
  180. struct EventBase* eventBase,
  181. struct SwitchPinger* switchPinger,
  182. struct Random* rand,
  183. struct Allocator* allocator,
  184. struct EventEmitter* ee);
  185. #endif