safe_connection.c 59 KB

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