url.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2001, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * In order to be useful for every potential user, curl and libcurl are
  11. * dual-licensed under the MPL and the MIT/X-derivate licenses.
  12. *
  13. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  14. * copies of the Software, and permit persons to whom the Software is
  15. * furnished to do so, under the terms of the MPL or the MIT/X-derivate
  16. * licenses. You may pick one of these licenses.
  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 <winsock.h>
  35. #include <time.h>
  36. #include <io.h>
  37. #else
  38. #ifdef HAVE_SYS_SOCKET_H
  39. #include <sys/socket.h>
  40. #endif
  41. #include <netinet/in.h>
  42. #include <sys/time.h>
  43. #include <sys/resource.h>
  44. #ifdef HAVE_UNISTD_H
  45. #include <unistd.h>
  46. #endif
  47. #include <netdb.h>
  48. #ifdef HAVE_ARPA_INET_H
  49. #include <arpa/inet.h>
  50. #endif
  51. #ifdef HAVE_NET_IF_H
  52. #include <net/if.h>
  53. #endif
  54. #include <sys/ioctl.h>
  55. #include <signal.h>
  56. #ifdef HAVE_SYS_PARAM_H
  57. #include <sys/param.h>
  58. #endif
  59. #ifdef HAVE_SYS_SELECT_H
  60. #include <sys/select.h>
  61. #endif
  62. #ifdef VMS
  63. #include <in.h>
  64. #include <inet.h>
  65. #endif
  66. #ifndef HAVE_SELECT
  67. #error "We can't compile without select() support!"
  68. #endif
  69. #ifndef HAVE_SOCKET
  70. #error "We can't compile without socket() support!"
  71. #endif
  72. #endif
  73. #include "urldata.h"
  74. #include "netrc.h"
  75. #include "formdata.h"
  76. #include "base64.h"
  77. #include "ssluse.h"
  78. #include "hostip.h"
  79. #include "if2ip.h"
  80. #include "transfer.h"
  81. #include "sendf.h"
  82. #include "getpass.h"
  83. #include "progress.h"
  84. #include "cookie.h"
  85. #include "strequal.h"
  86. #include "escape.h"
  87. #include "strtok.h"
  88. /* And now for the protocols */
  89. #include "ftp.h"
  90. #include "dict.h"
  91. #include "telnet.h"
  92. #include "http.h"
  93. #include "file.h"
  94. #include "ldap.h"
  95. #include "url.h"
  96. #include "connect.h"
  97. #include <curl/types.h>
  98. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  99. #include "inet_ntoa_r.h"
  100. #endif
  101. #define _MPRINTF_REPLACE /* use our functions only */
  102. #include <curl/mprintf.h>
  103. #ifdef KRB4
  104. #include "security.h"
  105. #endif
  106. /* The last #include file should be: */
  107. #ifdef MALLOCDEBUG
  108. #include "memdebug.h"
  109. #endif
  110. /* Local static prototypes */
  111. static int ConnectionKillOne(struct SessionHandle *data);
  112. static bool ConnectionExists(struct SessionHandle *data,
  113. struct connectdata *needle,
  114. struct connectdata **usethis);
  115. static unsigned int ConnectionStore(struct SessionHandle *data,
  116. struct connectdata *conn);
  117. #if !defined(WIN32)||defined(__CYGWIN32__)
  118. #ifndef RETSIGTYPE
  119. #define RETSIGTYPE void
  120. #endif
  121. static
  122. RETSIGTYPE alarmfunc(int signal)
  123. {
  124. /* this is for "-ansi -Wall -pedantic" to stop complaining! (rabe) */
  125. (void)signal;
  126. return;
  127. }
  128. #endif
  129. /*
  130. * This is the internal function curl_easy_cleanup() calls. This should
  131. * cleanup and free all resources associated with this sessionhandle.
  132. *
  133. * NOTE: if we ever add something that attempts to write to a socket or
  134. * similar here, we must ignore SIGPIPE first. It is currently only done
  135. * when curl_easy_perform() is invoked.
  136. */
  137. CURLcode Curl_close(struct SessionHandle *data)
  138. {
  139. /* Loop through all open connections and kill them one by one */
  140. while(-1 != ConnectionKillOne(data));
  141. #ifdef USE_SSLEAY
  142. /* Close down all open SSL info and sessions */
  143. Curl_SSL_Close_All(data);
  144. #endif
  145. if(data->state.auth_host)
  146. free(data->state.auth_host);
  147. if(data->change.proxy_alloc)
  148. free(data->change.proxy);
  149. if(data->change.referer_alloc)
  150. free(data->change.referer);
  151. if(data->change.url_alloc)
  152. free(data->change.url);
  153. if(data->state.headerbuff)
  154. free(data->state.headerbuff);
  155. if(data->set.cookiejar)
  156. /* we have a "destination" for all the cookies to get dumped to */
  157. Curl_cookie_output(data->cookies, data->set.cookiejar);
  158. Curl_cookie_cleanup(data->cookies);
  159. /* free the connection cache */
  160. free(data->state.connects);
  161. if(data->info.contenttype)
  162. free(data->info.contenttype);
  163. free(data);
  164. return CURLE_OK;
  165. }
  166. static
  167. int my_getpass(void *clientp, const char *prompt, char* buffer, int buflen )
  168. {
  169. char *retbuf;
  170. clientp=NULL; /* prevent compiler warning */
  171. retbuf = getpass_r(prompt, buffer, buflen);
  172. if(NULL == retbuf)
  173. return 1;
  174. else
  175. return 0; /* success */
  176. }
  177. CURLcode Curl_open(struct SessionHandle **curl)
  178. {
  179. /* We don't yet support specifying the URL at this point */
  180. struct SessionHandle *data;
  181. /* Very simple start-up: alloc the struct, init it with zeroes and return */
  182. data = (struct SessionHandle *)malloc(sizeof(struct SessionHandle));
  183. if(!data)
  184. /* this is a very serious error */
  185. return CURLE_OUT_OF_MEMORY;
  186. memset(data, 0, sizeof(struct SessionHandle));
  187. /* We do some initial setup here, all those fields that can't be just 0 */
  188. data->state.headerbuff=(char*)malloc(HEADERSIZE);
  189. if(!data->state.headerbuff) {
  190. free(data); /* free the memory again */
  191. return CURLE_OUT_OF_MEMORY;
  192. }
  193. data->state.headersize=HEADERSIZE;
  194. data->set.out = stdout; /* default output to stdout */
  195. data->set.in = stdin; /* default input from stdin */
  196. data->set.err = stderr; /* default stderr to stderr */
  197. /* use fwrite as default function to store output */
  198. data->set.fwrite = (curl_write_callback)fwrite;
  199. /* use fread as default function to read input */
  200. data->set.fread = (curl_read_callback)fread;
  201. /* set the default passwd function */
  202. data->set.fpasswd = my_getpass;
  203. data->set.infilesize = -1; /* we don't know any size */
  204. data->state.current_speed = -1; /* init to negative == impossible */
  205. data->set.httpreq = HTTPREQ_GET; /* Default HTTP request */
  206. data->set.ftp_use_epsv = TRUE; /* FTP defaults to EPSV operations */
  207. data->set.dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
  208. /* make libcurl quiet by default: */
  209. data->set.hide_progress = TRUE; /* CURLOPT_NOPROGRESS changes these */
  210. data->progress.flags |= PGRS_HIDE;
  211. /* Set the default size of the SSL session ID cache */
  212. data->set.ssl.numsessions = 5;
  213. /* create an array with connection data struct pointers */
  214. data->state.numconnects = 5; /* hard-coded right now */
  215. data->state.connects = (struct connectdata **)
  216. malloc(sizeof(struct connectdata *) * data->state.numconnects);
  217. if(!data->state.connects) {
  218. free(data);
  219. return CURLE_OUT_OF_MEMORY;
  220. }
  221. memset(data->state.connects, 0,
  222. sizeof(struct connectdata *)*data->state.numconnects);
  223. *curl = data;
  224. return CURLE_OK;
  225. }
  226. CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option, ...)
  227. {
  228. va_list param;
  229. char *cookiefile;
  230. va_start(param, option);
  231. switch(option) {
  232. case CURLOPT_DNS_CACHE_TIMEOUT:
  233. data->set.dns_cache_timeout = va_arg(param, int);
  234. break;
  235. case CURLOPT_DNS_USE_GLOBAL_CACHE:
  236. {
  237. int use_cache = va_arg(param, int);
  238. if (use_cache) {
  239. Curl_global_host_cache_init();
  240. }
  241. data->set.global_dns_cache = use_cache;
  242. }
  243. break;
  244. case CURLOPT_SSL_CIPHER_LIST:
  245. /* set a list of cipher we want to use in the SSL connection */
  246. data->set.ssl.cipher_list = va_arg(param, char *);
  247. break;
  248. case CURLOPT_RANDOM_FILE:
  249. /*
  250. * This is the path name to a file that contains random data to seed
  251. * the random SSL stuff with. The file is only used for reading.
  252. */
  253. data->set.ssl.random_file = va_arg(param, char *);
  254. break;
  255. case CURLOPT_EGDSOCKET:
  256. /*
  257. * The Entropy Gathering Daemon socket pathname
  258. */
  259. data->set.ssl.egdsocket = va_arg(param, char *);
  260. break;
  261. case CURLOPT_MAXCONNECTS:
  262. /*
  263. * Set the absolute number of maximum simultaneous alive connection that
  264. * libcurl is allowed to have.
  265. */
  266. {
  267. long newconnects= va_arg(param, long);
  268. struct connectdata **newptr;
  269. if(newconnects < data->state.numconnects) {
  270. /* Since this number is *decreased* from the existing number, we must
  271. close the possibly open connections that live on the indexes that
  272. are being removed! */
  273. int i;
  274. for(i=newconnects; i< data->state.numconnects; i++)
  275. Curl_disconnect(data->state.connects[i]);
  276. }
  277. if(newconnects) {
  278. newptr= (struct connectdata **)
  279. realloc(data->state.connects,
  280. sizeof(struct connectdata *) * newconnects);
  281. if(!newptr)
  282. /* we closed a few connections in vain, but so what? */
  283. return CURLE_OUT_OF_MEMORY;
  284. data->state.connects = newptr;
  285. data->state.numconnects = newconnects;
  286. }
  287. else {
  288. /* zero makes NO cache at all */
  289. if(data->state.connects)
  290. free(data->state.connects);
  291. data->state.connects=NULL;
  292. data->state.numconnects=0;
  293. }
  294. }
  295. break;
  296. case CURLOPT_FORBID_REUSE:
  297. /*
  298. * When this transfer is done, it must not be left to be reused by a
  299. * subsequent transfer but shall be closed immediately.
  300. */
  301. data->set.reuse_forbid = va_arg(param, long)?TRUE:FALSE;
  302. break;
  303. case CURLOPT_FRESH_CONNECT:
  304. /*
  305. * This transfer shall not use a previously cached connection but
  306. * should be made with a fresh new connect!
  307. */
  308. data->set.reuse_fresh = va_arg(param, long)?TRUE:FALSE;
  309. break;
  310. case CURLOPT_VERBOSE:
  311. /*
  312. * Verbose means infof() calls that give a lot of information about
  313. * the connection and transfer procedures as well as internal choices.
  314. */
  315. data->set.verbose = va_arg(param, long)?TRUE:FALSE;
  316. break;
  317. case CURLOPT_HEADER:
  318. /*
  319. * Set to include the header in the general data output stream.
  320. */
  321. data->set.http_include_header = va_arg(param, long)?TRUE:FALSE;
  322. break;
  323. case CURLOPT_NOPROGRESS:
  324. /*
  325. * Shut off the internal supported progress meter
  326. */
  327. data->set.hide_progress = va_arg(param, long)?TRUE:FALSE;
  328. if(data->set.hide_progress)
  329. data->progress.flags |= PGRS_HIDE;
  330. else
  331. data->progress.flags &= ~PGRS_HIDE;
  332. break;
  333. case CURLOPT_NOBODY:
  334. /*
  335. * Do not include the body part in the output data stream.
  336. */
  337. data->set.no_body = va_arg(param, long)?TRUE:FALSE;
  338. break;
  339. case CURLOPT_FAILONERROR:
  340. /*
  341. * Don't output the >=300 error code HTML-page, but instead only
  342. * return error.
  343. */
  344. data->set.http_fail_on_error = va_arg(param, long)?TRUE:FALSE;
  345. break;
  346. case CURLOPT_UPLOAD:
  347. /*
  348. * We want to sent data to the remote host
  349. */
  350. data->set.upload = va_arg(param, long)?TRUE:FALSE;
  351. if(data->set.upload)
  352. /* If this is HTTP, PUT is what's needed to "upload" */
  353. data->set.httpreq = HTTPREQ_PUT;
  354. break;
  355. case CURLOPT_FILETIME:
  356. /*
  357. * Try to get the file time of the remote document. The time will
  358. * later (possibly) become available using curl_easy_getinfo().
  359. */
  360. data->set.get_filetime = va_arg(param, long)?TRUE:FALSE;
  361. break;
  362. case CURLOPT_FTPLISTONLY:
  363. /*
  364. * An FTP option that changes the command to one that asks for a list
  365. * only, no file info details.
  366. */
  367. data->set.ftp_list_only = va_arg(param, long)?TRUE:FALSE;
  368. break;
  369. case CURLOPT_FTPAPPEND:
  370. /*
  371. * We want to upload and append to an existing (FTP) file.
  372. */
  373. data->set.ftp_append = va_arg(param, long)?TRUE:FALSE;
  374. break;
  375. case CURLOPT_NETRC:
  376. /*
  377. * Parse the $HOME/.netrc file
  378. */
  379. data->set.use_netrc = va_arg(param, long)?TRUE:FALSE;
  380. break;
  381. case CURLOPT_FOLLOWLOCATION:
  382. /*
  383. * Follow Location: header hints on a HTTP-server.
  384. */
  385. data->set.http_follow_location = va_arg(param, long)?TRUE:FALSE;
  386. break;
  387. case CURLOPT_HTTP_VERSION:
  388. /*
  389. * This sets a requested HTTP version to be used. The value is one of
  390. * the listed enums in curl/curl.h.
  391. */
  392. data->set.httpversion = va_arg(param, long);
  393. break;
  394. case CURLOPT_TRANSFERTEXT:
  395. /*
  396. * This option was previously named 'FTPASCII'. Renamed to work with
  397. * more protocols than merely FTP.
  398. *
  399. * Transfer using ASCII (instead of BINARY).
  400. */
  401. data->set.ftp_ascii = va_arg(param, long)?TRUE:FALSE;
  402. break;
  403. case CURLOPT_PUT:
  404. /*
  405. * Use the HTTP PUT request to transfer data if this is TRUE. If this is
  406. * FALSE, don't set the httpreq. We can't know what to revert it to!
  407. */
  408. if(va_arg(param, long))
  409. data->set.httpreq = HTTPREQ_PUT;
  410. break;
  411. case CURLOPT_TIMECONDITION:
  412. /*
  413. * Set HTTP time condition. This must be one of the defines in the
  414. * curl/curl.h header file.
  415. */
  416. data->set.timecondition = va_arg(param, long);
  417. break;
  418. case CURLOPT_TIMEVALUE:
  419. /*
  420. * This is the value to compare with the remote document with the
  421. * method set with CURLOPT_TIMECONDITION
  422. */
  423. data->set.timevalue = va_arg(param, long);
  424. break;
  425. case CURLOPT_SSLVERSION:
  426. /*
  427. * Set explicit SSL version to try to connect with, as some SSL
  428. * implementations are lame.
  429. */
  430. data->set.ssl.version = va_arg(param, long);
  431. break;
  432. case CURLOPT_COOKIEFILE:
  433. /*
  434. * Set cookie file to read and parse. Can be used multiple times.
  435. */
  436. cookiefile = (char *)va_arg(param, void *);
  437. if(cookiefile)
  438. data->cookies = Curl_cookie_init(cookiefile, data->cookies);
  439. break;
  440. case CURLOPT_COOKIEJAR:
  441. /*
  442. * Set cookie file name to dump all cookies to when we're done.
  443. */
  444. data->set.cookiejar = (char *)va_arg(param, void *);
  445. /*
  446. * Activate the cookie parser. This may or may not already
  447. * have been made.
  448. */
  449. data->cookies = Curl_cookie_init(NULL, data->cookies);
  450. break;
  451. case CURLOPT_WRITEHEADER:
  452. /*
  453. * Custom pointer to pass the header write callback function
  454. */
  455. data->set.writeheader = (void *)va_arg(param, void *);
  456. break;
  457. case CURLOPT_COOKIE:
  458. /*
  459. * Cookie string to send to the remote server in the request.
  460. */
  461. data->set.cookie = va_arg(param, char *);
  462. break;
  463. case CURLOPT_ERRORBUFFER:
  464. /*
  465. * Error buffer provided by the caller to get the human readable
  466. * error string in.
  467. */
  468. data->set.errorbuffer = va_arg(param, char *);
  469. break;
  470. case CURLOPT_FILE:
  471. /*
  472. * FILE pointer to write to or include in the data write callback
  473. */
  474. data->set.out = va_arg(param, FILE *);
  475. break;
  476. case CURLOPT_FTPPORT:
  477. /*
  478. * Use FTP PORT, this also specifies which IP address to use
  479. */
  480. data->set.ftpport = va_arg(param, char *);
  481. data->set.ftp_use_port = data->set.ftpport?1:0;
  482. break;
  483. case CURLOPT_FTP_USE_EPSV:
  484. data->set.ftp_use_epsv = va_arg(param, long)?TRUE:FALSE;
  485. break;
  486. case CURLOPT_HTTPHEADER:
  487. /*
  488. * Set a list with HTTP headers to use (or replace internals with)
  489. */
  490. data->set.headers = va_arg(param, struct curl_slist *);
  491. break;
  492. case CURLOPT_CUSTOMREQUEST:
  493. /*
  494. * Set a custom string to use as request
  495. */
  496. data->set.customrequest = va_arg(param, char *);
  497. /* we don't set
  498. data->set.httpreq = HTTPREQ_CUSTOM;
  499. here, we continue as if we were using the already set type
  500. and this just changes the actual request keyword */
  501. break;
  502. case CURLOPT_HTTPPOST:
  503. /*
  504. * Set to make us do HTTP POST
  505. */
  506. data->set.httppost = va_arg(param, struct HttpPost *);
  507. if(data->set.httppost)
  508. data->set.httpreq = HTTPREQ_POST_FORM;
  509. break;
  510. case CURLOPT_HTTPGET:
  511. /*
  512. * Set to force us do HTTP GET
  513. */
  514. if(va_arg(param, long)) {
  515. data->set.httpreq = HTTPREQ_GET;
  516. data->set.upload = FALSE; /* switch off upload */
  517. }
  518. break;
  519. case CURLOPT_INFILE:
  520. /*
  521. * FILE pointer to read the file to be uploaded from. Or possibly
  522. * used as argument to the read callback.
  523. */
  524. data->set.in = va_arg(param, FILE *);
  525. break;
  526. case CURLOPT_INFILESIZE:
  527. /*
  528. * If known, this should inform curl about the file size of the
  529. * to-be-uploaded file.
  530. */
  531. data->set.infilesize = va_arg(param, long);
  532. break;
  533. case CURLOPT_LOW_SPEED_LIMIT:
  534. /*
  535. * The low speed limit that if transfers are below this for
  536. * CURLOPT_LOW_SPEED_TIME, the transfer is aborted.
  537. */
  538. data->set.low_speed_limit=va_arg(param, long);
  539. break;
  540. case CURLOPT_LOW_SPEED_TIME:
  541. /*
  542. * The low speed time that if transfers are below the set
  543. * CURLOPT_LOW_SPEED_LIMIT during this time, the transfer is aborted.
  544. */
  545. data->set.low_speed_time=va_arg(param, long);
  546. break;
  547. case CURLOPT_URL:
  548. /*
  549. * The URL to fetch.
  550. */
  551. if(data->change.url_alloc) {
  552. /* the already set URL is allocated, free it first! */
  553. free(data->change.url);
  554. data->change.url_alloc=FALSE;
  555. }
  556. data->set.set_url = va_arg(param, char *);
  557. data->change.url = data->set.set_url;
  558. break;
  559. case CURLOPT_PORT:
  560. /*
  561. * The port number to use when getting the URL
  562. */
  563. data->set.use_port = va_arg(param, long);
  564. break;
  565. case CURLOPT_POST:
  566. /* Does this option serve a purpose anymore? */
  567. if(va_arg(param, long))
  568. data->set.httpreq = HTTPREQ_POST;
  569. break;
  570. case CURLOPT_POSTFIELDS:
  571. /*
  572. * A string with POST data. Makes curl HTTP POST.
  573. */
  574. data->set.postfields = va_arg(param, char *);
  575. if(data->set.postfields)
  576. data->set.httpreq = HTTPREQ_POST;
  577. break;
  578. case CURLOPT_POSTFIELDSIZE:
  579. /*
  580. * The size of the POSTFIELD data, if curl should now do a strlen
  581. * to find out. Enables binary posts.
  582. */
  583. data->set.postfieldsize = va_arg(param, long);
  584. break;
  585. case CURLOPT_REFERER:
  586. /*
  587. * String to set in the HTTP Referer: field.
  588. */
  589. if(data->change.referer_alloc) {
  590. free(data->change.referer);
  591. data->change.referer_alloc = FALSE;
  592. }
  593. data->set.set_referer = va_arg(param, char *);
  594. data->change.referer = data->set.set_referer;
  595. break;
  596. case CURLOPT_AUTOREFERER:
  597. /*
  598. * Switch on automatic referer that gets set if curl follows locations.
  599. */
  600. data->set.http_auto_referer = va_arg(param, long)?1:0;
  601. break;
  602. case CURLOPT_PROXY:
  603. /*
  604. * Set proxy server:port to use as HTTP proxy
  605. */
  606. if(data->change.proxy_alloc) {
  607. /*
  608. * The already set string is allocated, free that first
  609. */
  610. data->change.proxy_alloc=FALSE;;
  611. free(data->change.proxy);
  612. }
  613. data->set.set_proxy = va_arg(param, char *);
  614. data->change.proxy = data->set.set_proxy;
  615. break;
  616. case CURLOPT_HTTPPROXYTUNNEL:
  617. /*
  618. * Tunnel operations through the proxy instead of normal proxy use
  619. */
  620. data->set.tunnel_thru_httpproxy = va_arg(param, long)?TRUE:FALSE;
  621. break;
  622. case CURLOPT_PROXYPORT:
  623. /*
  624. * Explicitly set HTTP proxy port number.
  625. */
  626. data->set.proxyport = va_arg(param, long);
  627. break;
  628. case CURLOPT_TIMEOUT:
  629. /*
  630. * The maximum time you allow curl to use for a single transfer
  631. * operation.
  632. */
  633. data->set.timeout = va_arg(param, long);
  634. break;
  635. case CURLOPT_CONNECTTIMEOUT:
  636. /*
  637. * The maximum time you allow curl to use to connect.
  638. */
  639. data->set.connecttimeout = va_arg(param, long);
  640. break;
  641. case CURLOPT_MAXREDIRS:
  642. /*
  643. * The maximum amount of hops you allow curl to follow Location:
  644. * headers. This should mostly be used to detect never-ending loops.
  645. */
  646. data->set.maxredirs = va_arg(param, long);
  647. break;
  648. case CURLOPT_USERAGENT:
  649. /*
  650. * String to use in the HTTP User-Agent field
  651. */
  652. data->set.useragent = va_arg(param, char *);
  653. break;
  654. case CURLOPT_USERPWD:
  655. /*
  656. * user:password to use in the operation
  657. */
  658. data->set.userpwd = va_arg(param, char *);
  659. break;
  660. case CURLOPT_POSTQUOTE:
  661. /*
  662. * List of RAW FTP commands to use after a transfer
  663. */
  664. data->set.postquote = va_arg(param, struct curl_slist *);
  665. break;
  666. case CURLOPT_PREQUOTE:
  667. /*
  668. * List of RAW FTP commands to use prior to RETR (Wesley Laxton)
  669. */
  670. data->set.prequote = va_arg(param, struct curl_slist *);
  671. break;
  672. case CURLOPT_QUOTE:
  673. /*
  674. * List of RAW FTP commands to use before a transfer
  675. */
  676. data->set.quote = va_arg(param, struct curl_slist *);
  677. break;
  678. case CURLOPT_PROGRESSFUNCTION:
  679. /*
  680. * Progress callback function
  681. */
  682. data->set.fprogress = va_arg(param, curl_progress_callback);
  683. data->progress.callback = TRUE; /* no longer internal */
  684. break;
  685. case CURLOPT_PROGRESSDATA:
  686. /*
  687. * Custom client data to pass to the progress callback
  688. */
  689. data->set.progress_client = va_arg(param, void *);
  690. break;
  691. case CURLOPT_PASSWDFUNCTION:
  692. /*
  693. * Password prompt callback
  694. */
  695. data->set.fpasswd = va_arg(param, curl_passwd_callback);
  696. break;
  697. case CURLOPT_PASSWDDATA:
  698. /*
  699. * Custom client data to pass to the password callback
  700. */
  701. data->set.passwd_client = va_arg(param, void *);
  702. break;
  703. case CURLOPT_PROXYUSERPWD:
  704. /*
  705. * user:password needed to use the proxy
  706. */
  707. data->set.proxyuserpwd = va_arg(param, char *);
  708. break;
  709. case CURLOPT_RANGE:
  710. /*
  711. * What range of the file you want to transfer
  712. */
  713. data->set.set_range = va_arg(param, char *);
  714. break;
  715. case CURLOPT_RESUME_FROM:
  716. /*
  717. * Resume transfer at the give file position
  718. */
  719. data->set.set_resume_from = va_arg(param, long);
  720. break;
  721. case CURLOPT_STDERR:
  722. /*
  723. * Set to a FILE * that should receive all error writes. This
  724. * defaults to stderr for normal operations.
  725. */
  726. data->set.err = va_arg(param, FILE *);
  727. break;
  728. case CURLOPT_HEADERFUNCTION:
  729. /*
  730. * Set header write callback
  731. */
  732. data->set.fwrite_header = va_arg(param, curl_write_callback);
  733. break;
  734. case CURLOPT_WRITEFUNCTION:
  735. /*
  736. * Set data write callback
  737. */
  738. data->set.fwrite = va_arg(param, curl_write_callback);
  739. break;
  740. case CURLOPT_READFUNCTION:
  741. /*
  742. * Read data callback
  743. */
  744. data->set.fread = va_arg(param, curl_read_callback);
  745. break;
  746. case CURLOPT_SSLCERT:
  747. /*
  748. * String that holds file name of the SSL certificate to use
  749. */
  750. data->set.cert = va_arg(param, char *);
  751. break;
  752. case CURLOPT_SSLCERTTYPE:
  753. /*
  754. * String that holds file type of the SSL certificate to use
  755. */
  756. data->set.cert_type = va_arg(param, char *);
  757. break;
  758. case CURLOPT_SSLKEY:
  759. /*
  760. * String that holds file name of the SSL certificate to use
  761. */
  762. data->set.key = va_arg(param, char *);
  763. break;
  764. case CURLOPT_SSLKEYTYPE:
  765. /*
  766. * String that holds file type of the SSL certificate to use
  767. */
  768. data->set.key_type = va_arg(param, char *);
  769. break;
  770. case CURLOPT_SSLKEYPASSWD:
  771. /*
  772. * String that holds the SSL private key password.
  773. */
  774. data->set.key_passwd = va_arg(param, char *);
  775. break;
  776. case CURLOPT_SSLENGINE:
  777. /*
  778. * String that holds the SSL crypto engine.
  779. */
  780. #ifdef HAVE_OPENSSL_ENGINE_H
  781. {
  782. const char *cpTemp = va_arg(param, char *);
  783. ENGINE *e;
  784. if (cpTemp && cpTemp[0]) {
  785. e = ENGINE_by_id(cpTemp);
  786. if (e) {
  787. if (data->engine) {
  788. ENGINE_free(data->engine);
  789. }
  790. data->engine = e;
  791. }
  792. else {
  793. failf(data, "SSL Engine '%s' not found", cpTemp);
  794. return CURLE_SSL_ENGINE_NOTFOUND;
  795. }
  796. }
  797. }
  798. #else
  799. return CURLE_SSL_ENGINE_NOTFOUND;
  800. #endif
  801. break;
  802. case CURLOPT_SSLENGINE_DEFAULT:
  803. /*
  804. * flag to set engine as default.
  805. */
  806. #ifdef HAVE_OPENSSL_ENGINE_H
  807. if (data->engine) {
  808. if (ENGINE_set_default(data->engine, ENGINE_METHOD_ALL) > 0) {
  809. #ifdef DEBUG
  810. fprintf(stderr,"set default crypto engine\n");
  811. #endif
  812. }
  813. else {
  814. #ifdef DEBUG
  815. failf(data, "set default crypto engine failed");
  816. #endif
  817. return CURLE_SSL_ENGINE_SETFAILED;
  818. }
  819. }
  820. #endif
  821. break;
  822. case CURLOPT_CRLF:
  823. /*
  824. * Kludgy option to enable CRLF convertions. Subject for removal.
  825. */
  826. data->set.crlf = va_arg(param, long)?TRUE:FALSE;
  827. break;
  828. case CURLOPT_INTERFACE:
  829. /*
  830. * Set what interface to bind to when performing an operation and thus
  831. * what from-IP your connection will use.
  832. */
  833. data->set.device = va_arg(param, char *);
  834. break;
  835. case CURLOPT_KRB4LEVEL:
  836. /*
  837. * A string that defines the krb4 security level.
  838. */
  839. data->set.krb4_level = va_arg(param, char *);
  840. data->set.krb4=data->set.krb4_level?TRUE:FALSE;
  841. break;
  842. case CURLOPT_SSL_VERIFYPEER:
  843. /*
  844. * Enable peer SSL verifying.
  845. */
  846. data->set.ssl.verifypeer = va_arg(param, long);
  847. break;
  848. case CURLOPT_SSL_VERIFYHOST:
  849. /*
  850. * Enable verification of the CN contained in the peer certificate
  851. */
  852. data->set.ssl.verifyhost = va_arg(param, long);
  853. break;
  854. case CURLOPT_CAINFO:
  855. /*
  856. * Set CA info for SSL connection. Specify file name of the CA certificate
  857. */
  858. data->set.ssl.CAfile = va_arg(param, char *);
  859. data->set.ssl.CApath = NULL; /*This does not work on windows.*/
  860. break;
  861. case CURLOPT_TELNETOPTIONS:
  862. /*
  863. * Set a linked list of telnet options
  864. */
  865. data->set.telnet_options = va_arg(param, struct curl_slist *);
  866. break;
  867. default:
  868. /* unknown tag and its companion, just ignore: */
  869. return CURLE_READ_ERROR; /* correct this */
  870. }
  871. return CURLE_OK;
  872. }
  873. CURLcode Curl_disconnect(struct connectdata *conn)
  874. {
  875. if(!conn)
  876. return CURLE_OK; /* this is closed and fine already */
  877. /*
  878. * The range string is usually freed in curl_done(), but we might
  879. * get here *instead* if we fail prematurely. Thus we need to be able
  880. * to free this resource here as well.
  881. */
  882. if(conn->bits.rangestringalloc) {
  883. free(conn->range);
  884. conn->bits.rangestringalloc = FALSE;
  885. }
  886. if(-1 != conn->connectindex) {
  887. /* unlink ourselves! */
  888. infof(conn->data, "Closing connection #%d\n", conn->connectindex);
  889. conn->data->state.connects[conn->connectindex] = NULL;
  890. }
  891. if(conn->curl_disconnect)
  892. /* This is set if protocol-specific cleanups should be made */
  893. conn->curl_disconnect(conn);
  894. if(conn->proto.generic)
  895. free(conn->proto.generic);
  896. if(conn->newurl)
  897. free(conn->newurl);
  898. if(conn->path) /* the URL path part */
  899. free(conn->path);
  900. #ifdef USE_SSLEAY
  901. Curl_SSL_Close(conn);
  902. #endif /* USE_SSLEAY */
  903. /* close possibly still open sockets */
  904. if(-1 != conn->secondarysocket)
  905. sclose(conn->secondarysocket);
  906. if(-1 != conn->firstsocket)
  907. sclose(conn->firstsocket);
  908. if(conn->allocptr.proxyuserpwd)
  909. free(conn->allocptr.proxyuserpwd);
  910. if(conn->allocptr.uagent)
  911. free(conn->allocptr.uagent);
  912. if(conn->allocptr.userpwd)
  913. free(conn->allocptr.userpwd);
  914. if(conn->allocptr.rangeline)
  915. free(conn->allocptr.rangeline);
  916. if(conn->allocptr.ref)
  917. free(conn->allocptr.ref);
  918. if(conn->allocptr.cookie)
  919. free(conn->allocptr.cookie);
  920. if(conn->allocptr.host)
  921. free(conn->allocptr.host);
  922. if(conn->proxyhost)
  923. free(conn->proxyhost);
  924. free(conn); /* free all the connection oriented data */
  925. return CURLE_OK;
  926. }
  927. /*
  928. * This function should return TRUE if the socket is to be assumed to
  929. * be dead. Most commonly this happens when the server has closed the
  930. * connection due to inactivity.
  931. */
  932. static bool SocketIsDead(int sock)
  933. {
  934. int sval;
  935. bool ret_val = TRUE;
  936. fd_set check_set;
  937. struct timeval to;
  938. FD_ZERO(&check_set);
  939. FD_SET(sock,&check_set);
  940. to.tv_sec = 0;
  941. to.tv_usec = 1;
  942. sval = select(sock + 1, &check_set, 0, 0, &to);
  943. if(sval == 0)
  944. /* timeout */
  945. ret_val = FALSE;
  946. return ret_val;
  947. }
  948. /*
  949. * Given one filled in connection struct (named needle), this function should
  950. * detect if there already is one that have all the significant details
  951. * exactly the same and thus should be used instead.
  952. */
  953. static bool
  954. ConnectionExists(struct SessionHandle *data,
  955. struct connectdata *needle,
  956. struct connectdata **usethis)
  957. {
  958. long i;
  959. struct connectdata *check;
  960. for(i=0; i< data->state.numconnects; i++) {
  961. /*
  962. * Note that if we use a HTTP proxy, we check connections to that
  963. * proxy and not to the actual remote server.
  964. */
  965. check = data->state.connects[i];
  966. if(!check)
  967. /* NULL pointer means not filled-in entry */
  968. continue;
  969. if(!needle->bits.httpproxy || needle->protocol&PROT_SSL) {
  970. /* The requested connection does not use a HTTP proxy or it
  971. uses SSL. */
  972. if(!(needle->protocol&PROT_SSL) && check->bits.httpproxy)
  973. /* we don't do SSL but the cached connection has a proxy,
  974. then don't match this */
  975. continue;
  976. if(strequal(needle->protostr, check->protostr) &&
  977. strequal(needle->name, check->name) &&
  978. (needle->remote_port == check->remote_port) ) {
  979. bool dead;
  980. if(strequal(needle->protostr, "FTP")) {
  981. /* This is FTP, verify that we're using the same name and
  982. password as well */
  983. if(!strequal(needle->data->state.user, check->proto.ftp->user) ||
  984. !strequal(needle->data->state.passwd, check->proto.ftp->passwd)) {
  985. /* one of them was different */
  986. continue;
  987. }
  988. }
  989. dead = SocketIsDead(check->firstsocket);
  990. if(dead) {
  991. /*
  992. * Even though the connection seems to have passed away, we could
  993. * still make an effort to get the name information, as we intend to
  994. * connect to the same host again.
  995. *
  996. * This is now subject to discussion. What do you think?
  997. */
  998. infof(data, "Connection %d seems to be dead!\n", i);
  999. Curl_disconnect(check); /* disconnect resources */
  1000. data->state.connects[i]=NULL; /* nothing here */
  1001. /* There's no need to continue search, because we only store
  1002. one connection for each unique set of identifiers */
  1003. return FALSE;
  1004. }
  1005. *usethis = check;
  1006. return TRUE; /* yes, we found one to use! */
  1007. }
  1008. }
  1009. else { /* The requested needle connection is using a proxy,
  1010. is the checked one using the same? */
  1011. if(check->bits.httpproxy &&
  1012. strequal(needle->proxyhost, check->proxyhost) &&
  1013. needle->port == check->port) {
  1014. /* This is the same proxy connection, use it! */
  1015. *usethis = check;
  1016. return TRUE;
  1017. }
  1018. }
  1019. }
  1020. return FALSE; /* no matching connecting exists */
  1021. }
  1022. /*
  1023. * This function frees/closes a connection in the connection cache. This
  1024. * should take the previously set policy into account when deciding which
  1025. * of the connections to kill.
  1026. */
  1027. static int
  1028. ConnectionKillOne(struct SessionHandle *data)
  1029. {
  1030. long i;
  1031. struct connectdata *conn;
  1032. int highscore=-1;
  1033. int connindex=-1;
  1034. int score;
  1035. CURLcode result;
  1036. struct timeval now;
  1037. now = Curl_tvnow();
  1038. for(i=0; i< data->state.numconnects; i++) {
  1039. conn = data->state.connects[i];
  1040. if(!conn)
  1041. continue;
  1042. /*
  1043. * By using the set policy, we score each connection.
  1044. */
  1045. switch(data->set.closepolicy) {
  1046. case CURLCLOSEPOLICY_LEAST_RECENTLY_USED:
  1047. default:
  1048. /*
  1049. * Set higher score for the age passed since the connection
  1050. * was used.
  1051. */
  1052. score = Curl_tvdiff(now, conn->now);
  1053. break;
  1054. case CURLCLOSEPOLICY_OLDEST:
  1055. /*
  1056. * Set higher score for the age passed since the connection
  1057. * was created.
  1058. */
  1059. score = Curl_tvdiff(now, conn->created);
  1060. break;
  1061. }
  1062. if(score > highscore) {
  1063. highscore = score;
  1064. connindex = i;
  1065. }
  1066. }
  1067. if(connindex >= 0) {
  1068. /* the winner gets the honour of being disconnected */
  1069. result = Curl_disconnect(data->state.connects[connindex]);
  1070. /* clean the array entry */
  1071. data->state.connects[connindex] = NULL;
  1072. }
  1073. return connindex; /* return the available index or -1 */
  1074. }
  1075. /*
  1076. * The given input connection struct pointer is to be stored. If the "cache"
  1077. * is already full, we must clean out the most suitable using the previously
  1078. * set policy.
  1079. *
  1080. * The given connection should be unique. That must've been checked prior to
  1081. * this call.
  1082. */
  1083. static unsigned int
  1084. ConnectionStore(struct SessionHandle *data,
  1085. struct connectdata *conn)
  1086. {
  1087. long i;
  1088. for(i=0; i< data->state.numconnects; i++) {
  1089. if(!data->state.connects[i])
  1090. break;
  1091. }
  1092. if(i == data->state.numconnects) {
  1093. /* there was no room available, kill one */
  1094. i = ConnectionKillOne(data);
  1095. infof(data, "Connection (#%d) was killed to make room\n", i);
  1096. }
  1097. if(-1 != i) {
  1098. /* only do this if a true index was returned, if -1 was returned there
  1099. is no room in the cache for an unknown reason and we cannot store
  1100. this there. */
  1101. data->state.connects[i] = conn; /* fill in this */
  1102. conn->connectindex = i; /* make the child know where the pointer to this
  1103. particular data is stored */
  1104. }
  1105. return i;
  1106. }
  1107. static CURLcode ConnectPlease(struct connectdata *conn)
  1108. {
  1109. CURLcode result;
  1110. Curl_ipconnect *addr;
  1111. /*************************************************************
  1112. * Connect to server/proxy
  1113. *************************************************************/
  1114. result= Curl_connecthost(conn,
  1115. conn->hostaddr,
  1116. conn->port,
  1117. &conn->firstsocket,
  1118. &addr);
  1119. if(CURLE_OK == result) {
  1120. /* All is cool, then we store the current information from the hostaddr
  1121. struct to the serv_addr, as it might be needed later. The address
  1122. returned from the function above is crucial here. */
  1123. #ifdef ENABLE_IPV6
  1124. conn->serv_addr = addr;
  1125. #else
  1126. memset((char *) &conn->serv_addr, '\0', sizeof(conn->serv_addr));
  1127. memcpy((char *)&(conn->serv_addr.sin_addr),
  1128. (struct in_addr *)addr, sizeof(struct in_addr));
  1129. conn->serv_addr.sin_family = conn->hostaddr->h_addrtype;
  1130. conn->serv_addr.sin_port = htons(conn->port);
  1131. #endif
  1132. }
  1133. return result;
  1134. }
  1135. static CURLcode CreateConnection(struct SessionHandle *data,
  1136. struct connectdata **in_connect)
  1137. {
  1138. char *tmp;
  1139. char *buf;
  1140. CURLcode result=CURLE_OK;
  1141. char resumerange[40]="";
  1142. struct connectdata *conn;
  1143. struct connectdata *conn_temp;
  1144. char endbracket;
  1145. int urllen;
  1146. #ifdef HAVE_INET_NTOA_R
  1147. char ntoa_buf[64];
  1148. #endif
  1149. #ifdef HAVE_ALARM
  1150. unsigned int prev_alarm;
  1151. #endif
  1152. #ifdef HAVE_SIGACTION
  1153. struct sigaction keep_sigact; /* store the old struct here */
  1154. bool keep_copysig; /* did copy it? */
  1155. #else
  1156. #ifdef HAVE_SIGNAL
  1157. void *keep_sigact; /* store the old handler here */
  1158. #endif
  1159. #endif
  1160. /*************************************************************
  1161. * Check input data
  1162. *************************************************************/
  1163. if(!data->change.url)
  1164. return CURLE_URL_MALFORMAT;
  1165. /* First, split up the current URL in parts so that we can use the
  1166. parts for checking against the already present connections. In order
  1167. to not have to modify everything at once, we allocate a temporary
  1168. connection data struct and fill in for comparison purposes. */
  1169. conn = (struct connectdata *)malloc(sizeof(struct connectdata));
  1170. if(!conn) {
  1171. *in_connect = NULL; /* clear the pointer */
  1172. return CURLE_OUT_OF_MEMORY;
  1173. }
  1174. /* We must set the return variable as soon as possible, so that our
  1175. parent can cleanup any possible allocs we may have done before
  1176. any failure */
  1177. *in_connect = conn;
  1178. /* we have to init the struct */
  1179. memset(conn, 0, sizeof(struct connectdata));
  1180. /* and we setup a few fields in case we end up actually using this struct */
  1181. conn->data = data; /* remember our daddy */
  1182. conn->firstsocket = -1; /* no file descriptor */
  1183. conn->secondarysocket = -1; /* no file descriptor */
  1184. conn->connectindex = -1; /* no index */
  1185. conn->bits.httpproxy = data->change.proxy?TRUE:FALSE; /* proxy-or-not */
  1186. conn->bits.use_range = data->set.set_range?TRUE:FALSE; /* range status */
  1187. conn->range = data->set.set_range; /* clone the range setting */
  1188. conn->resume_from = data->set.set_resume_from; /* inherite resume_from */
  1189. /* Default protocol-independent behavior doesn't support persistant
  1190. connections, so we set this to force-close. Protocols that support
  1191. this need to set this to FALSE in their "curl_do" functions. */
  1192. conn->bits.close = TRUE;
  1193. /* inherite initial knowledge from the data struct */
  1194. conn->bits.user_passwd = data->set.userpwd?1:0;
  1195. conn->bits.proxy_user_passwd = data->set.proxyuserpwd?1:0;
  1196. /* maxdownload must be -1 on init, as 0 is a valid value! */
  1197. conn->maxdownload = -1; /* might have been used previously! */
  1198. /* Store creation time to help future close decision making */
  1199. conn->created = Curl_tvnow();
  1200. /***********************************************************
  1201. * We need to allocate memory to store the path in. We get the size of the
  1202. * full URL to be sure, and we need to make it at least 256 bytes since
  1203. * other parts of the code will rely on this fact
  1204. ***********************************************************/
  1205. #define LEAST_PATH_ALLOC 256
  1206. urllen=strlen(data->change.url);
  1207. if(urllen < LEAST_PATH_ALLOC)
  1208. urllen=LEAST_PATH_ALLOC;
  1209. conn->path=(char *)malloc(urllen);
  1210. if(NULL == conn->path)
  1211. return CURLE_OUT_OF_MEMORY; /* really bad error */
  1212. /*************************************************************
  1213. * Parse the URL.
  1214. *
  1215. * We need to parse the url even when using the proxy, because we will need
  1216. * the hostname and port in case we are trying to SSL connect through the
  1217. * proxy -- and we don't know if we will need to use SSL until we parse the
  1218. * url ...
  1219. ************************************************************/
  1220. if((2 == sscanf(data->change.url, "%64[^:]://%[^\n]",
  1221. conn->protostr,
  1222. conn->path)) && strequal(conn->protostr, "file")) {
  1223. /*
  1224. * we deal with file://<host>/<path> differently since it supports no
  1225. * hostname other than "localhost" and "127.0.0.1", which is unique among
  1226. * the URL protocols specified in RFC 1738
  1227. */
  1228. if(conn->path[0] != '/') {
  1229. /* the URL included a host name, we ignore host names in file:// URLs
  1230. as the standards don't define what to do with them */
  1231. char *ptr=strchr(conn->path, '/');
  1232. if(ptr) {
  1233. /* there was a slash present
  1234. RFC1738 (section 3.1, page 5) says:
  1235. The rest of the locator consists of data specific to the scheme,
  1236. and is known as the "url-path". It supplies the details of how the
  1237. specified resource can be accessed. Note that the "/" between the
  1238. host (or port) and the url-path is NOT part of the url-path.
  1239. As most agents use file://localhost/foo to get '/foo' although the
  1240. slash preceeding foo is a separator and not a slash for the path,
  1241. a URL as file://localhost//foo must be valid as well, to refer to
  1242. the same file with an absolute path.
  1243. */
  1244. if(ptr[1] && ('/' == ptr[1]))
  1245. /* if there was two slashes, we skip the first one as that is then
  1246. used truly as a separator */
  1247. ptr++;
  1248. strcpy(conn->path, ptr);
  1249. }
  1250. }
  1251. strcpy(conn->protostr, "file"); /* store protocol string lowercase */
  1252. }
  1253. else {
  1254. /* Set default host and default path */
  1255. strcpy(conn->gname, "curl.haxx.se");
  1256. strcpy(conn->path, "/");
  1257. if (2 > sscanf(data->change.url,
  1258. "%64[^\n:]://%512[^\n/]%[^\n]",
  1259. conn->protostr, conn->gname, conn->path)) {
  1260. /*
  1261. * The URL was badly formatted, let's try the browser-style _without_
  1262. * protocol specified like 'http://'.
  1263. */
  1264. if((1 > sscanf(data->change.url, "%512[^\n/]%[^\n]",
  1265. conn->gname, conn->path)) ) {
  1266. /*
  1267. * We couldn't even get this format.
  1268. */
  1269. failf(data, "<url> malformed");
  1270. return CURLE_URL_MALFORMAT;
  1271. }
  1272. /*
  1273. * Since there was no protocol part specified, we guess what protocol it
  1274. * is based on the first letters of the server name.
  1275. */
  1276. if(strnequal(conn->gname, "FTP", 3)) {
  1277. strcpy(conn->protostr, "ftp");
  1278. }
  1279. else if(strnequal(conn->gname, "GOPHER", 6))
  1280. strcpy(conn->protostr, "gopher");
  1281. #ifdef USE_SSLEAY
  1282. else if(strnequal(conn->gname, "HTTPS", 5))
  1283. strcpy(conn->protostr, "https");
  1284. else if(strnequal(conn->gname, "FTPS", 4))
  1285. strcpy(conn->protostr, "ftps");
  1286. #endif /* USE_SSLEAY */
  1287. else if(strnequal(conn->gname, "TELNET", 6))
  1288. strcpy(conn->protostr, "telnet");
  1289. else if (strnequal(conn->gname, "DICT", sizeof("DICT")-1))
  1290. strcpy(conn->protostr, "DICT");
  1291. else if (strnequal(conn->gname, "LDAP", sizeof("LDAP")-1))
  1292. strcpy(conn->protostr, "LDAP");
  1293. else {
  1294. strcpy(conn->protostr, "http");
  1295. }
  1296. conn->protocol |= PROT_MISSING; /* not given in URL */
  1297. }
  1298. }
  1299. buf = data->state.buffer; /* this is our buffer */
  1300. /*************************************************************
  1301. * Take care of user and password authentication stuff
  1302. *************************************************************/
  1303. if(conn->bits.user_passwd && !data->set.use_netrc) {
  1304. data->state.user[0] =0;
  1305. data->state.passwd[0]=0;
  1306. if(*data->set.userpwd != ':') {
  1307. /* the name is given, get user+password */
  1308. sscanf(data->set.userpwd, "%127[^:]:%127[^\n]",
  1309. data->state.user, data->state.passwd);
  1310. }
  1311. else
  1312. /* no name given, get the password only */
  1313. sscanf(data->set.userpwd+1, "%127[^\n]", data->state.passwd);
  1314. /* check for password, if no ask for one */
  1315. if( !data->state.passwd[0] ) {
  1316. if(!data->set.fpasswd ||
  1317. data->set.fpasswd(data->set.passwd_client,
  1318. "password:", data->state.passwd,
  1319. sizeof(data->state.passwd)))
  1320. return CURLE_BAD_PASSWORD_ENTERED;
  1321. }
  1322. }
  1323. /*************************************************************
  1324. * Take care of proxy authentication stuff
  1325. *************************************************************/
  1326. if(conn->bits.proxy_user_passwd) {
  1327. data->state.proxyuser[0] =0;
  1328. data->state.proxypasswd[0]=0;
  1329. if(*data->set.proxyuserpwd != ':') {
  1330. /* the name is given, get user+password */
  1331. sscanf(data->set.proxyuserpwd, "%127[^:]:%127[^\n]",
  1332. data->state.proxyuser, data->state.proxypasswd);
  1333. }
  1334. else
  1335. /* no name given, get the password only */
  1336. sscanf(data->set.proxyuserpwd+1, "%127[^\n]", data->state.proxypasswd);
  1337. /* check for password, if no ask for one */
  1338. if( !data->state.proxypasswd[0] ) {
  1339. if(!data->set.fpasswd ||
  1340. data->set.fpasswd( data->set.passwd_client,
  1341. "proxy password:",
  1342. data->state.proxypasswd,
  1343. sizeof(data->state.proxypasswd)))
  1344. return CURLE_BAD_PASSWORD_ENTERED;
  1345. }
  1346. }
  1347. /*************************************************************
  1348. * Set a few convenience pointers
  1349. *************************************************************/
  1350. conn->name = conn->gname;
  1351. conn->ppath = conn->path;
  1352. conn->hostname = conn->name;
  1353. /*************************************************************
  1354. * Detect what (if any) proxy to use
  1355. *************************************************************/
  1356. if(!data->change.proxy) {
  1357. /* If proxy was not specified, we check for default proxy environment
  1358. * variables, to enable i.e Lynx compliance:
  1359. *
  1360. * http_proxy=http://some.server.dom:port/
  1361. * https_proxy=http://some.server.dom:port/
  1362. * ftp_proxy=http://some.server.dom:port/
  1363. * gopher_proxy=http://some.server.dom:port/
  1364. * no_proxy=domain1.dom,host.domain2.dom
  1365. * (a comma-separated list of hosts which should
  1366. * not be proxied, or an asterisk to override
  1367. * all proxy variables)
  1368. * all_proxy=http://some.server.dom:port/
  1369. * (seems to exist for the CERN www lib. Probably
  1370. * the first to check for.)
  1371. *
  1372. * For compatibility, the all-uppercase versions of these variables are
  1373. * checked if the lowercase versions don't exist.
  1374. */
  1375. char *no_proxy=NULL;
  1376. char *no_proxy_tok_buf;
  1377. char *proxy=NULL;
  1378. char proxy_env[128];
  1379. no_proxy=curl_getenv("no_proxy");
  1380. if(!no_proxy)
  1381. no_proxy=curl_getenv("NO_PROXY");
  1382. if(!no_proxy || !strequal("*", no_proxy)) {
  1383. /* NO_PROXY wasn't specified or it wasn't just an asterisk */
  1384. char *nope;
  1385. nope=no_proxy?strtok_r(no_proxy, ", ", &no_proxy_tok_buf):NULL;
  1386. while(nope) {
  1387. if(strlen(nope) <= strlen(conn->name)) {
  1388. char *checkn=
  1389. conn->name + strlen(conn->name) - strlen(nope);
  1390. if(strnequal(nope, checkn, strlen(nope))) {
  1391. /* no proxy for this host! */
  1392. break;
  1393. }
  1394. }
  1395. nope=strtok_r(NULL, ", ", &no_proxy_tok_buf);
  1396. }
  1397. if(!nope) {
  1398. /* It was not listed as without proxy */
  1399. char *protop = conn->protostr;
  1400. char *envp = proxy_env;
  1401. char *prox;
  1402. /* Now, build <protocol>_proxy and check for such a one to use */
  1403. while(*protop)
  1404. *envp++ = tolower(*protop++);
  1405. /* append _proxy */
  1406. strcpy(envp, "_proxy");
  1407. /* read the protocol proxy: */
  1408. prox=curl_getenv(proxy_env);
  1409. /*
  1410. * We don't try the uppercase version of HTTP_PROXY because of
  1411. * security reasons:
  1412. *
  1413. * When curl is used in a webserver application
  1414. * environment (cgi or php), this environment variable can
  1415. * be controlled by the web server user by setting the
  1416. * http header 'Proxy:' to some value.
  1417. *
  1418. * This can cause 'internal' http/ftp requests to be
  1419. * arbitrarily redirected by any external attacker.
  1420. */
  1421. if(!prox && !strequal("http_proxy", proxy_env)) {
  1422. /* There was no lowercase variable, try the uppercase version: */
  1423. for(envp = proxy_env; *envp; envp++)
  1424. *envp = toupper(*envp);
  1425. prox=curl_getenv(proxy_env);
  1426. }
  1427. if(prox && *prox) { /* don't count "" strings */
  1428. proxy = prox; /* use this */
  1429. }
  1430. else {
  1431. proxy = curl_getenv("all_proxy"); /* default proxy to use */
  1432. if(!proxy)
  1433. proxy=curl_getenv("ALL_PROXY");
  1434. }
  1435. if(proxy && *proxy) {
  1436. /* we have a proxy here to set */
  1437. data->change.proxy = proxy;
  1438. data->change.proxy_alloc=TRUE; /* this needs to be freed later */
  1439. conn->bits.httpproxy = TRUE;
  1440. }
  1441. } /* if (!nope) - it wasn't specified non-proxy */
  1442. } /* NO_PROXY wasn't specified or '*' */
  1443. if(no_proxy)
  1444. free(no_proxy);
  1445. } /* if not using proxy */
  1446. /*************************************************************
  1447. * No protocol part in URL was used, add it!
  1448. *************************************************************/
  1449. if(conn->protocol&PROT_MISSING) {
  1450. /* We're guessing prefixes here and if we're told to use a proxy or if
  1451. we're gonna follow a Location: later or... then we need the protocol
  1452. part added so that we have a valid URL. */
  1453. char *reurl;
  1454. reurl = aprintf("%s://%s", conn->protostr, data->change.url);
  1455. if(!reurl)
  1456. return CURLE_OUT_OF_MEMORY;
  1457. data->change.url = reurl;
  1458. data->change.url_alloc = TRUE; /* free this later */
  1459. conn->protocol &= ~PROT_MISSING; /* switch that one off again */
  1460. }
  1461. /************************************************************
  1462. * RESUME on a HTTP page is a tricky business. First, let's just check that
  1463. * 'range' isn't used, then set the range parameter and leave the resume as
  1464. * it is to inform about this situation for later use. We will then
  1465. * "attempt" to resume, and if we're talking to a HTTP/1.1 (or later)
  1466. * server, we will get the document resumed. If we talk to a HTTP/1.0
  1467. * server, we just fail since we can't rewind the file writing from within
  1468. * this function.
  1469. ***********************************************************/
  1470. if(conn->resume_from) {
  1471. if(!conn->bits.use_range) {
  1472. /* if it already was in use, we just skip this */
  1473. snprintf(resumerange, sizeof(resumerange), "%d-", conn->resume_from);
  1474. conn->range=strdup(resumerange); /* tell ourselves to fetch this range */
  1475. conn->bits.rangestringalloc = TRUE; /* mark as allocated */
  1476. conn->bits.use_range = 1; /* switch on range usage */
  1477. }
  1478. }
  1479. /*************************************************************
  1480. * Setup internals depending on protocol
  1481. *************************************************************/
  1482. if (strequal(conn->protostr, "HTTP")) {
  1483. conn->port = (data->set.use_port && data->state.allow_port)?
  1484. data->set.use_port:PORT_HTTP;
  1485. conn->remote_port = PORT_HTTP;
  1486. conn->protocol |= PROT_HTTP;
  1487. conn->curl_do = Curl_http;
  1488. conn->curl_done = Curl_http_done;
  1489. conn->curl_connect = Curl_http_connect;
  1490. }
  1491. else if (strequal(conn->protostr, "HTTPS")) {
  1492. #ifdef USE_SSLEAY
  1493. conn->port = (data->set.use_port && data->state.allow_port)?
  1494. data->set.use_port:PORT_HTTPS;
  1495. conn->remote_port = PORT_HTTPS;
  1496. conn->protocol |= PROT_HTTP|PROT_HTTPS|PROT_SSL;
  1497. conn->curl_do = Curl_http;
  1498. conn->curl_done = Curl_http_done;
  1499. conn->curl_connect = Curl_http_connect;
  1500. #else /* USE_SSLEAY */
  1501. failf(data, LIBCURL_NAME
  1502. " was built with SSL disabled, https: not supported!");
  1503. return CURLE_UNSUPPORTED_PROTOCOL;
  1504. #endif /* !USE_SSLEAY */
  1505. }
  1506. else if (strequal(conn->protostr, "GOPHER")) {
  1507. conn->port = (data->set.use_port && data->state.allow_port)?
  1508. data->set.use_port:PORT_GOPHER;
  1509. conn->remote_port = PORT_GOPHER;
  1510. /* Skip /<item-type>/ in path if present */
  1511. if (isdigit((int)conn->path[1])) {
  1512. conn->ppath = strchr(&conn->path[1], '/');
  1513. if (conn->ppath == NULL)
  1514. conn->ppath = conn->path;
  1515. }
  1516. conn->protocol |= PROT_GOPHER;
  1517. conn->curl_do = Curl_http;
  1518. conn->curl_done = Curl_http_done;
  1519. }
  1520. else if(strequal(conn->protostr, "FTP") ||
  1521. strequal(conn->protostr, "FTPS")) {
  1522. char *type;
  1523. if(strequal(conn->protostr, "FTPS")) {
  1524. #ifdef USE_SSLEAY
  1525. conn->protocol |= PROT_FTPS|PROT_SSL;
  1526. #else
  1527. failf(data, LIBCURL_NAME
  1528. " was built with SSL disabled, ftps: not supported!");
  1529. return CURLE_UNSUPPORTED_PROTOCOL;
  1530. #endif /* !USE_SSLEAY */
  1531. }
  1532. conn->port = (data->set.use_port && data->state.allow_port)?
  1533. data->set.use_port:PORT_FTP;
  1534. conn->remote_port = PORT_FTP;
  1535. conn->protocol |= PROT_FTP;
  1536. if(data->change.proxy &&
  1537. !data->set.tunnel_thru_httpproxy) {
  1538. /* Unless we have asked to tunnel ftp operations through the proxy, we
  1539. switch and use HTTP operations only */
  1540. if(conn->protocol & PROT_FTPS) {
  1541. /* FTPS is a hacked protocol and does not work through your
  1542. ordinary http proxy! */
  1543. failf(data, "ftps does not work through http proxy!");
  1544. return CURLE_UNSUPPORTED_PROTOCOL;
  1545. }
  1546. conn->curl_do = Curl_http;
  1547. conn->curl_done = Curl_http_done;
  1548. }
  1549. else {
  1550. conn->curl_do = Curl_ftp;
  1551. conn->curl_done = Curl_ftp_done;
  1552. conn->curl_connect = Curl_ftp_connect;
  1553. conn->curl_disconnect = Curl_ftp_disconnect;
  1554. }
  1555. conn->ppath++; /* don't include the initial slash */
  1556. /* FTP URLs support an extension like ";type=<typecode>" that
  1557. * we'll try to get now! */
  1558. type=strstr(conn->ppath, ";type=");
  1559. if(!type) {
  1560. type=strstr(conn->gname, ";type=");
  1561. }
  1562. if(type) {
  1563. char command;
  1564. *type=0;
  1565. command = toupper(type[6]);
  1566. switch(command) {
  1567. case 'A': /* ASCII mode */
  1568. data->set.ftp_ascii = 1;
  1569. break;
  1570. case 'D': /* directory mode */
  1571. data->set.ftp_list_only = 1;
  1572. break;
  1573. case 'I': /* binary mode */
  1574. default:
  1575. /* switch off ASCII */
  1576. data->set.ftp_ascii = 0;
  1577. break;
  1578. }
  1579. }
  1580. }
  1581. else if(strequal(conn->protostr, "TELNET")) {
  1582. /* telnet testing factory */
  1583. conn->protocol |= PROT_TELNET;
  1584. conn->port = (data->set.use_port && data->state.allow_port)?
  1585. data->set.use_port: PORT_TELNET;
  1586. conn->remote_port = PORT_TELNET;
  1587. conn->curl_do = Curl_telnet;
  1588. conn->curl_done = Curl_telnet_done;
  1589. }
  1590. else if (strequal(conn->protostr, "DICT")) {
  1591. conn->protocol |= PROT_DICT;
  1592. conn->port = (data->set.use_port && data->state.allow_port)?
  1593. data->set.use_port:PORT_DICT;
  1594. conn->remote_port = PORT_DICT;
  1595. conn->curl_do = Curl_dict;
  1596. conn->curl_done = NULL; /* no DICT-specific done */
  1597. }
  1598. else if (strequal(conn->protostr, "LDAP")) {
  1599. conn->protocol |= PROT_LDAP;
  1600. conn->port = (data->set.use_port && data->state.allow_port)?
  1601. data->set.use_port:PORT_LDAP;
  1602. conn->remote_port = PORT_LDAP;
  1603. conn->curl_do = Curl_ldap;
  1604. conn->curl_done = NULL; /* no LDAP-specific done */
  1605. }
  1606. else if (strequal(conn->protostr, "FILE")) {
  1607. conn->protocol |= PROT_FILE;
  1608. conn->curl_do = Curl_file;
  1609. /* no done() function */
  1610. /* anyway, this is supposed to be the connect function so we better
  1611. at least check that the file is present here! */
  1612. result = Curl_file_connect(conn);
  1613. /* Setup a "faked" transfer that'll do nothing */
  1614. if(CURLE_OK == result) {
  1615. result = Curl_Transfer(conn, -1, -1, FALSE, NULL, /* no download */
  1616. -1, NULL); /* no upload */
  1617. }
  1618. return result;
  1619. }
  1620. else {
  1621. /* We fell through all checks and thus we don't support the specified
  1622. protocol */
  1623. failf(data, "Unsupported protocol: %s", conn->protostr);
  1624. return CURLE_UNSUPPORTED_PROTOCOL;
  1625. }
  1626. /*************************************************************
  1627. * .netrc scanning coming up
  1628. *************************************************************/
  1629. if(data->set.use_netrc) {
  1630. if(Curl_parsenetrc(conn->hostname,
  1631. data->state.user,
  1632. data->state.passwd)) {
  1633. infof(data, "Couldn't find host %s in the .netrc file, using defaults",
  1634. conn->hostname);
  1635. }
  1636. else
  1637. conn->bits.user_passwd = 1; /* enable user+password */
  1638. /* weather we failed or not, we don't know which fields that were filled
  1639. in anyway */
  1640. if(!data->state.user[0])
  1641. strcpy(data->state.user, CURL_DEFAULT_USER);
  1642. if(!data->state.passwd[0])
  1643. strcpy(data->state.passwd, CURL_DEFAULT_PASSWORD);
  1644. }
  1645. else if(!(conn->bits.user_passwd) &&
  1646. (conn->protocol & (PROT_FTP|PROT_HTTP)) ) {
  1647. /* This is a FTP or HTTP URL, and we haven't got the user+password in
  1648. * the extra parameter, we will now try to extract the possible
  1649. * user+password pair in a string like:
  1650. * ftp://user:[email protected]:8021/README */
  1651. char *ptr=NULL; /* assign to remove possible warnings */
  1652. if((ptr=strchr(conn->name, '@'))) {
  1653. /* there's a user+password given here, to the left of the @ */
  1654. data->state.user[0] =0;
  1655. data->state.passwd[0]=0;
  1656. if(*conn->name != ':') {
  1657. /* the name is given, get user+password */
  1658. sscanf(conn->name, "%127[^:@]:%127[^@]",
  1659. data->state.user, data->state.passwd);
  1660. }
  1661. else
  1662. /* no name given, get the password only */
  1663. sscanf(conn->name+1, "%127[^@]", data->state.passwd);
  1664. if(data->state.user[0]) {
  1665. char *newname=curl_unescape(data->state.user, 0);
  1666. if(strlen(newname) < sizeof(data->state.user)) {
  1667. strcpy(data->state.user, newname);
  1668. }
  1669. /* if the new name is longer than accepted, then just use
  1670. the unconverted name, it'll be wrong but what the heck */
  1671. free(newname);
  1672. }
  1673. /* check for password, if no ask for one */
  1674. if( !data->state.passwd[0] ) {
  1675. if(!data->set.fpasswd ||
  1676. data->set.fpasswd(data->set.passwd_client,
  1677. "password:", data->state.passwd,
  1678. sizeof(data->state.passwd)))
  1679. return CURLE_BAD_PASSWORD_ENTERED;
  1680. }
  1681. else {
  1682. /* we have a password found in the URL, decode it! */
  1683. char *newpasswd=curl_unescape(data->state.passwd, 0);
  1684. if(strlen(newpasswd) < sizeof(data->state.passwd)) {
  1685. strcpy(data->state.passwd, newpasswd);
  1686. }
  1687. free(newpasswd);
  1688. }
  1689. conn->name = ++ptr;
  1690. conn->bits.user_passwd=TRUE; /* enable user+password */
  1691. }
  1692. else {
  1693. strcpy(data->state.user, CURL_DEFAULT_USER);
  1694. strcpy(data->state.passwd, CURL_DEFAULT_PASSWORD);
  1695. }
  1696. }
  1697. /*************************************************************
  1698. * Figure out the remote port number
  1699. *
  1700. * No matter if we use a proxy or not, we have to figure out the remote
  1701. * port number of various reasons.
  1702. *
  1703. * To be able to detect port number flawlessly, we must not confuse them
  1704. * IPv6-specified addresses in the [0::1] style. (RFC2732)
  1705. *************************************************************/
  1706. if((1 == sscanf(conn->name, "[%*39[0-9a-fA-F:.]%c", &endbracket)) &&
  1707. (']' == endbracket)) {
  1708. /* This is a (IPv6-style) specified IP-address. We support _any_
  1709. IP within brackets to be really generic. */
  1710. conn->name++; /* pass the starting bracket */
  1711. tmp = strchr(conn->name, ']');
  1712. *tmp = 0; /* zero terminate */
  1713. tmp++; /* pass the ending bracket */
  1714. if(':' != *tmp)
  1715. tmp = NULL; /* no port number available */
  1716. }
  1717. else {
  1718. /* traditional IPv4-style port-extracting */
  1719. tmp = strchr(conn->name, ':');
  1720. }
  1721. if (tmp) {
  1722. *tmp++ = '\0'; /* cut off the name there */
  1723. conn->remote_port = atoi(tmp);
  1724. }
  1725. if(data->change.proxy) {
  1726. /* If this is supposed to use a proxy, we need to figure out the proxy
  1727. host name name, so that we can re-use an existing connection
  1728. that may exist registered to the same proxy host. */
  1729. char *prox_portno;
  1730. char *endofprot;
  1731. /* We need to make a duplicate of the proxy so that we can modify the
  1732. string safely. */
  1733. char *proxydup=strdup(data->change.proxy);
  1734. /* We use 'proxyptr' to point to the proxy name from now on... */
  1735. char *proxyptr=proxydup;
  1736. if(NULL == proxydup) {
  1737. failf(data, "memory shortage");
  1738. return CURLE_OUT_OF_MEMORY;
  1739. }
  1740. /* Daniel Dec 10, 1998:
  1741. We do the proxy host string parsing here. We want the host name and the
  1742. port name. Accept a protocol:// prefix, even though it should just be
  1743. ignored. */
  1744. /* 1. skip the protocol part if present */
  1745. endofprot=strstr(proxyptr, "://");
  1746. if(endofprot) {
  1747. proxyptr = endofprot+3;
  1748. }
  1749. /* allow user to specify proxy.server.com:1080 if desired */
  1750. prox_portno = strchr (proxyptr, ':');
  1751. if (prox_portno) {
  1752. *prox_portno = 0x0; /* cut off number from host name */
  1753. prox_portno ++;
  1754. /* now set the local port number */
  1755. conn->port = atoi(prox_portno);
  1756. }
  1757. else if(data->set.proxyport) {
  1758. /* None given in the proxy string, then get the default one if it is
  1759. given */
  1760. conn->port = data->set.proxyport;
  1761. }
  1762. /* now, clone the cleaned proxy host name */
  1763. conn->proxyhost = strdup(proxyptr);
  1764. free(proxydup); /* free the duplicate pointer and not the modified */
  1765. }
  1766. /*************************************************************
  1767. * Check the current list of connections to see if we can
  1768. * re-use an already existing one or if we have to create a
  1769. * new one.
  1770. *************************************************************/
  1771. /* reuse_fresh is set TRUE if we are told to use a fresh connection
  1772. by force */
  1773. if(!data->set.reuse_fresh &&
  1774. ConnectionExists(data, conn, &conn_temp)) {
  1775. /*
  1776. * We already have a connection for this, we got the former connection
  1777. * in the conn_temp variable and thus we need to cleanup the one we
  1778. * just allocated before we can move along and use the previously
  1779. * existing one.
  1780. */
  1781. struct connectdata *old_conn = conn;
  1782. char *path = old_conn->path; /* setup the current path pointer properly */
  1783. char *ppath = old_conn->ppath; /* this is the modified path pointer */
  1784. if(old_conn->proxyhost)
  1785. free(old_conn->proxyhost);
  1786. conn = conn_temp; /* use this connection from now on */
  1787. /* we need these pointers if we speak over a proxy */
  1788. conn->hostname = conn->gname;
  1789. conn->name = &conn->gname[old_conn->name - old_conn->gname];
  1790. free(conn->path); /* free the previously allocated path pointer */
  1791. /* 'path' points to the allocated data, 'ppath' may have been advanced
  1792. to point somewhere within the 'path' area. */
  1793. conn->path = path;
  1794. conn->ppath = ppath;
  1795. /* re-use init */
  1796. conn->bits.reuse = TRUE; /* yes, we're re-using here */
  1797. conn->bits.chunk = FALSE; /* always assume not chunked unless told
  1798. otherwise */
  1799. conn->maxdownload = -1; /* might have been used previously! */
  1800. free(old_conn); /* we don't need this anymore */
  1801. /*
  1802. * If we're doing a resumed transfer, we need to setup our stuff
  1803. * properly.
  1804. */
  1805. conn->resume_from = data->set.set_resume_from;
  1806. if (conn->resume_from) {
  1807. snprintf(resumerange, sizeof(resumerange), "%d-", conn->resume_from);
  1808. if (conn->bits.rangestringalloc == TRUE)
  1809. free(conn->range);
  1810. /* tell ourselves to fetch this range */
  1811. conn->range = strdup(resumerange);
  1812. conn->bits.use_range = TRUE; /* enable range download */
  1813. conn->bits.rangestringalloc = TRUE; /* mark range string allocated */
  1814. }
  1815. else if (data->set.set_range) {
  1816. /* There is a range, but is not a resume, useful for random ftp access */
  1817. conn->range = strdup(data->set.set_range);
  1818. conn->bits.rangestringalloc = TRUE; /* mark range string allocated */
  1819. conn->bits.use_range = TRUE; /* enable range download */
  1820. }
  1821. *in_connect = conn; /* return this instead! */
  1822. infof(data, "Re-using existing connection! (#%d)\n", conn->connectindex);
  1823. }
  1824. else {
  1825. /*
  1826. * This is a brand new connection, so let's store it in the connection
  1827. * cache of ours!
  1828. */
  1829. ConnectionStore(data, conn);
  1830. }
  1831. /*************************************************************
  1832. * Set timeout if that is being used
  1833. *************************************************************/
  1834. if(data->set.timeout || data->set.connecttimeout) {
  1835. /*************************************************************
  1836. * Set signal handler to catch SIGALRM
  1837. * Store the old value to be able to set it back later!
  1838. *************************************************************/
  1839. #ifdef HAVE_SIGACTION
  1840. struct sigaction sigact;
  1841. sigaction(SIGALRM, NULL, &sigact);
  1842. keep_sigact = sigact;
  1843. keep_copysig = TRUE; /* yes, we have a copy */
  1844. sigact.sa_handler = alarmfunc;
  1845. #ifdef SA_RESTART
  1846. /* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
  1847. sigact.sa_flags &= ~SA_RESTART;
  1848. #endif
  1849. /* now set the new struct */
  1850. sigaction(SIGALRM, &sigact, NULL);
  1851. #else
  1852. /* no sigaction(), revert to the much lamer signal() */
  1853. #ifdef HAVE_SIGNAL
  1854. keep_sigact = signal(SIGALRM, alarmfunc);
  1855. #endif
  1856. #endif
  1857. /* We set the timeout on the name resolving phase first, separately from
  1858. * the download/upload part to allow a maximum time on everything. This is
  1859. * a signal-based timeout, why it won't work and shouldn't be used in
  1860. * multi-threaded environments. */
  1861. #ifdef HAVE_ALARM
  1862. /* alarm() makes a signal get sent when the timeout fires off, and that
  1863. will abort system calls */
  1864. prev_alarm = alarm(data->set.connecttimeout?
  1865. data->set.connecttimeout:
  1866. data->set.timeout);
  1867. /* We can expect the conn->created time to be "now", as that was just
  1868. recently set in the beginning of this function and nothing slow
  1869. has been done since then until now. */
  1870. #endif
  1871. }
  1872. /*************************************************************
  1873. * Resolve the name of the server or proxy
  1874. *************************************************************/
  1875. if(!data->change.proxy) {
  1876. /* If not connecting via a proxy, extract the port from the URL, if it is
  1877. * there, thus overriding any defaults that might have been set above. */
  1878. conn->port = conn->remote_port; /* it is the same port */
  1879. /* Resolve target host right on */
  1880. if(!conn->hostaddr) {
  1881. /* it might already be set if reusing a connection */
  1882. conn->hostaddr = Curl_resolv(data, conn->name, conn->port,
  1883. &conn->hostent_buf);
  1884. }
  1885. if(!conn->hostaddr) {
  1886. failf(data, "Couldn't resolve host '%s'", conn->name);
  1887. result = CURLE_COULDNT_RESOLVE_HOST;
  1888. /* don't return yet, we need to clean up the timeout first */
  1889. }
  1890. }
  1891. else if(!conn->hostaddr) {
  1892. /* This is a proxy that hasn't been resolved yet. It may be resolved
  1893. if we're reusing an existing connection. */
  1894. /* resolve proxy */
  1895. /* it might already be set if reusing a connection */
  1896. conn->hostaddr = Curl_resolv(data, conn->proxyhost, conn->port,
  1897. &conn->hostent_buf);
  1898. if(!conn->hostaddr) {
  1899. failf(data, "Couldn't resolve proxy '%s'", conn->proxyhost);
  1900. result = CURLE_COULDNT_RESOLVE_PROXY;
  1901. /* don't return yet, we need to clean up the timeout first */
  1902. }
  1903. }
  1904. Curl_pgrsTime(data, TIMER_NAMELOOKUP);
  1905. #ifdef HAVE_ALARM
  1906. if(data->set.timeout || data->set.connecttimeout) {
  1907. #ifdef HAVE_SIGACTION
  1908. if(keep_copysig) {
  1909. /* we got a struct as it looked before, now put that one back nice
  1910. and clean */
  1911. sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
  1912. }
  1913. #else
  1914. #ifdef HAVE_SIGNAL
  1915. /* restore the previous SIGALRM handler */
  1916. signal(SIGALRM, keep_sigact);
  1917. #endif
  1918. #endif
  1919. /* switch back the alarm() to either zero or to what it was before minus
  1920. the time we spent until now! */
  1921. if(prev_alarm) {
  1922. /* there was an alarm() set before us, now put it back */
  1923. long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
  1924. long alarm_set;
  1925. /* the alarm period is counted in even number of seconds */
  1926. alarm_set = prev_alarm - elapsed_ms/1000;
  1927. if(alarm_set<=0) {
  1928. /* if it turned negative, we should fire off a SIGALRM here, but we
  1929. won't, and zero would be to switch it off so we never set it to
  1930. less than 1! */
  1931. alarm(1);
  1932. result = CURLE_OPERATION_TIMEOUTED;
  1933. failf(data, "Previous alarm fired off!");
  1934. }
  1935. else
  1936. alarm(alarm_set);
  1937. }
  1938. else
  1939. alarm(0); /* just shut it off */
  1940. }
  1941. #endif
  1942. if(result)
  1943. return result;
  1944. /*************************************************************
  1945. * Proxy authentication
  1946. *************************************************************/
  1947. if(conn->bits.proxy_user_passwd) {
  1948. char *authorization;
  1949. snprintf(data->state.buffer, BUFSIZE, "%s:%s",
  1950. data->state.proxyuser, data->state.proxypasswd);
  1951. if(Curl_base64_encode(data->state.buffer, strlen(data->state.buffer),
  1952. &authorization) >= 0) {
  1953. if(conn->allocptr.proxyuserpwd)
  1954. free(conn->allocptr.proxyuserpwd);
  1955. conn->allocptr.proxyuserpwd =
  1956. aprintf("Proxy-authorization: Basic %s\015\012", authorization);
  1957. free(authorization);
  1958. }
  1959. }
  1960. /*************************************************************
  1961. * Send user-agent to HTTP proxies even if the target protocol
  1962. * isn't HTTP.
  1963. *************************************************************/
  1964. if((conn->protocol&PROT_HTTP) || data->change.proxy) {
  1965. if(data->set.useragent) {
  1966. if(conn->allocptr.uagent)
  1967. free(conn->allocptr.uagent);
  1968. conn->allocptr.uagent =
  1969. aprintf("User-Agent: %s\015\012", data->set.useragent);
  1970. }
  1971. }
  1972. if(-1 == conn->firstsocket) {
  1973. /* Connect only if not already connected! */
  1974. result = ConnectPlease(conn);
  1975. if(CURLE_OK != result)
  1976. return result;
  1977. if(conn->curl_connect) {
  1978. /* is there a connect() procedure? */
  1979. /* set start time here for timeout purposes in the
  1980. * connect procedure, it is later set again for the
  1981. * progress meter purpose */
  1982. conn->now = Curl_tvnow();
  1983. /* Call the protocol-specific connect function */
  1984. result = conn->curl_connect(conn);
  1985. if(result != CURLE_OK)
  1986. return result; /* pass back errors */
  1987. }
  1988. }
  1989. Curl_pgrsTime(data, TIMER_CONNECT); /* we're connected */
  1990. conn->now = Curl_tvnow(); /* time this *after* the connect is done */
  1991. conn->bytecount = 0;
  1992. conn->headerbytecount = 0;
  1993. /* Figure out the ip-number and display the first host name it shows: */
  1994. #ifdef ENABLE_IPV6
  1995. {
  1996. char hbuf[NI_MAXHOST];
  1997. #ifdef NI_WITHSCOPEID
  1998. const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
  1999. #else
  2000. const int niflags = NI_NUMERICHOST;
  2001. #endif
  2002. struct addrinfo *ai = conn->serv_addr;
  2003. if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf), NULL, 0,
  2004. niflags)) {
  2005. snprintf(hbuf, sizeof(hbuf), "?");
  2006. }
  2007. if (ai->ai_canonname) {
  2008. infof(data, "Connected to %s (%s) port %d\n", ai->ai_canonname, hbuf,
  2009. conn->port);
  2010. } else {
  2011. infof(data, "Connected to %s port %d\n", hbuf, conn->port);
  2012. }
  2013. }
  2014. #else
  2015. {
  2016. struct in_addr in;
  2017. (void) memcpy(&in.s_addr, &conn->serv_addr.sin_addr, sizeof (in.s_addr));
  2018. infof(data, "Connected to %s (%s)\n", conn->hostaddr->h_name,
  2019. #if defined(HAVE_INET_NTOA_R)
  2020. inet_ntoa_r(in, ntoa_buf, sizeof(ntoa_buf))
  2021. #else
  2022. inet_ntoa(in)
  2023. #endif
  2024. );
  2025. }
  2026. #endif
  2027. #ifdef __EMX__
  2028. /* 20000330 mgs
  2029. * the check is quite a hack...
  2030. * we're calling _fsetmode to fix the problem with fwrite converting newline
  2031. * characters (you get mangled text files, and corrupted binary files when
  2032. * you download to stdout and redirect it to a file). */
  2033. if ((data->set.out)->_handle == NULL) {
  2034. _fsetmode(stdout, "b");
  2035. }
  2036. #endif
  2037. return CURLE_OK;
  2038. }
  2039. CURLcode Curl_connect(struct SessionHandle *data,
  2040. struct connectdata **in_connect)
  2041. {
  2042. CURLcode code;
  2043. struct connectdata *conn;
  2044. /* call the stuff that needs to be called */
  2045. code = CreateConnection(data, in_connect);
  2046. if(CURLE_OK != code) {
  2047. /* We're not allowed to return failure with memory left allocated
  2048. in the connectdata struct, free those here */
  2049. conn = (struct connectdata *)*in_connect;
  2050. if(conn) {
  2051. Curl_disconnect(conn); /* close the connection */
  2052. *in_connect = NULL; /* return a NULL */
  2053. }
  2054. }
  2055. return code;
  2056. }
  2057. CURLcode Curl_done(struct connectdata *conn)
  2058. {
  2059. struct SessionHandle *data=conn->data;
  2060. CURLcode result;
  2061. /* cleanups done even if the connection is re-used */
  2062. if(conn->bits.rangestringalloc) {
  2063. free(conn->range);
  2064. conn->bits.rangestringalloc = FALSE;
  2065. }
  2066. /* Cleanup possible redirect junk */
  2067. if(conn->newurl) {
  2068. free(conn->newurl);
  2069. conn->newurl = NULL;
  2070. }
  2071. /* this calls the protocol-specific function pointer previously set */
  2072. if(conn->curl_done)
  2073. result = conn->curl_done(conn);
  2074. else
  2075. result = CURLE_OK;
  2076. Curl_pgrsDone(conn); /* done with the operation */
  2077. /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
  2078. forced us to close this no matter what we think.
  2079. if conn->bits.close is TRUE, it means that the connection should be
  2080. closed in spite of all our efforts to be nice, due to protocol
  2081. restrictions in our or the server's end */
  2082. if(data->set.reuse_forbid ||
  2083. ((CURLE_OK == result) && conn->bits.close))
  2084. result = Curl_disconnect(conn); /* close the connection */
  2085. else
  2086. infof(data, "Connection #%d left intact\n", conn->connectindex);
  2087. return result;
  2088. }
  2089. CURLcode Curl_do(struct connectdata **connp)
  2090. {
  2091. CURLcode result=CURLE_OK;
  2092. struct connectdata *conn = *connp;
  2093. struct SessionHandle *data=conn->data;
  2094. if(conn->curl_do) {
  2095. /* generic protocol-specific function pointer set in curl_connect() */
  2096. result = conn->curl_do(conn);
  2097. /* This was formerly done in transfer.c, but we better do it here */
  2098. if((CURLE_WRITE_ERROR == result) && conn->bits.reuse) {
  2099. /* This was a re-use of a connection and we got a write error in the
  2100. * DO-phase. Then we DISCONNECT this connection and have another attempt
  2101. * to CONNECT and then DO again! The retry cannot possibly find another
  2102. * connection to re-use, since we only keep one possible connection for
  2103. * each. */
  2104. infof(data, "Re-used connection seems dead, get a new one\n");
  2105. conn->bits.close = TRUE; /* enforce close of this connetion */
  2106. result = Curl_done(conn); /* we are so done with this */
  2107. if(CURLE_OK == result) {
  2108. /* Now, redo the connect and get a new connection */
  2109. result = Curl_connect(data, connp);
  2110. if(CURLE_OK == result)
  2111. /* ... finally back to actually retry the DO phase */
  2112. result = conn->curl_do(*connp);
  2113. }
  2114. }
  2115. }
  2116. return result;
  2117. }
  2118. /*
  2119. * local variables:
  2120. * eval: (load-file "../curl-mode.el")
  2121. * end:
  2122. * vim600: fdm=marker
  2123. * vim: et sw=2 ts=2 sts=2 tw=78
  2124. */