EventEmitter.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include "memory/Allocator.h"
  16. #include "wire/PFChan.h"
  17. #include "net/EventEmitter.h"
  18. #include "util/Identity.h"
  19. #include "util/log/Log.h"
  20. #include "wire/Error.h"
  21. #include "util/version/Version.h"
  22. #include <stdbool.h>
  23. #define ArrayList_TYPE struct Iface
  24. #define ArrayList_NAME Ifaces
  25. #include "util/ArrayList.h"
  26. #define PING_MAGIC 0x01234567
  27. struct Pathfinder
  28. {
  29. struct EventEmitter_pvt* ee;
  30. struct Iface iface;
  31. struct Allocator* alloc;
  32. uint8_t userAgent[64];
  33. uint32_t superiority;
  34. uint32_t version;
  35. #define Pathfinder_state_DISCONNECTED 0
  36. #define Pathfinder_state_CONNECTED 1
  37. #define Pathfinder_state_ERROR 2
  38. uint16_t state;
  39. uint16_t pathfinderId;
  40. /**
  41. * Number of bytes sent since the last ping, each ping contains this number at the time
  42. * of pinging, then the number is incremented as more messages are sent, if it goes over
  43. * 65535, the state is set to error.
  44. */
  45. uint32_t bytesSinceLastPing;
  46. Identity
  47. };
  48. #define ArrayList_TYPE struct Pathfinder
  49. #define ArrayList_NAME Pathfinders
  50. #include "util/ArrayList.h"
  51. struct EventEmitter_pvt
  52. {
  53. struct EventEmitter pub;
  54. struct Iface trickIf;
  55. struct Allocator* alloc;
  56. struct Log* log;
  57. struct ArrayList_Ifaces* listTable[PFChan_Pathfinder__TOO_HIGH - PFChan_Pathfinder__TOO_LOW];
  58. struct ArrayList_Pathfinders* pathfinders;
  59. uint8_t publicKey[32];
  60. Identity
  61. };
  62. #define OFFSET(ev) (ev - PFChan_Pathfinder__TOO_LOW - 1)
  63. static struct ArrayList_Ifaces* getHandlers(struct EventEmitter_pvt* ee,
  64. enum PFChan_Pathfinder ev,
  65. bool create)
  66. {
  67. if (ev <= PFChan_Pathfinder__TOO_LOW || ev >= PFChan_Pathfinder__TOO_HIGH) { return NULL; }
  68. struct ArrayList_Ifaces* out = ee->listTable[OFFSET(ev)];
  69. if (!out) {
  70. out = ee->listTable[OFFSET(ev)] = ArrayList_Ifaces_new(ee->alloc);
  71. }
  72. return out;
  73. }
  74. static Iface_DEFUN sendToPathfinder(Message_t* msg, struct Pathfinder* pf)
  75. {
  76. if (!pf || pf->state != Pathfinder_state_CONNECTED) { return NULL; }
  77. if (pf->bytesSinceLastPing < 8192 && pf->bytesSinceLastPing + Message_getLength(msg) >= 8192) {
  78. Message_t* ping = Message_new(0, 512, Message_getAlloc(msg));
  79. Err(Message_epush32be(ping, pf->bytesSinceLastPing));
  80. Err(Message_epush32be(ping, PING_MAGIC));
  81. Err(Message_epush32be(ping, PFChan_Core_PING));
  82. Iface_send(&pf->iface, ping);
  83. }
  84. pf->bytesSinceLastPing += Message_getLength(msg);
  85. return Iface_next(&pf->iface, msg);
  86. }
  87. static bool PFChan_Pathfinder_sizeOk(enum PFChan_Pathfinder ev, int size)
  88. {
  89. switch (ev) {
  90. case PFChan_Pathfinder_CONNECT:
  91. return (size == 8 + PFChan_Pathfinder_Connect_SIZE);
  92. case PFChan_Pathfinder_SUPERIORITY:
  93. return (size == 8 + PFChan_Pathfinder_Superiority_SIZE);
  94. case PFChan_Pathfinder_NODE:
  95. case PFChan_Pathfinder_SNODE:
  96. return (size == 8 + PFChan_Node_SIZE);
  97. case PFChan_Pathfinder_SENDMSG:
  98. return (size >= 8 + PFChan_Msg_MIN_SIZE);
  99. case PFChan_Pathfinder_PING:
  100. case PFChan_Pathfinder_PONG:
  101. return (size >= 8 + PFChan_Ping_SIZE);
  102. case PFChan_Pathfinder_SESSIONS:
  103. case PFChan_Pathfinder_PEERS:
  104. case PFChan_Pathfinder_PATHFINDERS:
  105. return (size == 8);
  106. case PFChan_Pathfinder_CTRL_SENDMSG:
  107. return (size >= 8 + PFChan_CtrlMsg_MIN_SIZE);
  108. case PFChan_Pathfinder_CONNECT_PEER:
  109. return (size == 8 + sizeof(PFChan_Pathfinder_ConnectPeer_t));
  110. default:;
  111. }
  112. Assert_failure("invalid event [%d]", ev);
  113. }
  114. // Forget to add the event here? :)
  115. Assert_compileTime(PFChan_Pathfinder__TOO_LOW == 511);
  116. Assert_compileTime(PFChan_Pathfinder__TOO_HIGH == 524);
  117. static bool PFChan_Core_sizeOk(enum PFChan_Core ev, int size)
  118. {
  119. switch (ev) {
  120. case PFChan_Core_CONNECT:
  121. return (size == 8 + PFChan_Core_Connect_SIZE);
  122. case PFChan_Core_PATHFINDER:
  123. case PFChan_Core_PATHFINDER_GONE:
  124. return (size == 8 + PFChan_Core_Pathfinder_SIZE);
  125. case PFChan_Core_SWITCH_ERR:
  126. return (size >= 8 + PFChan_Core_SwitchErr_MIN_SIZE);
  127. case PFChan_Core_SEARCH_REQ:
  128. return (size == 8 + PFChan_Core_SearchReq_SIZE);
  129. case PFChan_Core_PEER:
  130. case PFChan_Core_PEER_GONE:
  131. case PFChan_Core_SESSION:
  132. case PFChan_Core_SESSION_ENDED:
  133. case PFChan_Core_DISCOVERED_PATH:
  134. case PFChan_Core_UNSETUP_SESSION:
  135. return (size == 8 + PFChan_Node_SIZE);
  136. case PFChan_Core_MSG:
  137. return (size >= 8 + PFChan_Msg_MIN_SIZE);
  138. case PFChan_Core_PING:
  139. case PFChan_Core_PONG:
  140. return (size == 8 + PFChan_Ping_SIZE);
  141. case PFChan_Core_CTRL_MSG:
  142. return (size >= 8 + PFChan_CtrlMsg_MIN_SIZE);
  143. case PFChan_Core_LINK_STATE:
  144. return (size >= 8 + PFChan_LinkState_Entry_SIZE) &&
  145. !((size - 8) % PFChan_LinkState_Entry_SIZE);
  146. default:;
  147. }
  148. Assert_failure("invalid event [%d]", ev);
  149. }
  150. // Remember to add the event to this function too!
  151. Assert_compileTime(PFChan_Core__TOO_LOW == 1023);
  152. Assert_compileTime(PFChan_Core__TOO_HIGH == 1040);
  153. static Iface_DEFUN incomingFromCore(Message_t* msg, struct Iface* trickIf)
  154. {
  155. struct EventEmitter_pvt* ee = Identity_containerOf(trickIf, struct EventEmitter_pvt, trickIf);
  156. Assert_true(!((uintptr_t)Message_bytes(msg) % 4) && "alignment");
  157. enum PFChan_Core ev = -1;
  158. Err(Message_epop32be(&ev, msg));
  159. Assert_true(PFChan_Core_sizeOk(ev, Message_getLength(msg)+4));
  160. uint32_t pathfinderNum = 0;
  161. Err(Message_epop32be(&pathfinderNum, msg));
  162. Err(Message_epush32be(msg, ev));
  163. if (pathfinderNum != 0xffffffff) {
  164. struct Pathfinder* pf = ArrayList_Pathfinders_get(ee->pathfinders, pathfinderNum);
  165. Assert_true(pf && pf->state == Pathfinder_state_CONNECTED);
  166. return sendToPathfinder(msg, pf);
  167. } else {
  168. for (int i = 0; i < ee->pathfinders->length; i++) {
  169. struct Pathfinder* pf = ArrayList_Pathfinders_get(ee->pathfinders, i);
  170. if (!pf || pf->state != Pathfinder_state_CONNECTED) { continue; }
  171. Message_t* messageClone = Message_clone(msg, Message_getAlloc(msg));
  172. Iface_CALL(sendToPathfinder, messageClone, pf);
  173. }
  174. }
  175. return NULL;
  176. }
  177. static Message_t* pathfinderMsg(enum PFChan_Core ev,
  178. struct Pathfinder* pf,
  179. struct Allocator* alloc)
  180. {
  181. Message_t* msg = Message_new(PFChan_Core_Pathfinder_SIZE, 512, alloc);
  182. struct PFChan_Core_Pathfinder* pathfinder = (struct PFChan_Core_Pathfinder*) Message_bytes(msg);
  183. pathfinder->superiority_be = Endian_hostToBigEndian32(pf->superiority);
  184. pathfinder->pathfinderId_be = Endian_hostToBigEndian32(pf->pathfinderId);
  185. Bits_memcpy(pathfinder->userAgent, pf->userAgent, 64);
  186. Err_assert(Message_epush32be(msg, 0xffffffff));
  187. Err_assert(Message_epush32be(msg, ev));
  188. return msg;
  189. }
  190. static int handleFromPathfinder(enum PFChan_Pathfinder ev,
  191. Message_t* msg,
  192. struct EventEmitter_pvt* ee,
  193. struct Pathfinder* pf)
  194. {
  195. switch (ev) {
  196. default: return false;
  197. case PFChan_Pathfinder_CONNECT: {
  198. struct PFChan_Pathfinder_Connect connect;
  199. Err_assert(Message_eshift(msg, -8));
  200. Err_assert(Message_epop(msg, &connect, PFChan_Pathfinder_Connect_SIZE));
  201. pf->superiority = Endian_bigEndianToHost32(connect.superiority_be);
  202. pf->version = Endian_bigEndianToHost32(connect.version_be);
  203. Bits_memcpy(pf->userAgent, connect.userAgent, 64);
  204. pf->state = Pathfinder_state_CONNECTED;
  205. struct PFChan_Core_Connect resp;
  206. resp.version_be = Endian_bigEndianToHost32(Version_CURRENT_PROTOCOL);
  207. resp.pathfinderId_be = Endian_hostToBigEndian32(pf->pathfinderId);
  208. Bits_memcpy(resp.publicKey, ee->publicKey, 32);
  209. Err_assert(Message_epush(msg, &resp, PFChan_Core_Connect_SIZE));
  210. Err_assert(Message_epush32be(msg, PFChan_Core_CONNECT));
  211. Message_t* sendMsg = Message_clone(msg, Message_getAlloc(msg));
  212. Iface_CALL(sendToPathfinder, sendMsg, pf);
  213. break;
  214. }
  215. case PFChan_Pathfinder_SUPERIORITY: {
  216. Err_assert(Message_eshift(msg, -8));
  217. Err_assert(Message_epop32be(&pf->superiority, msg));
  218. Message_t* resp = pathfinderMsg(PFChan_Core_PATHFINDER, pf, Message_getAlloc(msg));
  219. Iface_CALL(incomingFromCore, resp, &ee->trickIf);
  220. break;
  221. }
  222. case PFChan_Pathfinder_PING: {
  223. Message_t* sendMsg = Message_clone(msg, Message_getAlloc(msg));
  224. Iface_send(&pf->iface, sendMsg);
  225. break;
  226. }
  227. case PFChan_Pathfinder_PONG: {
  228. Err_assert(Message_eshift(msg, -8));
  229. uint32_t cookie = 0;
  230. Err_assert(Message_epop32be(&cookie, msg));
  231. uint32_t count = 0;
  232. Err_assert(Message_epop32be(&count, msg));
  233. if (cookie != PING_MAGIC || count > pf->bytesSinceLastPing) {
  234. pf->state = Pathfinder_state_ERROR;
  235. Message_t* resp = pathfinderMsg(PFChan_Core_PATHFINDER_GONE, pf, Message_getAlloc(msg));
  236. Iface_CALL(incomingFromCore, resp, &ee->trickIf);
  237. } else {
  238. pf->bytesSinceLastPing -= count;
  239. }
  240. break;
  241. }
  242. case PFChan_Pathfinder_PATHFINDERS: {
  243. for (int i = 0; i < ee->pathfinders->length; i++) {
  244. struct Pathfinder* xpf = ArrayList_Pathfinders_get(ee->pathfinders, i);
  245. if (!xpf || xpf->state != Pathfinder_state_CONNECTED) { continue; }
  246. struct Allocator* alloc = Allocator_child(Message_getAlloc(msg));
  247. Message_t* resp = pathfinderMsg(PFChan_Core_PATHFINDER, pf, alloc);
  248. Iface_CALL(sendToPathfinder, resp, pf);
  249. Allocator_free(alloc);
  250. }
  251. break;
  252. }
  253. }
  254. return true;
  255. }
  256. static Iface_DEFUN incomingFromPathfinder(Message_t* msg, struct Iface* iface)
  257. {
  258. struct Pathfinder* pf = Identity_containerOf(iface, struct Pathfinder, iface);
  259. struct EventEmitter_pvt* ee = Identity_check((struct EventEmitter_pvt*) pf->ee);
  260. if (Message_getLength(msg) < 4) {
  261. Log_debug(ee->log, "DROPPF runt");
  262. return Error(msg, "RUNT");
  263. }
  264. enum PFChan_Pathfinder ev = 0;
  265. Err(Message_epop32be(&ev, msg));
  266. Err(Message_epush32be(msg, pf->pathfinderId));
  267. Err(Message_epush32be(msg, ev));
  268. if (ev <= PFChan_Pathfinder__TOO_LOW || ev >= PFChan_Pathfinder__TOO_HIGH) {
  269. Log_debug(ee->log, "DROPPF invalid type [%d]", ev);
  270. return Error(msg, "INVALID");
  271. }
  272. if (!PFChan_Pathfinder_sizeOk(ev, Message_getLength(msg))) {
  273. Log_debug(ee->log, "DROPPF incorrect length[%d] for type [%d]", Message_getLength(msg), ev);
  274. return Error(msg, "INVALID");
  275. }
  276. if (pf->state == Pathfinder_state_DISCONNECTED) {
  277. if (ev != PFChan_Pathfinder_CONNECT) {
  278. Log_debug(ee->log, "DROPPF disconnected and event != CONNECT event:[%d]", ev);
  279. return Error(msg, "INVALID");
  280. }
  281. } else if (pf->state != Pathfinder_state_CONNECTED) {
  282. Log_debug(ee->log, "DROPPF error state");
  283. return Error(msg, "INVALID");
  284. }
  285. if (handleFromPathfinder(ev, msg, ee, pf)) { return NULL; }
  286. struct ArrayList_Ifaces* handlers = getHandlers(ee, ev, false);
  287. if (!handlers) { return NULL; }
  288. for (int i = 0; i < handlers->length; i++) {
  289. Message_t* messageClone = Message_clone(msg, Message_getAlloc(msg));
  290. struct Iface* iface = ArrayList_Ifaces_get(handlers, i);
  291. // We have to call this manually because we don't have an interface handy which is
  292. // actually plumbed with this one.
  293. Assert_true(iface);
  294. Assert_true(iface->send);
  295. Iface_CALL(iface->send, messageClone, iface);
  296. }
  297. return NULL;
  298. }
  299. void EventEmitter_regCore(struct EventEmitter* eventEmitter,
  300. struct Iface* iface,
  301. enum PFChan_Pathfinder ev)
  302. {
  303. struct EventEmitter_pvt* ee = Identity_check((struct EventEmitter_pvt*) eventEmitter);
  304. iface->connectedIf = &ee->trickIf;
  305. Iface_setIdentity(iface);
  306. struct ArrayList_Ifaces* l = getHandlers(ee, ev, true);
  307. if (!l) {
  308. Assert_true(ev == 0);
  309. return;
  310. }
  311. ArrayList_Ifaces_add(l, iface);
  312. }
  313. void EventEmitter_regPathfinderIface(struct EventEmitter* emitter, struct Iface* iface)
  314. {
  315. struct EventEmitter_pvt* ee = Identity_check((struct EventEmitter_pvt*) emitter);
  316. struct Allocator* alloc = Allocator_child(ee->alloc);
  317. struct Pathfinder* pf = Allocator_calloc(alloc, sizeof(struct Pathfinder), 1);
  318. pf->ee = ee;
  319. pf->iface.send = incomingFromPathfinder;
  320. pf->alloc = alloc;
  321. Iface_plumb(&pf->iface, iface);
  322. Identity_set(pf);
  323. int i = 0;
  324. for (; i < ee->pathfinders->length; i++) {
  325. struct Pathfinder* xpf = ArrayList_Pathfinders_get(ee->pathfinders, i);
  326. if (!xpf) { break; }
  327. }
  328. pf->pathfinderId = ArrayList_Pathfinders_put(ee->pathfinders, i, pf);
  329. }
  330. struct EventEmitter* EventEmitter_new(struct Allocator* allocator,
  331. struct Log* log,
  332. uint8_t* publicKey)
  333. {
  334. struct Allocator* alloc = Allocator_child(allocator);
  335. struct EventEmitter_pvt* ee = Allocator_calloc(alloc, sizeof(struct EventEmitter_pvt), 1);
  336. ee->log = log;
  337. ee->alloc = alloc;
  338. ee->trickIf.send = incomingFromCore;
  339. Iface_setIdentity(&ee->trickIf);
  340. ee->pathfinders = ArrayList_Pathfinders_new(ee->alloc);
  341. Bits_memcpy(ee->publicKey, publicKey, 32);
  342. Identity_set(ee);
  343. return &ee->pub;
  344. }