url.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://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. * $Id$
  22. ***************************************************************************/
  23. /* -- WIN32 approved -- */
  24. #include "setup.h"
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdarg.h>
  28. #include <stdlib.h>
  29. #include <ctype.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <errno.h>
  33. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  34. #include <time.h>
  35. #include <io.h>
  36. #else
  37. #ifdef HAVE_SYS_SOCKET_H
  38. #include <sys/socket.h>
  39. #endif
  40. #include <netinet/in.h>
  41. #include <sys/time.h>
  42. #ifdef HAVE_UNISTD_H
  43. #include <unistd.h>
  44. #endif
  45. #include <netdb.h>
  46. #ifdef HAVE_ARPA_INET_H
  47. #include <arpa/inet.h>
  48. #endif
  49. #ifdef HAVE_NET_IF_H
  50. #include <net/if.h>
  51. #endif
  52. #include <sys/ioctl.h>
  53. #include <signal.h>
  54. #ifdef HAVE_SYS_PARAM_H
  55. #include <sys/param.h>
  56. #endif
  57. #ifdef HAVE_SYS_SELECT_H
  58. #include <sys/select.h>
  59. #endif
  60. #ifdef VMS
  61. #include <in.h>
  62. #include <inet.h>
  63. #endif
  64. #ifdef HAVE_SETJMP_H
  65. #include <setjmp.h>
  66. #endif
  67. #ifndef HAVE_SELECT
  68. #error "We can't compile without select() support!"
  69. #endif
  70. #ifndef HAVE_SOCKET
  71. #error "We can't compile without socket() support!"
  72. #endif
  73. #endif
  74. #ifdef HAVE_OPENSSL_ENGINE_H
  75. #include <openssl/engine.h>
  76. #endif
  77. #include "urldata.h"
  78. #include "netrc.h"
  79. #include "formdata.h"
  80. #include "base64.h"
  81. #include "ssluse.h"
  82. #include "hostip.h"
  83. #include "if2ip.h"
  84. #include "transfer.h"
  85. #include "sendf.h"
  86. #include "progress.h"
  87. #include "cookie.h"
  88. #include "strequal.h"
  89. #include "escape.h"
  90. #include "strtok.h"
  91. #include "share.h"
  92. #include "content_encoding.h"
  93. #include "http_digest.h"
  94. #include "http_negotiate.h"
  95. /* And now for the protocols */
  96. #include "ftp.h"
  97. #include "dict.h"
  98. #include "telnet.h"
  99. #include "http.h"
  100. #include "file.h"
  101. #include "ldap.h"
  102. #include "url.h"
  103. #include "connect.h"
  104. #include <ca-bundle.h>
  105. #include <curl/types.h>
  106. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  107. #include "inet_ntoa_r.h"
  108. #endif
  109. #define _MPRINTF_REPLACE /* use our functions only */
  110. #include <curl/mprintf.h>
  111. #ifdef HAVE_KRB4
  112. #include "security.h"
  113. #endif
  114. /* The last #include file should be: */
  115. #ifdef CURLDEBUG
  116. #include "memdebug.h"
  117. #endif
  118. /* Local static prototypes */
  119. static int ConnectionKillOne(struct SessionHandle *data);
  120. static bool ConnectionExists(struct SessionHandle *data,
  121. struct connectdata *needle,
  122. struct connectdata **usethis);
  123. static unsigned int ConnectionStore(struct SessionHandle *data,
  124. struct connectdata *conn);
  125. static bool safe_strequal(char* str1, char* str2);
  126. #ifndef USE_ARES
  127. /* not for Win32, unless it is cygwin
  128. not for ares builds */
  129. #if !defined(WIN32) || defined(__CYGWIN32__)
  130. #ifndef RETSIGTYPE
  131. #define RETSIGTYPE void
  132. #endif
  133. #ifdef HAVE_SIGSETJMP
  134. extern sigjmp_buf curl_jmpenv;
  135. #endif
  136. static
  137. RETSIGTYPE alarmfunc(int sig)
  138. {
  139. /* this is for "-ansi -Wall -pedantic" to stop complaining! (rabe) */
  140. (void)sig;
  141. #ifdef HAVE_SIGSETJMP
  142. siglongjmp(curl_jmpenv, 1);
  143. #endif
  144. return;
  145. }
  146. #endif
  147. #endif /* USE_ARES */
  148. void Curl_safefree(void *ptr)
  149. {
  150. if(ptr)
  151. free(ptr);
  152. }
  153. /*
  154. * This is the internal function curl_easy_cleanup() calls. This should
  155. * cleanup and free all resources associated with this sessionhandle.
  156. *
  157. * NOTE: if we ever add something that attempts to write to a socket or
  158. * similar here, we must ignore SIGPIPE first. It is currently only done
  159. * when curl_easy_perform() is invoked.
  160. */
  161. CURLcode Curl_close(struct SessionHandle *data)
  162. {
  163. /* Loop through all open connections and kill them one by one */
  164. while(-1 != ConnectionKillOne(data));
  165. #ifdef USE_SSLEAY
  166. /* Close down all open SSL info and sessions */
  167. Curl_SSL_Close_All(data);
  168. #endif
  169. if(data->change.cookielist) /* clean up list if any */
  170. curl_slist_free_all(data->change.cookielist);
  171. Curl_safefree(data->state.auth_host);
  172. Curl_safefree(data->state.scratch);
  173. if(data->change.proxy_alloc)
  174. free(data->change.proxy);
  175. if(data->change.referer_alloc)
  176. free(data->change.referer);
  177. if(data->change.url_alloc)
  178. free(data->change.url);
  179. Curl_safefree(data->state.headerbuff);
  180. #ifndef CURL_DISABLE_HTTP
  181. Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
  182. if(data->set.cookiejar) {
  183. /* we have a "destination" for all the cookies to get dumped to */
  184. if(Curl_cookie_output(data->cookies, data->set.cookiejar))
  185. infof(data, "WARNING: failed to save cookies in %s\n",
  186. data->set.cookiejar);
  187. }
  188. if( !data->share || (data->cookies != data->share->cookies) ) {
  189. Curl_cookie_cleanup(data->cookies);
  190. }
  191. Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
  192. #endif
  193. /* free the connection cache */
  194. free(data->state.connects);
  195. Curl_safefree(data->info.contenttype);
  196. Curl_digest_cleanup(data);
  197. #ifdef USE_ARES
  198. /* this destroys the channel and we cannot use it anymore after this */
  199. ares_destroy(data->state.areschannel);
  200. #endif
  201. /* No longer a dirty share, if it exists */
  202. if (data->share)
  203. data->share->dirty--;
  204. free(data);
  205. return CURLE_OK;
  206. }
  207. CURLcode Curl_open(struct SessionHandle **curl)
  208. {
  209. /* We don't yet support specifying the URL at this point */
  210. struct SessionHandle *data;
  211. /* Very simple start-up: alloc the struct, init it with zeroes and return */
  212. data = (struct SessionHandle *)malloc(sizeof(struct SessionHandle));
  213. if(!data)
  214. /* this is a very serious error */
  215. return CURLE_OUT_OF_MEMORY;
  216. memset(data, 0, sizeof(struct SessionHandle));
  217. #ifdef USE_ARES
  218. if(ARES_SUCCESS != ares_init(&data->state.areschannel)) {
  219. free(data);
  220. return CURLE_FAILED_INIT;
  221. }
  222. /* make sure that all other returns from this function should destroy the
  223. ares channel before returning error! */
  224. #endif
  225. /* We do some initial setup here, all those fields that can't be just 0 */
  226. data->state.headerbuff=(char*)malloc(HEADERSIZE);
  227. if(!data->state.headerbuff) {
  228. free(data); /* free the memory again */
  229. return CURLE_OUT_OF_MEMORY;
  230. }
  231. data->state.headersize=HEADERSIZE;
  232. data->set.out = stdout; /* default output to stdout */
  233. data->set.in = stdin; /* default input from stdin */
  234. data->set.err = stderr; /* default stderr to stderr */
  235. /* use fwrite as default function to store output */
  236. data->set.fwrite = (curl_write_callback)fwrite;
  237. /* use fread as default function to read input */
  238. data->set.fread = (curl_read_callback)fread;
  239. data->set.infilesize = -1; /* we don't know any size */
  240. data->state.current_speed = -1; /* init to negative == impossible */
  241. data->set.httpreq = HTTPREQ_GET; /* Default HTTP request */
  242. data->set.ftp_use_epsv = TRUE; /* FTP defaults to EPSV operations */
  243. data->set.ftp_use_eprt = TRUE; /* FTP defaults to EPRT operations */
  244. data->set.dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
  245. /* make libcurl quiet by default: */
  246. data->set.hide_progress = TRUE; /* CURLOPT_NOPROGRESS changes these */
  247. data->progress.flags |= PGRS_HIDE;
  248. /* Set the default size of the SSL session ID cache */
  249. data->set.ssl.numsessions = 5;
  250. data->set.proxyport = 1080;
  251. data->set.proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
  252. data->set.httpauth = CURLAUTH_BASIC; /* defaults to basic authentication */
  253. data->set.proxyauth = CURLAUTH_BASIC; /* defaults to basic authentication */
  254. /* create an array with connection data struct pointers */
  255. data->state.numconnects = 5; /* hard-coded right now */
  256. data->state.connects = (struct connectdata **)
  257. malloc(sizeof(struct connectdata *) * data->state.numconnects);
  258. if(!data->state.connects) {
  259. free(data->state.headerbuff);
  260. free(data);
  261. return CURLE_OUT_OF_MEMORY;
  262. }
  263. /*
  264. * libcurl 7.10 introduces SSL verification *by default*! This needs to be
  265. * switched off unless wanted.
  266. */
  267. data->set.ssl.verifypeer = TRUE;
  268. data->set.ssl.verifyhost = 2;
  269. #ifdef CURL_CA_BUNDLE
  270. /* This is our prefered CA cert bundle since install time */
  271. data->set.ssl.CAfile = (char *)CURL_CA_BUNDLE;
  272. #endif
  273. memset(data->state.connects, 0,
  274. sizeof(struct connectdata *)*data->state.numconnects);
  275. *curl = data;
  276. return CURLE_OK;
  277. }
  278. CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option, ...)
  279. {
  280. va_list param;
  281. char *cookiefile;
  282. va_start(param, option);
  283. switch(option) {
  284. case CURLOPT_DNS_CACHE_TIMEOUT:
  285. data->set.dns_cache_timeout = va_arg(param, int);
  286. break;
  287. case CURLOPT_DNS_USE_GLOBAL_CACHE:
  288. {
  289. int use_cache = va_arg(param, int);
  290. if (use_cache) {
  291. Curl_global_host_cache_init();
  292. }
  293. data->set.global_dns_cache = use_cache;
  294. }
  295. break;
  296. case CURLOPT_SSL_CIPHER_LIST:
  297. /* set a list of cipher we want to use in the SSL connection */
  298. data->set.ssl.cipher_list = va_arg(param, char *);
  299. break;
  300. case CURLOPT_RANDOM_FILE:
  301. /*
  302. * This is the path name to a file that contains random data to seed
  303. * the random SSL stuff with. The file is only used for reading.
  304. */
  305. data->set.ssl.random_file = va_arg(param, char *);
  306. break;
  307. case CURLOPT_EGDSOCKET:
  308. /*
  309. * The Entropy Gathering Daemon socket pathname
  310. */
  311. data->set.ssl.egdsocket = va_arg(param, char *);
  312. break;
  313. case CURLOPT_MAXCONNECTS:
  314. /*
  315. * Set the absolute number of maximum simultaneous alive connection that
  316. * libcurl is allowed to have.
  317. */
  318. {
  319. long newconnects= va_arg(param, long);
  320. struct connectdata **newptr;
  321. if(newconnects < data->state.numconnects) {
  322. /* Since this number is *decreased* from the existing number, we must
  323. close the possibly open connections that live on the indexes that
  324. are being removed! */
  325. int i;
  326. for(i=newconnects; i< data->state.numconnects; i++)
  327. Curl_disconnect(data->state.connects[i]);
  328. }
  329. if(newconnects) {
  330. int i;
  331. newptr= (struct connectdata **)
  332. realloc(data->state.connects,
  333. sizeof(struct connectdata *) * newconnects);
  334. if(!newptr)
  335. /* we closed a few connections in vain, but so what? */
  336. return CURLE_OUT_OF_MEMORY;
  337. /* nullify the newly added pointers */
  338. for(i=data->state.numconnects; i<newconnects; i++) {
  339. newptr[i] = NULL;
  340. }
  341. data->state.connects = newptr;
  342. data->state.numconnects = newconnects;
  343. }
  344. else {
  345. /* zero makes NO cache at all */
  346. if(data->state.connects)
  347. free(data->state.connects);
  348. data->state.connects=NULL;
  349. data->state.numconnects=0;
  350. }
  351. }
  352. break;
  353. case CURLOPT_FORBID_REUSE:
  354. /*
  355. * When this transfer is done, it must not be left to be reused by a
  356. * subsequent transfer but shall be closed immediately.
  357. */
  358. data->set.reuse_forbid = va_arg(param, long)?TRUE:FALSE;
  359. break;
  360. case CURLOPT_FRESH_CONNECT:
  361. /*
  362. * This transfer shall not use a previously cached connection but
  363. * should be made with a fresh new connect!
  364. */
  365. data->set.reuse_fresh = va_arg(param, long)?TRUE:FALSE;
  366. break;
  367. case CURLOPT_VERBOSE:
  368. /*
  369. * Verbose means infof() calls that give a lot of information about
  370. * the connection and transfer procedures as well as internal choices.
  371. */
  372. data->set.verbose = va_arg(param, long)?TRUE:FALSE;
  373. break;
  374. case CURLOPT_HEADER:
  375. /*
  376. * Set to include the header in the general data output stream.
  377. */
  378. data->set.http_include_header = va_arg(param, long)?TRUE:FALSE;
  379. break;
  380. case CURLOPT_NOPROGRESS:
  381. /*
  382. * Shut off the internal supported progress meter
  383. */
  384. data->set.hide_progress = va_arg(param, long)?TRUE:FALSE;
  385. if(data->set.hide_progress)
  386. data->progress.flags |= PGRS_HIDE;
  387. else
  388. data->progress.flags &= ~PGRS_HIDE;
  389. break;
  390. case CURLOPT_NOBODY:
  391. /*
  392. * Do not include the body part in the output data stream.
  393. */
  394. data->set.no_body = va_arg(param, long)?TRUE:FALSE;
  395. break;
  396. case CURLOPT_FAILONERROR:
  397. /*
  398. * Don't output the >=300 error code HTML-page, but instead only
  399. * return error.
  400. */
  401. data->set.http_fail_on_error = va_arg(param, long)?TRUE:FALSE;
  402. break;
  403. case CURLOPT_UPLOAD:
  404. /*
  405. * We want to sent data to the remote host
  406. */
  407. data->set.upload = va_arg(param, long)?TRUE:FALSE;
  408. if(data->set.upload)
  409. /* If this is HTTP, PUT is what's needed to "upload" */
  410. data->set.httpreq = HTTPREQ_PUT;
  411. break;
  412. case CURLOPT_FILETIME:
  413. /*
  414. * Try to get the file time of the remote document. The time will
  415. * later (possibly) become available using curl_easy_getinfo().
  416. */
  417. data->set.get_filetime = va_arg(param, long)?TRUE:FALSE;
  418. break;
  419. case CURLOPT_FTP_CREATE_MISSING_DIRS:
  420. /*
  421. * An FTP option that modifies an upload to create missing directories on
  422. * the server.
  423. */
  424. data->set.ftp_create_missing_dirs = va_arg( param , long )?TRUE:FALSE;
  425. break;
  426. case CURLOPT_FTP_RESPONSE_TIMEOUT:
  427. /*
  428. * An FTP option that specifies how quickly an FTP response must be
  429. * obtained before it is considered failure.
  430. */
  431. data->set.ftp_response_timeout = va_arg( param , long );
  432. break;
  433. case CURLOPT_FTPLISTONLY:
  434. /*
  435. * An FTP option that changes the command to one that asks for a list
  436. * only, no file info details.
  437. */
  438. data->set.ftp_list_only = va_arg(param, long)?TRUE:FALSE;
  439. break;
  440. case CURLOPT_FTPAPPEND:
  441. /*
  442. * We want to upload and append to an existing (FTP) file.
  443. */
  444. data->set.ftp_append = va_arg(param, long)?TRUE:FALSE;
  445. break;
  446. case CURLOPT_NETRC:
  447. /*
  448. * Parse the $HOME/.netrc file
  449. */
  450. data->set.use_netrc = (enum CURL_NETRC_OPTION)va_arg(param, long);
  451. break;
  452. case CURLOPT_NETRC_FILE:
  453. /*
  454. * Use this file instead of the $HOME/.netrc file
  455. */
  456. data->set.netrc_file = va_arg(param, char *);
  457. break;
  458. case CURLOPT_FOLLOWLOCATION:
  459. /*
  460. * Follow Location: header hints on a HTTP-server.
  461. */
  462. data->set.http_follow_location = va_arg(param, long)?TRUE:FALSE;
  463. break;
  464. case CURLOPT_UNRESTRICTED_AUTH:
  465. /*
  466. * Send authentication (user+password) when following locations, even when
  467. * hostname changed.
  468. */
  469. data->set.http_disable_hostname_check_before_authentication =
  470. va_arg(param, long)?TRUE:FALSE;
  471. break;
  472. case CURLOPT_HTTP_VERSION:
  473. /*
  474. * This sets a requested HTTP version to be used. The value is one of
  475. * the listed enums in curl/curl.h.
  476. */
  477. data->set.httpversion = va_arg(param, long);
  478. break;
  479. case CURLOPT_TRANSFERTEXT:
  480. /*
  481. * This option was previously named 'FTPASCII'. Renamed to work with
  482. * more protocols than merely FTP.
  483. *
  484. * Transfer using ASCII (instead of BINARY).
  485. */
  486. data->set.ftp_ascii = va_arg(param, long)?TRUE:FALSE;
  487. break;
  488. case CURLOPT_PUT:
  489. /*
  490. * Use the HTTP PUT request to transfer data if this is TRUE. If this is
  491. * FALSE, don't set the httpreq. We can't know what to revert it to!
  492. */
  493. if(va_arg(param, long))
  494. data->set.httpreq = HTTPREQ_PUT;
  495. break;
  496. case CURLOPT_TIMECONDITION:
  497. /*
  498. * Set HTTP time condition. This must be one of the defines in the
  499. * curl/curl.h header file.
  500. */
  501. data->set.timecondition = (curl_TimeCond)va_arg(param, long);
  502. break;
  503. case CURLOPT_TIMEVALUE:
  504. /*
  505. * This is the value to compare with the remote document with the
  506. * method set with CURLOPT_TIMECONDITION
  507. */
  508. data->set.timevalue = va_arg(param, long);
  509. break;
  510. case CURLOPT_SSLVERSION:
  511. /*
  512. * Set explicit SSL version to try to connect with, as some SSL
  513. * implementations are lame.
  514. */
  515. data->set.ssl.version = va_arg(param, long);
  516. break;
  517. case CURLOPT_COOKIESESSION:
  518. /*
  519. * Set this option to TRUE to start a new "cookie session". It will
  520. * prevent the forthcoming read-cookies-from-file actions to accept
  521. * cookies that are marked as being session cookies, as they belong to a
  522. * previous session.
  523. *
  524. * In the original Netscape cookie spec, "session cookies" are cookies
  525. * with no expire date set. RFC2109 describes the same action if no
  526. * 'Max-Age' is set and RFC2965 includes the RFC2109 description and adds
  527. * a 'Discard' action that can enforce the discard even for cookies that
  528. * have a Max-Age.
  529. *
  530. * We run mostly with the original cookie spec, as hardly anyone implements
  531. * anything else.
  532. */
  533. data->set.cookiesession = (bool)va_arg(param, long);
  534. break;
  535. #ifndef CURL_DISABLE_HTTP
  536. case CURLOPT_COOKIEFILE:
  537. /*
  538. * Set cookie file to read and parse. Can be used multiple times.
  539. */
  540. cookiefile = (char *)va_arg(param, void *);
  541. if(cookiefile)
  542. /* append the cookie file name to the list of file names, and deal with
  543. them later */
  544. data->change.cookielist =
  545. curl_slist_append(data->change.cookielist, cookiefile);
  546. break;
  547. case CURLOPT_COOKIEJAR:
  548. /*
  549. * Set cookie file name to dump all cookies to when we're done.
  550. */
  551. data->set.cookiejar = (char *)va_arg(param, void *);
  552. /*
  553. * Activate the cookie parser. This may or may not already
  554. * have been made.
  555. */
  556. data->cookies = Curl_cookie_init(data, NULL, data->cookies,
  557. data->set.cookiesession);
  558. break;
  559. #endif
  560. case CURLOPT_WRITEHEADER:
  561. /*
  562. * Custom pointer to pass the header write callback function
  563. */
  564. data->set.writeheader = (void *)va_arg(param, void *);
  565. break;
  566. case CURLOPT_COOKIE:
  567. /*
  568. * Cookie string to send to the remote server in the request.
  569. */
  570. data->set.cookie = va_arg(param, char *);
  571. break;
  572. case CURLOPT_ERRORBUFFER:
  573. /*
  574. * Error buffer provided by the caller to get the human readable
  575. * error string in.
  576. */
  577. data->set.errorbuffer = va_arg(param, char *);
  578. break;
  579. case CURLOPT_FILE:
  580. /*
  581. * FILE pointer to write to or include in the data write callback
  582. */
  583. data->set.out = va_arg(param, FILE *);
  584. break;
  585. case CURLOPT_FTPPORT:
  586. /*
  587. * Use FTP PORT, this also specifies which IP address to use
  588. */
  589. data->set.ftpport = va_arg(param, char *);
  590. data->set.ftp_use_port = data->set.ftpport?1:0;
  591. break;
  592. case CURLOPT_FTP_USE_EPRT:
  593. data->set.ftp_use_eprt = va_arg(param, long)?TRUE:FALSE;
  594. break;
  595. case CURLOPT_FTP_USE_EPSV:
  596. data->set.ftp_use_epsv = va_arg(param, long)?TRUE:FALSE;
  597. break;
  598. case CURLOPT_HTTPHEADER:
  599. /*
  600. * Set a list with HTTP headers to use (or replace internals with)
  601. */
  602. data->set.headers = va_arg(param, struct curl_slist *);
  603. break;
  604. case CURLOPT_CUSTOMREQUEST:
  605. /*
  606. * Set a custom string to use as request
  607. */
  608. data->set.customrequest = va_arg(param, char *);
  609. /* we don't set
  610. data->set.httpreq = HTTPREQ_CUSTOM;
  611. here, we continue as if we were using the already set type
  612. and this just changes the actual request keyword */
  613. break;
  614. case CURLOPT_HTTPPOST:
  615. /*
  616. * Set to make us do HTTP POST
  617. */
  618. data->set.httppost = va_arg(param, struct curl_httppost *);
  619. if(data->set.httppost)
  620. data->set.httpreq = HTTPREQ_POST_FORM;
  621. break;
  622. case CURLOPT_HTTPGET:
  623. /*
  624. * Set to force us do HTTP GET
  625. */
  626. if(va_arg(param, long)) {
  627. data->set.httpreq = HTTPREQ_GET;
  628. data->set.upload = FALSE; /* switch off upload */
  629. }
  630. break;
  631. case CURLOPT_INFILE:
  632. /*
  633. * FILE pointer to read the file to be uploaded from. Or possibly
  634. * used as argument to the read callback.
  635. */
  636. data->set.in = va_arg(param, FILE *);
  637. break;
  638. case CURLOPT_INFILESIZE:
  639. /*
  640. * If known, this should inform curl about the file size of the
  641. * to-be-uploaded file.
  642. */
  643. data->set.infilesize = va_arg(param, long);
  644. break;
  645. case CURLOPT_INFILESIZE_LARGE:
  646. /*
  647. * If known, this should inform curl about the file size of the
  648. * to-be-uploaded file.
  649. */
  650. data->set.infilesize = va_arg(param, curl_off_t);
  651. break;
  652. case CURLOPT_LOW_SPEED_LIMIT:
  653. /*
  654. * The low speed limit that if transfers are below this for
  655. * CURLOPT_LOW_SPEED_TIME, the transfer is aborted.
  656. */
  657. data->set.low_speed_limit=va_arg(param, long);
  658. break;
  659. case CURLOPT_LOW_SPEED_TIME:
  660. /*
  661. * The low speed time that if transfers are below the set
  662. * CURLOPT_LOW_SPEED_LIMIT during this time, the transfer is aborted.
  663. */
  664. data->set.low_speed_time=va_arg(param, long);
  665. break;
  666. case CURLOPT_URL:
  667. /*
  668. * The URL to fetch.
  669. */
  670. if(data->change.url_alloc) {
  671. /* the already set URL is allocated, free it first! */
  672. free(data->change.url);
  673. data->change.url_alloc=FALSE;
  674. }
  675. data->set.set_url = va_arg(param, char *);
  676. data->change.url = data->set.set_url;
  677. data->change.url_changed = TRUE;
  678. break;
  679. case CURLOPT_PORT:
  680. /*
  681. * The port number to use when getting the URL
  682. */
  683. data->set.use_port = va_arg(param, long);
  684. break;
  685. case CURLOPT_POST:
  686. /* Does this option serve a purpose anymore? Yes it does, when
  687. CURLOPT_POSTFIELDS isn't used and the POST data is read off the
  688. callback! */
  689. if(va_arg(param, long))
  690. data->set.httpreq = HTTPREQ_POST;
  691. break;
  692. case CURLOPT_POSTFIELDS:
  693. /*
  694. * A string with POST data. Makes curl HTTP POST.
  695. */
  696. data->set.postfields = va_arg(param, char *);
  697. if(data->set.postfields)
  698. data->set.httpreq = HTTPREQ_POST;
  699. break;
  700. case CURLOPT_POSTFIELDSIZE:
  701. /*
  702. * The size of the POSTFIELD data to prevent libcurl to do strlen() to
  703. * figure it out. Enables binary posts.
  704. */
  705. data->set.postfieldsize = va_arg(param, long);
  706. break;
  707. case CURLOPT_POSTFIELDSIZE_LARGE:
  708. /*
  709. * The size of the POSTFIELD data to prevent libcurl to do strlen() to
  710. * figure it out. Enables binary posts.
  711. */
  712. data->set.postfieldsize = va_arg(param, curl_off_t);
  713. break;
  714. case CURLOPT_REFERER:
  715. /*
  716. * String to set in the HTTP Referer: field.
  717. */
  718. if(data->change.referer_alloc) {
  719. free(data->change.referer);
  720. data->change.referer_alloc = FALSE;
  721. }
  722. data->set.set_referer = va_arg(param, char *);
  723. data->change.referer = data->set.set_referer;
  724. break;
  725. case CURLOPT_AUTOREFERER:
  726. /*
  727. * Switch on automatic referer that gets set if curl follows locations.
  728. */
  729. data->set.http_auto_referer = va_arg(param, long)?1:0;
  730. break;
  731. case CURLOPT_PROXY:
  732. /*
  733. * Set proxy server:port to use as HTTP proxy.
  734. *
  735. * If the proxy is set to "" we explicitly say that we don't want to use a
  736. * proxy (even though there might be environment variables saying so).
  737. *
  738. * Setting it to NULL, means no proxy but allows the environment variables
  739. * to decide for us.
  740. */
  741. if(data->change.proxy_alloc) {
  742. /*
  743. * The already set string is allocated, free that first
  744. */
  745. data->change.proxy_alloc=FALSE;;
  746. free(data->change.proxy);
  747. }
  748. data->set.set_proxy = va_arg(param, char *);
  749. data->change.proxy = data->set.set_proxy;
  750. break;
  751. case CURLOPT_HTTPPROXYTUNNEL:
  752. /*
  753. * Tunnel operations through the proxy instead of normal proxy use
  754. */
  755. data->set.tunnel_thru_httpproxy = va_arg(param, long)?TRUE:FALSE;
  756. break;
  757. case CURLOPT_PROXYPORT:
  758. /*
  759. * Explicitly set HTTP proxy port number.
  760. */
  761. data->set.proxyport = va_arg(param, long);
  762. break;
  763. case CURLOPT_TIMEOUT:
  764. /*
  765. * The maximum time you allow curl to use for a single transfer
  766. * operation.
  767. */
  768. data->set.timeout = va_arg(param, long);
  769. break;
  770. case CURLOPT_CONNECTTIMEOUT:
  771. /*
  772. * The maximum time you allow curl to use to connect.
  773. */
  774. data->set.connecttimeout = va_arg(param, long);
  775. break;
  776. case CURLOPT_MAXREDIRS:
  777. /*
  778. * The maximum amount of hops you allow curl to follow Location:
  779. * headers. This should mostly be used to detect never-ending loops.
  780. */
  781. data->set.maxredirs = va_arg(param, long);
  782. break;
  783. case CURLOPT_USERAGENT:
  784. /*
  785. * String to use in the HTTP User-Agent field
  786. */
  787. data->set.useragent = va_arg(param, char *);
  788. break;
  789. case CURLOPT_ENCODING:
  790. /*
  791. * String to use at the value of Accept-Encoding header. 08/28/02 jhrg
  792. *
  793. * If the encoding is set to "" we use an Accept-Encoding header that
  794. * encompasses all the encodings we support.
  795. * If the encoding is set to NULL we don't send an Accept-Encoding header
  796. * and ignore an received Content-Encoding header.
  797. *
  798. */
  799. data->set.encoding = va_arg(param, char *);
  800. if(data->set.encoding && !*data->set.encoding)
  801. data->set.encoding = (char*)ALL_CONTENT_ENCODINGS;
  802. break;
  803. case CURLOPT_HTTPAUTH:
  804. /*
  805. * Set HTTP Authentication type BITMASK.
  806. */
  807. {
  808. long auth = va_arg(param, long);
  809. /* switch off bits we can't support */
  810. #ifndef USE_SSLEAY
  811. auth &= ~CURLAUTH_NTLM; /* no NTLM without SSL */
  812. #endif
  813. #ifndef HAVE_GSSAPI
  814. auth &= ~CURLAUTH_GSSNEGOTIATE; /* no GSS-Negotiate without GSSAPI */
  815. #endif
  816. if(!auth)
  817. return CURLE_FAILED_INIT; /* no supported types left! */
  818. data->set.httpauth = auth;
  819. }
  820. break;
  821. case CURLOPT_PROXYAUTH:
  822. /*
  823. * Set HTTP Authentication type BITMASK.
  824. */
  825. {
  826. long auth = va_arg(param, long);
  827. /* switch off bits we can't support */
  828. #ifndef USE_SSLEAY
  829. auth &= ~CURLAUTH_NTLM; /* no NTLM without SSL */
  830. #endif
  831. #ifndef HAVE_GSSAPI
  832. auth &= ~CURLAUTH_GSSNEGOTIATE; /* no GSS-Negotiate without GSSAPI */
  833. #endif
  834. if(!auth)
  835. return CURLE_FAILED_INIT; /* no supported types left! */
  836. data->set.proxyauth = auth;
  837. }
  838. break;
  839. case CURLOPT_USERPWD:
  840. /*
  841. * user:password to use in the operation
  842. */
  843. data->set.userpwd = va_arg(param, char *);
  844. break;
  845. case CURLOPT_POSTQUOTE:
  846. /*
  847. * List of RAW FTP commands to use after a transfer
  848. */
  849. data->set.postquote = va_arg(param, struct curl_slist *);
  850. break;
  851. case CURLOPT_PREQUOTE:
  852. /*
  853. * List of RAW FTP commands to use prior to RETR (Wesley Laxton)
  854. */
  855. data->set.prequote = va_arg(param, struct curl_slist *);
  856. break;
  857. case CURLOPT_QUOTE:
  858. /*
  859. * List of RAW FTP commands to use before a transfer
  860. */
  861. data->set.quote = va_arg(param, struct curl_slist *);
  862. break;
  863. case CURLOPT_PROGRESSFUNCTION:
  864. /*
  865. * Progress callback function
  866. */
  867. data->set.fprogress = va_arg(param, curl_progress_callback);
  868. if(data->set.fprogress)
  869. data->progress.callback = TRUE; /* no longer internal */
  870. else
  871. data->progress.callback = FALSE; /* NULL enforces internal */
  872. break;
  873. case CURLOPT_PROGRESSDATA:
  874. /*
  875. * Custom client data to pass to the progress callback
  876. */
  877. data->set.progress_client = va_arg(param, void *);
  878. break;
  879. case CURLOPT_PROXYUSERPWD:
  880. /*
  881. * user:password needed to use the proxy
  882. */
  883. data->set.proxyuserpwd = va_arg(param, char *);
  884. break;
  885. case CURLOPT_RANGE:
  886. /*
  887. * What range of the file you want to transfer
  888. */
  889. data->set.set_range = va_arg(param, char *);
  890. break;
  891. case CURLOPT_RESUME_FROM:
  892. /*
  893. * Resume transfer at the give file position
  894. */
  895. data->set.set_resume_from = va_arg(param, long);
  896. break;
  897. case CURLOPT_RESUME_FROM_LARGE:
  898. /*
  899. * Resume transfer at the give file position
  900. */
  901. data->set.set_resume_from = va_arg(param, curl_off_t);
  902. break;
  903. case CURLOPT_DEBUGFUNCTION:
  904. /*
  905. * stderr write callback.
  906. */
  907. data->set.fdebug = va_arg(param, curl_debug_callback);
  908. /*
  909. * if the callback provided is NULL, it'll use the default callback
  910. */
  911. break;
  912. case CURLOPT_DEBUGDATA:
  913. /*
  914. * Set to a void * that should receive all error writes. This
  915. * defaults to CURLOPT_STDERR for normal operations.
  916. */
  917. data->set.debugdata = va_arg(param, void *);
  918. break;
  919. case CURLOPT_STDERR:
  920. /*
  921. * Set to a FILE * that should receive all error writes. This
  922. * defaults to stderr for normal operations.
  923. */
  924. data->set.err = va_arg(param, FILE *);
  925. if(!data->set.err)
  926. data->set.err = stderr;
  927. break;
  928. case CURLOPT_HEADERFUNCTION:
  929. /*
  930. * Set header write callback
  931. */
  932. data->set.fwrite_header = va_arg(param, curl_write_callback);
  933. break;
  934. case CURLOPT_WRITEFUNCTION:
  935. /*
  936. * Set data write callback
  937. */
  938. data->set.fwrite = va_arg(param, curl_write_callback);
  939. if(!data->set.fwrite)
  940. /* When set to NULL, reset to our internal default function */
  941. data->set.fwrite = (curl_write_callback)fwrite;
  942. break;
  943. case CURLOPT_READFUNCTION:
  944. /*
  945. * Read data callback
  946. */
  947. data->set.fread = va_arg(param, curl_read_callback);
  948. if(!data->set.fread)
  949. /* When set to NULL, reset to our internal default function */
  950. data->set.fread = (curl_read_callback)fread;
  951. break;
  952. case CURLOPT_SSLCERT:
  953. /*
  954. * String that holds file name of the SSL certificate to use
  955. */
  956. data->set.cert = va_arg(param, char *);
  957. break;
  958. case CURLOPT_SSLCERTTYPE:
  959. /*
  960. * String that holds file type of the SSL certificate to use
  961. */
  962. data->set.cert_type = va_arg(param, char *);
  963. break;
  964. case CURLOPT_SSLKEY:
  965. /*
  966. * String that holds file name of the SSL certificate to use
  967. */
  968. data->set.key = va_arg(param, char *);
  969. break;
  970. case CURLOPT_SSLKEYTYPE:
  971. /*
  972. * String that holds file type of the SSL certificate to use
  973. */
  974. data->set.key_type = va_arg(param, char *);
  975. break;
  976. case CURLOPT_SSLKEYPASSWD:
  977. /*
  978. * String that holds the SSL private key password.
  979. */
  980. data->set.key_passwd = va_arg(param, char *);
  981. break;
  982. case CURLOPT_SSLENGINE:
  983. /*
  984. * String that holds the SSL crypto engine.
  985. */
  986. #ifdef HAVE_OPENSSL_ENGINE_H
  987. {
  988. const char *cpTemp = va_arg(param, char *);
  989. ENGINE *e;
  990. if (cpTemp && cpTemp[0]) {
  991. e = ENGINE_by_id(cpTemp);
  992. if (e) {
  993. if (data->engine) {
  994. ENGINE_free(data->engine);
  995. }
  996. data->engine = e;
  997. }
  998. else {
  999. failf(data, "SSL Engine '%s' not found", cpTemp);
  1000. return CURLE_SSL_ENGINE_NOTFOUND;
  1001. }
  1002. }
  1003. }
  1004. break;
  1005. #else
  1006. return CURLE_SSL_ENGINE_NOTFOUND;
  1007. #endif
  1008. case CURLOPT_SSLENGINE_DEFAULT:
  1009. /*
  1010. * flag to set engine as default.
  1011. */
  1012. #ifdef HAVE_OPENSSL_ENGINE_H
  1013. if (data->engine) {
  1014. if (ENGINE_set_default(data->engine, ENGINE_METHOD_ALL) > 0) {
  1015. #ifdef DEBUG
  1016. fprintf(stderr,"set default crypto engine\n");
  1017. #endif
  1018. }
  1019. else {
  1020. #ifdef DEBUG
  1021. failf(data, "set default crypto engine failed");
  1022. #endif
  1023. return CURLE_SSL_ENGINE_SETFAILED;
  1024. }
  1025. }
  1026. #endif
  1027. break;
  1028. case CURLOPT_CRLF:
  1029. /*
  1030. * Kludgy option to enable CRLF convertions. Subject for removal.
  1031. */
  1032. data->set.crlf = va_arg(param, long)?TRUE:FALSE;
  1033. break;
  1034. case CURLOPT_INTERFACE:
  1035. /*
  1036. * Set what interface to bind to when performing an operation and thus
  1037. * what from-IP your connection will use.
  1038. */
  1039. data->set.device = va_arg(param, char *);
  1040. break;
  1041. case CURLOPT_KRB4LEVEL:
  1042. /*
  1043. * A string that defines the krb4 security level.
  1044. */
  1045. data->set.krb4_level = va_arg(param, char *);
  1046. data->set.krb4=data->set.krb4_level?TRUE:FALSE;
  1047. break;
  1048. case CURLOPT_SSL_VERIFYPEER:
  1049. /*
  1050. * Enable peer SSL verifying.
  1051. */
  1052. data->set.ssl.verifypeer = va_arg(param, long);
  1053. break;
  1054. case CURLOPT_SSL_VERIFYHOST:
  1055. /*
  1056. * Enable verification of the CN contained in the peer certificate
  1057. */
  1058. data->set.ssl.verifyhost = va_arg(param, long);
  1059. break;
  1060. case CURLOPT_SSL_CTX_FUNCTION:
  1061. /*
  1062. * Set a SSL_CTX callback
  1063. */
  1064. data->set.ssl.fsslctx = va_arg(param, curl_ssl_ctx_callback);
  1065. break;
  1066. case CURLOPT_SSL_CTX_DATA:
  1067. /*
  1068. * Set a SSL_CTX callback parameter pointer
  1069. */
  1070. data->set.ssl.fsslctxp = va_arg(param, void *);
  1071. break;
  1072. case CURLOPT_CAINFO:
  1073. /*
  1074. * Set CA info for SSL connection. Specify file name of the CA certificate
  1075. */
  1076. data->set.ssl.CAfile = va_arg(param, char *);
  1077. break;
  1078. case CURLOPT_CAPATH:
  1079. /*
  1080. * Set CA path info for SSL connection. Specify directory name of the CA
  1081. * certificates which have been prepared using openssl c_rehash utility.
  1082. */
  1083. /* This does not work on windows. */
  1084. data->set.ssl.CApath = va_arg(param, char *);
  1085. break;
  1086. case CURLOPT_TELNETOPTIONS:
  1087. /*
  1088. * Set a linked list of telnet options
  1089. */
  1090. data->set.telnet_options = va_arg(param, struct curl_slist *);
  1091. break;
  1092. case CURLOPT_BUFFERSIZE:
  1093. /*
  1094. * The application kindly asks for a differently sized receive buffer.
  1095. * If it seems reasonable, we'll use it.
  1096. */
  1097. data->set.buffer_size = va_arg(param, long);
  1098. if((data->set.buffer_size> (BUFSIZE -1 )) ||
  1099. (data->set.buffer_size < 1))
  1100. data->set.buffer_size = 0; /* huge internal default */
  1101. break;
  1102. case CURLOPT_NOSIGNAL:
  1103. /*
  1104. * The application asks not to set any signal() or alarm() handlers,
  1105. * even when using a timeout.
  1106. */
  1107. data->set.no_signal = va_arg(param, long) ? TRUE : FALSE;
  1108. break;
  1109. case CURLOPT_SHARE:
  1110. {
  1111. struct Curl_share *set;
  1112. set = va_arg(param, struct Curl_share *);
  1113. /* disconnect from old share, if any */
  1114. if(data->share) {
  1115. Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
  1116. if(data->share->hostcache == data->hostcache)
  1117. data->hostcache = NULL;
  1118. if(data->share->cookies == data->cookies)
  1119. data->cookies = NULL;
  1120. data->share->dirty--;
  1121. Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
  1122. data->share = NULL;
  1123. }
  1124. /* use new share if it set */
  1125. data->share = set;
  1126. if(data->share) {
  1127. Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
  1128. data->share->dirty++;
  1129. if(data->share->hostcache) {
  1130. /* use shared host cache, first free own one if any */
  1131. if(data->hostcache)
  1132. Curl_hash_destroy(data->hostcache);
  1133. data->hostcache = data->share->hostcache;
  1134. }
  1135. if(data->share->cookies) {
  1136. /* use shared cookie list, first free own one if any */
  1137. if (data->cookies)
  1138. Curl_cookie_cleanup(data->cookies);
  1139. data->cookies = data->share->cookies;
  1140. }
  1141. Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
  1142. }
  1143. /* check cookie list is set */
  1144. if(!data->cookies)
  1145. data->cookies = Curl_cookie_init(data, NULL, NULL, TRUE );
  1146. /* check for host cache not needed,
  1147. * it will be done by curl_easy_perform */
  1148. }
  1149. break;
  1150. case CURLOPT_PROXYTYPE:
  1151. /*
  1152. * Set proxy type. HTTP/SOCKS4/SOCKS5
  1153. */
  1154. data->set.proxytype = (curl_proxytype)va_arg(param, long);
  1155. break;
  1156. case CURLOPT_PRIVATE:
  1157. /*
  1158. * Set private data pointer.
  1159. */
  1160. data->set.private = va_arg(param, char *);
  1161. break;
  1162. case CURLOPT_HTTP200ALIASES:
  1163. /*
  1164. * Set a list of aliases for HTTP 200 in response header
  1165. */
  1166. data->set.http200aliases = va_arg(param, struct curl_slist *);
  1167. break;
  1168. case CURLOPT_MAXFILESIZE:
  1169. /*
  1170. * Set the maximum size of a file to download.
  1171. */
  1172. data->set.max_filesize = va_arg(param, long);
  1173. break;
  1174. case CURLOPT_FTP_SSL:
  1175. /*
  1176. * Make FTP transfers attempt to use SSL/TLS.
  1177. */
  1178. data->set.ftp_ssl = (curl_ftpssl)va_arg(param, long);
  1179. break;
  1180. case CURLOPT_IPRESOLVE:
  1181. data->set.ip_version = va_arg(param, long);
  1182. break;
  1183. case CURLOPT_MAXFILESIZE_LARGE:
  1184. /*
  1185. * Set the maximum size of a file to download.
  1186. */
  1187. data->set.max_filesize = va_arg(param, curl_off_t);
  1188. break;
  1189. case CURLOPT_TCP_NODELAY:
  1190. /*
  1191. * Enable or disable TCP_NODELAY, which will disable/enable the Nagle
  1192. * algorithm
  1193. */
  1194. data->tcp_nodelay = va_arg(param, long);
  1195. break;
  1196. default:
  1197. /* unknown tag and its companion, just ignore: */
  1198. return CURLE_FAILED_INIT; /* correct this */
  1199. }
  1200. return CURLE_OK;
  1201. }
  1202. CURLcode Curl_disconnect(struct connectdata *conn)
  1203. {
  1204. if(!conn)
  1205. return CURLE_OK; /* this is closed and fine already */
  1206. /*
  1207. * The range string is usually freed in curl_done(), but we might
  1208. * get here *instead* if we fail prematurely. Thus we need to be able
  1209. * to free this resource here as well.
  1210. */
  1211. if(conn->bits.rangestringalloc) {
  1212. free(conn->range);
  1213. conn->bits.rangestringalloc = FALSE;
  1214. }
  1215. if((conn->ntlm.state != NTLMSTATE_NONE) ||
  1216. (conn->proxyntlm.state != NTLMSTATE_NONE))
  1217. /* Authentication data is a mix of connection-related and sessionhandle-
  1218. related stuff. NTLM is connection-related so when we close the shop
  1219. we shall forget. */
  1220. conn->data->state.authstage = 0;
  1221. if(conn->curl_disconnect)
  1222. /* This is set if protocol-specific cleanups should be made */
  1223. conn->curl_disconnect(conn);
  1224. if(-1 != conn->connectindex) {
  1225. /* unlink ourselves! */
  1226. infof(conn->data, "Closing connection #%d\n", conn->connectindex);
  1227. conn->data->state.connects[conn->connectindex] = NULL;
  1228. }
  1229. Curl_safefree(conn->proto.generic);
  1230. Curl_safefree(conn->newurl);
  1231. Curl_safefree(conn->path); /* the URL path part */
  1232. Curl_SSL_Close(conn);
  1233. /* close possibly still open sockets */
  1234. if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET])
  1235. sclose(conn->sock[SECONDARYSOCKET]);
  1236. if(CURL_SOCKET_BAD != conn->sock[FIRSTSOCKET])
  1237. sclose(conn->sock[FIRSTSOCKET]);
  1238. Curl_safefree(conn->user);
  1239. Curl_safefree(conn->passwd);
  1240. Curl_safefree(conn->proxyuser);
  1241. Curl_safefree(conn->proxypasswd);
  1242. Curl_safefree(conn->allocptr.proxyuserpwd);
  1243. Curl_safefree(conn->allocptr.uagent);
  1244. Curl_safefree(conn->allocptr.userpwd);
  1245. Curl_safefree(conn->allocptr.accept_encoding);
  1246. Curl_safefree(conn->allocptr.rangeline);
  1247. Curl_safefree(conn->allocptr.ref);
  1248. Curl_safefree(conn->allocptr.cookie);
  1249. Curl_safefree(conn->allocptr.host);
  1250. Curl_safefree(conn->allocptr.cookiehost);
  1251. Curl_safefree(conn->proxyhost);
  1252. #if defined(USE_ARES) || defined(USE_THREADING_GETHOSTBYNAME)
  1253. /* possible left-overs from the async name resolve */
  1254. Curl_safefree(conn->async.hostname);
  1255. Curl_safefree(conn->async.os_specific);
  1256. #endif
  1257. Curl_free_ssl_config(&conn->ssl_config);
  1258. free(conn); /* free all the connection oriented data */
  1259. return CURLE_OK;
  1260. }
  1261. /*
  1262. * This function should return TRUE if the socket is to be assumed to
  1263. * be dead. Most commonly this happens when the server has closed the
  1264. * connection due to inactivity.
  1265. */
  1266. static bool SocketIsDead(curl_socket_t sock)
  1267. {
  1268. int sval;
  1269. bool ret_val = TRUE;
  1270. fd_set check_set;
  1271. struct timeval to;
  1272. FD_ZERO(&check_set);
  1273. FD_SET(sock, &check_set);
  1274. to.tv_sec = 0;
  1275. to.tv_usec = 0;
  1276. sval = select(sock + 1, &check_set, 0, 0, &to);
  1277. if(sval == 0)
  1278. /* timeout */
  1279. ret_val = FALSE;
  1280. return ret_val;
  1281. }
  1282. /*
  1283. * Given one filled in connection struct (named needle), this function should
  1284. * detect if there already is one that have all the significant details
  1285. * exactly the same and thus should be used instead.
  1286. */
  1287. static bool
  1288. ConnectionExists(struct SessionHandle *data,
  1289. struct connectdata *needle,
  1290. struct connectdata **usethis)
  1291. {
  1292. long i;
  1293. struct connectdata *check;
  1294. for(i=0; i< data->state.numconnects; i++) {
  1295. bool match = FALSE;
  1296. /*
  1297. * Note that if we use a HTTP proxy, we check connections to that
  1298. * proxy and not to the actual remote server.
  1299. */
  1300. check = data->state.connects[i];
  1301. if(!check)
  1302. /* NULL pointer means not filled-in entry */
  1303. continue;
  1304. if((needle->protocol&PROT_SSL) != (check->protocol&PROT_SSL))
  1305. /* don't do mixed SSL and non-SSL connections */
  1306. continue;
  1307. if(!needle->bits.httpproxy || needle->protocol&PROT_SSL) {
  1308. /* The requested connection does not use a HTTP proxy or it
  1309. uses SSL. */
  1310. if(!(needle->protocol&PROT_SSL) && check->bits.httpproxy)
  1311. /* we don't do SSL but the cached connection has a proxy,
  1312. then don't match this */
  1313. continue;
  1314. if(strequal(needle->protostr, check->protostr) &&
  1315. strequal(needle->name, check->name) &&
  1316. (needle->remote_port == check->remote_port) ) {
  1317. if(needle->protocol & PROT_SSL) {
  1318. /* This is SSL, verify that we're using the same
  1319. ssl options as well */
  1320. if(!Curl_ssl_config_matches(&needle->ssl_config,
  1321. &check->ssl_config)) {
  1322. continue;
  1323. }
  1324. }
  1325. if((needle->protocol & PROT_FTP) ||
  1326. ((needle->protocol & PROT_HTTP) &&
  1327. (needle->data->state.authwant==CURLAUTH_NTLM))) {
  1328. /* This is FTP or HTTP+NTLM, verify that we're using the same name
  1329. and password as well */
  1330. if(!strequal(needle->user, check->user) ||
  1331. !strequal(needle->passwd, check->passwd)) {
  1332. /* one of them was different */
  1333. continue;
  1334. }
  1335. }
  1336. match = TRUE;
  1337. }
  1338. }
  1339. else { /* The requested needle connection is using a proxy,
  1340. is the checked one using the same? */
  1341. if(check->bits.httpproxy &&
  1342. strequal(needle->proxyhost, check->proxyhost) &&
  1343. needle->port == check->port) {
  1344. /* This is the same proxy connection, use it! */
  1345. match = TRUE;
  1346. }
  1347. }
  1348. if(match) {
  1349. bool dead = SocketIsDead(check->sock[FIRSTSOCKET]);
  1350. if(dead) {
  1351. /*
  1352. */
  1353. infof(data, "Connection %d seems to be dead!\n", i);
  1354. Curl_disconnect(check); /* disconnect resources */
  1355. data->state.connects[i]=NULL; /* nothing here */
  1356. /* There's no need to continue searching, because we only store
  1357. one connection for each unique set of identifiers */
  1358. return FALSE;
  1359. }
  1360. *usethis = check;
  1361. return TRUE; /* yes, we found one to use! */
  1362. }
  1363. }
  1364. return FALSE; /* no matching connecting exists */
  1365. }
  1366. /*
  1367. * This function frees/closes a connection in the connection cache. This
  1368. * should take the previously set policy into account when deciding which
  1369. * of the connections to kill.
  1370. */
  1371. static int
  1372. ConnectionKillOne(struct SessionHandle *data)
  1373. {
  1374. long i;
  1375. struct connectdata *conn;
  1376. int highscore=-1;
  1377. int connindex=-1;
  1378. int score;
  1379. struct timeval now;
  1380. now = Curl_tvnow();
  1381. for(i=0; i< data->state.numconnects; i++) {
  1382. conn = data->state.connects[i];
  1383. if(!conn)
  1384. continue;
  1385. /*
  1386. * By using the set policy, we score each connection.
  1387. */
  1388. switch(data->set.closepolicy) {
  1389. case CURLCLOSEPOLICY_LEAST_RECENTLY_USED:
  1390. default:
  1391. /*
  1392. * Set higher score for the age passed since the connection
  1393. * was used.
  1394. */
  1395. score = Curl_tvdiff(now, conn->now);
  1396. break;
  1397. case CURLCLOSEPOLICY_OLDEST:
  1398. /*
  1399. * Set higher score for the age passed since the connection
  1400. * was created.
  1401. */
  1402. score = Curl_tvdiff(now, conn->created);
  1403. break;
  1404. }
  1405. if(score > highscore) {
  1406. highscore = score;
  1407. connindex = i;
  1408. }
  1409. }
  1410. if(connindex >= 0) {
  1411. /* the winner gets the honour of being disconnected */
  1412. (void) Curl_disconnect(data->state.connects[connindex]);
  1413. /* clean the array entry */
  1414. data->state.connects[connindex] = NULL;
  1415. }
  1416. return connindex; /* return the available index or -1 */
  1417. }
  1418. /*
  1419. * The given input connection struct pointer is to be stored. If the "cache"
  1420. * is already full, we must clean out the most suitable using the previously
  1421. * set policy.
  1422. *
  1423. * The given connection should be unique. That must've been checked prior to
  1424. * this call.
  1425. */
  1426. static unsigned int
  1427. ConnectionStore(struct SessionHandle *data,
  1428. struct connectdata *conn)
  1429. {
  1430. long i;
  1431. for(i=0; i< data->state.numconnects; i++) {
  1432. if(!data->state.connects[i])
  1433. break;
  1434. }
  1435. if(i == data->state.numconnects) {
  1436. /* there was no room available, kill one */
  1437. i = ConnectionKillOne(data);
  1438. infof(data, "Connection (#%d) was killed to make room\n", i);
  1439. }
  1440. if(-1 != i) {
  1441. /* only do this if a true index was returned, if -1 was returned there
  1442. is no room in the cache for an unknown reason and we cannot store
  1443. this there. */
  1444. data->state.connects[i] = conn; /* fill in this */
  1445. conn->connectindex = i; /* make the child know where the pointer to this
  1446. particular data is stored */
  1447. }
  1448. return i;
  1449. }
  1450. /*
  1451. * This function logs in to a SOCKS5 proxy and sends the specifies the final
  1452. * desitination server.
  1453. */
  1454. static int handleSock5Proxy(const char *proxy_name,
  1455. const char *proxy_password,
  1456. struct connectdata *conn)
  1457. {
  1458. /*
  1459. According to the RFC1928, section "6. Replies". This is what a SOCK5
  1460. replies:
  1461. +----+-----+-------+------+----------+----------+
  1462. |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
  1463. +----+-----+-------+------+----------+----------+
  1464. | 1 | 1 | X'00' | 1 | Variable | 2 |
  1465. +----+-----+-------+------+----------+----------+
  1466. Where:
  1467. o VER protocol version: X'05'
  1468. o REP Reply field:
  1469. o X'00' succeeded
  1470. */
  1471. unsigned char socksreq[600]; /* room for large user/pw (255 max each) */
  1472. ssize_t actualread;
  1473. ssize_t written;
  1474. int result;
  1475. CURLcode code;
  1476. int sock = conn->sock[FIRSTSOCKET];
  1477. Curl_nonblock(sock, FALSE);
  1478. socksreq[0] = 5; /* version */
  1479. socksreq[1] = (char)(proxy_name ? 2 : 1); /* number of methods (below) */
  1480. socksreq[2] = 0; /* no authentication */
  1481. socksreq[3] = 2; /* username/password */
  1482. code = Curl_write(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
  1483. &written);
  1484. if ((code != CURLE_OK) || (written != (2 + (int)socksreq[1]))) {
  1485. failf(conn->data, "Unable to send initial SOCKS5 request.");
  1486. return 1;
  1487. }
  1488. result=Curl_read(conn, sock, (char *)socksreq, 2, &actualread);
  1489. if ((result != CURLE_OK) || (actualread != 2)) {
  1490. failf(conn->data, "Unable to receive initial SOCKS5 response.");
  1491. return 1;
  1492. }
  1493. if (socksreq[0] != 5) {
  1494. failf(conn->data, "Received invalid version in initial SOCKS5 response.");
  1495. return 1;
  1496. }
  1497. if (socksreq[1] == 0) {
  1498. /* Nothing to do, no authentication needed */
  1499. ;
  1500. }
  1501. else if (socksreq[1] == 2) {
  1502. /* Needs user name and password */
  1503. int userlen, pwlen, len;
  1504. userlen = strlen(proxy_name);
  1505. pwlen = proxy_password?strlen(proxy_password):0;
  1506. /* username/password request looks like
  1507. * +----+------+----------+------+----------+
  1508. * |VER | ULEN | UNAME | PLEN | PASSWD |
  1509. * +----+------+----------+------+----------+
  1510. * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
  1511. * +----+------+----------+------+----------+
  1512. */
  1513. len = 0;
  1514. socksreq[len++] = 1; /* username/pw subnegotiation version */
  1515. socksreq[len++] = (char) userlen;
  1516. memcpy(socksreq + len, proxy_name, (int) userlen);
  1517. len += userlen;
  1518. socksreq[len++] = (char) pwlen;
  1519. memcpy(socksreq + len, proxy_password, (int) pwlen);
  1520. len += pwlen;
  1521. code = Curl_write(conn, sock, (char *)socksreq, len, &written);
  1522. if ((code != CURLE_OK) || (len != written)) {
  1523. failf(conn->data, "Failed to send SOCKS5 sub-negotiation request.");
  1524. return 1;
  1525. }
  1526. result=Curl_read(conn, sock, (char *)socksreq, 2, &actualread);
  1527. if ((result != CURLE_OK) || (actualread != 2)) {
  1528. failf(conn->data, "Unable to receive SOCKS5 sub-negotiation response.");
  1529. return 1;
  1530. }
  1531. if ((socksreq[0] != 5) || /* version */
  1532. (socksreq[1] != 0)) { /* status */
  1533. failf(conn->data, "User was rejected by the SOCKS5 server (%d %d).",
  1534. socksreq[0], socksreq[1]);
  1535. return 1;
  1536. }
  1537. /* Everything is good so far, user was authenticated! */
  1538. }
  1539. else {
  1540. /* error */
  1541. if (socksreq[1] == 1) {
  1542. failf(conn->data,
  1543. "SOCKS5 GSSAPI per-message authentication is not supported.");
  1544. return 1;
  1545. }
  1546. else if (socksreq[1] == 255) {
  1547. if (proxy_name[0] == 0) {
  1548. failf(conn->data,
  1549. "No authentication method was acceptable. (It is quite likely"
  1550. " that the SOCKS5 server wanted a username/password, since none"
  1551. " was supplied to the server on this connection.)");
  1552. }
  1553. else {
  1554. failf(conn->data, "No authentication method was acceptable.");
  1555. }
  1556. return 1;
  1557. }
  1558. else {
  1559. failf(conn->data,
  1560. "Undocumented SOCKS5 mode attempted to be used by server.");
  1561. return 1;
  1562. }
  1563. }
  1564. /* Authentication is complete, now specify destination to the proxy */
  1565. socksreq[0] = 5; /* version (SOCKS5) */
  1566. socksreq[1] = 1; /* connect */
  1567. socksreq[2] = 0; /* must be zero */
  1568. socksreq[3] = 1; /* IPv4 = 1 */
  1569. {
  1570. #ifndef ENABLE_IPV6
  1571. struct Curl_dns_entry *dns;
  1572. Curl_addrinfo *hp=NULL;
  1573. int rc = Curl_resolv(conn, conn->hostname, conn->remote_port, &dns);
  1574. if(rc == -1)
  1575. return 1;
  1576. if(rc == 1)
  1577. /* this requires that we're in "wait for resolve" state */
  1578. rc = Curl_wait_for_resolv(conn, &dns);
  1579. /*
  1580. * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
  1581. * returns a Curl_addrinfo pointer that may not always look the same.
  1582. */
  1583. if(dns)
  1584. hp=dns->addr;
  1585. if (hp && hp->h_addr_list[0]) {
  1586. socksreq[4] = ((char*)hp->h_addr_list[0])[0];
  1587. socksreq[5] = ((char*)hp->h_addr_list[0])[1];
  1588. socksreq[6] = ((char*)hp->h_addr_list[0])[2];
  1589. socksreq[7] = ((char*)hp->h_addr_list[0])[3];
  1590. Curl_resolv_unlock(conn->data, dns); /* not used anymore from now on */
  1591. }
  1592. else {
  1593. failf(conn->data, "Failed to resolve \"%s\" for SOCKS5 connect.",
  1594. conn->hostname);
  1595. return 1;
  1596. }
  1597. #else
  1598. failf(conn->data,
  1599. "%s:%d has an internal error an needs to be fixed to work",
  1600. __FILE__, __LINE__);
  1601. return 1;
  1602. #endif
  1603. }
  1604. *((unsigned short*)&socksreq[8]) = htons(conn->remote_port);
  1605. {
  1606. const int packetsize = 10;
  1607. code = Curl_write(conn, sock, (char *)socksreq, packetsize, &written);
  1608. if ((code != CURLE_OK) || (written != packetsize)) {
  1609. failf(conn->data, "Failed to send SOCKS5 connect request.");
  1610. return 1;
  1611. }
  1612. result = Curl_read(conn, sock, (char *)socksreq, packetsize, &actualread);
  1613. if ((result != CURLE_OK) || (actualread != packetsize)) {
  1614. failf(conn->data, "Failed to receive SOCKS5 connect request ack.");
  1615. return 1;
  1616. }
  1617. if (socksreq[0] != 5) { /* version */
  1618. failf(conn->data,
  1619. "SOCKS5 reply has wrong version, version should be 5.");
  1620. return 1;
  1621. }
  1622. if (socksreq[1] != 0) { /* Anything besides 0 is an error */
  1623. failf(conn->data,
  1624. "Can't complete SOCKS5 connection to %d.%d.%d.%d:%d. (%d)",
  1625. (unsigned char)socksreq[4], (unsigned char)socksreq[5],
  1626. (unsigned char)socksreq[6], (unsigned char)socksreq[7],
  1627. (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
  1628. socksreq[1]);
  1629. return 1;
  1630. }
  1631. }
  1632. Curl_nonblock(sock, TRUE);
  1633. return 0; /* Proxy was successful! */
  1634. }
  1635. static CURLcode ConnectPlease(struct connectdata *conn,
  1636. struct Curl_dns_entry *hostaddr,
  1637. bool *connected)
  1638. {
  1639. CURLcode result;
  1640. Curl_ipconnect *addr;
  1641. /*************************************************************
  1642. * Connect to server/proxy
  1643. *************************************************************/
  1644. result= Curl_connecthost(conn,
  1645. hostaddr,
  1646. conn->port,
  1647. &conn->sock[FIRSTSOCKET],
  1648. &addr,
  1649. connected);
  1650. if(CURLE_OK == result) {
  1651. /* All is cool, then we store the current information from the hostaddr
  1652. struct to the serv_addr, as it might be needed later. The address
  1653. returned from the function above is crucial here. */
  1654. conn->connect_addr = hostaddr;
  1655. #ifdef ENABLE_IPV6
  1656. conn->serv_addr = addr;
  1657. #else
  1658. memset((char *) &conn->serv_addr, '\0', sizeof(conn->serv_addr));
  1659. memcpy((char *)&(conn->serv_addr.sin_addr),
  1660. (struct in_addr *)addr, sizeof(struct in_addr));
  1661. conn->serv_addr.sin_family = hostaddr->addr->h_addrtype;
  1662. conn->serv_addr.sin_port = htons((unsigned short)conn->port);
  1663. #endif
  1664. if (conn->data->set.proxytype == CURLPROXY_SOCKS5) {
  1665. return handleSock5Proxy(conn->proxyuser,
  1666. conn->proxypasswd,
  1667. conn) ?
  1668. CURLE_COULDNT_CONNECT : CURLE_OK;
  1669. }
  1670. else if (conn->data->set.proxytype == CURLPROXY_HTTP) {
  1671. /* do nothing here. handled later. */
  1672. }
  1673. else {
  1674. failf(conn->data, "unknown proxytype option given");
  1675. return CURLE_COULDNT_CONNECT;
  1676. }
  1677. }
  1678. return result;
  1679. }
  1680. /*
  1681. * ALERT! The 'dns' pointer being passed in here might be NULL at times.
  1682. */
  1683. static void verboseconnect(struct connectdata *conn,
  1684. struct Curl_dns_entry *dns)
  1685. {
  1686. struct SessionHandle *data = conn->data;
  1687. /* Figure out the ip-number and display the first host name it shows: */
  1688. #ifdef ENABLE_IPV6
  1689. {
  1690. char hbuf[NI_MAXHOST];
  1691. #ifdef NI_WITHSCOPEID
  1692. #define NIFLAGS NI_NUMERICHOST | NI_WITHSCOPEID
  1693. #else
  1694. #define NIFLAGS NI_NUMERICHOST
  1695. #endif
  1696. if(dns) {
  1697. struct addrinfo *ai = dns->addr;
  1698. /* Lookup the name of the given address. This should probably be remade
  1699. to use the DNS cache instead, as the host name is most likely cached
  1700. already. */
  1701. if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf), NULL, 0,
  1702. NIFLAGS)) {
  1703. snprintf(hbuf, sizeof(hbuf), "unknown");
  1704. }
  1705. else {
  1706. if (ai->ai_canonname) {
  1707. infof(data, "Connected to %s (%s) port %d\n", ai->ai_canonname, hbuf,
  1708. conn->port);
  1709. return;
  1710. }
  1711. }
  1712. }
  1713. else {
  1714. snprintf(hbuf, sizeof(hbuf), "same host");
  1715. }
  1716. infof(data, "Connected to %s port %d\n", hbuf, conn->port);
  1717. }
  1718. #else
  1719. {
  1720. #ifdef HAVE_INET_NTOA_R
  1721. char ntoa_buf[64];
  1722. #endif
  1723. Curl_addrinfo *hostaddr=dns?dns->addr:NULL;
  1724. struct in_addr in;
  1725. (void) memcpy(&in.s_addr, &conn->serv_addr.sin_addr, sizeof (in.s_addr));
  1726. infof(data, "Connected to %s (%s) port %d\n",
  1727. hostaddr?hostaddr->h_name:"",
  1728. #if defined(HAVE_INET_NTOA_R)
  1729. inet_ntoa_r(in, ntoa_buf, sizeof(ntoa_buf)),
  1730. #else
  1731. inet_ntoa(in),
  1732. #endif
  1733. conn->port);
  1734. }
  1735. #endif
  1736. }
  1737. /*
  1738. * We have discovered that the TCP connection has been successful, we can now
  1739. * proceed with some action.
  1740. *
  1741. * If we're using the multi interface, this host address pointer is most
  1742. * likely NULL at this point as we can't keep the resolved info around. This
  1743. * may call for some reworking, like a reference counter in the struct or
  1744. * something. The hostaddr is not used for very much though, we have the
  1745. * 'serv_addr' field in the connectdata struct for most of it.
  1746. */
  1747. CURLcode Curl_protocol_connect(struct connectdata *conn,
  1748. struct Curl_dns_entry *hostaddr)
  1749. {
  1750. struct SessionHandle *data = conn->data;
  1751. CURLcode result=CURLE_OK;
  1752. if(conn->bits.tcpconnect)
  1753. /* We already are connected, get back. This may happen when the connect
  1754. worked fine in the first call, like when we connect to a local server
  1755. or proxy. */
  1756. return CURLE_OK;
  1757. Curl_pgrsTime(data, TIMER_CONNECT); /* connect done */
  1758. if(data->set.verbose)
  1759. verboseconnect(conn, hostaddr);
  1760. if(conn->curl_connect) {
  1761. /* is there a protocol-specific connect() procedure? */
  1762. /* set start time here for timeout purposes in the
  1763. * connect procedure, it is later set again for the
  1764. * progress meter purpose */
  1765. conn->now = Curl_tvnow();
  1766. /* Call the protocol-specific connect function */
  1767. result = conn->curl_connect(conn);
  1768. }
  1769. return result; /* pass back status */
  1770. }
  1771. /*
  1772. * CreateConnection() sets up a new connectdata struct, or re-uses an already
  1773. * existing one, and resolves host name.
  1774. *
  1775. * if this function returns CURLE_OK and *async is set to TRUE, the resolve
  1776. * response will be coming asynchronously. If *async is FALSE, the name is
  1777. * already resolved.
  1778. */
  1779. static CURLcode CreateConnection(struct SessionHandle *data,
  1780. struct connectdata **in_connect,
  1781. struct Curl_dns_entry **addr,
  1782. bool *async)
  1783. {
  1784. char *tmp;
  1785. CURLcode result=CURLE_OK;
  1786. char resumerange[40]="";
  1787. struct connectdata *conn;
  1788. struct connectdata *conn_temp;
  1789. int urllen;
  1790. struct Curl_dns_entry *hostaddr;
  1791. #ifdef HAVE_ALARM
  1792. unsigned int prev_alarm=0;
  1793. #endif
  1794. char endbracket;
  1795. char user[MAX_CURL_USER_LENGTH];
  1796. char passwd[MAX_CURL_PASSWORD_LENGTH];
  1797. int rc;
  1798. bool reuse;
  1799. #ifdef HAVE_SIGACTION
  1800. struct sigaction keep_sigact; /* store the old struct here */
  1801. bool keep_copysig=FALSE; /* did copy it? */
  1802. #else
  1803. #ifdef HAVE_SIGNAL
  1804. void *keep_sigact; /* store the old handler here */
  1805. #endif
  1806. #endif
  1807. *addr = NULL; /* nothing yet */
  1808. *async = FALSE;
  1809. /*************************************************************
  1810. * Check input data
  1811. *************************************************************/
  1812. if(!data->change.url)
  1813. return CURLE_URL_MALFORMAT;
  1814. /* First, split up the current URL in parts so that we can use the
  1815. parts for checking against the already present connections. In order
  1816. to not have to modify everything at once, we allocate a temporary
  1817. connection data struct and fill in for comparison purposes. */
  1818. conn = (struct connectdata *)malloc(sizeof(struct connectdata));
  1819. if(!conn) {
  1820. *in_connect = NULL; /* clear the pointer */
  1821. return CURLE_OUT_OF_MEMORY;
  1822. }
  1823. /* We must set the return variable as soon as possible, so that our
  1824. parent can cleanup any possible allocs we may have done before
  1825. any failure */
  1826. *in_connect = conn;
  1827. /* we have to init the struct */
  1828. memset(conn, 0, sizeof(struct connectdata));
  1829. /* and we setup a few fields in case we end up actually using this struct */
  1830. conn->data = data; /* remember our daddy */
  1831. conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
  1832. conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
  1833. conn->connectindex = -1; /* no index */
  1834. conn->bits.httpproxy = (data->change.proxy && *data->change.proxy &&
  1835. (data->set.proxytype == CURLPROXY_HTTP))?
  1836. TRUE:FALSE; /* http proxy or not */
  1837. /* Default protocol-independent behavior doesn't support persistant
  1838. connections, so we set this to force-close. Protocols that support
  1839. this need to set this to FALSE in their "curl_do" functions. */
  1840. conn->bits.close = TRUE;
  1841. /* maxdownload must be -1 on init, as 0 is a valid value! */
  1842. conn->maxdownload = -1; /* might have been used previously! */
  1843. /* Store creation time to help future close decision making */
  1844. conn->created = Curl_tvnow();
  1845. conn->bits.use_range = data->set.set_range?TRUE:FALSE; /* range status */
  1846. conn->range = data->set.set_range; /* clone the range setting */
  1847. conn->resume_from = data->set.set_resume_from; /* inherite resume_from */
  1848. /* Set the start time temporary to this creation time to allow easier
  1849. timeout checks before the transfer has started for real. The start time
  1850. is later set "for real" using Curl_pgrsStartNow(). */
  1851. conn->data->progress.start = conn->created;
  1852. conn->bits.user_passwd = data->set.userpwd?1:0;
  1853. conn->bits.proxy_user_passwd = data->set.proxyuserpwd?1:0;
  1854. /* This initing continues below, see the comment "Continue connectdata
  1855. * initialization here" */
  1856. /***********************************************************
  1857. * We need to allocate memory to store the path in. We get the size of the
  1858. * full URL to be sure, and we need to make it at least 256 bytes since
  1859. * other parts of the code will rely on this fact
  1860. ***********************************************************/
  1861. #define LEAST_PATH_ALLOC 256
  1862. urllen=strlen(data->change.url);
  1863. if(urllen < LEAST_PATH_ALLOC)
  1864. urllen=LEAST_PATH_ALLOC;
  1865. conn->path=(char *)malloc(urllen);
  1866. if(NULL == conn->path)
  1867. return CURLE_OUT_OF_MEMORY; /* really bad error */
  1868. /*************************************************************
  1869. * Parse the URL.
  1870. *
  1871. * We need to parse the url even when using the proxy, because we will need
  1872. * the hostname and port in case we are trying to SSL connect through the
  1873. * proxy -- and we don't know if we will need to use SSL until we parse the
  1874. * url ...
  1875. ************************************************************/
  1876. if((2 == sscanf(data->change.url, "%64[^:]:%[^\n]",
  1877. conn->protostr,
  1878. conn->path)) && strequal(conn->protostr, "file")) {
  1879. if(conn->path[0] == '/' && conn->path[1] == '/') {
  1880. /* Allow omitted hostname (e.g. file:/<path>). This is not strictly
  1881. * speaking a valid file: URL by RFC 1738, but treating file:/<path> as
  1882. * file://localhost/<path> is similar to how other schemes treat missing
  1883. * hostnames. See RFC 1808. */
  1884. /* This cannot be done with strcpy() in a portable manner, since the
  1885. memory areas overlap! */
  1886. memmove(conn->path, conn->path + 2, strlen(conn->path + 2)+1);
  1887. }
  1888. /*
  1889. * we deal with file://<host>/<path> differently since it supports no
  1890. * hostname other than "localhost" and "127.0.0.1", which is unique among
  1891. * the URL protocols specified in RFC 1738
  1892. */
  1893. if(conn->path[0] != '/') {
  1894. /* the URL included a host name, we ignore host names in file:// URLs
  1895. as the standards don't define what to do with them */
  1896. char *ptr=strchr(conn->path, '/');
  1897. if(ptr) {
  1898. /* there was a slash present
  1899. RFC1738 (section 3.1, page 5) says:
  1900. The rest of the locator consists of data specific to the scheme,
  1901. and is known as the "url-path". It supplies the details of how the
  1902. specified resource can be accessed. Note that the "/" between the
  1903. host (or port) and the url-path is NOT part of the url-path.
  1904. As most agents use file://localhost/foo to get '/foo' although the
  1905. slash preceeding foo is a separator and not a slash for the path,
  1906. a URL as file://localhost//foo must be valid as well, to refer to
  1907. the same file with an absolute path.
  1908. */
  1909. if(ptr[1] && ('/' == ptr[1]))
  1910. /* if there was two slashes, we skip the first one as that is then
  1911. used truly as a separator */
  1912. ptr++;
  1913. /* This cannot be made with strcpy, as the memory chunks overlap! */
  1914. memmove(conn->path, ptr, strlen(ptr)+1);
  1915. }
  1916. }
  1917. strcpy(conn->protostr, "file"); /* store protocol string lowercase */
  1918. }
  1919. else {
  1920. /* Set default host and default path */
  1921. strcpy(conn->gname, "curl.haxx.se");
  1922. strcpy(conn->path, "/");
  1923. /* We need to search for '/' OR '?' - whichever comes first after host
  1924. * name but before the path. We need to change that to handle things like
  1925. * http://example.com?param= (notice the missing '/'). Later we'll insert
  1926. * that missing slash at the beginning of the path.
  1927. */
  1928. if (2 > sscanf(data->change.url,
  1929. "%64[^\n:]://%512[^\n/?]%[^\n]",
  1930. conn->protostr, conn->gname, conn->path)) {
  1931. /*
  1932. * The URL was badly formatted, let's try the browser-style _without_
  1933. * protocol specified like 'http://'.
  1934. */
  1935. if((1 > sscanf(data->change.url, "%512[^\n/?]%[^\n]",
  1936. conn->gname, conn->path)) ) {
  1937. /*
  1938. * We couldn't even get this format.
  1939. */
  1940. failf(data, "<url> malformed");
  1941. return CURLE_URL_MALFORMAT;
  1942. }
  1943. /*
  1944. * Since there was no protocol part specified, we guess what protocol it
  1945. * is based on the first letters of the server name.
  1946. */
  1947. /* Note: if you add a new protocol, please update the list in
  1948. * lib/version.c too! */
  1949. if(checkprefix("GOPHER", conn->gname))
  1950. strcpy(conn->protostr, "gopher");
  1951. #ifdef USE_SSLEAY
  1952. else if(checkprefix("HTTPS", conn->gname))
  1953. strcpy(conn->protostr, "https");
  1954. else if(checkprefix("FTPS", conn->gname))
  1955. strcpy(conn->protostr, "ftps");
  1956. #endif /* USE_SSLEAY */
  1957. else if(checkprefix("FTP", conn->gname))
  1958. strcpy(conn->protostr, "ftp");
  1959. else if(checkprefix("TELNET", conn->gname))
  1960. strcpy(conn->protostr, "telnet");
  1961. else if (checkprefix("DICT", conn->gname))
  1962. strcpy(conn->protostr, "DICT");
  1963. else if (checkprefix("LDAP", conn->gname))
  1964. strcpy(conn->protostr, "LDAP");
  1965. else {
  1966. strcpy(conn->protostr, "http");
  1967. }
  1968. conn->protocol |= PROT_MISSING; /* not given in URL */
  1969. }
  1970. }
  1971. /* If the URL is malformatted (missing a '/' after hostname before path) we
  1972. * insert a slash here. The only letter except '/' we accept to start a path
  1973. * is '?'.
  1974. */
  1975. if(conn->path[0] == '?') {
  1976. /* We need this function to deal with overlapping memory areas. We know
  1977. that the memory area 'path' points to is 'urllen' bytes big and that
  1978. is bigger than the path. Use +1 to move the zero byte too. */
  1979. memmove(&conn->path[1], conn->path, strlen(conn->path)+1);
  1980. conn->path[0] = '/';
  1981. }
  1982. /*
  1983. * So if the URL was A://B/C,
  1984. * conn->protostr is A
  1985. * conn->gname is B
  1986. * conn->path is /C
  1987. */
  1988. /*************************************************************
  1989. * Take care of proxy authentication stuff
  1990. *************************************************************/
  1991. if(conn->bits.proxy_user_passwd) {
  1992. char proxyuser[MAX_CURL_USER_LENGTH]="";
  1993. char proxypasswd[MAX_CURL_PASSWORD_LENGTH]="";
  1994. sscanf(data->set.proxyuserpwd,
  1995. "%" MAX_CURL_USER_LENGTH_TXT "[^:]:"
  1996. "%" MAX_CURL_PASSWORD_LENGTH_TXT "[^\n]",
  1997. proxyuser, proxypasswd);
  1998. conn->proxyuser = strdup(proxyuser);
  1999. if(!conn->proxyuser)
  2000. return CURLE_OUT_OF_MEMORY;
  2001. conn->proxypasswd = strdup(proxypasswd);
  2002. if(!conn->proxypasswd)
  2003. return CURLE_OUT_OF_MEMORY;
  2004. }
  2005. /*************************************************************
  2006. * Set a few convenience pointers
  2007. *************************************************************/
  2008. conn->name = conn->gname;
  2009. conn->ppath = conn->path;
  2010. conn->hostname = conn->name;
  2011. /*************************************************************
  2012. * Detect what (if any) proxy to use
  2013. *************************************************************/
  2014. if(!data->change.proxy) {
  2015. /* If proxy was not specified, we check for default proxy environment
  2016. * variables, to enable i.e Lynx compliance:
  2017. *
  2018. * http_proxy=http://some.server.dom:port/
  2019. * https_proxy=http://some.server.dom:port/
  2020. * ftp_proxy=http://some.server.dom:port/
  2021. * gopher_proxy=http://some.server.dom:port/
  2022. * no_proxy=domain1.dom,host.domain2.dom
  2023. * (a comma-separated list of hosts which should
  2024. * not be proxied, or an asterisk to override
  2025. * all proxy variables)
  2026. * all_proxy=http://some.server.dom:port/
  2027. * (seems to exist for the CERN www lib. Probably
  2028. * the first to check for.)
  2029. *
  2030. * For compatibility, the all-uppercase versions of these variables are
  2031. * checked if the lowercase versions don't exist.
  2032. */
  2033. char *no_proxy=NULL;
  2034. char *no_proxy_tok_buf;
  2035. char *proxy=NULL;
  2036. char proxy_env[128];
  2037. no_proxy=curl_getenv("no_proxy");
  2038. if(!no_proxy)
  2039. no_proxy=curl_getenv("NO_PROXY");
  2040. if(!no_proxy || !strequal("*", no_proxy)) {
  2041. /* NO_PROXY wasn't specified or it wasn't just an asterisk */
  2042. char *nope;
  2043. nope=no_proxy?strtok_r(no_proxy, ", ", &no_proxy_tok_buf):NULL;
  2044. while(nope) {
  2045. unsigned int namelen;
  2046. char *endptr = strchr(conn->name, ':');
  2047. if(endptr)
  2048. namelen=endptr-conn->name;
  2049. else
  2050. namelen=strlen(conn->name);
  2051. if(strlen(nope) <= namelen) {
  2052. char *checkn=
  2053. conn->name + namelen - strlen(nope);
  2054. if(checkprefix(nope, checkn)) {
  2055. /* no proxy for this host! */
  2056. break;
  2057. }
  2058. }
  2059. nope=strtok_r(NULL, ", ", &no_proxy_tok_buf);
  2060. }
  2061. if(!nope) {
  2062. /* It was not listed as without proxy */
  2063. char *protop = conn->protostr;
  2064. char *envp = proxy_env;
  2065. char *prox;
  2066. /* Now, build <protocol>_proxy and check for such a one to use */
  2067. while(*protop)
  2068. *envp++ = tolower(*protop++);
  2069. /* append _proxy */
  2070. strcpy(envp, "_proxy");
  2071. /* read the protocol proxy: */
  2072. prox=curl_getenv(proxy_env);
  2073. /*
  2074. * We don't try the uppercase version of HTTP_PROXY because of
  2075. * security reasons:
  2076. *
  2077. * When curl is used in a webserver application
  2078. * environment (cgi or php), this environment variable can
  2079. * be controlled by the web server user by setting the
  2080. * http header 'Proxy:' to some value.
  2081. *
  2082. * This can cause 'internal' http/ftp requests to be
  2083. * arbitrarily redirected by any external attacker.
  2084. */
  2085. if(!prox && !strequal("http_proxy", proxy_env)) {
  2086. /* There was no lowercase variable, try the uppercase version: */
  2087. for(envp = proxy_env; *envp; envp++)
  2088. *envp = toupper(*envp);
  2089. prox=curl_getenv(proxy_env);
  2090. }
  2091. if(prox && *prox) { /* don't count "" strings */
  2092. proxy = prox; /* use this */
  2093. }
  2094. else {
  2095. proxy = curl_getenv("all_proxy"); /* default proxy to use */
  2096. if(!proxy)
  2097. proxy=curl_getenv("ALL_PROXY");
  2098. }
  2099. if(proxy && *proxy) {
  2100. /* we have a proxy here to set */
  2101. char *ptr;
  2102. char proxyuser[MAX_CURL_USER_LENGTH];
  2103. char proxypasswd[MAX_CURL_PASSWORD_LENGTH];
  2104. /* skip the possible protocol piece */
  2105. ptr=strstr(proxy, "://");
  2106. if(ptr)
  2107. ptr += 3;
  2108. else
  2109. ptr = proxy;
  2110. /* check for an @-letter */
  2111. ptr = strchr(ptr, '@');
  2112. if(ptr && (2 == sscanf(proxy, "%" MAX_CURL_USER_LENGTH_TXT"[^:]:"
  2113. "%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]",
  2114. proxyuser, proxypasswd))) {
  2115. /* found user and password, rip them out */
  2116. Curl_safefree(conn->proxyuser);
  2117. conn->proxyuser = strdup(proxyuser);
  2118. if(!conn->proxyuser)
  2119. return CURLE_OUT_OF_MEMORY;
  2120. Curl_safefree(conn->proxypasswd);
  2121. conn->proxypasswd = strdup(proxypasswd);
  2122. if(!conn->proxypasswd)
  2123. return CURLE_OUT_OF_MEMORY;
  2124. conn->bits.proxy_user_passwd = TRUE; /* enable it */
  2125. ptr = strdup(ptr+1);
  2126. free(proxy); /* free the former data */
  2127. proxy = ptr; /* now use this instead */
  2128. }
  2129. data->change.proxy = proxy;
  2130. data->change.proxy_alloc=TRUE; /* this needs to be freed later */
  2131. conn->bits.httpproxy = TRUE;
  2132. }
  2133. } /* if (!nope) - it wasn't specified non-proxy */
  2134. } /* NO_PROXY wasn't specified or '*' */
  2135. if(no_proxy)
  2136. free(no_proxy);
  2137. } /* if not using proxy */
  2138. /*************************************************************
  2139. * No protocol part in URL was used, add it!
  2140. *************************************************************/
  2141. if(conn->protocol&PROT_MISSING) {
  2142. /* We're guessing prefixes here and if we're told to use a proxy or if
  2143. we're gonna follow a Location: later or... then we need the protocol
  2144. part added so that we have a valid URL. */
  2145. char *reurl;
  2146. reurl = aprintf("%s://%s", conn->protostr, data->change.url);
  2147. if(!reurl)
  2148. return CURLE_OUT_OF_MEMORY;
  2149. data->change.url = reurl;
  2150. data->change.url_alloc = TRUE; /* free this later */
  2151. conn->protocol &= ~PROT_MISSING; /* switch that one off again */
  2152. }
  2153. #ifndef CURL_DISABLE_HTTP
  2154. /************************************************************
  2155. * RESUME on a HTTP page is a tricky business. First, let's just check that
  2156. * 'range' isn't used, then set the range parameter and leave the resume as
  2157. * it is to inform about this situation for later use. We will then
  2158. * "attempt" to resume, and if we're talking to a HTTP/1.1 (or later)
  2159. * server, we will get the document resumed. If we talk to a HTTP/1.0
  2160. * server, we just fail since we can't rewind the file writing from within
  2161. * this function.
  2162. ***********************************************************/
  2163. if(conn->resume_from) {
  2164. if(!conn->bits.use_range) {
  2165. /* if it already was in use, we just skip this */
  2166. snprintf(resumerange, sizeof(resumerange), "%" FORMAT_OFF_T "-",
  2167. conn->resume_from);
  2168. conn->range=strdup(resumerange); /* tell ourselves to fetch this range */
  2169. conn->bits.rangestringalloc = TRUE; /* mark as allocated */
  2170. conn->bits.use_range = 1; /* switch on range usage */
  2171. }
  2172. }
  2173. #endif
  2174. /*************************************************************
  2175. * Setup internals depending on protocol
  2176. *************************************************************/
  2177. if (strequal(conn->protostr, "HTTP")) {
  2178. #ifndef CURL_DISABLE_HTTP
  2179. conn->port = (data->set.use_port && data->state.allow_port)?
  2180. data->set.use_port:PORT_HTTP;
  2181. conn->remote_port = PORT_HTTP;
  2182. conn->protocol |= PROT_HTTP;
  2183. conn->curl_do = Curl_http;
  2184. conn->curl_do_more = NULL;
  2185. conn->curl_done = Curl_http_done;
  2186. conn->curl_connect = Curl_http_connect;
  2187. #else
  2188. failf(data, LIBCURL_NAME
  2189. " was built with HTTP disabled, http: not supported!");
  2190. return CURLE_UNSUPPORTED_PROTOCOL;
  2191. #endif
  2192. }
  2193. else if (strequal(conn->protostr, "HTTPS")) {
  2194. #if defined(USE_SSLEAY) && !defined(CURL_DISABLE_HTTP)
  2195. conn->port = (data->set.use_port && data->state.allow_port)?
  2196. data->set.use_port:PORT_HTTPS;
  2197. conn->remote_port = PORT_HTTPS;
  2198. conn->protocol |= PROT_HTTP|PROT_HTTPS|PROT_SSL;
  2199. conn->curl_do = Curl_http;
  2200. conn->curl_do_more = NULL;
  2201. conn->curl_done = Curl_http_done;
  2202. conn->curl_connect = Curl_http_connect;
  2203. #else /* USE_SSLEAY */
  2204. failf(data, LIBCURL_NAME
  2205. " was built with SSL disabled, https: not supported!");
  2206. return CURLE_UNSUPPORTED_PROTOCOL;
  2207. #endif /* !USE_SSLEAY */
  2208. }
  2209. else if (strequal(conn->protostr, "GOPHER")) {
  2210. #ifndef CURL_DISABLE_GOPHER
  2211. conn->port = (data->set.use_port && data->state.allow_port)?
  2212. data->set.use_port:PORT_GOPHER;
  2213. conn->remote_port = PORT_GOPHER;
  2214. /* Skip /<item-type>/ in path if present */
  2215. if (isdigit((int)conn->path[1])) {
  2216. conn->ppath = strchr(&conn->path[1], '/');
  2217. if (conn->ppath == NULL)
  2218. conn->ppath = conn->path;
  2219. }
  2220. conn->protocol |= PROT_GOPHER;
  2221. conn->curl_do = Curl_http;
  2222. conn->curl_do_more = NULL;
  2223. conn->curl_done = Curl_http_done;
  2224. #else
  2225. failf(data, LIBCURL_NAME
  2226. " was built with GOPHER disabled, gopher: not supported!");
  2227. #endif
  2228. }
  2229. else if(strequal(conn->protostr, "FTP") ||
  2230. strequal(conn->protostr, "FTPS")) {
  2231. /* MN 06/07/02 */
  2232. #ifndef CURL_DISABLE_FTP
  2233. char *type;
  2234. int port = PORT_FTP;
  2235. if(strequal(conn->protostr, "FTPS")) {
  2236. #ifdef USE_SSLEAY
  2237. conn->protocol |= PROT_FTPS|PROT_SSL;
  2238. conn->ssl[SECONDARYSOCKET].use = TRUE; /* send data securely */
  2239. port = PORT_FTPS;
  2240. #else
  2241. failf(data, LIBCURL_NAME
  2242. " was built with SSL disabled, ftps: not supported!");
  2243. return CURLE_UNSUPPORTED_PROTOCOL;
  2244. #endif /* !USE_SSLEAY */
  2245. }
  2246. conn->port = (data->set.use_port && data->state.allow_port)?
  2247. data->set.use_port:port;
  2248. conn->remote_port = port;
  2249. conn->protocol |= PROT_FTP;
  2250. if(data->change.proxy &&
  2251. *data->change.proxy &&
  2252. !data->set.tunnel_thru_httpproxy) {
  2253. /* Unless we have asked to tunnel ftp operations through the proxy, we
  2254. switch and use HTTP operations only */
  2255. if(conn->protocol & PROT_FTPS) {
  2256. /* FTPS is a hacked protocol and does not work through your
  2257. ordinary http proxy! */
  2258. failf(data, "ftps does not work through http proxy!");
  2259. return CURLE_UNSUPPORTED_PROTOCOL;
  2260. }
  2261. #ifndef CURL_DISABLE_HTTP
  2262. conn->curl_do = Curl_http;
  2263. conn->curl_done = Curl_http_done;
  2264. #else
  2265. failf(data, "FTP over http proxy requires HTTP support built-in!");
  2266. return CURLE_UNSUPPORTED_PROTOCOL;
  2267. #endif
  2268. }
  2269. else {
  2270. conn->curl_do = Curl_ftp;
  2271. conn->curl_do_more = Curl_ftp_nextconnect;
  2272. conn->curl_done = Curl_ftp_done;
  2273. conn->curl_connect = Curl_ftp_connect;
  2274. conn->curl_disconnect = Curl_ftp_disconnect;
  2275. }
  2276. conn->ppath++; /* don't include the initial slash */
  2277. /* FTP URLs support an extension like ";type=<typecode>" that
  2278. * we'll try to get now! */
  2279. type=strstr(conn->ppath, ";type=");
  2280. if(!type) {
  2281. type=strstr(conn->gname, ";type=");
  2282. }
  2283. if(type) {
  2284. char command;
  2285. *type=0; /* it was in the middle of the hostname */
  2286. command = toupper(type[6]);
  2287. switch(command) {
  2288. case 'A': /* ASCII mode */
  2289. data->set.ftp_ascii = 1;
  2290. break;
  2291. case 'D': /* directory mode */
  2292. data->set.ftp_list_only = 1;
  2293. break;
  2294. case 'I': /* binary mode */
  2295. default:
  2296. /* switch off ASCII */
  2297. data->set.ftp_ascii = 0;
  2298. break;
  2299. }
  2300. }
  2301. /* MN 06/07/02 */
  2302. #else /* CURL_DISABLE_FTP */
  2303. failf(data, LIBCURL_NAME
  2304. " was built with FTP disabled, ftp/ftps: not supported!");
  2305. return CURLE_UNSUPPORTED_PROTOCOL;
  2306. #endif
  2307. }
  2308. else if(strequal(conn->protostr, "TELNET")) {
  2309. #ifndef CURL_DISABLE_TELNET
  2310. /* telnet testing factory */
  2311. conn->protocol |= PROT_TELNET;
  2312. conn->port = (data->set.use_port && data->state.allow_port)?
  2313. data->set.use_port: PORT_TELNET;
  2314. conn->remote_port = PORT_TELNET;
  2315. conn->curl_do = Curl_telnet;
  2316. conn->curl_done = Curl_telnet_done;
  2317. #else
  2318. failf(data, LIBCURL_NAME
  2319. " was built with TELNET disabled!");
  2320. #endif
  2321. }
  2322. else if (strequal(conn->protostr, "DICT")) {
  2323. #ifndef CURL_DISABLE_DICT
  2324. conn->protocol |= PROT_DICT;
  2325. conn->port = (data->set.use_port && data->state.allow_port)?
  2326. data->set.use_port:PORT_DICT;
  2327. conn->remote_port = PORT_DICT;
  2328. conn->curl_do = Curl_dict;
  2329. conn->curl_done = NULL; /* no DICT-specific done */
  2330. #else
  2331. failf(data, LIBCURL_NAME
  2332. " was built with DICT disabled!");
  2333. #endif
  2334. }
  2335. else if (strequal(conn->protostr, "LDAP")) {
  2336. #ifndef CURL_DISABLE_LDAP
  2337. conn->protocol |= PROT_LDAP;
  2338. conn->port = (data->set.use_port && data->state.allow_port)?
  2339. data->set.use_port:PORT_LDAP;
  2340. conn->remote_port = PORT_LDAP;
  2341. conn->curl_do = Curl_ldap;
  2342. conn->curl_done = NULL; /* no LDAP-specific done */
  2343. #else
  2344. failf(data, LIBCURL_NAME
  2345. " was built with LDAP disabled!");
  2346. #endif
  2347. }
  2348. else if (strequal(conn->protostr, "FILE")) {
  2349. #ifndef CURL_DISABLE_FILE
  2350. conn->protocol |= PROT_FILE;
  2351. conn->curl_do = Curl_file;
  2352. /* no done() function */
  2353. /* anyway, this is supposed to be the connect function so we better
  2354. at least check that the file is present here! */
  2355. result = Curl_file_connect(conn);
  2356. /* Setup a "faked" transfer that'll do nothing */
  2357. if(CURLE_OK == result) {
  2358. conn->bits.tcpconnect = TRUE; /* we are "connected */
  2359. result = Curl_Transfer(conn, -1, -1, FALSE, NULL, /* no download */
  2360. -1, NULL); /* no upload */
  2361. }
  2362. return result;
  2363. #else
  2364. failf(data, LIBCURL_NAME
  2365. " was built with FILE disabled!");
  2366. #endif
  2367. }
  2368. else {
  2369. /* We fell through all checks and thus we don't support the specified
  2370. protocol */
  2371. failf(data, "Unsupported protocol: %s", conn->protostr);
  2372. return CURLE_UNSUPPORTED_PROTOCOL;
  2373. }
  2374. /*************************************************************
  2375. * Figure out the remote port number
  2376. *
  2377. * No matter if we use a proxy or not, we have to figure out the remote
  2378. * port number of various reasons.
  2379. *
  2380. * To be able to detect port number flawlessly, we must not confuse them
  2381. * IPv6-specified addresses in the [0::1] style. (RFC2732)
  2382. *
  2383. * The conn->name is currently [user:passwd@]host[:port] where host could
  2384. * be a hostname, IPv4 address or IPv6 address.
  2385. *************************************************************/
  2386. if((1 == sscanf(conn->name, "[%*39[0-9a-fA-F:.]%c", &endbracket)) &&
  2387. (']' == endbracket)) {
  2388. /* this is a RFC2732-style specified IP-address */
  2389. conn->bits.ipv6_ip = TRUE;
  2390. conn->name++; /* pass the starting bracket */
  2391. conn->hostname++;
  2392. tmp = strchr(conn->name, ']');
  2393. *tmp = 0; /* zero terminate */
  2394. tmp++; /* pass the ending bracket */
  2395. if(':' != *tmp)
  2396. tmp = NULL; /* no port number available */
  2397. }
  2398. else
  2399. tmp = strrchr(conn->name, ':');
  2400. if (tmp) {
  2401. char *rest;
  2402. unsigned long port;
  2403. port=strtoul(tmp+1, &rest, 10); /* Port number must be decimal */
  2404. if (rest != (tmp+1) && *rest == '\0') {
  2405. /* The colon really did have only digits after it,
  2406. * so it is either a port number or a mistake */
  2407. if (port > 0xffff) { /* Single unix standard says port numbers are
  2408. * 16 bits long */
  2409. failf(data, "Port number too large: %lu", port);
  2410. return CURLE_URL_MALFORMAT;
  2411. }
  2412. *tmp = '\0'; /* cut off the name there */
  2413. conn->remote_port = (unsigned short)port;
  2414. }
  2415. }
  2416. if(data->change.proxy && *data->change.proxy) {
  2417. /* If this is supposed to use a proxy, we need to figure out the proxy
  2418. host name name, so that we can re-use an existing connection
  2419. that may exist registered to the same proxy host. */
  2420. char *prox_portno;
  2421. char *endofprot;
  2422. /* We need to make a duplicate of the proxy so that we can modify the
  2423. string safely. */
  2424. char *proxydup=strdup(data->change.proxy);
  2425. /* We use 'proxyptr' to point to the proxy name from now on... */
  2426. char *proxyptr=proxydup;
  2427. if(NULL == proxydup) {
  2428. failf(data, "memory shortage");
  2429. return CURLE_OUT_OF_MEMORY;
  2430. }
  2431. /* Daniel Dec 10, 1998:
  2432. We do the proxy host string parsing here. We want the host name and the
  2433. port name. Accept a protocol:// prefix, even though it should just be
  2434. ignored. */
  2435. /* 1. skip the protocol part if present */
  2436. endofprot=strstr(proxyptr, "://");
  2437. if(endofprot) {
  2438. proxyptr = endofprot+3;
  2439. }
  2440. /* allow user to specify proxy.server.com:1080 if desired */
  2441. prox_portno = strchr (proxyptr, ':');
  2442. if (prox_portno) {
  2443. *prox_portno = 0x0; /* cut off number from host name */
  2444. prox_portno ++;
  2445. /* now set the local port number */
  2446. conn->port = atoi(prox_portno);
  2447. }
  2448. else if(data->set.proxyport) {
  2449. /* None given in the proxy string, then get the default one if it is
  2450. given */
  2451. conn->port = data->set.proxyport;
  2452. }
  2453. /* now, clone the cleaned proxy host name */
  2454. conn->proxyhost = strdup(proxyptr);
  2455. free(proxydup); /* free the duplicate pointer and not the modified */
  2456. }
  2457. /*************************************************************
  2458. * Take care of user and password authentication stuff
  2459. *************************************************************/
  2460. /*
  2461. * Inputs: data->set.userpwd (CURLOPT_USERPWD)
  2462. * data->set.fpasswd (CURLOPT_PASSWDFUNCTION)
  2463. * data->set.use_netrc (CURLOPT_NETRC)
  2464. * conn->hostname
  2465. * netrc file
  2466. * hard-coded defaults
  2467. *
  2468. * Outputs: (almost :- all currently undefined)
  2469. * conn->bits.user_passwd - non-zero if non-default passwords exist
  2470. * conn->user - non-zero length if defined
  2471. * conn->passwd - ditto
  2472. * conn->hostname - remove user name and password
  2473. */
  2474. /* At this point, we're hoping all the other special cases have
  2475. * been taken care of, so conn->hostname is at most
  2476. * [user[:password]]@]hostname
  2477. *
  2478. * We need somewhere to put the embedded details, so do that first.
  2479. */
  2480. user[0] =0; /* to make everything well-defined */
  2481. passwd[0]=0;
  2482. if (conn->protocol & (PROT_FTP|PROT_HTTP)) {
  2483. /* This is a FTP or HTTP URL, we will now try to extract the possible
  2484. * user+password pair in a string like:
  2485. * ftp://user:password@ftp.my.site:8021/README */
  2486. char *ptr=strchr(conn->name, '@');
  2487. char *userpass = conn->name;
  2488. if(ptr != NULL) {
  2489. /* there's a user+password given here, to the left of the @ */
  2490. conn->name = conn->hostname = ++ptr;
  2491. /* So the hostname is sane. Only bother interpreting the
  2492. * results if we could care. It could still be wasted
  2493. * work because it might be overtaken by the programmatically
  2494. * set user/passwd, but doing that first adds more cases here :-(
  2495. */
  2496. if (data->set.use_netrc != CURL_NETRC_REQUIRED) {
  2497. /* We could use the one in the URL */
  2498. conn->bits.user_passwd = 1; /* enable user+password */
  2499. if(*userpass != ':') {
  2500. /* the name is given, get user+password */
  2501. sscanf(userpass, "%127[^:@]:%127[^@]",
  2502. user, passwd);
  2503. }
  2504. else
  2505. /* no name given, get the password only */
  2506. sscanf(userpass, ":%127[^@]", passwd);
  2507. if(user[0]) {
  2508. char *newname=curl_unescape(user, 0);
  2509. if(strlen(newname) < sizeof(user)) {
  2510. strcpy(user, newname);
  2511. }
  2512. /* if the new name is longer than accepted, then just use
  2513. the unconverted name, it'll be wrong but what the heck */
  2514. free(newname);
  2515. }
  2516. if (passwd[0]) {
  2517. /* we have a password found in the URL, decode it! */
  2518. char *newpasswd=curl_unescape(passwd, 0);
  2519. if(strlen(newpasswd) < sizeof(passwd)) {
  2520. strcpy(passwd, newpasswd);
  2521. }
  2522. free(newpasswd);
  2523. }
  2524. }
  2525. }
  2526. }
  2527. /* Programmatically set password:
  2528. * - always applies, if available
  2529. * - takes precedence over the values we just set above
  2530. * so scribble it over the top.
  2531. * User-supplied passwords are assumed not to need unescaping.
  2532. *
  2533. * user_password is set in "inherite initial knowledge' above,
  2534. * so it doesn't have to be set in this block
  2535. */
  2536. if (data->set.userpwd != NULL) {
  2537. /* the name is given, get user+password */
  2538. sscanf(data->set.userpwd,
  2539. "%" MAX_CURL_USER_LENGTH_TXT "[^:]:"
  2540. "%" MAX_CURL_PASSWORD_LENGTH_TXT "[^\n]",
  2541. user, passwd);
  2542. }
  2543. if (data->set.use_netrc != CURL_NETRC_IGNORED) {
  2544. if(Curl_parsenetrc(conn->hostname,
  2545. user, passwd,
  2546. data->set.netrc_file)) {
  2547. infof(data, "Couldn't find host %s in the .netrc file, using defaults\n",
  2548. conn->hostname);
  2549. }
  2550. else
  2551. conn->bits.user_passwd = 1; /* enable user+password */
  2552. }
  2553. /* If our protocol needs a password and we have none, use the defaults */
  2554. if ( (conn->protocol & PROT_FTP) &&
  2555. !conn->bits.user_passwd) {
  2556. conn->user = strdup(CURL_DEFAULT_USER);
  2557. conn->passwd = strdup(CURL_DEFAULT_PASSWORD);
  2558. /* This is the default password, so DON'T set conn->bits.user_passwd */
  2559. }
  2560. else {
  2561. /* store user + password, zero-length if not set */
  2562. conn->user = strdup(user);
  2563. conn->passwd = strdup(passwd);
  2564. }
  2565. /*************************************************************
  2566. * Check the current list of connections to see if we can
  2567. * re-use an already existing one or if we have to create a
  2568. * new one.
  2569. *************************************************************/
  2570. /* get a cloned copy of the SSL config situation stored in the
  2571. connection struct */
  2572. if(!Curl_clone_ssl_config(&data->set.ssl, &conn->ssl_config))
  2573. return CURLE_OUT_OF_MEMORY;
  2574. /* reuse_fresh is TRUE if we are told to use a new connection by force, but
  2575. we only acknowledge this option if this is not a re-used connection
  2576. already (which happens due to follow-location or during a HTTP
  2577. authentication phase). */
  2578. if(data->set.reuse_fresh && !data->state.this_is_a_follow)
  2579. reuse = FALSE;
  2580. else
  2581. reuse = ConnectionExists(data, conn, &conn_temp);
  2582. if(reuse) {
  2583. /*
  2584. * We already have a connection for this, we got the former connection
  2585. * in the conn_temp variable and thus we need to cleanup the one we
  2586. * just allocated before we can move along and use the previously
  2587. * existing one.
  2588. */
  2589. struct connectdata *old_conn = conn;
  2590. char *path = old_conn->path; /* setup the current path pointer properly */
  2591. char *ppath = old_conn->ppath; /* this is the modified path pointer */
  2592. if(old_conn->proxyhost)
  2593. free(old_conn->proxyhost);
  2594. /* free the SSL config struct from this connection struct as this was
  2595. allocated in vain and is targeted for destruction */
  2596. Curl_free_ssl_config(&conn->ssl_config);
  2597. conn = conn_temp; /* use this connection from now on */
  2598. /* get the user+password information from the old_conn struct since it may
  2599. * be new for this request even when we re-use an existing connection */
  2600. conn->bits.user_passwd = old_conn->bits.user_passwd;
  2601. conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
  2602. /* If we speak over a proxy, we need to copy the host name too, as it
  2603. might be another remote host even when re-using a connection */
  2604. strcpy(conn->gname, old_conn->gname); /* safe strcpy() */
  2605. /* we need these pointers if we speak over a proxy */
  2606. conn->hostname = conn->gname;
  2607. conn->name = &conn->gname[old_conn->name - old_conn->gname];
  2608. free(conn->path); /* free the previously allocated path pointer */
  2609. /* 'path' points to the allocated data, 'ppath' may have been advanced
  2610. to point somewhere within the 'path' area. */
  2611. conn->path = path;
  2612. conn->ppath = ppath;
  2613. /* re-use init */
  2614. conn->bits.reuse = TRUE; /* yes, we're re-using here */
  2615. conn->bits.chunk = FALSE; /* always assume not chunked unless told
  2616. otherwise */
  2617. conn->maxdownload = -1; /* might have been used previously! */
  2618. Curl_safefree(old_conn->user);
  2619. Curl_safefree(old_conn->passwd);
  2620. Curl_safefree(old_conn->proxyuser);
  2621. Curl_safefree(old_conn->proxypasswd);
  2622. if(old_conn->bits.rangestringalloc)
  2623. free(old_conn->range);
  2624. free(old_conn); /* we don't need this anymore */
  2625. /*
  2626. * If we're doing a resumed transfer, we need to setup our stuff
  2627. * properly.
  2628. */
  2629. conn->resume_from = data->set.set_resume_from;
  2630. if (conn->resume_from) {
  2631. snprintf(resumerange, sizeof(resumerange), "%" FORMAT_OFF_T "-",
  2632. conn->resume_from);
  2633. if (conn->bits.rangestringalloc == TRUE)
  2634. free(conn->range);
  2635. /* tell ourselves to fetch this range */
  2636. conn->range = strdup(resumerange);
  2637. conn->bits.use_range = TRUE; /* enable range download */
  2638. conn->bits.rangestringalloc = TRUE; /* mark range string allocated */
  2639. }
  2640. else if (data->set.set_range) {
  2641. /* There is a range, but is not a resume, useful for random ftp access */
  2642. conn->range = strdup(data->set.set_range);
  2643. conn->bits.rangestringalloc = TRUE; /* mark range string allocated */
  2644. conn->bits.use_range = TRUE; /* enable range download */
  2645. }
  2646. else
  2647. conn->bits.use_range = FALSE; /* disable range download */
  2648. *in_connect = conn; /* return this instead! */
  2649. infof(data, "Re-using existing connection! (#%d)\n", conn->connectindex);
  2650. }
  2651. else {
  2652. /*
  2653. * This is a brand new connection, so let's store it in the connection
  2654. * cache of ours!
  2655. */
  2656. ConnectionStore(data, conn);
  2657. }
  2658. /* Continue connectdata initialization here.
  2659. *
  2660. * Inherit the proper values from the urldata struct AFTER we have arranged
  2661. * the persistant conncetion stuff */
  2662. conn->fread = data->set.fread;
  2663. conn->fread_in = data->set.in;
  2664. conn->bits.upload_chunky =
  2665. ((conn->protocol&PROT_HTTP) &&
  2666. data->set.upload &&
  2667. (data->set.infilesize == -1) &&
  2668. (data->set.httpversion != CURL_HTTP_VERSION_1_0))?
  2669. /* HTTP, upload, unknown file size and not HTTP 1.0 */
  2670. TRUE:
  2671. /* else, no chunky upload */
  2672. FALSE;
  2673. #ifndef USE_ARES
  2674. /*************************************************************
  2675. * Set timeout if that is being used, and we're not using an asynchronous
  2676. * name resolve.
  2677. *************************************************************/
  2678. if((data->set.timeout || data->set.connecttimeout) && !data->set.no_signal) {
  2679. /*************************************************************
  2680. * Set signal handler to catch SIGALRM
  2681. * Store the old value to be able to set it back later!
  2682. *************************************************************/
  2683. #ifdef SIGALRM
  2684. #ifdef HAVE_SIGACTION
  2685. struct sigaction sigact;
  2686. sigaction(SIGALRM, NULL, &sigact);
  2687. keep_sigact = sigact;
  2688. keep_copysig = TRUE; /* yes, we have a copy */
  2689. sigact.sa_handler = alarmfunc;
  2690. #ifdef SA_RESTART
  2691. /* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
  2692. sigact.sa_flags &= ~SA_RESTART;
  2693. #endif
  2694. /* now set the new struct */
  2695. sigaction(SIGALRM, &sigact, NULL);
  2696. #else /* HAVE_SIGACTION */
  2697. /* no sigaction(), revert to the much lamer signal() */
  2698. #ifdef HAVE_SIGNAL
  2699. keep_sigact = signal(SIGALRM, alarmfunc);
  2700. #endif
  2701. #endif /* HAVE_SIGACTION */
  2702. /* We set the timeout on the name resolving phase first, separately from
  2703. * the download/upload part to allow a maximum time on everything. This is
  2704. * a signal-based timeout, why it won't work and shouldn't be used in
  2705. * multi-threaded environments. */
  2706. #ifdef HAVE_ALARM
  2707. /* alarm() makes a signal get sent when the timeout fires off, and that
  2708. will abort system calls */
  2709. prev_alarm = alarm(data->set.connecttimeout?
  2710. data->set.connecttimeout:
  2711. data->set.timeout);
  2712. /* We can expect the conn->created time to be "now", as that was just
  2713. recently set in the beginning of this function and nothing slow
  2714. has been done since then until now. */
  2715. #endif
  2716. #endif /* SIGALRM */
  2717. }
  2718. #endif /* USE_ARES */
  2719. /*************************************************************
  2720. * Resolve the name of the server or proxy
  2721. *************************************************************/
  2722. if(conn->bits.reuse) {
  2723. /* re-used connection, no resolving is necessary */
  2724. hostaddr = NULL;
  2725. conn->connect_addr = NULL; /* we don't connect now so we don't have any
  2726. fresh connect_addr struct to point to */
  2727. }
  2728. else if(!data->change.proxy || !*data->change.proxy) {
  2729. /* If not connecting via a proxy, extract the port from the URL, if it is
  2730. * there, thus overriding any defaults that might have been set above. */
  2731. conn->port = conn->remote_port; /* it is the same port */
  2732. /* Resolve target host right on */
  2733. rc = Curl_resolv(conn, conn->name, conn->port, &hostaddr);
  2734. if(rc == 1)
  2735. *async = TRUE;
  2736. else if(!hostaddr) {
  2737. failf(data, "Couldn't resolve host '%s'", conn->name);
  2738. result = CURLE_COULDNT_RESOLVE_HOST;
  2739. /* don't return yet, we need to clean up the timeout first */
  2740. }
  2741. }
  2742. else {
  2743. /* This is a proxy that hasn't been resolved yet. */
  2744. /* resolve proxy */
  2745. rc = Curl_resolv(conn, conn->proxyhost, conn->port, &hostaddr);
  2746. if(rc == 1)
  2747. *async = TRUE;
  2748. else if(!hostaddr) {
  2749. failf(data, "Couldn't resolve proxy '%s'", conn->proxyhost);
  2750. result = CURLE_COULDNT_RESOLVE_PROXY;
  2751. /* don't return yet, we need to clean up the timeout first */
  2752. }
  2753. }
  2754. *addr = hostaddr;
  2755. #if defined(HAVE_ALARM) && defined(SIGALRM)
  2756. if((data->set.timeout || data->set.connecttimeout) && !data->set.no_signal) {
  2757. #ifdef HAVE_SIGACTION
  2758. if(keep_copysig) {
  2759. /* we got a struct as it looked before, now put that one back nice
  2760. and clean */
  2761. sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
  2762. }
  2763. #else
  2764. #ifdef HAVE_SIGNAL
  2765. /* restore the previous SIGALRM handler */
  2766. signal(SIGALRM, keep_sigact);
  2767. #endif
  2768. #endif /* HAVE_SIGACTION */
  2769. /* switch back the alarm() to either zero or to what it was before minus
  2770. the time we spent until now! */
  2771. if(prev_alarm) {
  2772. /* there was an alarm() set before us, now put it back */
  2773. long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
  2774. long alarm_set;
  2775. /* the alarm period is counted in even number of seconds */
  2776. alarm_set = prev_alarm - elapsed_ms/1000;
  2777. if(alarm_set<=0) {
  2778. /* if it turned negative, we should fire off a SIGALRM here, but we
  2779. won't, and zero would be to switch it off so we never set it to
  2780. less than 1! */
  2781. alarm(1);
  2782. result = CURLE_OPERATION_TIMEOUTED;
  2783. failf(data, "Previous alarm fired off!");
  2784. }
  2785. else
  2786. alarm(alarm_set);
  2787. }
  2788. else
  2789. alarm(0); /* just shut it off */
  2790. }
  2791. #endif
  2792. return result;
  2793. }
  2794. /* SetupConnection() should be called after the name resolve initiated in
  2795. * CreateConnection() is all done.
  2796. */
  2797. static CURLcode SetupConnection(struct connectdata *conn,
  2798. struct Curl_dns_entry *hostaddr)
  2799. {
  2800. struct SessionHandle *data = conn->data;
  2801. CURLcode result=CURLE_OK;
  2802. Curl_pgrsTime(data, TIMER_NAMELOOKUP);
  2803. if(conn->protocol & PROT_FILE)
  2804. /* There's nothing in this function to setup if we're only doing
  2805. a file:// transfer */
  2806. return result;
  2807. /*************************************************************
  2808. * Send user-agent to HTTP proxies even if the target protocol
  2809. * isn't HTTP.
  2810. *************************************************************/
  2811. if((conn->protocol&PROT_HTTP) ||
  2812. (data->change.proxy && *data->change.proxy)) {
  2813. if(data->set.useragent) {
  2814. Curl_safefree(conn->allocptr.uagent);
  2815. conn->allocptr.uagent =
  2816. aprintf("User-Agent: %s\015\012", data->set.useragent);
  2817. }
  2818. }
  2819. if(data->set.encoding) {
  2820. Curl_safefree(conn->allocptr.accept_encoding);
  2821. conn->allocptr.accept_encoding =
  2822. aprintf("Accept-Encoding: %s\015\012", data->set.encoding);
  2823. }
  2824. conn->bytecount = 0;
  2825. conn->headerbytecount = 0;
  2826. if(CURL_SOCKET_BAD == conn->sock[FIRSTSOCKET]) {
  2827. bool connected;
  2828. /* Connect only if not already connected! */
  2829. result = ConnectPlease(conn, hostaddr, &connected);
  2830. if(connected) {
  2831. result = Curl_protocol_connect(conn, hostaddr);
  2832. if(CURLE_OK == result)
  2833. conn->bits.tcpconnect = TRUE;
  2834. }
  2835. else
  2836. conn->bits.tcpconnect = FALSE;
  2837. if(CURLE_OK != result)
  2838. return result;
  2839. }
  2840. else {
  2841. Curl_pgrsTime(data, TIMER_CONNECT); /* we're connected already */
  2842. conn->bits.tcpconnect = TRUE;
  2843. if(data->set.verbose)
  2844. verboseconnect(conn, hostaddr);
  2845. }
  2846. conn->now = Curl_tvnow(); /* time this *after* the connect is done, we
  2847. set this here perhaps a second time */
  2848. #ifdef __EMX__
  2849. /* 20000330 mgs
  2850. * the check is quite a hack...
  2851. * we're calling _fsetmode to fix the problem with fwrite converting newline
  2852. * characters (you get mangled text files, and corrupted binary files when
  2853. * you download to stdout and redirect it to a file). */
  2854. if ((data->set.out)->_handle == NULL) {
  2855. _fsetmode(stdout, "b");
  2856. }
  2857. #endif
  2858. return CURLE_OK;
  2859. }
  2860. CURLcode Curl_connect(struct SessionHandle *data,
  2861. struct connectdata **in_connect,
  2862. bool *asyncp)
  2863. {
  2864. CURLcode code;
  2865. struct Curl_dns_entry *dns;
  2866. *asyncp = FALSE; /* assume synchronous resolves by default */
  2867. /* call the stuff that needs to be called */
  2868. code = CreateConnection(data, in_connect, &dns, asyncp);
  2869. if(CURLE_OK == code) {
  2870. /* no error */
  2871. if(dns || !*asyncp)
  2872. /* If an address is available it means that we already have the name
  2873. resolved, OR it isn't async.
  2874. If so => continue connecting from here */
  2875. code = SetupConnection(*in_connect, dns);
  2876. /* else
  2877. response will be received and treated async wise */
  2878. }
  2879. if(CURLE_OK != code) {
  2880. /* We're not allowed to return failure with memory left allocated
  2881. in the connectdata struct, free those here */
  2882. if(*in_connect) {
  2883. Curl_disconnect(*in_connect); /* close the connection */
  2884. *in_connect = NULL; /* return a NULL */
  2885. }
  2886. }
  2887. return code;
  2888. }
  2889. /* Call this function after Curl_connect() has returned async=TRUE and
  2890. then a successful name resolve has been received */
  2891. CURLcode Curl_async_resolved(struct connectdata *conn)
  2892. {
  2893. #if defined(USE_ARES) || defined(USE_THREADING_GETHOSTBYNAME)
  2894. CURLcode code = SetupConnection(conn, conn->async.dns);
  2895. if(code)
  2896. /* We're not allowed to return failure with memory left allocated
  2897. in the connectdata struct, free those here */
  2898. Curl_disconnect(conn); /* close the connection */
  2899. return code;
  2900. #else
  2901. (void)conn;
  2902. return CURLE_OK;
  2903. #endif
  2904. }
  2905. CURLcode Curl_done(struct connectdata *conn)
  2906. {
  2907. struct SessionHandle *data=conn->data;
  2908. CURLcode result;
  2909. /* cleanups done even if the connection is re-used */
  2910. if(conn->bits.rangestringalloc) {
  2911. free(conn->range);
  2912. conn->bits.rangestringalloc = FALSE;
  2913. }
  2914. /* Cleanup possible redirect junk */
  2915. if(conn->newurl) {
  2916. free(conn->newurl);
  2917. conn->newurl = NULL;
  2918. }
  2919. if(conn->connect_addr)
  2920. Curl_resolv_unlock(conn->data, conn->connect_addr); /* done with this */
  2921. #if defined(CURLDEBUG) && defined(AGGRESIVE_TEST)
  2922. /* scan for DNS cache entries still marked as in use */
  2923. Curl_hash_apply(data->hostcache,
  2924. NULL, Curl_scan_cache_used);
  2925. #endif
  2926. Curl_hostcache_prune(data); /* kill old DNS cache entries */
  2927. /* this calls the protocol-specific function pointer previously set */
  2928. if(conn->curl_done)
  2929. result = conn->curl_done(conn);
  2930. else
  2931. result = CURLE_OK;
  2932. Curl_pgrsDone(conn); /* done with the operation */
  2933. /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
  2934. forced us to close this no matter what we think.
  2935. if conn->bits.close is TRUE, it means that the connection should be
  2936. closed in spite of all our efforts to be nice, due to protocol
  2937. restrictions in our or the server's end */
  2938. if(data->set.reuse_forbid || conn->bits.close) {
  2939. CURLcode res2;
  2940. res2 = Curl_disconnect(conn); /* close the connection */
  2941. /* If we had an error already, make sure we return that one. But
  2942. if we got a new error, return that. */
  2943. if(!result && res2)
  2944. result = res2;
  2945. }
  2946. else
  2947. infof(data, "Connection #%d left intact\n", conn->connectindex);
  2948. return result;
  2949. }
  2950. CURLcode Curl_do(struct connectdata **connp)
  2951. {
  2952. CURLcode result=CURLE_OK;
  2953. struct connectdata *conn = *connp;
  2954. struct SessionHandle *data=conn->data;
  2955. conn->bits.do_more = FALSE; /* by default there's no curl_do_more() to use */
  2956. if(conn->curl_do) {
  2957. /* generic protocol-specific function pointer set in curl_connect() */
  2958. result = conn->curl_do(conn);
  2959. /* This was formerly done in transfer.c, but we better do it here */
  2960. if((CURLE_SEND_ERROR == result) && conn->bits.reuse) {
  2961. /* This was a re-use of a connection and we got a write error in the
  2962. * DO-phase. Then we DISCONNECT this connection and have another attempt
  2963. * to CONNECT and then DO again! The retry cannot possibly find another
  2964. * connection to re-use, since we only keep one possible connection for
  2965. * each. */
  2966. infof(data, "Re-used connection seems dead, get a new one\n");
  2967. conn->bits.close = TRUE; /* enforce close of this connection */
  2968. result = Curl_done(conn); /* we are so done with this */
  2969. /* conn is no longer a good pointer */
  2970. if(CURLE_OK == result) {
  2971. bool async;
  2972. /* Now, redo the connect and get a new connection */
  2973. result = Curl_connect(data, connp, &async);
  2974. if(CURLE_OK == result) {
  2975. /* We have connected or sent away a name resolve query fine */
  2976. conn = *connp; /* setup conn to again point to something nice */
  2977. if(async) {
  2978. /* Now, if async is TRUE here, we need to wait for the name
  2979. to resolve */
  2980. result = Curl_wait_for_resolv(conn, NULL);
  2981. if(result)
  2982. return result;
  2983. /* Resolved, continue with the connection */
  2984. result = Curl_async_resolved(conn);
  2985. if(result)
  2986. return result;
  2987. }
  2988. /* ... finally back to actually retry the DO phase */
  2989. result = conn->curl_do(conn);
  2990. }
  2991. }
  2992. }
  2993. }
  2994. return result;
  2995. }
  2996. CURLcode Curl_do_more(struct connectdata *conn)
  2997. {
  2998. CURLcode result=CURLE_OK;
  2999. if(conn->curl_do_more)
  3000. result = conn->curl_do_more(conn);
  3001. return result;
  3002. }
  3003. static bool safe_strequal(char* str1, char* str2)
  3004. {
  3005. if(str1 && str2)
  3006. /* both pointers point to something then compare them */
  3007. return strequal(str1, str2);
  3008. else
  3009. /* if both pointers are NULL then treat them as equal */
  3010. return (!str1 && !str2);
  3011. }
  3012. bool
  3013. Curl_ssl_config_matches(struct ssl_config_data* data,
  3014. struct ssl_config_data* needle)
  3015. {
  3016. if((data->version == needle->version) &&
  3017. (data->verifypeer == needle->verifypeer) &&
  3018. (data->verifyhost == needle->verifyhost) &&
  3019. safe_strequal(data->CApath, needle->CApath) &&
  3020. safe_strequal(data->CAfile, needle->CAfile) &&
  3021. safe_strequal(data->random_file, needle->random_file) &&
  3022. safe_strequal(data->egdsocket, needle->egdsocket) &&
  3023. safe_strequal(data->cipher_list, needle->cipher_list))
  3024. return TRUE;
  3025. return FALSE;
  3026. }
  3027. bool
  3028. Curl_clone_ssl_config(struct ssl_config_data *source,
  3029. struct ssl_config_data *dest)
  3030. {
  3031. dest->verifyhost = source->verifyhost;
  3032. dest->verifypeer = source->verifypeer;
  3033. dest->version = source->version;
  3034. if(source->CAfile) {
  3035. dest->CAfile = strdup(source->CAfile);
  3036. if(!dest->CAfile)
  3037. return FALSE;
  3038. }
  3039. if(source->CApath) {
  3040. dest->CApath = strdup(source->CApath);
  3041. if(!dest->CApath)
  3042. return FALSE;
  3043. }
  3044. if(source->cipher_list) {
  3045. dest->cipher_list = strdup(source->cipher_list);
  3046. if(!dest->cipher_list)
  3047. return FALSE;
  3048. }
  3049. if(source->egdsocket) {
  3050. dest->egdsocket = strdup(source->egdsocket);
  3051. if(!dest->egdsocket)
  3052. return FALSE;
  3053. }
  3054. if(source->random_file) {
  3055. dest->random_file = strdup(source->random_file);
  3056. if(!dest->random_file)
  3057. return FALSE;
  3058. }
  3059. return TRUE;
  3060. }
  3061. void Curl_free_ssl_config(struct ssl_config_data* sslc)
  3062. {
  3063. if(sslc->CAfile)
  3064. free(sslc->CAfile);
  3065. if(sslc->CApath)
  3066. free(sslc->CApath);
  3067. if(sslc->cipher_list)
  3068. free(sslc->cipher_list);
  3069. if(sslc->egdsocket)
  3070. free(sslc->egdsocket);
  3071. if(sslc->random_file)
  3072. free(sslc->random_file);
  3073. }