url.c 126 KB

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