InterfaceController.h 9.5 KB

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