safe_connection.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  1. #include "core/or/safe_connection.h"
  2. #include "app/config/config.h"
  3. #include "lib/net/buffers_net.h"
  4. #include "lib/tls/tortls.h"
  5. #include "lib/tls/buffers_tls.h"
  6. #include "lib/malloc/malloc.h"
  7. #include "core/proto/proto_cell.h"
  8. #include "core/or/connection_or.h"
  9. #include "core/or/var_cell_st.h"
  10. #include "core/or/cell_st.h"
  11. #include "core/or/cell_queue_st.h"
  12. #include "core/mainloop/throughput_logging.h"
  13. event_label_t safe_or_conn_tcp_connecting_ev = EVENT_LABEL_UNSET;
  14. event_label_t safe_or_conn_tls_handshaking_ev = EVENT_LABEL_UNSET;
  15. event_label_t safe_or_conn_link_handshaking_ev = EVENT_LABEL_UNSET;
  16. event_label_t safe_or_conn_open_ev = EVENT_LABEL_UNSET;
  17. event_label_t safe_or_conn_closed_ev = EVENT_LABEL_UNSET;
  18. event_label_t safe_or_conn_fixed_cell_ev = EVENT_LABEL_UNSET;
  19. event_label_t safe_or_conn_var_cell_ev = EVENT_LABEL_UNSET;
  20. static void
  21. safe_connection_refresh_events(safe_connection_t *safe_conn);
  22. static void
  23. safe_or_connection_refresh_bucket_rw_states(safe_or_connection_t *safe_or_conn);
  24. static void
  25. safe_or_conn_link_protocol_version_cb(event_label_t label, event_data_t data,
  26. void *context);
  27. static void
  28. safe_or_conn_open_cb(event_label_t label, event_data_t data, void *context);
  29. static tor_error_t
  30. safe_or_connection_update_state(safe_or_connection_t *safe_or_conn,
  31. or_conn_state_t new_state);
  32. static bool
  33. safe_or_connection_is_read_wanted(safe_connection_t *safe_conn);
  34. static bool
  35. safe_or_connection_is_write_wanted(safe_connection_t *safe_conn);
  36. static void
  37. safe_or_connection_read_cb(safe_connection_t *safe_conn);
  38. static void
  39. safe_or_connection_write_cb(safe_connection_t *safe_conn);
  40. static void
  41. safe_or_connection_socket_added_cb(safe_connection_t *safe_conn);
  42. static void
  43. safe_or_connection_outbuf_modified_cb(safe_connection_t *safe_conn);
  44. static void
  45. safe_or_conn_outgoing_cell_cb(event_label_t label, event_data_t data,
  46. void *context);
  47. static void
  48. process_cells_from_inbuf(safe_or_connection_t *safe_or_conn);
  49. /********************************************************/
  50. safe_or_connection_t *
  51. TO_SAFE_OR_CONN(safe_connection_t *safe_conn)
  52. {
  53. tor_assert(safe_conn != NULL);
  54. tor_assert(safe_conn->magic == SAFE_OR_CONN_MAGIC);
  55. return DOWNCAST(safe_or_connection_t, safe_conn);
  56. }
  57. void
  58. safe_or_conn_register_events(event_registry_t *registry)
  59. {
  60. tor_assert(safe_or_conn_tcp_connecting_ev == EVENT_LABEL_UNSET);
  61. tor_assert(safe_or_conn_tls_handshaking_ev == EVENT_LABEL_UNSET);
  62. tor_assert(safe_or_conn_link_handshaking_ev == EVENT_LABEL_UNSET);
  63. tor_assert(safe_or_conn_open_ev == EVENT_LABEL_UNSET);
  64. tor_assert(safe_or_conn_closed_ev == EVENT_LABEL_UNSET);
  65. tor_assert(safe_or_conn_fixed_cell_ev == EVENT_LABEL_UNSET);
  66. tor_assert(safe_or_conn_var_cell_ev == EVENT_LABEL_UNSET);
  67. safe_or_conn_tcp_connecting_ev = \
  68. event_registry_register_event(registry, "OR Connection Connecting");
  69. safe_or_conn_tls_handshaking_ev = \
  70. event_registry_register_event(registry, "Starting OR TLS Handshake");
  71. safe_or_conn_link_handshaking_ev = \
  72. event_registry_register_event(registry, "Starting OR Link Handshake");
  73. safe_or_conn_open_ev = \
  74. event_registry_register_event(registry, "OR Connection Open");
  75. safe_or_conn_closed_ev = \
  76. event_registry_register_event(registry, "OR Connection Closed");
  77. safe_or_conn_fixed_cell_ev = \
  78. event_registry_register_event(registry, "OR Connection New Fixed-Size Cell");
  79. safe_or_conn_var_cell_ev = \
  80. event_registry_register_event(registry, "OR Connection New Variable-Size Cell");
  81. }
  82. /********************************************************/
  83. void
  84. link_handshaking_ev_free(void *ptr)
  85. {
  86. // we don't need to free the certs since we passed the ownership
  87. tor_free(ptr);
  88. }
  89. /********************************************************/
  90. static void
  91. socket_rw_state_init(socket_rw_state_t *rw_state,
  92. bool initial_state)
  93. {
  94. tor_assert(rw_state != NULL);
  95. rw_state->state = initial_state;
  96. }
  97. static bool
  98. socket_rw_state_get(socket_rw_state_t *rw_state)
  99. {
  100. tor_assert(rw_state != NULL);
  101. return rw_state->state;
  102. }
  103. static void
  104. socket_rw_state_set(socket_rw_state_t *rw_state,
  105. bool new_state,
  106. safe_connection_t *safe_conn)
  107. {
  108. tor_assert(rw_state != NULL);
  109. tor_assert(safe_conn != NULL);
  110. if (new_state != rw_state->state) {
  111. rw_state->state = new_state;
  112. safe_connection_refresh_events(safe_conn);
  113. }
  114. }
  115. /********************************************************/
  116. /*
  117. void
  118. safe_cell_queue_init(safe_cell_queue_t *queue)
  119. {
  120. tor_assert(queue != NULL);
  121. memset(queue, 0, sizeof(*queue));
  122. tor_mutex_init(&queue->lock);
  123. TOR_SIMPLEQ_INIT(&queue->head);
  124. }
  125. void
  126. safe_cell_queue_append(safe_cell_queue_t *queue,
  127. generic_cell_t *cell)
  128. {
  129. tor_assert(queue != NULL);
  130. tor_assert(cell != NULL);
  131. tor_mutex_acquire(&queue->lock);
  132. TOR_TAILQ_INSERT_TAIL(&queue->head, cell);
  133. tor_mutex_release(&queue->lock);
  134. }
  135. generic_cell_t *
  136. safe_cell_queue_pop(safe_cell_queue_t *queue)
  137. {
  138. }
  139. */
  140. /********************************************************/
  141. void
  142. safe_connection_init(safe_connection_t *safe_conn, uint32_t type_magic,
  143. event_source_t *conn_event_source,
  144. bool (*is_read_wanted)(safe_connection_t *),
  145. bool (*is_write_wanted)(safe_connection_t *),
  146. void (*read_cb)(safe_connection_t *),
  147. void (*write_cb)(safe_connection_t *),
  148. void (*socket_added_cb)(safe_connection_t *),
  149. void (*inbuf_modified_cb)(safe_connection_t *),
  150. void (*outbuf_modified_cb)(safe_connection_t *),
  151. bool requires_buffers, bool linked)
  152. {
  153. (void)conn_event_source;
  154. tor_assert(safe_conn != NULL);
  155. tor_assert(is_read_wanted != NULL);
  156. tor_assert(is_write_wanted != NULL);
  157. tor_assert(read_cb != NULL);
  158. tor_assert(write_cb != NULL);
  159. memset(safe_conn, 0, sizeof(*safe_conn));
  160. safe_conn->magic = type_magic;
  161. safe_conn->socket = TOR_INVALID_SOCKET;
  162. safe_conn->linked = linked;
  163. safe_conn->event_source = event_source_new();
  164. safe_conn->event_listener = event_listener_new(safe_conn);
  165. socket_rw_state_init(&safe_conn->read_allowed, true);
  166. socket_rw_state_init(&safe_conn->write_allowed, true);
  167. tor_mutex_init(&safe_conn->lock);
  168. safe_conn->is_read_wanted = is_read_wanted;
  169. safe_conn->is_write_wanted = is_write_wanted;
  170. safe_conn->read_cb = read_cb;
  171. safe_conn->write_cb = write_cb;
  172. safe_conn->socket_added_cb = socket_added_cb;
  173. safe_conn->inbuf_modified_cb = inbuf_modified_cb;
  174. safe_conn->outbuf_modified_cb = outbuf_modified_cb;
  175. if (requires_buffers) {
  176. safe_conn->inbuf = buf_new();
  177. safe_conn->outbuf = buf_new();
  178. }
  179. safe_conn->care_about_modified = true;
  180. }
  181. void
  182. safe_connection_set_socket(safe_connection_t *safe_conn, tor_socket_t socket)
  183. {
  184. tor_assert(safe_conn != NULL);
  185. tor_mutex_acquire(&safe_conn->lock);
  186. tor_assert(!safe_conn->linked);
  187. tor_assert(SOCKET_OK(socket));
  188. if (safe_conn->socket != TOR_INVALID_SOCKET) {
  189. log_warn(LD_BUG, "We're overwriting a previous socket");
  190. }
  191. safe_conn->socket = socket;
  192. if (safe_conn->socket_added_cb != NULL) {
  193. safe_conn->socket_added_cb(safe_conn);
  194. }
  195. tor_mutex_release(&safe_conn->lock);
  196. }
  197. static void
  198. safe_connection_read_cb(evutil_socket_t ev_sock, short fd, void *void_safe_conn)
  199. {
  200. (void)ev_sock;
  201. (void)fd;
  202. safe_connection_t *safe_conn = void_safe_conn;
  203. tor_assert(safe_conn != NULL);
  204. tor_mutex_acquire(&safe_conn->lock);
  205. tor_assert(safe_conn->read_cb != NULL);
  206. //tor_assert(safe_conn->read_event != NULL);
  207. // NOTE: the below requires obtaining a lock on the event base, which adds
  208. // unnecessary slowness
  209. // XX: Is the above true?
  210. //if (!event_pending(safe_conn->read_event, EV_READ, NULL)) {
  211. // // another thread may have disabled this event between when the
  212. // // callback started, and when we acquired the lock above
  213. // return;
  214. //}
  215. //if (!safe_conn->read_allowed || !safe_conn->read_wanted) {
  216. // // we shouldn't be reading
  217. // return;
  218. //}
  219. safe_conn->read_cb(safe_conn);
  220. tor_mutex_release(&safe_conn->lock);
  221. }
  222. static void
  223. safe_connection_write_cb(evutil_socket_t ev_sock, short fd, void *void_safe_conn)
  224. {
  225. (void)ev_sock;
  226. (void)fd;
  227. safe_connection_t *safe_conn = void_safe_conn;
  228. tor_assert(safe_conn != NULL);
  229. tor_mutex_acquire(&safe_conn->lock);
  230. tor_assert(safe_conn->write_cb != NULL);
  231. //tor_assert(safe_conn->write_event != NULL);
  232. // NOTE: the below requires obtaining a lock on the event base, which adds
  233. // unnecessary slowness
  234. // XX: Is the above true?
  235. //if (!event_pending(safe_conn->write_event, EV_WRITE, NULL)) {
  236. // // another thread may have disabled this event between when the
  237. // // callback started, and when we acquired the lock above
  238. // return;
  239. //}
  240. //if (!safe_conn->write_allowed || !safe_conn->write_wanted) {
  241. // // we shouldn't be writing
  242. // return;
  243. //}
  244. safe_conn->write_cb(safe_conn);
  245. tor_mutex_release(&safe_conn->lock);
  246. }
  247. void
  248. safe_connection_subscribe(safe_connection_t *safe_conn,
  249. event_listener_t *listener, event_label_t label)
  250. {
  251. tor_assert(safe_conn != NULL);
  252. tor_mutex_acquire(&safe_conn->lock);
  253. event_source_subscribe(safe_conn->event_source, listener, label);
  254. tor_mutex_release(&safe_conn->lock);
  255. }
  256. void
  257. safe_connection_unsubscribe_all(safe_connection_t *safe_conn,
  258. event_listener_t *listener)
  259. {
  260. tor_assert(safe_conn != NULL);
  261. tor_mutex_acquire(&safe_conn->lock);
  262. event_source_unsubscribe_all(safe_conn->event_source, listener);
  263. tor_mutex_release(&safe_conn->lock);
  264. }
  265. void
  266. safe_connection_unregister_events(safe_connection_t *safe_conn)
  267. {
  268. tor_assert(safe_conn != NULL);
  269. tor_mutex_acquire(&safe_conn->lock);
  270. if (safe_conn->read_event != NULL) {
  271. tor_event_free(safe_conn->read_event);
  272. }
  273. if (safe_conn->write_event != NULL) {
  274. tor_event_free(safe_conn->write_event);
  275. }
  276. event_listener_detach(safe_conn->event_listener);
  277. tor_mutex_release(&safe_conn->lock);
  278. }
  279. tor_error_t
  280. safe_connection_register_events(safe_connection_t *safe_conn,
  281. struct event_base *event_base)
  282. {
  283. tor_assert(safe_conn != NULL);
  284. tor_mutex_acquire(&safe_conn->lock);
  285. tor_assert(safe_conn->read_cb != NULL);
  286. tor_assert(safe_conn->write_cb != NULL);
  287. tor_assert(safe_conn->linked != SOCKET_OK(safe_conn->socket));
  288. // is either linked or has a socket, but not both (or neither)
  289. safe_connection_unregister_events(safe_conn);
  290. safe_conn->read_event = tor_event_new(event_base, safe_conn->socket,
  291. EV_READ|EV_PERSIST,
  292. safe_connection_read_cb, safe_conn);
  293. safe_conn->write_event = tor_event_new(event_base, safe_conn->socket,
  294. EV_WRITE|EV_PERSIST,
  295. safe_connection_write_cb, safe_conn);
  296. if (safe_conn->read_event == NULL || safe_conn->write_event == NULL) {
  297. log_warn(LD_BUG, "Could not set events for %d", (int)safe_conn->socket);
  298. safe_connection_unregister_events(safe_conn);
  299. tor_mutex_release(&safe_conn->lock);
  300. return E_ERROR;
  301. }
  302. event_listener_attach(safe_conn->event_listener, event_base);
  303. safe_connection_refresh_events(safe_conn);
  304. tor_mutex_release(&safe_conn->lock);
  305. return E_SUCCESS;
  306. }
  307. static void
  308. safe_connection_refresh_events(safe_connection_t *safe_conn)
  309. {
  310. tor_assert(safe_conn != NULL);
  311. tor_mutex_acquire(&safe_conn->lock);
  312. tor_assert(safe_conn->is_read_wanted != NULL);
  313. tor_assert(safe_conn->is_write_wanted != NULL);
  314. if (safe_conn->read_event != NULL) {
  315. if (socket_rw_state_get(&safe_conn->read_allowed) &&
  316. safe_conn->is_read_wanted(safe_conn)) {
  317. event_add(safe_conn->read_event, NULL);
  318. } else {
  319. event_del(safe_conn->read_event);
  320. }
  321. }
  322. if (safe_conn->write_event != NULL) {
  323. if (socket_rw_state_get(&safe_conn->write_allowed) &&
  324. safe_conn->is_write_wanted(safe_conn)) {
  325. event_add(safe_conn->write_event, NULL);
  326. } else {
  327. event_del(safe_conn->write_event);
  328. }
  329. }
  330. tor_mutex_release(&safe_conn->lock);
  331. }
  332. void
  333. safe_connection_set_read_permission(safe_connection_t *safe_conn,
  334. bool read_allowed)
  335. {
  336. tor_assert(safe_conn != NULL);
  337. tor_mutex_acquire(&safe_conn->lock);
  338. socket_rw_state_set(&safe_conn->read_allowed, read_allowed, safe_conn);
  339. tor_mutex_release(&safe_conn->lock);
  340. }
  341. void
  342. safe_connection_set_write_permission(safe_connection_t *safe_conn,
  343. bool write_allowed)
  344. {
  345. tor_assert(safe_conn != NULL);
  346. tor_mutex_acquire(&safe_conn->lock);
  347. socket_rw_state_set(&safe_conn->write_allowed, write_allowed, safe_conn);
  348. tor_mutex_release(&safe_conn->lock);
  349. }
  350. void
  351. safe_connection_start_caring_about_modified(safe_connection_t *safe_conn)
  352. {
  353. tor_assert(safe_conn != NULL);
  354. tor_mutex_acquire(&safe_conn->lock);
  355. safe_conn->care_about_modified = true;
  356. tor_mutex_release(&safe_conn->lock);
  357. }
  358. void
  359. safe_connection_stop_caring_about_modified(safe_connection_t *safe_conn)
  360. {
  361. tor_assert(safe_conn != NULL);
  362. tor_mutex_acquire(&safe_conn->lock);
  363. safe_conn->care_about_modified = false;
  364. tor_mutex_release(&safe_conn->lock);
  365. }
  366. void
  367. safe_connection_inbuf_modified(safe_connection_t *safe_conn)
  368. {
  369. tor_assert(safe_conn != NULL);
  370. tor_mutex_acquire(&safe_conn->lock);
  371. if (safe_conn->inbuf_modified_cb != NULL && safe_conn->care_about_modified) {
  372. safe_conn->inbuf_modified_cb(safe_conn);
  373. }
  374. tor_mutex_release(&safe_conn->lock);
  375. }
  376. void
  377. safe_connection_outbuf_modified(safe_connection_t *safe_conn)
  378. {
  379. tor_assert(safe_conn != NULL);
  380. tor_mutex_acquire(&safe_conn->lock);
  381. if (safe_conn->outbuf_modified_cb != NULL && safe_conn->care_about_modified) {
  382. safe_conn->outbuf_modified_cb(safe_conn);
  383. }
  384. tor_mutex_release(&safe_conn->lock);
  385. }
  386. //void
  387. //safe_connection_use_inbuf(safe_connection_t *safe_conn,
  388. // int (*f)(struct buf_t *, void *, void **),
  389. // void *data,
  390. // void **ret_val)
  391. //{
  392. // tor_assert(safe_conn != NULL);
  393. // tor_assert(f != NULL);
  394. // tor_mutex_acquire(&safe_conn->lock);
  395. //
  396. // int rv = f(safe_conn->inbuf, data, ret_val);
  397. //
  398. // tor_mutex_release(&safe_conn->lock);
  399. //
  400. // return rv;
  401. //}
  402. /********************************************************/
  403. safe_or_connection_t *
  404. safe_or_connection_new(bool requires_buffers, bool is_outgoing,
  405. const char *remote_address_str,
  406. event_source_t *conn_event_source)
  407. {
  408. safe_or_connection_t *safe_or_conn = \
  409. tor_malloc_zero(sizeof(safe_or_connection_t));
  410. safe_connection_init(TO_SAFE_CONN(safe_or_conn),
  411. SAFE_OR_CONN_MAGIC,
  412. conn_event_source,
  413. safe_or_connection_is_read_wanted,
  414. safe_or_connection_is_write_wanted,
  415. safe_or_connection_read_cb,
  416. safe_or_connection_write_cb,
  417. safe_or_connection_socket_added_cb,
  418. NULL,
  419. safe_or_connection_outbuf_modified_cb,
  420. requires_buffers, false);
  421. token_bucket_rw_init(&safe_or_conn->bucket, 1, 1, time(NULL));
  422. safe_or_conn->is_outgoing = is_outgoing;
  423. if (remote_address_str != NULL) {
  424. safe_or_conn->remote_address_str = \
  425. tor_strdup(escaped_safe_str(remote_address_str));
  426. // the function 'escaped_safe_str' must be run in the main thread
  427. } else {
  428. safe_or_conn->remote_address_str = NULL;
  429. log_warn(LD_OR, "No remote address string was provided");
  430. }
  431. event_listener_set_callback(TO_SAFE_CONN(safe_or_conn)->event_listener,
  432. or_conn_link_protocol_version_ev,
  433. NULL, safe_or_conn_link_protocol_version_cb);
  434. event_listener_set_callback(TO_SAFE_CONN(safe_or_conn)->event_listener,
  435. or_conn_open_ev,
  436. NULL, safe_or_conn_open_cb);
  437. event_listener_set_callback(TO_SAFE_CONN(safe_or_conn)->event_listener,
  438. or_conn_outgoing_packed_cell,
  439. NULL, safe_or_conn_outgoing_cell_cb);
  440. event_listener_set_callback(TO_SAFE_CONN(safe_or_conn)->event_listener,
  441. or_conn_outgoing_fixed_cell,
  442. NULL, safe_or_conn_outgoing_cell_cb);
  443. event_listener_set_callback(TO_SAFE_CONN(safe_or_conn)->event_listener,
  444. or_conn_outgoing_variable_cell,
  445. NULL, safe_or_conn_outgoing_cell_cb);
  446. if (conn_event_source) {
  447. event_source_subscribe(conn_event_source,
  448. TO_SAFE_CONN(safe_or_conn)->event_listener,
  449. or_conn_link_protocol_version_ev);
  450. event_source_subscribe(conn_event_source,
  451. TO_SAFE_CONN(safe_or_conn)->event_listener,
  452. or_conn_open_ev);
  453. event_source_subscribe(conn_event_source,
  454. TO_SAFE_CONN(safe_or_conn)->event_listener,
  455. or_conn_outgoing_packed_cell);
  456. event_source_subscribe(conn_event_source,
  457. TO_SAFE_CONN(safe_or_conn)->event_listener,
  458. or_conn_outgoing_fixed_cell);
  459. event_source_subscribe(conn_event_source,
  460. TO_SAFE_CONN(safe_or_conn)->event_listener,
  461. or_conn_outgoing_variable_cell);
  462. }
  463. safe_or_conn->link_protocol = 0; // unknown protocol
  464. safe_or_conn->wide_circ_ids = false;
  465. safe_or_conn->waiting_for_link_protocol = false;
  466. // these states should be set by 'safe_or_connection_update_state()'
  467. socket_rw_state_init(&safe_or_conn->tor_read_wanted, false);
  468. socket_rw_state_init(&safe_or_conn->tor_write_wanted, false);
  469. socket_rw_state_init(&safe_or_conn->tls_read_wanted, false);
  470. socket_rw_state_init(&safe_or_conn->tls_write_wanted, false);
  471. socket_rw_state_init(&safe_or_conn->bucket_read_allowed, false);
  472. socket_rw_state_init(&safe_or_conn->bucket_write_allowed, false);
  473. safe_or_connection_refresh_bucket_rw_states(safe_or_conn);
  474. tor_assert(safe_or_connection_update_state(safe_or_conn,
  475. SAFE_OR_CONN_STATE_NO_SOCKET) == E_SUCCESS);
  476. return safe_or_conn;
  477. }
  478. static void
  479. safe_or_connection_socket_added_cb(safe_connection_t *safe_conn)
  480. {
  481. tor_assert(safe_conn != NULL);
  482. tor_assert(safe_or_connection_update_state(TO_SAFE_OR_CONN(safe_conn),
  483. SAFE_OR_CONN_STATE_TCP_CONNECTING) == E_SUCCESS);
  484. // it might already be connected, but it should be fine to transition
  485. // through this state first
  486. }
  487. static void
  488. safe_or_connection_outbuf_modified_cb(safe_connection_t *safe_conn)
  489. {
  490. log_warn(LD_OR, "Nothing should write directly to an OR conn buffer");
  491. tor_assert(0);
  492. tor_assert(safe_conn != NULL);
  493. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  494. if (safe_or_conn->state == SAFE_OR_CONN_STATE_LINK_HANDSHAKING ||
  495. safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN) {
  496. if (buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf) > 0) {
  497. socket_rw_state_set(&safe_or_conn->tor_write_wanted, true,
  498. TO_SAFE_CONN(safe_or_conn));
  499. }
  500. } else {
  501. log_warn(LD_OR, "The outbuf was modified when in a state where it "
  502. "shouldn't be modified (state %d)", safe_or_conn->state);
  503. }
  504. }
  505. static void
  506. safe_or_connection_refresh_bucket_rw_states(safe_or_connection_t *safe_or_conn)
  507. {
  508. if (token_bucket_rw_get_read(&safe_or_conn->bucket) > 0) {
  509. // token bucket is not empty, so we can read now
  510. socket_rw_state_set(&safe_or_conn->bucket_read_allowed, true,
  511. TO_SAFE_CONN(safe_or_conn));
  512. log_debug(LD_OR, "Token bucket for %p read is non-empty", safe_or_conn);
  513. } else {
  514. // token bucket is empty, so can't read now
  515. socket_rw_state_set(&safe_or_conn->bucket_read_allowed, false,
  516. TO_SAFE_CONN(safe_or_conn));
  517. log_debug(LD_OR, "Token bucket for %p read is empty", safe_or_conn);
  518. }
  519. if (token_bucket_rw_get_write(&safe_or_conn->bucket) > 0) {
  520. // token bucket is not empty, so we can write now
  521. socket_rw_state_set(&safe_or_conn->bucket_write_allowed, true,
  522. TO_SAFE_CONN(safe_or_conn));
  523. log_debug(LD_OR, "Token bucket for %p write is non-empty", safe_or_conn);
  524. } else {
  525. // token bucket is empty, so can't write now
  526. socket_rw_state_set(&safe_or_conn->bucket_write_allowed, false,
  527. TO_SAFE_CONN(safe_or_conn));
  528. log_debug(LD_OR, "Token bucket for %p write is empty", safe_or_conn);
  529. }
  530. }
  531. static void
  532. safe_or_conn_link_protocol_version_cb(event_label_t label, event_data_t data,
  533. void *context)
  534. {
  535. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(context);
  536. tor_assert(label == or_conn_link_protocol_version_ev);
  537. tor_assert(safe_or_conn != NULL);
  538. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  539. tor_assert(safe_or_conn->state == SAFE_OR_CONN_STATE_LINK_HANDSHAKING);
  540. tor_assert(safe_or_conn->waiting_for_link_protocol);
  541. uint16_t link_protocol = data.u16;
  542. tor_assert(link_protocol >= 3);
  543. safe_or_conn->link_protocol = link_protocol;
  544. safe_or_conn->wide_circ_ids = (link_protocol >= 3);
  545. safe_or_conn->waiting_for_link_protocol = false;
  546. event_active(TO_SAFE_CONN(safe_or_conn)->read_event, 0, 0);
  547. // we need to process incoming cells on the buffer, even if there's
  548. // no data waiting on the incoming socket
  549. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  550. }
  551. static void
  552. safe_or_conn_open_cb(event_label_t label, event_data_t data, void *context)
  553. {
  554. (void)data;
  555. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(context);
  556. tor_assert(label == or_conn_open_ev);
  557. tor_assert(safe_or_conn != NULL);
  558. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  559. tor_assert(safe_or_conn->state == SAFE_OR_CONN_STATE_LINK_HANDSHAKING ||
  560. safe_or_conn->state == SAFE_OR_CONN_STATE_CLOSED);
  561. if (safe_or_conn->state != SAFE_OR_CONN_STATE_CLOSED) {
  562. // if we're already closed, then just ignore it
  563. safe_or_connection_update_state(safe_or_conn, SAFE_OR_CONN_STATE_OPEN);
  564. }
  565. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  566. }
  567. // TODO: we should get rid of this at some point
  568. void
  569. safe_or_connection_get_tls_desc(safe_or_connection_t *safe_or_conn,
  570. char *buf, size_t buf_size)
  571. {
  572. tor_assert(safe_or_conn != NULL);
  573. tor_assert(buf != NULL);
  574. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  575. if (safe_or_conn->tls != NULL) {
  576. tor_tls_get_state_description(safe_or_conn->tls, buf, buf_size);
  577. } else {
  578. tor_snprintf(buf, buf_size, "<no tls object>");
  579. }
  580. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  581. }
  582. int
  583. safe_or_connection_tls_secrets(safe_or_connection_t *safe_or_conn,
  584. uint8_t *secrets_out)
  585. {
  586. tor_assert(safe_or_conn != NULL);
  587. tor_assert(secrets_out != NULL);
  588. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  589. int rv = -1;
  590. if (safe_or_conn->tls == NULL){
  591. log_warn(LD_OR, "safe_or_conn->tls is NULL");
  592. } else {
  593. rv = tor_tls_get_tlssecrets(safe_or_conn->tls, secrets_out);
  594. }
  595. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  596. return rv;
  597. }
  598. int
  599. safe_or_connection_key_material(safe_or_connection_t *safe_or_conn,
  600. uint8_t *secrets_out,
  601. const uint8_t *context,
  602. size_t context_len, const char *label)
  603. {
  604. tor_assert(safe_or_conn != NULL);
  605. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  606. int rv = -1;
  607. if (safe_or_conn->tls == NULL){
  608. log_warn(LD_OR, "safe_or_conn->tls is NULL");
  609. } else {
  610. rv = tor_tls_export_key_material(safe_or_conn->tls, secrets_out,
  611. context, context_len, label);
  612. }
  613. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  614. return rv;
  615. }
  616. void
  617. safe_or_connection_refill_buckets(safe_or_connection_t *safe_or_conn,
  618. uint32_t now_ts)
  619. {
  620. tor_assert(safe_or_conn != NULL);
  621. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  622. tor_assert(&safe_or_conn->bucket != NULL);
  623. token_bucket_rw_refill(&safe_or_conn->bucket, now_ts);
  624. safe_or_connection_refresh_bucket_rw_states(safe_or_conn);
  625. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  626. }
  627. // TODO: this might be better implemented as a message so that we don't need
  628. // to wait for the lock (but would require us to add a listener to the
  629. // safe conn)
  630. void
  631. safe_or_connection_adjust_buckets(safe_or_connection_t *safe_or_conn,
  632. uint32_t rate, uint32_t burst,
  633. bool reset, uint32_t now_ts)
  634. {
  635. tor_assert(safe_or_conn != NULL);
  636. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  637. tor_assert(&safe_or_conn->bucket != NULL);
  638. token_bucket_rw_adjust(&safe_or_conn->bucket, rate, burst);
  639. if (reset) {
  640. token_bucket_rw_reset(&safe_or_conn->bucket, now_ts);
  641. safe_or_connection_refresh_bucket_rw_states(safe_or_conn);
  642. }
  643. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  644. }
  645. static void
  646. safe_or_connection_decrement_buckets(safe_or_connection_t *safe_or_conn,
  647. size_t num_read, size_t num_written,
  648. bool use_conn_buckets)
  649. {
  650. if (use_conn_buckets) {
  651. token_bucket_rw_dec(&safe_or_conn->bucket, num_read, num_written);
  652. }
  653. safe_or_connection_refresh_bucket_rw_states(safe_or_conn);
  654. }
  655. static size_t
  656. safe_or_connection_max_bytes_can_read(safe_or_connection_t *safe_or_conn,
  657. bool use_conn_buckets)
  658. {
  659. // this function may become more complicated if we add support for global
  660. // buckets in the future
  661. // note: that would be a bad way to do it, since instead we should borrow
  662. // some space from the global bucket, and then commit it once the read
  663. // is actually finished
  664. if (use_conn_buckets) {
  665. return token_bucket_rw_get_read(&safe_or_conn->bucket);
  666. } else {
  667. return SIZE_MAX;
  668. }
  669. }
  670. static size_t
  671. safe_or_connection_max_bytes_can_write(safe_or_connection_t *safe_or_conn,
  672. bool use_conn_buckets)
  673. {
  674. // this function may become more complicated if we add support for global
  675. // buckets in the future
  676. // note: that would be a bad way to do it, since instead we should borrow
  677. // some space from the global bucket, and then commit it once the write
  678. // is actually finished
  679. if (use_conn_buckets) {
  680. return token_bucket_rw_get_write(&safe_or_conn->bucket);
  681. } else {
  682. return SIZE_MAX;
  683. }
  684. }
  685. static bool
  686. safe_or_connection_is_read_wanted(safe_connection_t *safe_conn)
  687. {
  688. tor_assert(safe_conn != NULL);
  689. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  690. return socket_rw_state_get(&safe_or_conn->tls_read_wanted) ||
  691. (socket_rw_state_get(&safe_or_conn->tor_read_wanted) &&
  692. socket_rw_state_get(&safe_or_conn->bucket_read_allowed));
  693. }
  694. static bool
  695. safe_or_connection_is_write_wanted(safe_connection_t *safe_conn)
  696. {
  697. tor_assert(safe_conn != NULL);
  698. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  699. return socket_rw_state_get(&safe_or_conn->tls_write_wanted) ||
  700. (socket_rw_state_get(&safe_or_conn->tor_write_wanted) &&
  701. socket_rw_state_get(&safe_or_conn->bucket_write_allowed));
  702. }
  703. static tor_error_t
  704. safe_or_connection_update_state(safe_or_connection_t *safe_or_conn,
  705. or_conn_state_t new_state)
  706. {
  707. if (new_state == safe_or_conn->state) {
  708. log_warn(LD_OR, "Trying to change to the current state (or_conn_state_t) "
  709. "of %d", new_state);
  710. }
  711. event_data_t null_data = { .ptr = NULL };
  712. // this is used by several cases below
  713. switch (new_state) {
  714. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  715. tor_assert_unreached();
  716. break;
  717. case SAFE_OR_CONN_STATE_NO_SOCKET:
  718. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  719. TO_SAFE_CONN(safe_or_conn));
  720. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  721. TO_SAFE_CONN(safe_or_conn));
  722. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  723. TO_SAFE_CONN(safe_or_conn));
  724. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  725. TO_SAFE_CONN(safe_or_conn));
  726. break;
  727. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  728. // the socket was EINPROGRESS, so wait for the socket to become
  729. // writable
  730. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  731. TO_SAFE_CONN(safe_or_conn));
  732. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  733. TO_SAFE_CONN(safe_or_conn));
  734. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  735. TO_SAFE_CONN(safe_or_conn));
  736. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  737. TO_SAFE_CONN(safe_or_conn));
  738. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  739. safe_or_conn_tcp_connecting_ev,
  740. null_data, NULL);
  741. break;
  742. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  743. log_warn(LD_OR, "Relay connection proxy handshake state has not yet "
  744. "been implemented");
  745. tor_assert(0);
  746. break;
  747. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  748. {
  749. // begin the handshake when either the socket is readable or
  750. // writable
  751. if (safe_or_conn->tls != NULL) {
  752. log_warn(LD_OR, "safe_or_conn->tls should not be set");
  753. return E_ERROR;
  754. }
  755. bool is_receiving = !safe_or_conn->is_outgoing;
  756. if (TO_SAFE_CONN(safe_or_conn)->socket == TOR_INVALID_SOCKET) {
  757. log_warn(LD_OR, "No socket was set yet");
  758. return E_ERROR;
  759. }
  760. safe_or_conn->tls = tor_tls_new(TO_SAFE_CONN(safe_or_conn)->socket,
  761. is_receiving);
  762. if (safe_or_conn->tls == NULL) {
  763. log_warn(LD_OR, "Could not create a new tor TLS object");
  764. return E_ERROR;
  765. }
  766. if (safe_or_conn->remote_address_str != NULL) {
  767. tor_tls_set_logged_address(safe_or_conn->tls,
  768. safe_or_conn->remote_address_str);
  769. }
  770. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  771. TO_SAFE_CONN(safe_or_conn));
  772. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  773. TO_SAFE_CONN(safe_or_conn));
  774. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  775. TO_SAFE_CONN(safe_or_conn));
  776. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  777. TO_SAFE_CONN(safe_or_conn));
  778. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  779. safe_or_conn_tls_handshaking_ev,
  780. null_data, NULL);
  781. break;
  782. }
  783. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  784. {
  785. if (safe_or_conn->tls == NULL) {
  786. log_warn(LD_OR, "safe_or_conn->tls was not set");
  787. return E_ERROR;
  788. }
  789. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  790. TO_SAFE_CONN(safe_or_conn));
  791. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  792. TO_SAFE_CONN(safe_or_conn));
  793. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  794. TO_SAFE_CONN(safe_or_conn));
  795. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  796. TO_SAFE_CONN(safe_or_conn));
  797. link_handshaking_ev_data *handshake_data = \
  798. tor_malloc_zero(sizeof(link_handshaking_ev_data));
  799. handshake_data->tls_own_cert = tor_tls_get_own_cert(safe_or_conn->tls);
  800. handshake_data->tls_peer_cert = tor_tls_get_peer_cert(safe_or_conn->tls);
  801. event_data_t ev_data = { .ptr = handshake_data };
  802. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  803. safe_or_conn_link_handshaking_ev,
  804. ev_data, link_handshaking_ev_free);
  805. break;
  806. }
  807. case SAFE_OR_CONN_STATE_OPEN:
  808. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  809. TO_SAFE_CONN(safe_or_conn));
  810. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  811. TO_SAFE_CONN(safe_or_conn));
  812. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  813. TO_SAFE_CONN(safe_or_conn));
  814. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  815. TO_SAFE_CONN(safe_or_conn));
  816. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  817. safe_or_conn_open_ev, null_data, NULL);
  818. break;
  819. case SAFE_OR_CONN_STATE_CLOSED:
  820. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  821. TO_SAFE_CONN(safe_or_conn));
  822. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  823. TO_SAFE_CONN(safe_or_conn));
  824. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  825. TO_SAFE_CONN(safe_or_conn));
  826. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  827. TO_SAFE_CONN(safe_or_conn));
  828. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  829. safe_or_conn_closed_ev, null_data, NULL);
  830. break;
  831. default:
  832. log_warn(LD_OR, "Unexpected state");
  833. tor_assert(0);
  834. break;
  835. }
  836. log_debug(LD_OR, "Safe OR conn changed from state %d to state %d",
  837. safe_or_conn->state, new_state);
  838. safe_or_conn->state = new_state;
  839. return E_SUCCESS;
  840. }
  841. static tor_error_t
  842. safe_or_connection_check_tcp_connection(safe_or_connection_t *safe_or_conn)
  843. {
  844. tor_assert(safe_or_conn != NULL);
  845. int e;
  846. socklen_t len = (socklen_t)sizeof(e);
  847. if (getsockopt(TO_SAFE_CONN(safe_or_conn)->socket, SOL_SOCKET, SO_ERROR,
  848. (void *)&e, &len) < 0) {
  849. log_warn(LD_BUG, "getsockopt() syscall failed");
  850. return E_ERROR;
  851. }
  852. if (e != 0) {
  853. // some sort of error, but maybe just inprogress still
  854. if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
  855. log_info(LD_NET, "In-progress connect failed. Removing. (%s)",
  856. tor_socket_strerror(e));
  857. return E_ERROR;
  858. } else {
  859. // no change, see if next time is better
  860. return E_SUCCESS;
  861. }
  862. }
  863. // there was no error
  864. return safe_or_connection_update_state(safe_or_conn,
  865. SAFE_OR_CONN_STATE_TLS_HANDSHAKING);
  866. }
  867. static int
  868. safe_or_connection_read_tls(safe_or_connection_t *safe_or_conn,
  869. size_t suggested_bytes_to_read,
  870. size_t *total_bytes_read)
  871. {
  872. tor_assert(safe_or_conn != NULL);
  873. tor_assert(suggested_bytes_to_read > 0);
  874. *total_bytes_read = 0;
  875. {
  876. size_t bytes_read = 0;
  877. int tls_rv = buf_read_from_tls(TO_SAFE_CONN(safe_or_conn)->inbuf,
  878. safe_or_conn->tls,
  879. suggested_bytes_to_read,
  880. &bytes_read);
  881. *total_bytes_read += bytes_read;
  882. if (tls_rv != TOR_TLS_DONE) {
  883. return tls_rv;
  884. }
  885. }
  886. int pending_bytes_to_read = tor_tls_get_pending_bytes(safe_or_conn->tls);
  887. if (pending_bytes_to_read > 0) {
  888. size_t bytes_read = 0;
  889. int tls_rv = buf_read_from_tls(TO_SAFE_CONN(safe_or_conn)->inbuf,
  890. safe_or_conn->tls,
  891. pending_bytes_to_read,
  892. &bytes_read);
  893. if (PREDICT_LIKELY(SIZE_MAX-(*total_bytes_read) > bytes_read)) {
  894. *total_bytes_read += bytes_read;
  895. } else {
  896. *total_bytes_read = SIZE_MAX;
  897. }
  898. tor_assert(tls_rv != TOR_TLS_WANTREAD && tls_rv != TOR_TLS_WANTWRITE);
  899. // we don't expect either of these when reading pending bytes
  900. if (tls_rv != TOR_TLS_DONE) {
  901. return tls_rv;
  902. }
  903. }
  904. return TOR_TLS_DONE;
  905. }
  906. static int
  907. safe_or_connection_write_tls(safe_or_connection_t *safe_or_conn,
  908. size_t max_bytes_to_write,
  909. size_t *total_bytes_written)
  910. {
  911. tor_assert(safe_or_conn != NULL);
  912. tor_assert(max_bytes_to_write > 0);
  913. *total_bytes_written = 0;
  914. size_t bytes_written = 0;
  915. int tls_rv = buf_flush_to_tls(TO_SAFE_CONN(safe_or_conn)->outbuf,
  916. safe_or_conn->tls,
  917. max_bytes_to_write,
  918. &bytes_written);
  919. *total_bytes_written += bytes_written;
  920. return tls_rv;
  921. }
  922. // this function will be needed when proxies are supported
  923. /*
  924. static tor_error_t
  925. safe_or_connection_read_plaintext(safe_or_connection_t *safe_or_conn)
  926. {
  927. tor_assert(safe_or_conn != NULL);
  928. uint32_t coarse_time = monotime_coarse_get_stamp();
  929. safe_or_connection_refill_buckets(safe_or_conn, coarse_time);
  930. size_t bytes_to_read = safe_or_connection_max_bytes_can_read(safe_or_conn);
  931. if (bytes_to_read == 0) {
  932. log_debug(LD_NET, "Read callback running, but not supposed to read bytes.");
  933. return E_SUCCESS;
  934. }
  935. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf);
  936. size_t bytes_read = 0;
  937. int reached_eof = 0;
  938. int socket_error = 0;
  939. // STEVE: if reusing this with control connections, then need to wrap
  940. // with 'CONN_LOG_PROTECT' (see connection.c,
  941. // !connection_speaks_cells, !conn->linked_conn. )
  942. int rv = buf_read_from_socket(TO_SAFE_CONN(safe_or_conn)->inbuf,
  943. TO_SAFE_CONN(safe_or_conn)->socket,
  944. bytes_to_read, &reached_eof,
  945. &socket_error);
  946. if (rv < 0) {
  947. log_debug(LD_NET, "OR plaintext connection closed on read error.");
  948. // TODO: need to send the socket_error back to the main thread
  949. return E_ERROR;
  950. } else if(rv == 0 && reached_eof != 0) {
  951. // close the connection normally
  952. log_debug(LD_NET, "OR plaintext connection closed on read eof.");
  953. // return an error so that the calling function will close it
  954. return E_ERROR;
  955. } else {
  956. bytes_read = rv;
  957. }
  958. if (PREDICT_LIKELY(bytes_read < SIZE_MAX)) {
  959. tor_assert(bytes_read == \
  960. buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf)-buf_initial_size);
  961. } else {
  962. log_warn(LD_NET, "We read an unexpectedly large number of bytes: %zu "
  963. ">= SIZE_MAX",
  964. bytes_read);
  965. }
  966. log_debug(LD_NET, "OR plaintext read of %zu", bytes_read);
  967. safe_or_connection_decrement_buckets(safe_or_conn, bytes_read, 0);
  968. return E_SUCCESS;
  969. }
  970. */
  971. static tor_error_t
  972. safe_or_connection_read_encrypted(safe_or_connection_t *safe_or_conn,
  973. bool use_conn_buckets)
  974. {
  975. tor_assert(safe_or_conn != NULL);
  976. monotime_coarse_t now;
  977. monotime_coarse_get(&now);
  978. safe_or_connection_refill_buckets(safe_or_conn, monotime_coarse_to_stamp(&now));
  979. size_t suggested_bytes_to_read = \
  980. safe_or_connection_max_bytes_can_read(safe_or_conn, use_conn_buckets);
  981. // we may read slightly more than this due to pending TLS bytes
  982. if (suggested_bytes_to_read == 0) {
  983. log_debug(LD_NET, "Read callback running, but not supposed to read bytes.");
  984. return E_SUCCESS;
  985. }
  986. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf);
  987. size_t bytes_read = 0;
  988. int tls_rv = safe_or_connection_read_tls(safe_or_conn,
  989. suggested_bytes_to_read,
  990. &bytes_read);
  991. switch (tls_rv) {
  992. case TOR_TLS_CLOSE:
  993. case TOR_TLS_ERROR_IO:
  994. log_debug(LD_NET, "TLS connection closed %son read. Closing.",
  995. tls_rv == TOR_TLS_CLOSE ? "cleanly " : "");
  996. return E_ERROR;
  997. CASE_TOR_TLS_ERROR_ANY_NONIO:
  998. log_debug(LD_NET, "TLS error [%s]. Breaking.",
  999. tor_tls_err_to_string(tls_rv));
  1000. return E_ERROR;
  1001. case TOR_TLS_WANTWRITE:
  1002. // we need to wait for the socket to become writable
  1003. // before we can do another read
  1004. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  1005. TO_SAFE_CONN(safe_or_conn));
  1006. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  1007. TO_SAFE_CONN(safe_or_conn));
  1008. break;
  1009. case TOR_TLS_WANTREAD:
  1010. // we need to wait for the socket to become readable
  1011. // again, then do another read
  1012. break;
  1013. default:
  1014. break;
  1015. }
  1016. if (PREDICT_LIKELY(bytes_read < SIZE_MAX)) {
  1017. size_t buf_len_diff = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf)-buf_initial_size;
  1018. if (bytes_read != buf_len_diff) {
  1019. log_warn(LD_OR, "Doesn't match! bytes_read: %zu, buf_len_diff: %zu",
  1020. bytes_read, buf_len_diff);
  1021. tor_assert_nonfatal_unreached_once();
  1022. }
  1023. } else {
  1024. log_warn(LD_NET, "We read an unexpectedly large number of bytes: %zu "
  1025. ">= SIZE_MAX",
  1026. bytes_read);
  1027. }
  1028. log_recv_bytes(bytes_read, &now);
  1029. size_t tls_bytes_read = 0;
  1030. size_t tls_bytes_written = 0;
  1031. tor_tls_get_n_raw_bytes(safe_or_conn->tls, &tls_bytes_read,
  1032. &tls_bytes_written);
  1033. log_debug(LD_NET, "After TLS read of %zu: %zu read, %zu written",
  1034. bytes_read, tls_bytes_read, tls_bytes_written);
  1035. safe_or_connection_decrement_buckets(safe_or_conn, tls_bytes_read,
  1036. tls_bytes_written, use_conn_buckets);
  1037. // TODO: if get_options()->TestingEnableConnBwEvent, increase conn stats?
  1038. return E_SUCCESS;
  1039. }
  1040. static tor_error_t
  1041. safe_or_connection_write_encrypted(safe_or_connection_t *safe_or_conn,
  1042. bool use_conn_buckets)
  1043. {
  1044. tor_assert(safe_or_conn != NULL);
  1045. monotime_coarse_t now;
  1046. monotime_coarse_get(&now);
  1047. safe_or_connection_refill_buckets(safe_or_conn, monotime_coarse_to_stamp(&now));
  1048. size_t max_bytes_to_write = \
  1049. safe_or_connection_max_bytes_can_write(safe_or_conn, use_conn_buckets);
  1050. if (max_bytes_to_write == 0) {
  1051. log_debug(LD_NET, "Write callback running, but not supposed to write bytes.");
  1052. return E_SUCCESS;
  1053. }
  1054. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf);
  1055. size_t bytes_written = 0;
  1056. max_bytes_to_write = MIN(max_bytes_to_write, buf_initial_size);
  1057. int tls_rv = safe_or_connection_write_tls(safe_or_conn,
  1058. max_bytes_to_write,
  1059. &bytes_written);
  1060. switch (tls_rv) {
  1061. case TOR_TLS_CLOSE:
  1062. case TOR_TLS_ERROR_IO:
  1063. log_debug(LD_NET, "TLS connection closed %son write. Closing.",
  1064. tls_rv == TOR_TLS_CLOSE ? "cleanly " : "");
  1065. return E_ERROR;
  1066. CASE_TOR_TLS_ERROR_ANY_NONIO:
  1067. log_debug(LD_NET, "TLS error [%s]. Breaking.",
  1068. tor_tls_err_to_string(tls_rv));
  1069. return E_ERROR;
  1070. case TOR_TLS_WANTWRITE:
  1071. // we need to wait for the socket to become writable
  1072. // again, then do another write
  1073. break;
  1074. case TOR_TLS_WANTREAD:
  1075. // we need to wait for the socket to become readable
  1076. // before we can do another write
  1077. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  1078. TO_SAFE_CONN(safe_or_conn));
  1079. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  1080. TO_SAFE_CONN(safe_or_conn));
  1081. break;
  1082. default:
  1083. break;
  1084. }
  1085. if (PREDICT_LIKELY(bytes_written < SIZE_MAX)) {
  1086. size_t buf_len_diff = buf_initial_size-buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf);
  1087. if (bytes_written != buf_len_diff) {
  1088. log_warn(LD_OR, "Doesn't match! bytes_written: %zu, buf_len_diff: %zu",
  1089. bytes_written, buf_len_diff);
  1090. tor_assert_nonfatal_unreached_once();
  1091. }
  1092. } else {
  1093. log_warn(LD_NET, "We wrote an unexpectedly large number of bytes: %zu "
  1094. ">= SIZE_MAX",
  1095. bytes_written);
  1096. }
  1097. log_sent_bytes(bytes_written, &now);
  1098. // fixes a throughput problem in old versions of Windows
  1099. // TODO: we should still include this, but needs to be moved here since it's
  1100. // currently static
  1101. //update_send_buffer_size(TO_SAFE_CONN(safe_or_conn)->socket);
  1102. if (buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf) == 0) {
  1103. // we have no more data to write
  1104. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  1105. TO_SAFE_CONN(safe_or_conn));
  1106. }
  1107. size_t tls_bytes_read = 0;
  1108. size_t tls_bytes_written = 0;
  1109. tor_tls_get_n_raw_bytes(safe_or_conn->tls, &tls_bytes_read,
  1110. &tls_bytes_written);
  1111. log_debug(LD_NET, "After TLS write of %zu: %zu read, %zu written",
  1112. bytes_written, tls_bytes_read, tls_bytes_written);
  1113. safe_or_connection_decrement_buckets(safe_or_conn, tls_bytes_read,
  1114. tls_bytes_written, use_conn_buckets);
  1115. // TODO: if get_options()->TestingEnableConnBwEvent, increase conn stats?
  1116. return E_SUCCESS;
  1117. }
  1118. static tor_error_t
  1119. safe_or_connection_tls_handshake(safe_or_connection_t *safe_or_conn)
  1120. {
  1121. tor_assert(safe_or_conn != NULL);
  1122. check_no_tls_errors();
  1123. int result = tor_tls_handshake(safe_or_conn->tls);
  1124. switch (result) {
  1125. CASE_TOR_TLS_ERROR_ANY:
  1126. log_info(LD_OR, "TLS error [%s]",
  1127. tor_tls_err_to_string(result));
  1128. return E_ERROR;
  1129. case TOR_TLS_CLOSE:
  1130. log_info(LD_OR, "TLS closed");
  1131. return E_ERROR;
  1132. case TOR_TLS_WANTWRITE:
  1133. // we need to wait for the socket to become writable
  1134. // before we can continue the handshake
  1135. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  1136. TO_SAFE_CONN(safe_or_conn));
  1137. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1138. TO_SAFE_CONN(safe_or_conn));
  1139. return E_SUCCESS;
  1140. case TOR_TLS_WANTREAD:
  1141. // we need to wait for the socket to become readable
  1142. // before we can continue the handshake
  1143. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  1144. TO_SAFE_CONN(safe_or_conn));
  1145. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1146. TO_SAFE_CONN(safe_or_conn));
  1147. return E_SUCCESS;
  1148. case TOR_TLS_DONE:
  1149. // the TLS handshake has finished, but not the entire link handshake
  1150. if (tor_tls_is_server(safe_or_conn->tls)) {
  1151. // we didn't start the handshake, so prepare for a v3 handshake
  1152. log_debug(LD_OR, "Done with initial SSL handshake (receiver-side)");
  1153. } else {
  1154. // we need to start the v3 handshake
  1155. log_debug(LD_OR, "Done with initial SSL handshake (initiator-side)");
  1156. //if (connection_or_launch_v3_or_handshake(conn) < 0) {
  1157. // return E_ERROR;
  1158. //}
  1159. }
  1160. return safe_or_connection_update_state(safe_or_conn,
  1161. SAFE_OR_CONN_STATE_LINK_HANDSHAKING);
  1162. default:
  1163. log_warn(LD_OR, "Unexpected return value from handshake");
  1164. return E_ERROR;
  1165. }
  1166. }
  1167. /*
  1168. static int
  1169. safe_or_connection_tls_finish_v1_handshake(safe_or_connection_t *safe_or_conn)
  1170. {
  1171. tor_assert(safe_or_conn != NULL);
  1172. tor_assert(tor_tls_used_v1_handshake(safe_or_conn->tls));
  1173. tor_assert(tor_tls_is_server(safe_or_conn->tls));
  1174. tor_assert(!safe_or_conn->is_outgoing);
  1175. // we should not be making v1 handshakes, but we may receive v1 handshakes
  1176. log_debug(LD_HANDSHAKE, "%s tls v1 handshake on %p with %s done, using "
  1177. "ciphersuite %s. verifying.",
  1178. safe_or_conn->is_outgoing?"Outgoing":"Incoming",
  1179. safe_or_conn,
  1180. safe_or_conn->remote_address_str,
  1181. tor_tls_get_ciphersuite_name(safe_or_conn->tls));
  1182. //tor_tls_block_renegotiation(safe_or_conn->tls);
  1183. char digest_rcvd[DIGEST_LEN] = {0};
  1184. // TODO fix below
  1185. if (connection_or_check_valid_tls_handshake(conn, started_here,
  1186. digest_rcvd) < 0) {
  1187. return -1;
  1188. }
  1189. // TODO in main thread
  1190. //circuit_build_times_network_is_live(get_circuit_build_times_mutable());
  1191. //conn->link_proto = 1;
  1192. //connection_or_init_conn_from_address(conn, &conn->base_.addr,
  1193. // conn->base_.port, digest_rcvd,
  1194. // NULL, 0);
  1195. //rep_hist_note_negotiated_link_proto(1, started_here);
  1196. //return connection_or_set_state_open(conn);
  1197. return 0;
  1198. }
  1199. */
  1200. static void
  1201. safe_or_connection_read_cb(safe_connection_t *safe_conn)
  1202. {
  1203. tor_assert(safe_conn != NULL);
  1204. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  1205. log_debug(LD_OR, "OR connection read cb (state=%d, obj=%p, %s)",
  1206. safe_or_conn->state, safe_or_conn,
  1207. safe_or_conn->is_outgoing?"outgoing":"incoming");
  1208. //if (safe_or_conn->tls_write_waiting_on_socket_readable) {
  1209. // // since the socket is now readable, we can re-enable TLS write again
  1210. // safe_or_conn->tls_write_waiting_on_socket_readable = false;
  1211. // safe_connection_set_write_state(TO_SAFE_CONN(safe_or_conn), true);
  1212. //}
  1213. switch (safe_or_conn->state) {
  1214. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  1215. tor_assert_unreached();
  1216. break;
  1217. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  1218. // we shouldn't get here, so make sure we're not wanting to read
  1219. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1220. TO_SAFE_CONN(safe_or_conn));
  1221. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  1222. TO_SAFE_CONN(safe_or_conn));
  1223. log_warn(LD_OR, "Connecting OR conection wants to read");
  1224. break;
  1225. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  1226. log_warn(LD_OR, "Relay connection proxy handshaking state has not yet "
  1227. "been implemented");
  1228. tor_assert(0);
  1229. // we are performing the proxy handshake
  1230. //tor_error_t rv = safe_or_connection_plaintext(safe_or_conn);
  1231. //if (rv != E_SUCCESS) {
  1232. // tor_assert(safe_or_connection_update_state(safe_or_conn,
  1233. // SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1234. //}
  1235. break;
  1236. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  1237. {
  1238. // we are performing the initial TLS handshake
  1239. tor_error_t rv = safe_or_connection_tls_handshake(safe_or_conn);
  1240. if (rv != E_SUCCESS) {
  1241. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1242. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1243. }
  1244. break;
  1245. }
  1246. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  1247. case SAFE_OR_CONN_STATE_OPEN:
  1248. {
  1249. // performing the link handshake, or the handshake has already
  1250. // completed and we're sending/receiving cells
  1251. if (socket_rw_state_get(&safe_or_conn->tls_read_wanted)) {
  1252. // since the socket is now readable, we can re-enable writing again
  1253. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1254. TO_SAFE_CONN(safe_or_conn));
  1255. socket_rw_state_set(&safe_or_conn->tor_write_wanted, true,
  1256. TO_SAFE_CONN(safe_or_conn));
  1257. }
  1258. // TODO: we may not actually want to read here now that the states are
  1259. // updated, should we re-check?
  1260. //bool use_conn_buckets = (safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN);
  1261. bool use_conn_buckets = false;
  1262. // TODO: still need to implement a timer event to refresh the token buckets
  1263. tor_error_t rv = safe_or_connection_read_encrypted(safe_or_conn,
  1264. use_conn_buckets);
  1265. if (rv != E_SUCCESS) {
  1266. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1267. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1268. }
  1269. if (!safe_or_conn->waiting_for_link_protocol) {
  1270. process_cells_from_inbuf(safe_or_conn);
  1271. }
  1272. break;
  1273. }
  1274. case SAFE_OR_CONN_STATE_CLOSED:
  1275. case SAFE_OR_CONN_STATE_NO_SOCKET:
  1276. // we shouldn't get here, so make sure we're not wanting to read
  1277. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1278. TO_SAFE_CONN(safe_or_conn));
  1279. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  1280. TO_SAFE_CONN(safe_or_conn));
  1281. log_warn(LD_OR, "Closed OR conection wants to read");
  1282. break;
  1283. default:
  1284. log_warn(LD_OR, "Unexpected safe OR connection state");
  1285. tor_assert(0);
  1286. break;
  1287. }
  1288. }
  1289. static void
  1290. safe_or_connection_write_cb(safe_connection_t *safe_conn)
  1291. {
  1292. tor_assert(safe_conn != NULL);
  1293. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  1294. log_debug(LD_OR, "OR connection write cb (state=%d, obj=%p, %s)",
  1295. safe_or_conn->state, safe_or_conn,
  1296. safe_or_conn->is_outgoing?"outgoing":"incoming");
  1297. switch (safe_or_conn->state) {
  1298. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  1299. tor_assert_unreached();
  1300. break;
  1301. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  1302. {
  1303. // the socket was connecting and is now ready to write, so we
  1304. // should check for errors before using the socket
  1305. tor_error_t rv = safe_or_connection_check_tcp_connection(safe_or_conn);
  1306. if (rv != E_SUCCESS) {
  1307. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1308. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1309. }
  1310. break;
  1311. }
  1312. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  1313. log_warn(LD_OR, "Relay connection proxy handshaking state has not yet "
  1314. "been implemented");
  1315. tor_assert(0);
  1316. // we are performing the proxy handshake
  1317. break;
  1318. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  1319. {
  1320. // we are performing the initial TLS handshake
  1321. tor_error_t rv = safe_or_connection_tls_handshake(safe_or_conn);
  1322. if (rv != E_SUCCESS) {
  1323. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1324. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1325. }
  1326. break;
  1327. }
  1328. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  1329. case SAFE_OR_CONN_STATE_OPEN:
  1330. {
  1331. // performing the link handshake, or the handshake has already
  1332. // completed and we're sending/receiving cells
  1333. if (socket_rw_state_get(&safe_or_conn->tls_write_wanted)) {
  1334. // since the socket is now writable, we can re-enable reading again
  1335. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1336. TO_SAFE_CONN(safe_or_conn));
  1337. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  1338. TO_SAFE_CONN(safe_or_conn));
  1339. }
  1340. // TODO: we may not actually want to write here now that the states are
  1341. // updated, should we re-check?
  1342. bool use_conn_buckets = (safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN);
  1343. tor_error_t rv = safe_or_connection_write_encrypted(safe_or_conn,
  1344. use_conn_buckets);
  1345. if (rv != E_SUCCESS) {
  1346. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1347. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1348. }
  1349. break;
  1350. }
  1351. case SAFE_OR_CONN_STATE_CLOSED:
  1352. case SAFE_OR_CONN_STATE_NO_SOCKET:
  1353. // we shouldn't get here, so make sure we're not wanting to write
  1354. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1355. TO_SAFE_CONN(safe_or_conn));
  1356. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  1357. TO_SAFE_CONN(safe_or_conn));
  1358. log_warn(LD_OR, "Closed OR conection wants to write");
  1359. break;
  1360. default:
  1361. log_warn(LD_OR, "Unexpected safe OR connection state");
  1362. tor_assert(0);
  1363. break;
  1364. }
  1365. }
  1366. /********************************************************/
  1367. /*
  1368. static void
  1369. append_to_incoming_cell_queue(safe_or_connection_t *safe_or_conn,
  1370. generic_cell_t *cell)
  1371. {
  1372. tor_assert(safe_or_conn != NULL);
  1373. tor_mutex_acquire(&safe_or_conn->incoming_cell_queue->lock);
  1374. TOR_TAILQ_INSERT_TAIL(&safe_or_conn->incoming_cell_queue->head, cell);
  1375. tor_mutex_release(&safe_or_conn->incoming_cell_queue->lock);
  1376. }
  1377. */
  1378. static void
  1379. safe_or_conn_outgoing_cell_cb(event_label_t label, event_data_t data,
  1380. void *context)
  1381. {
  1382. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(context);
  1383. tor_assert(safe_or_conn != NULL);
  1384. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  1385. if (safe_or_conn->state == SAFE_OR_CONN_STATE_CLOSED) {
  1386. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  1387. return;
  1388. }
  1389. tor_assert(safe_or_conn->state == SAFE_OR_CONN_STATE_LINK_HANDSHAKING ||
  1390. safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN);
  1391. struct buf_t *outbuf = TO_SAFE_CONN(safe_or_conn)->outbuf;
  1392. int rv = -1;
  1393. if (label == or_conn_outgoing_packed_cell) {
  1394. packed_cell_t *packed_cell = data.ptr;
  1395. tor_assert(packed_cell != NULL);
  1396. size_t cell_network_size = \
  1397. get_cell_network_size(safe_or_conn->wide_circ_ids?1:0);
  1398. tor_assert(packed_cell_get_command(packed_cell,
  1399. safe_or_conn->wide_circ_ids?1:0) != 0);
  1400. rv = buf_add(outbuf, packed_cell->body, cell_network_size);
  1401. } else if (label == or_conn_outgoing_fixed_cell) {
  1402. cell_t *cell = data.ptr;
  1403. tor_assert(cell != NULL);
  1404. //tor_assert(cell->command != 0); // PADDING cells have command == 0
  1405. size_t cell_network_size = \
  1406. get_cell_network_size(safe_or_conn->wide_circ_ids?1:0);
  1407. packed_cell_t packed_cell;
  1408. cell_pack(&packed_cell, cell, safe_or_conn->wide_circ_ids?1:0);
  1409. rv = buf_add(outbuf, packed_cell.body, cell_network_size);
  1410. } else if (label == or_conn_outgoing_variable_cell) {
  1411. var_cell_t *var_cell = data.ptr;
  1412. tor_assert(var_cell != NULL);
  1413. tor_assert(var_cell->command != 0);
  1414. char header[VAR_CELL_MAX_HEADER_SIZE];
  1415. int header_len = var_cell_pack_header(var_cell, header,
  1416. safe_or_conn->wide_circ_ids?1:0);
  1417. rv = buf_add(outbuf, header, header_len);
  1418. if (rv >= 0) {
  1419. rv = buf_add(outbuf, (char *)var_cell->payload, var_cell->payload_len);
  1420. }
  1421. } else {
  1422. log_warn(LD_OR, "Received an unexpected event type");
  1423. tor_assert_nonfatal_unreached_once();
  1424. }
  1425. if (rv < 0) {
  1426. log_warn(LD_OR, "Safe OR connection could not write to outgoing buffer");
  1427. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1428. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1429. } else {
  1430. socket_rw_state_set(&safe_or_conn->tor_write_wanted, true,
  1431. TO_SAFE_CONN(safe_or_conn));
  1432. }
  1433. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  1434. }
  1435. static bool
  1436. fetch_cell(safe_or_connection_t *safe_or_conn, char *cell_buf)
  1437. {
  1438. safe_connection_t *safe_conn = TO_SAFE_CONN(safe_or_conn);
  1439. size_t cell_network_size = \
  1440. get_cell_network_size(safe_or_conn->wide_circ_ids?1:0);
  1441. if (buf_datalen(safe_conn->inbuf) < cell_network_size) {
  1442. // don't have a full cell
  1443. return false;
  1444. }
  1445. buf_get_bytes(safe_conn->inbuf, cell_buf, cell_network_size);
  1446. safe_connection_inbuf_modified(safe_conn);
  1447. return true;
  1448. }
  1449. static bool
  1450. fetch_var_cell(safe_or_connection_t *safe_or_conn, var_cell_t **var_cell_ptr)
  1451. {
  1452. safe_connection_t *safe_conn = TO_SAFE_CONN(safe_or_conn);
  1453. int link_protocol = safe_or_conn->link_protocol;
  1454. *var_cell_ptr = NULL;
  1455. int found_var_cell = fetch_var_cell_from_buf(safe_conn->inbuf, var_cell_ptr,
  1456. link_protocol);
  1457. if (*var_cell_ptr != NULL) {
  1458. // there was not a *full* cell
  1459. safe_connection_inbuf_modified(safe_conn);
  1460. }
  1461. return (found_var_cell != 0);
  1462. }
  1463. static void
  1464. void_var_cell_free(void *void_var_cell)
  1465. {
  1466. var_cell_free_((var_cell_t *)void_var_cell);
  1467. }
  1468. static void
  1469. process_cells_from_inbuf(safe_or_connection_t *safe_or_conn)
  1470. {
  1471. tor_assert(safe_or_conn != NULL);
  1472. tor_assert(safe_or_conn->waiting_for_link_protocol == false);
  1473. while (true) {
  1474. var_cell_t *var_cell = NULL;
  1475. bool found_var_cell = fetch_var_cell(safe_or_conn, &var_cell);
  1476. if (found_var_cell) {
  1477. if (var_cell == NULL) {
  1478. // the next cell is a var cell, but it is not yet complete
  1479. return;
  1480. }
  1481. uint8_t command = var_cell->command;
  1482. event_data_t event_data = { .ptr = var_cell };
  1483. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  1484. safe_or_conn_var_cell_ev, event_data,
  1485. void_var_cell_free);
  1486. // we no longer own the var cell at this point, so don't access it again
  1487. if (safe_or_conn->link_protocol == 0 && command == CELL_VERSIONS) {
  1488. // this is the first VERSIONS cell we've received;
  1489. // in order to process future cells, we need to be told our
  1490. // protocol version
  1491. safe_or_conn->waiting_for_link_protocol = true;
  1492. return;
  1493. }
  1494. } else {
  1495. char buf[CELL_MAX_NETWORK_SIZE];
  1496. bool found_cell = fetch_cell(safe_or_conn, buf);
  1497. if (found_cell) {
  1498. // retrieve cell info from buf (create the host-order struct from the
  1499. // network-order string)
  1500. cell_t *cell = tor_malloc(sizeof(cell_t));
  1501. cell_unpack(cell, buf, safe_or_conn->wide_circ_ids?1:0);
  1502. event_data_t event_data = { .ptr = cell };
  1503. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  1504. safe_or_conn_fixed_cell_ev, event_data,
  1505. tor_free_);
  1506. // we no longer own the cell at this point, so don't access it again
  1507. } else {
  1508. // there is not yet a complete cell
  1509. return;
  1510. }
  1511. }
  1512. }
  1513. }