safe_connection.c 60 KB

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