connection.c 140 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2012, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file connection.c
  8. * \brief General high-level functions to handle reading and writing
  9. * on connections.
  10. **/
  11. #include "or.h"
  12. #include "buffers.h"
  13. #include "circuitbuild.h"
  14. #include "circuitlist.h"
  15. #include "circuituse.h"
  16. #include "config.h"
  17. #include "connection.h"
  18. #include "connection_edge.h"
  19. #include "connection_or.h"
  20. #include "control.h"
  21. #include "cpuworker.h"
  22. #include "directory.h"
  23. #include "dirserv.h"
  24. #include "dns.h"
  25. #include "dnsserv.h"
  26. #include "geoip.h"
  27. #include "main.h"
  28. #include "policies.h"
  29. #include "reasons.h"
  30. #include "relay.h"
  31. #include "rendclient.h"
  32. #include "rendcommon.h"
  33. #include "rephist.h"
  34. #include "router.h"
  35. #include "transports.h"
  36. #include "routerparse.h"
  37. #ifdef USE_BUFFEREVENTS
  38. #include <event2/event.h>
  39. #endif
  40. #ifdef HAVE_PWD_H
  41. #include <pwd.h>
  42. #endif
  43. static connection_t *connection_listener_new(
  44. const struct sockaddr *listensockaddr,
  45. socklen_t listensocklen, int type,
  46. const char *address,
  47. const port_cfg_t *portcfg);
  48. static void connection_init(time_t now, connection_t *conn, int type,
  49. int socket_family);
  50. static int connection_init_accepted_conn(connection_t *conn,
  51. const listener_connection_t *listener);
  52. static int connection_handle_listener_read(connection_t *conn, int new_type);
  53. #ifndef USE_BUFFEREVENTS
  54. static int connection_bucket_should_increase(int bucket,
  55. or_connection_t *conn);
  56. #endif
  57. static int connection_finished_flushing(connection_t *conn);
  58. static int connection_flushed_some(connection_t *conn);
  59. static int connection_finished_connecting(connection_t *conn);
  60. static int connection_reached_eof(connection_t *conn);
  61. static int connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
  62. int *socket_error);
  63. static int connection_process_inbuf(connection_t *conn, int package_partial);
  64. static void client_check_address_changed(tor_socket_t sock);
  65. static void set_constrained_socket_buffers(tor_socket_t sock, int size);
  66. static const char *connection_proxy_state_to_string(int state);
  67. static int connection_read_https_proxy_response(connection_t *conn);
  68. static void connection_send_socks5_connect(connection_t *conn);
  69. static const char *proxy_type_to_string(int proxy_type);
  70. static int get_proxy_type(void);
  71. /** The last addresses that our network interface seemed to have been
  72. * binding to. We use this as one way to detect when our IP changes.
  73. *
  74. * XXX024 We should really use the entire list of interfaces here.
  75. **/
  76. static tor_addr_t *last_interface_ipv4 = NULL;
  77. /* DOCDOC last_interface_ipv6 */
  78. static tor_addr_t *last_interface_ipv6 = NULL;
  79. /** A list of tor_addr_t for addresses we've used in outgoing connections.
  80. * Used to detect IP address changes. */
  81. static smartlist_t *outgoing_addrs = NULL;
  82. #define CASE_ANY_LISTENER_TYPE \
  83. case CONN_TYPE_OR_LISTENER: \
  84. case CONN_TYPE_AP_LISTENER: \
  85. case CONN_TYPE_DIR_LISTENER: \
  86. case CONN_TYPE_CONTROL_LISTENER: \
  87. case CONN_TYPE_AP_TRANS_LISTENER: \
  88. case CONN_TYPE_AP_NATD_LISTENER: \
  89. case CONN_TYPE_AP_DNS_LISTENER
  90. /**************************************************************/
  91. /**
  92. * Return the human-readable name for the connection type <b>type</b>
  93. */
  94. const char *
  95. conn_type_to_string(int type)
  96. {
  97. static char buf[64];
  98. switch (type) {
  99. case CONN_TYPE_OR_LISTENER: return "OR listener";
  100. case CONN_TYPE_OR: return "OR";
  101. case CONN_TYPE_EXIT: return "Exit";
  102. case CONN_TYPE_AP_LISTENER: return "Socks listener";
  103. case CONN_TYPE_AP_TRANS_LISTENER:
  104. return "Transparent pf/netfilter listener";
  105. case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
  106. case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
  107. case CONN_TYPE_AP: return "Socks";
  108. case CONN_TYPE_DIR_LISTENER: return "Directory listener";
  109. case CONN_TYPE_DIR: return "Directory";
  110. case CONN_TYPE_CPUWORKER: return "CPU worker";
  111. case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
  112. case CONN_TYPE_CONTROL: return "Control";
  113. default:
  114. log_warn(LD_BUG, "unknown connection type %d", type);
  115. tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
  116. return buf;
  117. }
  118. }
  119. /**
  120. * Return the human-readable name for the connection state <b>state</b>
  121. * for the connection type <b>type</b>
  122. */
  123. const char *
  124. conn_state_to_string(int type, int state)
  125. {
  126. static char buf[96];
  127. switch (type) {
  128. CASE_ANY_LISTENER_TYPE:
  129. if (state == LISTENER_STATE_READY)
  130. return "ready";
  131. break;
  132. case CONN_TYPE_OR:
  133. switch (state) {
  134. case OR_CONN_STATE_CONNECTING: return "connect()ing";
  135. case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
  136. case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
  137. case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
  138. return "renegotiating (TLS, v2 handshake)";
  139. case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
  140. return "waiting for renegotiation or V3 handshake";
  141. case OR_CONN_STATE_OR_HANDSHAKING_V2:
  142. return "handshaking (Tor, v2 handshake)";
  143. case OR_CONN_STATE_OR_HANDSHAKING_V3:
  144. return "handshaking (Tor, v3 handshake)";
  145. case OR_CONN_STATE_OPEN: return "open";
  146. }
  147. break;
  148. case CONN_TYPE_EXIT:
  149. switch (state) {
  150. case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
  151. case EXIT_CONN_STATE_CONNECTING: return "connecting";
  152. case EXIT_CONN_STATE_OPEN: return "open";
  153. case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
  154. }
  155. break;
  156. case CONN_TYPE_AP:
  157. switch (state) {
  158. case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
  159. case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
  160. case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
  161. case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
  162. case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
  163. case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
  164. case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
  165. case AP_CONN_STATE_OPEN: return "open";
  166. }
  167. break;
  168. case CONN_TYPE_DIR:
  169. switch (state) {
  170. case DIR_CONN_STATE_CONNECTING: return "connecting";
  171. case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
  172. case DIR_CONN_STATE_CLIENT_READING: return "client reading";
  173. case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
  174. case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
  175. case DIR_CONN_STATE_SERVER_WRITING: return "writing";
  176. }
  177. break;
  178. case CONN_TYPE_CPUWORKER:
  179. switch (state) {
  180. case CPUWORKER_STATE_IDLE: return "idle";
  181. case CPUWORKER_STATE_BUSY_ONION: return "busy with onion";
  182. }
  183. break;
  184. case CONN_TYPE_CONTROL:
  185. switch (state) {
  186. case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
  187. case CONTROL_CONN_STATE_NEEDAUTH:
  188. return "waiting for authentication (protocol v1)";
  189. }
  190. break;
  191. }
  192. log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
  193. tor_snprintf(buf, sizeof(buf),
  194. "unknown state [%d] on unknown [%s] connection",
  195. state, conn_type_to_string(type));
  196. return buf;
  197. }
  198. #ifdef USE_BUFFEREVENTS
  199. /** Return true iff the connection's type is one that can use a
  200. bufferevent-based implementation. */
  201. int
  202. connection_type_uses_bufferevent(connection_t *conn)
  203. {
  204. switch (conn->type) {
  205. case CONN_TYPE_AP:
  206. case CONN_TYPE_EXIT:
  207. case CONN_TYPE_DIR:
  208. case CONN_TYPE_CONTROL:
  209. case CONN_TYPE_OR:
  210. case CONN_TYPE_CPUWORKER:
  211. return 1;
  212. default:
  213. return 0;
  214. }
  215. }
  216. #endif
  217. /** Allocate and return a new dir_connection_t, initialized as by
  218. * connection_init(). */
  219. dir_connection_t *
  220. dir_connection_new(int socket_family)
  221. {
  222. dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
  223. connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
  224. return dir_conn;
  225. }
  226. /** Allocate and return a new or_connection_t, initialized as by
  227. * connection_init(). */
  228. or_connection_t *
  229. or_connection_new(int socket_family)
  230. {
  231. or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
  232. time_t now = time(NULL);
  233. connection_init(now, TO_CONN(or_conn), CONN_TYPE_OR, socket_family);
  234. or_conn->timestamp_last_added_nonpadding = time(NULL);
  235. or_conn->next_circ_id = crypto_rand_int(1<<15);
  236. or_conn->active_circuit_pqueue = smartlist_new();
  237. or_conn->active_circuit_pqueue_last_recalibrated = cell_ewma_get_tick();
  238. return or_conn;
  239. }
  240. /** Allocate and return a new entry_connection_t, initialized as by
  241. * connection_init(). */
  242. entry_connection_t *
  243. entry_connection_new(int type, int socket_family)
  244. {
  245. entry_connection_t *entry_conn = tor_malloc_zero(sizeof(entry_connection_t));
  246. tor_assert(type == CONN_TYPE_AP);
  247. connection_init(time(NULL), ENTRY_TO_CONN(entry_conn), type, socket_family);
  248. entry_conn->socks_request = socks_request_new();
  249. return entry_conn;
  250. }
  251. /** Allocate and return a new edge_connection_t, initialized as by
  252. * connection_init(). */
  253. edge_connection_t *
  254. edge_connection_new(int type, int socket_family)
  255. {
  256. edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
  257. tor_assert(type == CONN_TYPE_EXIT);
  258. connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
  259. return edge_conn;
  260. }
  261. /** Allocate and return a new control_connection_t, initialized as by
  262. * connection_init(). */
  263. control_connection_t *
  264. control_connection_new(int socket_family)
  265. {
  266. control_connection_t *control_conn =
  267. tor_malloc_zero(sizeof(control_connection_t));
  268. connection_init(time(NULL),
  269. TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
  270. log_notice(LD_CONTROL, "New control connection opened.");
  271. return control_conn;
  272. }
  273. /** Allocate and return a new listener_connection_t, initialized as by
  274. * connection_init(). */
  275. listener_connection_t *
  276. listener_connection_new(int type, int socket_family)
  277. {
  278. listener_connection_t *listener_conn =
  279. tor_malloc_zero(sizeof(listener_connection_t));
  280. connection_init(time(NULL), TO_CONN(listener_conn), type, socket_family);
  281. return listener_conn;
  282. }
  283. /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
  284. * to make or receive connections of address family <b>socket_family</b>. The
  285. * type should be one of the CONN_TYPE_* constants. */
  286. connection_t *
  287. connection_new(int type, int socket_family)
  288. {
  289. switch (type) {
  290. case CONN_TYPE_OR:
  291. return TO_CONN(or_connection_new(socket_family));
  292. case CONN_TYPE_EXIT:
  293. return TO_CONN(edge_connection_new(type, socket_family));
  294. case CONN_TYPE_AP:
  295. return ENTRY_TO_CONN(entry_connection_new(type, socket_family));
  296. case CONN_TYPE_DIR:
  297. return TO_CONN(dir_connection_new(socket_family));
  298. case CONN_TYPE_CONTROL:
  299. return TO_CONN(control_connection_new(socket_family));
  300. CASE_ANY_LISTENER_TYPE:
  301. return TO_CONN(listener_connection_new(type, socket_family));
  302. default: {
  303. connection_t *conn = tor_malloc_zero(sizeof(connection_t));
  304. connection_init(time(NULL), conn, type, socket_family);
  305. return conn;
  306. }
  307. }
  308. }
  309. /** Initializes conn. (you must call connection_add() to link it into the main
  310. * array).
  311. *
  312. * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
  313. * -1 to signify they are not yet assigned.
  314. *
  315. * If conn is not a listener type, allocate buffers for it. If it's
  316. * an AP type, allocate space to store the socks_request.
  317. *
  318. * Assign a pseudorandom next_circ_id between 0 and 2**15.
  319. *
  320. * Initialize conn's timestamps to now.
  321. */
  322. static void
  323. connection_init(time_t now, connection_t *conn, int type, int socket_family)
  324. {
  325. static uint64_t n_connections_allocated = 1;
  326. switch (type) {
  327. case CONN_TYPE_OR:
  328. conn->magic = OR_CONNECTION_MAGIC;
  329. break;
  330. case CONN_TYPE_EXIT:
  331. conn->magic = EDGE_CONNECTION_MAGIC;
  332. break;
  333. case CONN_TYPE_AP:
  334. conn->magic = ENTRY_CONNECTION_MAGIC;
  335. break;
  336. case CONN_TYPE_DIR:
  337. conn->magic = DIR_CONNECTION_MAGIC;
  338. break;
  339. case CONN_TYPE_CONTROL:
  340. conn->magic = CONTROL_CONNECTION_MAGIC;
  341. break;
  342. CASE_ANY_LISTENER_TYPE:
  343. conn->magic = LISTENER_CONNECTION_MAGIC;
  344. break;
  345. default:
  346. conn->magic = BASE_CONNECTION_MAGIC;
  347. break;
  348. }
  349. conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
  350. conn->conn_array_index = -1; /* also default to 'not used' */
  351. conn->global_identifier = n_connections_allocated++;
  352. conn->type = type;
  353. conn->socket_family = socket_family;
  354. #ifndef USE_BUFFEREVENTS
  355. if (!connection_is_listener(conn)) {
  356. /* listeners never use their buf */
  357. conn->inbuf = buf_new();
  358. conn->outbuf = buf_new();
  359. }
  360. #endif
  361. conn->timestamp_created = now;
  362. conn->timestamp_lastread = now;
  363. conn->timestamp_lastwritten = now;
  364. }
  365. /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
  366. void
  367. connection_link_connections(connection_t *conn_a, connection_t *conn_b)
  368. {
  369. tor_assert(! SOCKET_OK(conn_a->s));
  370. tor_assert(! SOCKET_OK(conn_b->s));
  371. conn_a->linked = 1;
  372. conn_b->linked = 1;
  373. conn_a->linked_conn = conn_b;
  374. conn_b->linked_conn = conn_a;
  375. }
  376. /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
  377. * necessary, close its socket if necessary, and mark the directory as dirty
  378. * if <b>conn</b> is an OR or OP connection.
  379. */
  380. static void
  381. _connection_free(connection_t *conn)
  382. {
  383. void *mem;
  384. size_t memlen;
  385. if (!conn)
  386. return;
  387. switch (conn->type) {
  388. case CONN_TYPE_OR:
  389. tor_assert(conn->magic == OR_CONNECTION_MAGIC);
  390. mem = TO_OR_CONN(conn);
  391. memlen = sizeof(or_connection_t);
  392. break;
  393. case CONN_TYPE_AP:
  394. tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
  395. mem = TO_ENTRY_CONN(conn);
  396. memlen = sizeof(entry_connection_t);
  397. break;
  398. case CONN_TYPE_EXIT:
  399. tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
  400. mem = TO_EDGE_CONN(conn);
  401. memlen = sizeof(edge_connection_t);
  402. break;
  403. case CONN_TYPE_DIR:
  404. tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
  405. mem = TO_DIR_CONN(conn);
  406. memlen = sizeof(dir_connection_t);
  407. break;
  408. case CONN_TYPE_CONTROL:
  409. tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
  410. mem = TO_CONTROL_CONN(conn);
  411. memlen = sizeof(control_connection_t);
  412. break;
  413. CASE_ANY_LISTENER_TYPE:
  414. tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
  415. mem = TO_LISTENER_CONN(conn);
  416. memlen = sizeof(listener_connection_t);
  417. break;
  418. default:
  419. tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
  420. mem = conn;
  421. memlen = sizeof(connection_t);
  422. break;
  423. }
  424. if (conn->linked) {
  425. log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
  426. "bytes on inbuf, %d on outbuf.",
  427. conn_type_to_string(conn->type),
  428. conn_state_to_string(conn->type, conn->state),
  429. (int)connection_get_inbuf_len(conn),
  430. (int)connection_get_outbuf_len(conn));
  431. }
  432. if (!connection_is_listener(conn)) {
  433. buf_free(conn->inbuf);
  434. buf_free(conn->outbuf);
  435. } else {
  436. if (conn->socket_family == AF_UNIX) {
  437. /* For now only control ports can be Unix domain sockets
  438. * and listeners at the same time */
  439. tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
  440. if (unlink(conn->address) < 0 && errno != ENOENT) {
  441. log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
  442. strerror(errno));
  443. }
  444. }
  445. }
  446. tor_free(conn->address);
  447. if (connection_speaks_cells(conn)) {
  448. or_connection_t *or_conn = TO_OR_CONN(conn);
  449. tor_tls_free(or_conn->tls);
  450. or_conn->tls = NULL;
  451. or_handshake_state_free(or_conn->handshake_state);
  452. or_conn->handshake_state = NULL;
  453. smartlist_free(or_conn->active_circuit_pqueue);
  454. tor_free(or_conn->nickname);
  455. }
  456. if (conn->type == CONN_TYPE_AP) {
  457. entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
  458. tor_free(entry_conn->chosen_exit_name);
  459. tor_free(entry_conn->original_dest_address);
  460. if (entry_conn->socks_request)
  461. socks_request_free(entry_conn->socks_request);
  462. if (entry_conn->pending_optimistic_data) {
  463. generic_buffer_free(entry_conn->pending_optimistic_data);
  464. }
  465. if (entry_conn->sending_optimistic_data) {
  466. generic_buffer_free(entry_conn->sending_optimistic_data);
  467. }
  468. }
  469. if (CONN_IS_EDGE(conn)) {
  470. rend_data_free(TO_EDGE_CONN(conn)->rend_data);
  471. }
  472. if (conn->type == CONN_TYPE_CONTROL) {
  473. control_connection_t *control_conn = TO_CONTROL_CONN(conn);
  474. tor_free(control_conn->safecookie_client_hash);
  475. tor_free(control_conn->incoming_cmd);
  476. }
  477. tor_free(conn->read_event); /* Probably already freed by connection_free. */
  478. tor_free(conn->write_event); /* Probably already freed by connection_free. */
  479. IF_HAS_BUFFEREVENT(conn, {
  480. /* This was a workaround to handle bugs in some old versions of libevent
  481. * where callbacks can occur after calling bufferevent_free(). Setting
  482. * the callbacks to NULL prevented this. It shouldn't be necessary any
  483. * more, but let's not tempt fate for now. */
  484. bufferevent_setcb(conn->bufev, NULL, NULL, NULL, NULL);
  485. bufferevent_free(conn->bufev);
  486. conn->bufev = NULL;
  487. });
  488. if (conn->type == CONN_TYPE_DIR) {
  489. dir_connection_t *dir_conn = TO_DIR_CONN(conn);
  490. tor_free(dir_conn->requested_resource);
  491. tor_zlib_free(dir_conn->zlib_state);
  492. if (dir_conn->fingerprint_stack) {
  493. SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
  494. smartlist_free(dir_conn->fingerprint_stack);
  495. }
  496. cached_dir_decref(dir_conn->cached_dir);
  497. rend_data_free(dir_conn->rend_data);
  498. }
  499. if (SOCKET_OK(conn->s)) {
  500. log_debug(LD_NET,"closing fd %d.",(int)conn->s);
  501. tor_close_socket(conn->s);
  502. conn->s = TOR_INVALID_SOCKET;
  503. }
  504. if (conn->type == CONN_TYPE_OR &&
  505. !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
  506. log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
  507. connection_or_remove_from_identity_map(TO_OR_CONN(conn));
  508. }
  509. #ifdef USE_BUFFEREVENTS
  510. if (conn->type == CONN_TYPE_OR && TO_OR_CONN(conn)->bucket_cfg) {
  511. ev_token_bucket_cfg_free(TO_OR_CONN(conn)->bucket_cfg);
  512. TO_OR_CONN(conn)->bucket_cfg = NULL;
  513. }
  514. #endif
  515. memset(mem, 0xCC, memlen); /* poison memory */
  516. tor_free(mem);
  517. }
  518. /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
  519. */
  520. void
  521. connection_free(connection_t *conn)
  522. {
  523. if (!conn)
  524. return;
  525. tor_assert(!connection_is_on_closeable_list(conn));
  526. tor_assert(!connection_in_array(conn));
  527. if (conn->linked_conn) {
  528. log_err(LD_BUG, "Called with conn->linked_conn still set.");
  529. tor_fragile_assert();
  530. conn->linked_conn->linked_conn = NULL;
  531. if (! conn->linked_conn->marked_for_close &&
  532. conn->linked_conn->reading_from_linked_conn)
  533. connection_start_reading(conn->linked_conn);
  534. conn->linked_conn = NULL;
  535. }
  536. if (connection_speaks_cells(conn)) {
  537. if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
  538. connection_or_remove_from_identity_map(TO_OR_CONN(conn));
  539. }
  540. }
  541. if (conn->type == CONN_TYPE_CONTROL) {
  542. connection_control_closed(TO_CONTROL_CONN(conn));
  543. }
  544. connection_unregister_events(conn);
  545. _connection_free(conn);
  546. }
  547. /**
  548. * Called when we're about to finally unlink and free a connection:
  549. * perform necessary accounting and cleanup
  550. * - Directory conns that failed to fetch a rendezvous descriptor
  551. * need to inform pending rendezvous streams.
  552. * - OR conns need to call rep_hist_note_*() to record status.
  553. * - AP conns need to send a socks reject if necessary.
  554. * - Exit conns need to call connection_dns_remove() if necessary.
  555. * - AP and Exit conns need to send an end cell if they can.
  556. * - DNS conns need to fail any resolves that are pending on them.
  557. * - OR and edge connections need to be unlinked from circuits.
  558. */
  559. void
  560. connection_about_to_close_connection(connection_t *conn)
  561. {
  562. tor_assert(conn->marked_for_close);
  563. switch (conn->type) {
  564. case CONN_TYPE_DIR:
  565. connection_dir_about_to_close(TO_DIR_CONN(conn));
  566. break;
  567. case CONN_TYPE_OR:
  568. connection_or_about_to_close(TO_OR_CONN(conn));
  569. break;
  570. case CONN_TYPE_AP:
  571. connection_ap_about_to_close(TO_ENTRY_CONN(conn));
  572. break;
  573. case CONN_TYPE_EXIT:
  574. connection_exit_about_to_close(TO_EDGE_CONN(conn));
  575. break;
  576. }
  577. }
  578. /** Return true iff connection_close_immediate() has been called on this
  579. * connection. */
  580. #define CONN_IS_CLOSED(c) \
  581. ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
  582. /** Close the underlying socket for <b>conn</b>, so we don't try to
  583. * flush it. Must be used in conjunction with (right before)
  584. * connection_mark_for_close().
  585. */
  586. void
  587. connection_close_immediate(connection_t *conn)
  588. {
  589. assert_connection_ok(conn,0);
  590. if (CONN_IS_CLOSED(conn)) {
  591. log_err(LD_BUG,"Attempt to close already-closed connection.");
  592. tor_fragile_assert();
  593. return;
  594. }
  595. if (conn->outbuf_flushlen) {
  596. log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
  597. (int)conn->s, conn_type_to_string(conn->type),
  598. conn_state_to_string(conn->type, conn->state),
  599. (int)conn->outbuf_flushlen);
  600. }
  601. connection_unregister_events(conn);
  602. if (SOCKET_OK(conn->s))
  603. tor_close_socket(conn->s);
  604. conn->s = TOR_INVALID_SOCKET;
  605. if (conn->linked)
  606. conn->linked_conn_is_closed = 1;
  607. if (conn->outbuf)
  608. buf_clear(conn->outbuf);
  609. conn->outbuf_flushlen = 0;
  610. }
  611. /** Mark <b>conn</b> to be closed next time we loop through
  612. * conn_close_if_marked() in main.c. */
  613. void
  614. _connection_mark_for_close(connection_t *conn, int line, const char *file)
  615. {
  616. assert_connection_ok(conn,0);
  617. tor_assert(line);
  618. tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
  619. tor_assert(file);
  620. if (conn->marked_for_close) {
  621. log(LOG_WARN,LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
  622. " (first at %s:%d)", file, line, conn->marked_for_close_file,
  623. conn->marked_for_close);
  624. tor_fragile_assert();
  625. return;
  626. }
  627. conn->marked_for_close = line;
  628. conn->marked_for_close_file = file;
  629. add_connection_to_closeable_list(conn);
  630. /* in case we're going to be held-open-til-flushed, reset
  631. * the number of seconds since last successful write, so
  632. * we get our whole 15 seconds */
  633. conn->timestamp_lastwritten = time(NULL);
  634. }
  635. /** Find each connection that has hold_open_until_flushed set to
  636. * 1 but hasn't written in the past 15 seconds, and set
  637. * hold_open_until_flushed to 0. This means it will get cleaned
  638. * up in the next loop through close_if_marked() in main.c.
  639. */
  640. void
  641. connection_expire_held_open(void)
  642. {
  643. time_t now;
  644. smartlist_t *conns = get_connection_array();
  645. now = time(NULL);
  646. SMARTLIST_FOREACH(conns, connection_t *, conn,
  647. {
  648. /* If we've been holding the connection open, but we haven't written
  649. * for 15 seconds...
  650. */
  651. if (conn->hold_open_until_flushed) {
  652. tor_assert(conn->marked_for_close);
  653. if (now - conn->timestamp_lastwritten >= 15) {
  654. int severity;
  655. if (conn->type == CONN_TYPE_EXIT ||
  656. (conn->type == CONN_TYPE_DIR &&
  657. conn->purpose == DIR_PURPOSE_SERVER))
  658. severity = LOG_INFO;
  659. else
  660. severity = LOG_NOTICE;
  661. log_fn(severity, LD_NET,
  662. "Giving up on marked_for_close conn that's been flushing "
  663. "for 15s (fd %d, type %s, state %s).",
  664. (int)conn->s, conn_type_to_string(conn->type),
  665. conn_state_to_string(conn->type, conn->state));
  666. conn->hold_open_until_flushed = 0;
  667. }
  668. }
  669. });
  670. }
  671. #if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
  672. /** Create an AF_UNIX listenaddr struct.
  673. * <b>listenaddress</b> provides the path to the Unix socket.
  674. *
  675. * Eventually <b>listenaddress</b> will also optionally contain user, group,
  676. * and file permissions for the new socket. But not yet. XXX
  677. * Also, since we do not create the socket here the information doesn't help
  678. * here.
  679. *
  680. * If not NULL <b>readable_address</b> will contain a copy of the path part of
  681. * <b>listenaddress</b>.
  682. *
  683. * The listenaddr struct has to be freed by the caller.
  684. */
  685. static struct sockaddr_un *
  686. create_unix_sockaddr(const char *listenaddress, char **readable_address,
  687. socklen_t *len_out)
  688. {
  689. struct sockaddr_un *sockaddr = NULL;
  690. sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
  691. sockaddr->sun_family = AF_UNIX;
  692. if (strlcpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path))
  693. >= sizeof(sockaddr->sun_path)) {
  694. log_warn(LD_CONFIG, "Unix socket path '%s' is too long to fit.",
  695. escaped(listenaddress));
  696. tor_free(sockaddr);
  697. return NULL;
  698. }
  699. if (readable_address)
  700. *readable_address = tor_strdup(listenaddress);
  701. *len_out = sizeof(struct sockaddr_un);
  702. return sockaddr;
  703. }
  704. #else
  705. static struct sockaddr *
  706. create_unix_sockaddr(const char *listenaddress, char **readable_address,
  707. socklen_t *len_out)
  708. {
  709. (void)listenaddress;
  710. (void)readable_address;
  711. log_fn(LOG_ERR, LD_BUG,
  712. "Unix domain sockets not supported, yet we tried to create one.");
  713. *len_out = 0;
  714. tor_fragile_assert();
  715. return NULL;
  716. }
  717. #endif /* HAVE_SYS_UN_H */
  718. /** Warn that an accept or a connect has failed because we're running up
  719. * against our ulimit. Rate-limit these warnings so that we don't spam
  720. * the log. */
  721. static void
  722. warn_too_many_conns(void)
  723. {
  724. #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
  725. static ratelim_t last_warned = RATELIM_INIT(WARN_TOO_MANY_CONNS_INTERVAL);
  726. char *m;
  727. if ((m = rate_limit_log(&last_warned, approx_time()))) {
  728. int n_conns = get_n_open_sockets();
  729. log_warn(LD_NET,"Failing because we have %d connections already. Please "
  730. "raise your ulimit -n.%s", n_conns, m);
  731. tor_free(m);
  732. control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
  733. n_conns);
  734. }
  735. }
  736. #ifdef HAVE_SYS_UN_H
  737. /** Check whether we should be willing to open an AF_UNIX socket in
  738. * <b>path</b>. Return 0 if we should go ahead and -1 if we shouldn't. */
  739. static int
  740. check_location_for_unix_socket(const or_options_t *options, const char *path)
  741. {
  742. int r = -1;
  743. char *p = tor_strdup(path);
  744. cpd_check_t flags = CPD_CHECK_MODE_ONLY;
  745. if (get_parent_directory(p)<0)
  746. goto done;
  747. if (options->ControlSocketsGroupWritable)
  748. flags |= CPD_GROUP_OK;
  749. if (check_private_dir(p, flags, options->User) < 0) {
  750. char *escpath, *escdir;
  751. escpath = esc_for_log(path);
  752. escdir = esc_for_log(p);
  753. log_warn(LD_GENERAL, "Before Tor can create a control socket in %s, the "
  754. "directory %s needs to exist, and to be accessible only by the "
  755. "user%s account that is running Tor. (On some Unix systems, "
  756. "anybody who can list a socket can connect to it, so Tor is "
  757. "being careful.)", escpath, escdir,
  758. options->ControlSocketsGroupWritable ? " and group" : "");
  759. tor_free(escpath);
  760. tor_free(escdir);
  761. goto done;
  762. }
  763. r = 0;
  764. done:
  765. tor_free(p);
  766. return r;
  767. }
  768. #endif
  769. /** Tell the TCP stack that it shouldn't wait for a long time after
  770. * <b>sock</b> has closed before reusing its port. */
  771. static void
  772. make_socket_reuseable(tor_socket_t sock)
  773. {
  774. #ifdef _WIN32
  775. (void) sock;
  776. #else
  777. int one=1;
  778. /* REUSEADDR on normal places means you can rebind to the port
  779. * right after somebody else has let it go. But REUSEADDR on win32
  780. * means you can bind to the port _even when somebody else
  781. * already has it bound_. So, don't do that on Win32. */
  782. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
  783. (socklen_t)sizeof(one));
  784. #endif
  785. }
  786. /** Bind a new non-blocking socket listening to the socket described
  787. * by <b>listensockaddr</b>.
  788. *
  789. * <b>address</b> is only used for logging purposes and to add the information
  790. * to the conn.
  791. */
  792. static connection_t *
  793. connection_listener_new(const struct sockaddr *listensockaddr,
  794. socklen_t socklen,
  795. int type, const char *address,
  796. const port_cfg_t *port_cfg)
  797. {
  798. listener_connection_t *lis_conn;
  799. connection_t *conn;
  800. tor_socket_t s; /* the socket we're going to make */
  801. or_options_t const *options = get_options();
  802. #if defined(HAVE_PWD_H) && defined(HAVE_SYS_UN_H)
  803. struct passwd *pw = NULL;
  804. #endif
  805. uint16_t usePort = 0, gotPort = 0;
  806. int start_reading = 0;
  807. static int global_next_session_group = SESSION_GROUP_FIRST_AUTO;
  808. tor_addr_t addr;
  809. if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
  810. warn_too_many_conns();
  811. return NULL;
  812. }
  813. if (listensockaddr->sa_family == AF_INET ||
  814. listensockaddr->sa_family == AF_INET6) {
  815. int is_tcp = (type != CONN_TYPE_AP_DNS_LISTENER);
  816. if (is_tcp)
  817. start_reading = 1;
  818. tor_addr_from_sockaddr(&addr, listensockaddr, &usePort);
  819. log_notice(LD_NET, "Opening %s on %s:%d",
  820. conn_type_to_string(type), fmt_addr(&addr), usePort);
  821. s = tor_open_socket(tor_addr_family(&addr),
  822. is_tcp ? SOCK_STREAM : SOCK_DGRAM,
  823. is_tcp ? IPPROTO_TCP: IPPROTO_UDP);
  824. if (!SOCKET_OK(s)) {
  825. log_warn(LD_NET,"Socket creation failed: %s",
  826. tor_socket_strerror(tor_socket_errno(-1)));
  827. goto err;
  828. }
  829. make_socket_reuseable(s);
  830. #ifdef IPV6_V6ONLY
  831. if (listensockaddr->sa_family == AF_INET6) {
  832. #ifdef _WIN32
  833. /* In Redmond, this kind of thing passes for standards-conformance. */
  834. DWORD one = 1;
  835. #else
  836. int one = 1;
  837. #endif
  838. /* We need to set IPV6_V6ONLY so that this socket can't get used for
  839. * IPv4 connections. */
  840. if (setsockopt(s,IPPROTO_IPV6, IPV6_V6ONLY,
  841. (void*)&one, sizeof(one))<0) {
  842. int e = tor_socket_errno(s);
  843. log_warn(LD_NET, "Error setting IPV6_V6ONLY flag: %s",
  844. tor_socket_strerror(e));
  845. /* Keep going; probably not harmful. */
  846. }
  847. }
  848. #endif
  849. if (bind(s,listensockaddr,socklen) < 0) {
  850. const char *helpfulhint = "";
  851. int e = tor_socket_errno(s);
  852. if (ERRNO_IS_EADDRINUSE(e))
  853. helpfulhint = ". Is Tor already running?";
  854. log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
  855. tor_socket_strerror(e), helpfulhint);
  856. tor_close_socket(s);
  857. goto err;
  858. }
  859. if (is_tcp) {
  860. if (listen(s,SOMAXCONN) < 0) {
  861. log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
  862. tor_socket_strerror(tor_socket_errno(s)));
  863. tor_close_socket(s);
  864. goto err;
  865. }
  866. }
  867. if (usePort != 0) {
  868. gotPort = usePort;
  869. } else {
  870. tor_addr_t addr2;
  871. struct sockaddr_storage ss;
  872. socklen_t ss_len=sizeof(ss);
  873. if (getsockname(s, (struct sockaddr*)&ss, &ss_len)<0) {
  874. log_warn(LD_NET, "getsockname() couldn't learn address for %s: %s",
  875. conn_type_to_string(type),
  876. tor_socket_strerror(tor_socket_errno(s)));
  877. gotPort = 0;
  878. }
  879. tor_addr_from_sockaddr(&addr2, (struct sockaddr*)&ss, &gotPort);
  880. }
  881. #ifdef HAVE_SYS_UN_H
  882. } else if (listensockaddr->sa_family == AF_UNIX) {
  883. start_reading = 1;
  884. /* For now only control ports can be Unix domain sockets
  885. * and listeners at the same time */
  886. tor_assert(type == CONN_TYPE_CONTROL_LISTENER);
  887. if (check_location_for_unix_socket(options, address) < 0)
  888. goto err;
  889. log_notice(LD_NET, "Opening %s on %s",
  890. conn_type_to_string(type), address);
  891. tor_addr_make_unspec(&addr);
  892. if (unlink(address) < 0 && errno != ENOENT) {
  893. log_warn(LD_NET, "Could not unlink %s: %s", address,
  894. strerror(errno));
  895. goto err;
  896. }
  897. s = tor_open_socket(AF_UNIX, SOCK_STREAM, 0);
  898. if (! SOCKET_OK(s)) {
  899. log_warn(LD_NET,"Socket creation failed: %s.", strerror(errno));
  900. goto err;
  901. }
  902. if (bind(s, listensockaddr, (socklen_t)sizeof(struct sockaddr_un)) == -1) {
  903. log_warn(LD_NET,"Bind to %s failed: %s.", address,
  904. tor_socket_strerror(tor_socket_errno(s)));
  905. goto err;
  906. }
  907. #ifdef HAVE_PWD_H
  908. if (options->User) {
  909. pw = getpwnam(options->User);
  910. if (pw == NULL) {
  911. log_warn(LD_NET,"Unable to chown() %s socket: user %s not found.",
  912. address, options->User);
  913. } else if (chown(address, pw->pw_uid, pw->pw_gid) < 0) {
  914. log_warn(LD_NET,"Unable to chown() %s socket: %s.",
  915. address, strerror(errno));
  916. goto err;
  917. }
  918. }
  919. #endif
  920. if (options->ControlSocketsGroupWritable) {
  921. /* We need to use chmod; fchmod doesn't work on sockets on all
  922. * platforms. */
  923. if (chmod(address, 0660) < 0) {
  924. log_warn(LD_FS,"Unable to make %s group-writable.", address);
  925. tor_close_socket(s);
  926. goto err;
  927. }
  928. }
  929. if (listen(s,SOMAXCONN) < 0) {
  930. log_warn(LD_NET, "Could not listen on %s: %s", address,
  931. tor_socket_strerror(tor_socket_errno(s)));
  932. tor_close_socket(s);
  933. goto err;
  934. }
  935. #else
  936. (void)options;
  937. #endif /* HAVE_SYS_UN_H */
  938. } else {
  939. log_err(LD_BUG,"Got unexpected address family %d.",
  940. listensockaddr->sa_family);
  941. tor_assert(0);
  942. }
  943. set_socket_nonblocking(s);
  944. lis_conn = listener_connection_new(type, listensockaddr->sa_family);
  945. conn = TO_CONN(lis_conn);
  946. conn->socket_family = listensockaddr->sa_family;
  947. conn->s = s;
  948. conn->address = tor_strdup(address);
  949. conn->port = gotPort;
  950. tor_addr_copy(&conn->addr, &addr);
  951. if (port_cfg->isolation_flags) {
  952. lis_conn->isolation_flags = port_cfg->isolation_flags;
  953. if (port_cfg->session_group >= 0) {
  954. lis_conn->session_group = port_cfg->session_group;
  955. } else {
  956. /* This can wrap after around INT_MAX listeners are opened. But I don't
  957. * believe that matters, since you would need to open a ridiculous
  958. * number of listeners while keeping the early ones open before you ever
  959. * hit this. An OR with a dozen ports open, for example, would have to
  960. * close and re-open its listeners every second for 4 years nonstop.
  961. */
  962. lis_conn->session_group = global_next_session_group--;
  963. }
  964. }
  965. if (connection_add(conn) < 0) { /* no space, forget it */
  966. log_warn(LD_NET,"connection_add for listener failed. Giving up.");
  967. connection_free(conn);
  968. goto err;
  969. }
  970. log_fn(usePort==gotPort ? LOG_DEBUG : LOG_NOTICE, LD_NET,
  971. "%s listening on port %u.",
  972. conn_type_to_string(type), gotPort);
  973. conn->state = LISTENER_STATE_READY;
  974. if (start_reading) {
  975. connection_start_reading(conn);
  976. } else {
  977. tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
  978. dnsserv_configure_listener(conn);
  979. }
  980. return conn;
  981. err:
  982. return NULL;
  983. }
  984. /** Do basic sanity checking on a newly received socket. Return 0
  985. * if it looks ok, else return -1.
  986. *
  987. * Notably, some TCP stacks can erroneously have accept() return successfully
  988. * with socklen 0, when the client sends an RST before the accept call (as
  989. * nmap does). We want to detect that, and not go on with the connection.
  990. */
  991. static int
  992. check_sockaddr(struct sockaddr *sa, int len, int level)
  993. {
  994. int ok = 1;
  995. if (sa->sa_family == AF_INET) {
  996. struct sockaddr_in *sin=(struct sockaddr_in*)sa;
  997. if (len != sizeof(struct sockaddr_in)) {
  998. log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
  999. len,(int)sizeof(struct sockaddr_in));
  1000. ok = 0;
  1001. }
  1002. if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
  1003. log_fn(level, LD_NET,
  1004. "Address for new connection has address/port equal to zero.");
  1005. ok = 0;
  1006. }
  1007. } else if (sa->sa_family == AF_INET6) {
  1008. struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
  1009. if (len != sizeof(struct sockaddr_in6)) {
  1010. log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
  1011. len,(int)sizeof(struct sockaddr_in6));
  1012. ok = 0;
  1013. }
  1014. if (tor_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
  1015. sin6->sin6_port == 0) {
  1016. log_fn(level, LD_NET,
  1017. "Address for new connection has address/port equal to zero.");
  1018. ok = 0;
  1019. }
  1020. } else {
  1021. ok = 0;
  1022. }
  1023. return ok ? 0 : -1;
  1024. }
  1025. /** Check whether the socket family from an accepted socket <b>got</b> is the
  1026. * same as the one that <b>listener</b> is waiting for. If it isn't, log
  1027. * a useful message and return -1. Else return 0.
  1028. *
  1029. * This is annoying, but can apparently happen on some Darwins. */
  1030. static int
  1031. check_sockaddr_family_match(sa_family_t got, connection_t *listener)
  1032. {
  1033. if (got != listener->socket_family) {
  1034. log_info(LD_BUG, "A listener connection returned a socket with a "
  1035. "mismatched family. %s for addr_family %d gave us a socket "
  1036. "with address family %d. Dropping.",
  1037. conn_type_to_string(listener->type),
  1038. (int)listener->socket_family,
  1039. (int)got);
  1040. return -1;
  1041. }
  1042. return 0;
  1043. }
  1044. /** The listener connection <b>conn</b> told poll() it wanted to read.
  1045. * Call accept() on conn-\>s, and add the new connection if necessary.
  1046. */
  1047. static int
  1048. connection_handle_listener_read(connection_t *conn, int new_type)
  1049. {
  1050. tor_socket_t news; /* the new socket */
  1051. connection_t *newconn;
  1052. /* information about the remote peer when connecting to other routers */
  1053. struct sockaddr_storage addrbuf;
  1054. struct sockaddr *remote = (struct sockaddr*)&addrbuf;
  1055. /* length of the remote address. Must be whatever accept() needs. */
  1056. socklen_t remotelen = (socklen_t)sizeof(addrbuf);
  1057. const or_options_t *options = get_options();
  1058. tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
  1059. memset(&addrbuf, 0, sizeof(addrbuf));
  1060. news = tor_accept_socket(conn->s,remote,&remotelen);
  1061. if (!SOCKET_OK(news)) { /* accept() error */
  1062. int e = tor_socket_errno(conn->s);
  1063. if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
  1064. return 0; /* he hung up before we could accept(). that's fine. */
  1065. } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
  1066. warn_too_many_conns();
  1067. return 0;
  1068. }
  1069. /* else there was a real error. */
  1070. log_warn(LD_NET,"accept() failed: %s. Closing listener.",
  1071. tor_socket_strerror(e));
  1072. connection_mark_for_close(conn);
  1073. return -1;
  1074. }
  1075. log_debug(LD_NET,
  1076. "Connection accepted on socket %d (child of fd %d).",
  1077. (int)news,(int)conn->s);
  1078. make_socket_reuseable(news);
  1079. set_socket_nonblocking(news);
  1080. if (options->ConstrainedSockets)
  1081. set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
  1082. if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
  1083. tor_close_socket(news);
  1084. return 0;
  1085. }
  1086. if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6) {
  1087. tor_addr_t addr;
  1088. uint16_t port;
  1089. if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
  1090. log_info(LD_NET,
  1091. "accept() returned a strange address; closing connection.");
  1092. tor_close_socket(news);
  1093. return 0;
  1094. }
  1095. if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
  1096. tor_close_socket(news);
  1097. return 0;
  1098. }
  1099. tor_addr_from_sockaddr(&addr, remote, &port);
  1100. /* process entrance policies here, before we even create the connection */
  1101. if (new_type == CONN_TYPE_AP) {
  1102. /* check sockspolicy to see if we should accept it */
  1103. if (socks_policy_permits_address(&addr) == 0) {
  1104. log_notice(LD_APP,
  1105. "Denying socks connection from untrusted address %s.",
  1106. fmt_addr(&addr));
  1107. tor_close_socket(news);
  1108. return 0;
  1109. }
  1110. }
  1111. if (new_type == CONN_TYPE_DIR) {
  1112. /* check dirpolicy to see if we should accept it */
  1113. if (dir_policy_permits_address(&addr) == 0) {
  1114. log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
  1115. fmt_addr(&addr));
  1116. tor_close_socket(news);
  1117. return 0;
  1118. }
  1119. }
  1120. newconn = connection_new(new_type, conn->socket_family);
  1121. newconn->s = news;
  1122. /* remember the remote address */
  1123. tor_addr_copy(&newconn->addr, &addr);
  1124. newconn->port = port;
  1125. newconn->address = tor_dup_addr(&addr);
  1126. } else if (conn->socket_family == AF_UNIX) {
  1127. /* For now only control ports can be Unix domain sockets
  1128. * and listeners at the same time */
  1129. tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
  1130. newconn = connection_new(new_type, conn->socket_family);
  1131. newconn->s = news;
  1132. /* remember the remote address -- do we have anything sane to put here? */
  1133. tor_addr_make_unspec(&newconn->addr);
  1134. newconn->port = 1;
  1135. newconn->address = tor_strdup(conn->address);
  1136. } else {
  1137. tor_assert(0);
  1138. };
  1139. if (connection_add(newconn) < 0) { /* no space, forget it */
  1140. connection_free(newconn);
  1141. return 0; /* no need to tear down the parent */
  1142. }
  1143. if (connection_init_accepted_conn(newconn, TO_LISTENER_CONN(conn)) < 0) {
  1144. if (! newconn->marked_for_close)
  1145. connection_mark_for_close(newconn);
  1146. return 0;
  1147. }
  1148. return 0;
  1149. }
  1150. /** Initialize states for newly accepted connection <b>conn</b>.
  1151. * If conn is an OR, start the TLS handshake.
  1152. * If conn is a transparent AP, get its original destination
  1153. * and place it in circuit_wait.
  1154. */
  1155. static int
  1156. connection_init_accepted_conn(connection_t *conn,
  1157. const listener_connection_t *listener)
  1158. {
  1159. connection_start_reading(conn);
  1160. switch (conn->type) {
  1161. case CONN_TYPE_OR:
  1162. control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
  1163. return connection_tls_start_handshake(TO_OR_CONN(conn), 1);
  1164. case CONN_TYPE_AP:
  1165. TO_ENTRY_CONN(conn)->isolation_flags = listener->isolation_flags;
  1166. TO_ENTRY_CONN(conn)->session_group = listener->session_group;
  1167. TO_ENTRY_CONN(conn)->nym_epoch = get_signewnym_epoch();
  1168. TO_ENTRY_CONN(conn)->socks_request->listener_type = listener->_base.type;
  1169. switch (TO_CONN(listener)->type) {
  1170. case CONN_TYPE_AP_LISTENER:
  1171. conn->state = AP_CONN_STATE_SOCKS_WAIT;
  1172. break;
  1173. case CONN_TYPE_AP_TRANS_LISTENER:
  1174. TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
  1175. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  1176. return connection_ap_process_transparent(TO_ENTRY_CONN(conn));
  1177. case CONN_TYPE_AP_NATD_LISTENER:
  1178. TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
  1179. conn->state = AP_CONN_STATE_NATD_WAIT;
  1180. break;
  1181. }
  1182. break;
  1183. case CONN_TYPE_DIR:
  1184. conn->purpose = DIR_PURPOSE_SERVER;
  1185. conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
  1186. break;
  1187. case CONN_TYPE_CONTROL:
  1188. conn->state = CONTROL_CONN_STATE_NEEDAUTH;
  1189. break;
  1190. }
  1191. return 0;
  1192. }
  1193. /** Take conn, make a nonblocking socket; try to connect to
  1194. * addr:port (they arrive in *host order*). If fail, return -1 and if
  1195. * applicable put your best guess about errno into *<b>socket_error</b>.
  1196. * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
  1197. *
  1198. * address is used to make the logs useful.
  1199. *
  1200. * On success, add conn to the list of polled connections.
  1201. */
  1202. int
  1203. connection_connect(connection_t *conn, const char *address,
  1204. const tor_addr_t *addr, uint16_t port, int *socket_error)
  1205. {
  1206. tor_socket_t s;
  1207. int inprogress = 0;
  1208. struct sockaddr_storage addrbuf;
  1209. struct sockaddr *dest_addr;
  1210. int dest_addr_len;
  1211. const or_options_t *options = get_options();
  1212. int protocol_family;
  1213. if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
  1214. warn_too_many_conns();
  1215. *socket_error = ENOBUFS;
  1216. return -1;
  1217. }
  1218. if (tor_addr_family(addr) == AF_INET6)
  1219. protocol_family = PF_INET6;
  1220. else
  1221. protocol_family = PF_INET;
  1222. if (get_options()->DisableNetwork) {
  1223. /* We should never even try to connect anyplace if DisableNetwork is set.
  1224. * Warn if we do, and refuse to make the connection. */
  1225. static ratelim_t disablenet_violated = RATELIM_INIT(30*60);
  1226. char *m;
  1227. #ifdef _WIN32
  1228. *socket_error = WSAENETUNREACH;
  1229. #else
  1230. *socket_error = ENETUNREACH;
  1231. #endif
  1232. if ((m = rate_limit_log(&disablenet_violated, approx_time()))) {
  1233. log_warn(LD_BUG, "Tried to open a socket with DisableNetwork set.%s", m);
  1234. tor_free(m);
  1235. }
  1236. tor_fragile_assert();
  1237. return -1;
  1238. }
  1239. s = tor_open_socket(protocol_family,SOCK_STREAM,IPPROTO_TCP);
  1240. if (! SOCKET_OK(s)) {
  1241. *socket_error = tor_socket_errno(-1);
  1242. log_warn(LD_NET,"Error creating network socket: %s",
  1243. tor_socket_strerror(*socket_error));
  1244. return -1;
  1245. }
  1246. make_socket_reuseable(s);
  1247. if (options->OutboundBindAddress && !tor_addr_is_loopback(addr)) {
  1248. struct sockaddr_in ext_addr;
  1249. memset(&ext_addr, 0, sizeof(ext_addr));
  1250. ext_addr.sin_family = AF_INET;
  1251. ext_addr.sin_port = 0;
  1252. if (!tor_inet_aton(options->OutboundBindAddress, &ext_addr.sin_addr)) {
  1253. log_warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
  1254. options->OutboundBindAddress);
  1255. } else {
  1256. if (bind(s, (struct sockaddr*)&ext_addr,
  1257. (socklen_t)sizeof(ext_addr)) < 0) {
  1258. *socket_error = tor_socket_errno(s);
  1259. log_warn(LD_NET,"Error binding network socket: %s",
  1260. tor_socket_strerror(*socket_error));
  1261. tor_close_socket(s);
  1262. return -1;
  1263. }
  1264. }
  1265. }
  1266. set_socket_nonblocking(s);
  1267. if (options->ConstrainedSockets)
  1268. set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
  1269. memset(&addrbuf,0,sizeof(addrbuf));
  1270. dest_addr = (struct sockaddr*) &addrbuf;
  1271. dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
  1272. tor_assert(dest_addr_len > 0);
  1273. log_debug(LD_NET, "Connecting to %s:%u.",
  1274. escaped_safe_str_client(address), port);
  1275. if (connect(s, dest_addr, (socklen_t)dest_addr_len) < 0) {
  1276. int e = tor_socket_errno(s);
  1277. if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
  1278. /* yuck. kill it. */
  1279. *socket_error = e;
  1280. log_info(LD_NET,
  1281. "connect() to %s:%u failed: %s",
  1282. escaped_safe_str_client(address),
  1283. port, tor_socket_strerror(e));
  1284. tor_close_socket(s);
  1285. return -1;
  1286. } else {
  1287. inprogress = 1;
  1288. }
  1289. }
  1290. /* it succeeded. we're connected. */
  1291. log_fn(inprogress?LOG_DEBUG:LOG_INFO, LD_NET,
  1292. "Connection to %s:%u %s (sock %d).",
  1293. escaped_safe_str_client(address),
  1294. port, inprogress?"in progress":"established", s);
  1295. conn->s = s;
  1296. if (connection_add_connecting(conn) < 0) {
  1297. /* no space, forget it */
  1298. *socket_error = ENOBUFS;
  1299. return -1;
  1300. }
  1301. return inprogress ? 0 : 1;
  1302. }
  1303. /** Convert state number to string representation for logging purposes.
  1304. */
  1305. static const char *
  1306. connection_proxy_state_to_string(int state)
  1307. {
  1308. static const char *unknown = "???";
  1309. static const char *states[] = {
  1310. "PROXY_NONE",
  1311. "PROXY_INFANT",
  1312. "PROXY_HTTPS_WANT_CONNECT_OK",
  1313. "PROXY_SOCKS4_WANT_CONNECT_OK",
  1314. "PROXY_SOCKS5_WANT_AUTH_METHOD_NONE",
  1315. "PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929",
  1316. "PROXY_SOCKS5_WANT_AUTH_RFC1929_OK",
  1317. "PROXY_SOCKS5_WANT_CONNECT_OK",
  1318. "PROXY_CONNECTED",
  1319. };
  1320. if (state < PROXY_NONE || state > PROXY_CONNECTED)
  1321. return unknown;
  1322. return states[state];
  1323. }
  1324. /** Write a proxy request of <b>type</b> (socks4, socks5, https) to conn
  1325. * for conn->addr:conn->port, authenticating with the auth details given
  1326. * in the configuration (if available). SOCKS 5 and HTTP CONNECT proxies
  1327. * support authentication.
  1328. *
  1329. * Returns -1 if conn->addr is incompatible with the proxy protocol, and
  1330. * 0 otherwise.
  1331. *
  1332. * Use connection_read_proxy_handshake() to complete the handshake.
  1333. */
  1334. int
  1335. connection_proxy_connect(connection_t *conn, int type)
  1336. {
  1337. const or_options_t *options;
  1338. tor_assert(conn);
  1339. options = get_options();
  1340. switch (type) {
  1341. case PROXY_CONNECT: {
  1342. char buf[1024];
  1343. char *base64_authenticator=NULL;
  1344. const char *authenticator = options->HTTPSProxyAuthenticator;
  1345. /* Send HTTP CONNECT and authentication (if available) in
  1346. * one request */
  1347. if (authenticator) {
  1348. base64_authenticator = alloc_http_authenticator(authenticator);
  1349. if (!base64_authenticator)
  1350. log_warn(LD_OR, "Encoding https authenticator failed");
  1351. }
  1352. if (base64_authenticator) {
  1353. const char *addr = fmt_addr(&conn->addr);
  1354. tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.1\r\n"
  1355. "Host: %s:%d\r\n"
  1356. "Proxy-Authorization: Basic %s\r\n\r\n",
  1357. addr, conn->port,
  1358. addr, conn->port,
  1359. base64_authenticator);
  1360. tor_free(base64_authenticator);
  1361. } else {
  1362. tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.0\r\n\r\n",
  1363. fmt_addr(&conn->addr), conn->port);
  1364. }
  1365. connection_write_to_buf(buf, strlen(buf), conn);
  1366. conn->proxy_state = PROXY_HTTPS_WANT_CONNECT_OK;
  1367. break;
  1368. }
  1369. case PROXY_SOCKS4: {
  1370. unsigned char buf[9];
  1371. uint16_t portn;
  1372. uint32_t ip4addr;
  1373. /* Send a SOCKS4 connect request with empty user id */
  1374. if (tor_addr_family(&conn->addr) != AF_INET) {
  1375. log_warn(LD_NET, "SOCKS4 client is incompatible with IPv6");
  1376. return -1;
  1377. }
  1378. ip4addr = tor_addr_to_ipv4n(&conn->addr);
  1379. portn = htons(conn->port);
  1380. buf[0] = 4; /* version */
  1381. buf[1] = SOCKS_COMMAND_CONNECT; /* command */
  1382. memcpy(buf + 2, &portn, 2); /* port */
  1383. memcpy(buf + 4, &ip4addr, 4); /* addr */
  1384. buf[8] = 0; /* userid (empty) */
  1385. connection_write_to_buf((char *)buf, sizeof(buf), conn);
  1386. conn->proxy_state = PROXY_SOCKS4_WANT_CONNECT_OK;
  1387. break;
  1388. }
  1389. case PROXY_SOCKS5: {
  1390. unsigned char buf[4]; /* fields: vers, num methods, method list */
  1391. /* Send a SOCKS5 greeting (connect request must wait) */
  1392. buf[0] = 5; /* version */
  1393. /* number of auth methods */
  1394. if (options->Socks5ProxyUsername) {
  1395. buf[1] = 2;
  1396. buf[2] = 0x00; /* no authentication */
  1397. buf[3] = 0x02; /* rfc1929 Username/Passwd auth */
  1398. conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929;
  1399. } else {
  1400. buf[1] = 1;
  1401. buf[2] = 0x00; /* no authentication */
  1402. conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_NONE;
  1403. }
  1404. connection_write_to_buf((char *)buf, 2 + buf[1], conn);
  1405. break;
  1406. }
  1407. default:
  1408. log_err(LD_BUG, "Invalid proxy protocol, %d", type);
  1409. tor_fragile_assert();
  1410. return -1;
  1411. }
  1412. log_debug(LD_NET, "set state %s",
  1413. connection_proxy_state_to_string(conn->proxy_state));
  1414. return 0;
  1415. }
  1416. /** Read conn's inbuf. If the http response from the proxy is all
  1417. * here, make sure it's good news, then return 1. If it's bad news,
  1418. * return -1. Else return 0 and hope for better luck next time.
  1419. */
  1420. static int
  1421. connection_read_https_proxy_response(connection_t *conn)
  1422. {
  1423. char *headers;
  1424. char *reason=NULL;
  1425. int status_code;
  1426. time_t date_header;
  1427. switch (fetch_from_buf_http(conn->inbuf,
  1428. &headers, MAX_HEADERS_SIZE,
  1429. NULL, NULL, 10000, 0)) {
  1430. case -1: /* overflow */
  1431. log_warn(LD_PROTOCOL,
  1432. "Your https proxy sent back an oversized response. Closing.");
  1433. return -1;
  1434. case 0:
  1435. log_info(LD_NET,"https proxy response not all here yet. Waiting.");
  1436. return 0;
  1437. /* case 1, fall through */
  1438. }
  1439. if (parse_http_response(headers, &status_code, &date_header,
  1440. NULL, &reason) < 0) {
  1441. log_warn(LD_NET,
  1442. "Unparseable headers from proxy (connecting to '%s'). Closing.",
  1443. conn->address);
  1444. tor_free(headers);
  1445. return -1;
  1446. }
  1447. if (!reason) reason = tor_strdup("[no reason given]");
  1448. if (status_code == 200) {
  1449. log_info(LD_NET,
  1450. "HTTPS connect to '%s' successful! (200 %s) Starting TLS.",
  1451. conn->address, escaped(reason));
  1452. tor_free(reason);
  1453. return 1;
  1454. }
  1455. /* else, bad news on the status code */
  1456. switch (status_code) {
  1457. case 403:
  1458. log_warn(LD_NET,
  1459. "The https proxy refused to allow connection to %s "
  1460. "(status code %d, %s). Closing.",
  1461. conn->address, status_code, escaped(reason));
  1462. break;
  1463. default:
  1464. log_warn(LD_NET,
  1465. "The https proxy sent back an unexpected status code %d (%s). "
  1466. "Closing.",
  1467. status_code, escaped(reason));
  1468. break;
  1469. }
  1470. tor_free(reason);
  1471. return -1;
  1472. }
  1473. /** Send SOCKS5 CONNECT command to <b>conn</b>, copying <b>conn->addr</b>
  1474. * and <b>conn->port</b> into the request.
  1475. */
  1476. static void
  1477. connection_send_socks5_connect(connection_t *conn)
  1478. {
  1479. unsigned char buf[1024];
  1480. size_t reqsize = 6;
  1481. uint16_t port = htons(conn->port);
  1482. buf[0] = 5; /* version */
  1483. buf[1] = SOCKS_COMMAND_CONNECT; /* command */
  1484. buf[2] = 0; /* reserved */
  1485. if (tor_addr_family(&conn->addr) == AF_INET) {
  1486. uint32_t addr = tor_addr_to_ipv4n(&conn->addr);
  1487. buf[3] = 1;
  1488. reqsize += 4;
  1489. memcpy(buf + 4, &addr, 4);
  1490. memcpy(buf + 8, &port, 2);
  1491. } else { /* AF_INET6 */
  1492. buf[3] = 4;
  1493. reqsize += 16;
  1494. memcpy(buf + 4, tor_addr_to_in6(&conn->addr), 16);
  1495. memcpy(buf + 20, &port, 2);
  1496. }
  1497. connection_write_to_buf((char *)buf, reqsize, conn);
  1498. conn->proxy_state = PROXY_SOCKS5_WANT_CONNECT_OK;
  1499. }
  1500. /** Wrapper around fetch_from_(buf/evbuffer)_socks_client: see those functions
  1501. * for documentation of its behavior. */
  1502. static int
  1503. connection_fetch_from_buf_socks_client(connection_t *conn,
  1504. int state, char **reason)
  1505. {
  1506. IF_HAS_BUFFEREVENT(conn, {
  1507. struct evbuffer *input = bufferevent_get_input(conn->bufev);
  1508. return fetch_from_evbuffer_socks_client(input, state, reason);
  1509. }) ELSE_IF_NO_BUFFEREVENT {
  1510. return fetch_from_buf_socks_client(conn->inbuf, state, reason);
  1511. }
  1512. }
  1513. /** Call this from connection_*_process_inbuf() to advance the proxy
  1514. * handshake.
  1515. *
  1516. * No matter what proxy protocol is used, if this function returns 1, the
  1517. * handshake is complete, and the data remaining on inbuf may contain the
  1518. * start of the communication with the requested server.
  1519. *
  1520. * Returns 0 if the current buffer contains an incomplete response, and -1
  1521. * on error.
  1522. */
  1523. int
  1524. connection_read_proxy_handshake(connection_t *conn)
  1525. {
  1526. int ret = 0;
  1527. char *reason = NULL;
  1528. log_debug(LD_NET, "enter state %s",
  1529. connection_proxy_state_to_string(conn->proxy_state));
  1530. switch (conn->proxy_state) {
  1531. case PROXY_HTTPS_WANT_CONNECT_OK:
  1532. ret = connection_read_https_proxy_response(conn);
  1533. if (ret == 1)
  1534. conn->proxy_state = PROXY_CONNECTED;
  1535. break;
  1536. case PROXY_SOCKS4_WANT_CONNECT_OK:
  1537. ret = connection_fetch_from_buf_socks_client(conn,
  1538. conn->proxy_state,
  1539. &reason);
  1540. if (ret == 1)
  1541. conn->proxy_state = PROXY_CONNECTED;
  1542. break;
  1543. case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
  1544. ret = connection_fetch_from_buf_socks_client(conn,
  1545. conn->proxy_state,
  1546. &reason);
  1547. /* no auth needed, do connect */
  1548. if (ret == 1) {
  1549. connection_send_socks5_connect(conn);
  1550. ret = 0;
  1551. }
  1552. break;
  1553. case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
  1554. ret = connection_fetch_from_buf_socks_client(conn,
  1555. conn->proxy_state,
  1556. &reason);
  1557. /* send auth if needed, otherwise do connect */
  1558. if (ret == 1) {
  1559. connection_send_socks5_connect(conn);
  1560. ret = 0;
  1561. } else if (ret == 2) {
  1562. unsigned char buf[1024];
  1563. size_t reqsize, usize, psize;
  1564. const char *user, *pass;
  1565. user = get_options()->Socks5ProxyUsername;
  1566. pass = get_options()->Socks5ProxyPassword;
  1567. tor_assert(user && pass);
  1568. /* XXX len of user and pass must be <= 255 !!! */
  1569. usize = strlen(user);
  1570. psize = strlen(pass);
  1571. tor_assert(usize <= 255 && psize <= 255);
  1572. reqsize = 3 + usize + psize;
  1573. buf[0] = 1; /* negotiation version */
  1574. buf[1] = usize;
  1575. memcpy(buf + 2, user, usize);
  1576. buf[2 + usize] = psize;
  1577. memcpy(buf + 3 + usize, pass, psize);
  1578. connection_write_to_buf((char *)buf, reqsize, conn);
  1579. conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_RFC1929_OK;
  1580. ret = 0;
  1581. }
  1582. break;
  1583. case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
  1584. ret = connection_fetch_from_buf_socks_client(conn,
  1585. conn->proxy_state,
  1586. &reason);
  1587. /* send the connect request */
  1588. if (ret == 1) {
  1589. connection_send_socks5_connect(conn);
  1590. ret = 0;
  1591. }
  1592. break;
  1593. case PROXY_SOCKS5_WANT_CONNECT_OK:
  1594. ret = connection_fetch_from_buf_socks_client(conn,
  1595. conn->proxy_state,
  1596. &reason);
  1597. if (ret == 1)
  1598. conn->proxy_state = PROXY_CONNECTED;
  1599. break;
  1600. default:
  1601. log_err(LD_BUG, "Invalid proxy_state for reading, %d",
  1602. conn->proxy_state);
  1603. tor_fragile_assert();
  1604. ret = -1;
  1605. break;
  1606. }
  1607. log_debug(LD_NET, "leaving state %s",
  1608. connection_proxy_state_to_string(conn->proxy_state));
  1609. if (ret < 0) {
  1610. if (reason) {
  1611. log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d (%s)",
  1612. conn->address, conn->port, escaped(reason));
  1613. tor_free(reason);
  1614. } else {
  1615. log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d",
  1616. conn->address, conn->port);
  1617. }
  1618. } else if (ret == 1) {
  1619. log_info(LD_NET, "Proxy Client: connection to %s:%d successful",
  1620. conn->address, conn->port);
  1621. }
  1622. return ret;
  1623. }
  1624. /** Given a list of listener connections in <b>old_conns</b>, and list of
  1625. * port_cfg_t entries in <b>ports</b>, open a new listener for every port in
  1626. * <b>ports</b> that does not already have a listener in <b>old_conns</b>.
  1627. *
  1628. * Remove from <b>old_conns</b> every connection that has a corresponding
  1629. * entry in <b>ports</b>. Add to <b>new_conns</b> new every connection we
  1630. * launch.
  1631. *
  1632. * If <b>control_listeners_only</b> is true, then we only open control
  1633. * listeners, and we do not remove any noncontrol listeners from old_conns.
  1634. *
  1635. * Return 0 on success, -1 on failure.
  1636. **/
  1637. static int
  1638. retry_listener_ports(smartlist_t *old_conns,
  1639. const smartlist_t *ports,
  1640. smartlist_t *new_conns,
  1641. int control_listeners_only)
  1642. {
  1643. smartlist_t *launch = smartlist_new();
  1644. int r = 0;
  1645. if (control_listeners_only) {
  1646. SMARTLIST_FOREACH(ports, port_cfg_t *, p, {
  1647. if (p->type == CONN_TYPE_CONTROL_LISTENER)
  1648. smartlist_add(launch, p);
  1649. });
  1650. } else {
  1651. smartlist_add_all(launch, ports);
  1652. }
  1653. /* Iterate through old_conns, comparing it to launch: remove from both lists
  1654. * each pair of elements that corresponds to the same port. */
  1655. SMARTLIST_FOREACH_BEGIN(old_conns, connection_t *, conn) {
  1656. const port_cfg_t *found_port = NULL;
  1657. /* Okay, so this is a listener. Is it configured? */
  1658. SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, wanted) {
  1659. if (conn->type != wanted->type)
  1660. continue;
  1661. if ((conn->socket_family != AF_UNIX && wanted->is_unix_addr) ||
  1662. (conn->socket_family == AF_UNIX && ! wanted->is_unix_addr))
  1663. continue;
  1664. if (wanted->no_listen)
  1665. continue; /* We don't want to open a listener for this one */
  1666. if (wanted->is_unix_addr) {
  1667. if (conn->socket_family == AF_UNIX &&
  1668. !strcmp(wanted->unix_addr, conn->address)) {
  1669. found_port = wanted;
  1670. break;
  1671. }
  1672. } else {
  1673. int port_matches;
  1674. if (wanted->port == CFG_AUTO_PORT) {
  1675. port_matches = 1;
  1676. } else {
  1677. port_matches = (wanted->port == conn->port);
  1678. }
  1679. if (port_matches && tor_addr_eq(&wanted->addr, &conn->addr)) {
  1680. found_port = wanted;
  1681. break;
  1682. }
  1683. }
  1684. } SMARTLIST_FOREACH_END(wanted);
  1685. if (found_port) {
  1686. /* This listener is already running; we don't need to launch it. */
  1687. //log_debug(LD_NET, "Already have %s on %s:%d",
  1688. // conn_type_to_string(found_port->type), conn->address, conn->port);
  1689. smartlist_remove(launch, found_port);
  1690. /* And we can remove the connection from old_conns too. */
  1691. SMARTLIST_DEL_CURRENT(old_conns, conn);
  1692. }
  1693. } SMARTLIST_FOREACH_END(conn);
  1694. /* Now open all the listeners that are configured but not opened. */
  1695. SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, port) {
  1696. struct sockaddr *listensockaddr;
  1697. socklen_t listensocklen = 0;
  1698. char *address=NULL;
  1699. connection_t *conn;
  1700. int real_port = port->port == CFG_AUTO_PORT ? 0 : port->port;
  1701. tor_assert(real_port <= UINT16_MAX);
  1702. if (port->no_listen)
  1703. continue;
  1704. if (port->is_unix_addr) {
  1705. listensockaddr = (struct sockaddr *)
  1706. create_unix_sockaddr(port->unix_addr,
  1707. &address, &listensocklen);
  1708. } else {
  1709. listensockaddr = tor_malloc(sizeof(struct sockaddr_storage));
  1710. listensocklen = tor_addr_to_sockaddr(&port->addr,
  1711. real_port,
  1712. listensockaddr,
  1713. sizeof(struct sockaddr_storage));
  1714. address = tor_dup_addr(&port->addr);
  1715. }
  1716. if (listensockaddr) {
  1717. conn = connection_listener_new(listensockaddr, listensocklen,
  1718. port->type, address, port);
  1719. tor_free(listensockaddr);
  1720. tor_free(address);
  1721. } else {
  1722. conn = NULL;
  1723. }
  1724. if (!conn) {
  1725. r = -1;
  1726. } else {
  1727. if (new_conns)
  1728. smartlist_add(new_conns, conn);
  1729. }
  1730. } SMARTLIST_FOREACH_END(port);
  1731. smartlist_free(launch);
  1732. return r;
  1733. }
  1734. /** Launch listeners for each port you should have open. Only launch
  1735. * listeners who are not already open, and only close listeners we no longer
  1736. * want.
  1737. *
  1738. * Add all old conns that should be closed to <b>replaced_conns</b>.
  1739. * Add all new connections to <b>new_conns</b>.
  1740. *
  1741. * If <b>close_all_noncontrol</b> is true, then we only open control
  1742. * listeners, and we close all other listeners.
  1743. */
  1744. int
  1745. retry_all_listeners(smartlist_t *replaced_conns,
  1746. smartlist_t *new_conns, int close_all_noncontrol)
  1747. {
  1748. smartlist_t *listeners = smartlist_new();
  1749. const or_options_t *options = get_options();
  1750. int retval = 0;
  1751. const uint16_t old_or_port = router_get_advertised_or_port(options);
  1752. const uint16_t old_dir_port = router_get_advertised_dir_port(options, 0);
  1753. SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
  1754. if (connection_is_listener(conn) && !conn->marked_for_close)
  1755. smartlist_add(listeners, conn);
  1756. } SMARTLIST_FOREACH_END(conn);
  1757. if (retry_listener_ports(listeners,
  1758. get_configured_ports(),
  1759. new_conns,
  1760. close_all_noncontrol) < 0)
  1761. retval = -1;
  1762. /* Any members that were still in 'listeners' don't correspond to
  1763. * any configured port. Kill 'em. */
  1764. SMARTLIST_FOREACH_BEGIN(listeners, connection_t *, conn) {
  1765. log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
  1766. conn_type_to_string(conn->type), conn->address, conn->port);
  1767. if (replaced_conns) {
  1768. smartlist_add(replaced_conns, conn);
  1769. } else {
  1770. connection_close_immediate(conn);
  1771. connection_mark_for_close(conn);
  1772. }
  1773. } SMARTLIST_FOREACH_END(conn);
  1774. smartlist_free(listeners);
  1775. /* XXXprop186 should take all advertised ports into account */
  1776. if (old_or_port != router_get_advertised_or_port(options) ||
  1777. old_dir_port != router_get_advertised_dir_port(options, 0)) {
  1778. /* Our chosen ORPort or DirPort is not what it used to be: the
  1779. * descriptor we had (if any) should be regenerated. (We won't
  1780. * automatically notice this because of changes in the option,
  1781. * since the value could be "auto".) */
  1782. mark_my_descriptor_dirty("Chosen Or/DirPort changed");
  1783. }
  1784. return retval;
  1785. }
  1786. /** Mark every listener of type other than CONTROL_LISTENER to be closed. */
  1787. void
  1788. connection_mark_all_noncontrol_listeners(void)
  1789. {
  1790. SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
  1791. if (conn->marked_for_close)
  1792. continue;
  1793. if (conn->type == CONN_TYPE_CONTROL_LISTENER)
  1794. continue;
  1795. if (connection_is_listener(conn))
  1796. connection_mark_for_close(conn);
  1797. } SMARTLIST_FOREACH_END(conn);
  1798. }
  1799. /** Mark every external connection not used for controllers for close. */
  1800. void
  1801. connection_mark_all_noncontrol_connections(void)
  1802. {
  1803. SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
  1804. if (conn->marked_for_close)
  1805. continue;
  1806. switch (conn->type) {
  1807. case CONN_TYPE_CPUWORKER:
  1808. case CONN_TYPE_CONTROL_LISTENER:
  1809. case CONN_TYPE_CONTROL:
  1810. break;
  1811. case CONN_TYPE_AP:
  1812. connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
  1813. END_STREAM_REASON_HIBERNATING);
  1814. break;
  1815. default:
  1816. connection_mark_for_close(conn);
  1817. break;
  1818. }
  1819. } SMARTLIST_FOREACH_END(conn);
  1820. }
  1821. /** Return 1 if we should apply rate limiting to <b>conn</b>, and 0
  1822. * otherwise.
  1823. * Right now this just checks if it's an internal IP address or an
  1824. * internal connection. We also check if the connection uses pluggable
  1825. * transports, since we should then limit it even if it comes from an
  1826. * internal IP address. */
  1827. static int
  1828. connection_is_rate_limited(connection_t *conn)
  1829. {
  1830. const or_options_t *options = get_options();
  1831. if (conn->linked)
  1832. return 0; /* Internal connection */
  1833. else if (! options->CountPrivateBandwidth &&
  1834. (tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
  1835. tor_addr_is_internal(&conn->addr, 0)))
  1836. return 0; /* Internal address */
  1837. else
  1838. return 1;
  1839. }
  1840. #ifdef USE_BUFFEREVENTS
  1841. static struct bufferevent_rate_limit_group *global_rate_limit = NULL;
  1842. #else
  1843. extern int global_read_bucket, global_write_bucket;
  1844. extern int global_relayed_read_bucket, global_relayed_write_bucket;
  1845. /** Did either global write bucket run dry last second? If so,
  1846. * we are likely to run dry again this second, so be stingy with the
  1847. * tokens we just put in. */
  1848. static int write_buckets_empty_last_second = 0;
  1849. #endif
  1850. /** How many seconds of no active local circuits will make the
  1851. * connection revert to the "relayed" bandwidth class? */
  1852. #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
  1853. #ifndef USE_BUFFEREVENTS
  1854. /** Return 1 if <b>conn</b> should use tokens from the "relayed"
  1855. * bandwidth rates, else 0. Currently, only OR conns with bandwidth
  1856. * class 1, and directory conns that are serving data out, count.
  1857. */
  1858. static int
  1859. connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
  1860. {
  1861. if (conn->type == CONN_TYPE_OR &&
  1862. TO_OR_CONN(conn)->client_used + CLIENT_IDLE_TIME_FOR_PRIORITY < now)
  1863. return 1;
  1864. if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
  1865. return 1;
  1866. return 0;
  1867. }
  1868. /** Helper function to decide how many bytes out of <b>global_bucket</b>
  1869. * we're willing to use for this transaction. <b>base</b> is the size
  1870. * of a cell on the network; <b>priority</b> says whether we should
  1871. * write many of them or just a few; and <b>conn_bucket</b> (if
  1872. * non-negative) provides an upper limit for our answer. */
  1873. static ssize_t
  1874. connection_bucket_round_robin(int base, int priority,
  1875. ssize_t global_bucket, ssize_t conn_bucket)
  1876. {
  1877. ssize_t at_most;
  1878. ssize_t num_bytes_high = (priority ? 32 : 16) * base;
  1879. ssize_t num_bytes_low = (priority ? 4 : 2) * base;
  1880. /* Do a rudimentary round-robin so one circuit can't hog a connection.
  1881. * Pick at most 32 cells, at least 4 cells if possible, and if we're in
  1882. * the middle pick 1/8 of the available bandwidth. */
  1883. at_most = global_bucket / 8;
  1884. at_most -= (at_most % base); /* round down */
  1885. if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
  1886. at_most = num_bytes_high;
  1887. else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
  1888. at_most = num_bytes_low;
  1889. if (at_most > global_bucket)
  1890. at_most = global_bucket;
  1891. if (conn_bucket >= 0 && at_most > conn_bucket)
  1892. at_most = conn_bucket;
  1893. if (at_most < 0)
  1894. return 0;
  1895. return at_most;
  1896. }
  1897. /** How many bytes at most can we read onto this connection? */
  1898. static ssize_t
  1899. connection_bucket_read_limit(connection_t *conn, time_t now)
  1900. {
  1901. int base = connection_speaks_cells(conn) ?
  1902. CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
  1903. int priority = conn->type != CONN_TYPE_DIR;
  1904. int conn_bucket = -1;
  1905. int global_bucket = global_read_bucket;
  1906. if (connection_speaks_cells(conn)) {
  1907. or_connection_t *or_conn = TO_OR_CONN(conn);
  1908. if (conn->state == OR_CONN_STATE_OPEN)
  1909. conn_bucket = or_conn->read_bucket;
  1910. }
  1911. if (!connection_is_rate_limited(conn)) {
  1912. /* be willing to read on local conns even if our buckets are empty */
  1913. return conn_bucket>=0 ? conn_bucket : 1<<14;
  1914. }
  1915. if (connection_counts_as_relayed_traffic(conn, now) &&
  1916. global_relayed_read_bucket <= global_read_bucket)
  1917. global_bucket = global_relayed_read_bucket;
  1918. return connection_bucket_round_robin(base, priority,
  1919. global_bucket, conn_bucket);
  1920. }
  1921. /** How many bytes at most can we write onto this connection? */
  1922. ssize_t
  1923. connection_bucket_write_limit(connection_t *conn, time_t now)
  1924. {
  1925. int base = connection_speaks_cells(conn) ?
  1926. CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
  1927. int priority = conn->type != CONN_TYPE_DIR;
  1928. int conn_bucket = (int)conn->outbuf_flushlen;
  1929. int global_bucket = global_write_bucket;
  1930. if (!connection_is_rate_limited(conn)) {
  1931. /* be willing to write to local conns even if our buckets are empty */
  1932. return conn->outbuf_flushlen;
  1933. }
  1934. if (connection_speaks_cells(conn)) {
  1935. /* use the per-conn write limit if it's lower, but if it's less
  1936. * than zero just use zero */
  1937. or_connection_t *or_conn = TO_OR_CONN(conn);
  1938. if (conn->state == OR_CONN_STATE_OPEN)
  1939. if (or_conn->write_bucket < conn_bucket)
  1940. conn_bucket = or_conn->write_bucket >= 0 ?
  1941. or_conn->write_bucket : 0;
  1942. }
  1943. if (connection_counts_as_relayed_traffic(conn, now) &&
  1944. global_relayed_write_bucket <= global_write_bucket)
  1945. global_bucket = global_relayed_write_bucket;
  1946. return connection_bucket_round_robin(base, priority,
  1947. global_bucket, conn_bucket);
  1948. }
  1949. #else
  1950. static ssize_t
  1951. connection_bucket_read_limit(connection_t *conn, time_t now)
  1952. {
  1953. (void) now;
  1954. return bufferevent_get_max_to_read(conn->bufev);
  1955. }
  1956. ssize_t
  1957. connection_bucket_write_limit(connection_t *conn, time_t now)
  1958. {
  1959. (void) now;
  1960. return bufferevent_get_max_to_write(conn->bufev);
  1961. }
  1962. #endif
  1963. /** Return 1 if the global write buckets are low enough that we
  1964. * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
  1965. * out to <b>conn</b>. Else return 0.
  1966. * Priority is 1 for v1 requests (directories and running-routers),
  1967. * and 2 for v2 requests (statuses and descriptors). But see FFFF in
  1968. * directory_handle_command_get() for why we don't use priority 2 yet.
  1969. *
  1970. * There are a lot of parameters we could use here:
  1971. * - global_relayed_write_bucket. Low is bad.
  1972. * - global_write_bucket. Low is bad.
  1973. * - bandwidthrate. Low is bad.
  1974. * - bandwidthburst. Not a big factor?
  1975. * - attempt. High is bad.
  1976. * - total bytes queued on outbufs. High is bad. But I'm wary of
  1977. * using this, since a few slow-flushing queues will pump up the
  1978. * number without meaning what we meant to mean. What we really
  1979. * mean is "total directory bytes added to outbufs recently", but
  1980. * that's harder to quantify and harder to keep track of.
  1981. */
  1982. int
  1983. global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
  1984. {
  1985. #ifdef USE_BUFFEREVENTS
  1986. ssize_t smaller_bucket = bufferevent_get_max_to_write(conn->bufev);
  1987. #else
  1988. int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
  1989. global_write_bucket : global_relayed_write_bucket;
  1990. #endif
  1991. if (authdir_mode(get_options()) && priority>1)
  1992. return 0; /* there's always room to answer v2 if we're an auth dir */
  1993. if (!connection_is_rate_limited(conn))
  1994. return 0; /* local conns don't get limited */
  1995. if (smaller_bucket < (int)attempt)
  1996. return 1; /* not enough space no matter the priority */
  1997. #ifndef USE_BUFFEREVENTS
  1998. if (write_buckets_empty_last_second)
  1999. return 1; /* we're already hitting our limits, no more please */
  2000. #endif
  2001. if (priority == 1) { /* old-style v1 query */
  2002. /* Could we handle *two* of these requests within the next two seconds? */
  2003. const or_options_t *options = get_options();
  2004. int64_t can_write = (int64_t)smaller_bucket
  2005. + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
  2006. options->BandwidthRate);
  2007. if (can_write < 2*(int64_t)attempt)
  2008. return 1;
  2009. } else { /* v2 query */
  2010. /* no further constraints yet */
  2011. }
  2012. return 0;
  2013. }
  2014. /** Helper: adjusts our bandwidth history and informs the controller as
  2015. * appropriate, given that we have just read <b>num_read</b> bytes and written
  2016. * <b>num_written</b> bytes on <b>conn</b>. */
  2017. static void
  2018. record_num_bytes_transferred_impl(connection_t *conn,
  2019. time_t now, size_t num_read, size_t num_written)
  2020. {
  2021. /* Count bytes of answering direct and tunneled directory requests */
  2022. if (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER) {
  2023. if (num_read > 0)
  2024. rep_hist_note_dir_bytes_read(num_read, now);
  2025. if (num_written > 0)
  2026. rep_hist_note_dir_bytes_written(num_written, now);
  2027. }
  2028. if (!connection_is_rate_limited(conn))
  2029. return; /* local IPs are free */
  2030. if (conn->type == CONN_TYPE_OR)
  2031. rep_hist_note_or_conn_bytes(conn->global_identifier, num_read,
  2032. num_written, now);
  2033. if (num_read > 0) {
  2034. rep_hist_note_bytes_read(num_read, now);
  2035. }
  2036. if (num_written > 0) {
  2037. rep_hist_note_bytes_written(num_written, now);
  2038. }
  2039. if (conn->type == CONN_TYPE_EXIT)
  2040. rep_hist_note_exit_bytes(conn->port, num_written, num_read);
  2041. }
  2042. #ifdef USE_BUFFEREVENTS
  2043. /** Wrapper around fetch_from_(buf/evbuffer)_socks_client: see those functions
  2044. * for documentation of its behavior. */
  2045. static void
  2046. record_num_bytes_transferred(connection_t *conn,
  2047. time_t now, size_t num_read, size_t num_written)
  2048. {
  2049. /* XXX024 check if this is necessary */
  2050. if (num_written >= INT_MAX || num_read >= INT_MAX) {
  2051. log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
  2052. "connection type=%s, state=%s",
  2053. (unsigned long)num_read, (unsigned long)num_written,
  2054. conn_type_to_string(conn->type),
  2055. conn_state_to_string(conn->type, conn->state));
  2056. if (num_written >= INT_MAX) num_written = 1;
  2057. if (num_read >= INT_MAX) num_read = 1;
  2058. tor_fragile_assert();
  2059. }
  2060. record_num_bytes_transferred_impl(conn,now,num_read,num_written);
  2061. }
  2062. #endif
  2063. #ifndef USE_BUFFEREVENTS
  2064. /** We just read <b>num_read</b> and wrote <b>num_written</b> bytes
  2065. * onto <b>conn</b>. Decrement buckets appropriately. */
  2066. static void
  2067. connection_buckets_decrement(connection_t *conn, time_t now,
  2068. size_t num_read, size_t num_written)
  2069. {
  2070. if (num_written >= INT_MAX || num_read >= INT_MAX) {
  2071. log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
  2072. "connection type=%s, state=%s",
  2073. (unsigned long)num_read, (unsigned long)num_written,
  2074. conn_type_to_string(conn->type),
  2075. conn_state_to_string(conn->type, conn->state));
  2076. if (num_written >= INT_MAX) num_written = 1;
  2077. if (num_read >= INT_MAX) num_read = 1;
  2078. tor_fragile_assert();
  2079. }
  2080. record_num_bytes_transferred_impl(conn, now, num_read, num_written);
  2081. if (!connection_is_rate_limited(conn))
  2082. return; /* local IPs are free */
  2083. if (connection_counts_as_relayed_traffic(conn, now)) {
  2084. global_relayed_read_bucket -= (int)num_read;
  2085. global_relayed_write_bucket -= (int)num_written;
  2086. }
  2087. global_read_bucket -= (int)num_read;
  2088. global_write_bucket -= (int)num_written;
  2089. if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
  2090. TO_OR_CONN(conn)->read_bucket -= (int)num_read;
  2091. TO_OR_CONN(conn)->write_bucket -= (int)num_written;
  2092. }
  2093. }
  2094. /** If we have exhausted our global buckets, or the buckets for conn,
  2095. * stop reading. */
  2096. static void
  2097. connection_consider_empty_read_buckets(connection_t *conn)
  2098. {
  2099. const char *reason;
  2100. if (global_read_bucket <= 0) {
  2101. reason = "global read bucket exhausted. Pausing.";
  2102. } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
  2103. global_relayed_read_bucket <= 0) {
  2104. reason = "global relayed read bucket exhausted. Pausing.";
  2105. } else if (connection_speaks_cells(conn) &&
  2106. conn->state == OR_CONN_STATE_OPEN &&
  2107. TO_OR_CONN(conn)->read_bucket <= 0) {
  2108. reason = "connection read bucket exhausted. Pausing.";
  2109. } else
  2110. return; /* all good, no need to stop it */
  2111. LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
  2112. conn->read_blocked_on_bw = 1;
  2113. connection_stop_reading(conn);
  2114. }
  2115. /** If we have exhausted our global buckets, or the buckets for conn,
  2116. * stop writing. */
  2117. static void
  2118. connection_consider_empty_write_buckets(connection_t *conn)
  2119. {
  2120. const char *reason;
  2121. if (global_write_bucket <= 0) {
  2122. reason = "global write bucket exhausted. Pausing.";
  2123. } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
  2124. global_relayed_write_bucket <= 0) {
  2125. reason = "global relayed write bucket exhausted. Pausing.";
  2126. } else if (connection_speaks_cells(conn) &&
  2127. conn->state == OR_CONN_STATE_OPEN &&
  2128. TO_OR_CONN(conn)->write_bucket <= 0) {
  2129. reason = "connection write bucket exhausted. Pausing.";
  2130. } else
  2131. return; /* all good, no need to stop it */
  2132. LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
  2133. conn->write_blocked_on_bw = 1;
  2134. connection_stop_writing(conn);
  2135. }
  2136. /** Initialize the global read bucket to options-\>BandwidthBurst. */
  2137. void
  2138. connection_bucket_init(void)
  2139. {
  2140. const or_options_t *options = get_options();
  2141. /* start it at max traffic */
  2142. global_read_bucket = (int)options->BandwidthBurst;
  2143. global_write_bucket = (int)options->BandwidthBurst;
  2144. if (options->RelayBandwidthRate) {
  2145. global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
  2146. global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
  2147. } else {
  2148. global_relayed_read_bucket = (int)options->BandwidthBurst;
  2149. global_relayed_write_bucket = (int)options->BandwidthBurst;
  2150. }
  2151. }
  2152. /** Refill a single <b>bucket</b> called <b>name</b> with bandwidth rate per
  2153. * second <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
  2154. * <b>milliseconds_elapsed</b> milliseconds have passed since the last
  2155. * call. */
  2156. static void
  2157. connection_bucket_refill_helper(int *bucket, int rate, int burst,
  2158. int milliseconds_elapsed,
  2159. const char *name)
  2160. {
  2161. int starting_bucket = *bucket;
  2162. if (starting_bucket < burst && milliseconds_elapsed > 0) {
  2163. int64_t incr = (((int64_t)rate) * milliseconds_elapsed) / 1000;
  2164. if ((burst - starting_bucket) < incr) {
  2165. *bucket = burst; /* We would overflow the bucket; just set it to
  2166. * the maximum. */
  2167. } else {
  2168. *bucket += (int)incr;
  2169. if (*bucket > burst || *bucket < starting_bucket) {
  2170. /* If we overflow the burst, or underflow our starting bucket,
  2171. * cap the bucket value to burst. */
  2172. /* XXXX this might be redundant now, but it doesn't show up
  2173. * in profiles. Remove it after analysis. */
  2174. *bucket = burst;
  2175. }
  2176. }
  2177. log(LOG_DEBUG, LD_NET,"%s now %d.", name, *bucket);
  2178. }
  2179. }
  2180. /** Time has passed; increment buckets appropriately. */
  2181. void
  2182. connection_bucket_refill(int milliseconds_elapsed, time_t now)
  2183. {
  2184. const or_options_t *options = get_options();
  2185. smartlist_t *conns = get_connection_array();
  2186. int bandwidthrate, bandwidthburst, relayrate, relayburst;
  2187. bandwidthrate = (int)options->BandwidthRate;
  2188. bandwidthburst = (int)options->BandwidthBurst;
  2189. if (options->RelayBandwidthRate) {
  2190. relayrate = (int)options->RelayBandwidthRate;
  2191. relayburst = (int)options->RelayBandwidthBurst;
  2192. } else {
  2193. relayrate = bandwidthrate;
  2194. relayburst = bandwidthburst;
  2195. }
  2196. tor_assert(milliseconds_elapsed >= 0);
  2197. write_buckets_empty_last_second =
  2198. global_relayed_write_bucket <= 0 || global_write_bucket <= 0;
  2199. /* refill the global buckets */
  2200. connection_bucket_refill_helper(&global_read_bucket,
  2201. bandwidthrate, bandwidthburst,
  2202. milliseconds_elapsed,
  2203. "global_read_bucket");
  2204. connection_bucket_refill_helper(&global_write_bucket,
  2205. bandwidthrate, bandwidthburst,
  2206. milliseconds_elapsed,
  2207. "global_write_bucket");
  2208. connection_bucket_refill_helper(&global_relayed_read_bucket,
  2209. relayrate, relayburst,
  2210. milliseconds_elapsed,
  2211. "global_relayed_read_bucket");
  2212. connection_bucket_refill_helper(&global_relayed_write_bucket,
  2213. relayrate, relayburst,
  2214. milliseconds_elapsed,
  2215. "global_relayed_write_bucket");
  2216. /* refill the per-connection buckets */
  2217. SMARTLIST_FOREACH(conns, connection_t *, conn,
  2218. {
  2219. if (connection_speaks_cells(conn)) {
  2220. or_connection_t *or_conn = TO_OR_CONN(conn);
  2221. int orbandwidthrate = or_conn->bandwidthrate;
  2222. int orbandwidthburst = or_conn->bandwidthburst;
  2223. if (connection_bucket_should_increase(or_conn->read_bucket, or_conn)) {
  2224. connection_bucket_refill_helper(&or_conn->read_bucket,
  2225. orbandwidthrate,
  2226. orbandwidthburst,
  2227. milliseconds_elapsed,
  2228. "or_conn->read_bucket");
  2229. }
  2230. if (connection_bucket_should_increase(or_conn->write_bucket, or_conn)) {
  2231. connection_bucket_refill_helper(&or_conn->write_bucket,
  2232. orbandwidthrate,
  2233. orbandwidthburst,
  2234. milliseconds_elapsed,
  2235. "or_conn->write_bucket");
  2236. }
  2237. }
  2238. if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
  2239. && global_read_bucket > 0 /* and we're allowed to read */
  2240. && (!connection_counts_as_relayed_traffic(conn, now) ||
  2241. global_relayed_read_bucket > 0) /* even if we're relayed traffic */
  2242. && (!connection_speaks_cells(conn) ||
  2243. conn->state != OR_CONN_STATE_OPEN ||
  2244. TO_OR_CONN(conn)->read_bucket > 0)) {
  2245. /* and either a non-cell conn or a cell conn with non-empty bucket */
  2246. LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
  2247. "waking up conn (fd %d) for read", (int)conn->s));
  2248. conn->read_blocked_on_bw = 0;
  2249. connection_start_reading(conn);
  2250. }
  2251. if (conn->write_blocked_on_bw == 1
  2252. && global_write_bucket > 0 /* and we're allowed to write */
  2253. && (!connection_counts_as_relayed_traffic(conn, now) ||
  2254. global_relayed_write_bucket > 0) /* even if it's relayed traffic */
  2255. && (!connection_speaks_cells(conn) ||
  2256. conn->state != OR_CONN_STATE_OPEN ||
  2257. TO_OR_CONN(conn)->write_bucket > 0)) {
  2258. LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
  2259. "waking up conn (fd %d) for write", (int)conn->s));
  2260. conn->write_blocked_on_bw = 0;
  2261. connection_start_writing(conn);
  2262. }
  2263. });
  2264. }
  2265. /** Is the <b>bucket</b> for connection <b>conn</b> low enough that we
  2266. * should add another pile of tokens to it?
  2267. */
  2268. static int
  2269. connection_bucket_should_increase(int bucket, or_connection_t *conn)
  2270. {
  2271. tor_assert(conn);
  2272. if (conn->_base.state != OR_CONN_STATE_OPEN)
  2273. return 0; /* only open connections play the rate limiting game */
  2274. if (bucket >= conn->bandwidthburst)
  2275. return 0;
  2276. return 1;
  2277. }
  2278. #else
  2279. static void
  2280. connection_buckets_decrement(connection_t *conn, time_t now,
  2281. size_t num_read, size_t num_written)
  2282. {
  2283. (void) conn;
  2284. (void) now;
  2285. (void) num_read;
  2286. (void) num_written;
  2287. /* Libevent does this for us. */
  2288. }
  2289. void
  2290. connection_bucket_refill(int seconds_elapsed, time_t now)
  2291. {
  2292. (void) seconds_elapsed;
  2293. (void) now;
  2294. /* Libevent does this for us. */
  2295. }
  2296. void
  2297. connection_bucket_init(void)
  2298. {
  2299. const or_options_t *options = get_options();
  2300. const struct timeval *tick = tor_libevent_get_one_tick_timeout();
  2301. struct ev_token_bucket_cfg *bucket_cfg;
  2302. uint64_t rate, burst;
  2303. if (options->RelayBandwidthRate) {
  2304. rate = options->RelayBandwidthRate;
  2305. burst = options->RelayBandwidthBurst;
  2306. } else {
  2307. rate = options->BandwidthRate;
  2308. burst = options->BandwidthBurst;
  2309. }
  2310. /* This can't overflow, since TokenBucketRefillInterval <= 1000,
  2311. * and rate started out less than INT32_MAX. */
  2312. rate = (rate * options->TokenBucketRefillInterval) / 1000;
  2313. bucket_cfg = ev_token_bucket_cfg_new((uint32_t)rate, (uint32_t)burst,
  2314. (uint32_t)rate, (uint32_t)burst,
  2315. tick);
  2316. if (!global_rate_limit) {
  2317. global_rate_limit =
  2318. bufferevent_rate_limit_group_new(tor_libevent_get_base(), bucket_cfg);
  2319. } else {
  2320. bufferevent_rate_limit_group_set_cfg(global_rate_limit, bucket_cfg);
  2321. }
  2322. ev_token_bucket_cfg_free(bucket_cfg);
  2323. }
  2324. void
  2325. connection_get_rate_limit_totals(uint64_t *read_out, uint64_t *written_out)
  2326. {
  2327. if (global_rate_limit == NULL) {
  2328. *read_out = *written_out = 0;
  2329. } else {
  2330. bufferevent_rate_limit_group_get_totals(
  2331. global_rate_limit, read_out, written_out);
  2332. }
  2333. }
  2334. /** Perform whatever operations are needed on <b>conn</b> to enable
  2335. * rate-limiting. */
  2336. void
  2337. connection_enable_rate_limiting(connection_t *conn)
  2338. {
  2339. if (conn->bufev) {
  2340. if (!global_rate_limit)
  2341. connection_bucket_init();
  2342. tor_add_bufferevent_to_rate_limit_group(conn->bufev, global_rate_limit);
  2343. }
  2344. }
  2345. static void
  2346. connection_consider_empty_write_buckets(connection_t *conn)
  2347. {
  2348. (void) conn;
  2349. }
  2350. static void
  2351. connection_consider_empty_read_buckets(connection_t *conn)
  2352. {
  2353. (void) conn;
  2354. }
  2355. #endif
  2356. /** Read bytes from conn-\>s and process them.
  2357. *
  2358. * It calls connection_read_to_buf() to bring in any new bytes,
  2359. * and then calls connection_process_inbuf() to process them.
  2360. *
  2361. * Mark the connection and return -1 if you want to close it, else
  2362. * return 0.
  2363. */
  2364. static int
  2365. connection_handle_read_impl(connection_t *conn)
  2366. {
  2367. ssize_t max_to_read=-1, try_to_read;
  2368. size_t before, n_read = 0;
  2369. int socket_error = 0;
  2370. if (conn->marked_for_close)
  2371. return 0; /* do nothing */
  2372. conn->timestamp_lastread = approx_time();
  2373. switch (conn->type) {
  2374. case CONN_TYPE_OR_LISTENER:
  2375. return connection_handle_listener_read(conn, CONN_TYPE_OR);
  2376. case CONN_TYPE_AP_LISTENER:
  2377. case CONN_TYPE_AP_TRANS_LISTENER:
  2378. case CONN_TYPE_AP_NATD_LISTENER:
  2379. return connection_handle_listener_read(conn, CONN_TYPE_AP);
  2380. case CONN_TYPE_DIR_LISTENER:
  2381. return connection_handle_listener_read(conn, CONN_TYPE_DIR);
  2382. case CONN_TYPE_CONTROL_LISTENER:
  2383. return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
  2384. case CONN_TYPE_AP_DNS_LISTENER:
  2385. /* This should never happen; eventdns.c handles the reads here. */
  2386. tor_fragile_assert();
  2387. return 0;
  2388. }
  2389. loop_again:
  2390. try_to_read = max_to_read;
  2391. tor_assert(!conn->marked_for_close);
  2392. before = buf_datalen(conn->inbuf);
  2393. if (connection_read_to_buf(conn, &max_to_read, &socket_error) < 0) {
  2394. /* There's a read error; kill the connection.*/
  2395. if (conn->type == CONN_TYPE_OR &&
  2396. conn->state == OR_CONN_STATE_CONNECTING) {
  2397. connection_or_connect_failed(TO_OR_CONN(conn),
  2398. errno_to_orconn_end_reason(socket_error),
  2399. tor_socket_strerror(socket_error));
  2400. }
  2401. if (CONN_IS_EDGE(conn)) {
  2402. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  2403. connection_edge_end_errno(edge_conn);
  2404. if (conn->type == CONN_TYPE_AP && TO_ENTRY_CONN(conn)->socks_request) {
  2405. /* broken, don't send a socks reply back */
  2406. TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
  2407. }
  2408. }
  2409. connection_close_immediate(conn); /* Don't flush; connection is dead. */
  2410. connection_mark_for_close(conn);
  2411. return -1;
  2412. }
  2413. n_read += buf_datalen(conn->inbuf) - before;
  2414. if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
  2415. /* instruct it not to try to package partial cells. */
  2416. if (connection_process_inbuf(conn, 0) < 0) {
  2417. return -1;
  2418. }
  2419. if (!conn->marked_for_close &&
  2420. connection_is_reading(conn) &&
  2421. !conn->inbuf_reached_eof &&
  2422. max_to_read > 0)
  2423. goto loop_again; /* try reading again, in case more is here now */
  2424. }
  2425. /* one last try, packaging partial cells and all. */
  2426. if (!conn->marked_for_close &&
  2427. connection_process_inbuf(conn, 1) < 0) {
  2428. return -1;
  2429. }
  2430. if (conn->linked_conn) {
  2431. /* The other side's handle_write() will never actually get called, so
  2432. * we need to invoke the appropriate callbacks ourself. */
  2433. connection_t *linked = conn->linked_conn;
  2434. if (n_read) {
  2435. /* Probably a no-op, since linked conns typically don't count for
  2436. * bandwidth rate limiting. But do it anyway so we can keep stats
  2437. * accurately. Note that since we read the bytes from conn, and
  2438. * we're writing the bytes onto the linked connection, we count
  2439. * these as <i>written</i> bytes. */
  2440. connection_buckets_decrement(linked, approx_time(), 0, n_read);
  2441. if (connection_flushed_some(linked) < 0)
  2442. connection_mark_for_close(linked);
  2443. if (!connection_wants_to_flush(linked))
  2444. connection_finished_flushing(linked);
  2445. }
  2446. if (!buf_datalen(linked->outbuf) && conn->active_on_link)
  2447. connection_stop_reading_from_linked_conn(conn);
  2448. }
  2449. /* If we hit the EOF, call connection_reached_eof(). */
  2450. if (!conn->marked_for_close &&
  2451. conn->inbuf_reached_eof &&
  2452. connection_reached_eof(conn) < 0) {
  2453. return -1;
  2454. }
  2455. return 0;
  2456. }
  2457. /* DOCDOC connection_handle_read */
  2458. int
  2459. connection_handle_read(connection_t *conn)
  2460. {
  2461. int res;
  2462. tor_gettimeofday_cache_clear();
  2463. res = connection_handle_read_impl(conn);
  2464. return res;
  2465. }
  2466. /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
  2467. * either directly or via TLS. Reduce the token buckets by the number of bytes
  2468. * read.
  2469. *
  2470. * If *max_to_read is -1, then decide it ourselves, else go with the
  2471. * value passed to us. When returning, if it's changed, subtract the
  2472. * number of bytes we read from *max_to_read.
  2473. *
  2474. * Return -1 if we want to break conn, else return 0.
  2475. */
  2476. static int
  2477. connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
  2478. int *socket_error)
  2479. {
  2480. int result;
  2481. ssize_t at_most = *max_to_read;
  2482. size_t slack_in_buf, more_to_read;
  2483. size_t n_read = 0, n_written = 0;
  2484. if (at_most == -1) { /* we need to initialize it */
  2485. /* how many bytes are we allowed to read? */
  2486. at_most = connection_bucket_read_limit(conn, approx_time());
  2487. }
  2488. slack_in_buf = buf_slack(conn->inbuf);
  2489. again:
  2490. if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
  2491. more_to_read = at_most - slack_in_buf;
  2492. at_most = slack_in_buf;
  2493. } else {
  2494. more_to_read = 0;
  2495. }
  2496. if (connection_speaks_cells(conn) &&
  2497. conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
  2498. int pending;
  2499. or_connection_t *or_conn = TO_OR_CONN(conn);
  2500. size_t initial_size;
  2501. if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
  2502. conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
  2503. /* continue handshaking even if global token bucket is empty */
  2504. return connection_tls_continue_handshake(or_conn);
  2505. }
  2506. log_debug(LD_NET,
  2507. "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
  2508. " at_most %ld.",
  2509. (int)conn->s,(long)buf_datalen(conn->inbuf),
  2510. tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
  2511. initial_size = buf_datalen(conn->inbuf);
  2512. /* else open, or closing */
  2513. result = read_to_buf_tls(or_conn->tls, at_most, conn->inbuf);
  2514. if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
  2515. or_conn->tls_error = result;
  2516. else
  2517. or_conn->tls_error = 0;
  2518. switch (result) {
  2519. case TOR_TLS_CLOSE:
  2520. case TOR_TLS_ERROR_IO:
  2521. log_debug(LD_NET,"TLS connection closed %son read. Closing. "
  2522. "(Nickname %s, address %s)",
  2523. result == TOR_TLS_CLOSE ? "cleanly " : "",
  2524. or_conn->nickname ? or_conn->nickname : "not set",
  2525. conn->address);
  2526. return result;
  2527. CASE_TOR_TLS_ERROR_ANY_NONIO:
  2528. log_debug(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
  2529. tor_tls_err_to_string(result),
  2530. or_conn->nickname ? or_conn->nickname : "not set",
  2531. conn->address);
  2532. return result;
  2533. case TOR_TLS_WANTWRITE:
  2534. connection_start_writing(conn);
  2535. return 0;
  2536. case TOR_TLS_WANTREAD: /* we're already reading */
  2537. case TOR_TLS_DONE: /* no data read, so nothing to process */
  2538. result = 0;
  2539. break; /* so we call bucket_decrement below */
  2540. default:
  2541. break;
  2542. }
  2543. pending = tor_tls_get_pending_bytes(or_conn->tls);
  2544. if (pending) {
  2545. /* If we have any pending bytes, we read them now. This *can*
  2546. * take us over our read allotment, but really we shouldn't be
  2547. * believing that SSL bytes are the same as TCP bytes anyway. */
  2548. int r2 = read_to_buf_tls(or_conn->tls, pending, conn->inbuf);
  2549. if (r2<0) {
  2550. log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
  2551. return -1;
  2552. }
  2553. }
  2554. result = (int)(buf_datalen(conn->inbuf)-initial_size);
  2555. tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
  2556. log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
  2557. result, (long)n_read, (long)n_written);
  2558. } else if (conn->linked) {
  2559. if (conn->linked_conn) {
  2560. result = move_buf_to_buf(conn->inbuf, conn->linked_conn->outbuf,
  2561. &conn->linked_conn->outbuf_flushlen);
  2562. } else {
  2563. result = 0;
  2564. }
  2565. //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
  2566. /* If the other side has disappeared, or if it's been marked for close and
  2567. * we flushed its outbuf, then we should set our inbuf_reached_eof. */
  2568. if (!conn->linked_conn ||
  2569. (conn->linked_conn->marked_for_close &&
  2570. buf_datalen(conn->linked_conn->outbuf) == 0))
  2571. conn->inbuf_reached_eof = 1;
  2572. n_read = (size_t) result;
  2573. } else {
  2574. /* !connection_speaks_cells, !conn->linked_conn. */
  2575. int reached_eof = 0;
  2576. CONN_LOG_PROTECT(conn,
  2577. result = read_to_buf(conn->s, at_most, conn->inbuf, &reached_eof,
  2578. socket_error));
  2579. if (reached_eof)
  2580. conn->inbuf_reached_eof = 1;
  2581. // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
  2582. if (result < 0)
  2583. return -1;
  2584. n_read = (size_t) result;
  2585. }
  2586. if (n_read > 0) {
  2587. /* change *max_to_read */
  2588. *max_to_read = at_most - n_read;
  2589. /* Update edge_conn->n_read */
  2590. if (conn->type == CONN_TYPE_AP) {
  2591. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  2592. /* Check for overflow: */
  2593. if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_read > n_read))
  2594. edge_conn->n_read += (int)n_read;
  2595. else
  2596. edge_conn->n_read = UINT32_MAX;
  2597. }
  2598. }
  2599. connection_buckets_decrement(conn, approx_time(), n_read, n_written);
  2600. if (more_to_read && result == at_most) {
  2601. slack_in_buf = buf_slack(conn->inbuf);
  2602. at_most = more_to_read;
  2603. goto again;
  2604. }
  2605. /* Call even if result is 0, since the global read bucket may
  2606. * have reached 0 on a different conn, and this guy needs to
  2607. * know to stop reading. */
  2608. connection_consider_empty_read_buckets(conn);
  2609. if (n_written > 0 && connection_is_writing(conn))
  2610. connection_consider_empty_write_buckets(conn);
  2611. return 0;
  2612. }
  2613. #ifdef USE_BUFFEREVENTS
  2614. /* XXXX These generic versions could be simplified by making them
  2615. type-specific */
  2616. /** Callback: Invoked whenever bytes are added to or drained from an input
  2617. * evbuffer. Used to track the number of bytes read. */
  2618. static void
  2619. evbuffer_inbuf_callback(struct evbuffer *buf,
  2620. const struct evbuffer_cb_info *info, void *arg)
  2621. {
  2622. connection_t *conn = arg;
  2623. (void) buf;
  2624. /* XXXX These need to get real counts on the non-nested TLS case. - NM */
  2625. if (info->n_added) {
  2626. time_t now = approx_time();
  2627. conn->timestamp_lastread = now;
  2628. record_num_bytes_transferred(conn, now, info->n_added, 0);
  2629. connection_consider_empty_read_buckets(conn);
  2630. if (conn->type == CONN_TYPE_AP) {
  2631. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  2632. /*XXXX024 check for overflow*/
  2633. edge_conn->n_read += (int)info->n_added;
  2634. }
  2635. }
  2636. }
  2637. /** Callback: Invoked whenever bytes are added to or drained from an output
  2638. * evbuffer. Used to track the number of bytes written. */
  2639. static void
  2640. evbuffer_outbuf_callback(struct evbuffer *buf,
  2641. const struct evbuffer_cb_info *info, void *arg)
  2642. {
  2643. connection_t *conn = arg;
  2644. (void)buf;
  2645. if (info->n_deleted) {
  2646. time_t now = approx_time();
  2647. conn->timestamp_lastwritten = now;
  2648. record_num_bytes_transferred(conn, now, 0, info->n_deleted);
  2649. connection_consider_empty_write_buckets(conn);
  2650. if (conn->type == CONN_TYPE_AP) {
  2651. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  2652. /*XXXX024 check for overflow*/
  2653. edge_conn->n_written += (int)info->n_deleted;
  2654. }
  2655. }
  2656. }
  2657. /** Callback: invoked whenever a bufferevent has read data. */
  2658. void
  2659. connection_handle_read_cb(struct bufferevent *bufev, void *arg)
  2660. {
  2661. connection_t *conn = arg;
  2662. (void) bufev;
  2663. if (!conn->marked_for_close) {
  2664. if (connection_process_inbuf(conn, 1)<0) /* XXXX Always 1? */
  2665. if (!conn->marked_for_close)
  2666. connection_mark_for_close(conn);
  2667. }
  2668. }
  2669. /** Callback: invoked whenever a bufferevent has written data. */
  2670. void
  2671. connection_handle_write_cb(struct bufferevent *bufev, void *arg)
  2672. {
  2673. connection_t *conn = arg;
  2674. struct evbuffer *output;
  2675. if (connection_flushed_some(conn)<0) {
  2676. if (!conn->marked_for_close)
  2677. connection_mark_for_close(conn);
  2678. return;
  2679. }
  2680. output = bufferevent_get_output(bufev);
  2681. if (!evbuffer_get_length(output)) {
  2682. connection_finished_flushing(conn);
  2683. if (conn->marked_for_close && conn->hold_open_until_flushed) {
  2684. conn->hold_open_until_flushed = 0;
  2685. if (conn->linked) {
  2686. /* send eof */
  2687. bufferevent_flush(conn->bufev, EV_WRITE, BEV_FINISHED);
  2688. }
  2689. }
  2690. }
  2691. }
  2692. /** Callback: invoked whenever a bufferevent has had an event (like a
  2693. * connection, or an eof, or an error) occur. */
  2694. void
  2695. connection_handle_event_cb(struct bufferevent *bufev, short event, void *arg)
  2696. {
  2697. connection_t *conn = arg;
  2698. (void) bufev;
  2699. if (conn->marked_for_close)
  2700. return;
  2701. if (event & BEV_EVENT_CONNECTED) {
  2702. tor_assert(connection_state_is_connecting(conn));
  2703. if (connection_finished_connecting(conn)<0)
  2704. return;
  2705. }
  2706. if (event & BEV_EVENT_EOF) {
  2707. if (!conn->marked_for_close) {
  2708. conn->inbuf_reached_eof = 1;
  2709. if (connection_reached_eof(conn)<0)
  2710. return;
  2711. }
  2712. }
  2713. if (event & BEV_EVENT_ERROR) {
  2714. int socket_error = evutil_socket_geterror(conn->s);
  2715. if (conn->type == CONN_TYPE_OR &&
  2716. conn->state == OR_CONN_STATE_CONNECTING) {
  2717. connection_or_connect_failed(TO_OR_CONN(conn),
  2718. errno_to_orconn_end_reason(socket_error),
  2719. tor_socket_strerror(socket_error));
  2720. } else if (CONN_IS_EDGE(conn)) {
  2721. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  2722. if (!edge_conn->edge_has_sent_end)
  2723. connection_edge_end_errno(edge_conn);
  2724. if (conn->type == CONN_TYPE_AP && TO_ENTRY_CONN(conn)->socks_request) {
  2725. /* broken, don't send a socks reply back */
  2726. TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
  2727. }
  2728. }
  2729. connection_close_immediate(conn); /* Connection is dead. */
  2730. if (!conn->marked_for_close)
  2731. connection_mark_for_close(conn);
  2732. }
  2733. }
  2734. /** Set up the generic callbacks for the bufferevent on <b>conn</b>. */
  2735. void
  2736. connection_configure_bufferevent_callbacks(connection_t *conn)
  2737. {
  2738. struct bufferevent *bufev;
  2739. struct evbuffer *input, *output;
  2740. tor_assert(conn->bufev);
  2741. bufev = conn->bufev;
  2742. bufferevent_setcb(bufev,
  2743. connection_handle_read_cb,
  2744. connection_handle_write_cb,
  2745. connection_handle_event_cb,
  2746. conn);
  2747. /* Set a fairly high write low-watermark so that we get the write callback
  2748. called whenever data is written to bring us under 128K. Leave the
  2749. high-watermark at 0.
  2750. */
  2751. bufferevent_setwatermark(bufev, EV_WRITE, 128*1024, 0);
  2752. input = bufferevent_get_input(bufev);
  2753. output = bufferevent_get_output(bufev);
  2754. evbuffer_add_cb(input, evbuffer_inbuf_callback, conn);
  2755. evbuffer_add_cb(output, evbuffer_outbuf_callback, conn);
  2756. }
  2757. #endif
  2758. /** A pass-through to fetch_from_buf. */
  2759. int
  2760. connection_fetch_from_buf(char *string, size_t len, connection_t *conn)
  2761. {
  2762. IF_HAS_BUFFEREVENT(conn, {
  2763. /* XXX overflow -seb */
  2764. return (int)bufferevent_read(conn->bufev, string, len);
  2765. }) ELSE_IF_NO_BUFFEREVENT {
  2766. return fetch_from_buf(string, len, conn->inbuf);
  2767. }
  2768. }
  2769. /** As fetch_from_buf_line(), but read from a connection's input buffer. */
  2770. int
  2771. connection_fetch_from_buf_line(connection_t *conn, char *data,
  2772. size_t *data_len)
  2773. {
  2774. IF_HAS_BUFFEREVENT(conn, {
  2775. int r;
  2776. size_t eol_len=0;
  2777. struct evbuffer *input = bufferevent_get_input(conn->bufev);
  2778. struct evbuffer_ptr ptr =
  2779. evbuffer_search_eol(input, NULL, &eol_len, EVBUFFER_EOL_LF);
  2780. if (ptr.pos == -1)
  2781. return 0; /* No EOL found. */
  2782. if ((size_t)ptr.pos+eol_len >= *data_len) {
  2783. return -1; /* Too long */
  2784. }
  2785. *data_len = ptr.pos+eol_len;
  2786. r = evbuffer_remove(input, data, ptr.pos+eol_len);
  2787. tor_assert(r >= 0);
  2788. data[ptr.pos+eol_len] = '\0';
  2789. return 1;
  2790. }) ELSE_IF_NO_BUFFEREVENT {
  2791. return fetch_from_buf_line(conn->inbuf, data, data_len);
  2792. }
  2793. }
  2794. /** As fetch_from_buf_http, but fetches from a conncetion's input buffer_t or
  2795. * its bufferevent as appropriate. */
  2796. int
  2797. connection_fetch_from_buf_http(connection_t *conn,
  2798. char **headers_out, size_t max_headerlen,
  2799. char **body_out, size_t *body_used,
  2800. size_t max_bodylen, int force_complete)
  2801. {
  2802. IF_HAS_BUFFEREVENT(conn, {
  2803. struct evbuffer *input = bufferevent_get_input(conn->bufev);
  2804. return fetch_from_evbuffer_http(input, headers_out, max_headerlen,
  2805. body_out, body_used, max_bodylen, force_complete);
  2806. }) ELSE_IF_NO_BUFFEREVENT {
  2807. return fetch_from_buf_http(conn->inbuf, headers_out, max_headerlen,
  2808. body_out, body_used, max_bodylen, force_complete);
  2809. }
  2810. }
  2811. /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
  2812. * from its outbuf. */
  2813. int
  2814. connection_wants_to_flush(connection_t *conn)
  2815. {
  2816. return conn->outbuf_flushlen > 0;
  2817. }
  2818. /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
  2819. * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
  2820. * connection_edge_consider_sending_sendme().
  2821. */
  2822. int
  2823. connection_outbuf_too_full(connection_t *conn)
  2824. {
  2825. return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
  2826. }
  2827. /** Try to flush more bytes onto <b>conn</b>-\>s.
  2828. *
  2829. * This function gets called either from conn_write() in main.c
  2830. * when poll() has declared that conn wants to write, or below
  2831. * from connection_write_to_buf() when an entire TLS record is ready.
  2832. *
  2833. * Update <b>conn</b>-\>timestamp_lastwritten to now, and call flush_buf
  2834. * or flush_buf_tls appropriately. If it succeeds and there are no more
  2835. * more bytes on <b>conn</b>-\>outbuf, then call connection_finished_flushing
  2836. * on it too.
  2837. *
  2838. * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
  2839. * limits. (Used for flushing messages to controller connections on fatal
  2840. * errors.)
  2841. *
  2842. * Mark the connection and return -1 if you want to close it, else
  2843. * return 0.
  2844. */
  2845. static int
  2846. connection_handle_write_impl(connection_t *conn, int force)
  2847. {
  2848. int e;
  2849. socklen_t len=(socklen_t)sizeof(e);
  2850. int result;
  2851. ssize_t max_to_write;
  2852. time_t now = approx_time();
  2853. size_t n_read = 0, n_written = 0;
  2854. tor_assert(!connection_is_listener(conn));
  2855. if (conn->marked_for_close || !SOCKET_OK(conn->s))
  2856. return 0; /* do nothing */
  2857. if (conn->in_flushed_some) {
  2858. log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some");
  2859. return 0;
  2860. }
  2861. conn->timestamp_lastwritten = now;
  2862. /* Sometimes, "writable" means "connected". */
  2863. if (connection_state_is_connecting(conn)) {
  2864. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
  2865. log_warn(LD_BUG, "getsockopt() syscall failed");
  2866. if (CONN_IS_EDGE(conn))
  2867. connection_edge_end_errno(TO_EDGE_CONN(conn));
  2868. connection_mark_for_close(conn);
  2869. return -1;
  2870. }
  2871. if (e) {
  2872. /* some sort of error, but maybe just inprogress still */
  2873. if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
  2874. log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
  2875. tor_socket_strerror(e));
  2876. if (CONN_IS_EDGE(conn))
  2877. connection_edge_end_errno(TO_EDGE_CONN(conn));
  2878. if (conn->type == CONN_TYPE_OR)
  2879. connection_or_connect_failed(TO_OR_CONN(conn),
  2880. errno_to_orconn_end_reason(e),
  2881. tor_socket_strerror(e));
  2882. connection_close_immediate(conn);
  2883. connection_mark_for_close(conn);
  2884. return -1;
  2885. } else {
  2886. return 0; /* no change, see if next time is better */
  2887. }
  2888. }
  2889. /* The connection is successful. */
  2890. if (connection_finished_connecting(conn)<0)
  2891. return -1;
  2892. }
  2893. max_to_write = force ? (ssize_t)conn->outbuf_flushlen
  2894. : connection_bucket_write_limit(conn, now);
  2895. if (connection_speaks_cells(conn) &&
  2896. conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
  2897. or_connection_t *or_conn = TO_OR_CONN(conn);
  2898. if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
  2899. conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
  2900. connection_stop_writing(conn);
  2901. if (connection_tls_continue_handshake(or_conn) < 0) {
  2902. /* Don't flush; connection is dead. */
  2903. connection_close_immediate(conn);
  2904. connection_mark_for_close(conn);
  2905. return -1;
  2906. }
  2907. return 0;
  2908. } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
  2909. return connection_handle_read(conn);
  2910. }
  2911. /* else open, or closing */
  2912. result = flush_buf_tls(or_conn->tls, conn->outbuf,
  2913. max_to_write, &conn->outbuf_flushlen);
  2914. /* If we just flushed the last bytes, check if this tunneled dir
  2915. * request is done. */
  2916. /* XXXX move this to flushed_some or finished_flushing -NM */
  2917. if (buf_datalen(conn->outbuf) == 0 && conn->dirreq_id)
  2918. geoip_change_dirreq_state(conn->dirreq_id, DIRREQ_TUNNELED,
  2919. DIRREQ_OR_CONN_BUFFER_FLUSHED);
  2920. switch (result) {
  2921. CASE_TOR_TLS_ERROR_ANY:
  2922. case TOR_TLS_CLOSE:
  2923. log_info(LD_NET,result!=TOR_TLS_CLOSE?
  2924. "tls error. breaking.":"TLS connection closed on flush");
  2925. /* Don't flush; connection is dead. */
  2926. connection_close_immediate(conn);
  2927. connection_mark_for_close(conn);
  2928. return -1;
  2929. case TOR_TLS_WANTWRITE:
  2930. log_debug(LD_NET,"wanted write.");
  2931. /* we're already writing */
  2932. return 0;
  2933. case TOR_TLS_WANTREAD:
  2934. /* Make sure to avoid a loop if the receive buckets are empty. */
  2935. log_debug(LD_NET,"wanted read.");
  2936. if (!connection_is_reading(conn)) {
  2937. connection_stop_writing(conn);
  2938. conn->write_blocked_on_bw = 1;
  2939. /* we'll start reading again when we get more tokens in our
  2940. * read bucket; then we'll start writing again too.
  2941. */
  2942. }
  2943. /* else no problem, we're already reading */
  2944. return 0;
  2945. /* case TOR_TLS_DONE:
  2946. * for TOR_TLS_DONE, fall through to check if the flushlen
  2947. * is empty, so we can stop writing.
  2948. */
  2949. }
  2950. tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
  2951. log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
  2952. result, (long)n_read, (long)n_written);
  2953. } else {
  2954. CONN_LOG_PROTECT(conn,
  2955. result = flush_buf(conn->s, conn->outbuf,
  2956. max_to_write, &conn->outbuf_flushlen));
  2957. if (result < 0) {
  2958. if (CONN_IS_EDGE(conn))
  2959. connection_edge_end_errno(TO_EDGE_CONN(conn));
  2960. connection_close_immediate(conn); /* Don't flush; connection is dead. */
  2961. connection_mark_for_close(conn);
  2962. return -1;
  2963. }
  2964. n_written = (size_t) result;
  2965. }
  2966. if (n_written && conn->type == CONN_TYPE_AP) {
  2967. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  2968. /* Check for overflow: */
  2969. if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_written > n_written))
  2970. edge_conn->n_written += (int)n_written;
  2971. else
  2972. edge_conn->n_written = UINT32_MAX;
  2973. }
  2974. connection_buckets_decrement(conn, approx_time(), n_read, n_written);
  2975. if (result > 0) {
  2976. /* If we wrote any bytes from our buffer, then call the appropriate
  2977. * functions. */
  2978. if (connection_flushed_some(conn) < 0)
  2979. connection_mark_for_close(conn);
  2980. }
  2981. if (!connection_wants_to_flush(conn)) { /* it's done flushing */
  2982. if (connection_finished_flushing(conn) < 0) {
  2983. /* already marked */
  2984. return -1;
  2985. }
  2986. return 0;
  2987. }
  2988. /* Call even if result is 0, since the global write bucket may
  2989. * have reached 0 on a different conn, and this guy needs to
  2990. * know to stop writing. */
  2991. connection_consider_empty_write_buckets(conn);
  2992. if (n_read > 0 && connection_is_reading(conn))
  2993. connection_consider_empty_read_buckets(conn);
  2994. return 0;
  2995. }
  2996. /* DOCDOC connection_handle_write */
  2997. int
  2998. connection_handle_write(connection_t *conn, int force)
  2999. {
  3000. int res;
  3001. tor_gettimeofday_cache_clear();
  3002. res = connection_handle_write_impl(conn, force);
  3003. return res;
  3004. }
  3005. /**
  3006. * Try to flush data that's waiting for a write on <b>conn</b>. Return
  3007. * -1 on failure, 0 on success.
  3008. *
  3009. * Don't use this function for regular writing; the buffers/bufferevents
  3010. * system should be good enough at scheduling writes there. Instead, this
  3011. * function is for cases when we're about to exit or something and we want
  3012. * to report it right away.
  3013. */
  3014. int
  3015. connection_flush(connection_t *conn)
  3016. {
  3017. IF_HAS_BUFFEREVENT(conn, {
  3018. int r = bufferevent_flush(conn->bufev, EV_WRITE, BEV_FLUSH);
  3019. return (r < 0) ? -1 : 0;
  3020. });
  3021. return connection_handle_write(conn, 1);
  3022. }
  3023. /** OpenSSL TLS record size is 16383; this is close. The goal here is to
  3024. * push data out as soon as we know there's enough for a TLS record, so
  3025. * during periods of high load we won't read entire megabytes from
  3026. * input before pushing any data out. It also has the feature of not
  3027. * growing huge outbufs unless something is slow. */
  3028. #define MIN_TLS_FLUSHLEN 15872
  3029. /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
  3030. * outbuf, and ask it to start writing.
  3031. *
  3032. * If <b>zlib</b> is nonzero, this is a directory connection that should get
  3033. * its contents compressed or decompressed as they're written. If zlib is
  3034. * negative, this is the last data to be compressed, and the connection's zlib
  3035. * state should be flushed.
  3036. *
  3037. * If it's an OR conn and an entire TLS record is ready, then try to
  3038. * flush the record now. Similarly, if it's a local control connection
  3039. * and a 64k chunk is ready, try to flush it all, so we don't end up with
  3040. * many megabytes of controller info queued at once.
  3041. */
  3042. void
  3043. _connection_write_to_buf_impl(const char *string, size_t len,
  3044. connection_t *conn, int zlib)
  3045. {
  3046. /* XXXX This function really needs to return -1 on failure. */
  3047. int r;
  3048. size_t old_datalen;
  3049. if (!len && !(zlib<0))
  3050. return;
  3051. /* if it's marked for close, only allow write if we mean to flush it */
  3052. if (conn->marked_for_close && !conn->hold_open_until_flushed)
  3053. return;
  3054. IF_HAS_BUFFEREVENT(conn, {
  3055. if (zlib) {
  3056. int done = zlib < 0;
  3057. r = write_to_evbuffer_zlib(bufferevent_get_output(conn->bufev),
  3058. TO_DIR_CONN(conn)->zlib_state,
  3059. string, len, done);
  3060. } else {
  3061. r = bufferevent_write(conn->bufev, string, len);
  3062. }
  3063. if (r < 0) {
  3064. /* XXXX mark for close? */
  3065. log_warn(LD_NET, "bufferevent_write failed! That shouldn't happen.");
  3066. }
  3067. return;
  3068. });
  3069. old_datalen = buf_datalen(conn->outbuf);
  3070. if (zlib) {
  3071. dir_connection_t *dir_conn = TO_DIR_CONN(conn);
  3072. int done = zlib < 0;
  3073. CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
  3074. dir_conn->zlib_state,
  3075. string, len, done));
  3076. } else {
  3077. CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
  3078. }
  3079. if (r < 0) {
  3080. if (CONN_IS_EDGE(conn)) {
  3081. /* if it failed, it means we have our package/delivery windows set
  3082. wrong compared to our max outbuf size. close the whole circuit. */
  3083. log_warn(LD_NET,
  3084. "write_to_buf failed. Closing circuit (fd %d).", (int)conn->s);
  3085. circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
  3086. END_CIRC_REASON_INTERNAL);
  3087. } else {
  3088. log_warn(LD_NET,
  3089. "write_to_buf failed. Closing connection (fd %d).",
  3090. (int)conn->s);
  3091. connection_mark_for_close(conn);
  3092. }
  3093. return;
  3094. }
  3095. /* If we receive optimistic data in the EXIT_CONN_STATE_RESOLVING
  3096. * state, we don't want to try to write it right away, since
  3097. * conn->write_event won't be set yet. Otherwise, write data from
  3098. * this conn as the socket is available. */
  3099. if (conn->write_event) {
  3100. connection_start_writing(conn);
  3101. }
  3102. if (zlib) {
  3103. conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
  3104. } else {
  3105. ssize_t extra = 0;
  3106. conn->outbuf_flushlen += len;
  3107. /* Should we try flushing the outbuf now? */
  3108. if (conn->in_flushed_some) {
  3109. /* Don't flush the outbuf when the reason we're writing more stuff is
  3110. * _because_ we flushed the outbuf. That's unfair. */
  3111. return;
  3112. }
  3113. if (conn->type == CONN_TYPE_OR &&
  3114. conn->outbuf_flushlen-len < MIN_TLS_FLUSHLEN &&
  3115. conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
  3116. /* We just pushed outbuf_flushlen to MIN_TLS_FLUSHLEN or above;
  3117. * we can send out a full TLS frame now if we like. */
  3118. extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
  3119. conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
  3120. } else if (conn->type == CONN_TYPE_CONTROL &&
  3121. !connection_is_rate_limited(conn) &&
  3122. conn->outbuf_flushlen-len < 1<<16 &&
  3123. conn->outbuf_flushlen >= 1<<16) {
  3124. /* just try to flush all of it */
  3125. } else
  3126. return; /* no need to try flushing */
  3127. if (connection_handle_write(conn, 0) < 0) {
  3128. if (!conn->marked_for_close) {
  3129. /* this connection is broken. remove it. */
  3130. log_warn(LD_BUG, "unhandled error on write for "
  3131. "conn (type %d, fd %d); removing",
  3132. conn->type, (int)conn->s);
  3133. tor_fragile_assert();
  3134. /* do a close-immediate here, so we don't try to flush */
  3135. connection_close_immediate(conn);
  3136. }
  3137. return;
  3138. }
  3139. if (extra) {
  3140. conn->outbuf_flushlen += extra;
  3141. connection_start_writing(conn);
  3142. }
  3143. }
  3144. }
  3145. /** Return a connection with given type, address, port, and purpose;
  3146. * or NULL if no such connection exists. */
  3147. connection_t *
  3148. connection_get_by_type_addr_port_purpose(int type,
  3149. const tor_addr_t *addr, uint16_t port,
  3150. int purpose)
  3151. {
  3152. smartlist_t *conns = get_connection_array();
  3153. SMARTLIST_FOREACH(conns, connection_t *, conn,
  3154. {
  3155. if (conn->type == type &&
  3156. tor_addr_eq(&conn->addr, addr) &&
  3157. conn->port == port &&
  3158. conn->purpose == purpose &&
  3159. !conn->marked_for_close)
  3160. return conn;
  3161. });
  3162. return NULL;
  3163. }
  3164. /** Return the stream with id <b>id</b> if it is not already marked for
  3165. * close.
  3166. */
  3167. connection_t *
  3168. connection_get_by_global_id(uint64_t id)
  3169. {
  3170. smartlist_t *conns = get_connection_array();
  3171. SMARTLIST_FOREACH(conns, connection_t *, conn,
  3172. {
  3173. if (conn->global_identifier == id)
  3174. return conn;
  3175. });
  3176. return NULL;
  3177. }
  3178. /** Return a connection of type <b>type</b> that is not marked for close.
  3179. */
  3180. connection_t *
  3181. connection_get_by_type(int type)
  3182. {
  3183. smartlist_t *conns = get_connection_array();
  3184. SMARTLIST_FOREACH(conns, connection_t *, conn,
  3185. {
  3186. if (conn->type == type && !conn->marked_for_close)
  3187. return conn;
  3188. });
  3189. return NULL;
  3190. }
  3191. /** Return a connection of type <b>type</b> that is in state <b>state</b>,
  3192. * and that is not marked for close.
  3193. */
  3194. connection_t *
  3195. connection_get_by_type_state(int type, int state)
  3196. {
  3197. smartlist_t *conns = get_connection_array();
  3198. SMARTLIST_FOREACH(conns, connection_t *, conn,
  3199. {
  3200. if (conn->type == type && conn->state == state && !conn->marked_for_close)
  3201. return conn;
  3202. });
  3203. return NULL;
  3204. }
  3205. /** Return a connection of type <b>type</b> that has rendquery equal
  3206. * to <b>rendquery</b>, and that is not marked for close. If state
  3207. * is non-zero, conn must be of that state too.
  3208. */
  3209. connection_t *
  3210. connection_get_by_type_state_rendquery(int type, int state,
  3211. const char *rendquery)
  3212. {
  3213. smartlist_t *conns = get_connection_array();
  3214. tor_assert(type == CONN_TYPE_DIR ||
  3215. type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
  3216. tor_assert(rendquery);
  3217. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  3218. if (conn->type == type &&
  3219. !conn->marked_for_close &&
  3220. (!state || state == conn->state)) {
  3221. if (type == CONN_TYPE_DIR &&
  3222. TO_DIR_CONN(conn)->rend_data &&
  3223. !rend_cmp_service_ids(rendquery,
  3224. TO_DIR_CONN(conn)->rend_data->onion_address))
  3225. return conn;
  3226. else if (CONN_IS_EDGE(conn) &&
  3227. TO_EDGE_CONN(conn)->rend_data &&
  3228. !rend_cmp_service_ids(rendquery,
  3229. TO_EDGE_CONN(conn)->rend_data->onion_address))
  3230. return conn;
  3231. }
  3232. } SMARTLIST_FOREACH_END(conn);
  3233. return NULL;
  3234. }
  3235. /** Return a directory connection (if any one exists) that is fetching
  3236. * the item described by <b>state</b>/<b>resource</b> */
  3237. dir_connection_t *
  3238. connection_dir_get_by_purpose_and_resource(int purpose,
  3239. const char *resource)
  3240. {
  3241. smartlist_t *conns = get_connection_array();
  3242. SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
  3243. dir_connection_t *dirconn;
  3244. if (conn->type != CONN_TYPE_DIR || conn->marked_for_close ||
  3245. conn->purpose != purpose)
  3246. continue;
  3247. dirconn = TO_DIR_CONN(conn);
  3248. if (dirconn->requested_resource == NULL) {
  3249. if (resource == NULL)
  3250. return dirconn;
  3251. } else if (resource) {
  3252. if (0 == strcmp(resource, dirconn->requested_resource))
  3253. return dirconn;
  3254. }
  3255. } SMARTLIST_FOREACH_END(conn);
  3256. return NULL;
  3257. }
  3258. /** Return an open, non-marked connection of a given type and purpose, or NULL
  3259. * if no such connection exists. */
  3260. connection_t *
  3261. connection_get_by_type_purpose(int type, int purpose)
  3262. {
  3263. smartlist_t *conns = get_connection_array();
  3264. SMARTLIST_FOREACH(conns, connection_t *, conn,
  3265. {
  3266. if (conn->type == type &&
  3267. !conn->marked_for_close &&
  3268. (purpose == conn->purpose))
  3269. return conn;
  3270. });
  3271. return NULL;
  3272. }
  3273. /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
  3274. int
  3275. connection_is_listener(connection_t *conn)
  3276. {
  3277. if (conn->type == CONN_TYPE_OR_LISTENER ||
  3278. conn->type == CONN_TYPE_AP_LISTENER ||
  3279. conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
  3280. conn->type == CONN_TYPE_AP_DNS_LISTENER ||
  3281. conn->type == CONN_TYPE_AP_NATD_LISTENER ||
  3282. conn->type == CONN_TYPE_DIR_LISTENER ||
  3283. conn->type == CONN_TYPE_CONTROL_LISTENER)
  3284. return 1;
  3285. return 0;
  3286. }
  3287. /** Return 1 if <b>conn</b> is in state "open" and is not marked
  3288. * for close, else return 0.
  3289. */
  3290. int
  3291. connection_state_is_open(connection_t *conn)
  3292. {
  3293. tor_assert(conn);
  3294. if (conn->marked_for_close)
  3295. return 0;
  3296. if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
  3297. (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
  3298. (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
  3299. (conn->type == CONN_TYPE_CONTROL &&
  3300. conn->state == CONTROL_CONN_STATE_OPEN))
  3301. return 1;
  3302. return 0;
  3303. }
  3304. /** Return 1 if conn is in 'connecting' state, else return 0. */
  3305. int
  3306. connection_state_is_connecting(connection_t *conn)
  3307. {
  3308. tor_assert(conn);
  3309. if (conn->marked_for_close)
  3310. return 0;
  3311. switch (conn->type)
  3312. {
  3313. case CONN_TYPE_OR:
  3314. return conn->state == OR_CONN_STATE_CONNECTING;
  3315. case CONN_TYPE_EXIT:
  3316. return conn->state == EXIT_CONN_STATE_CONNECTING;
  3317. case CONN_TYPE_DIR:
  3318. return conn->state == DIR_CONN_STATE_CONNECTING;
  3319. }
  3320. return 0;
  3321. }
  3322. /** Allocates a base64'ed authenticator for use in http or https
  3323. * auth, based on the input string <b>authenticator</b>. Returns it
  3324. * if success, else returns NULL. */
  3325. char *
  3326. alloc_http_authenticator(const char *authenticator)
  3327. {
  3328. /* an authenticator in Basic authentication
  3329. * is just the string "username:password" */
  3330. const size_t authenticator_length = strlen(authenticator);
  3331. /* The base64_encode function needs a minimum buffer length
  3332. * of 66 bytes. */
  3333. const size_t base64_authenticator_length = (authenticator_length/48+1)*66;
  3334. char *base64_authenticator = tor_malloc(base64_authenticator_length);
  3335. if (base64_encode(base64_authenticator, base64_authenticator_length,
  3336. authenticator, authenticator_length) < 0) {
  3337. tor_free(base64_authenticator); /* free and set to null */
  3338. } else {
  3339. int i = 0, j = 0;
  3340. ssize_t len = strlen(base64_authenticator);
  3341. /* remove all newline occurrences within the string */
  3342. for (i=0; i < len; ++i) {
  3343. if ('\n' != base64_authenticator[i]) {
  3344. base64_authenticator[j] = base64_authenticator[i];
  3345. ++j;
  3346. }
  3347. }
  3348. base64_authenticator[j]='\0';
  3349. }
  3350. return base64_authenticator;
  3351. }
  3352. /** Given a socket handle, check whether the local address (sockname) of the
  3353. * socket is one that we've connected from before. If so, double-check
  3354. * whether our address has changed and we need to generate keys. If we do,
  3355. * call init_keys().
  3356. */
  3357. static void
  3358. client_check_address_changed(tor_socket_t sock)
  3359. {
  3360. struct sockaddr_storage out_sockaddr;
  3361. socklen_t out_addr_len = (socklen_t) sizeof(out_sockaddr);
  3362. tor_addr_t out_addr, iface_addr;
  3363. tor_addr_t **last_interface_ip_ptr;
  3364. sa_family_t family;
  3365. if (!outgoing_addrs)
  3366. outgoing_addrs = smartlist_new();
  3367. if (getsockname(sock, (struct sockaddr*)&out_sockaddr, &out_addr_len)<0) {
  3368. int e = tor_socket_errno(sock);
  3369. log_warn(LD_NET, "getsockname() to check for address change failed: %s",
  3370. tor_socket_strerror(e));
  3371. return;
  3372. }
  3373. tor_addr_from_sockaddr(&out_addr, (struct sockaddr*)&out_sockaddr, NULL);
  3374. family = tor_addr_family(&out_addr);
  3375. if (family == AF_INET)
  3376. last_interface_ip_ptr = &last_interface_ipv4;
  3377. else if (family == AF_INET6)
  3378. last_interface_ip_ptr = &last_interface_ipv6;
  3379. else
  3380. return;
  3381. if (! *last_interface_ip_ptr) {
  3382. tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
  3383. if (get_interface_address6(LOG_INFO, family, a)==0) {
  3384. *last_interface_ip_ptr = a;
  3385. } else {
  3386. tor_free(a);
  3387. }
  3388. }
  3389. /* If we've used this address previously, we're okay. */
  3390. SMARTLIST_FOREACH(outgoing_addrs, const tor_addr_t *, a_ptr,
  3391. if (tor_addr_eq(a_ptr, &out_addr))
  3392. return;
  3393. );
  3394. /* Uh-oh. We haven't connected from this address before. Has the interface
  3395. * address changed? */
  3396. if (get_interface_address6(LOG_INFO, family, &iface_addr)<0)
  3397. return;
  3398. if (tor_addr_eq(&iface_addr, *last_interface_ip_ptr)) {
  3399. /* Nope, it hasn't changed. Add this address to the list. */
  3400. smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
  3401. } else {
  3402. /* The interface changed. We're a client, so we need to regenerate our
  3403. * keys. First, reset the state. */
  3404. log(LOG_NOTICE, LD_NET, "Our IP address has changed. Rotating keys...");
  3405. tor_addr_copy(*last_interface_ip_ptr, &iface_addr);
  3406. SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t*, a_ptr, tor_free(a_ptr));
  3407. smartlist_clear(outgoing_addrs);
  3408. smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
  3409. /* Okay, now change our keys. */
  3410. ip_address_changed(1);
  3411. }
  3412. }
  3413. /** Some systems have limited system buffers for recv and xmit on
  3414. * sockets allocated in a virtual server or similar environment. For a Tor
  3415. * server this can produce the "Error creating network socket: No buffer
  3416. * space available" error once all available TCP buffer space is consumed.
  3417. * This method will attempt to constrain the buffers allocated for the socket
  3418. * to the desired size to stay below system TCP buffer limits.
  3419. */
  3420. static void
  3421. set_constrained_socket_buffers(tor_socket_t sock, int size)
  3422. {
  3423. void *sz = (void*)&size;
  3424. socklen_t sz_sz = (socklen_t) sizeof(size);
  3425. if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
  3426. int e = tor_socket_errno(sock);
  3427. log_warn(LD_NET, "setsockopt() to constrain send "
  3428. "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
  3429. }
  3430. if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
  3431. int e = tor_socket_errno(sock);
  3432. log_warn(LD_NET, "setsockopt() to constrain recv "
  3433. "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
  3434. }
  3435. }
  3436. /** Process new bytes that have arrived on conn-\>inbuf.
  3437. *
  3438. * This function just passes conn to the connection-specific
  3439. * connection_*_process_inbuf() function. It also passes in
  3440. * package_partial if wanted.
  3441. */
  3442. static int
  3443. connection_process_inbuf(connection_t *conn, int package_partial)
  3444. {
  3445. tor_assert(conn);
  3446. switch (conn->type) {
  3447. case CONN_TYPE_OR:
  3448. return connection_or_process_inbuf(TO_OR_CONN(conn));
  3449. case CONN_TYPE_EXIT:
  3450. case CONN_TYPE_AP:
  3451. return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
  3452. package_partial);
  3453. case CONN_TYPE_DIR:
  3454. return connection_dir_process_inbuf(TO_DIR_CONN(conn));
  3455. case CONN_TYPE_CPUWORKER:
  3456. return connection_cpu_process_inbuf(conn);
  3457. case CONN_TYPE_CONTROL:
  3458. return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
  3459. default:
  3460. log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
  3461. tor_fragile_assert();
  3462. return -1;
  3463. }
  3464. }
  3465. /** Called whenever we've written data on a connection. */
  3466. static int
  3467. connection_flushed_some(connection_t *conn)
  3468. {
  3469. int r = 0;
  3470. tor_assert(!conn->in_flushed_some);
  3471. conn->in_flushed_some = 1;
  3472. if (conn->type == CONN_TYPE_DIR &&
  3473. conn->state == DIR_CONN_STATE_SERVER_WRITING) {
  3474. r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
  3475. } else if (conn->type == CONN_TYPE_OR) {
  3476. r = connection_or_flushed_some(TO_OR_CONN(conn));
  3477. } else if (CONN_IS_EDGE(conn)) {
  3478. r = connection_edge_flushed_some(TO_EDGE_CONN(conn));
  3479. }
  3480. conn->in_flushed_some = 0;
  3481. return r;
  3482. }
  3483. /** We just finished flushing bytes from conn-\>outbuf, and there
  3484. * are no more bytes remaining.
  3485. *
  3486. * This function just passes conn to the connection-specific
  3487. * connection_*_finished_flushing() function.
  3488. */
  3489. static int
  3490. connection_finished_flushing(connection_t *conn)
  3491. {
  3492. tor_assert(conn);
  3493. /* If the connection is closed, don't try to do anything more here. */
  3494. if (CONN_IS_CLOSED(conn))
  3495. return 0;
  3496. // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
  3497. IF_HAS_NO_BUFFEREVENT(conn)
  3498. connection_stop_writing(conn);
  3499. switch (conn->type) {
  3500. case CONN_TYPE_OR:
  3501. return connection_or_finished_flushing(TO_OR_CONN(conn));
  3502. case CONN_TYPE_AP:
  3503. case CONN_TYPE_EXIT:
  3504. return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
  3505. case CONN_TYPE_DIR:
  3506. return connection_dir_finished_flushing(TO_DIR_CONN(conn));
  3507. case CONN_TYPE_CPUWORKER:
  3508. return connection_cpu_finished_flushing(conn);
  3509. case CONN_TYPE_CONTROL:
  3510. return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
  3511. default:
  3512. log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
  3513. tor_fragile_assert();
  3514. return -1;
  3515. }
  3516. }
  3517. /** Called when our attempt to connect() to another server has just
  3518. * succeeded.
  3519. *
  3520. * This function just passes conn to the connection-specific
  3521. * connection_*_finished_connecting() function.
  3522. */
  3523. static int
  3524. connection_finished_connecting(connection_t *conn)
  3525. {
  3526. tor_assert(conn);
  3527. if (!server_mode(get_options())) {
  3528. /* See whether getsockname() says our address changed. We need to do this
  3529. * now that the connection has finished, because getsockname() on Windows
  3530. * won't work until then. */
  3531. client_check_address_changed(conn->s);
  3532. }
  3533. switch (conn->type)
  3534. {
  3535. case CONN_TYPE_OR:
  3536. return connection_or_finished_connecting(TO_OR_CONN(conn));
  3537. case CONN_TYPE_EXIT:
  3538. return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
  3539. case CONN_TYPE_DIR:
  3540. return connection_dir_finished_connecting(TO_DIR_CONN(conn));
  3541. default:
  3542. log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
  3543. tor_fragile_assert();
  3544. return -1;
  3545. }
  3546. }
  3547. /** Callback: invoked when a connection reaches an EOF event. */
  3548. static int
  3549. connection_reached_eof(connection_t *conn)
  3550. {
  3551. switch (conn->type) {
  3552. case CONN_TYPE_OR:
  3553. return connection_or_reached_eof(TO_OR_CONN(conn));
  3554. case CONN_TYPE_AP:
  3555. case CONN_TYPE_EXIT:
  3556. return connection_edge_reached_eof(TO_EDGE_CONN(conn));
  3557. case CONN_TYPE_DIR:
  3558. return connection_dir_reached_eof(TO_DIR_CONN(conn));
  3559. case CONN_TYPE_CPUWORKER:
  3560. return connection_cpu_reached_eof(conn);
  3561. case CONN_TYPE_CONTROL:
  3562. return connection_control_reached_eof(TO_CONTROL_CONN(conn));
  3563. default:
  3564. log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
  3565. tor_fragile_assert();
  3566. return -1;
  3567. }
  3568. }
  3569. /** Log how many bytes are used by buffers of different kinds and sizes. */
  3570. void
  3571. connection_dump_buffer_mem_stats(int severity)
  3572. {
  3573. uint64_t used_by_type[_CONN_TYPE_MAX+1];
  3574. uint64_t alloc_by_type[_CONN_TYPE_MAX+1];
  3575. int n_conns_by_type[_CONN_TYPE_MAX+1];
  3576. uint64_t total_alloc = 0;
  3577. uint64_t total_used = 0;
  3578. int i;
  3579. smartlist_t *conns = get_connection_array();
  3580. memset(used_by_type, 0, sizeof(used_by_type));
  3581. memset(alloc_by_type, 0, sizeof(alloc_by_type));
  3582. memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
  3583. SMARTLIST_FOREACH(conns, connection_t *, c,
  3584. {
  3585. int tp = c->type;
  3586. ++n_conns_by_type[tp];
  3587. if (c->inbuf) {
  3588. used_by_type[tp] += buf_datalen(c->inbuf);
  3589. alloc_by_type[tp] += buf_allocation(c->inbuf);
  3590. }
  3591. if (c->outbuf) {
  3592. used_by_type[tp] += buf_datalen(c->outbuf);
  3593. alloc_by_type[tp] += buf_allocation(c->outbuf);
  3594. }
  3595. });
  3596. for (i=0; i <= _CONN_TYPE_MAX; ++i) {
  3597. total_used += used_by_type[i];
  3598. total_alloc += alloc_by_type[i];
  3599. }
  3600. log(severity, LD_GENERAL,
  3601. "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
  3602. smartlist_len(conns),
  3603. U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
  3604. for (i=_CONN_TYPE_MIN; i <= _CONN_TYPE_MAX; ++i) {
  3605. if (!n_conns_by_type[i])
  3606. continue;
  3607. log(severity, LD_GENERAL,
  3608. " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
  3609. n_conns_by_type[i], conn_type_to_string(i),
  3610. U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
  3611. }
  3612. }
  3613. /** Verify that connection <b>conn</b> has all of its invariants
  3614. * correct. Trigger an assert if anything is invalid.
  3615. */
  3616. void
  3617. assert_connection_ok(connection_t *conn, time_t now)
  3618. {
  3619. (void) now; /* XXXX unused. */
  3620. tor_assert(conn);
  3621. tor_assert(conn->type >= _CONN_TYPE_MIN);
  3622. tor_assert(conn->type <= _CONN_TYPE_MAX);
  3623. #ifdef USE_BUFFEREVENTS
  3624. if (conn->bufev) {
  3625. tor_assert(conn->read_event == NULL);
  3626. tor_assert(conn->write_event == NULL);
  3627. tor_assert(conn->inbuf == NULL);
  3628. tor_assert(conn->outbuf == NULL);
  3629. }
  3630. #endif
  3631. switch (conn->type) {
  3632. case CONN_TYPE_OR:
  3633. tor_assert(conn->magic == OR_CONNECTION_MAGIC);
  3634. break;
  3635. case CONN_TYPE_AP:
  3636. tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
  3637. break;
  3638. case CONN_TYPE_EXIT:
  3639. tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
  3640. break;
  3641. case CONN_TYPE_DIR:
  3642. tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
  3643. break;
  3644. case CONN_TYPE_CONTROL:
  3645. tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
  3646. break;
  3647. CASE_ANY_LISTENER_TYPE:
  3648. tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
  3649. break;
  3650. default:
  3651. tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
  3652. break;
  3653. }
  3654. if (conn->linked_conn) {
  3655. tor_assert(conn->linked_conn->linked_conn == conn);
  3656. tor_assert(conn->linked);
  3657. }
  3658. if (conn->linked)
  3659. tor_assert(!SOCKET_OK(conn->s));
  3660. if (conn->outbuf_flushlen > 0) {
  3661. /* With optimistic data, we may have queued data in
  3662. * EXIT_CONN_STATE_RESOLVING while the conn is not yet marked to writing.
  3663. * */
  3664. tor_assert((conn->type == CONN_TYPE_EXIT &&
  3665. conn->state == EXIT_CONN_STATE_RESOLVING) ||
  3666. connection_is_writing(conn) ||
  3667. conn->write_blocked_on_bw ||
  3668. (CONN_IS_EDGE(conn) &&
  3669. TO_EDGE_CONN(conn)->edge_blocked_on_circ));
  3670. }
  3671. if (conn->hold_open_until_flushed)
  3672. tor_assert(conn->marked_for_close);
  3673. /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
  3674. * marked_for_close. */
  3675. /* buffers */
  3676. if (conn->inbuf)
  3677. assert_buf_ok(conn->inbuf);
  3678. if (conn->outbuf)
  3679. assert_buf_ok(conn->outbuf);
  3680. if (conn->type == CONN_TYPE_OR) {
  3681. or_connection_t *or_conn = TO_OR_CONN(conn);
  3682. if (conn->state == OR_CONN_STATE_OPEN) {
  3683. /* tor_assert(conn->bandwidth > 0); */
  3684. /* the above isn't necessarily true: if we just did a TLS
  3685. * handshake but we didn't recognize the other peer, or it
  3686. * gave a bad cert/etc, then we won't have assigned bandwidth,
  3687. * yet it will be open. -RD
  3688. */
  3689. // tor_assert(conn->read_bucket >= 0);
  3690. }
  3691. // tor_assert(conn->addr && conn->port);
  3692. tor_assert(conn->address);
  3693. if (conn->state > OR_CONN_STATE_PROXY_HANDSHAKING)
  3694. tor_assert(or_conn->tls);
  3695. }
  3696. if (CONN_IS_EDGE(conn)) {
  3697. /* XXX unchecked: package window, deliver window. */
  3698. if (conn->type == CONN_TYPE_AP) {
  3699. entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
  3700. if (entry_conn->chosen_exit_optional || entry_conn->chosen_exit_retries)
  3701. tor_assert(entry_conn->chosen_exit_name);
  3702. tor_assert(entry_conn->socks_request);
  3703. if (conn->state == AP_CONN_STATE_OPEN) {
  3704. tor_assert(entry_conn->socks_request->has_finished);
  3705. if (!conn->marked_for_close) {
  3706. tor_assert(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
  3707. assert_cpath_layer_ok(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
  3708. }
  3709. }
  3710. }
  3711. if (conn->type == CONN_TYPE_EXIT) {
  3712. tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
  3713. conn->purpose == EXIT_PURPOSE_RESOLVE);
  3714. }
  3715. } else if (conn->type == CONN_TYPE_DIR) {
  3716. } else {
  3717. /* Purpose is only used for dir and exit types currently */
  3718. tor_assert(!conn->purpose);
  3719. }
  3720. switch (conn->type)
  3721. {
  3722. CASE_ANY_LISTENER_TYPE:
  3723. tor_assert(conn->state == LISTENER_STATE_READY);
  3724. break;
  3725. case CONN_TYPE_OR:
  3726. tor_assert(conn->state >= _OR_CONN_STATE_MIN);
  3727. tor_assert(conn->state <= _OR_CONN_STATE_MAX);
  3728. tor_assert(TO_OR_CONN(conn)->n_circuits >= 0);
  3729. break;
  3730. case CONN_TYPE_EXIT:
  3731. tor_assert(conn->state >= _EXIT_CONN_STATE_MIN);
  3732. tor_assert(conn->state <= _EXIT_CONN_STATE_MAX);
  3733. tor_assert(conn->purpose >= _EXIT_PURPOSE_MIN);
  3734. tor_assert(conn->purpose <= _EXIT_PURPOSE_MAX);
  3735. break;
  3736. case CONN_TYPE_AP:
  3737. tor_assert(conn->state >= _AP_CONN_STATE_MIN);
  3738. tor_assert(conn->state <= _AP_CONN_STATE_MAX);
  3739. tor_assert(TO_ENTRY_CONN(conn)->socks_request);
  3740. break;
  3741. case CONN_TYPE_DIR:
  3742. tor_assert(conn->state >= _DIR_CONN_STATE_MIN);
  3743. tor_assert(conn->state <= _DIR_CONN_STATE_MAX);
  3744. tor_assert(conn->purpose >= _DIR_PURPOSE_MIN);
  3745. tor_assert(conn->purpose <= _DIR_PURPOSE_MAX);
  3746. break;
  3747. case CONN_TYPE_CPUWORKER:
  3748. tor_assert(conn->state >= _CPUWORKER_STATE_MIN);
  3749. tor_assert(conn->state <= _CPUWORKER_STATE_MAX);
  3750. break;
  3751. case CONN_TYPE_CONTROL:
  3752. tor_assert(conn->state >= _CONTROL_CONN_STATE_MIN);
  3753. tor_assert(conn->state <= _CONTROL_CONN_STATE_MAX);
  3754. break;
  3755. default:
  3756. tor_assert(0);
  3757. }
  3758. }
  3759. /** Fills <b>addr</b> and <b>port</b> with the details of the global
  3760. * proxy server we are using.
  3761. * <b>conn</b> contains the connection we are using the proxy for.
  3762. *
  3763. * Return 0 on success, -1 on failure.
  3764. */
  3765. int
  3766. get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
  3767. const connection_t *conn)
  3768. {
  3769. const or_options_t *options = get_options();
  3770. if (options->HTTPSProxy) {
  3771. tor_addr_copy(addr, &options->HTTPSProxyAddr);
  3772. *port = options->HTTPSProxyPort;
  3773. *proxy_type = PROXY_CONNECT;
  3774. return 0;
  3775. } else if (options->Socks4Proxy) {
  3776. tor_addr_copy(addr, &options->Socks4ProxyAddr);
  3777. *port = options->Socks4ProxyPort;
  3778. *proxy_type = PROXY_SOCKS4;
  3779. return 0;
  3780. } else if (options->Socks5Proxy) {
  3781. tor_addr_copy(addr, &options->Socks5ProxyAddr);
  3782. *port = options->Socks5ProxyPort;
  3783. *proxy_type = PROXY_SOCKS5;
  3784. return 0;
  3785. } else if (options->ClientTransportPlugin ||
  3786. options->Bridges) {
  3787. const transport_t *transport = NULL;
  3788. int r;
  3789. r = find_transport_by_bridge_addrport(&conn->addr, conn->port, &transport);
  3790. if (r<0)
  3791. return -1;
  3792. if (transport) { /* transport found */
  3793. tor_addr_copy(addr, &transport->addr);
  3794. *port = transport->port;
  3795. *proxy_type = transport->socks_version;
  3796. return 0;
  3797. }
  3798. }
  3799. *proxy_type = PROXY_NONE;
  3800. return 0;
  3801. }
  3802. /** Returns the global proxy type used by tor. */
  3803. static int
  3804. get_proxy_type(void)
  3805. {
  3806. const or_options_t *options = get_options();
  3807. if (options->HTTPSProxy)
  3808. return PROXY_CONNECT;
  3809. else if (options->Socks4Proxy)
  3810. return PROXY_SOCKS4;
  3811. else if (options->Socks5Proxy)
  3812. return PROXY_SOCKS5;
  3813. else if (options->ClientTransportPlugin)
  3814. return PROXY_PLUGGABLE;
  3815. else
  3816. return PROXY_NONE;
  3817. }
  3818. /** Log a failed connection to a proxy server.
  3819. * <b>conn</b> is the connection we use the proxy server for. */
  3820. void
  3821. log_failed_proxy_connection(connection_t *conn)
  3822. {
  3823. tor_addr_t proxy_addr;
  3824. uint16_t proxy_port;
  3825. int proxy_type;
  3826. if (get_proxy_addrport(&proxy_addr, &proxy_port, &proxy_type, conn) != 0)
  3827. return; /* if we have no proxy set up, leave this function. */
  3828. log_warn(LD_NET,
  3829. "The connection to the %s proxy server at %s:%u just failed. "
  3830. "Make sure that the proxy server is up and running.",
  3831. proxy_type_to_string(get_proxy_type()), fmt_addr(&proxy_addr),
  3832. proxy_port);
  3833. }
  3834. /** Return string representation of <b>proxy_type</b>. */
  3835. static const char *
  3836. proxy_type_to_string(int proxy_type)
  3837. {
  3838. switch (proxy_type) {
  3839. case PROXY_CONNECT: return "HTTP";
  3840. case PROXY_SOCKS4: return "SOCKS4";
  3841. case PROXY_SOCKS5: return "SOCKS5";
  3842. case PROXY_PLUGGABLE: return "pluggable transports SOCKS";
  3843. case PROXY_NONE: return "NULL";
  3844. default: tor_assert(0);
  3845. }
  3846. return NULL; /*Unreached*/
  3847. }
  3848. /** Call _connection_free() on every connection in our array, and release all
  3849. * storage held by connection.c. This is used by cpuworkers and dnsworkers
  3850. * when they fork, so they don't keep resources held open (especially
  3851. * sockets).
  3852. *
  3853. * Don't do the checks in connection_free(), because they will
  3854. * fail.
  3855. */
  3856. void
  3857. connection_free_all(void)
  3858. {
  3859. smartlist_t *conns = get_connection_array();
  3860. /* We don't want to log any messages to controllers. */
  3861. SMARTLIST_FOREACH(conns, connection_t *, conn,
  3862. if (conn->type == CONN_TYPE_CONTROL)
  3863. TO_CONTROL_CONN(conn)->event_mask = 0);
  3864. control_update_global_event_mask();
  3865. /* Unlink everything from the identity map. */
  3866. connection_or_clear_identity_map();
  3867. /* Clear out our list of broken connections */
  3868. clear_broken_connection_map(0);
  3869. SMARTLIST_FOREACH(conns, connection_t *, conn, _connection_free(conn));
  3870. if (outgoing_addrs) {
  3871. SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t *, addr, tor_free(addr));
  3872. smartlist_free(outgoing_addrs);
  3873. outgoing_addrs = NULL;
  3874. }
  3875. #ifdef USE_BUFFEREVENTS
  3876. if (global_rate_limit)
  3877. bufferevent_rate_limit_group_free(global_rate_limit);
  3878. #endif
  3879. }