safe_connection.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  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. size_t cell_network_size = \
  665. get_cell_network_size(safe_or_conn->wide_circ_ids?1:0);
  666. size_t bucket_max = token_bucket_rw_get_read(&safe_or_conn->bucket);
  667. size_t rv = 32*cell_network_size;
  668. // this is the limit that 'connection_bucket_get_share()' uses
  669. if (use_conn_buckets && rv > bucket_max) {
  670. rv = bucket_max;
  671. }
  672. return rv;
  673. }
  674. static size_t
  675. safe_or_connection_max_bytes_can_write(safe_or_connection_t *safe_or_conn,
  676. bool use_conn_buckets)
  677. {
  678. // this function may become more complicated if we add support for global
  679. // buckets in the future
  680. // note: that would be a bad way to do it, since instead we should borrow
  681. // some space from the global bucket, and then commit it once the write
  682. // is actually finished
  683. if (use_conn_buckets) {
  684. return token_bucket_rw_get_write(&safe_or_conn->bucket);
  685. } else {
  686. return SIZE_MAX;
  687. }
  688. }
  689. static bool
  690. safe_or_connection_is_read_wanted(safe_connection_t *safe_conn)
  691. {
  692. tor_assert(safe_conn != NULL);
  693. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  694. return socket_rw_state_get(&safe_or_conn->tls_read_wanted) ||
  695. (socket_rw_state_get(&safe_or_conn->tor_read_wanted) &&
  696. socket_rw_state_get(&safe_or_conn->bucket_read_allowed));
  697. }
  698. static bool
  699. safe_or_connection_is_write_wanted(safe_connection_t *safe_conn)
  700. {
  701. tor_assert(safe_conn != NULL);
  702. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  703. return socket_rw_state_get(&safe_or_conn->tls_write_wanted) ||
  704. (socket_rw_state_get(&safe_or_conn->tor_write_wanted) &&
  705. socket_rw_state_get(&safe_or_conn->bucket_write_allowed));
  706. }
  707. static tor_error_t
  708. safe_or_connection_update_state(safe_or_connection_t *safe_or_conn,
  709. or_conn_state_t new_state)
  710. {
  711. if (new_state == safe_or_conn->state) {
  712. log_warn(LD_OR, "Trying to change to the current state (or_conn_state_t) "
  713. "of %d", new_state);
  714. }
  715. if (safe_or_conn->state == SAFE_OR_CONN_STATE_CLOSED &&
  716. new_state != SAFE_OR_CONN_STATE_CLOSED) {
  717. log_warn(LD_OR, "Trying to change out of the CLOSED state "
  718. "(or_conn_state_t) to %d", new_state);
  719. tor_assert(0);
  720. }
  721. event_data_t null_data = { .ptr = NULL };
  722. // this is used by several cases below
  723. switch (new_state) {
  724. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  725. tor_assert_unreached();
  726. break;
  727. case SAFE_OR_CONN_STATE_NO_SOCKET:
  728. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  729. TO_SAFE_CONN(safe_or_conn));
  730. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  731. TO_SAFE_CONN(safe_or_conn));
  732. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  733. TO_SAFE_CONN(safe_or_conn));
  734. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  735. TO_SAFE_CONN(safe_or_conn));
  736. break;
  737. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  738. // the socket was EINPROGRESS, so wait for the socket to become
  739. // writable
  740. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  741. TO_SAFE_CONN(safe_or_conn));
  742. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  743. TO_SAFE_CONN(safe_or_conn));
  744. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  745. TO_SAFE_CONN(safe_or_conn));
  746. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  747. TO_SAFE_CONN(safe_or_conn));
  748. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  749. safe_or_conn_tcp_connecting_ev,
  750. null_data, NULL);
  751. break;
  752. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  753. log_warn(LD_OR, "Relay connection proxy handshake state has not yet "
  754. "been implemented");
  755. tor_assert(0);
  756. break;
  757. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  758. {
  759. // begin the handshake when either the socket is readable or
  760. // writable
  761. if (safe_or_conn->tls != NULL) {
  762. log_warn(LD_OR, "safe_or_conn->tls should not be set");
  763. return E_ERROR;
  764. }
  765. bool is_receiving = !safe_or_conn->is_outgoing;
  766. if (TO_SAFE_CONN(safe_or_conn)->socket == TOR_INVALID_SOCKET) {
  767. log_warn(LD_OR, "No socket was set yet");
  768. return E_ERROR;
  769. }
  770. safe_or_conn->tls = tor_tls_new(TO_SAFE_CONN(safe_or_conn)->socket,
  771. is_receiving);
  772. if (safe_or_conn->tls == NULL) {
  773. log_warn(LD_OR, "Could not create a new tor TLS object");
  774. return E_ERROR;
  775. }
  776. if (safe_or_conn->remote_address_str != NULL) {
  777. tor_tls_set_logged_address(safe_or_conn->tls,
  778. safe_or_conn->remote_address_str);
  779. }
  780. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  781. TO_SAFE_CONN(safe_or_conn));
  782. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  783. TO_SAFE_CONN(safe_or_conn));
  784. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  785. TO_SAFE_CONN(safe_or_conn));
  786. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  787. TO_SAFE_CONN(safe_or_conn));
  788. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  789. safe_or_conn_tls_handshaking_ev,
  790. null_data, NULL);
  791. break;
  792. }
  793. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  794. {
  795. if (safe_or_conn->tls == NULL) {
  796. log_warn(LD_OR, "safe_or_conn->tls was not set");
  797. return E_ERROR;
  798. }
  799. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  800. TO_SAFE_CONN(safe_or_conn));
  801. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  802. TO_SAFE_CONN(safe_or_conn));
  803. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  804. TO_SAFE_CONN(safe_or_conn));
  805. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  806. TO_SAFE_CONN(safe_or_conn));
  807. link_handshaking_ev_data *handshake_data = \
  808. tor_malloc_zero(sizeof(link_handshaking_ev_data));
  809. handshake_data->tls_own_cert = tor_tls_get_own_cert(safe_or_conn->tls);
  810. handshake_data->tls_peer_cert = tor_tls_get_peer_cert(safe_or_conn->tls);
  811. event_data_t ev_data = { .ptr = handshake_data };
  812. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  813. safe_or_conn_link_handshaking_ev,
  814. ev_data, link_handshaking_ev_free);
  815. break;
  816. }
  817. case SAFE_OR_CONN_STATE_OPEN:
  818. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  819. TO_SAFE_CONN(safe_or_conn));
  820. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  821. TO_SAFE_CONN(safe_or_conn));
  822. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  823. TO_SAFE_CONN(safe_or_conn));
  824. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  825. TO_SAFE_CONN(safe_or_conn));
  826. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  827. safe_or_conn_open_ev, null_data, NULL);
  828. break;
  829. case SAFE_OR_CONN_STATE_CLOSED:
  830. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  831. TO_SAFE_CONN(safe_or_conn));
  832. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  833. TO_SAFE_CONN(safe_or_conn));
  834. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  835. TO_SAFE_CONN(safe_or_conn));
  836. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  837. TO_SAFE_CONN(safe_or_conn));
  838. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  839. safe_or_conn_closed_ev, null_data, NULL);
  840. break;
  841. default:
  842. log_warn(LD_OR, "Unexpected state");
  843. tor_assert(0);
  844. break;
  845. }
  846. log_debug(LD_OR, "Safe OR conn changed from state %d to state %d",
  847. safe_or_conn->state, new_state);
  848. safe_or_conn->state = new_state;
  849. return E_SUCCESS;
  850. }
  851. static tor_error_t
  852. safe_or_connection_check_tcp_connection(safe_or_connection_t *safe_or_conn)
  853. {
  854. tor_assert(safe_or_conn != NULL);
  855. int e;
  856. socklen_t len = (socklen_t)sizeof(e);
  857. if (getsockopt(TO_SAFE_CONN(safe_or_conn)->socket, SOL_SOCKET, SO_ERROR,
  858. (void *)&e, &len) < 0) {
  859. log_warn(LD_BUG, "getsockopt() syscall failed");
  860. return E_ERROR;
  861. }
  862. if (e != 0) {
  863. // some sort of error, but maybe just inprogress still
  864. if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
  865. log_info(LD_NET, "In-progress connect failed. Removing. (%s)",
  866. tor_socket_strerror(e));
  867. return E_ERROR;
  868. } else {
  869. // no change, see if next time is better
  870. return E_SUCCESS;
  871. }
  872. }
  873. // there was no error
  874. return safe_or_connection_update_state(safe_or_conn,
  875. SAFE_OR_CONN_STATE_TLS_HANDSHAKING);
  876. }
  877. static int
  878. safe_or_connection_read_tls(safe_or_connection_t *safe_or_conn,
  879. size_t suggested_bytes_to_read,
  880. size_t *total_bytes_read)
  881. {
  882. tor_assert(safe_or_conn != NULL);
  883. tor_assert(suggested_bytes_to_read > 0);
  884. *total_bytes_read = 0;
  885. {
  886. size_t bytes_read = 0;
  887. int tls_rv = buf_read_from_tls(TO_SAFE_CONN(safe_or_conn)->inbuf,
  888. safe_or_conn->tls,
  889. suggested_bytes_to_read,
  890. &bytes_read);
  891. *total_bytes_read += bytes_read;
  892. if (tls_rv != TOR_TLS_DONE) {
  893. return tls_rv;
  894. }
  895. }
  896. int pending_bytes_to_read = tor_tls_get_pending_bytes(safe_or_conn->tls);
  897. if (pending_bytes_to_read > 0) {
  898. size_t bytes_read = 0;
  899. int tls_rv = buf_read_from_tls(TO_SAFE_CONN(safe_or_conn)->inbuf,
  900. safe_or_conn->tls,
  901. pending_bytes_to_read,
  902. &bytes_read);
  903. if (PREDICT_LIKELY(SIZE_MAX-(*total_bytes_read) > bytes_read)) {
  904. *total_bytes_read += bytes_read;
  905. } else {
  906. *total_bytes_read = SIZE_MAX;
  907. }
  908. tor_assert(tls_rv != TOR_TLS_WANTREAD && tls_rv != TOR_TLS_WANTWRITE);
  909. // we don't expect either of these when reading pending bytes
  910. if (tls_rv != TOR_TLS_DONE) {
  911. return tls_rv;
  912. }
  913. }
  914. return TOR_TLS_DONE;
  915. }
  916. static int
  917. safe_or_connection_write_tls(safe_or_connection_t *safe_or_conn,
  918. size_t max_bytes_to_write,
  919. size_t *total_bytes_written)
  920. {
  921. tor_assert(safe_or_conn != NULL);
  922. tor_assert(max_bytes_to_write > 0);
  923. *total_bytes_written = 0;
  924. size_t bytes_written = 0;
  925. int tls_rv = buf_flush_to_tls(TO_SAFE_CONN(safe_or_conn)->outbuf,
  926. safe_or_conn->tls,
  927. max_bytes_to_write,
  928. &bytes_written);
  929. *total_bytes_written += bytes_written;
  930. return tls_rv;
  931. }
  932. // this function will be needed when proxies are supported
  933. /*
  934. static tor_error_t
  935. safe_or_connection_read_plaintext(safe_or_connection_t *safe_or_conn)
  936. {
  937. tor_assert(safe_or_conn != NULL);
  938. uint32_t coarse_time = monotime_coarse_get_stamp();
  939. safe_or_connection_refill_buckets(safe_or_conn, coarse_time);
  940. size_t bytes_to_read = safe_or_connection_max_bytes_can_read(safe_or_conn);
  941. if (bytes_to_read == 0) {
  942. log_debug(LD_NET, "Read callback running, but not supposed to read bytes.");
  943. return E_SUCCESS;
  944. }
  945. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf);
  946. size_t bytes_read = 0;
  947. int reached_eof = 0;
  948. int socket_error = 0;
  949. // STEVE: if reusing this with control connections, then need to wrap
  950. // with 'CONN_LOG_PROTECT' (see connection.c,
  951. // !connection_speaks_cells, !conn->linked_conn. )
  952. int rv = buf_read_from_socket(TO_SAFE_CONN(safe_or_conn)->inbuf,
  953. TO_SAFE_CONN(safe_or_conn)->socket,
  954. bytes_to_read, &reached_eof,
  955. &socket_error);
  956. if (rv < 0) {
  957. log_debug(LD_NET, "OR plaintext connection closed on read error.");
  958. // TODO: need to send the socket_error back to the main thread
  959. return E_ERROR;
  960. } else if(rv == 0 && reached_eof != 0) {
  961. // close the connection normally
  962. log_debug(LD_NET, "OR plaintext connection closed on read eof.");
  963. // return an error so that the calling function will close it
  964. return E_ERROR;
  965. } else {
  966. bytes_read = rv;
  967. }
  968. if (PREDICT_LIKELY(bytes_read < SIZE_MAX)) {
  969. tor_assert(bytes_read == \
  970. buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf)-buf_initial_size);
  971. } else {
  972. log_warn(LD_NET, "We read an unexpectedly large number of bytes: %zu "
  973. ">= SIZE_MAX",
  974. bytes_read);
  975. }
  976. log_debug(LD_NET, "OR plaintext read of %zu", bytes_read);
  977. safe_or_connection_decrement_buckets(safe_or_conn, bytes_read, 0);
  978. return E_SUCCESS;
  979. }
  980. */
  981. static tor_error_t
  982. safe_or_connection_read_encrypted(safe_or_connection_t *safe_or_conn,
  983. bool use_conn_buckets)
  984. {
  985. tor_assert(safe_or_conn != NULL);
  986. monotime_coarse_t now;
  987. monotime_coarse_get(&now);
  988. safe_or_connection_refill_buckets(safe_or_conn, monotime_coarse_to_stamp(&now));
  989. size_t suggested_bytes_to_read = \
  990. safe_or_connection_max_bytes_can_read(safe_or_conn, use_conn_buckets);
  991. // we may read slightly more than this due to pending TLS bytes
  992. if (suggested_bytes_to_read == 0) {
  993. log_debug(LD_NET, "Read callback running, but not supposed to read bytes.");
  994. return E_SUCCESS;
  995. }
  996. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf);
  997. size_t bytes_read = 0;
  998. int tls_rv = safe_or_connection_read_tls(safe_or_conn,
  999. suggested_bytes_to_read,
  1000. &bytes_read);
  1001. switch (tls_rv) {
  1002. case TOR_TLS_CLOSE:
  1003. case TOR_TLS_ERROR_IO:
  1004. log_debug(LD_NET, "TLS connection closed %son read. Closing.",
  1005. tls_rv == TOR_TLS_CLOSE ? "cleanly " : "");
  1006. return E_ERROR;
  1007. CASE_TOR_TLS_ERROR_ANY_NONIO:
  1008. log_debug(LD_NET, "TLS error [%s]. Breaking.",
  1009. tor_tls_err_to_string(tls_rv));
  1010. return E_ERROR;
  1011. case TOR_TLS_WANTWRITE:
  1012. // we need to wait for the socket to become writable
  1013. // before we can do another read
  1014. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  1015. TO_SAFE_CONN(safe_or_conn));
  1016. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  1017. TO_SAFE_CONN(safe_or_conn));
  1018. break;
  1019. case TOR_TLS_WANTREAD:
  1020. // we need to wait for the socket to become readable
  1021. // again, then do another read
  1022. break;
  1023. default:
  1024. break;
  1025. }
  1026. if (PREDICT_LIKELY(bytes_read < SIZE_MAX)) {
  1027. size_t buf_len_diff = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf)-buf_initial_size;
  1028. if (bytes_read != buf_len_diff) {
  1029. log_warn(LD_OR, "Doesn't match! bytes_read: %zu, buf_len_diff: %zu",
  1030. bytes_read, buf_len_diff);
  1031. tor_assert_nonfatal_unreached_once();
  1032. }
  1033. } else {
  1034. log_warn(LD_NET, "We read an unexpectedly large number of bytes: %zu "
  1035. ">= SIZE_MAX",
  1036. bytes_read);
  1037. }
  1038. log_recv_bytes(bytes_read, &now);
  1039. size_t tls_bytes_read = 0;
  1040. size_t tls_bytes_written = 0;
  1041. tor_tls_get_n_raw_bytes(safe_or_conn->tls, &tls_bytes_read,
  1042. &tls_bytes_written);
  1043. log_debug(LD_NET, "After TLS read of %zu: %zu read, %zu written",
  1044. bytes_read, tls_bytes_read, tls_bytes_written);
  1045. safe_or_connection_decrement_buckets(safe_or_conn, tls_bytes_read,
  1046. tls_bytes_written, use_conn_buckets);
  1047. // TODO: if get_options()->TestingEnableConnBwEvent, increase conn stats?
  1048. return E_SUCCESS;
  1049. }
  1050. static tor_error_t
  1051. safe_or_connection_write_encrypted(safe_or_connection_t *safe_or_conn,
  1052. bool use_conn_buckets)
  1053. {
  1054. tor_assert(safe_or_conn != NULL);
  1055. monotime_coarse_t now;
  1056. monotime_coarse_get(&now);
  1057. safe_or_connection_refill_buckets(safe_or_conn, monotime_coarse_to_stamp(&now));
  1058. size_t max_bytes_to_write = \
  1059. safe_or_connection_max_bytes_can_write(safe_or_conn, use_conn_buckets);
  1060. if (max_bytes_to_write == 0) {
  1061. log_debug(LD_NET, "Write callback running, but not supposed to write bytes.");
  1062. return E_SUCCESS;
  1063. }
  1064. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf);
  1065. size_t bytes_written = 0;
  1066. max_bytes_to_write = MIN(max_bytes_to_write, buf_initial_size);
  1067. int tls_rv = safe_or_connection_write_tls(safe_or_conn,
  1068. max_bytes_to_write,
  1069. &bytes_written);
  1070. switch (tls_rv) {
  1071. case TOR_TLS_CLOSE:
  1072. case TOR_TLS_ERROR_IO:
  1073. log_debug(LD_NET, "TLS connection closed %son write. Closing.",
  1074. tls_rv == TOR_TLS_CLOSE ? "cleanly " : "");
  1075. return E_ERROR;
  1076. CASE_TOR_TLS_ERROR_ANY_NONIO:
  1077. log_debug(LD_NET, "TLS error [%s]. Breaking.",
  1078. tor_tls_err_to_string(tls_rv));
  1079. return E_ERROR;
  1080. case TOR_TLS_WANTWRITE:
  1081. // we need to wait for the socket to become writable
  1082. // again, then do another write
  1083. break;
  1084. case TOR_TLS_WANTREAD:
  1085. // we need to wait for the socket to become readable
  1086. // before we can do another write
  1087. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  1088. TO_SAFE_CONN(safe_or_conn));
  1089. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  1090. TO_SAFE_CONN(safe_or_conn));
  1091. break;
  1092. default:
  1093. break;
  1094. }
  1095. if (PREDICT_LIKELY(bytes_written < SIZE_MAX)) {
  1096. size_t buf_len_diff = buf_initial_size-buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf);
  1097. if (bytes_written != buf_len_diff) {
  1098. log_warn(LD_OR, "Doesn't match! bytes_written: %zu, buf_len_diff: %zu",
  1099. bytes_written, buf_len_diff);
  1100. tor_assert_nonfatal_unreached_once();
  1101. }
  1102. } else {
  1103. log_warn(LD_NET, "We wrote an unexpectedly large number of bytes: %zu "
  1104. ">= SIZE_MAX",
  1105. bytes_written);
  1106. }
  1107. log_sent_bytes(bytes_written, &now);
  1108. // fixes a throughput problem in old versions of Windows
  1109. // TODO: we should still include this, but needs to be moved here since it's
  1110. // currently static
  1111. //update_send_buffer_size(TO_SAFE_CONN(safe_or_conn)->socket);
  1112. if (buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf) == 0) {
  1113. // we have no more data to write
  1114. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  1115. TO_SAFE_CONN(safe_or_conn));
  1116. }
  1117. size_t tls_bytes_read = 0;
  1118. size_t tls_bytes_written = 0;
  1119. tor_tls_get_n_raw_bytes(safe_or_conn->tls, &tls_bytes_read,
  1120. &tls_bytes_written);
  1121. log_debug(LD_NET, "After TLS write of %zu: %zu read, %zu written",
  1122. bytes_written, tls_bytes_read, tls_bytes_written);
  1123. safe_or_connection_decrement_buckets(safe_or_conn, tls_bytes_read,
  1124. tls_bytes_written, use_conn_buckets);
  1125. // TODO: if get_options()->TestingEnableConnBwEvent, increase conn stats?
  1126. return E_SUCCESS;
  1127. }
  1128. static tor_error_t
  1129. safe_or_connection_tls_handshake(safe_or_connection_t *safe_or_conn)
  1130. {
  1131. tor_assert(safe_or_conn != NULL);
  1132. check_no_tls_errors();
  1133. int result = tor_tls_handshake(safe_or_conn->tls);
  1134. switch (result) {
  1135. CASE_TOR_TLS_ERROR_ANY:
  1136. log_info(LD_OR, "TLS error [%s]",
  1137. tor_tls_err_to_string(result));
  1138. return E_ERROR;
  1139. case TOR_TLS_CLOSE:
  1140. log_info(LD_OR, "TLS closed");
  1141. return E_ERROR;
  1142. case TOR_TLS_WANTWRITE:
  1143. // we need to wait for the socket to become writable
  1144. // before we can continue the handshake
  1145. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  1146. TO_SAFE_CONN(safe_or_conn));
  1147. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1148. TO_SAFE_CONN(safe_or_conn));
  1149. return E_SUCCESS;
  1150. case TOR_TLS_WANTREAD:
  1151. // we need to wait for the socket to become readable
  1152. // before we can continue the handshake
  1153. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  1154. TO_SAFE_CONN(safe_or_conn));
  1155. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1156. TO_SAFE_CONN(safe_or_conn));
  1157. return E_SUCCESS;
  1158. case TOR_TLS_DONE:
  1159. // the TLS handshake has finished, but not the entire link handshake
  1160. if (tor_tls_is_server(safe_or_conn->tls)) {
  1161. // we didn't start the handshake, so prepare for a v3 handshake
  1162. log_debug(LD_OR, "Done with initial SSL handshake (receiver-side)");
  1163. } else {
  1164. // we need to start the v3 handshake
  1165. log_debug(LD_OR, "Done with initial SSL handshake (initiator-side)");
  1166. //if (connection_or_launch_v3_or_handshake(conn) < 0) {
  1167. // return E_ERROR;
  1168. //}
  1169. }
  1170. return safe_or_connection_update_state(safe_or_conn,
  1171. SAFE_OR_CONN_STATE_LINK_HANDSHAKING);
  1172. default:
  1173. log_warn(LD_OR, "Unexpected return value from handshake");
  1174. return E_ERROR;
  1175. }
  1176. }
  1177. /*
  1178. static int
  1179. safe_or_connection_tls_finish_v1_handshake(safe_or_connection_t *safe_or_conn)
  1180. {
  1181. tor_assert(safe_or_conn != NULL);
  1182. tor_assert(tor_tls_used_v1_handshake(safe_or_conn->tls));
  1183. tor_assert(tor_tls_is_server(safe_or_conn->tls));
  1184. tor_assert(!safe_or_conn->is_outgoing);
  1185. // we should not be making v1 handshakes, but we may receive v1 handshakes
  1186. log_debug(LD_HANDSHAKE, "%s tls v1 handshake on %p with %s done, using "
  1187. "ciphersuite %s. verifying.",
  1188. safe_or_conn->is_outgoing?"Outgoing":"Incoming",
  1189. safe_or_conn,
  1190. safe_or_conn->remote_address_str,
  1191. tor_tls_get_ciphersuite_name(safe_or_conn->tls));
  1192. //tor_tls_block_renegotiation(safe_or_conn->tls);
  1193. char digest_rcvd[DIGEST_LEN] = {0};
  1194. // TODO fix below
  1195. if (connection_or_check_valid_tls_handshake(conn, started_here,
  1196. digest_rcvd) < 0) {
  1197. return -1;
  1198. }
  1199. // TODO in main thread
  1200. //circuit_build_times_network_is_live(get_circuit_build_times_mutable());
  1201. //conn->link_proto = 1;
  1202. //connection_or_init_conn_from_address(conn, &conn->base_.addr,
  1203. // conn->base_.port, digest_rcvd,
  1204. // NULL, 0);
  1205. //rep_hist_note_negotiated_link_proto(1, started_here);
  1206. //return connection_or_set_state_open(conn);
  1207. return 0;
  1208. }
  1209. */
  1210. static void
  1211. safe_or_connection_read_cb(safe_connection_t *safe_conn)
  1212. {
  1213. tor_assert(safe_conn != NULL);
  1214. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  1215. log_debug(LD_OR, "OR connection read cb (state=%d, obj=%p, %s)",
  1216. safe_or_conn->state, safe_or_conn,
  1217. safe_or_conn->is_outgoing?"outgoing":"incoming");
  1218. //if (safe_or_conn->tls_write_waiting_on_socket_readable) {
  1219. // // since the socket is now readable, we can re-enable TLS write again
  1220. // safe_or_conn->tls_write_waiting_on_socket_readable = false;
  1221. // safe_connection_set_write_state(TO_SAFE_CONN(safe_or_conn), true);
  1222. //}
  1223. switch (safe_or_conn->state) {
  1224. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  1225. tor_assert_unreached();
  1226. break;
  1227. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  1228. // we shouldn't get here, so make sure we're not wanting to read
  1229. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1230. TO_SAFE_CONN(safe_or_conn));
  1231. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  1232. TO_SAFE_CONN(safe_or_conn));
  1233. log_warn(LD_OR, "Connecting OR conection wants to read");
  1234. break;
  1235. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  1236. log_warn(LD_OR, "Relay connection proxy handshaking state has not yet "
  1237. "been implemented");
  1238. tor_assert(0);
  1239. // we are performing the proxy handshake
  1240. //tor_error_t rv = safe_or_connection_plaintext(safe_or_conn);
  1241. //if (rv != E_SUCCESS) {
  1242. // tor_assert(safe_or_connection_update_state(safe_or_conn,
  1243. // SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1244. //}
  1245. break;
  1246. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  1247. {
  1248. // we are performing the initial TLS handshake
  1249. tor_error_t rv = safe_or_connection_tls_handshake(safe_or_conn);
  1250. if (rv != E_SUCCESS) {
  1251. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1252. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1253. }
  1254. break;
  1255. }
  1256. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  1257. case SAFE_OR_CONN_STATE_OPEN:
  1258. {
  1259. // performing the link handshake, or the handshake has already
  1260. // completed and we're sending/receiving cells
  1261. if (socket_rw_state_get(&safe_or_conn->tls_read_wanted)) {
  1262. // since the socket is now readable, we can re-enable writing again
  1263. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1264. TO_SAFE_CONN(safe_or_conn));
  1265. socket_rw_state_set(&safe_or_conn->tor_write_wanted, true,
  1266. TO_SAFE_CONN(safe_or_conn));
  1267. }
  1268. // TODO: we may not actually want to read here now that the states are
  1269. // updated, should we re-check?
  1270. //bool use_conn_buckets = (safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN);
  1271. bool use_conn_buckets = false;
  1272. // TODO: still need to implement a timer event to refresh the token buckets
  1273. tor_error_t rv = safe_or_connection_read_encrypted(safe_or_conn,
  1274. use_conn_buckets);
  1275. if (rv != E_SUCCESS) {
  1276. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1277. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1278. }
  1279. if (!safe_or_conn->waiting_for_link_protocol) {
  1280. process_cells_from_inbuf(safe_or_conn);
  1281. }
  1282. break;
  1283. }
  1284. case SAFE_OR_CONN_STATE_CLOSED:
  1285. case SAFE_OR_CONN_STATE_NO_SOCKET:
  1286. // we shouldn't get here, so make sure we're not wanting to read
  1287. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1288. TO_SAFE_CONN(safe_or_conn));
  1289. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  1290. TO_SAFE_CONN(safe_or_conn));
  1291. log_warn(LD_OR, "Closed OR conection wants to read");
  1292. break;
  1293. default:
  1294. log_warn(LD_OR, "Unexpected safe OR connection state");
  1295. tor_assert(0);
  1296. break;
  1297. }
  1298. }
  1299. static void
  1300. safe_or_connection_write_cb(safe_connection_t *safe_conn)
  1301. {
  1302. tor_assert(safe_conn != NULL);
  1303. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  1304. log_debug(LD_OR, "OR connection write cb (state=%d, obj=%p, %s)",
  1305. safe_or_conn->state, safe_or_conn,
  1306. safe_or_conn->is_outgoing?"outgoing":"incoming");
  1307. switch (safe_or_conn->state) {
  1308. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  1309. tor_assert_unreached();
  1310. break;
  1311. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  1312. {
  1313. // the socket was connecting and is now ready to write, so we
  1314. // should check for errors before using the socket
  1315. tor_error_t rv = safe_or_connection_check_tcp_connection(safe_or_conn);
  1316. if (rv != E_SUCCESS) {
  1317. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1318. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1319. }
  1320. break;
  1321. }
  1322. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  1323. log_warn(LD_OR, "Relay connection proxy handshaking state has not yet "
  1324. "been implemented");
  1325. tor_assert(0);
  1326. // we are performing the proxy handshake
  1327. break;
  1328. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  1329. {
  1330. // we are performing the initial TLS handshake
  1331. tor_error_t rv = safe_or_connection_tls_handshake(safe_or_conn);
  1332. if (rv != E_SUCCESS) {
  1333. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1334. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1335. }
  1336. break;
  1337. }
  1338. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  1339. case SAFE_OR_CONN_STATE_OPEN:
  1340. {
  1341. // performing the link handshake, or the handshake has already
  1342. // completed and we're sending/receiving cells
  1343. if (socket_rw_state_get(&safe_or_conn->tls_write_wanted)) {
  1344. // since the socket is now writable, we can re-enable reading again
  1345. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1346. TO_SAFE_CONN(safe_or_conn));
  1347. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  1348. TO_SAFE_CONN(safe_or_conn));
  1349. }
  1350. // TODO: we may not actually want to write here now that the states are
  1351. // updated, should we re-check?
  1352. bool use_conn_buckets = (safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN);
  1353. tor_error_t rv = safe_or_connection_write_encrypted(safe_or_conn,
  1354. use_conn_buckets);
  1355. if (rv != E_SUCCESS) {
  1356. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1357. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1358. }
  1359. break;
  1360. }
  1361. case SAFE_OR_CONN_STATE_CLOSED:
  1362. case SAFE_OR_CONN_STATE_NO_SOCKET:
  1363. // we shouldn't get here, so make sure we're not wanting to write
  1364. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1365. TO_SAFE_CONN(safe_or_conn));
  1366. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  1367. TO_SAFE_CONN(safe_or_conn));
  1368. log_warn(LD_OR, "Closed OR conection wants to write");
  1369. break;
  1370. default:
  1371. log_warn(LD_OR, "Unexpected safe OR connection state");
  1372. tor_assert(0);
  1373. break;
  1374. }
  1375. }
  1376. /********************************************************/
  1377. /*
  1378. static void
  1379. append_to_incoming_cell_queue(safe_or_connection_t *safe_or_conn,
  1380. generic_cell_t *cell)
  1381. {
  1382. tor_assert(safe_or_conn != NULL);
  1383. tor_mutex_acquire(&safe_or_conn->incoming_cell_queue->lock);
  1384. TOR_TAILQ_INSERT_TAIL(&safe_or_conn->incoming_cell_queue->head, cell);
  1385. tor_mutex_release(&safe_or_conn->incoming_cell_queue->lock);
  1386. }
  1387. */
  1388. static void
  1389. safe_or_conn_outgoing_cell_cb(event_label_t label, event_data_t data,
  1390. void *context)
  1391. {
  1392. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(context);
  1393. tor_assert(safe_or_conn != NULL);
  1394. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  1395. if (safe_or_conn->state == SAFE_OR_CONN_STATE_CLOSED) {
  1396. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  1397. return;
  1398. }
  1399. tor_assert(safe_or_conn->state == SAFE_OR_CONN_STATE_LINK_HANDSHAKING ||
  1400. safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN);
  1401. struct buf_t *outbuf = TO_SAFE_CONN(safe_or_conn)->outbuf;
  1402. int rv = -1;
  1403. if (label == or_conn_outgoing_packed_cell) {
  1404. packed_cell_t *packed_cell = data.ptr;
  1405. tor_assert(packed_cell != NULL);
  1406. size_t cell_network_size = \
  1407. get_cell_network_size(safe_or_conn->wide_circ_ids?1:0);
  1408. tor_assert(packed_cell_get_command(packed_cell,
  1409. safe_or_conn->wide_circ_ids?1:0) != 0);
  1410. rv = buf_add(outbuf, packed_cell->body, cell_network_size);
  1411. } else if (label == or_conn_outgoing_fixed_cell) {
  1412. cell_t *cell = data.ptr;
  1413. tor_assert(cell != NULL);
  1414. //tor_assert(cell->command != 0); // PADDING cells have command == 0
  1415. size_t cell_network_size = \
  1416. get_cell_network_size(safe_or_conn->wide_circ_ids?1:0);
  1417. packed_cell_t packed_cell;
  1418. cell_pack(&packed_cell, cell, safe_or_conn->wide_circ_ids?1:0);
  1419. rv = buf_add(outbuf, packed_cell.body, cell_network_size);
  1420. } else if (label == or_conn_outgoing_variable_cell) {
  1421. var_cell_t *var_cell = data.ptr;
  1422. tor_assert(var_cell != NULL);
  1423. tor_assert(var_cell->command != 0);
  1424. char header[VAR_CELL_MAX_HEADER_SIZE];
  1425. int header_len = var_cell_pack_header(var_cell, header,
  1426. safe_or_conn->wide_circ_ids?1:0);
  1427. rv = buf_add(outbuf, header, header_len);
  1428. if (rv >= 0) {
  1429. rv = buf_add(outbuf, (char *)var_cell->payload, var_cell->payload_len);
  1430. }
  1431. } else {
  1432. log_warn(LD_OR, "Received an unexpected event type");
  1433. tor_assert_nonfatal_unreached_once();
  1434. }
  1435. if (rv < 0) {
  1436. log_warn(LD_OR, "Safe OR connection could not write to outgoing buffer");
  1437. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1438. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1439. } else {
  1440. socket_rw_state_set(&safe_or_conn->tor_write_wanted, true,
  1441. TO_SAFE_CONN(safe_or_conn));
  1442. }
  1443. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  1444. }
  1445. static bool
  1446. fetch_cell(safe_or_connection_t *safe_or_conn, char *cell_buf)
  1447. {
  1448. safe_connection_t *safe_conn = TO_SAFE_CONN(safe_or_conn);
  1449. size_t cell_network_size = \
  1450. get_cell_network_size(safe_or_conn->wide_circ_ids?1:0);
  1451. if (buf_datalen(safe_conn->inbuf) < cell_network_size) {
  1452. // don't have a full cell
  1453. return false;
  1454. }
  1455. buf_get_bytes(safe_conn->inbuf, cell_buf, cell_network_size);
  1456. safe_connection_inbuf_modified(safe_conn);
  1457. return true;
  1458. }
  1459. static bool
  1460. fetch_var_cell(safe_or_connection_t *safe_or_conn, var_cell_t **var_cell_ptr)
  1461. {
  1462. safe_connection_t *safe_conn = TO_SAFE_CONN(safe_or_conn);
  1463. int link_protocol = safe_or_conn->link_protocol;
  1464. *var_cell_ptr = NULL;
  1465. int found_var_cell = fetch_var_cell_from_buf(safe_conn->inbuf, var_cell_ptr,
  1466. link_protocol);
  1467. if (*var_cell_ptr != NULL) {
  1468. // there was not a *full* cell
  1469. safe_connection_inbuf_modified(safe_conn);
  1470. }
  1471. return (found_var_cell != 0);
  1472. }
  1473. static void
  1474. void_var_cell_free(void *void_var_cell)
  1475. {
  1476. var_cell_free_((var_cell_t *)void_var_cell);
  1477. }
  1478. static void
  1479. process_cells_from_inbuf(safe_or_connection_t *safe_or_conn)
  1480. {
  1481. tor_assert(safe_or_conn != NULL);
  1482. tor_assert(safe_or_conn->waiting_for_link_protocol == false);
  1483. while (true) {
  1484. var_cell_t *var_cell = NULL;
  1485. bool found_var_cell = fetch_var_cell(safe_or_conn, &var_cell);
  1486. if (found_var_cell) {
  1487. if (var_cell == NULL) {
  1488. // the next cell is a var cell, but it is not yet complete
  1489. return;
  1490. }
  1491. uint8_t command = var_cell->command;
  1492. event_data_t event_data = { .ptr = var_cell };
  1493. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  1494. safe_or_conn_var_cell_ev, event_data,
  1495. void_var_cell_free);
  1496. // we no longer own the var cell at this point, so don't access it again
  1497. if (safe_or_conn->link_protocol == 0 && command == CELL_VERSIONS) {
  1498. // this is the first VERSIONS cell we've received;
  1499. // in order to process future cells, we need to be told our
  1500. // protocol version
  1501. safe_or_conn->waiting_for_link_protocol = true;
  1502. return;
  1503. }
  1504. } else {
  1505. char buf[CELL_MAX_NETWORK_SIZE];
  1506. bool found_cell = fetch_cell(safe_or_conn, buf);
  1507. if (found_cell) {
  1508. // retrieve cell info from buf (create the host-order struct from the
  1509. // network-order string)
  1510. cell_t *cell = tor_malloc(sizeof(cell_t));
  1511. cell_unpack(cell, buf, safe_or_conn->wide_circ_ids?1:0);
  1512. event_data_t event_data = { .ptr = cell };
  1513. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  1514. safe_or_conn_fixed_cell_ev, event_data,
  1515. tor_free_);
  1516. // we no longer own the cell at this point, so don't access it again
  1517. } else {
  1518. // there is not yet a complete cell
  1519. return;
  1520. }
  1521. }
  1522. }
  1523. }