safe_connection.c 59 KB

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