safe_connection.c 60 KB

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