url.c 128 KB

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