safe_connection.c 63 KB

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