1
0

InterfaceController.h 9.5 KB

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