1
0

EventEmitter.c 14 KB

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