connection.c 175 KB

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