url.c 111 KB

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