url.c 126 KB

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