1
0

EventEmitter.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. Message_push32(ping, pf->bytesSinceLastPing, NULL);
  78. Message_push32(ping, PING_MAGIC, NULL);
  79. Message_push32(ping, PFChan_Core_PING, NULL);
  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. default:;
  140. }
  141. Assert_failure("invalid event [%d]", ev);
  142. }
  143. // Remember to add the event to this function too!
  144. Assert_compileTime(PFChan_Core__TOO_LOW == 1023);
  145. Assert_compileTime(PFChan_Core__TOO_HIGH == 1039);
  146. static Iface_DEFUN incomingFromCore(struct Message* msg, struct Iface* trickIf)
  147. {
  148. struct EventEmitter_pvt* ee = Identity_containerOf(trickIf, struct EventEmitter_pvt, trickIf);
  149. Assert_true(!((uintptr_t)msg->bytes % 4) && "alignment");
  150. enum PFChan_Core ev = Message_pop32(msg, NULL);
  151. Assert_true(PFChan_Core_sizeOk(ev, msg->length+4));
  152. uint32_t pathfinderNum = Message_pop32(msg, NULL);
  153. Message_push32(msg, ev, NULL);
  154. if (pathfinderNum != 0xffffffff) {
  155. struct Pathfinder* pf = ArrayList_Pathfinders_get(ee->pathfinders, pathfinderNum);
  156. Assert_true(pf && pf->state == Pathfinder_state_CONNECTED);
  157. return sendToPathfinder(msg, pf);
  158. } else {
  159. for (int i = 0; i < ee->pathfinders->length; i++) {
  160. struct Pathfinder* pf = ArrayList_Pathfinders_get(ee->pathfinders, i);
  161. if (!pf || pf->state != Pathfinder_state_CONNECTED) { continue; }
  162. struct Message* messageClone = Message_clone(msg, msg->alloc);
  163. Iface_CALL(sendToPathfinder, messageClone, pf);
  164. }
  165. }
  166. return NULL;
  167. }
  168. static struct Message* pathfinderMsg(enum PFChan_Core ev,
  169. struct Pathfinder* pf,
  170. struct Allocator* alloc)
  171. {
  172. struct Message* msg = Message_new(PFChan_Core_Pathfinder_SIZE, 512, alloc);
  173. struct PFChan_Core_Pathfinder* pathfinder = (struct PFChan_Core_Pathfinder*) msg->bytes;
  174. pathfinder->superiority_be = Endian_hostToBigEndian32(pf->superiority);
  175. pathfinder->pathfinderId_be = Endian_hostToBigEndian32(pf->pathfinderId);
  176. Bits_memcpy(pathfinder->userAgent, pf->userAgent, 64);
  177. Message_push32(msg, 0xffffffff, NULL);
  178. Message_push32(msg, ev, NULL);
  179. return msg;
  180. }
  181. static int handleFromPathfinder(enum PFChan_Pathfinder ev,
  182. struct Message* msg,
  183. struct EventEmitter_pvt* ee,
  184. struct Pathfinder* pf)
  185. {
  186. switch (ev) {
  187. default: return false;
  188. case PFChan_Pathfinder_CONNECT: {
  189. struct PFChan_Pathfinder_Connect connect;
  190. Message_shift(msg, -8, NULL);
  191. Message_pop(msg, &connect, PFChan_Pathfinder_Connect_SIZE, NULL);
  192. pf->superiority = Endian_bigEndianToHost32(connect.superiority_be);
  193. pf->version = Endian_bigEndianToHost32(connect.version_be);
  194. Bits_memcpy(pf->userAgent, connect.userAgent, 64);
  195. pf->state = Pathfinder_state_CONNECTED;
  196. struct PFChan_Core_Connect resp;
  197. resp.version_be = Endian_bigEndianToHost32(Version_CURRENT_PROTOCOL);
  198. resp.pathfinderId_be = Endian_hostToBigEndian32(pf->pathfinderId);
  199. Bits_memcpy(resp.publicKey, ee->publicKey, 32);
  200. Message_push(msg, &resp, PFChan_Core_Connect_SIZE, NULL);
  201. Message_push32(msg, PFChan_Core_CONNECT, NULL);
  202. struct Message* sendMsg = Message_clone(msg, msg->alloc);
  203. Iface_CALL(sendToPathfinder, sendMsg, pf);
  204. break;
  205. }
  206. case PFChan_Pathfinder_SUPERIORITY: {
  207. Message_shift(msg, -8, NULL);
  208. pf->superiority = Message_pop32(msg, NULL);
  209. struct Message* resp = pathfinderMsg(PFChan_Core_PATHFINDER, pf, msg->alloc);
  210. Iface_CALL(incomingFromCore, resp, &ee->trickIf);
  211. break;
  212. }
  213. case PFChan_Pathfinder_PING: {
  214. struct Message* sendMsg = Message_clone(msg, msg->alloc);
  215. Iface_send(&pf->iface, sendMsg);
  216. break;
  217. }
  218. case PFChan_Pathfinder_PONG: {
  219. Message_shift(msg, -8, NULL);
  220. uint32_t cookie = Message_pop32(msg, NULL);
  221. uint32_t count = Message_pop32(msg, NULL);
  222. if (cookie != PING_MAGIC || count > pf->bytesSinceLastPing) {
  223. pf->state = Pathfinder_state_ERROR;
  224. struct Message* resp = pathfinderMsg(PFChan_Core_PATHFINDER_GONE, pf, msg->alloc);
  225. Iface_CALL(incomingFromCore, resp, &ee->trickIf);
  226. } else {
  227. pf->bytesSinceLastPing -= count;
  228. }
  229. break;
  230. }
  231. case PFChan_Pathfinder_PATHFINDERS: {
  232. for (int i = 0; i < ee->pathfinders->length; i++) {
  233. struct Pathfinder* xpf = ArrayList_Pathfinders_get(ee->pathfinders, i);
  234. if (!xpf || xpf->state != Pathfinder_state_CONNECTED) { continue; }
  235. struct Allocator* alloc = Allocator_child(msg->alloc);
  236. struct Message* resp = pathfinderMsg(PFChan_Core_PATHFINDER, pf, alloc);
  237. Iface_CALL(sendToPathfinder, resp, pf);
  238. Allocator_free(alloc);
  239. }
  240. break;
  241. }
  242. }
  243. return true;
  244. }
  245. static Iface_DEFUN incomingFromPathfinder(struct Message* msg, struct Iface* iface)
  246. {
  247. struct Pathfinder* pf = Identity_containerOf(iface, struct Pathfinder, iface);
  248. struct EventEmitter_pvt* ee = Identity_check((struct EventEmitter_pvt*) pf->ee);
  249. if (msg->length < 4) {
  250. Log_debug(ee->log, "DROPPF runt");
  251. return NULL;
  252. }
  253. enum PFChan_Pathfinder ev = Message_pop32(msg, NULL);
  254. Message_push32(msg, pf->pathfinderId, NULL);
  255. Message_push32(msg, ev, NULL);
  256. if (ev <= PFChan_Pathfinder__TOO_LOW || ev >= PFChan_Pathfinder__TOO_HIGH) {
  257. Log_debug(ee->log, "DROPPF invalid type [%d]", ev);
  258. return NULL;
  259. }
  260. if (!PFChan_Pathfinder_sizeOk(ev, msg->length)) {
  261. Log_debug(ee->log, "DROPPF incorrect length[%d] for type [%d]", msg->length, ev);
  262. return NULL;
  263. }
  264. if (pf->state == Pathfinder_state_DISCONNECTED) {
  265. if (ev != PFChan_Pathfinder_CONNECT) {
  266. Log_debug(ee->log, "DROPPF disconnected and event != CONNECT event:[%d]", ev);
  267. return NULL;
  268. }
  269. } else if (pf->state != Pathfinder_state_CONNECTED) {
  270. Log_debug(ee->log, "DROPPF error state");
  271. return NULL;
  272. }
  273. if (handleFromPathfinder(ev, msg, ee, pf)) { return NULL; }
  274. struct ArrayList_Ifaces* handlers = getHandlers(ee, ev, false);
  275. if (!handlers) { return NULL; }
  276. for (int i = 0; i < handlers->length; i++) {
  277. struct Message* messageClone = Message_clone(msg, msg->alloc);
  278. struct Iface* iface = ArrayList_Ifaces_get(handlers, i);
  279. // We have to call this manually because we don't have an interface handy which is
  280. // actually plumbed with this one.
  281. Assert_true(iface);
  282. Assert_true(iface->send);
  283. Iface_CALL(iface->send, messageClone, iface);
  284. }
  285. return NULL;
  286. }
  287. void EventEmitter_regCore(struct EventEmitter* eventEmitter,
  288. struct Iface* iface,
  289. enum PFChan_Pathfinder ev)
  290. {
  291. struct EventEmitter_pvt* ee = Identity_check((struct EventEmitter_pvt*) eventEmitter);
  292. iface->connectedIf = &ee->trickIf;
  293. struct ArrayList_Ifaces* l = getHandlers(ee, ev, true);
  294. if (!l) {
  295. Assert_true(ev == 0);
  296. return;
  297. }
  298. ArrayList_Ifaces_add(l, iface);
  299. }
  300. void EventEmitter_regPathfinderIface(struct EventEmitter* emitter, struct Iface* iface)
  301. {
  302. struct EventEmitter_pvt* ee = Identity_check((struct EventEmitter_pvt*) emitter);
  303. struct Allocator* alloc = Allocator_child(ee->alloc);
  304. struct Pathfinder* pf = Allocator_calloc(alloc, sizeof(struct Pathfinder), 1);
  305. pf->ee = ee;
  306. pf->iface.send = incomingFromPathfinder;
  307. pf->alloc = alloc;
  308. Iface_plumb(&pf->iface, iface);
  309. Identity_set(pf);
  310. int i = 0;
  311. for (; i < ee->pathfinders->length; i++) {
  312. struct Pathfinder* xpf = ArrayList_Pathfinders_get(ee->pathfinders, i);
  313. if (!xpf) { break; }
  314. }
  315. pf->pathfinderId = ArrayList_Pathfinders_put(ee->pathfinders, i, pf);
  316. }
  317. struct EventEmitter* EventEmitter_new(struct Allocator* allocator,
  318. struct Log* log,
  319. uint8_t* publicKey)
  320. {
  321. struct Allocator* alloc = Allocator_child(allocator);
  322. struct EventEmitter_pvt* ee = Allocator_calloc(alloc, sizeof(struct EventEmitter_pvt), 1);
  323. ee->log = log;
  324. ee->alloc = alloc;
  325. ee->trickIf.send = incomingFromCore;
  326. ee->pathfinders = ArrayList_Pathfinders_new(ee->alloc);
  327. Bits_memcpy(ee->publicKey, publicKey, 32);
  328. Identity_set(ee);
  329. return &ee->pub;
  330. }