2
0

url.c 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef HAVE_NETINET_IN_H
  24. #include <netinet/in.h>
  25. #endif
  26. #ifdef HAVE_NETDB_H
  27. #include <netdb.h>
  28. #endif
  29. #ifdef HAVE_ARPA_INET_H
  30. #include <arpa/inet.h>
  31. #endif
  32. #ifdef HAVE_NET_IF_H
  33. #include <net/if.h>
  34. #endif
  35. #ifdef HAVE_IPHLPAPI_H
  36. #include <Iphlpapi.h>
  37. #endif
  38. #ifdef HAVE_SYS_IOCTL_H
  39. #include <sys/ioctl.h>
  40. #endif
  41. #ifdef HAVE_SYS_PARAM_H
  42. #include <sys/param.h>
  43. #endif
  44. #ifdef __VMS
  45. #include <in.h>
  46. #include <inet.h>
  47. #endif
  48. #ifdef HAVE_SYS_UN_H
  49. #include <sys/un.h>
  50. #endif
  51. #ifndef HAVE_SOCKET
  52. #error "We can't compile without socket() support!"
  53. #endif
  54. #include <limits.h>
  55. #ifdef USE_LIBIDN2
  56. #include <idn2.h>
  57. #elif defined(USE_WIN32_IDN)
  58. /* prototype for curl_win32_idn_to_ascii() */
  59. bool curl_win32_idn_to_ascii(const char *in, char **out);
  60. #endif /* USE_LIBIDN2 */
  61. #include "urldata.h"
  62. #include "netrc.h"
  63. #include "formdata.h"
  64. #include "mime.h"
  65. #include "vtls/vtls.h"
  66. #include "hostip.h"
  67. #include "transfer.h"
  68. #include "sendf.h"
  69. #include "progress.h"
  70. #include "cookie.h"
  71. #include "strcase.h"
  72. #include "strerror.h"
  73. #include "escape.h"
  74. #include "strtok.h"
  75. #include "share.h"
  76. #include "content_encoding.h"
  77. #include "http_digest.h"
  78. #include "http_negotiate.h"
  79. #include "select.h"
  80. #include "multiif.h"
  81. #include "easyif.h"
  82. #include "speedcheck.h"
  83. #include "warnless.h"
  84. #include "non-ascii.h"
  85. #include "inet_pton.h"
  86. #include "getinfo.h"
  87. #include "urlapi-int.h"
  88. #include "system_win32.h"
  89. /* And now for the protocols */
  90. #include "ftp.h"
  91. #include "dict.h"
  92. #include "telnet.h"
  93. #include "tftp.h"
  94. #include "http.h"
  95. #include "http2.h"
  96. #include "file.h"
  97. #include "curl_ldap.h"
  98. #include "vssh/ssh.h"
  99. #include "imap.h"
  100. #include "url.h"
  101. #include "connect.h"
  102. #include "inet_ntop.h"
  103. #include "http_ntlm.h"
  104. #include "curl_rtmp.h"
  105. #include "gopher.h"
  106. #include "mqtt.h"
  107. #include "http_proxy.h"
  108. #include "conncache.h"
  109. #include "multihandle.h"
  110. #include "dotdot.h"
  111. #include "strdup.h"
  112. #include "setopt.h"
  113. #include "altsvc.h"
  114. #include "dynbuf.h"
  115. /* The last 3 #include files should be in this order */
  116. #include "curl_printf.h"
  117. #include "curl_memory.h"
  118. #include "memdebug.h"
  119. static void conn_free(struct connectdata *conn);
  120. static unsigned int get_protocol_family(unsigned int protocol);
  121. /* Some parts of the code (e.g. chunked encoding) assume this buffer has at
  122. * more than just a few bytes to play with. Don't let it become too small or
  123. * bad things will happen.
  124. */
  125. #if READBUFFER_SIZE < READBUFFER_MIN
  126. # error READBUFFER_SIZE is too small
  127. #endif
  128. /*
  129. * Protocol table. Schemes (roughly) in 2019 popularity order:
  130. *
  131. * HTTPS, HTTP, FTP, FTPS, SFTP, FILE, SCP, SMTP, LDAP, IMAPS, TELNET, IMAP,
  132. * LDAPS, SMTPS, TFTP, SMB, POP3, GOPHER POP3S, RTSP, RTMP, SMBS, DICT
  133. */
  134. static const struct Curl_handler * const protocols[] = {
  135. #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
  136. &Curl_handler_https,
  137. #endif
  138. #ifndef CURL_DISABLE_HTTP
  139. &Curl_handler_http,
  140. #endif
  141. #ifndef CURL_DISABLE_FTP
  142. &Curl_handler_ftp,
  143. #endif
  144. #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
  145. &Curl_handler_ftps,
  146. #endif
  147. #if defined(USE_SSH)
  148. &Curl_handler_sftp,
  149. #endif
  150. #ifndef CURL_DISABLE_FILE
  151. &Curl_handler_file,
  152. #endif
  153. #if defined(USE_SSH) && !defined(USE_WOLFSSH)
  154. &Curl_handler_scp,
  155. #endif
  156. #ifndef CURL_DISABLE_SMTP
  157. &Curl_handler_smtp,
  158. #ifdef USE_SSL
  159. &Curl_handler_smtps,
  160. #endif
  161. #endif
  162. #ifndef CURL_DISABLE_LDAP
  163. &Curl_handler_ldap,
  164. #if !defined(CURL_DISABLE_LDAPS) && \
  165. ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
  166. (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
  167. &Curl_handler_ldaps,
  168. #endif
  169. #endif
  170. #ifndef CURL_DISABLE_IMAP
  171. &Curl_handler_imap,
  172. #ifdef USE_SSL
  173. &Curl_handler_imaps,
  174. #endif
  175. #endif
  176. #ifndef CURL_DISABLE_TELNET
  177. &Curl_handler_telnet,
  178. #endif
  179. #ifndef CURL_DISABLE_TFTP
  180. &Curl_handler_tftp,
  181. #endif
  182. #ifndef CURL_DISABLE_POP3
  183. &Curl_handler_pop3,
  184. #ifdef USE_SSL
  185. &Curl_handler_pop3s,
  186. #endif
  187. #endif
  188. #if !defined(CURL_DISABLE_SMB) && defined(USE_NTLM) && \
  189. (CURL_SIZEOF_CURL_OFF_T > 4) && \
  190. (!defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO))
  191. &Curl_handler_smb,
  192. #ifdef USE_SSL
  193. &Curl_handler_smbs,
  194. #endif
  195. #endif
  196. #ifndef CURL_DISABLE_RTSP
  197. &Curl_handler_rtsp,
  198. #endif
  199. #ifdef CURL_ENABLE_MQTT
  200. &Curl_handler_mqtt,
  201. #endif
  202. #ifndef CURL_DISABLE_GOPHER
  203. &Curl_handler_gopher,
  204. #endif
  205. #ifdef USE_LIBRTMP
  206. &Curl_handler_rtmp,
  207. &Curl_handler_rtmpt,
  208. &Curl_handler_rtmpe,
  209. &Curl_handler_rtmpte,
  210. &Curl_handler_rtmps,
  211. &Curl_handler_rtmpts,
  212. #endif
  213. #ifndef CURL_DISABLE_DICT
  214. &Curl_handler_dict,
  215. #endif
  216. (struct Curl_handler *) NULL
  217. };
  218. /*
  219. * Dummy handler for undefined protocol schemes.
  220. */
  221. static const struct Curl_handler Curl_handler_dummy = {
  222. "<no protocol>", /* scheme */
  223. ZERO_NULL, /* setup_connection */
  224. ZERO_NULL, /* do_it */
  225. ZERO_NULL, /* done */
  226. ZERO_NULL, /* do_more */
  227. ZERO_NULL, /* connect_it */
  228. ZERO_NULL, /* connecting */
  229. ZERO_NULL, /* doing */
  230. ZERO_NULL, /* proto_getsock */
  231. ZERO_NULL, /* doing_getsock */
  232. ZERO_NULL, /* domore_getsock */
  233. ZERO_NULL, /* perform_getsock */
  234. ZERO_NULL, /* disconnect */
  235. ZERO_NULL, /* readwrite */
  236. ZERO_NULL, /* connection_check */
  237. 0, /* defport */
  238. 0, /* protocol */
  239. PROTOPT_NONE /* flags */
  240. };
  241. void Curl_freeset(struct Curl_easy *data)
  242. {
  243. /* Free all dynamic strings stored in the data->set substructure. */
  244. enum dupstring i;
  245. for(i = (enum dupstring)0; i < STRING_LAST; i++) {
  246. Curl_safefree(data->set.str[i]);
  247. }
  248. if(data->change.referer_alloc) {
  249. Curl_safefree(data->change.referer);
  250. data->change.referer_alloc = FALSE;
  251. }
  252. data->change.referer = NULL;
  253. if(data->change.url_alloc) {
  254. Curl_safefree(data->change.url);
  255. data->change.url_alloc = FALSE;
  256. }
  257. data->change.url = NULL;
  258. Curl_mime_cleanpart(&data->set.mimepost);
  259. }
  260. /* free the URL pieces */
  261. static void up_free(struct Curl_easy *data)
  262. {
  263. struct urlpieces *up = &data->state.up;
  264. Curl_safefree(up->scheme);
  265. Curl_safefree(up->hostname);
  266. Curl_safefree(up->port);
  267. Curl_safefree(up->user);
  268. Curl_safefree(up->password);
  269. Curl_safefree(up->options);
  270. Curl_safefree(up->path);
  271. Curl_safefree(up->query);
  272. curl_url_cleanup(data->state.uh);
  273. data->state.uh = NULL;
  274. }
  275. /*
  276. * This is the internal function curl_easy_cleanup() calls. This should
  277. * cleanup and free all resources associated with this sessionhandle.
  278. *
  279. * NOTE: if we ever add something that attempts to write to a socket or
  280. * similar here, we must ignore SIGPIPE first. It is currently only done
  281. * when curl_easy_perform() is invoked.
  282. */
  283. CURLcode Curl_close(struct Curl_easy **datap)
  284. {
  285. struct Curl_multi *m;
  286. struct Curl_easy *data;
  287. if(!datap || !*datap)
  288. return CURLE_OK;
  289. data = *datap;
  290. *datap = NULL;
  291. Curl_expire_clear(data); /* shut off timers */
  292. m = data->multi;
  293. if(m)
  294. /* This handle is still part of a multi handle, take care of this first
  295. and detach this handle from there. */
  296. curl_multi_remove_handle(data->multi, data);
  297. if(data->multi_easy) {
  298. /* when curl_easy_perform() is used, it creates its own multi handle to
  299. use and this is the one */
  300. curl_multi_cleanup(data->multi_easy);
  301. data->multi_easy = NULL;
  302. }
  303. /* Destroy the timeout list that is held in the easy handle. It is
  304. /normally/ done by curl_multi_remove_handle() but this is "just in
  305. case" */
  306. Curl_llist_destroy(&data->state.timeoutlist, NULL);
  307. data->magic = 0; /* force a clear AFTER the possibly enforced removal from
  308. the multi handle, since that function uses the magic
  309. field! */
  310. if(data->state.rangestringalloc)
  311. free(data->state.range);
  312. /* freed here just in case DONE wasn't called */
  313. Curl_free_request_state(data);
  314. /* Close down all open SSL info and sessions */
  315. Curl_ssl_close_all(data);
  316. Curl_safefree(data->state.first_host);
  317. Curl_safefree(data->state.scratch);
  318. Curl_ssl_free_certinfo(data);
  319. /* Cleanup possible redirect junk */
  320. free(data->req.newurl);
  321. data->req.newurl = NULL;
  322. if(data->change.referer_alloc) {
  323. Curl_safefree(data->change.referer);
  324. data->change.referer_alloc = FALSE;
  325. }
  326. data->change.referer = NULL;
  327. up_free(data);
  328. Curl_safefree(data->state.buffer);
  329. Curl_dyn_free(&data->state.headerb);
  330. Curl_safefree(data->state.ulbuf);
  331. Curl_flush_cookies(data, TRUE);
  332. #ifdef USE_ALTSVC
  333. Curl_altsvc_save(data, data->asi, data->set.str[STRING_ALTSVC]);
  334. Curl_altsvc_cleanup(data->asi);
  335. data->asi = NULL;
  336. #endif
  337. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_CRYPTO_AUTH)
  338. Curl_http_auth_cleanup_digest(data);
  339. #endif
  340. Curl_safefree(data->info.contenttype);
  341. Curl_safefree(data->info.wouldredirect);
  342. /* this destroys the channel and we cannot use it anymore after this */
  343. Curl_resolver_cleanup(data->state.resolver);
  344. Curl_http2_cleanup_dependencies(data);
  345. Curl_convert_close(data);
  346. /* No longer a dirty share, if it exists */
  347. if(data->share) {
  348. Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
  349. data->share->dirty--;
  350. Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
  351. }
  352. #ifndef CURL_DISABLE_DOH
  353. Curl_dyn_free(&data->req.doh.probe[0].serverdoh);
  354. Curl_dyn_free(&data->req.doh.probe[1].serverdoh);
  355. curl_slist_free_all(data->req.doh.headers);
  356. #endif
  357. /* destruct wildcard structures if it is needed */
  358. Curl_wildcard_dtor(&data->wildcard);
  359. Curl_freeset(data);
  360. free(data);
  361. return CURLE_OK;
  362. }
  363. /*
  364. * Initialize the UserDefined fields within a Curl_easy.
  365. * This may be safely called on a new or existing Curl_easy.
  366. */
  367. CURLcode Curl_init_userdefined(struct Curl_easy *data)
  368. {
  369. struct UserDefined *set = &data->set;
  370. CURLcode result = CURLE_OK;
  371. set->out = stdout; /* default output to stdout */
  372. set->in_set = stdin; /* default input from stdin */
  373. set->err = stderr; /* default stderr to stderr */
  374. /* use fwrite as default function to store output */
  375. set->fwrite_func = (curl_write_callback)fwrite;
  376. /* use fread as default function to read input */
  377. set->fread_func_set = (curl_read_callback)fread;
  378. set->is_fread_set = 0;
  379. set->is_fwrite_set = 0;
  380. set->seek_func = ZERO_NULL;
  381. set->seek_client = ZERO_NULL;
  382. /* conversion callbacks for non-ASCII hosts */
  383. set->convfromnetwork = ZERO_NULL;
  384. set->convtonetwork = ZERO_NULL;
  385. set->convfromutf8 = ZERO_NULL;
  386. set->filesize = -1; /* we don't know the size */
  387. set->postfieldsize = -1; /* unknown size */
  388. set->maxredirs = -1; /* allow any amount by default */
  389. set->httpreq = HTTPREQ_GET; /* Default HTTP request */
  390. set->rtspreq = RTSPREQ_OPTIONS; /* Default RTSP request */
  391. #ifndef CURL_DISABLE_FTP
  392. set->ftp_use_epsv = TRUE; /* FTP defaults to EPSV operations */
  393. set->ftp_use_eprt = TRUE; /* FTP defaults to EPRT operations */
  394. set->ftp_use_pret = FALSE; /* mainly useful for drftpd servers */
  395. set->ftp_filemethod = FTPFILE_MULTICWD;
  396. #endif
  397. set->dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
  398. /* Set the default size of the SSL session ID cache */
  399. set->general_ssl.max_ssl_sessions = 5;
  400. set->proxyport = 0;
  401. set->proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
  402. set->httpauth = CURLAUTH_BASIC; /* defaults to basic */
  403. set->proxyauth = CURLAUTH_BASIC; /* defaults to basic */
  404. /* SOCKS5 proxy auth defaults to username/password + GSS-API */
  405. set->socks5auth = CURLAUTH_BASIC | CURLAUTH_GSSAPI;
  406. /* make libcurl quiet by default: */
  407. set->hide_progress = TRUE; /* CURLOPT_NOPROGRESS changes these */
  408. Curl_mime_initpart(&set->mimepost, data);
  409. /*
  410. * libcurl 7.10 introduced SSL verification *by default*! This needs to be
  411. * switched off unless wanted.
  412. */
  413. set->ssl.primary.verifypeer = TRUE;
  414. set->ssl.primary.verifyhost = TRUE;
  415. #ifdef USE_TLS_SRP
  416. set->ssl.authtype = CURL_TLSAUTH_NONE;
  417. #endif
  418. set->ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
  419. type */
  420. set->ssl.primary.sessionid = TRUE; /* session ID caching enabled by
  421. default */
  422. set->proxy_ssl = set->ssl;
  423. set->new_file_perms = 0644; /* Default permissions */
  424. set->new_directory_perms = 0755; /* Default permissions */
  425. /* for the *protocols fields we don't use the CURLPROTO_ALL convenience
  426. define since we internally only use the lower 16 bits for the passed
  427. in bitmask to not conflict with the private bits */
  428. set->allowed_protocols = CURLPROTO_ALL;
  429. set->redir_protocols = CURLPROTO_HTTP | CURLPROTO_HTTPS | CURLPROTO_FTP |
  430. CURLPROTO_FTPS;
  431. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  432. /*
  433. * disallow unprotected protection negotiation NEC reference implementation
  434. * seem not to follow rfc1961 section 4.3/4.4
  435. */
  436. set->socks5_gssapi_nec = FALSE;
  437. #endif
  438. /* Set the default CA cert bundle/path detected/specified at build time.
  439. *
  440. * If Schannel is the selected SSL backend then these locations are
  441. * ignored. We allow setting CA location for schannel only when explicitly
  442. * specified by the user via CURLOPT_CAINFO / --cacert.
  443. */
  444. if(Curl_ssl_backend() != CURLSSLBACKEND_SCHANNEL) {
  445. #if defined(CURL_CA_BUNDLE)
  446. result = Curl_setstropt(&set->str[STRING_SSL_CAFILE_ORIG], CURL_CA_BUNDLE);
  447. if(result)
  448. return result;
  449. result = Curl_setstropt(&set->str[STRING_SSL_CAFILE_PROXY],
  450. CURL_CA_BUNDLE);
  451. if(result)
  452. return result;
  453. #endif
  454. #if defined(CURL_CA_PATH)
  455. result = Curl_setstropt(&set->str[STRING_SSL_CAPATH_ORIG], CURL_CA_PATH);
  456. if(result)
  457. return result;
  458. result = Curl_setstropt(&set->str[STRING_SSL_CAPATH_PROXY], CURL_CA_PATH);
  459. if(result)
  460. return result;
  461. #endif
  462. }
  463. set->wildcard_enabled = FALSE;
  464. set->chunk_bgn = ZERO_NULL;
  465. set->chunk_end = ZERO_NULL;
  466. set->tcp_keepalive = FALSE;
  467. set->tcp_keepintvl = 60;
  468. set->tcp_keepidle = 60;
  469. set->tcp_fastopen = FALSE;
  470. set->tcp_nodelay = TRUE;
  471. set->ssl_enable_npn = TRUE;
  472. set->ssl_enable_alpn = TRUE;
  473. set->expect_100_timeout = 1000L; /* Wait for a second by default. */
  474. set->sep_headers = TRUE; /* separated header lists by default */
  475. set->buffer_size = READBUFFER_SIZE;
  476. set->upload_buffer_size = UPLOADBUFFER_DEFAULT;
  477. set->happy_eyeballs_timeout = CURL_HET_DEFAULT;
  478. set->fnmatch = ZERO_NULL;
  479. set->upkeep_interval_ms = CURL_UPKEEP_INTERVAL_DEFAULT;
  480. set->maxconnects = DEFAULT_CONNCACHE_SIZE; /* for easy handles */
  481. set->maxage_conn = 118;
  482. set->http09_allowed = FALSE;
  483. set->httpversion =
  484. #ifdef USE_NGHTTP2
  485. CURL_HTTP_VERSION_2TLS
  486. #else
  487. CURL_HTTP_VERSION_1_1
  488. #endif
  489. ;
  490. Curl_http2_init_userset(set);
  491. return result;
  492. }
  493. /**
  494. * Curl_open()
  495. *
  496. * @param curl is a pointer to a sessionhandle pointer that gets set by this
  497. * function.
  498. * @return CURLcode
  499. */
  500. CURLcode Curl_open(struct Curl_easy **curl)
  501. {
  502. CURLcode result;
  503. struct Curl_easy *data;
  504. /* Very simple start-up: alloc the struct, init it with zeroes and return */
  505. data = calloc(1, sizeof(struct Curl_easy));
  506. if(!data) {
  507. /* this is a very serious error */
  508. DEBUGF(fprintf(stderr, "Error: calloc of Curl_easy failed\n"));
  509. return CURLE_OUT_OF_MEMORY;
  510. }
  511. data->magic = CURLEASY_MAGIC_NUMBER;
  512. result = Curl_resolver_init(data, &data->state.resolver);
  513. if(result) {
  514. DEBUGF(fprintf(stderr, "Error: resolver_init failed\n"));
  515. free(data);
  516. return result;
  517. }
  518. /* We do some initial setup here, all those fields that can't be just 0 */
  519. data->state.buffer = malloc(READBUFFER_SIZE + 1);
  520. if(!data->state.buffer) {
  521. DEBUGF(fprintf(stderr, "Error: malloc of buffer failed\n"));
  522. result = CURLE_OUT_OF_MEMORY;
  523. }
  524. else {
  525. result = Curl_init_userdefined(data);
  526. if(!result) {
  527. Curl_dyn_init(&data->state.headerb, CURL_MAX_HTTP_HEADER);
  528. Curl_convert_init(data);
  529. Curl_initinfo(data);
  530. /* most recent connection is not yet defined */
  531. data->state.lastconnect = NULL;
  532. data->progress.flags |= PGRS_HIDE;
  533. data->state.current_speed = -1; /* init to negative == impossible */
  534. }
  535. }
  536. if(result) {
  537. Curl_resolver_cleanup(data->state.resolver);
  538. free(data->state.buffer);
  539. Curl_dyn_free(&data->state.headerb);
  540. Curl_freeset(data);
  541. free(data);
  542. data = NULL;
  543. }
  544. else
  545. *curl = data;
  546. return result;
  547. }
  548. #ifdef USE_RECV_BEFORE_SEND_WORKAROUND
  549. static void conn_reset_postponed_data(struct connectdata *conn, int num)
  550. {
  551. struct postponed_data * const psnd = &(conn->postponed[num]);
  552. if(psnd->buffer) {
  553. DEBUGASSERT(psnd->allocated_size > 0);
  554. DEBUGASSERT(psnd->recv_size <= psnd->allocated_size);
  555. DEBUGASSERT(psnd->recv_size ?
  556. (psnd->recv_processed < psnd->recv_size) :
  557. (psnd->recv_processed == 0));
  558. DEBUGASSERT(psnd->bindsock != CURL_SOCKET_BAD);
  559. free(psnd->buffer);
  560. psnd->buffer = NULL;
  561. psnd->allocated_size = 0;
  562. psnd->recv_size = 0;
  563. psnd->recv_processed = 0;
  564. #ifdef DEBUGBUILD
  565. psnd->bindsock = CURL_SOCKET_BAD; /* used only for DEBUGASSERT */
  566. #endif /* DEBUGBUILD */
  567. }
  568. else {
  569. DEBUGASSERT(psnd->allocated_size == 0);
  570. DEBUGASSERT(psnd->recv_size == 0);
  571. DEBUGASSERT(psnd->recv_processed == 0);
  572. DEBUGASSERT(psnd->bindsock == CURL_SOCKET_BAD);
  573. }
  574. }
  575. static void conn_reset_all_postponed_data(struct connectdata *conn)
  576. {
  577. conn_reset_postponed_data(conn, 0);
  578. conn_reset_postponed_data(conn, 1);
  579. }
  580. #else /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
  581. /* Use "do-nothing" macro instead of function when workaround not used */
  582. #define conn_reset_all_postponed_data(c) do {} while(0)
  583. #endif /* ! USE_RECV_BEFORE_SEND_WORKAROUND */
  584. static void conn_shutdown(struct connectdata *conn)
  585. {
  586. DEBUGASSERT(conn);
  587. infof(conn->data, "Closing connection %ld\n", conn->connection_id);
  588. DEBUGASSERT(conn->data);
  589. /* possible left-overs from the async name resolvers */
  590. Curl_resolver_cancel(conn);
  591. /* close the SSL stuff before we close any sockets since they will/may
  592. write to the sockets */
  593. Curl_ssl_close(conn, FIRSTSOCKET);
  594. Curl_ssl_close(conn, SECONDARYSOCKET);
  595. /* close possibly still open sockets */
  596. if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET])
  597. Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
  598. if(CURL_SOCKET_BAD != conn->sock[FIRSTSOCKET])
  599. Curl_closesocket(conn, conn->sock[FIRSTSOCKET]);
  600. if(CURL_SOCKET_BAD != conn->tempsock[0])
  601. Curl_closesocket(conn, conn->tempsock[0]);
  602. if(CURL_SOCKET_BAD != conn->tempsock[1])
  603. Curl_closesocket(conn, conn->tempsock[1]);
  604. }
  605. static void conn_free(struct connectdata *conn)
  606. {
  607. DEBUGASSERT(conn);
  608. Curl_free_idnconverted_hostname(&conn->host);
  609. Curl_free_idnconverted_hostname(&conn->conn_to_host);
  610. Curl_free_idnconverted_hostname(&conn->http_proxy.host);
  611. Curl_free_idnconverted_hostname(&conn->socks_proxy.host);
  612. Curl_safefree(conn->user);
  613. Curl_safefree(conn->passwd);
  614. Curl_safefree(conn->sasl_authzid);
  615. Curl_safefree(conn->options);
  616. Curl_safefree(conn->http_proxy.user);
  617. Curl_safefree(conn->socks_proxy.user);
  618. Curl_safefree(conn->http_proxy.passwd);
  619. Curl_safefree(conn->socks_proxy.passwd);
  620. Curl_safefree(conn->allocptr.proxyuserpwd);
  621. Curl_safefree(conn->allocptr.uagent);
  622. Curl_safefree(conn->allocptr.userpwd);
  623. Curl_safefree(conn->allocptr.accept_encoding);
  624. Curl_safefree(conn->allocptr.te);
  625. Curl_safefree(conn->allocptr.rangeline);
  626. Curl_safefree(conn->allocptr.ref);
  627. Curl_safefree(conn->allocptr.host);
  628. Curl_safefree(conn->allocptr.cookiehost);
  629. Curl_safefree(conn->allocptr.rtsp_transport);
  630. Curl_safefree(conn->trailer);
  631. Curl_safefree(conn->host.rawalloc); /* host name buffer */
  632. Curl_safefree(conn->conn_to_host.rawalloc); /* host name buffer */
  633. Curl_safefree(conn->hostname_resolve);
  634. Curl_safefree(conn->secondaryhostname);
  635. Curl_safefree(conn->http_proxy.host.rawalloc); /* http proxy name buffer */
  636. Curl_safefree(conn->socks_proxy.host.rawalloc); /* socks proxy name buffer */
  637. Curl_safefree(conn->connect_state);
  638. conn_reset_all_postponed_data(conn);
  639. Curl_llist_destroy(&conn->easyq, NULL);
  640. Curl_safefree(conn->localdev);
  641. Curl_free_primary_ssl_config(&conn->ssl_config);
  642. Curl_free_primary_ssl_config(&conn->proxy_ssl_config);
  643. #ifdef USE_UNIX_SOCKETS
  644. Curl_safefree(conn->unix_domain_socket);
  645. #endif
  646. #ifdef USE_SSL
  647. Curl_safefree(conn->ssl_extra);
  648. #endif
  649. free(conn); /* free all the connection oriented data */
  650. }
  651. /*
  652. * Disconnects the given connection. Note the connection may not be the
  653. * primary connection, like when freeing room in the connection cache or
  654. * killing of a dead old connection.
  655. *
  656. * A connection needs an easy handle when closing down. We support this passed
  657. * in separately since the connection to get closed here is often already
  658. * disassociated from an easy handle.
  659. *
  660. * This function MUST NOT reset state in the Curl_easy struct if that
  661. * isn't strictly bound to the life-time of *this* particular connection.
  662. *
  663. */
  664. CURLcode Curl_disconnect(struct Curl_easy *data,
  665. struct connectdata *conn, bool dead_connection)
  666. {
  667. /* there must be a connection to close */
  668. DEBUGASSERT(conn);
  669. /* it must be removed from the connection cache */
  670. DEBUGASSERT(!conn->bundle);
  671. /* there must be an associated transfer */
  672. DEBUGASSERT(data);
  673. /* the transfer must be detached from the connection */
  674. DEBUGASSERT(!data->conn);
  675. /*
  676. * If this connection isn't marked to force-close, leave it open if there
  677. * are other users of it
  678. */
  679. if(CONN_INUSE(conn) && !dead_connection) {
  680. DEBUGF(infof(data, "Curl_disconnect when inuse: %zu\n", CONN_INUSE(conn)));
  681. return CURLE_OK;
  682. }
  683. if(conn->dns_entry != NULL) {
  684. Curl_resolv_unlock(data, conn->dns_entry);
  685. conn->dns_entry = NULL;
  686. }
  687. /* Cleanup NTLM connection-related data */
  688. Curl_http_auth_cleanup_ntlm(conn);
  689. /* Cleanup NEGOTIATE connection-related data */
  690. Curl_http_auth_cleanup_negotiate(conn);
  691. /* the protocol specific disconnect handler and conn_shutdown need a transfer
  692. for the connection! */
  693. conn->data = data;
  694. if(conn->bits.connect_only)
  695. /* treat the connection as dead in CONNECT_ONLY situations */
  696. dead_connection = TRUE;
  697. if(conn->handler->disconnect)
  698. /* This is set if protocol-specific cleanups should be made */
  699. conn->handler->disconnect(conn, dead_connection);
  700. conn_shutdown(conn);
  701. conn_free(conn);
  702. return CURLE_OK;
  703. }
  704. /*
  705. * This function should return TRUE if the socket is to be assumed to
  706. * be dead. Most commonly this happens when the server has closed the
  707. * connection due to inactivity.
  708. */
  709. static bool SocketIsDead(curl_socket_t sock)
  710. {
  711. int sval;
  712. bool ret_val = TRUE;
  713. sval = SOCKET_READABLE(sock, 0);
  714. if(sval == 0)
  715. /* timeout */
  716. ret_val = FALSE;
  717. return ret_val;
  718. }
  719. /*
  720. * IsMultiplexingPossible()
  721. *
  722. * Return a bitmask with the available multiplexing options for the given
  723. * requested connection.
  724. */
  725. static int IsMultiplexingPossible(const struct Curl_easy *handle,
  726. const struct connectdata *conn)
  727. {
  728. int avail = 0;
  729. /* If a HTTP protocol and multiplexing is enabled */
  730. if((conn->handler->protocol & PROTO_FAMILY_HTTP) &&
  731. (!conn->bits.protoconnstart || !conn->bits.close)) {
  732. if(Curl_multiplex_wanted(handle->multi) &&
  733. (handle->set.httpversion >= CURL_HTTP_VERSION_2))
  734. /* allows HTTP/2 */
  735. avail |= CURLPIPE_MULTIPLEX;
  736. }
  737. return avail;
  738. }
  739. #ifndef CURL_DISABLE_PROXY
  740. static bool
  741. proxy_info_matches(const struct proxy_info *data,
  742. const struct proxy_info *needle)
  743. {
  744. if((data->proxytype == needle->proxytype) &&
  745. (data->port == needle->port) &&
  746. Curl_safe_strcasecompare(data->host.name, needle->host.name))
  747. return TRUE;
  748. return FALSE;
  749. }
  750. static bool
  751. socks_proxy_info_matches(const struct proxy_info *data,
  752. const struct proxy_info *needle)
  753. {
  754. if(!proxy_info_matches(data, needle))
  755. return FALSE;
  756. /* the user information is case-sensitive
  757. or at least it is not defined as case-insensitive
  758. see https://tools.ietf.org/html/rfc3986#section-3.2.1 */
  759. if((data->user == NULL) != (needle->user == NULL))
  760. return FALSE;
  761. /* curl_strequal does a case insentive comparison, so do not use it here! */
  762. if(data->user &&
  763. needle->user &&
  764. strcmp(data->user, needle->user) != 0)
  765. return FALSE;
  766. if((data->passwd == NULL) != (needle->passwd == NULL))
  767. return FALSE;
  768. /* curl_strequal does a case insentive comparison, so do not use it here! */
  769. if(data->passwd &&
  770. needle->passwd &&
  771. strcmp(data->passwd, needle->passwd) != 0)
  772. return FALSE;
  773. return TRUE;
  774. }
  775. #else
  776. /* disabled, won't get called */
  777. #define proxy_info_matches(x,y) FALSE
  778. #define socks_proxy_info_matches(x,y) FALSE
  779. #endif
  780. /* A connection has to have been idle for a shorter time than 'maxage_conn' to
  781. be subject for reuse. The success rate is just too low after this. */
  782. static bool conn_maxage(struct Curl_easy *data,
  783. struct connectdata *conn,
  784. struct curltime now)
  785. {
  786. if(!conn->data) {
  787. timediff_t idletime = Curl_timediff(now, conn->lastused);
  788. idletime /= 1000; /* integer seconds is fine */
  789. if(idletime > data->set.maxage_conn) {
  790. infof(data, "Too old connection (%ld seconds), disconnect it\n",
  791. idletime);
  792. return TRUE;
  793. }
  794. }
  795. return FALSE;
  796. }
  797. /*
  798. * This function checks if the given connection is dead and extracts it from
  799. * the connection cache if so.
  800. *
  801. * When this is called as a Curl_conncache_foreach() callback, the connection
  802. * cache lock is held!
  803. *
  804. * Returns TRUE if the connection was dead and extracted.
  805. */
  806. static bool extract_if_dead(struct connectdata *conn,
  807. struct Curl_easy *data)
  808. {
  809. if(!CONN_INUSE(conn) && !conn->data) {
  810. /* The check for a dead socket makes sense only if the connection isn't in
  811. use */
  812. bool dead;
  813. struct curltime now = Curl_now();
  814. if(conn_maxage(data, conn, now)) {
  815. dead = TRUE;
  816. }
  817. else if(conn->handler->connection_check) {
  818. /* The protocol has a special method for checking the state of the
  819. connection. Use it to check if the connection is dead. */
  820. unsigned int state;
  821. struct Curl_easy *olddata = conn->data;
  822. conn->data = data; /* use this transfer for now */
  823. state = conn->handler->connection_check(conn, CONNCHECK_ISDEAD);
  824. conn->data = olddata;
  825. dead = (state & CONNRESULT_DEAD);
  826. }
  827. else {
  828. /* Use the general method for determining the death of a connection */
  829. dead = SocketIsDead(conn->sock[FIRSTSOCKET]);
  830. }
  831. if(dead) {
  832. infof(data, "Connection %ld seems to be dead!\n", conn->connection_id);
  833. Curl_conncache_remove_conn(data, conn, FALSE);
  834. return TRUE;
  835. }
  836. }
  837. return FALSE;
  838. }
  839. struct prunedead {
  840. struct Curl_easy *data;
  841. struct connectdata *extracted;
  842. };
  843. /*
  844. * Wrapper to use extract_if_dead() function in Curl_conncache_foreach()
  845. *
  846. */
  847. static int call_extract_if_dead(struct connectdata *conn, void *param)
  848. {
  849. struct prunedead *p = (struct prunedead *)param;
  850. if(extract_if_dead(conn, p->data)) {
  851. /* stop the iteration here, pass back the connection that was extracted */
  852. p->extracted = conn;
  853. return 1;
  854. }
  855. return 0; /* continue iteration */
  856. }
  857. /*
  858. * This function scans the connection cache for half-open/dead connections,
  859. * closes and removes them.
  860. * The cleanup is done at most once per second.
  861. */
  862. static void prune_dead_connections(struct Curl_easy *data)
  863. {
  864. struct curltime now = Curl_now();
  865. timediff_t elapsed;
  866. CONNCACHE_LOCK(data);
  867. elapsed =
  868. Curl_timediff(now, data->state.conn_cache->last_cleanup);
  869. CONNCACHE_UNLOCK(data);
  870. if(elapsed >= 1000L) {
  871. struct prunedead prune;
  872. prune.data = data;
  873. prune.extracted = NULL;
  874. while(Curl_conncache_foreach(data, data->state.conn_cache, &prune,
  875. call_extract_if_dead)) {
  876. /* unlocked */
  877. /* remove connection from cache */
  878. Curl_conncache_remove_conn(data, prune.extracted, TRUE);
  879. /* disconnect it */
  880. (void)Curl_disconnect(data, prune.extracted, /* dead_connection */TRUE);
  881. }
  882. CONNCACHE_LOCK(data);
  883. data->state.conn_cache->last_cleanup = now;
  884. CONNCACHE_UNLOCK(data);
  885. }
  886. }
  887. /*
  888. * Given one filled in connection struct (named needle), this function should
  889. * detect if there already is one that has all the significant details
  890. * exactly the same and thus should be used instead.
  891. *
  892. * If there is a match, this function returns TRUE - and has marked the
  893. * connection as 'in-use'. It must later be called with ConnectionDone() to
  894. * return back to 'idle' (unused) state.
  895. *
  896. * The force_reuse flag is set if the connection must be used.
  897. */
  898. static bool
  899. ConnectionExists(struct Curl_easy *data,
  900. struct connectdata *needle,
  901. struct connectdata **usethis,
  902. bool *force_reuse,
  903. bool *waitpipe)
  904. {
  905. struct connectdata *check;
  906. struct connectdata *chosen = 0;
  907. bool foundPendingCandidate = FALSE;
  908. bool canmultiplex = IsMultiplexingPossible(data, needle);
  909. struct connectbundle *bundle;
  910. const char *hostbundle;
  911. #ifdef USE_NTLM
  912. bool wantNTLMhttp = ((data->state.authhost.want &
  913. (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
  914. (needle->handler->protocol & PROTO_FAMILY_HTTP));
  915. bool wantProxyNTLMhttp = (needle->bits.proxy_user_passwd &&
  916. ((data->state.authproxy.want &
  917. (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
  918. (needle->handler->protocol & PROTO_FAMILY_HTTP)));
  919. #endif
  920. *force_reuse = FALSE;
  921. *waitpipe = FALSE;
  922. /* Look up the bundle with all the connections to this particular host.
  923. Locks the connection cache, beware of early returns! */
  924. bundle = Curl_conncache_find_bundle(needle, data->state.conn_cache,
  925. &hostbundle);
  926. if(bundle) {
  927. /* Max pipe length is zero (unlimited) for multiplexed connections */
  928. struct curl_llist_element *curr;
  929. infof(data, "Found bundle for host %s: %p [%s]\n",
  930. hostbundle, (void *)bundle, (bundle->multiuse == BUNDLE_MULTIPLEX ?
  931. "can multiplex" : "serially"));
  932. /* We can't multiplex if we don't know anything about the server */
  933. if(canmultiplex) {
  934. if(bundle->multiuse == BUNDLE_UNKNOWN) {
  935. if(data->set.pipewait) {
  936. infof(data, "Server doesn't support multiplex yet, wait\n");
  937. *waitpipe = TRUE;
  938. CONNCACHE_UNLOCK(data);
  939. return FALSE; /* no re-use */
  940. }
  941. infof(data, "Server doesn't support multiplex (yet)\n");
  942. canmultiplex = FALSE;
  943. }
  944. if((bundle->multiuse == BUNDLE_MULTIPLEX) &&
  945. !Curl_multiplex_wanted(data->multi)) {
  946. infof(data, "Could multiplex, but not asked to!\n");
  947. canmultiplex = FALSE;
  948. }
  949. if(bundle->multiuse == BUNDLE_NO_MULTIUSE) {
  950. infof(data, "Can not multiplex, even if we wanted to!\n");
  951. canmultiplex = FALSE;
  952. }
  953. }
  954. curr = bundle->conn_list.head;
  955. while(curr) {
  956. bool match = FALSE;
  957. size_t multiplexed = 0;
  958. /*
  959. * Note that if we use a HTTP proxy in normal mode (no tunneling), we
  960. * check connections to that proxy and not to the actual remote server.
  961. */
  962. check = curr->ptr;
  963. curr = curr->next;
  964. if(check->bits.connect_only || check->bits.close)
  965. /* connect-only or to-be-closed connections will not be reused */
  966. continue;
  967. if(bundle->multiuse == BUNDLE_MULTIPLEX)
  968. multiplexed = CONN_INUSE(check);
  969. if(canmultiplex) {
  970. ;
  971. }
  972. else {
  973. if(multiplexed) {
  974. /* can only happen within multi handles, and means that another easy
  975. handle is using this connection */
  976. continue;
  977. }
  978. if(Curl_resolver_asynch()) {
  979. /* ip_addr_str[0] is NUL only if the resolving of the name hasn't
  980. completed yet and until then we don't re-use this connection */
  981. if(!check->ip_addr_str[0]) {
  982. infof(data,
  983. "Connection #%ld is still name resolving, can't reuse\n",
  984. check->connection_id);
  985. continue;
  986. }
  987. }
  988. if(check->sock[FIRSTSOCKET] == CURL_SOCKET_BAD) {
  989. foundPendingCandidate = TRUE;
  990. /* Don't pick a connection that hasn't connected yet */
  991. infof(data, "Connection #%ld isn't open enough, can't reuse\n",
  992. check->connection_id);
  993. continue;
  994. }
  995. }
  996. #ifdef USE_UNIX_SOCKETS
  997. if(needle->unix_domain_socket) {
  998. if(!check->unix_domain_socket)
  999. continue;
  1000. if(strcmp(needle->unix_domain_socket, check->unix_domain_socket))
  1001. continue;
  1002. if(needle->abstract_unix_socket != check->abstract_unix_socket)
  1003. continue;
  1004. }
  1005. else if(check->unix_domain_socket)
  1006. continue;
  1007. #endif
  1008. if((needle->handler->flags&PROTOPT_SSL) !=
  1009. (check->handler->flags&PROTOPT_SSL))
  1010. /* don't do mixed SSL and non-SSL connections */
  1011. if(get_protocol_family(check->handler->protocol) !=
  1012. needle->handler->protocol || !check->tls_upgraded)
  1013. /* except protocols that have been upgraded via TLS */
  1014. continue;
  1015. if(needle->bits.httpproxy != check->bits.httpproxy ||
  1016. needle->bits.socksproxy != check->bits.socksproxy)
  1017. continue;
  1018. if(needle->bits.socksproxy &&
  1019. !socks_proxy_info_matches(&needle->socks_proxy,
  1020. &check->socks_proxy))
  1021. continue;
  1022. if(needle->bits.conn_to_host != check->bits.conn_to_host)
  1023. /* don't mix connections that use the "connect to host" feature and
  1024. * connections that don't use this feature */
  1025. continue;
  1026. if(needle->bits.conn_to_port != check->bits.conn_to_port)
  1027. /* don't mix connections that use the "connect to port" feature and
  1028. * connections that don't use this feature */
  1029. continue;
  1030. if(needle->bits.httpproxy) {
  1031. if(!proxy_info_matches(&needle->http_proxy, &check->http_proxy))
  1032. continue;
  1033. if(needle->bits.tunnel_proxy != check->bits.tunnel_proxy)
  1034. continue;
  1035. if(needle->http_proxy.proxytype == CURLPROXY_HTTPS) {
  1036. /* use https proxy */
  1037. if(needle->handler->flags&PROTOPT_SSL) {
  1038. /* use double layer ssl */
  1039. if(!Curl_ssl_config_matches(&needle->proxy_ssl_config,
  1040. &check->proxy_ssl_config))
  1041. continue;
  1042. if(check->proxy_ssl[FIRSTSOCKET].state != ssl_connection_complete)
  1043. continue;
  1044. }
  1045. else {
  1046. if(!Curl_ssl_config_matches(&needle->ssl_config,
  1047. &check->ssl_config))
  1048. continue;
  1049. if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete)
  1050. continue;
  1051. }
  1052. }
  1053. }
  1054. DEBUGASSERT(!check->data || GOOD_EASY_HANDLE(check->data));
  1055. if(!canmultiplex && check->data)
  1056. /* this request can't be multiplexed but the checked connection is
  1057. already in use so we skip it */
  1058. continue;
  1059. if(check->data && (check->data->multi != needle->data->multi))
  1060. /* this could be subject for multiplex use, but only if they belong to
  1061. * the same multi handle */
  1062. continue;
  1063. if(needle->localdev || needle->localport) {
  1064. /* If we are bound to a specific local end (IP+port), we must not
  1065. re-use a random other one, although if we didn't ask for a
  1066. particular one we can reuse one that was bound.
  1067. This comparison is a bit rough and too strict. Since the input
  1068. parameters can be specified in numerous ways and still end up the
  1069. same it would take a lot of processing to make it really accurate.
  1070. Instead, this matching will assume that re-uses of bound connections
  1071. will most likely also re-use the exact same binding parameters and
  1072. missing out a few edge cases shouldn't hurt anyone very much.
  1073. */
  1074. if((check->localport != needle->localport) ||
  1075. (check->localportrange != needle->localportrange) ||
  1076. (needle->localdev &&
  1077. (!check->localdev || strcmp(check->localdev, needle->localdev))))
  1078. continue;
  1079. }
  1080. if(!(needle->handler->flags & PROTOPT_CREDSPERREQUEST)) {
  1081. /* This protocol requires credentials per connection,
  1082. so verify that we're using the same name and password as well */
  1083. if(strcmp(needle->user, check->user) ||
  1084. strcmp(needle->passwd, check->passwd)) {
  1085. /* one of them was different */
  1086. continue;
  1087. }
  1088. }
  1089. if(!needle->bits.httpproxy || (needle->handler->flags&PROTOPT_SSL) ||
  1090. needle->bits.tunnel_proxy) {
  1091. /* The requested connection does not use a HTTP proxy or it uses SSL or
  1092. it is a non-SSL protocol tunneled or it is a non-SSL protocol which
  1093. is allowed to be upgraded via TLS */
  1094. if((strcasecompare(needle->handler->scheme, check->handler->scheme) ||
  1095. (get_protocol_family(check->handler->protocol) ==
  1096. needle->handler->protocol && check->tls_upgraded)) &&
  1097. (!needle->bits.conn_to_host || strcasecompare(
  1098. needle->conn_to_host.name, check->conn_to_host.name)) &&
  1099. (!needle->bits.conn_to_port ||
  1100. needle->conn_to_port == check->conn_to_port) &&
  1101. strcasecompare(needle->host.name, check->host.name) &&
  1102. needle->remote_port == check->remote_port) {
  1103. /* The schemes match or the protocol family is the same and the
  1104. previous connection was TLS upgraded, and the hostname and host
  1105. port match */
  1106. if(needle->handler->flags & PROTOPT_SSL) {
  1107. /* This is a SSL connection so verify that we're using the same
  1108. SSL options as well */
  1109. if(!Curl_ssl_config_matches(&needle->ssl_config,
  1110. &check->ssl_config)) {
  1111. DEBUGF(infof(data,
  1112. "Connection #%ld has different SSL parameters, "
  1113. "can't reuse\n",
  1114. check->connection_id));
  1115. continue;
  1116. }
  1117. if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete) {
  1118. foundPendingCandidate = TRUE;
  1119. DEBUGF(infof(data,
  1120. "Connection #%ld has not started SSL connect, "
  1121. "can't reuse\n",
  1122. check->connection_id));
  1123. continue;
  1124. }
  1125. }
  1126. match = TRUE;
  1127. }
  1128. }
  1129. else {
  1130. /* The requested connection is using the same HTTP proxy in normal
  1131. mode (no tunneling) */
  1132. match = TRUE;
  1133. }
  1134. if(match) {
  1135. #if defined(USE_NTLM)
  1136. /* If we are looking for an HTTP+NTLM connection, check if this is
  1137. already authenticating with the right credentials. If not, keep
  1138. looking so that we can reuse NTLM connections if
  1139. possible. (Especially we must not reuse the same connection if
  1140. partway through a handshake!) */
  1141. if(wantNTLMhttp) {
  1142. if(strcmp(needle->user, check->user) ||
  1143. strcmp(needle->passwd, check->passwd)) {
  1144. /* we prefer a credential match, but this is at least a connection
  1145. that can be reused and "upgraded" to NTLM */
  1146. if(check->http_ntlm_state == NTLMSTATE_NONE)
  1147. chosen = check;
  1148. continue;
  1149. }
  1150. }
  1151. else if(check->http_ntlm_state != NTLMSTATE_NONE) {
  1152. /* Connection is using NTLM auth but we don't want NTLM */
  1153. continue;
  1154. }
  1155. /* Same for Proxy NTLM authentication */
  1156. if(wantProxyNTLMhttp) {
  1157. /* Both check->http_proxy.user and check->http_proxy.passwd can be
  1158. * NULL */
  1159. if(!check->http_proxy.user || !check->http_proxy.passwd)
  1160. continue;
  1161. if(strcmp(needle->http_proxy.user, check->http_proxy.user) ||
  1162. strcmp(needle->http_proxy.passwd, check->http_proxy.passwd))
  1163. continue;
  1164. }
  1165. else if(check->proxy_ntlm_state != NTLMSTATE_NONE) {
  1166. /* Proxy connection is using NTLM auth but we don't want NTLM */
  1167. continue;
  1168. }
  1169. if(wantNTLMhttp || wantProxyNTLMhttp) {
  1170. /* Credentials are already checked, we can use this connection */
  1171. chosen = check;
  1172. if((wantNTLMhttp &&
  1173. (check->http_ntlm_state != NTLMSTATE_NONE)) ||
  1174. (wantProxyNTLMhttp &&
  1175. (check->proxy_ntlm_state != NTLMSTATE_NONE))) {
  1176. /* We must use this connection, no other */
  1177. *force_reuse = TRUE;
  1178. break;
  1179. }
  1180. /* Continue look up for a better connection */
  1181. continue;
  1182. }
  1183. #endif
  1184. if(canmultiplex) {
  1185. /* We can multiplex if we want to. Let's continue looking for
  1186. the optimal connection to use. */
  1187. if(!multiplexed) {
  1188. /* We have the optimal connection. Let's stop looking. */
  1189. chosen = check;
  1190. break;
  1191. }
  1192. #ifdef USE_NGHTTP2
  1193. /* If multiplexed, make sure we don't go over concurrency limit */
  1194. if(check->bits.multiplex) {
  1195. /* Multiplexed connections can only be HTTP/2 for now */
  1196. struct http_conn *httpc = &check->proto.httpc;
  1197. if(multiplexed >= httpc->settings.max_concurrent_streams) {
  1198. infof(data, "MAX_CONCURRENT_STREAMS reached, skip (%zu)\n",
  1199. multiplexed);
  1200. continue;
  1201. }
  1202. else if(multiplexed >=
  1203. Curl_multi_max_concurrent_streams(needle->data->multi)) {
  1204. infof(data, "client side MAX_CONCURRENT_STREAMS reached"
  1205. ", skip (%zu)\n",
  1206. multiplexed);
  1207. continue;
  1208. }
  1209. }
  1210. #endif
  1211. /* When not multiplexed, we have a match here! */
  1212. chosen = check;
  1213. infof(data, "Multiplexed connection found!\n");
  1214. break;
  1215. }
  1216. else {
  1217. /* We have found a connection. Let's stop searching. */
  1218. chosen = check;
  1219. break;
  1220. }
  1221. }
  1222. }
  1223. }
  1224. if(chosen) {
  1225. /* mark it as used before releasing the lock */
  1226. chosen->data = data; /* own it! */
  1227. Curl_attach_connnection(data, chosen);
  1228. CONNCACHE_UNLOCK(data);
  1229. *usethis = chosen;
  1230. return TRUE; /* yes, we found one to use! */
  1231. }
  1232. CONNCACHE_UNLOCK(data);
  1233. if(foundPendingCandidate && data->set.pipewait) {
  1234. infof(data,
  1235. "Found pending candidate for reuse and CURLOPT_PIPEWAIT is set\n");
  1236. *waitpipe = TRUE;
  1237. }
  1238. return FALSE; /* no matching connecting exists */
  1239. }
  1240. /*
  1241. * verboseconnect() displays verbose information after a connect
  1242. */
  1243. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1244. void Curl_verboseconnect(struct connectdata *conn)
  1245. {
  1246. if(conn->data->set.verbose)
  1247. infof(conn->data, "Connected to %s (%s) port %ld (#%ld)\n",
  1248. conn->bits.socksproxy ? conn->socks_proxy.host.dispname :
  1249. conn->bits.httpproxy ? conn->http_proxy.host.dispname :
  1250. conn->bits.conn_to_host ? conn->conn_to_host.dispname :
  1251. conn->host.dispname,
  1252. conn->ip_addr_str, conn->port, conn->connection_id);
  1253. }
  1254. #endif
  1255. /*
  1256. * Helpers for IDNA conversions.
  1257. */
  1258. bool Curl_is_ASCII_name(const char *hostname)
  1259. {
  1260. /* get an UNSIGNED local version of the pointer */
  1261. const unsigned char *ch = (const unsigned char *)hostname;
  1262. if(!hostname) /* bad input, consider it ASCII! */
  1263. return TRUE;
  1264. while(*ch) {
  1265. if(*ch++ & 0x80)
  1266. return FALSE;
  1267. }
  1268. return TRUE;
  1269. }
  1270. /*
  1271. * Strip single trailing dot in the hostname,
  1272. * primarily for SNI and http host header.
  1273. */
  1274. static void strip_trailing_dot(struct hostname *host)
  1275. {
  1276. size_t len;
  1277. if(!host || !host->name)
  1278. return;
  1279. len = strlen(host->name);
  1280. if(len && (host->name[len-1] == '.'))
  1281. host->name[len-1] = 0;
  1282. }
  1283. /*
  1284. * Perform any necessary IDN conversion of hostname
  1285. */
  1286. CURLcode Curl_idnconvert_hostname(struct connectdata *conn,
  1287. struct hostname *host)
  1288. {
  1289. struct Curl_easy *data = conn->data;
  1290. #ifndef USE_LIBIDN2
  1291. (void)data;
  1292. (void)conn;
  1293. #elif defined(CURL_DISABLE_VERBOSE_STRINGS)
  1294. (void)conn;
  1295. #endif
  1296. /* set the name we use to display the host name */
  1297. host->dispname = host->name;
  1298. /* Check name for non-ASCII and convert hostname to ACE form if we can */
  1299. if(!Curl_is_ASCII_name(host->name)) {
  1300. #ifdef USE_LIBIDN2
  1301. if(idn2_check_version(IDN2_VERSION)) {
  1302. char *ace_hostname = NULL;
  1303. #if IDN2_VERSION_NUMBER >= 0x00140000
  1304. /* IDN2_NFC_INPUT: Normalize input string using normalization form C.
  1305. IDN2_NONTRANSITIONAL: Perform Unicode TR46 non-transitional
  1306. processing. */
  1307. int flags = IDN2_NFC_INPUT | IDN2_NONTRANSITIONAL;
  1308. #else
  1309. int flags = IDN2_NFC_INPUT;
  1310. #endif
  1311. int rc = idn2_lookup_ul((const char *)host->name, &ace_hostname, flags);
  1312. if(rc == IDN2_OK) {
  1313. host->encalloc = (char *)ace_hostname;
  1314. /* change the name pointer to point to the encoded hostname */
  1315. host->name = host->encalloc;
  1316. }
  1317. else {
  1318. failf(data, "Failed to convert %s to ACE; %s\n", host->name,
  1319. idn2_strerror(rc));
  1320. return CURLE_URL_MALFORMAT;
  1321. }
  1322. }
  1323. #elif defined(USE_WIN32_IDN)
  1324. char *ace_hostname = NULL;
  1325. if(curl_win32_idn_to_ascii(host->name, &ace_hostname)) {
  1326. host->encalloc = ace_hostname;
  1327. /* change the name pointer to point to the encoded hostname */
  1328. host->name = host->encalloc;
  1329. }
  1330. else {
  1331. char buffer[STRERROR_LEN];
  1332. failf(data, "Failed to convert %s to ACE; %s\n", host->name,
  1333. Curl_winapi_strerror(GetLastError(), buffer, sizeof(buffer)));
  1334. return CURLE_URL_MALFORMAT;
  1335. }
  1336. #else
  1337. infof(data, "IDN support not present, can't parse Unicode domains\n");
  1338. #endif
  1339. }
  1340. return CURLE_OK;
  1341. }
  1342. /*
  1343. * Frees data allocated by idnconvert_hostname()
  1344. */
  1345. void Curl_free_idnconverted_hostname(struct hostname *host)
  1346. {
  1347. #if defined(USE_LIBIDN2)
  1348. if(host->encalloc) {
  1349. idn2_free(host->encalloc); /* must be freed with idn2_free() since this was
  1350. allocated by libidn */
  1351. host->encalloc = NULL;
  1352. }
  1353. #elif defined(USE_WIN32_IDN)
  1354. free(host->encalloc); /* must be freed with free() since this was
  1355. allocated by curl_win32_idn_to_ascii */
  1356. host->encalloc = NULL;
  1357. #else
  1358. (void)host;
  1359. #endif
  1360. }
  1361. /*
  1362. * Allocate and initialize a new connectdata object.
  1363. */
  1364. static struct connectdata *allocate_conn(struct Curl_easy *data)
  1365. {
  1366. struct connectdata *conn = calloc(1, sizeof(struct connectdata));
  1367. if(!conn)
  1368. return NULL;
  1369. #ifdef USE_SSL
  1370. /* The SSL backend-specific data (ssl_backend_data) objects are allocated as
  1371. a separate array to ensure suitable alignment.
  1372. Note that these backend pointers can be swapped by vtls (eg ssl backend
  1373. data becomes proxy backend data). */
  1374. {
  1375. size_t sslsize = Curl_ssl->sizeof_ssl_backend_data;
  1376. char *ssl = calloc(4, sslsize);
  1377. if(!ssl) {
  1378. free(conn);
  1379. return NULL;
  1380. }
  1381. conn->ssl_extra = ssl;
  1382. conn->ssl[0].backend = (void *)ssl;
  1383. conn->ssl[1].backend = (void *)(ssl + sslsize);
  1384. conn->proxy_ssl[0].backend = (void *)(ssl + 2 * sslsize);
  1385. conn->proxy_ssl[1].backend = (void *)(ssl + 3 * sslsize);
  1386. }
  1387. #endif
  1388. conn->handler = &Curl_handler_dummy; /* Be sure we have a handler defined
  1389. already from start to avoid NULL
  1390. situations and checks */
  1391. /* and we setup a few fields in case we end up actually using this struct */
  1392. conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
  1393. conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
  1394. conn->tempsock[0] = CURL_SOCKET_BAD; /* no file descriptor */
  1395. conn->tempsock[1] = CURL_SOCKET_BAD; /* no file descriptor */
  1396. conn->connection_id = -1; /* no ID */
  1397. conn->port = -1; /* unknown at this point */
  1398. conn->remote_port = -1; /* unknown at this point */
  1399. #if defined(USE_RECV_BEFORE_SEND_WORKAROUND) && defined(DEBUGBUILD)
  1400. conn->postponed[0].bindsock = CURL_SOCKET_BAD; /* no file descriptor */
  1401. conn->postponed[1].bindsock = CURL_SOCKET_BAD; /* no file descriptor */
  1402. #endif /* USE_RECV_BEFORE_SEND_WORKAROUND && DEBUGBUILD */
  1403. /* Default protocol-independent behavior doesn't support persistent
  1404. connections, so we set this to force-close. Protocols that support
  1405. this need to set this to FALSE in their "curl_do" functions. */
  1406. connclose(conn, "Default to force-close");
  1407. /* Store creation time to help future close decision making */
  1408. conn->created = Curl_now();
  1409. /* Store current time to give a baseline to keepalive connection times. */
  1410. conn->keepalive = Curl_now();
  1411. /* Store off the configured connection upkeep time. */
  1412. conn->upkeep_interval_ms = data->set.upkeep_interval_ms;
  1413. conn->data = data; /* Setup the association between this connection
  1414. and the Curl_easy */
  1415. conn->http_proxy.proxytype = data->set.proxytype;
  1416. conn->socks_proxy.proxytype = CURLPROXY_SOCKS4;
  1417. #if !defined(CURL_DISABLE_PROXY)
  1418. /* note that these two proxy bits are now just on what looks to be
  1419. requested, they may be altered down the road */
  1420. conn->bits.proxy = (data->set.str[STRING_PROXY] &&
  1421. *data->set.str[STRING_PROXY]) ? TRUE : FALSE;
  1422. conn->bits.httpproxy = (conn->bits.proxy &&
  1423. (conn->http_proxy.proxytype == CURLPROXY_HTTP ||
  1424. conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0 ||
  1425. conn->http_proxy.proxytype == CURLPROXY_HTTPS)) ?
  1426. TRUE : FALSE;
  1427. conn->bits.socksproxy = (conn->bits.proxy &&
  1428. !conn->bits.httpproxy) ? TRUE : FALSE;
  1429. if(data->set.str[STRING_PRE_PROXY] && *data->set.str[STRING_PRE_PROXY]) {
  1430. conn->bits.proxy = TRUE;
  1431. conn->bits.socksproxy = TRUE;
  1432. }
  1433. conn->bits.proxy_user_passwd =
  1434. (data->set.str[STRING_PROXYUSERNAME]) ? TRUE : FALSE;
  1435. conn->bits.tunnel_proxy = data->set.tunnel_thru_httpproxy;
  1436. #endif /* CURL_DISABLE_PROXY */
  1437. conn->bits.user_passwd = (data->set.str[STRING_USERNAME]) ? TRUE : FALSE;
  1438. #ifndef CURL_DISABLE_FTP
  1439. conn->bits.ftp_use_epsv = data->set.ftp_use_epsv;
  1440. conn->bits.ftp_use_eprt = data->set.ftp_use_eprt;
  1441. #endif
  1442. conn->ssl_config.verifystatus = data->set.ssl.primary.verifystatus;
  1443. conn->ssl_config.verifypeer = data->set.ssl.primary.verifypeer;
  1444. conn->ssl_config.verifyhost = data->set.ssl.primary.verifyhost;
  1445. conn->proxy_ssl_config.verifystatus =
  1446. data->set.proxy_ssl.primary.verifystatus;
  1447. conn->proxy_ssl_config.verifypeer = data->set.proxy_ssl.primary.verifypeer;
  1448. conn->proxy_ssl_config.verifyhost = data->set.proxy_ssl.primary.verifyhost;
  1449. conn->ip_version = data->set.ipver;
  1450. conn->bits.connect_only = data->set.connect_only;
  1451. conn->transport = TRNSPRT_TCP; /* most of them are TCP streams */
  1452. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
  1453. defined(NTLM_WB_ENABLED)
  1454. conn->ntlm.ntlm_auth_hlpr_socket = CURL_SOCKET_BAD;
  1455. conn->proxyntlm.ntlm_auth_hlpr_socket = CURL_SOCKET_BAD;
  1456. #endif
  1457. /* Initialize the easy handle list */
  1458. Curl_llist_init(&conn->easyq, NULL);
  1459. #ifdef HAVE_GSSAPI
  1460. conn->data_prot = PROT_CLEAR;
  1461. #endif
  1462. /* Store the local bind parameters that will be used for this connection */
  1463. if(data->set.str[STRING_DEVICE]) {
  1464. conn->localdev = strdup(data->set.str[STRING_DEVICE]);
  1465. if(!conn->localdev)
  1466. goto error;
  1467. }
  1468. conn->localportrange = data->set.localportrange;
  1469. conn->localport = data->set.localport;
  1470. /* the close socket stuff needs to be copied to the connection struct as
  1471. it may live on without (this specific) Curl_easy */
  1472. conn->fclosesocket = data->set.fclosesocket;
  1473. conn->closesocket_client = data->set.closesocket_client;
  1474. conn->lastused = Curl_now(); /* used now */
  1475. return conn;
  1476. error:
  1477. Curl_llist_destroy(&conn->easyq, NULL);
  1478. free(conn->localdev);
  1479. #ifdef USE_SSL
  1480. free(conn->ssl_extra);
  1481. #endif
  1482. free(conn);
  1483. return NULL;
  1484. }
  1485. /* returns the handler if the given scheme is built-in */
  1486. const struct Curl_handler *Curl_builtin_scheme(const char *scheme)
  1487. {
  1488. const struct Curl_handler * const *pp;
  1489. const struct Curl_handler *p;
  1490. /* Scan protocol handler table and match against 'scheme'. The handler may
  1491. be changed later when the protocol specific setup function is called. */
  1492. for(pp = protocols; (p = *pp) != NULL; pp++)
  1493. if(strcasecompare(p->scheme, scheme))
  1494. /* Protocol found in table. Check if allowed */
  1495. return p;
  1496. return NULL; /* not found */
  1497. }
  1498. static CURLcode findprotocol(struct Curl_easy *data,
  1499. struct connectdata *conn,
  1500. const char *protostr)
  1501. {
  1502. const struct Curl_handler *p = Curl_builtin_scheme(protostr);
  1503. if(p && /* Protocol found in table. Check if allowed */
  1504. (data->set.allowed_protocols & p->protocol)) {
  1505. /* it is allowed for "normal" request, now do an extra check if this is
  1506. the result of a redirect */
  1507. if(data->state.this_is_a_follow &&
  1508. !(data->set.redir_protocols & p->protocol))
  1509. /* nope, get out */
  1510. ;
  1511. else {
  1512. /* Perform setup complement if some. */
  1513. conn->handler = conn->given = p;
  1514. /* 'port' and 'remote_port' are set in setup_connection_internals() */
  1515. return CURLE_OK;
  1516. }
  1517. }
  1518. /* The protocol was not found in the table, but we don't have to assign it
  1519. to anything since it is already assigned to a dummy-struct in the
  1520. create_conn() function when the connectdata struct is allocated. */
  1521. failf(data, "Protocol \"%s\" not supported or disabled in " LIBCURL_NAME,
  1522. protostr);
  1523. return CURLE_UNSUPPORTED_PROTOCOL;
  1524. }
  1525. CURLcode Curl_uc_to_curlcode(CURLUcode uc)
  1526. {
  1527. switch(uc) {
  1528. default:
  1529. return CURLE_URL_MALFORMAT;
  1530. case CURLUE_UNSUPPORTED_SCHEME:
  1531. return CURLE_UNSUPPORTED_PROTOCOL;
  1532. case CURLUE_OUT_OF_MEMORY:
  1533. return CURLE_OUT_OF_MEMORY;
  1534. case CURLUE_USER_NOT_ALLOWED:
  1535. return CURLE_LOGIN_DENIED;
  1536. }
  1537. }
  1538. /*
  1539. * If the URL was set with an IPv6 numerical address with a zone id part, set
  1540. * the scope_id based on that!
  1541. */
  1542. static void zonefrom_url(CURLU *uh, struct connectdata *conn)
  1543. {
  1544. char *zoneid;
  1545. CURLUcode uc;
  1546. uc = curl_url_get(uh, CURLUPART_ZONEID, &zoneid, 0);
  1547. if(!uc && zoneid) {
  1548. char *endp;
  1549. unsigned long scope = strtoul(zoneid, &endp, 10);
  1550. if(!*endp && (scope < UINT_MAX))
  1551. /* A plain number, use it directly as a scope id. */
  1552. conn->scope_id = (unsigned int)scope;
  1553. #if defined(HAVE_IF_NAMETOINDEX)
  1554. else {
  1555. #elif defined(WIN32)
  1556. else if(Curl_if_nametoindex) {
  1557. #endif
  1558. #if defined(HAVE_IF_NAMETOINDEX) || defined(WIN32)
  1559. /* Zone identifier is not numeric */
  1560. unsigned int scopeidx = 0;
  1561. #if defined(WIN32)
  1562. scopeidx = Curl_if_nametoindex(zoneid);
  1563. #else
  1564. scopeidx = if_nametoindex(zoneid);
  1565. #endif
  1566. if(!scopeidx)
  1567. infof(conn->data, "Invalid zoneid: %s; %s\n", zoneid,
  1568. strerror(errno));
  1569. else
  1570. conn->scope_id = scopeidx;
  1571. }
  1572. #endif /* HAVE_IF_NAMETOINDEX || WIN32 */
  1573. free(zoneid);
  1574. }
  1575. }
  1576. /*
  1577. * Parse URL and fill in the relevant members of the connection struct.
  1578. */
  1579. static CURLcode parseurlandfillconn(struct Curl_easy *data,
  1580. struct connectdata *conn)
  1581. {
  1582. CURLcode result;
  1583. CURLU *uh;
  1584. CURLUcode uc;
  1585. char *hostname;
  1586. up_free(data); /* cleanup previous leftovers first */
  1587. /* parse the URL */
  1588. if(data->set.uh) {
  1589. uh = data->state.uh = curl_url_dup(data->set.uh);
  1590. }
  1591. else {
  1592. uh = data->state.uh = curl_url();
  1593. }
  1594. if(!uh)
  1595. return CURLE_OUT_OF_MEMORY;
  1596. if(data->set.str[STRING_DEFAULT_PROTOCOL] &&
  1597. !Curl_is_absolute_url(data->change.url, NULL, MAX_SCHEME_LEN)) {
  1598. char *url;
  1599. if(data->change.url_alloc)
  1600. free(data->change.url);
  1601. url = aprintf("%s://%s", data->set.str[STRING_DEFAULT_PROTOCOL],
  1602. data->change.url);
  1603. if(!url)
  1604. return CURLE_OUT_OF_MEMORY;
  1605. data->change.url = url;
  1606. data->change.url_alloc = TRUE;
  1607. }
  1608. if(!data->set.uh) {
  1609. char *newurl;
  1610. uc = curl_url_set(uh, CURLUPART_URL, data->change.url,
  1611. CURLU_GUESS_SCHEME |
  1612. CURLU_NON_SUPPORT_SCHEME |
  1613. (data->set.disallow_username_in_url ?
  1614. CURLU_DISALLOW_USER : 0) |
  1615. (data->set.path_as_is ? CURLU_PATH_AS_IS : 0));
  1616. if(uc) {
  1617. DEBUGF(infof(data, "curl_url_set rejected %s\n", data->change.url));
  1618. return Curl_uc_to_curlcode(uc);
  1619. }
  1620. /* after it was parsed, get the generated normalized version */
  1621. uc = curl_url_get(uh, CURLUPART_URL, &newurl, 0);
  1622. if(uc)
  1623. return Curl_uc_to_curlcode(uc);
  1624. if(data->change.url_alloc)
  1625. free(data->change.url);
  1626. data->change.url = newurl;
  1627. data->change.url_alloc = TRUE;
  1628. }
  1629. uc = curl_url_get(uh, CURLUPART_SCHEME, &data->state.up.scheme, 0);
  1630. if(uc)
  1631. return Curl_uc_to_curlcode(uc);
  1632. result = findprotocol(data, conn, data->state.up.scheme);
  1633. if(result)
  1634. return result;
  1635. uc = curl_url_get(uh, CURLUPART_USER, &data->state.up.user,
  1636. CURLU_URLDECODE);
  1637. if(!uc) {
  1638. conn->user = strdup(data->state.up.user);
  1639. if(!conn->user)
  1640. return CURLE_OUT_OF_MEMORY;
  1641. conn->bits.user_passwd = TRUE;
  1642. }
  1643. else if(uc != CURLUE_NO_USER)
  1644. return Curl_uc_to_curlcode(uc);
  1645. uc = curl_url_get(uh, CURLUPART_PASSWORD, &data->state.up.password,
  1646. CURLU_URLDECODE);
  1647. if(!uc) {
  1648. conn->passwd = strdup(data->state.up.password);
  1649. if(!conn->passwd)
  1650. return CURLE_OUT_OF_MEMORY;
  1651. conn->bits.user_passwd = TRUE;
  1652. }
  1653. else if(uc != CURLUE_NO_PASSWORD)
  1654. return Curl_uc_to_curlcode(uc);
  1655. uc = curl_url_get(uh, CURLUPART_OPTIONS, &data->state.up.options,
  1656. CURLU_URLDECODE);
  1657. if(!uc) {
  1658. conn->options = strdup(data->state.up.options);
  1659. if(!conn->options)
  1660. return CURLE_OUT_OF_MEMORY;
  1661. }
  1662. else if(uc != CURLUE_NO_OPTIONS)
  1663. return Curl_uc_to_curlcode(uc);
  1664. uc = curl_url_get(uh, CURLUPART_HOST, &data->state.up.hostname, 0);
  1665. if(uc) {
  1666. if(!strcasecompare("file", data->state.up.scheme))
  1667. return CURLE_OUT_OF_MEMORY;
  1668. }
  1669. uc = curl_url_get(uh, CURLUPART_PATH, &data->state.up.path, 0);
  1670. if(uc)
  1671. return Curl_uc_to_curlcode(uc);
  1672. uc = curl_url_get(uh, CURLUPART_PORT, &data->state.up.port,
  1673. CURLU_DEFAULT_PORT);
  1674. if(uc) {
  1675. if(!strcasecompare("file", data->state.up.scheme))
  1676. return CURLE_OUT_OF_MEMORY;
  1677. }
  1678. else {
  1679. unsigned long port = strtoul(data->state.up.port, NULL, 10);
  1680. conn->port = conn->remote_port = curlx_ultous(port);
  1681. }
  1682. (void)curl_url_get(uh, CURLUPART_QUERY, &data->state.up.query, 0);
  1683. hostname = data->state.up.hostname;
  1684. if(hostname && hostname[0] == '[') {
  1685. /* This looks like an IPv6 address literal. See if there is an address
  1686. scope. */
  1687. size_t hlen;
  1688. conn->bits.ipv6_ip = TRUE;
  1689. /* cut off the brackets! */
  1690. hostname++;
  1691. hlen = strlen(hostname);
  1692. hostname[hlen - 1] = 0;
  1693. zonefrom_url(uh, conn);
  1694. }
  1695. /* make sure the connect struct gets its own copy of the host name */
  1696. conn->host.rawalloc = strdup(hostname ? hostname : "");
  1697. if(!conn->host.rawalloc)
  1698. return CURLE_OUT_OF_MEMORY;
  1699. conn->host.name = conn->host.rawalloc;
  1700. if(data->set.scope_id)
  1701. /* Override any scope that was set above. */
  1702. conn->scope_id = data->set.scope_id;
  1703. return CURLE_OK;
  1704. }
  1705. /*
  1706. * If we're doing a resumed transfer, we need to setup our stuff
  1707. * properly.
  1708. */
  1709. static CURLcode setup_range(struct Curl_easy *data)
  1710. {
  1711. struct UrlState *s = &data->state;
  1712. s->resume_from = data->set.set_resume_from;
  1713. if(s->resume_from || data->set.str[STRING_SET_RANGE]) {
  1714. if(s->rangestringalloc)
  1715. free(s->range);
  1716. if(s->resume_from)
  1717. s->range = aprintf("%" CURL_FORMAT_CURL_OFF_T "-", s->resume_from);
  1718. else
  1719. s->range = strdup(data->set.str[STRING_SET_RANGE]);
  1720. s->rangestringalloc = (s->range) ? TRUE : FALSE;
  1721. if(!s->range)
  1722. return CURLE_OUT_OF_MEMORY;
  1723. /* tell ourselves to fetch this range */
  1724. s->use_range = TRUE; /* enable range download */
  1725. }
  1726. else
  1727. s->use_range = FALSE; /* disable range download */
  1728. return CURLE_OK;
  1729. }
  1730. /*
  1731. * setup_connection_internals() -
  1732. *
  1733. * Setup connection internals specific to the requested protocol in the
  1734. * Curl_easy. This is inited and setup before the connection is made but
  1735. * is about the particular protocol that is to be used.
  1736. *
  1737. * This MUST get called after proxy magic has been figured out.
  1738. */
  1739. static CURLcode setup_connection_internals(struct connectdata *conn)
  1740. {
  1741. const struct Curl_handler *p;
  1742. CURLcode result;
  1743. /* Perform setup complement if some. */
  1744. p = conn->handler;
  1745. if(p->setup_connection) {
  1746. result = (*p->setup_connection)(conn);
  1747. if(result)
  1748. return result;
  1749. p = conn->handler; /* May have changed. */
  1750. }
  1751. if(conn->port < 0)
  1752. /* we check for -1 here since if proxy was detected already, this
  1753. was very likely already set to the proxy port */
  1754. conn->port = p->defport;
  1755. return CURLE_OK;
  1756. }
  1757. /*
  1758. * Curl_free_request_state() should free temp data that was allocated in the
  1759. * Curl_easy for this single request.
  1760. */
  1761. void Curl_free_request_state(struct Curl_easy *data)
  1762. {
  1763. Curl_safefree(data->req.protop);
  1764. Curl_safefree(data->req.newurl);
  1765. #ifndef CURL_DISABLE_DOH
  1766. Curl_close(&data->req.doh.probe[0].easy);
  1767. Curl_close(&data->req.doh.probe[1].easy);
  1768. #endif
  1769. }
  1770. #ifndef CURL_DISABLE_PROXY
  1771. /****************************************************************
  1772. * Checks if the host is in the noproxy list. returns true if it matches
  1773. * and therefore the proxy should NOT be used.
  1774. ****************************************************************/
  1775. static bool check_noproxy(const char *name, const char *no_proxy)
  1776. {
  1777. /* no_proxy=domain1.dom,host.domain2.dom
  1778. * (a comma-separated list of hosts which should
  1779. * not be proxied, or an asterisk to override
  1780. * all proxy variables)
  1781. */
  1782. if(no_proxy && no_proxy[0]) {
  1783. size_t tok_start;
  1784. size_t tok_end;
  1785. const char *separator = ", ";
  1786. size_t no_proxy_len;
  1787. size_t namelen;
  1788. char *endptr;
  1789. if(strcasecompare("*", no_proxy)) {
  1790. return TRUE;
  1791. }
  1792. /* NO_PROXY was specified and it wasn't just an asterisk */
  1793. no_proxy_len = strlen(no_proxy);
  1794. if(name[0] == '[') {
  1795. /* IPv6 numerical address */
  1796. endptr = strchr(name, ']');
  1797. if(!endptr)
  1798. return FALSE;
  1799. name++;
  1800. namelen = endptr - name;
  1801. }
  1802. else
  1803. namelen = strlen(name);
  1804. for(tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
  1805. while(tok_start < no_proxy_len &&
  1806. strchr(separator, no_proxy[tok_start]) != NULL) {
  1807. /* Look for the beginning of the token. */
  1808. ++tok_start;
  1809. }
  1810. if(tok_start == no_proxy_len)
  1811. break; /* It was all trailing separator chars, no more tokens. */
  1812. for(tok_end = tok_start; tok_end < no_proxy_len &&
  1813. strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end)
  1814. /* Look for the end of the token. */
  1815. ;
  1816. /* To match previous behaviour, where it was necessary to specify
  1817. * ".local.com" to prevent matching "notlocal.com", we will leave
  1818. * the '.' off.
  1819. */
  1820. if(no_proxy[tok_start] == '.')
  1821. ++tok_start;
  1822. if((tok_end - tok_start) <= namelen) {
  1823. /* Match the last part of the name to the domain we are checking. */
  1824. const char *checkn = name + namelen - (tok_end - tok_start);
  1825. if(strncasecompare(no_proxy + tok_start, checkn,
  1826. tok_end - tok_start)) {
  1827. if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
  1828. /* We either have an exact match, or the previous character is a .
  1829. * so it is within the same domain, so no proxy for this host.
  1830. */
  1831. return TRUE;
  1832. }
  1833. }
  1834. } /* if((tok_end - tok_start) <= namelen) */
  1835. } /* for(tok_start = 0; tok_start < no_proxy_len;
  1836. tok_start = tok_end + 1) */
  1837. } /* NO_PROXY was specified and it wasn't just an asterisk */
  1838. return FALSE;
  1839. }
  1840. #ifndef CURL_DISABLE_HTTP
  1841. /****************************************************************
  1842. * Detect what (if any) proxy to use. Remember that this selects a host
  1843. * name and is not limited to HTTP proxies only.
  1844. * The returned pointer must be freed by the caller (unless NULL)
  1845. ****************************************************************/
  1846. static char *detect_proxy(struct connectdata *conn)
  1847. {
  1848. char *proxy = NULL;
  1849. /* If proxy was not specified, we check for default proxy environment
  1850. * variables, to enable i.e Lynx compliance:
  1851. *
  1852. * http_proxy=http://some.server.dom:port/
  1853. * https_proxy=http://some.server.dom:port/
  1854. * ftp_proxy=http://some.server.dom:port/
  1855. * no_proxy=domain1.dom,host.domain2.dom
  1856. * (a comma-separated list of hosts which should
  1857. * not be proxied, or an asterisk to override
  1858. * all proxy variables)
  1859. * all_proxy=http://some.server.dom:port/
  1860. * (seems to exist for the CERN www lib. Probably
  1861. * the first to check for.)
  1862. *
  1863. * For compatibility, the all-uppercase versions of these variables are
  1864. * checked if the lowercase versions don't exist.
  1865. */
  1866. char proxy_env[128];
  1867. const char *protop = conn->handler->scheme;
  1868. char *envp = proxy_env;
  1869. char *prox;
  1870. /* Now, build <protocol>_proxy and check for such a one to use */
  1871. while(*protop)
  1872. *envp++ = (char)tolower((int)*protop++);
  1873. /* append _proxy */
  1874. strcpy(envp, "_proxy");
  1875. /* read the protocol proxy: */
  1876. prox = curl_getenv(proxy_env);
  1877. /*
  1878. * We don't try the uppercase version of HTTP_PROXY because of
  1879. * security reasons:
  1880. *
  1881. * When curl is used in a webserver application
  1882. * environment (cgi or php), this environment variable can
  1883. * be controlled by the web server user by setting the
  1884. * http header 'Proxy:' to some value.
  1885. *
  1886. * This can cause 'internal' http/ftp requests to be
  1887. * arbitrarily redirected by any external attacker.
  1888. */
  1889. if(!prox && !strcasecompare("http_proxy", proxy_env)) {
  1890. /* There was no lowercase variable, try the uppercase version: */
  1891. Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
  1892. prox = curl_getenv(proxy_env);
  1893. }
  1894. envp = proxy_env;
  1895. if(prox) {
  1896. proxy = prox; /* use this */
  1897. }
  1898. else {
  1899. envp = (char *)"all_proxy";
  1900. proxy = curl_getenv(envp); /* default proxy to use */
  1901. if(!proxy) {
  1902. envp = (char *)"ALL_PROXY";
  1903. proxy = curl_getenv(envp);
  1904. }
  1905. }
  1906. if(proxy)
  1907. infof(conn->data, "Uses proxy env variable %s == '%s'\n", envp, proxy);
  1908. return proxy;
  1909. }
  1910. #endif /* CURL_DISABLE_HTTP */
  1911. /*
  1912. * If this is supposed to use a proxy, we need to figure out the proxy
  1913. * host name, so that we can re-use an existing connection
  1914. * that may exist registered to the same proxy host.
  1915. */
  1916. static CURLcode parse_proxy(struct Curl_easy *data,
  1917. struct connectdata *conn, char *proxy,
  1918. curl_proxytype proxytype)
  1919. {
  1920. char *portptr = NULL;
  1921. long port = -1;
  1922. char *proxyuser = NULL;
  1923. char *proxypasswd = NULL;
  1924. char *host;
  1925. bool sockstype;
  1926. CURLUcode uc;
  1927. struct proxy_info *proxyinfo;
  1928. CURLU *uhp = curl_url();
  1929. CURLcode result = CURLE_OK;
  1930. char *scheme = NULL;
  1931. /* When parsing the proxy, allowing non-supported schemes since we have
  1932. these made up ones for proxies. Guess scheme for URLs without it. */
  1933. uc = curl_url_set(uhp, CURLUPART_URL, proxy,
  1934. CURLU_NON_SUPPORT_SCHEME|CURLU_GUESS_SCHEME);
  1935. if(!uc) {
  1936. /* parsed okay as a URL */
  1937. uc = curl_url_get(uhp, CURLUPART_SCHEME, &scheme, 0);
  1938. if(uc) {
  1939. result = CURLE_OUT_OF_MEMORY;
  1940. goto error;
  1941. }
  1942. if(strcasecompare("https", scheme))
  1943. proxytype = CURLPROXY_HTTPS;
  1944. else if(strcasecompare("socks5h", scheme))
  1945. proxytype = CURLPROXY_SOCKS5_HOSTNAME;
  1946. else if(strcasecompare("socks5", scheme))
  1947. proxytype = CURLPROXY_SOCKS5;
  1948. else if(strcasecompare("socks4a", scheme))
  1949. proxytype = CURLPROXY_SOCKS4A;
  1950. else if(strcasecompare("socks4", scheme) ||
  1951. strcasecompare("socks", scheme))
  1952. proxytype = CURLPROXY_SOCKS4;
  1953. else if(strcasecompare("http", scheme))
  1954. ; /* leave it as HTTP or HTTP/1.0 */
  1955. else {
  1956. /* Any other xxx:// reject! */
  1957. failf(data, "Unsupported proxy scheme for \'%s\'", proxy);
  1958. result = CURLE_COULDNT_CONNECT;
  1959. goto error;
  1960. }
  1961. }
  1962. else {
  1963. failf(data, "Unsupported proxy syntax in \'%s\'", proxy);
  1964. result = CURLE_COULDNT_RESOLVE_PROXY;
  1965. goto error;
  1966. }
  1967. #ifdef USE_SSL
  1968. if(!(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY))
  1969. #endif
  1970. if(proxytype == CURLPROXY_HTTPS) {
  1971. failf(data, "Unsupported proxy \'%s\', libcurl is built without the "
  1972. "HTTPS-proxy support.", proxy);
  1973. result = CURLE_NOT_BUILT_IN;
  1974. goto error;
  1975. }
  1976. sockstype =
  1977. proxytype == CURLPROXY_SOCKS5_HOSTNAME ||
  1978. proxytype == CURLPROXY_SOCKS5 ||
  1979. proxytype == CURLPROXY_SOCKS4A ||
  1980. proxytype == CURLPROXY_SOCKS4;
  1981. proxyinfo = sockstype ? &conn->socks_proxy : &conn->http_proxy;
  1982. proxyinfo->proxytype = proxytype;
  1983. /* Is there a username and password given in this proxy url? */
  1984. curl_url_get(uhp, CURLUPART_USER, &proxyuser, CURLU_URLDECODE);
  1985. curl_url_get(uhp, CURLUPART_PASSWORD, &proxypasswd, CURLU_URLDECODE);
  1986. if(proxyuser || proxypasswd) {
  1987. Curl_safefree(proxyinfo->user);
  1988. proxyinfo->user = proxyuser;
  1989. Curl_safefree(proxyinfo->passwd);
  1990. if(!proxypasswd) {
  1991. proxypasswd = strdup("");
  1992. if(!proxypasswd) {
  1993. result = CURLE_OUT_OF_MEMORY;
  1994. goto error;
  1995. }
  1996. }
  1997. proxyinfo->passwd = proxypasswd;
  1998. conn->bits.proxy_user_passwd = TRUE; /* enable it */
  1999. }
  2000. curl_url_get(uhp, CURLUPART_PORT, &portptr, 0);
  2001. if(portptr) {
  2002. port = strtol(portptr, NULL, 10);
  2003. free(portptr);
  2004. }
  2005. else {
  2006. if(data->set.proxyport)
  2007. /* None given in the proxy string, then get the default one if it is
  2008. given */
  2009. port = data->set.proxyport;
  2010. else {
  2011. if(proxytype == CURLPROXY_HTTPS)
  2012. port = CURL_DEFAULT_HTTPS_PROXY_PORT;
  2013. else
  2014. port = CURL_DEFAULT_PROXY_PORT;
  2015. }
  2016. }
  2017. if(port >= 0) {
  2018. proxyinfo->port = port;
  2019. if(conn->port < 0 || sockstype || !conn->socks_proxy.host.rawalloc)
  2020. conn->port = port;
  2021. }
  2022. /* now, clone the proxy host name */
  2023. uc = curl_url_get(uhp, CURLUPART_HOST, &host, CURLU_URLDECODE);
  2024. if(uc) {
  2025. result = CURLE_OUT_OF_MEMORY;
  2026. goto error;
  2027. }
  2028. Curl_safefree(proxyinfo->host.rawalloc);
  2029. proxyinfo->host.rawalloc = host;
  2030. if(host[0] == '[') {
  2031. /* this is a numerical IPv6, strip off the brackets */
  2032. size_t len = strlen(host);
  2033. host[len-1] = 0; /* clear the trailing bracket */
  2034. host++;
  2035. zonefrom_url(uhp, conn);
  2036. }
  2037. proxyinfo->host.name = host;
  2038. error:
  2039. free(scheme);
  2040. curl_url_cleanup(uhp);
  2041. return result;
  2042. }
  2043. /*
  2044. * Extract the user and password from the authentication string
  2045. */
  2046. static CURLcode parse_proxy_auth(struct Curl_easy *data,
  2047. struct connectdata *conn)
  2048. {
  2049. char proxyuser[MAX_CURL_USER_LENGTH]="";
  2050. char proxypasswd[MAX_CURL_PASSWORD_LENGTH]="";
  2051. CURLcode result;
  2052. if(data->set.str[STRING_PROXYUSERNAME] != NULL) {
  2053. strncpy(proxyuser, data->set.str[STRING_PROXYUSERNAME],
  2054. MAX_CURL_USER_LENGTH);
  2055. proxyuser[MAX_CURL_USER_LENGTH-1] = '\0'; /*To be on safe side*/
  2056. }
  2057. if(data->set.str[STRING_PROXYPASSWORD] != NULL) {
  2058. strncpy(proxypasswd, data->set.str[STRING_PROXYPASSWORD],
  2059. MAX_CURL_PASSWORD_LENGTH);
  2060. proxypasswd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
  2061. }
  2062. result = Curl_urldecode(data, proxyuser, 0, &conn->http_proxy.user, NULL,
  2063. FALSE);
  2064. if(!result)
  2065. result = Curl_urldecode(data, proxypasswd, 0, &conn->http_proxy.passwd,
  2066. NULL, FALSE);
  2067. return result;
  2068. }
  2069. /* create_conn helper to parse and init proxy values. to be called after unix
  2070. socket init but before any proxy vars are evaluated. */
  2071. static CURLcode create_conn_helper_init_proxy(struct connectdata *conn)
  2072. {
  2073. char *proxy = NULL;
  2074. char *socksproxy = NULL;
  2075. char *no_proxy = NULL;
  2076. CURLcode result = CURLE_OK;
  2077. struct Curl_easy *data = conn->data;
  2078. /*************************************************************
  2079. * Extract the user and password from the authentication string
  2080. *************************************************************/
  2081. if(conn->bits.proxy_user_passwd) {
  2082. result = parse_proxy_auth(data, conn);
  2083. if(result)
  2084. goto out;
  2085. }
  2086. /*************************************************************
  2087. * Detect what (if any) proxy to use
  2088. *************************************************************/
  2089. if(data->set.str[STRING_PROXY]) {
  2090. proxy = strdup(data->set.str[STRING_PROXY]);
  2091. /* if global proxy is set, this is it */
  2092. if(NULL == proxy) {
  2093. failf(data, "memory shortage");
  2094. result = CURLE_OUT_OF_MEMORY;
  2095. goto out;
  2096. }
  2097. }
  2098. if(data->set.str[STRING_PRE_PROXY]) {
  2099. socksproxy = strdup(data->set.str[STRING_PRE_PROXY]);
  2100. /* if global socks proxy is set, this is it */
  2101. if(NULL == socksproxy) {
  2102. failf(data, "memory shortage");
  2103. result = CURLE_OUT_OF_MEMORY;
  2104. goto out;
  2105. }
  2106. }
  2107. if(!data->set.str[STRING_NOPROXY]) {
  2108. const char *p = "no_proxy";
  2109. no_proxy = curl_getenv(p);
  2110. if(!no_proxy) {
  2111. p = "NO_PROXY";
  2112. no_proxy = curl_getenv(p);
  2113. }
  2114. if(no_proxy) {
  2115. infof(conn->data, "Uses proxy env variable %s == '%s'\n", p, no_proxy);
  2116. }
  2117. }
  2118. if(check_noproxy(conn->host.name, data->set.str[STRING_NOPROXY] ?
  2119. data->set.str[STRING_NOPROXY] : no_proxy)) {
  2120. Curl_safefree(proxy);
  2121. Curl_safefree(socksproxy);
  2122. }
  2123. #ifndef CURL_DISABLE_HTTP
  2124. else if(!proxy && !socksproxy)
  2125. /* if the host is not in the noproxy list, detect proxy. */
  2126. proxy = detect_proxy(conn);
  2127. #endif /* CURL_DISABLE_HTTP */
  2128. Curl_safefree(no_proxy);
  2129. #ifdef USE_UNIX_SOCKETS
  2130. /* For the time being do not mix proxy and unix domain sockets. See #1274 */
  2131. if(proxy && conn->unix_domain_socket) {
  2132. free(proxy);
  2133. proxy = NULL;
  2134. }
  2135. #endif
  2136. if(proxy && (!*proxy || (conn->handler->flags & PROTOPT_NONETWORK))) {
  2137. free(proxy); /* Don't bother with an empty proxy string or if the
  2138. protocol doesn't work with network */
  2139. proxy = NULL;
  2140. }
  2141. if(socksproxy && (!*socksproxy ||
  2142. (conn->handler->flags & PROTOPT_NONETWORK))) {
  2143. free(socksproxy); /* Don't bother with an empty socks proxy string or if
  2144. the protocol doesn't work with network */
  2145. socksproxy = NULL;
  2146. }
  2147. /***********************************************************************
  2148. * If this is supposed to use a proxy, we need to figure out the proxy host
  2149. * name, proxy type and port number, so that we can re-use an existing
  2150. * connection that may exist registered to the same proxy host.
  2151. ***********************************************************************/
  2152. if(proxy || socksproxy) {
  2153. if(proxy) {
  2154. result = parse_proxy(data, conn, proxy, conn->http_proxy.proxytype);
  2155. Curl_safefree(proxy); /* parse_proxy copies the proxy string */
  2156. if(result)
  2157. goto out;
  2158. }
  2159. if(socksproxy) {
  2160. result = parse_proxy(data, conn, socksproxy,
  2161. conn->socks_proxy.proxytype);
  2162. /* parse_proxy copies the socks proxy string */
  2163. Curl_safefree(socksproxy);
  2164. if(result)
  2165. goto out;
  2166. }
  2167. if(conn->http_proxy.host.rawalloc) {
  2168. #ifdef CURL_DISABLE_HTTP
  2169. /* asking for a HTTP proxy is a bit funny when HTTP is disabled... */
  2170. result = CURLE_UNSUPPORTED_PROTOCOL;
  2171. goto out;
  2172. #else
  2173. /* force this connection's protocol to become HTTP if compatible */
  2174. if(!(conn->handler->protocol & PROTO_FAMILY_HTTP)) {
  2175. if((conn->handler->flags & PROTOPT_PROXY_AS_HTTP) &&
  2176. !conn->bits.tunnel_proxy)
  2177. conn->handler = &Curl_handler_http;
  2178. else
  2179. /* if not converting to HTTP over the proxy, enforce tunneling */
  2180. conn->bits.tunnel_proxy = TRUE;
  2181. }
  2182. conn->bits.httpproxy = TRUE;
  2183. #endif
  2184. }
  2185. else {
  2186. conn->bits.httpproxy = FALSE; /* not a HTTP proxy */
  2187. conn->bits.tunnel_proxy = FALSE; /* no tunneling if not HTTP */
  2188. }
  2189. if(conn->socks_proxy.host.rawalloc) {
  2190. if(!conn->http_proxy.host.rawalloc) {
  2191. /* once a socks proxy */
  2192. if(!conn->socks_proxy.user) {
  2193. conn->socks_proxy.user = conn->http_proxy.user;
  2194. conn->http_proxy.user = NULL;
  2195. Curl_safefree(conn->socks_proxy.passwd);
  2196. conn->socks_proxy.passwd = conn->http_proxy.passwd;
  2197. conn->http_proxy.passwd = NULL;
  2198. }
  2199. }
  2200. conn->bits.socksproxy = TRUE;
  2201. }
  2202. else
  2203. conn->bits.socksproxy = FALSE; /* not a socks proxy */
  2204. }
  2205. else {
  2206. conn->bits.socksproxy = FALSE;
  2207. conn->bits.httpproxy = FALSE;
  2208. }
  2209. conn->bits.proxy = conn->bits.httpproxy || conn->bits.socksproxy;
  2210. if(!conn->bits.proxy) {
  2211. /* we aren't using the proxy after all... */
  2212. conn->bits.proxy = FALSE;
  2213. conn->bits.httpproxy = FALSE;
  2214. conn->bits.socksproxy = FALSE;
  2215. conn->bits.proxy_user_passwd = FALSE;
  2216. conn->bits.tunnel_proxy = FALSE;
  2217. }
  2218. out:
  2219. free(socksproxy);
  2220. free(proxy);
  2221. return result;
  2222. }
  2223. #endif /* CURL_DISABLE_PROXY */
  2224. /*
  2225. * Curl_parse_login_details()
  2226. *
  2227. * This is used to parse a login string for user name, password and options in
  2228. * the following formats:
  2229. *
  2230. * user
  2231. * user:password
  2232. * user:password;options
  2233. * user;options
  2234. * user;options:password
  2235. * :password
  2236. * :password;options
  2237. * ;options
  2238. * ;options:password
  2239. *
  2240. * Parameters:
  2241. *
  2242. * login [in] - The login string.
  2243. * len [in] - The length of the login string.
  2244. * userp [in/out] - The address where a pointer to newly allocated memory
  2245. * holding the user will be stored upon completion.
  2246. * passwdp [in/out] - The address where a pointer to newly allocated memory
  2247. * holding the password will be stored upon completion.
  2248. * optionsp [in/out] - The address where a pointer to newly allocated memory
  2249. * holding the options will be stored upon completion.
  2250. *
  2251. * Returns CURLE_OK on success.
  2252. */
  2253. CURLcode Curl_parse_login_details(const char *login, const size_t len,
  2254. char **userp, char **passwdp,
  2255. char **optionsp)
  2256. {
  2257. CURLcode result = CURLE_OK;
  2258. char *ubuf = NULL;
  2259. char *pbuf = NULL;
  2260. char *obuf = NULL;
  2261. const char *psep = NULL;
  2262. const char *osep = NULL;
  2263. size_t ulen;
  2264. size_t plen;
  2265. size_t olen;
  2266. /* the input length check is because this is called directcly from setopt
  2267. and isn't going through the regular string length check */
  2268. size_t llen = strlen(login);
  2269. if(llen > CURL_MAX_INPUT_LENGTH)
  2270. return CURLE_BAD_FUNCTION_ARGUMENT;
  2271. /* Attempt to find the password separator */
  2272. if(passwdp) {
  2273. psep = strchr(login, ':');
  2274. /* Within the constraint of the login string */
  2275. if(psep >= login + len)
  2276. psep = NULL;
  2277. }
  2278. /* Attempt to find the options separator */
  2279. if(optionsp) {
  2280. osep = strchr(login, ';');
  2281. /* Within the constraint of the login string */
  2282. if(osep >= login + len)
  2283. osep = NULL;
  2284. }
  2285. /* Calculate the portion lengths */
  2286. ulen = (psep ?
  2287. (size_t)(osep && psep > osep ? osep - login : psep - login) :
  2288. (osep ? (size_t)(osep - login) : len));
  2289. plen = (psep ?
  2290. (osep && osep > psep ? (size_t)(osep - psep) :
  2291. (size_t)(login + len - psep)) - 1 : 0);
  2292. olen = (osep ?
  2293. (psep && psep > osep ? (size_t)(psep - osep) :
  2294. (size_t)(login + len - osep)) - 1 : 0);
  2295. /* Allocate the user portion buffer */
  2296. if(userp && ulen) {
  2297. ubuf = malloc(ulen + 1);
  2298. if(!ubuf)
  2299. result = CURLE_OUT_OF_MEMORY;
  2300. }
  2301. /* Allocate the password portion buffer */
  2302. if(!result && passwdp && plen) {
  2303. pbuf = malloc(plen + 1);
  2304. if(!pbuf) {
  2305. free(ubuf);
  2306. result = CURLE_OUT_OF_MEMORY;
  2307. }
  2308. }
  2309. /* Allocate the options portion buffer */
  2310. if(!result && optionsp && olen) {
  2311. obuf = malloc(olen + 1);
  2312. if(!obuf) {
  2313. free(pbuf);
  2314. free(ubuf);
  2315. result = CURLE_OUT_OF_MEMORY;
  2316. }
  2317. }
  2318. if(!result) {
  2319. /* Store the user portion if necessary */
  2320. if(ubuf) {
  2321. memcpy(ubuf, login, ulen);
  2322. ubuf[ulen] = '\0';
  2323. Curl_safefree(*userp);
  2324. *userp = ubuf;
  2325. }
  2326. /* Store the password portion if necessary */
  2327. if(pbuf) {
  2328. memcpy(pbuf, psep + 1, plen);
  2329. pbuf[plen] = '\0';
  2330. Curl_safefree(*passwdp);
  2331. *passwdp = pbuf;
  2332. }
  2333. /* Store the options portion if necessary */
  2334. if(obuf) {
  2335. memcpy(obuf, osep + 1, olen);
  2336. obuf[olen] = '\0';
  2337. Curl_safefree(*optionsp);
  2338. *optionsp = obuf;
  2339. }
  2340. }
  2341. return result;
  2342. }
  2343. /*************************************************************
  2344. * Figure out the remote port number and fix it in the URL
  2345. *
  2346. * No matter if we use a proxy or not, we have to figure out the remote
  2347. * port number of various reasons.
  2348. *
  2349. * The port number embedded in the URL is replaced, if necessary.
  2350. *************************************************************/
  2351. static CURLcode parse_remote_port(struct Curl_easy *data,
  2352. struct connectdata *conn)
  2353. {
  2354. if(data->set.use_port && data->state.allow_port) {
  2355. /* if set, we use this instead of the port possibly given in the URL */
  2356. char portbuf[16];
  2357. CURLUcode uc;
  2358. conn->remote_port = (unsigned short)data->set.use_port;
  2359. msnprintf(portbuf, sizeof(portbuf), "%d", conn->remote_port);
  2360. uc = curl_url_set(data->state.uh, CURLUPART_PORT, portbuf, 0);
  2361. if(uc)
  2362. return CURLE_OUT_OF_MEMORY;
  2363. }
  2364. return CURLE_OK;
  2365. }
  2366. /*
  2367. * Override the login details from the URL with that in the CURLOPT_USERPWD
  2368. * option or a .netrc file, if applicable.
  2369. */
  2370. static CURLcode override_login(struct Curl_easy *data,
  2371. struct connectdata *conn,
  2372. char **userp, char **passwdp, char **optionsp)
  2373. {
  2374. bool user_changed = FALSE;
  2375. bool passwd_changed = FALSE;
  2376. CURLUcode uc;
  2377. if(data->set.use_netrc == CURL_NETRC_REQUIRED && conn->bits.user_passwd) {
  2378. /* ignore user+password in the URL */
  2379. if(*userp) {
  2380. Curl_safefree(*userp);
  2381. user_changed = TRUE;
  2382. }
  2383. if(*passwdp) {
  2384. Curl_safefree(*passwdp);
  2385. passwd_changed = TRUE;
  2386. }
  2387. conn->bits.user_passwd = FALSE; /* disable user+password */
  2388. }
  2389. if(data->set.str[STRING_USERNAME]) {
  2390. free(*userp);
  2391. *userp = strdup(data->set.str[STRING_USERNAME]);
  2392. if(!*userp)
  2393. return CURLE_OUT_OF_MEMORY;
  2394. conn->bits.user_passwd = TRUE; /* enable user+password */
  2395. user_changed = TRUE;
  2396. }
  2397. if(data->set.str[STRING_PASSWORD]) {
  2398. free(*passwdp);
  2399. *passwdp = strdup(data->set.str[STRING_PASSWORD]);
  2400. if(!*passwdp)
  2401. return CURLE_OUT_OF_MEMORY;
  2402. conn->bits.user_passwd = TRUE; /* enable user+password */
  2403. passwd_changed = TRUE;
  2404. }
  2405. if(data->set.str[STRING_OPTIONS]) {
  2406. free(*optionsp);
  2407. *optionsp = strdup(data->set.str[STRING_OPTIONS]);
  2408. if(!*optionsp)
  2409. return CURLE_OUT_OF_MEMORY;
  2410. }
  2411. conn->bits.netrc = FALSE;
  2412. if(data->set.use_netrc != CURL_NETRC_IGNORED &&
  2413. (!*userp || !**userp || !*passwdp || !**passwdp)) {
  2414. bool netrc_user_changed = FALSE;
  2415. bool netrc_passwd_changed = FALSE;
  2416. int ret;
  2417. ret = Curl_parsenetrc(conn->host.name,
  2418. userp, passwdp,
  2419. &netrc_user_changed, &netrc_passwd_changed,
  2420. data->set.str[STRING_NETRC_FILE]);
  2421. if(ret > 0) {
  2422. infof(data, "Couldn't find host %s in the .netrc file; using defaults\n",
  2423. conn->host.name);
  2424. }
  2425. else if(ret < 0) {
  2426. return CURLE_OUT_OF_MEMORY;
  2427. }
  2428. else {
  2429. /* set bits.netrc TRUE to remember that we got the name from a .netrc
  2430. file, so that it is safe to use even if we followed a Location: to a
  2431. different host or similar. */
  2432. conn->bits.netrc = TRUE;
  2433. conn->bits.user_passwd = TRUE; /* enable user+password */
  2434. if(netrc_user_changed) {
  2435. user_changed = TRUE;
  2436. }
  2437. if(netrc_passwd_changed) {
  2438. passwd_changed = TRUE;
  2439. }
  2440. }
  2441. }
  2442. /* for updated strings, we update them in the URL */
  2443. if(user_changed) {
  2444. uc = curl_url_set(data->state.uh, CURLUPART_USER, *userp,
  2445. CURLU_URLENCODE);
  2446. if(uc)
  2447. return Curl_uc_to_curlcode(uc);
  2448. }
  2449. if(passwd_changed) {
  2450. uc = curl_url_set(data->state.uh, CURLUPART_PASSWORD, *passwdp,
  2451. CURLU_URLENCODE);
  2452. if(uc)
  2453. return Curl_uc_to_curlcode(uc);
  2454. }
  2455. return CURLE_OK;
  2456. }
  2457. /*
  2458. * Set the login details so they're available in the connection
  2459. */
  2460. static CURLcode set_login(struct connectdata *conn)
  2461. {
  2462. CURLcode result = CURLE_OK;
  2463. const char *setuser = CURL_DEFAULT_USER;
  2464. const char *setpasswd = CURL_DEFAULT_PASSWORD;
  2465. /* If our protocol needs a password and we have none, use the defaults */
  2466. if((conn->handler->flags & PROTOPT_NEEDSPWD) && !conn->bits.user_passwd)
  2467. ;
  2468. else {
  2469. setuser = "";
  2470. setpasswd = "";
  2471. }
  2472. /* Store the default user */
  2473. if(!conn->user) {
  2474. conn->user = strdup(setuser);
  2475. if(!conn->user)
  2476. return CURLE_OUT_OF_MEMORY;
  2477. }
  2478. /* Store the default password */
  2479. if(!conn->passwd) {
  2480. conn->passwd = strdup(setpasswd);
  2481. if(!conn->passwd)
  2482. result = CURLE_OUT_OF_MEMORY;
  2483. }
  2484. return result;
  2485. }
  2486. /*
  2487. * Parses a "host:port" string to connect to.
  2488. * The hostname and the port may be empty; in this case, NULL is returned for
  2489. * the hostname and -1 for the port.
  2490. */
  2491. static CURLcode parse_connect_to_host_port(struct Curl_easy *data,
  2492. const char *host,
  2493. char **hostname_result,
  2494. int *port_result)
  2495. {
  2496. char *host_dup;
  2497. char *hostptr;
  2498. char *host_portno;
  2499. char *portptr;
  2500. int port = -1;
  2501. #if defined(CURL_DISABLE_VERBOSE_STRINGS)
  2502. (void) data;
  2503. #endif
  2504. *hostname_result = NULL;
  2505. *port_result = -1;
  2506. if(!host || !*host)
  2507. return CURLE_OK;
  2508. host_dup = strdup(host);
  2509. if(!host_dup)
  2510. return CURLE_OUT_OF_MEMORY;
  2511. hostptr = host_dup;
  2512. /* start scanning for port number at this point */
  2513. portptr = hostptr;
  2514. /* detect and extract RFC6874-style IPv6-addresses */
  2515. if(*hostptr == '[') {
  2516. #ifdef ENABLE_IPV6
  2517. char *ptr = ++hostptr; /* advance beyond the initial bracket */
  2518. while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '.')))
  2519. ptr++;
  2520. if(*ptr == '%') {
  2521. /* There might be a zone identifier */
  2522. if(strncmp("%25", ptr, 3))
  2523. infof(data, "Please URL encode %% as %%25, see RFC 6874.\n");
  2524. ptr++;
  2525. /* Allow unreserved characters as defined in RFC 3986 */
  2526. while(*ptr && (ISALPHA(*ptr) || ISXDIGIT(*ptr) || (*ptr == '-') ||
  2527. (*ptr == '.') || (*ptr == '_') || (*ptr == '~')))
  2528. ptr++;
  2529. }
  2530. if(*ptr == ']')
  2531. /* yeps, it ended nicely with a bracket as well */
  2532. *ptr++ = '\0';
  2533. else
  2534. infof(data, "Invalid IPv6 address format\n");
  2535. portptr = ptr;
  2536. /* Note that if this didn't end with a bracket, we still advanced the
  2537. * hostptr first, but I can't see anything wrong with that as no host
  2538. * name nor a numeric can legally start with a bracket.
  2539. */
  2540. #else
  2541. failf(data, "Use of IPv6 in *_CONNECT_TO without IPv6 support built-in!");
  2542. free(host_dup);
  2543. return CURLE_NOT_BUILT_IN;
  2544. #endif
  2545. }
  2546. /* Get port number off server.com:1080 */
  2547. host_portno = strchr(portptr, ':');
  2548. if(host_portno) {
  2549. char *endp = NULL;
  2550. *host_portno = '\0'; /* cut off number from host name */
  2551. host_portno++;
  2552. if(*host_portno) {
  2553. long portparse = strtol(host_portno, &endp, 10);
  2554. if((endp && *endp) || (portparse < 0) || (portparse > 65535)) {
  2555. infof(data, "No valid port number in connect to host string (%s)\n",
  2556. host_portno);
  2557. hostptr = NULL;
  2558. port = -1;
  2559. }
  2560. else
  2561. port = (int)portparse; /* we know it will fit */
  2562. }
  2563. }
  2564. /* now, clone the cleaned host name */
  2565. if(hostptr) {
  2566. *hostname_result = strdup(hostptr);
  2567. if(!*hostname_result) {
  2568. free(host_dup);
  2569. return CURLE_OUT_OF_MEMORY;
  2570. }
  2571. }
  2572. *port_result = port;
  2573. free(host_dup);
  2574. return CURLE_OK;
  2575. }
  2576. /*
  2577. * Parses one "connect to" string in the form:
  2578. * "HOST:PORT:CONNECT-TO-HOST:CONNECT-TO-PORT".
  2579. */
  2580. static CURLcode parse_connect_to_string(struct Curl_easy *data,
  2581. struct connectdata *conn,
  2582. const char *conn_to_host,
  2583. char **host_result,
  2584. int *port_result)
  2585. {
  2586. CURLcode result = CURLE_OK;
  2587. const char *ptr = conn_to_host;
  2588. int host_match = FALSE;
  2589. int port_match = FALSE;
  2590. *host_result = NULL;
  2591. *port_result = -1;
  2592. if(*ptr == ':') {
  2593. /* an empty hostname always matches */
  2594. host_match = TRUE;
  2595. ptr++;
  2596. }
  2597. else {
  2598. /* check whether the URL's hostname matches */
  2599. size_t hostname_to_match_len;
  2600. char *hostname_to_match = aprintf("%s%s%s",
  2601. conn->bits.ipv6_ip ? "[" : "",
  2602. conn->host.name,
  2603. conn->bits.ipv6_ip ? "]" : "");
  2604. if(!hostname_to_match)
  2605. return CURLE_OUT_OF_MEMORY;
  2606. hostname_to_match_len = strlen(hostname_to_match);
  2607. host_match = strncasecompare(ptr, hostname_to_match,
  2608. hostname_to_match_len);
  2609. free(hostname_to_match);
  2610. ptr += hostname_to_match_len;
  2611. host_match = host_match && *ptr == ':';
  2612. ptr++;
  2613. }
  2614. if(host_match) {
  2615. if(*ptr == ':') {
  2616. /* an empty port always matches */
  2617. port_match = TRUE;
  2618. ptr++;
  2619. }
  2620. else {
  2621. /* check whether the URL's port matches */
  2622. char *ptr_next = strchr(ptr, ':');
  2623. if(ptr_next) {
  2624. char *endp = NULL;
  2625. long port_to_match = strtol(ptr, &endp, 10);
  2626. if((endp == ptr_next) && (port_to_match == conn->remote_port)) {
  2627. port_match = TRUE;
  2628. ptr = ptr_next + 1;
  2629. }
  2630. }
  2631. }
  2632. }
  2633. if(host_match && port_match) {
  2634. /* parse the hostname and port to connect to */
  2635. result = parse_connect_to_host_port(data, ptr, host_result, port_result);
  2636. }
  2637. return result;
  2638. }
  2639. /*
  2640. * Processes all strings in the "connect to" slist, and uses the "connect
  2641. * to host" and "connect to port" of the first string that matches.
  2642. */
  2643. static CURLcode parse_connect_to_slist(struct Curl_easy *data,
  2644. struct connectdata *conn,
  2645. struct curl_slist *conn_to_host)
  2646. {
  2647. CURLcode result = CURLE_OK;
  2648. char *host = NULL;
  2649. int port = -1;
  2650. while(conn_to_host && !host && port == -1) {
  2651. result = parse_connect_to_string(data, conn, conn_to_host->data,
  2652. &host, &port);
  2653. if(result)
  2654. return result;
  2655. if(host && *host) {
  2656. conn->conn_to_host.rawalloc = host;
  2657. conn->conn_to_host.name = host;
  2658. conn->bits.conn_to_host = TRUE;
  2659. infof(data, "Connecting to hostname: %s\n", host);
  2660. }
  2661. else {
  2662. /* no "connect to host" */
  2663. conn->bits.conn_to_host = FALSE;
  2664. Curl_safefree(host);
  2665. }
  2666. if(port >= 0) {
  2667. conn->conn_to_port = port;
  2668. conn->bits.conn_to_port = TRUE;
  2669. infof(data, "Connecting to port: %d\n", port);
  2670. }
  2671. else {
  2672. /* no "connect to port" */
  2673. conn->bits.conn_to_port = FALSE;
  2674. port = -1;
  2675. }
  2676. conn_to_host = conn_to_host->next;
  2677. }
  2678. #ifdef USE_ALTSVC
  2679. if(data->asi && !host && (port == -1) &&
  2680. ((conn->handler->protocol == CURLPROTO_HTTPS) ||
  2681. #ifdef CURLDEBUG
  2682. /* allow debug builds to circumvent the HTTPS restriction */
  2683. getenv("CURL_ALTSVC_HTTP")
  2684. #else
  2685. 0
  2686. #endif
  2687. )) {
  2688. /* no connect_to match, try alt-svc! */
  2689. enum alpnid srcalpnid;
  2690. bool hit;
  2691. struct altsvc *as;
  2692. const int allowed_versions = ( ALPN_h1
  2693. #ifdef USE_NGHTTP2
  2694. | ALPN_h2
  2695. #endif
  2696. #ifdef ENABLE_QUIC
  2697. | ALPN_h3
  2698. #endif
  2699. ) & data->asi->flags;
  2700. host = conn->host.rawalloc;
  2701. #ifdef USE_NGHTTP2
  2702. /* with h2 support, check that first */
  2703. srcalpnid = ALPN_h2;
  2704. hit = Curl_altsvc_lookup(data->asi,
  2705. srcalpnid, host, conn->remote_port, /* from */
  2706. &as /* to */,
  2707. allowed_versions);
  2708. if(!hit)
  2709. #endif
  2710. {
  2711. srcalpnid = ALPN_h1;
  2712. hit = Curl_altsvc_lookup(data->asi,
  2713. srcalpnid, host, conn->remote_port, /* from */
  2714. &as /* to */,
  2715. allowed_versions);
  2716. }
  2717. if(hit) {
  2718. char *hostd = strdup((char *)as->dst.host);
  2719. if(!hostd)
  2720. return CURLE_OUT_OF_MEMORY;
  2721. conn->conn_to_host.rawalloc = hostd;
  2722. conn->conn_to_host.name = hostd;
  2723. conn->bits.conn_to_host = TRUE;
  2724. conn->conn_to_port = as->dst.port;
  2725. conn->bits.conn_to_port = TRUE;
  2726. conn->bits.altused = TRUE;
  2727. infof(data, "Alt-svc connecting from [%s]%s:%d to [%s]%s:%d\n",
  2728. Curl_alpnid2str(srcalpnid), host, conn->remote_port,
  2729. Curl_alpnid2str(as->dst.alpnid), hostd, as->dst.port);
  2730. if(srcalpnid != as->dst.alpnid) {
  2731. /* protocol version switch */
  2732. switch(as->dst.alpnid) {
  2733. case ALPN_h1:
  2734. conn->httpversion = 11;
  2735. break;
  2736. case ALPN_h2:
  2737. conn->httpversion = 20;
  2738. break;
  2739. case ALPN_h3:
  2740. conn->transport = TRNSPRT_QUIC;
  2741. conn->httpversion = 30;
  2742. break;
  2743. default: /* shouldn't be possible */
  2744. break;
  2745. }
  2746. }
  2747. }
  2748. }
  2749. #endif
  2750. return result;
  2751. }
  2752. /*************************************************************
  2753. * Resolve the address of the server or proxy
  2754. *************************************************************/
  2755. static CURLcode resolve_server(struct Curl_easy *data,
  2756. struct connectdata *conn,
  2757. bool *async)
  2758. {
  2759. CURLcode result = CURLE_OK;
  2760. timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  2761. DEBUGASSERT(conn);
  2762. DEBUGASSERT(data);
  2763. /*************************************************************
  2764. * Resolve the name of the server or proxy
  2765. *************************************************************/
  2766. if(conn->bits.reuse)
  2767. /* We're reusing the connection - no need to resolve anything, and
  2768. idnconvert_hostname() was called already in create_conn() for the re-use
  2769. case. */
  2770. *async = FALSE;
  2771. else {
  2772. /* this is a fresh connect */
  2773. int rc;
  2774. struct Curl_dns_entry *hostaddr;
  2775. #ifdef USE_UNIX_SOCKETS
  2776. if(conn->unix_domain_socket) {
  2777. /* Unix domain sockets are local. The host gets ignored, just use the
  2778. * specified domain socket address. Do not cache "DNS entries". There is
  2779. * no DNS involved and we already have the filesystem path available */
  2780. const char *path = conn->unix_domain_socket;
  2781. hostaddr = calloc(1, sizeof(struct Curl_dns_entry));
  2782. if(!hostaddr)
  2783. result = CURLE_OUT_OF_MEMORY;
  2784. else {
  2785. bool longpath = FALSE;
  2786. hostaddr->addr = Curl_unix2addr(path, &longpath,
  2787. conn->abstract_unix_socket);
  2788. if(hostaddr->addr)
  2789. hostaddr->inuse++;
  2790. else {
  2791. /* Long paths are not supported for now */
  2792. if(longpath) {
  2793. failf(data, "Unix socket path too long: '%s'", path);
  2794. result = CURLE_COULDNT_RESOLVE_HOST;
  2795. }
  2796. else
  2797. result = CURLE_OUT_OF_MEMORY;
  2798. free(hostaddr);
  2799. hostaddr = NULL;
  2800. }
  2801. }
  2802. }
  2803. else
  2804. #endif
  2805. if(!conn->bits.proxy) {
  2806. struct hostname *connhost;
  2807. if(conn->bits.conn_to_host)
  2808. connhost = &conn->conn_to_host;
  2809. else
  2810. connhost = &conn->host;
  2811. /* If not connecting via a proxy, extract the port from the URL, if it is
  2812. * there, thus overriding any defaults that might have been set above. */
  2813. if(conn->bits.conn_to_port)
  2814. conn->port = conn->conn_to_port;
  2815. else
  2816. conn->port = conn->remote_port;
  2817. /* Resolve target host right on */
  2818. conn->hostname_resolve = strdup(connhost->name);
  2819. if(!conn->hostname_resolve)
  2820. return CURLE_OUT_OF_MEMORY;
  2821. rc = Curl_resolv_timeout(conn, conn->hostname_resolve, (int)conn->port,
  2822. &hostaddr, timeout_ms);
  2823. if(rc == CURLRESOLV_PENDING)
  2824. *async = TRUE;
  2825. else if(rc == CURLRESOLV_TIMEDOUT)
  2826. result = CURLE_OPERATION_TIMEDOUT;
  2827. else if(!hostaddr) {
  2828. failf(data, "Couldn't resolve host '%s'", connhost->dispname);
  2829. result = CURLE_COULDNT_RESOLVE_HOST;
  2830. /* don't return yet, we need to clean up the timeout first */
  2831. }
  2832. }
  2833. else {
  2834. /* This is a proxy that hasn't been resolved yet. */
  2835. struct hostname * const host = conn->bits.socksproxy ?
  2836. &conn->socks_proxy.host : &conn->http_proxy.host;
  2837. /* resolve proxy */
  2838. conn->hostname_resolve = strdup(host->name);
  2839. if(!conn->hostname_resolve)
  2840. return CURLE_OUT_OF_MEMORY;
  2841. rc = Curl_resolv_timeout(conn, conn->hostname_resolve, (int)conn->port,
  2842. &hostaddr, timeout_ms);
  2843. if(rc == CURLRESOLV_PENDING)
  2844. *async = TRUE;
  2845. else if(rc == CURLRESOLV_TIMEDOUT)
  2846. result = CURLE_OPERATION_TIMEDOUT;
  2847. else if(!hostaddr) {
  2848. failf(data, "Couldn't resolve proxy '%s'", host->dispname);
  2849. result = CURLE_COULDNT_RESOLVE_PROXY;
  2850. /* don't return yet, we need to clean up the timeout first */
  2851. }
  2852. }
  2853. DEBUGASSERT(conn->dns_entry == NULL);
  2854. conn->dns_entry = hostaddr;
  2855. }
  2856. return result;
  2857. }
  2858. /*
  2859. * Cleanup the connection just allocated before we can move along and use the
  2860. * previously existing one. All relevant data is copied over and old_conn is
  2861. * ready for freeing once this function returns.
  2862. */
  2863. static void reuse_conn(struct connectdata *old_conn,
  2864. struct connectdata *conn)
  2865. {
  2866. Curl_free_idnconverted_hostname(&old_conn->http_proxy.host);
  2867. Curl_free_idnconverted_hostname(&old_conn->socks_proxy.host);
  2868. free(old_conn->http_proxy.host.rawalloc);
  2869. free(old_conn->socks_proxy.host.rawalloc);
  2870. /* free the SSL config struct from this connection struct as this was
  2871. allocated in vain and is targeted for destruction */
  2872. Curl_free_primary_ssl_config(&old_conn->ssl_config);
  2873. Curl_free_primary_ssl_config(&old_conn->proxy_ssl_config);
  2874. conn->data = old_conn->data;
  2875. /* get the user+password information from the old_conn struct since it may
  2876. * be new for this request even when we re-use an existing connection */
  2877. conn->bits.user_passwd = old_conn->bits.user_passwd;
  2878. if(conn->bits.user_passwd) {
  2879. /* use the new user name and password though */
  2880. Curl_safefree(conn->user);
  2881. Curl_safefree(conn->passwd);
  2882. conn->user = old_conn->user;
  2883. conn->passwd = old_conn->passwd;
  2884. old_conn->user = NULL;
  2885. old_conn->passwd = NULL;
  2886. }
  2887. conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
  2888. if(conn->bits.proxy_user_passwd) {
  2889. /* use the new proxy user name and proxy password though */
  2890. Curl_safefree(conn->http_proxy.user);
  2891. Curl_safefree(conn->socks_proxy.user);
  2892. Curl_safefree(conn->http_proxy.passwd);
  2893. Curl_safefree(conn->socks_proxy.passwd);
  2894. conn->http_proxy.user = old_conn->http_proxy.user;
  2895. conn->socks_proxy.user = old_conn->socks_proxy.user;
  2896. conn->http_proxy.passwd = old_conn->http_proxy.passwd;
  2897. conn->socks_proxy.passwd = old_conn->socks_proxy.passwd;
  2898. old_conn->http_proxy.user = NULL;
  2899. old_conn->socks_proxy.user = NULL;
  2900. old_conn->http_proxy.passwd = NULL;
  2901. old_conn->socks_proxy.passwd = NULL;
  2902. }
  2903. /* host can change, when doing keepalive with a proxy or if the case is
  2904. different this time etc */
  2905. Curl_free_idnconverted_hostname(&conn->host);
  2906. Curl_free_idnconverted_hostname(&conn->conn_to_host);
  2907. Curl_safefree(conn->host.rawalloc);
  2908. Curl_safefree(conn->conn_to_host.rawalloc);
  2909. conn->host = old_conn->host;
  2910. conn->conn_to_host = old_conn->conn_to_host;
  2911. conn->conn_to_port = old_conn->conn_to_port;
  2912. conn->remote_port = old_conn->remote_port;
  2913. Curl_safefree(conn->hostname_resolve);
  2914. conn->hostname_resolve = old_conn->hostname_resolve;
  2915. old_conn->hostname_resolve = NULL;
  2916. /* persist connection info in session handle */
  2917. Curl_persistconninfo(conn);
  2918. conn_reset_all_postponed_data(old_conn); /* free buffers */
  2919. /* re-use init */
  2920. conn->bits.reuse = TRUE; /* yes, we're re-using here */
  2921. Curl_safefree(old_conn->user);
  2922. Curl_safefree(old_conn->passwd);
  2923. Curl_safefree(old_conn->options);
  2924. Curl_safefree(old_conn->http_proxy.user);
  2925. Curl_safefree(old_conn->socks_proxy.user);
  2926. Curl_safefree(old_conn->http_proxy.passwd);
  2927. Curl_safefree(old_conn->socks_proxy.passwd);
  2928. Curl_safefree(old_conn->localdev);
  2929. Curl_llist_destroy(&old_conn->easyq, NULL);
  2930. #ifdef USE_UNIX_SOCKETS
  2931. Curl_safefree(old_conn->unix_domain_socket);
  2932. #endif
  2933. }
  2934. /**
  2935. * create_conn() sets up a new connectdata struct, or re-uses an already
  2936. * existing one, and resolves host name.
  2937. *
  2938. * if this function returns CURLE_OK and *async is set to TRUE, the resolve
  2939. * response will be coming asynchronously. If *async is FALSE, the name is
  2940. * already resolved.
  2941. *
  2942. * @param data The sessionhandle pointer
  2943. * @param in_connect is set to the next connection data pointer
  2944. * @param async is set TRUE when an async DNS resolution is pending
  2945. * @see Curl_setup_conn()
  2946. *
  2947. * *NOTE* this function assigns the conn->data pointer!
  2948. */
  2949. static CURLcode create_conn(struct Curl_easy *data,
  2950. struct connectdata **in_connect,
  2951. bool *async)
  2952. {
  2953. CURLcode result = CURLE_OK;
  2954. struct connectdata *conn;
  2955. struct connectdata *conn_temp = NULL;
  2956. bool reuse;
  2957. bool connections_available = TRUE;
  2958. bool force_reuse = FALSE;
  2959. bool waitpipe = FALSE;
  2960. size_t max_host_connections = Curl_multi_max_host_connections(data->multi);
  2961. size_t max_total_connections = Curl_multi_max_total_connections(data->multi);
  2962. *async = FALSE;
  2963. *in_connect = NULL;
  2964. /*************************************************************
  2965. * Check input data
  2966. *************************************************************/
  2967. if(!data->change.url) {
  2968. result = CURLE_URL_MALFORMAT;
  2969. goto out;
  2970. }
  2971. /* First, split up the current URL in parts so that we can use the
  2972. parts for checking against the already present connections. In order
  2973. to not have to modify everything at once, we allocate a temporary
  2974. connection data struct and fill in for comparison purposes. */
  2975. conn = allocate_conn(data);
  2976. if(!conn) {
  2977. result = CURLE_OUT_OF_MEMORY;
  2978. goto out;
  2979. }
  2980. /* We must set the return variable as soon as possible, so that our
  2981. parent can cleanup any possible allocs we may have done before
  2982. any failure */
  2983. *in_connect = conn;
  2984. result = parseurlandfillconn(data, conn);
  2985. if(result)
  2986. goto out;
  2987. if(data->set.str[STRING_SASL_AUTHZID]) {
  2988. conn->sasl_authzid = strdup(data->set.str[STRING_SASL_AUTHZID]);
  2989. if(!conn->sasl_authzid) {
  2990. result = CURLE_OUT_OF_MEMORY;
  2991. goto out;
  2992. }
  2993. }
  2994. #ifdef USE_UNIX_SOCKETS
  2995. if(data->set.str[STRING_UNIX_SOCKET_PATH]) {
  2996. conn->unix_domain_socket = strdup(data->set.str[STRING_UNIX_SOCKET_PATH]);
  2997. if(conn->unix_domain_socket == NULL) {
  2998. result = CURLE_OUT_OF_MEMORY;
  2999. goto out;
  3000. }
  3001. conn->abstract_unix_socket = data->set.abstract_unix_socket;
  3002. }
  3003. #endif
  3004. /* After the unix socket init but before the proxy vars are used, parse and
  3005. initialize the proxy vars */
  3006. #ifndef CURL_DISABLE_PROXY
  3007. result = create_conn_helper_init_proxy(conn);
  3008. if(result)
  3009. goto out;
  3010. #endif
  3011. /*************************************************************
  3012. * If the protocol is using SSL and HTTP proxy is used, we set
  3013. * the tunnel_proxy bit.
  3014. *************************************************************/
  3015. if((conn->given->flags&PROTOPT_SSL) && conn->bits.httpproxy)
  3016. conn->bits.tunnel_proxy = TRUE;
  3017. /*************************************************************
  3018. * Figure out the remote port number and fix it in the URL
  3019. *************************************************************/
  3020. result = parse_remote_port(data, conn);
  3021. if(result)
  3022. goto out;
  3023. /* Check for overridden login details and set them accordingly so they
  3024. they are known when protocol->setup_connection is called! */
  3025. result = override_login(data, conn, &conn->user, &conn->passwd,
  3026. &conn->options);
  3027. if(result)
  3028. goto out;
  3029. result = set_login(conn); /* default credentials */
  3030. if(result)
  3031. goto out;
  3032. /*************************************************************
  3033. * Process the "connect to" linked list of hostname/port mappings.
  3034. * Do this after the remote port number has been fixed in the URL.
  3035. *************************************************************/
  3036. result = parse_connect_to_slist(data, conn, data->set.connect_to);
  3037. if(result)
  3038. goto out;
  3039. /*************************************************************
  3040. * IDN-convert the hostnames
  3041. *************************************************************/
  3042. result = Curl_idnconvert_hostname(conn, &conn->host);
  3043. if(result)
  3044. goto out;
  3045. if(conn->bits.conn_to_host) {
  3046. result = Curl_idnconvert_hostname(conn, &conn->conn_to_host);
  3047. if(result)
  3048. goto out;
  3049. }
  3050. if(conn->bits.httpproxy) {
  3051. result = Curl_idnconvert_hostname(conn, &conn->http_proxy.host);
  3052. if(result)
  3053. goto out;
  3054. }
  3055. if(conn->bits.socksproxy) {
  3056. result = Curl_idnconvert_hostname(conn, &conn->socks_proxy.host);
  3057. if(result)
  3058. goto out;
  3059. }
  3060. /*************************************************************
  3061. * Check whether the host and the "connect to host" are equal.
  3062. * Do this after the hostnames have been IDN-converted.
  3063. *************************************************************/
  3064. if(conn->bits.conn_to_host &&
  3065. strcasecompare(conn->conn_to_host.name, conn->host.name)) {
  3066. conn->bits.conn_to_host = FALSE;
  3067. }
  3068. /*************************************************************
  3069. * Check whether the port and the "connect to port" are equal.
  3070. * Do this after the remote port number has been fixed in the URL.
  3071. *************************************************************/
  3072. if(conn->bits.conn_to_port && conn->conn_to_port == conn->remote_port) {
  3073. conn->bits.conn_to_port = FALSE;
  3074. }
  3075. /*************************************************************
  3076. * If the "connect to" feature is used with an HTTP proxy,
  3077. * we set the tunnel_proxy bit.
  3078. *************************************************************/
  3079. if((conn->bits.conn_to_host || conn->bits.conn_to_port) &&
  3080. conn->bits.httpproxy)
  3081. conn->bits.tunnel_proxy = TRUE;
  3082. /*************************************************************
  3083. * Setup internals depending on protocol. Needs to be done after
  3084. * we figured out what/if proxy to use.
  3085. *************************************************************/
  3086. result = setup_connection_internals(conn);
  3087. if(result)
  3088. goto out;
  3089. conn->recv[FIRSTSOCKET] = Curl_recv_plain;
  3090. conn->send[FIRSTSOCKET] = Curl_send_plain;
  3091. conn->recv[SECONDARYSOCKET] = Curl_recv_plain;
  3092. conn->send[SECONDARYSOCKET] = Curl_send_plain;
  3093. conn->bits.tcp_fastopen = data->set.tcp_fastopen;
  3094. /***********************************************************************
  3095. * file: is a special case in that it doesn't need a network connection
  3096. ***********************************************************************/
  3097. #ifndef CURL_DISABLE_FILE
  3098. if(conn->handler->flags & PROTOPT_NONETWORK) {
  3099. bool done;
  3100. /* this is supposed to be the connect function so we better at least check
  3101. that the file is present here! */
  3102. DEBUGASSERT(conn->handler->connect_it);
  3103. Curl_persistconninfo(conn);
  3104. result = conn->handler->connect_it(conn, &done);
  3105. /* Setup a "faked" transfer that'll do nothing */
  3106. if(!result) {
  3107. conn->bits.tcpconnect[FIRSTSOCKET] = TRUE; /* we are "connected */
  3108. Curl_attach_connnection(data, conn);
  3109. result = Curl_conncache_add_conn(data->state.conn_cache, conn);
  3110. if(result)
  3111. goto out;
  3112. /*
  3113. * Setup whatever necessary for a resumed transfer
  3114. */
  3115. result = setup_range(data);
  3116. if(result) {
  3117. DEBUGASSERT(conn->handler->done);
  3118. /* we ignore the return code for the protocol-specific DONE */
  3119. (void)conn->handler->done(conn, result, FALSE);
  3120. goto out;
  3121. }
  3122. Curl_setup_transfer(data, -1, -1, FALSE, -1);
  3123. }
  3124. /* since we skip do_init() */
  3125. Curl_init_do(data, conn);
  3126. goto out;
  3127. }
  3128. #endif
  3129. /* Get a cloned copy of the SSL config situation stored in the
  3130. connection struct. But to get this going nicely, we must first make
  3131. sure that the strings in the master copy are pointing to the correct
  3132. strings in the session handle strings array!
  3133. Keep in mind that the pointers in the master copy are pointing to strings
  3134. that will be freed as part of the Curl_easy struct, but all cloned
  3135. copies will be separately allocated.
  3136. */
  3137. data->set.ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH_ORIG];
  3138. data->set.proxy_ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH_PROXY];
  3139. data->set.ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE_ORIG];
  3140. data->set.proxy_ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE_PROXY];
  3141. data->set.ssl.primary.random_file = data->set.str[STRING_SSL_RANDOM_FILE];
  3142. data->set.proxy_ssl.primary.random_file =
  3143. data->set.str[STRING_SSL_RANDOM_FILE];
  3144. data->set.ssl.primary.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
  3145. data->set.proxy_ssl.primary.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
  3146. data->set.ssl.primary.cipher_list =
  3147. data->set.str[STRING_SSL_CIPHER_LIST_ORIG];
  3148. data->set.proxy_ssl.primary.cipher_list =
  3149. data->set.str[STRING_SSL_CIPHER_LIST_PROXY];
  3150. data->set.ssl.primary.cipher_list13 =
  3151. data->set.str[STRING_SSL_CIPHER13_LIST_ORIG];
  3152. data->set.proxy_ssl.primary.cipher_list13 =
  3153. data->set.str[STRING_SSL_CIPHER13_LIST_PROXY];
  3154. data->set.ssl.primary.pinned_key =
  3155. data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
  3156. data->set.proxy_ssl.primary.pinned_key =
  3157. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY];
  3158. data->set.ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE_ORIG];
  3159. data->set.proxy_ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE_PROXY];
  3160. data->set.ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT_ORIG];
  3161. data->set.proxy_ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT_PROXY];
  3162. data->set.ssl.cert = data->set.str[STRING_CERT_ORIG];
  3163. data->set.proxy_ssl.cert = data->set.str[STRING_CERT_PROXY];
  3164. data->set.ssl.cert_type = data->set.str[STRING_CERT_TYPE_ORIG];
  3165. data->set.proxy_ssl.cert_type = data->set.str[STRING_CERT_TYPE_PROXY];
  3166. data->set.ssl.key = data->set.str[STRING_KEY_ORIG];
  3167. data->set.proxy_ssl.key = data->set.str[STRING_KEY_PROXY];
  3168. data->set.ssl.key_type = data->set.str[STRING_KEY_TYPE_ORIG];
  3169. data->set.proxy_ssl.key_type = data->set.str[STRING_KEY_TYPE_PROXY];
  3170. data->set.ssl.key_passwd = data->set.str[STRING_KEY_PASSWD_ORIG];
  3171. data->set.proxy_ssl.key_passwd = data->set.str[STRING_KEY_PASSWD_PROXY];
  3172. data->set.ssl.primary.clientcert = data->set.str[STRING_CERT_ORIG];
  3173. data->set.proxy_ssl.primary.clientcert = data->set.str[STRING_CERT_PROXY];
  3174. #ifdef USE_TLS_SRP
  3175. data->set.ssl.username = data->set.str[STRING_TLSAUTH_USERNAME_ORIG];
  3176. data->set.proxy_ssl.username = data->set.str[STRING_TLSAUTH_USERNAME_PROXY];
  3177. data->set.ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD_ORIG];
  3178. data->set.proxy_ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD_PROXY];
  3179. #endif
  3180. if(!Curl_clone_primary_ssl_config(&data->set.ssl.primary,
  3181. &conn->ssl_config)) {
  3182. result = CURLE_OUT_OF_MEMORY;
  3183. goto out;
  3184. }
  3185. if(!Curl_clone_primary_ssl_config(&data->set.proxy_ssl.primary,
  3186. &conn->proxy_ssl_config)) {
  3187. result = CURLE_OUT_OF_MEMORY;
  3188. goto out;
  3189. }
  3190. prune_dead_connections(data);
  3191. /*************************************************************
  3192. * Check the current list of connections to see if we can
  3193. * re-use an already existing one or if we have to create a
  3194. * new one.
  3195. *************************************************************/
  3196. DEBUGASSERT(conn->user);
  3197. DEBUGASSERT(conn->passwd);
  3198. /* reuse_fresh is TRUE if we are told to use a new connection by force, but
  3199. we only acknowledge this option if this is not a re-used connection
  3200. already (which happens due to follow-location or during a HTTP
  3201. authentication phase). CONNECT_ONLY transfers also refuse reuse. */
  3202. if((data->set.reuse_fresh && !data->state.this_is_a_follow) ||
  3203. data->set.connect_only)
  3204. reuse = FALSE;
  3205. else
  3206. reuse = ConnectionExists(data, conn, &conn_temp, &force_reuse, &waitpipe);
  3207. if(reuse) {
  3208. /*
  3209. * We already have a connection for this, we got the former connection
  3210. * in the conn_temp variable and thus we need to cleanup the one we
  3211. * just allocated before we can move along and use the previously
  3212. * existing one.
  3213. */
  3214. reuse_conn(conn, conn_temp);
  3215. #ifdef USE_SSL
  3216. free(conn->ssl_extra);
  3217. #endif
  3218. free(conn); /* we don't need this anymore */
  3219. conn = conn_temp;
  3220. *in_connect = conn;
  3221. infof(data, "Re-using existing connection! (#%ld) with %s %s\n",
  3222. conn->connection_id,
  3223. conn->bits.proxy?"proxy":"host",
  3224. conn->socks_proxy.host.name ? conn->socks_proxy.host.dispname :
  3225. conn->http_proxy.host.name ? conn->http_proxy.host.dispname :
  3226. conn->host.dispname);
  3227. }
  3228. else {
  3229. /* We have decided that we want a new connection. However, we may not
  3230. be able to do that if we have reached the limit of how many
  3231. connections we are allowed to open. */
  3232. if(conn->handler->flags & PROTOPT_ALPN_NPN) {
  3233. /* The protocol wants it, so set the bits if enabled in the easy handle
  3234. (default) */
  3235. if(data->set.ssl_enable_alpn)
  3236. conn->bits.tls_enable_alpn = TRUE;
  3237. if(data->set.ssl_enable_npn)
  3238. conn->bits.tls_enable_npn = TRUE;
  3239. }
  3240. if(waitpipe)
  3241. /* There is a connection that *might* become usable for multiplexing
  3242. "soon", and we wait for that */
  3243. connections_available = FALSE;
  3244. else {
  3245. /* this gets a lock on the conncache */
  3246. const char *bundlehost;
  3247. struct connectbundle *bundle =
  3248. Curl_conncache_find_bundle(conn, data->state.conn_cache, &bundlehost);
  3249. if(max_host_connections > 0 && bundle &&
  3250. (bundle->num_connections >= max_host_connections)) {
  3251. struct connectdata *conn_candidate;
  3252. /* The bundle is full. Extract the oldest connection. */
  3253. conn_candidate = Curl_conncache_extract_bundle(data, bundle);
  3254. CONNCACHE_UNLOCK(data);
  3255. if(conn_candidate)
  3256. (void)Curl_disconnect(data, conn_candidate,
  3257. /* dead_connection */ FALSE);
  3258. else {
  3259. infof(data, "No more connections allowed to host %s: %zu\n",
  3260. bundlehost, max_host_connections);
  3261. connections_available = FALSE;
  3262. }
  3263. }
  3264. else
  3265. CONNCACHE_UNLOCK(data);
  3266. }
  3267. if(connections_available &&
  3268. (max_total_connections > 0) &&
  3269. (Curl_conncache_size(data) >= max_total_connections)) {
  3270. struct connectdata *conn_candidate;
  3271. /* The cache is full. Let's see if we can kill a connection. */
  3272. conn_candidate = Curl_conncache_extract_oldest(data);
  3273. if(conn_candidate)
  3274. (void)Curl_disconnect(data, conn_candidate,
  3275. /* dead_connection */ FALSE);
  3276. else {
  3277. infof(data, "No connections available in cache\n");
  3278. connections_available = FALSE;
  3279. }
  3280. }
  3281. if(!connections_available) {
  3282. infof(data, "No connections available.\n");
  3283. conn_free(conn);
  3284. *in_connect = NULL;
  3285. result = CURLE_NO_CONNECTION_AVAILABLE;
  3286. goto out;
  3287. }
  3288. else {
  3289. /*
  3290. * This is a brand new connection, so let's store it in the connection
  3291. * cache of ours!
  3292. */
  3293. Curl_attach_connnection(data, conn);
  3294. result = Curl_conncache_add_conn(data->state.conn_cache, conn);
  3295. if(result)
  3296. goto out;
  3297. }
  3298. #if defined(USE_NTLM)
  3299. /* If NTLM is requested in a part of this connection, make sure we don't
  3300. assume the state is fine as this is a fresh connection and NTLM is
  3301. connection based. */
  3302. if((data->state.authhost.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
  3303. data->state.authhost.done) {
  3304. infof(data, "NTLM picked AND auth done set, clear picked!\n");
  3305. data->state.authhost.picked = CURLAUTH_NONE;
  3306. data->state.authhost.done = FALSE;
  3307. }
  3308. if((data->state.authproxy.picked & (CURLAUTH_NTLM | CURLAUTH_NTLM_WB)) &&
  3309. data->state.authproxy.done) {
  3310. infof(data, "NTLM-proxy picked AND auth done set, clear picked!\n");
  3311. data->state.authproxy.picked = CURLAUTH_NONE;
  3312. data->state.authproxy.done = FALSE;
  3313. }
  3314. #endif
  3315. }
  3316. /* Setup and init stuff before DO starts, in preparing for the transfer. */
  3317. Curl_init_do(data, conn);
  3318. /*
  3319. * Setup whatever necessary for a resumed transfer
  3320. */
  3321. result = setup_range(data);
  3322. if(result)
  3323. goto out;
  3324. /* Continue connectdata initialization here. */
  3325. /*
  3326. * Inherit the proper values from the urldata struct AFTER we have arranged
  3327. * the persistent connection stuff
  3328. */
  3329. conn->seek_func = data->set.seek_func;
  3330. conn->seek_client = data->set.seek_client;
  3331. /*************************************************************
  3332. * Resolve the address of the server or proxy
  3333. *************************************************************/
  3334. result = resolve_server(data, conn, async);
  3335. /* Strip trailing dots. resolve_server copied the name. */
  3336. strip_trailing_dot(&conn->host);
  3337. if(conn->bits.httpproxy)
  3338. strip_trailing_dot(&conn->http_proxy.host);
  3339. if(conn->bits.socksproxy)
  3340. strip_trailing_dot(&conn->socks_proxy.host);
  3341. if(conn->bits.conn_to_host)
  3342. strip_trailing_dot(&conn->conn_to_host);
  3343. out:
  3344. return result;
  3345. }
  3346. /* Curl_setup_conn() is called after the name resolve initiated in
  3347. * create_conn() is all done.
  3348. *
  3349. * Curl_setup_conn() also handles reused connections
  3350. *
  3351. * conn->data MUST already have been setup fine (in create_conn)
  3352. */
  3353. CURLcode Curl_setup_conn(struct connectdata *conn,
  3354. bool *protocol_done)
  3355. {
  3356. CURLcode result = CURLE_OK;
  3357. struct Curl_easy *data = conn->data;
  3358. Curl_pgrsTime(data, TIMER_NAMELOOKUP);
  3359. if(conn->handler->flags & PROTOPT_NONETWORK) {
  3360. /* nothing to setup when not using a network */
  3361. *protocol_done = TRUE;
  3362. return result;
  3363. }
  3364. *protocol_done = FALSE; /* default to not done */
  3365. /* set proxy_connect_closed to false unconditionally already here since it
  3366. is used strictly to provide extra information to a parent function in the
  3367. case of proxy CONNECT failures and we must make sure we don't have it
  3368. lingering set from a previous invoke */
  3369. conn->bits.proxy_connect_closed = FALSE;
  3370. /*
  3371. * Set user-agent. Used for HTTP, but since we can attempt to tunnel
  3372. * basically anything through a http proxy we can't limit this based on
  3373. * protocol.
  3374. */
  3375. if(data->set.str[STRING_USERAGENT]) {
  3376. Curl_safefree(conn->allocptr.uagent);
  3377. conn->allocptr.uagent =
  3378. aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
  3379. if(!conn->allocptr.uagent)
  3380. return CURLE_OUT_OF_MEMORY;
  3381. }
  3382. data->req.headerbytecount = 0;
  3383. #ifdef CURL_DO_LINEEND_CONV
  3384. data->state.crlf_conversions = 0; /* reset CRLF conversion counter */
  3385. #endif /* CURL_DO_LINEEND_CONV */
  3386. /* set start time here for timeout purposes in the connect procedure, it
  3387. is later set again for the progress meter purpose */
  3388. conn->now = Curl_now();
  3389. if(CURL_SOCKET_BAD == conn->sock[FIRSTSOCKET]) {
  3390. conn->bits.tcpconnect[FIRSTSOCKET] = FALSE;
  3391. result = Curl_connecthost(conn, conn->dns_entry);
  3392. if(result)
  3393. return result;
  3394. }
  3395. else {
  3396. Curl_pgrsTime(data, TIMER_CONNECT); /* we're connected already */
  3397. if(conn->ssl[FIRSTSOCKET].use ||
  3398. (conn->handler->protocol & PROTO_FAMILY_SSH))
  3399. Curl_pgrsTime(data, TIMER_APPCONNECT); /* we're connected already */
  3400. conn->bits.tcpconnect[FIRSTSOCKET] = TRUE;
  3401. *protocol_done = TRUE;
  3402. Curl_updateconninfo(conn, conn->sock[FIRSTSOCKET]);
  3403. Curl_verboseconnect(conn);
  3404. }
  3405. conn->now = Curl_now(); /* time this *after* the connect is done, we set
  3406. this here perhaps a second time */
  3407. return result;
  3408. }
  3409. CURLcode Curl_connect(struct Curl_easy *data,
  3410. bool *asyncp,
  3411. bool *protocol_done)
  3412. {
  3413. CURLcode result;
  3414. struct connectdata *conn;
  3415. *asyncp = FALSE; /* assume synchronous resolves by default */
  3416. /* init the single-transfer specific data */
  3417. Curl_free_request_state(data);
  3418. memset(&data->req, 0, sizeof(struct SingleRequest));
  3419. data->req.maxdownload = -1;
  3420. /* call the stuff that needs to be called */
  3421. result = create_conn(data, &conn, asyncp);
  3422. if(!result) {
  3423. if(CONN_INUSE(conn) > 1)
  3424. /* multiplexed */
  3425. *protocol_done = TRUE;
  3426. else if(!*asyncp) {
  3427. /* DNS resolution is done: that's either because this is a reused
  3428. connection, in which case DNS was unnecessary, or because DNS
  3429. really did finish already (synch resolver/fast async resolve) */
  3430. result = Curl_setup_conn(conn, protocol_done);
  3431. }
  3432. }
  3433. if(result == CURLE_NO_CONNECTION_AVAILABLE) {
  3434. return result;
  3435. }
  3436. else if(result && conn) {
  3437. /* We're not allowed to return failure with memory left allocated in the
  3438. connectdata struct, free those here */
  3439. Curl_detach_connnection(data);
  3440. Curl_conncache_remove_conn(data, conn, TRUE);
  3441. Curl_disconnect(data, conn, TRUE);
  3442. }
  3443. return result;
  3444. }
  3445. /*
  3446. * Curl_init_do() inits the readwrite session. This is inited each time (in
  3447. * the DO function before the protocol-specific DO functions are invoked) for
  3448. * a transfer, sometimes multiple times on the same Curl_easy. Make sure
  3449. * nothing in here depends on stuff that are setup dynamically for the
  3450. * transfer.
  3451. *
  3452. * Allow this function to get called with 'conn' set to NULL.
  3453. */
  3454. CURLcode Curl_init_do(struct Curl_easy *data, struct connectdata *conn)
  3455. {
  3456. struct SingleRequest *k = &data->req;
  3457. if(conn) {
  3458. conn->bits.do_more = FALSE; /* by default there's no curl_do_more() to
  3459. use */
  3460. /* if the protocol used doesn't support wildcards, switch it off */
  3461. if(data->state.wildcardmatch &&
  3462. !(conn->handler->flags & PROTOPT_WILDCARD))
  3463. data->state.wildcardmatch = FALSE;
  3464. }
  3465. data->state.done = FALSE; /* *_done() is not called yet */
  3466. data->state.expect100header = FALSE;
  3467. if(data->set.opt_no_body)
  3468. /* in HTTP lingo, no body means using the HEAD request... */
  3469. data->set.httpreq = HTTPREQ_HEAD;
  3470. else if(HTTPREQ_HEAD == data->set.httpreq)
  3471. /* ... but if unset there really is no perfect method that is the
  3472. "opposite" of HEAD but in reality most people probably think GET
  3473. then. The important thing is that we can't let it remain HEAD if the
  3474. opt_no_body is set FALSE since then we'll behave wrong when getting
  3475. HTTP. */
  3476. data->set.httpreq = HTTPREQ_GET;
  3477. k->start = Curl_now(); /* start time */
  3478. k->now = k->start; /* current time is now */
  3479. k->header = TRUE; /* assume header */
  3480. k->bytecount = 0;
  3481. k->buf = data->state.buffer;
  3482. k->ignorebody = FALSE;
  3483. Curl_speedinit(data);
  3484. Curl_pgrsSetUploadCounter(data, 0);
  3485. Curl_pgrsSetDownloadCounter(data, 0);
  3486. return CURLE_OK;
  3487. }
  3488. /*
  3489. * get_protocol_family()
  3490. *
  3491. * This is used to return the protocol family for a given protocol.
  3492. *
  3493. * Parameters:
  3494. *
  3495. * protocol [in] - A single bit protocol identifier such as HTTP or HTTPS.
  3496. *
  3497. * Returns the family as a single bit protocol identifier.
  3498. */
  3499. static unsigned int get_protocol_family(unsigned int protocol)
  3500. {
  3501. unsigned int family;
  3502. switch(protocol) {
  3503. case CURLPROTO_HTTP:
  3504. case CURLPROTO_HTTPS:
  3505. family = CURLPROTO_HTTP;
  3506. break;
  3507. case CURLPROTO_FTP:
  3508. case CURLPROTO_FTPS:
  3509. family = CURLPROTO_FTP;
  3510. break;
  3511. case CURLPROTO_SCP:
  3512. family = CURLPROTO_SCP;
  3513. break;
  3514. case CURLPROTO_SFTP:
  3515. family = CURLPROTO_SFTP;
  3516. break;
  3517. case CURLPROTO_TELNET:
  3518. family = CURLPROTO_TELNET;
  3519. break;
  3520. case CURLPROTO_LDAP:
  3521. case CURLPROTO_LDAPS:
  3522. family = CURLPROTO_LDAP;
  3523. break;
  3524. case CURLPROTO_DICT:
  3525. family = CURLPROTO_DICT;
  3526. break;
  3527. case CURLPROTO_FILE:
  3528. family = CURLPROTO_FILE;
  3529. break;
  3530. case CURLPROTO_TFTP:
  3531. family = CURLPROTO_TFTP;
  3532. break;
  3533. case CURLPROTO_IMAP:
  3534. case CURLPROTO_IMAPS:
  3535. family = CURLPROTO_IMAP;
  3536. break;
  3537. case CURLPROTO_POP3:
  3538. case CURLPROTO_POP3S:
  3539. family = CURLPROTO_POP3;
  3540. break;
  3541. case CURLPROTO_SMTP:
  3542. case CURLPROTO_SMTPS:
  3543. family = CURLPROTO_SMTP;
  3544. break;
  3545. case CURLPROTO_RTSP:
  3546. family = CURLPROTO_RTSP;
  3547. break;
  3548. case CURLPROTO_RTMP:
  3549. case CURLPROTO_RTMPS:
  3550. family = CURLPROTO_RTMP;
  3551. break;
  3552. case CURLPROTO_RTMPT:
  3553. case CURLPROTO_RTMPTS:
  3554. family = CURLPROTO_RTMPT;
  3555. break;
  3556. case CURLPROTO_RTMPE:
  3557. family = CURLPROTO_RTMPE;
  3558. break;
  3559. case CURLPROTO_RTMPTE:
  3560. family = CURLPROTO_RTMPTE;
  3561. break;
  3562. case CURLPROTO_GOPHER:
  3563. family = CURLPROTO_GOPHER;
  3564. break;
  3565. case CURLPROTO_SMB:
  3566. case CURLPROTO_SMBS:
  3567. family = CURLPROTO_SMB;
  3568. break;
  3569. default:
  3570. family = 0;
  3571. break;
  3572. }
  3573. return family;
  3574. }