safe_connection.c 63 KB

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