InterfaceController.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 "crypto/Ca.h"
  18. #include "dht/Address.h"
  19. #include "interface/Iface.h"
  20. #include "memory/Allocator.h"
  21. #include "switch/SwitchCore.h"
  22. #include "net/SwitchPinger.h"
  23. #include "net/EventEmitter.h"
  24. #include "util/platform/Sockaddr.h"
  25. #include "util/log/Log.h"
  26. #include "util/Linker.h"
  27. Linker_require("net/InterfaceController.c")
  28. #include <stdint.h>
  29. #include <stdbool.h>
  30. enum InterfaceController_PeerState
  31. {
  32. /**
  33. * In state >= NEW, a valid packet has been received but it could still be a replay.
  34. * Or it's an outgoing connection so we don't care about authentication.
  35. */
  36. InterfaceController_PeerState_INIT = Ca_State_INIT,
  37. InterfaceController_PeerState_SENT_HELLO = Ca_State_SENT_HELLO,
  38. InterfaceController_PeerState_RECEIVED_HELLO = Ca_State_RECEIVED_HELLO,
  39. InterfaceController_PeerState_SENT_KEY = Ca_State_SENT_KEY,
  40. InterfaceController_PeerState_RECEIVED_KEY = Ca_State_RECEIVED_KEY,
  41. /** In state == ESTABLISHED, we know the node at the other end is authentic. */
  42. InterfaceController_PeerState_ESTABLISHED = Ca_State_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. // The other node has a version which is incompatible with ours, no communication is possible
  48. InterfaceController_PeerState_INCOMPATIBLE = -3,
  49. };
  50. static inline char* InterfaceController_stateString(enum InterfaceController_PeerState ps)
  51. {
  52. switch (ps) {
  53. case InterfaceController_PeerState_INIT: return "INIT";
  54. case InterfaceController_PeerState_SENT_HELLO: return "SENT_HELLO";
  55. case InterfaceController_PeerState_RECEIVED_HELLO: return "RECEIVED_HELLO";
  56. case InterfaceController_PeerState_SENT_KEY: return "SENT_KEY";
  57. case InterfaceController_PeerState_RECEIVED_KEY: return "RECEIVED_KEY";
  58. case InterfaceController_PeerState_ESTABLISHED: return "ESTABLISHED";
  59. case InterfaceController_PeerState_UNRESPONSIVE: return "UNRESPONSIVE";
  60. case InterfaceController_PeerState_UNAUTHENTICATED: return "UNAUTHENTICATED";
  61. case InterfaceController_PeerState_INCOMPATIBLE: return "INCOMPATIBLE";
  62. default: return "INVALID";
  63. }
  64. }
  65. enum InterfaceController_BeaconState
  66. {
  67. InterfaceController_BeaconState_DISABLED,
  68. InterfaceController_BeaconState_ACCEPTING,
  69. InterfaceController_BeaconState_SENDING
  70. };
  71. static inline char* InterfaceController_beaconStateString(enum InterfaceController_BeaconState bs)
  72. {
  73. switch (bs) {
  74. case InterfaceController_BeaconState_DISABLED: return "DISABLED";
  75. case InterfaceController_BeaconState_ACCEPTING: return "ACCEPTING";
  76. case InterfaceController_BeaconState_SENDING: return "SENDING";
  77. default: return "INVALID";
  78. }
  79. }
  80. /**
  81. * Stats about a peer
  82. */
  83. struct InterfaceController_PeerStats
  84. {
  85. struct Address addr;
  86. struct Sockaddr* lladdr;
  87. int state;
  88. int ifNum;
  89. uint64_t timeOfLastMessage;
  90. uint64_t bytesOut;
  91. uint64_t bytesIn;
  92. bool isIncomingConnection;
  93. bool noiseProto;
  94. String* user;
  95. /** Packet loss/duplication statistics. see: ReplayProtector */
  96. uint32_t duplicates;
  97. uint32_t lostPackets;
  98. uint32_t receivedPackets;
  99. uint32_t receivedOutOfRange;
  100. uint32_t sendKbps;
  101. uint32_t recvKbps;
  102. };
  103. struct InterfaceController
  104. {
  105. int opaque;
  106. };
  107. struct InterfaceController_Iface
  108. {
  109. struct Iface addrIf;
  110. /** Interface number within InterfaceController. */
  111. int ifNum;
  112. // Address family, set by caller to InterfaceController_newIface() -1 if unknown.
  113. int af;
  114. enum InterfaceController_BeaconState beaconState;
  115. String* name;
  116. };
  117. /**
  118. * Register an Ethernet-like interface.
  119. * Ethernet-like means the interface is capable of sending messages to one or more nodes
  120. * and differentiates between them using an address.
  121. *
  122. * @param ifc the interface controller
  123. * @param name a string which will identify this interface
  124. * @param alloc an allocator, the interface will be removed when this is freed.
  125. */
  126. struct InterfaceController_Iface* InterfaceController_newIface(struct InterfaceController* ifc,
  127. String* name,
  128. struct Allocator* alloc);
  129. /** Get the number of interfaces registered with the controller. */
  130. int InterfaceController_ifaceCount(struct InterfaceController* ifc);
  131. /** Get an interface from the InterfaceController. */
  132. struct InterfaceController_Iface* InterfaceController_getIface(struct InterfaceController* ifc,
  133. int ifNum);
  134. /**
  135. * Add a new peer.
  136. * Called from the network interface when it is asked to make a connection or it autoconnects.
  137. * If the peer which is connected to becomes unresponsive, IC will *not* remove it but will
  138. * set it's state to UNRESPONSIVE and it is the job of the caller to remove the peer by freeing
  139. * the allocator which is provided with iface.
  140. *
  141. * @param ifc the interface controller.
  142. * @param interfaceNumber a number for the interface to use, see regIface.
  143. * @param herPublicKey the public key of the foreign node, NULL if unknown.
  144. * @param lladdr the link level address, must be the size given by the interface for interfaceNumber
  145. * @param password the password for authenticating with the other node.
  146. * @param login an identity to provide to the other node with the password,
  147. * if null then authtype 1 will be used.
  148. * @param displayName the username to assign the other node in the CryptoAuth session. May be null.
  149. *
  150. * @return 0 if all goes well.
  151. * InterfaceController_bootstrapPeer_BAD_IFNUM if there is no such interface for this num.
  152. * InterfaceController_bootstrapPeer_OUT_OF_SPACE if there is no space to store the peer.
  153. * InterfaceController_bootstrapPeer_BAD_KEY the provided herPublicKey is not valid.
  154. * InterfaceController_bootstrapPeer_INTERNAL unspecified error.
  155. */
  156. #define InterfaceController_bootstrapPeer_BAD_IFNUM -1
  157. #define InterfaceController_bootstrapPeer_BAD_KEY -2
  158. #define InterfaceController_bootstrapPeer_OUT_OF_SPACE -3
  159. #define InterfaceController_bootstrapPeer_INTERNAL -4
  160. int InterfaceController_bootstrapPeer(struct InterfaceController* ifc,
  161. int interfaceNumber,
  162. uint8_t* herPublicKey,
  163. const struct Sockaddr* lladdr,
  164. String* password,
  165. String* login,
  166. String* displayName,
  167. int version);
  168. #define InterfaceController_beaconState_newState_OFF 0
  169. #define InterfaceController_beaconState_newState_ACCEPT 1
  170. #define InterfaceController_beaconState_newState_SEND 2
  171. #define InterfaceController_beaconState_NO_SUCH_IFACE -1
  172. #define InterfaceController_beaconState_INVALID_STATE -2
  173. int InterfaceController_beaconState(struct InterfaceController* ifc,
  174. int interfaceNumber,
  175. int newState);
  176. /**
  177. * Ca_reset() a peer to reestablish the connection.
  178. *
  179. * @param ic the if controller
  180. * @param herPublicKey the public key of the foreign node or NULL for all peers
  181. * @return void
  182. */
  183. void InterfaceController_resetPeering(struct InterfaceController* ifController,
  184. uint8_t herPublicKey[32]);
  185. /**
  186. * Disconnect a previously registered peer.
  187. *
  188. * @param ic the if controller
  189. * @param herPublicKey the public key of the foreign node
  190. * @return number of sessions disconnected
  191. */
  192. int InterfaceController_disconnectPeer(struct InterfaceController* ifc, uint8_t herPublicKey[32]);
  193. struct Sockaddr* InterfaceController_getPeerLlAddr(struct InterfaceController* ifController,
  194. struct Allocator* alloc,
  195. uint64_t peerLabel);
  196. /**
  197. * Get stats for the connected peers.
  198. *
  199. * @params ic the if controller
  200. * @params alloc the Allocator to use for the peerStats array in statsOut
  201. * @params statsOut pointer to the InterfaceController_peerStats array
  202. * @return the number of InterfaceController_peerStats in statsOut
  203. */
  204. int InterfaceController_getPeerStats(struct InterfaceController* ic,
  205. struct Allocator* alloc,
  206. struct InterfaceController_PeerStats** statsOut);
  207. struct InterfaceController* InterfaceController_new(Ca_t* ca,
  208. struct SwitchCore* switchCore,
  209. struct Log* logger,
  210. EventBase_t* eventBase,
  211. struct SwitchPinger* switchPinger,
  212. struct Random* rand,
  213. struct Allocator* allocator,
  214. struct EventEmitter* ee,
  215. bool enableNoise);
  216. #endif