sws.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "server_setup.h"
  23. /* sws.c: simple (silly?) web server
  24. This code was originally graciously donated to the project by Juergen
  25. Wilke. Thanks a bunch!
  26. */
  27. #ifdef HAVE_SIGNAL_H
  28. #include <signal.h>
  29. #endif
  30. #ifdef HAVE_NETINET_IN_H
  31. #include <netinet/in.h>
  32. #endif
  33. #ifdef HAVE_NETINET_IN6_H
  34. #include <netinet/in6.h>
  35. #endif
  36. #ifdef HAVE_ARPA_INET_H
  37. #include <arpa/inet.h>
  38. #endif
  39. #ifdef HAVE_NETDB_H
  40. #include <netdb.h>
  41. #endif
  42. #ifdef HAVE_NETINET_TCP_H
  43. #include <netinet/tcp.h> /* for TCP_NODELAY */
  44. #endif
  45. #define ENABLE_CURLX_PRINTF
  46. /* make the curlx header define all printf() functions to use the curlx_*
  47. versions instead */
  48. #include "curlx.h" /* from the private lib dir */
  49. #include "getpart.h"
  50. #include "inet_pton.h"
  51. #include "util.h"
  52. #include "server_sockaddr.h"
  53. /* include memdebug.h last */
  54. #include "memdebug.h"
  55. #ifdef USE_WINSOCK
  56. #undef EINTR
  57. #define EINTR 4 /* errno.h value */
  58. #undef EAGAIN
  59. #define EAGAIN 11 /* errno.h value */
  60. #undef ERANGE
  61. #define ERANGE 34 /* errno.h value */
  62. #endif
  63. static enum {
  64. socket_domain_inet = AF_INET
  65. #ifdef ENABLE_IPV6
  66. , socket_domain_inet6 = AF_INET6
  67. #endif
  68. #ifdef USE_UNIX_SOCKETS
  69. , socket_domain_unix = AF_UNIX
  70. #endif
  71. } socket_domain = AF_INET;
  72. static bool use_gopher = FALSE;
  73. static int serverlogslocked = 0;
  74. static bool is_proxy = FALSE;
  75. #define REQBUFSIZ 150000
  76. #define REQBUFSIZ_TXT "149999"
  77. static long prevtestno = -1; /* previous test number we served */
  78. static long prevpartno = -1; /* previous part number we served */
  79. static bool prevbounce = FALSE; /* instructs the server to increase the part
  80. number for a test in case the identical
  81. testno+partno request shows up again */
  82. #define RCMD_NORMALREQ 0 /* default request, use the tests file normally */
  83. #define RCMD_IDLE 1 /* told to sit idle */
  84. #define RCMD_STREAM 2 /* told to stream */
  85. struct httprequest {
  86. char reqbuf[REQBUFSIZ]; /* buffer area for the incoming request */
  87. bool connect_request; /* if a CONNECT */
  88. unsigned short connect_port; /* the port number CONNECT used */
  89. size_t checkindex; /* where to start checking of the request */
  90. size_t offset; /* size of the incoming request */
  91. long testno; /* test number found in the request */
  92. long partno; /* part number found in the request */
  93. bool open; /* keep connection open info, as found in the request */
  94. bool auth_req; /* authentication required, don't wait for body unless
  95. there's an Authorization header */
  96. bool auth; /* Authorization header present in the incoming request */
  97. size_t cl; /* Content-Length of the incoming request */
  98. bool digest; /* Authorization digest header found */
  99. bool ntlm; /* Authorization ntlm header found */
  100. int writedelay; /* if non-zero, delay this number of seconds between
  101. writes in the response */
  102. int skip; /* if non-zero, the server is instructed to not read this
  103. many bytes from a PUT/POST request. Ie the client sends N
  104. bytes said in Content-Length, but the server only reads N
  105. - skip bytes. */
  106. int rcmd; /* doing a special command, see defines above */
  107. int prot_version; /* HTTP version * 10 */
  108. int callcount; /* times ProcessRequest() gets called */
  109. bool skipall; /* skip all incoming data */
  110. bool noexpect; /* refuse Expect: (don't read the body) */
  111. bool connmon; /* monitor the state of the connection, log disconnects */
  112. bool upgrade; /* test case allows upgrade to http2 */
  113. bool upgrade_request; /* upgrade request found and allowed */
  114. bool close; /* similar to swsclose in response: close connection after
  115. response is sent */
  116. int done_processing;
  117. };
  118. #define MAX_SOCKETS 1024
  119. static curl_socket_t all_sockets[MAX_SOCKETS];
  120. static size_t num_sockets = 0;
  121. static int ProcessRequest(struct httprequest *req);
  122. static void storerequest(const char *reqbuf, size_t totalsize);
  123. #define DEFAULT_PORT 8999
  124. #ifndef DEFAULT_LOGFILE
  125. #define DEFAULT_LOGFILE "log/sws.log"
  126. #endif
  127. const char *serverlogfile = DEFAULT_LOGFILE;
  128. #define SWSVERSION "curl test suite HTTP server/0.1"
  129. #define REQUEST_DUMP "log/server.input"
  130. #define RESPONSE_DUMP "log/server.response"
  131. /* when told to run as proxy, we store the logs in different files so that
  132. they can co-exist with the same program running as a "server" */
  133. #define REQUEST_PROXY_DUMP "log/proxy.input"
  134. #define RESPONSE_PROXY_DUMP "log/proxy.response"
  135. /* file in which additional instructions may be found */
  136. #define DEFAULT_CMDFILE "log/ftpserver.cmd"
  137. const char *cmdfile = DEFAULT_CMDFILE;
  138. /* very-big-path support */
  139. #define MAXDOCNAMELEN 140000
  140. #define MAXDOCNAMELEN_TXT "139999"
  141. #define REQUEST_KEYWORD_SIZE 256
  142. #define REQUEST_KEYWORD_SIZE_TXT "255"
  143. #define CMD_AUTH_REQUIRED "auth_required"
  144. /* 'idle' means that it will accept the request fine but never respond
  145. any data. Just keep the connection alive. */
  146. #define CMD_IDLE "idle"
  147. /* 'stream' means to send a never-ending stream of data */
  148. #define CMD_STREAM "stream"
  149. /* 'connection-monitor' will output when a server/proxy connection gets
  150. disconnected as for some cases it is important that it gets done at the
  151. proper point - like with NTLM */
  152. #define CMD_CONNECTIONMONITOR "connection-monitor"
  153. /* upgrade to http2 */
  154. #define CMD_UPGRADE "upgrade"
  155. /* close connection */
  156. #define CMD_SWSCLOSE "swsclose"
  157. /* deny Expect: requests */
  158. #define CMD_NOEXPECT "no-expect"
  159. #define END_OF_HEADERS "\r\n\r\n"
  160. enum {
  161. DOCNUMBER_NOTHING = -4,
  162. DOCNUMBER_QUIT = -3,
  163. DOCNUMBER_WERULEZ = -2,
  164. DOCNUMBER_404 = -1
  165. };
  166. static const char *end_of_headers = END_OF_HEADERS;
  167. /* sent as reply to a QUIT */
  168. static const char *docquit =
  169. "HTTP/1.1 200 Goodbye" END_OF_HEADERS;
  170. /* send back this on 404 file not found */
  171. static const char *doc404 = "HTTP/1.1 404 Not Found\r\n"
  172. "Server: " SWSVERSION "\r\n"
  173. "Connection: close\r\n"
  174. "Content-Type: text/html"
  175. END_OF_HEADERS
  176. "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
  177. "<HTML><HEAD>\n"
  178. "<TITLE>404 Not Found</TITLE>\n"
  179. "</HEAD><BODY>\n"
  180. "<H1>Not Found</H1>\n"
  181. "The requested URL was not found on this server.\n"
  182. "<P><HR><ADDRESS>" SWSVERSION "</ADDRESS>\n" "</BODY></HTML>\n";
  183. /* work around for handling trailing headers */
  184. static int already_recv_zeroed_chunk = FALSE;
  185. /* returns true if the current socket is an IP one */
  186. static bool socket_domain_is_ip(void)
  187. {
  188. switch(socket_domain) {
  189. case AF_INET:
  190. #ifdef ENABLE_IPV6
  191. case AF_INET6:
  192. #endif
  193. return true;
  194. default:
  195. /* case AF_UNIX: */
  196. return false;
  197. }
  198. }
  199. /* parse the file on disk that might have a test number for us */
  200. static int parse_cmdfile(struct httprequest *req)
  201. {
  202. int testnum = DOCNUMBER_NOTHING;
  203. char buf[256];
  204. FILE *f = fopen(cmdfile, FOPEN_READTEXT);
  205. if(f) {
  206. while(fgets(buf, sizeof(buf), f)) {
  207. if(1 == sscanf(buf, "Testnum %d", &testnum)) {
  208. logmsg("[%s] cmdfile says testnum %d", cmdfile, testnum);
  209. req->testno = testnum;
  210. }
  211. }
  212. fclose(f);
  213. }
  214. return 0;
  215. }
  216. /* based on the testno, parse the correct server commands */
  217. static int parse_servercmd(struct httprequest *req)
  218. {
  219. FILE *stream;
  220. int error;
  221. stream = test2fopen(req->testno);
  222. req->close = FALSE;
  223. req->connmon = FALSE;
  224. if(!stream) {
  225. error = errno;
  226. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  227. logmsg(" Couldn't open test file %ld", req->testno);
  228. req->open = FALSE; /* closes connection */
  229. return 1; /* done */
  230. }
  231. else {
  232. char *orgcmd = NULL;
  233. char *cmd = NULL;
  234. size_t cmdsize = 0;
  235. int num = 0;
  236. /* get the custom server control "commands" */
  237. error = getpart(&orgcmd, &cmdsize, "reply", "servercmd", stream);
  238. fclose(stream);
  239. if(error) {
  240. logmsg("getpart() failed with error: %d", error);
  241. req->open = FALSE; /* closes connection */
  242. return 1; /* done */
  243. }
  244. cmd = orgcmd;
  245. while(cmd && cmdsize) {
  246. char *check;
  247. if(!strncmp(CMD_AUTH_REQUIRED, cmd, strlen(CMD_AUTH_REQUIRED))) {
  248. logmsg("instructed to require authorization header");
  249. req->auth_req = TRUE;
  250. }
  251. else if(!strncmp(CMD_IDLE, cmd, strlen(CMD_IDLE))) {
  252. logmsg("instructed to idle");
  253. req->rcmd = RCMD_IDLE;
  254. req->open = TRUE;
  255. }
  256. else if(!strncmp(CMD_STREAM, cmd, strlen(CMD_STREAM))) {
  257. logmsg("instructed to stream");
  258. req->rcmd = RCMD_STREAM;
  259. }
  260. else if(!strncmp(CMD_CONNECTIONMONITOR, cmd,
  261. strlen(CMD_CONNECTIONMONITOR))) {
  262. logmsg("enabled connection monitoring");
  263. req->connmon = TRUE;
  264. }
  265. else if(!strncmp(CMD_UPGRADE, cmd, strlen(CMD_UPGRADE))) {
  266. logmsg("enabled upgrade to http2");
  267. req->upgrade = TRUE;
  268. }
  269. else if(!strncmp(CMD_SWSCLOSE, cmd, strlen(CMD_SWSCLOSE))) {
  270. logmsg("swsclose: close this connection after response");
  271. req->close = TRUE;
  272. }
  273. else if(1 == sscanf(cmd, "skip: %d", &num)) {
  274. logmsg("instructed to skip this number of bytes %d", num);
  275. req->skip = num;
  276. }
  277. else if(!strncmp(CMD_NOEXPECT, cmd, strlen(CMD_NOEXPECT))) {
  278. logmsg("instructed to reject Expect: 100-continue");
  279. req->noexpect = TRUE;
  280. }
  281. else if(1 == sscanf(cmd, "writedelay: %d", &num)) {
  282. logmsg("instructed to delay %d secs between packets", num);
  283. req->writedelay = num;
  284. }
  285. else {
  286. logmsg("Unknown <servercmd> instruction found: %s", cmd);
  287. }
  288. /* try to deal with CRLF or just LF */
  289. check = strchr(cmd, '\r');
  290. if(!check)
  291. check = strchr(cmd, '\n');
  292. if(check) {
  293. /* get to the letter following the newline */
  294. while((*check == '\r') || (*check == '\n'))
  295. check++;
  296. if(!*check)
  297. /* if we reached a zero, get out */
  298. break;
  299. cmd = check;
  300. }
  301. else
  302. break;
  303. }
  304. free(orgcmd);
  305. }
  306. return 0; /* OK! */
  307. }
  308. static int ProcessRequest(struct httprequest *req)
  309. {
  310. char *line = &req->reqbuf[req->checkindex];
  311. bool chunked = FALSE;
  312. static char request[REQUEST_KEYWORD_SIZE];
  313. static char doc[MAXDOCNAMELEN];
  314. char logbuf[456];
  315. int prot_major, prot_minor;
  316. char *end = strstr(line, end_of_headers);
  317. req->callcount++;
  318. logmsg("Process %d bytes request%s", req->offset,
  319. req->callcount > 1?" [CONTINUED]":"");
  320. /* try to figure out the request characteristics as soon as possible, but
  321. only once! */
  322. if(use_gopher &&
  323. (req->testno == DOCNUMBER_NOTHING) &&
  324. !strncmp("/verifiedserver", line, 15)) {
  325. logmsg("Are-we-friendly question received");
  326. req->testno = DOCNUMBER_WERULEZ;
  327. return 1; /* done */
  328. }
  329. else if((req->testno == DOCNUMBER_NOTHING) &&
  330. sscanf(line,
  331. "%" REQUEST_KEYWORD_SIZE_TXT"s %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
  332. request,
  333. doc,
  334. &prot_major,
  335. &prot_minor) == 4) {
  336. char *ptr;
  337. req->prot_version = prot_major*10 + prot_minor;
  338. /* find the last slash */
  339. ptr = strrchr(doc, '/');
  340. /* get the number after it */
  341. if(ptr) {
  342. if((strlen(doc) + strlen(request)) < 400)
  343. msnprintf(logbuf, sizeof(logbuf), "Got request: %s %s HTTP/%d.%d",
  344. request, doc, prot_major, prot_minor);
  345. else
  346. msnprintf(logbuf, sizeof(logbuf), "Got a *HUGE* request HTTP/%d.%d",
  347. prot_major, prot_minor);
  348. logmsg("%s", logbuf);
  349. if(!strncmp("/verifiedserver", ptr, 15)) {
  350. logmsg("Are-we-friendly question received");
  351. req->testno = DOCNUMBER_WERULEZ;
  352. return 1; /* done */
  353. }
  354. if(!strncmp("/quit", ptr, 5)) {
  355. logmsg("Request-to-quit received");
  356. req->testno = DOCNUMBER_QUIT;
  357. return 1; /* done */
  358. }
  359. ptr++; /* skip the slash */
  360. /* skip all non-numericals following the slash */
  361. while(*ptr && !ISDIGIT(*ptr))
  362. ptr++;
  363. req->testno = strtol(ptr, &ptr, 10);
  364. if(req->testno > 10000) {
  365. req->partno = req->testno % 10000;
  366. req->testno /= 10000;
  367. }
  368. else
  369. req->partno = 0;
  370. if(req->testno) {
  371. msnprintf(logbuf, sizeof(logbuf), "Requested test number %ld part %ld",
  372. req->testno, req->partno);
  373. logmsg("%s", logbuf);
  374. }
  375. else {
  376. logmsg("No test number");
  377. req->testno = DOCNUMBER_NOTHING;
  378. }
  379. }
  380. if(req->testno == DOCNUMBER_NOTHING) {
  381. /* didn't find any in the first scan, try alternative test case
  382. number placements */
  383. if(sscanf(req->reqbuf, "CONNECT %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
  384. doc, &prot_major, &prot_minor) == 3) {
  385. char *portp = NULL;
  386. msnprintf(logbuf, sizeof(logbuf),
  387. "Received a CONNECT %s HTTP/%d.%d request",
  388. doc, prot_major, prot_minor);
  389. logmsg("%s", logbuf);
  390. req->connect_request = TRUE;
  391. if(req->prot_version == 10)
  392. req->open = FALSE; /* HTTP 1.0 closes connection by default */
  393. if(doc[0] == '[') {
  394. char *p = &doc[1];
  395. unsigned long part = 0;
  396. /* scan through the hexgroups and store the value of the last group
  397. in the 'part' variable and use as test case number!! */
  398. while(*p && (ISXDIGIT(*p) || (*p == ':') || (*p == '.'))) {
  399. char *endp;
  400. part = strtoul(p, &endp, 16);
  401. if(ISXDIGIT(*p))
  402. p = endp;
  403. else
  404. p++;
  405. }
  406. if(*p != ']')
  407. logmsg("Invalid CONNECT IPv6 address format");
  408. else if(*(p + 1) != ':')
  409. logmsg("Invalid CONNECT IPv6 port format");
  410. else
  411. portp = p + 1;
  412. req->testno = part;
  413. }
  414. else
  415. portp = strchr(doc, ':');
  416. if(portp && (*(portp + 1) != '\0') && ISDIGIT(*(portp + 1))) {
  417. unsigned long ulnum = strtoul(portp + 1, NULL, 10);
  418. if(!ulnum || (ulnum > 65535UL))
  419. logmsg("Invalid CONNECT port received");
  420. else
  421. req->connect_port = curlx_ultous(ulnum);
  422. }
  423. logmsg("Port number: %d, test case number: %ld",
  424. req->connect_port, req->testno);
  425. }
  426. }
  427. if(req->testno == DOCNUMBER_NOTHING) {
  428. /* Still no test case number. Try to get the number off the last dot
  429. instead, IE we consider the TLD to be the test number. Test 123 can
  430. then be written as "example.com.123". */
  431. /* find the last dot */
  432. ptr = strrchr(doc, '.');
  433. /* get the number after it */
  434. if(ptr) {
  435. long num;
  436. ptr++; /* skip the dot */
  437. num = strtol(ptr, &ptr, 10);
  438. if(num) {
  439. req->testno = num;
  440. if(req->testno > 10000) {
  441. req->partno = req->testno % 10000;
  442. req->testno /= 10000;
  443. logmsg("found test %d in requested host name", req->testno);
  444. }
  445. else
  446. req->partno = 0;
  447. }
  448. if(req->testno != DOCNUMBER_NOTHING) {
  449. logmsg("Requested test number %ld part %ld (from host name)",
  450. req->testno, req->partno);
  451. }
  452. }
  453. }
  454. if(req->testno == DOCNUMBER_NOTHING)
  455. /* might get the test number */
  456. parse_cmdfile(req);
  457. if(req->testno == DOCNUMBER_NOTHING) {
  458. logmsg("Did not find test number in PATH");
  459. req->testno = DOCNUMBER_404;
  460. }
  461. else
  462. parse_servercmd(req);
  463. }
  464. else if((req->offset >= 3) && (req->testno == DOCNUMBER_NOTHING)) {
  465. logmsg("** Unusual request. Starts with %02x %02x %02x (%c%c%c)",
  466. line[0], line[1], line[2], line[0], line[1], line[2]);
  467. }
  468. if(!end) {
  469. /* we don't have a complete request yet! */
  470. logmsg("request not complete yet");
  471. return 0; /* not complete yet */
  472. }
  473. logmsg("- request found to be complete (%d)", req->testno);
  474. if(req->testno == DOCNUMBER_NOTHING) {
  475. /* check for a Testno: header with the test case number */
  476. char *testno = strstr(line, "\nTestno: ");
  477. if(testno) {
  478. req->testno = strtol(&testno[9], NULL, 10);
  479. logmsg("Found test number %d in Testno: header!", req->testno);
  480. }
  481. else {
  482. logmsg("No Testno: header");
  483. }
  484. }
  485. /* find and parse <servercmd> for this test */
  486. parse_servercmd(req);
  487. if(use_gopher) {
  488. /* when using gopher we cannot check the request until the entire
  489. thing has been received */
  490. char *ptr;
  491. /* find the last slash in the line */
  492. ptr = strrchr(line, '/');
  493. if(ptr) {
  494. ptr++; /* skip the slash */
  495. /* skip all non-numericals following the slash */
  496. while(*ptr && !ISDIGIT(*ptr))
  497. ptr++;
  498. req->testno = strtol(ptr, &ptr, 10);
  499. if(req->testno > 10000) {
  500. req->partno = req->testno % 10000;
  501. req->testno /= 10000;
  502. }
  503. else
  504. req->partno = 0;
  505. msnprintf(logbuf, sizeof(logbuf),
  506. "Requested GOPHER test number %ld part %ld",
  507. req->testno, req->partno);
  508. logmsg("%s", logbuf);
  509. }
  510. }
  511. /* **** Persistence ****
  512. *
  513. * If the request is a HTTP/1.0 one, we close the connection unconditionally
  514. * when we're done.
  515. *
  516. * If the request is a HTTP/1.1 one, we MUST check for a "Connection:"
  517. * header that might say "close". If it does, we close a connection when
  518. * this request is processed. Otherwise, we keep the connection alive for X
  519. * seconds.
  520. */
  521. do {
  522. if(got_exit_signal)
  523. return 1; /* done */
  524. if((req->cl == 0) && strncasecompare("Content-Length:", line, 15)) {
  525. /* If we don't ignore content-length, we read it and we read the whole
  526. request including the body before we return. If we've been told to
  527. ignore the content-length, we will return as soon as all headers
  528. have been received */
  529. char *endptr;
  530. char *ptr = line + 15;
  531. unsigned long clen = 0;
  532. while(*ptr && ISSPACE(*ptr))
  533. ptr++;
  534. endptr = ptr;
  535. errno = 0;
  536. clen = strtoul(ptr, &endptr, 10);
  537. if((ptr == endptr) || !ISSPACE(*endptr) || (ERANGE == errno)) {
  538. /* this assumes that a zero Content-Length is valid */
  539. logmsg("Found invalid Content-Length: (%s) in the request", ptr);
  540. req->open = FALSE; /* closes connection */
  541. return 1; /* done */
  542. }
  543. if(req->skipall)
  544. req->cl = 0;
  545. else
  546. req->cl = clen - req->skip;
  547. logmsg("Found Content-Length: %lu in the request", clen);
  548. if(req->skip)
  549. logmsg("... but will abort after %zu bytes", req->cl);
  550. }
  551. else if(strncasecompare("Transfer-Encoding: chunked", line,
  552. strlen("Transfer-Encoding: chunked"))) {
  553. /* chunked data coming in */
  554. chunked = TRUE;
  555. }
  556. else if(req->noexpect &&
  557. strncasecompare("Expect: 100-continue", line,
  558. strlen("Expect: 100-continue"))) {
  559. if(req->cl)
  560. req->cl = 0;
  561. req->skipall = TRUE;
  562. logmsg("Found Expect: 100-continue, ignore body");
  563. }
  564. if(chunked) {
  565. if(strstr(req->reqbuf, "\r\n0\r\n\r\n")) {
  566. /* end of chunks reached */
  567. return 1; /* done */
  568. }
  569. else if(strstr(req->reqbuf, "\r\n0\r\n")) {
  570. char *last_crlf_char = strstr(req->reqbuf, "\r\n\r\n");
  571. while(TRUE) {
  572. if(!strstr(last_crlf_char + 4, "\r\n\r\n"))
  573. break;
  574. last_crlf_char = strstr(last_crlf_char + 4, "\r\n\r\n");
  575. }
  576. if(last_crlf_char &&
  577. last_crlf_char > strstr(req->reqbuf, "\r\n0\r\n"))
  578. return 1;
  579. already_recv_zeroed_chunk = TRUE;
  580. return 0;
  581. }
  582. else if(already_recv_zeroed_chunk && strstr(req->reqbuf, "\r\n\r\n"))
  583. return 1;
  584. else
  585. return 0; /* not done */
  586. }
  587. line = strchr(line, '\n');
  588. if(line)
  589. line++;
  590. } while(line);
  591. if(!req->auth && strstr(req->reqbuf, "Authorization:")) {
  592. req->auth = TRUE; /* Authorization: header present! */
  593. if(req->auth_req)
  594. logmsg("Authorization header found, as required");
  595. }
  596. if(strstr(req->reqbuf, "Authorization: Negotiate")) {
  597. /* Negotiate iterations */
  598. static long prev_testno = -1;
  599. static long prev_partno = -1;
  600. logmsg("Negotiate: prev_testno: %d, prev_partno: %d",
  601. prev_testno, prev_partno);
  602. if(req->testno != prev_testno) {
  603. prev_testno = req->testno;
  604. prev_partno = req->partno;
  605. }
  606. prev_partno += 1;
  607. req->partno = prev_partno;
  608. }
  609. else if(!req->digest && strstr(req->reqbuf, "Authorization: Digest")) {
  610. /* If the client is passing this Digest-header, we set the part number
  611. to 1000. Not only to spice up the complexity of this, but to make
  612. Digest stuff to work in the test suite. */
  613. req->partno += 1000;
  614. req->digest = TRUE; /* header found */
  615. logmsg("Received Digest request, sending back data %ld", req->partno);
  616. }
  617. else if(!req->ntlm &&
  618. strstr(req->reqbuf, "Authorization: NTLM TlRMTVNTUAAD")) {
  619. /* If the client is passing this type-3 NTLM header */
  620. req->partno += 1002;
  621. req->ntlm = TRUE; /* NTLM found */
  622. logmsg("Received NTLM type-3, sending back data %ld", req->partno);
  623. if(req->cl) {
  624. logmsg(" Expecting %zu POSTed bytes", req->cl);
  625. }
  626. }
  627. else if(!req->ntlm &&
  628. strstr(req->reqbuf, "Authorization: NTLM TlRMTVNTUAAB")) {
  629. /* If the client is passing this type-1 NTLM header */
  630. req->partno += 1001;
  631. req->ntlm = TRUE; /* NTLM found */
  632. logmsg("Received NTLM type-1, sending back data %ld", req->partno);
  633. }
  634. else if((req->partno >= 1000) &&
  635. strstr(req->reqbuf, "Authorization: Basic")) {
  636. /* If the client is passing this Basic-header and the part number is
  637. already >=1000, we add 1 to the part number. This allows simple Basic
  638. authentication negotiation to work in the test suite. */
  639. req->partno += 1;
  640. logmsg("Received Basic request, sending back data %ld", req->partno);
  641. }
  642. if(strstr(req->reqbuf, "Connection: close"))
  643. req->open = FALSE; /* close connection after this request */
  644. if(req->open &&
  645. req->prot_version >= 11 &&
  646. end &&
  647. req->reqbuf + req->offset > end + strlen(end_of_headers) &&
  648. !req->cl &&
  649. (!strncmp(req->reqbuf, "GET", strlen("GET")) ||
  650. !strncmp(req->reqbuf, "HEAD", strlen("HEAD")))) {
  651. /* If we have a persistent connection, HTTP version >= 1.1
  652. and GET/HEAD request, enable pipelining. */
  653. req->checkindex = (end - req->reqbuf) + strlen(end_of_headers);
  654. }
  655. /* If authentication is required and no auth was provided, end now. This
  656. makes the server NOT wait for PUT/POST data and you can then make the
  657. test case send a rejection before any such data has been sent. Test case
  658. 154 uses this.*/
  659. if(req->auth_req && !req->auth) {
  660. logmsg("Return early due to auth requested by none provided");
  661. return 1; /* done */
  662. }
  663. if(req->upgrade && strstr(req->reqbuf, "Upgrade:")) {
  664. /* we allow upgrade and there was one! */
  665. logmsg("Found Upgrade: in request and allows it");
  666. req->upgrade_request = TRUE;
  667. }
  668. if(req->cl > 0) {
  669. if(req->cl <= req->offset - (end - req->reqbuf) - strlen(end_of_headers))
  670. return 1; /* done */
  671. else
  672. return 0; /* not complete yet */
  673. }
  674. return 1; /* done */
  675. }
  676. /* store the entire request in a file */
  677. static void storerequest(const char *reqbuf, size_t totalsize)
  678. {
  679. int res;
  680. int error = 0;
  681. size_t written;
  682. size_t writeleft;
  683. FILE *dump;
  684. const char *dumpfile = is_proxy?REQUEST_PROXY_DUMP:REQUEST_DUMP;
  685. if(reqbuf == NULL)
  686. return;
  687. if(totalsize == 0)
  688. return;
  689. do {
  690. dump = fopen(dumpfile, "ab");
  691. } while((dump == NULL) && ((error = errno) == EINTR));
  692. if(dump == NULL) {
  693. logmsg("[2] Error opening file %s error: %d %s",
  694. dumpfile, error, strerror(error));
  695. logmsg("Failed to write request input ");
  696. return;
  697. }
  698. writeleft = totalsize;
  699. do {
  700. written = fwrite(&reqbuf[totalsize-writeleft],
  701. 1, writeleft, dump);
  702. if(got_exit_signal)
  703. goto storerequest_cleanup;
  704. if(written > 0)
  705. writeleft -= written;
  706. } while((writeleft > 0) && ((error = errno) == EINTR));
  707. if(writeleft == 0)
  708. logmsg("Wrote request (%zu bytes) input to %s", totalsize, dumpfile);
  709. else if(writeleft > 0) {
  710. logmsg("Error writing file %s error: %d %s",
  711. dumpfile, error, strerror(error));
  712. logmsg("Wrote only (%zu bytes) of (%zu bytes) request input to %s",
  713. totalsize-writeleft, totalsize, dumpfile);
  714. }
  715. storerequest_cleanup:
  716. do {
  717. res = fclose(dump);
  718. } while(res && ((error = errno) == EINTR));
  719. if(res)
  720. logmsg("Error closing file %s error: %d %s",
  721. dumpfile, error, strerror(error));
  722. }
  723. static void init_httprequest(struct httprequest *req)
  724. {
  725. req->checkindex = 0;
  726. req->offset = 0;
  727. req->testno = DOCNUMBER_NOTHING;
  728. req->partno = 0;
  729. req->connect_request = FALSE;
  730. req->open = TRUE;
  731. req->auth_req = FALSE;
  732. req->auth = FALSE;
  733. req->cl = 0;
  734. req->digest = FALSE;
  735. req->ntlm = FALSE;
  736. req->skip = 0;
  737. req->skipall = FALSE;
  738. req->noexpect = FALSE;
  739. req->writedelay = 0;
  740. req->rcmd = RCMD_NORMALREQ;
  741. req->prot_version = 0;
  742. req->callcount = 0;
  743. req->connect_port = 0;
  744. req->done_processing = 0;
  745. req->upgrade = 0;
  746. req->upgrade_request = 0;
  747. }
  748. /* returns 1 if the connection should be serviced again immediately, 0 if there
  749. is no data waiting, or < 0 if it should be closed */
  750. static int get_request(curl_socket_t sock, struct httprequest *req)
  751. {
  752. int fail = 0;
  753. char *reqbuf = req->reqbuf;
  754. ssize_t got = 0;
  755. int overflow = 0;
  756. if(req->offset >= REQBUFSIZ-1) {
  757. /* buffer is already full; do nothing */
  758. overflow = 1;
  759. }
  760. else {
  761. if(req->skip)
  762. /* we are instructed to not read the entire thing, so we make sure to
  763. only read what we're supposed to and NOT read the enire thing the
  764. client wants to send! */
  765. got = sread(sock, reqbuf + req->offset, req->cl);
  766. else
  767. got = sread(sock, reqbuf + req->offset, REQBUFSIZ-1 - req->offset);
  768. if(got_exit_signal)
  769. return -1;
  770. if(got == 0) {
  771. logmsg("Connection closed by client");
  772. fail = 1;
  773. }
  774. else if(got < 0) {
  775. int error = SOCKERRNO;
  776. if(EAGAIN == error || EWOULDBLOCK == error) {
  777. /* nothing to read at the moment */
  778. return 0;
  779. }
  780. logmsg("recv() returned error: (%d) %s", error, strerror(error));
  781. fail = 1;
  782. }
  783. if(fail) {
  784. /* dump the request received so far to the external file */
  785. reqbuf[req->offset] = '\0';
  786. storerequest(reqbuf, req->offset);
  787. return -1;
  788. }
  789. logmsg("Read %zd bytes", got);
  790. req->offset += (size_t)got;
  791. reqbuf[req->offset] = '\0';
  792. req->done_processing = ProcessRequest(req);
  793. if(got_exit_signal)
  794. return -1;
  795. }
  796. if(overflow || (req->offset == REQBUFSIZ-1 && got > 0)) {
  797. logmsg("Request would overflow buffer, closing connection");
  798. /* dump request received so far to external file anyway */
  799. reqbuf[REQBUFSIZ-1] = '\0';
  800. fail = 1;
  801. }
  802. else if(req->offset > REQBUFSIZ-1) {
  803. logmsg("Request buffer overflow, closing connection");
  804. /* dump request received so far to external file anyway */
  805. reqbuf[REQBUFSIZ-1] = '\0';
  806. fail = 1;
  807. }
  808. else
  809. reqbuf[req->offset] = '\0';
  810. /* at the end of a request dump it to an external file */
  811. if(fail || req->done_processing)
  812. storerequest(reqbuf, req->offset);
  813. if(got_exit_signal)
  814. return -1;
  815. return fail ? -1 : 1;
  816. }
  817. /* returns -1 on failure */
  818. static int send_doc(curl_socket_t sock, struct httprequest *req)
  819. {
  820. ssize_t written;
  821. size_t count;
  822. const char *buffer;
  823. char *ptr = NULL;
  824. FILE *stream;
  825. char *cmd = NULL;
  826. size_t cmdsize = 0;
  827. FILE *dump;
  828. bool persistent = TRUE;
  829. bool sendfailure = FALSE;
  830. size_t responsesize;
  831. int error = 0;
  832. int res;
  833. const char *responsedump = is_proxy?RESPONSE_PROXY_DUMP:RESPONSE_DUMP;
  834. static char weare[256];
  835. switch(req->rcmd) {
  836. default:
  837. case RCMD_NORMALREQ:
  838. break; /* continue with business as usual */
  839. case RCMD_STREAM:
  840. #define STREAMTHIS "a string to stream 01234567890\n"
  841. count = strlen(STREAMTHIS);
  842. for(;;) {
  843. written = swrite(sock, STREAMTHIS, count);
  844. if(got_exit_signal)
  845. return -1;
  846. if(written != (ssize_t)count) {
  847. logmsg("Stopped streaming");
  848. break;
  849. }
  850. }
  851. return -1;
  852. case RCMD_IDLE:
  853. /* Do nothing. Sit idle. Pretend it rains. */
  854. return 0;
  855. }
  856. req->open = FALSE;
  857. if(req->testno < 0) {
  858. size_t msglen;
  859. char msgbuf[64];
  860. switch(req->testno) {
  861. case DOCNUMBER_QUIT:
  862. logmsg("Replying to QUIT");
  863. buffer = docquit;
  864. break;
  865. case DOCNUMBER_WERULEZ:
  866. /* we got a "friends?" question, reply back that we sure are */
  867. logmsg("Identifying ourselves as friends");
  868. msnprintf(msgbuf, sizeof(msgbuf), "WE ROOLZ: %ld\r\n", (long)getpid());
  869. msglen = strlen(msgbuf);
  870. if(use_gopher)
  871. msnprintf(weare, sizeof(weare), "%s", msgbuf);
  872. else
  873. msnprintf(weare, sizeof(weare),
  874. "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
  875. msglen, msgbuf);
  876. buffer = weare;
  877. break;
  878. case DOCNUMBER_404:
  879. default:
  880. logmsg("Replying to with a 404");
  881. buffer = doc404;
  882. break;
  883. }
  884. count = strlen(buffer);
  885. }
  886. else {
  887. char partbuf[80];
  888. /* select the <data> tag for "normal" requests and the <connect> one
  889. for CONNECT requests (within the <reply> section) */
  890. const char *section = req->connect_request?"connect":"data";
  891. if(req->partno)
  892. msnprintf(partbuf, sizeof(partbuf), "%s%ld", section, req->partno);
  893. else
  894. msnprintf(partbuf, sizeof(partbuf), "%s", section);
  895. logmsg("Send response test%ld section <%s>", req->testno, partbuf);
  896. stream = test2fopen(req->testno);
  897. if(!stream) {
  898. error = errno;
  899. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  900. return 0;
  901. }
  902. else {
  903. error = getpart(&ptr, &count, "reply", partbuf, stream);
  904. fclose(stream);
  905. if(error) {
  906. logmsg("getpart() failed with error: %d", error);
  907. return 0;
  908. }
  909. buffer = ptr;
  910. }
  911. if(got_exit_signal) {
  912. free(ptr);
  913. return -1;
  914. }
  915. /* re-open the same file again */
  916. stream = test2fopen(req->testno);
  917. if(!stream) {
  918. error = errno;
  919. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  920. free(ptr);
  921. return 0;
  922. }
  923. else {
  924. /* get the custom server control "commands" */
  925. error = getpart(&cmd, &cmdsize, "reply", "postcmd", stream);
  926. fclose(stream);
  927. if(error) {
  928. logmsg("getpart() failed with error: %d", error);
  929. free(ptr);
  930. return 0;
  931. }
  932. }
  933. }
  934. if(got_exit_signal) {
  935. free(ptr);
  936. free(cmd);
  937. return -1;
  938. }
  939. /* If the word 'swsclose' is present anywhere in the reply chunk, the
  940. connection will be closed after the data has been sent to the requesting
  941. client... */
  942. if(strstr(buffer, "swsclose") || !count || req->close) {
  943. persistent = FALSE;
  944. logmsg("connection close instruction \"swsclose\" found in response");
  945. }
  946. if(strstr(buffer, "swsbounce")) {
  947. prevbounce = TRUE;
  948. logmsg("enable \"swsbounce\" in the next request");
  949. }
  950. else
  951. prevbounce = FALSE;
  952. dump = fopen(responsedump, "ab");
  953. if(!dump) {
  954. error = errno;
  955. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  956. logmsg(" [5] Error opening file: %s", responsedump);
  957. free(ptr);
  958. free(cmd);
  959. return -1;
  960. }
  961. responsesize = count;
  962. do {
  963. /* Ok, we send no more than N bytes at a time, just to make sure that
  964. larger chunks are split up so that the client will need to do multiple
  965. recv() calls to get it and thus we exercise that code better */
  966. size_t num = count;
  967. if(num > 20)
  968. num = 20;
  969. retry:
  970. written = swrite(sock, buffer, num);
  971. if(written < 0) {
  972. if((EWOULDBLOCK == SOCKERRNO) || (EAGAIN == SOCKERRNO)) {
  973. wait_ms(10);
  974. goto retry;
  975. }
  976. sendfailure = TRUE;
  977. break;
  978. }
  979. /* write to file as well */
  980. fwrite(buffer, 1, (size_t)written, dump);
  981. count -= written;
  982. buffer += written;
  983. if(req->writedelay) {
  984. int quarters = req->writedelay * 4;
  985. logmsg("Pausing %d seconds", req->writedelay);
  986. while((quarters > 0) && !got_exit_signal) {
  987. quarters--;
  988. wait_ms(250);
  989. }
  990. }
  991. } while((count > 0) && !got_exit_signal);
  992. do {
  993. res = fclose(dump);
  994. } while(res && ((error = errno) == EINTR));
  995. if(res)
  996. logmsg("Error closing file %s error: %d %s",
  997. responsedump, error, strerror(error));
  998. if(got_exit_signal) {
  999. free(ptr);
  1000. free(cmd);
  1001. return -1;
  1002. }
  1003. if(sendfailure) {
  1004. logmsg("Sending response failed. Only (%zu bytes) of (%zu bytes) "
  1005. "were sent",
  1006. responsesize-count, responsesize);
  1007. prevtestno = req->testno;
  1008. prevpartno = req->partno;
  1009. free(ptr);
  1010. free(cmd);
  1011. return -1;
  1012. }
  1013. logmsg("Response sent (%zu bytes) and written to %s",
  1014. responsesize, responsedump);
  1015. free(ptr);
  1016. if(cmdsize > 0) {
  1017. char command[32];
  1018. int quarters;
  1019. int num;
  1020. ptr = cmd;
  1021. do {
  1022. if(2 == sscanf(ptr, "%31s %d", command, &num)) {
  1023. if(!strcmp("wait", command)) {
  1024. logmsg("Told to sleep for %d seconds", num);
  1025. quarters = num * 4;
  1026. while((quarters > 0) && !got_exit_signal) {
  1027. quarters--;
  1028. res = wait_ms(250);
  1029. if(res) {
  1030. /* should not happen */
  1031. error = errno;
  1032. logmsg("wait_ms() failed with error: (%d) %s",
  1033. error, strerror(error));
  1034. break;
  1035. }
  1036. }
  1037. if(!quarters)
  1038. logmsg("Continuing after sleeping %d seconds", num);
  1039. }
  1040. else
  1041. logmsg("Unknown command in reply command section");
  1042. }
  1043. ptr = strchr(ptr, '\n');
  1044. if(ptr)
  1045. ptr++;
  1046. else
  1047. ptr = NULL;
  1048. } while(ptr && *ptr);
  1049. }
  1050. free(cmd);
  1051. req->open = use_gopher?FALSE:persistent;
  1052. prevtestno = req->testno;
  1053. prevpartno = req->partno;
  1054. return 0;
  1055. }
  1056. static curl_socket_t connect_to(const char *ipaddr, unsigned short port)
  1057. {
  1058. srvr_sockaddr_union_t serveraddr;
  1059. curl_socket_t serverfd;
  1060. int error;
  1061. int rc = 0;
  1062. const char *op_br = "";
  1063. const char *cl_br = "";
  1064. #ifdef ENABLE_IPV6
  1065. if(socket_domain == AF_INET6) {
  1066. op_br = "[";
  1067. cl_br = "]";
  1068. }
  1069. #endif
  1070. if(!ipaddr)
  1071. return CURL_SOCKET_BAD;
  1072. logmsg("about to connect to %s%s%s:%hu",
  1073. op_br, ipaddr, cl_br, port);
  1074. serverfd = socket(socket_domain, SOCK_STREAM, 0);
  1075. if(CURL_SOCKET_BAD == serverfd) {
  1076. error = SOCKERRNO;
  1077. logmsg("Error creating socket for server connection: (%d) %s",
  1078. error, strerror(error));
  1079. return CURL_SOCKET_BAD;
  1080. }
  1081. #ifdef TCP_NODELAY
  1082. if(socket_domain_is_ip()) {
  1083. /* Disable the Nagle algorithm */
  1084. curl_socklen_t flag = 1;
  1085. if(0 != setsockopt(serverfd, IPPROTO_TCP, TCP_NODELAY,
  1086. (void *)&flag, sizeof(flag)))
  1087. logmsg("====> TCP_NODELAY for server connection failed");
  1088. }
  1089. #endif
  1090. switch(socket_domain) {
  1091. case AF_INET:
  1092. memset(&serveraddr.sa4, 0, sizeof(serveraddr.sa4));
  1093. serveraddr.sa4.sin_family = AF_INET;
  1094. serveraddr.sa4.sin_port = htons(port);
  1095. if(Curl_inet_pton(AF_INET, ipaddr, &serveraddr.sa4.sin_addr) < 1) {
  1096. logmsg("Error inet_pton failed AF_INET conversion of '%s'", ipaddr);
  1097. sclose(serverfd);
  1098. return CURL_SOCKET_BAD;
  1099. }
  1100. rc = connect(serverfd, &serveraddr.sa, sizeof(serveraddr.sa4));
  1101. break;
  1102. #ifdef ENABLE_IPV6
  1103. case AF_INET6:
  1104. memset(&serveraddr.sa6, 0, sizeof(serveraddr.sa6));
  1105. serveraddr.sa6.sin6_family = AF_INET6;
  1106. serveraddr.sa6.sin6_port = htons(port);
  1107. if(Curl_inet_pton(AF_INET6, ipaddr, &serveraddr.sa6.sin6_addr) < 1) {
  1108. logmsg("Error inet_pton failed AF_INET6 conversion of '%s'", ipaddr);
  1109. sclose(serverfd);
  1110. return CURL_SOCKET_BAD;
  1111. }
  1112. rc = connect(serverfd, &serveraddr.sa, sizeof(serveraddr.sa6));
  1113. break;
  1114. #endif /* ENABLE_IPV6 */
  1115. #ifdef USE_UNIX_SOCKETS
  1116. case AF_UNIX:
  1117. logmsg("Proxying through Unix socket is not (yet?) supported.");
  1118. return CURL_SOCKET_BAD;
  1119. #endif /* USE_UNIX_SOCKETS */
  1120. }
  1121. if(got_exit_signal) {
  1122. sclose(serverfd);
  1123. return CURL_SOCKET_BAD;
  1124. }
  1125. if(rc) {
  1126. error = SOCKERRNO;
  1127. logmsg("Error connecting to server port %hu: (%d) %s",
  1128. port, error, strerror(error));
  1129. sclose(serverfd);
  1130. return CURL_SOCKET_BAD;
  1131. }
  1132. logmsg("connected fine to %s%s%s:%hu, now tunnel",
  1133. op_br, ipaddr, cl_br, port);
  1134. return serverfd;
  1135. }
  1136. /*
  1137. * A CONNECT has been received, a CONNECT response has been sent.
  1138. *
  1139. * This function needs to connect to the server, and then pass data between
  1140. * the client and the server back and forth until the connection is closed by
  1141. * either end.
  1142. *
  1143. * When doing FTP through a CONNECT proxy, we expect that the data connection
  1144. * will be setup while the first connect is still being kept up. Therefore we
  1145. * must accept a new connection and deal with it appropriately.
  1146. */
  1147. #define data_or_ctrl(x) ((x)?"DATA":"CTRL")
  1148. #define CTRL 0
  1149. #define DATA 1
  1150. static void http_connect(curl_socket_t *infdp,
  1151. curl_socket_t rootfd,
  1152. const char *ipaddr,
  1153. unsigned short ipport)
  1154. {
  1155. curl_socket_t serverfd[2] = {CURL_SOCKET_BAD, CURL_SOCKET_BAD};
  1156. curl_socket_t clientfd[2] = {CURL_SOCKET_BAD, CURL_SOCKET_BAD};
  1157. ssize_t toc[2] = {0, 0}; /* number of bytes to client */
  1158. ssize_t tos[2] = {0, 0}; /* number of bytes to server */
  1159. char readclient[2][256];
  1160. char readserver[2][256];
  1161. bool poll_client_rd[2] = { TRUE, TRUE };
  1162. bool poll_server_rd[2] = { TRUE, TRUE };
  1163. bool poll_client_wr[2] = { TRUE, TRUE };
  1164. bool poll_server_wr[2] = { TRUE, TRUE };
  1165. bool primary = FALSE;
  1166. bool secondary = FALSE;
  1167. int max_tunnel_idx; /* CTRL or DATA */
  1168. int loop;
  1169. int i;
  1170. int timeout_count = 0;
  1171. /* primary tunnel client endpoint already connected */
  1172. clientfd[CTRL] = *infdp;
  1173. /* Sleep here to make sure the client reads CONNECT response's
  1174. 'end of headers' separate from the server data that follows.
  1175. This is done to prevent triggering libcurl known bug #39. */
  1176. for(loop = 2; (loop > 0) && !got_exit_signal; loop--)
  1177. wait_ms(250);
  1178. if(got_exit_signal)
  1179. goto http_connect_cleanup;
  1180. serverfd[CTRL] = connect_to(ipaddr, ipport);
  1181. if(serverfd[CTRL] == CURL_SOCKET_BAD)
  1182. goto http_connect_cleanup;
  1183. /* Primary tunnel socket endpoints are now connected. Tunnel data back and
  1184. forth over the primary tunnel until client or server breaks the primary
  1185. tunnel, simultaneously allowing establishment, operation and teardown of
  1186. a secondary tunnel that may be used for passive FTP data connection. */
  1187. max_tunnel_idx = CTRL;
  1188. primary = TRUE;
  1189. while(!got_exit_signal) {
  1190. fd_set input;
  1191. fd_set output;
  1192. struct timeval timeout = {1, 0}; /* 1000 ms */
  1193. ssize_t rc;
  1194. curl_socket_t maxfd = (curl_socket_t)-1;
  1195. FD_ZERO(&input);
  1196. FD_ZERO(&output);
  1197. if((clientfd[DATA] == CURL_SOCKET_BAD) &&
  1198. (serverfd[DATA] == CURL_SOCKET_BAD) &&
  1199. poll_client_rd[CTRL] && poll_client_wr[CTRL] &&
  1200. poll_server_rd[CTRL] && poll_server_wr[CTRL]) {
  1201. /* listener socket is monitored to allow client to establish
  1202. secondary tunnel only when this tunnel is not established
  1203. and primary one is fully operational */
  1204. FD_SET(rootfd, &input);
  1205. maxfd = rootfd;
  1206. }
  1207. /* set tunnel sockets to wait for */
  1208. for(i = 0; i <= max_tunnel_idx; i++) {
  1209. /* client side socket monitoring */
  1210. if(clientfd[i] != CURL_SOCKET_BAD) {
  1211. if(poll_client_rd[i]) {
  1212. /* unless told not to do so, monitor readability */
  1213. FD_SET(clientfd[i], &input);
  1214. if(clientfd[i] > maxfd)
  1215. maxfd = clientfd[i];
  1216. }
  1217. if(poll_client_wr[i] && toc[i]) {
  1218. /* unless told not to do so, monitor writability
  1219. if there is data ready to be sent to client */
  1220. FD_SET(clientfd[i], &output);
  1221. if(clientfd[i] > maxfd)
  1222. maxfd = clientfd[i];
  1223. }
  1224. }
  1225. /* server side socket monitoring */
  1226. if(serverfd[i] != CURL_SOCKET_BAD) {
  1227. if(poll_server_rd[i]) {
  1228. /* unless told not to do so, monitor readability */
  1229. FD_SET(serverfd[i], &input);
  1230. if(serverfd[i] > maxfd)
  1231. maxfd = serverfd[i];
  1232. }
  1233. if(poll_server_wr[i] && tos[i]) {
  1234. /* unless told not to do so, monitor writability
  1235. if there is data ready to be sent to server */
  1236. FD_SET(serverfd[i], &output);
  1237. if(serverfd[i] > maxfd)
  1238. maxfd = serverfd[i];
  1239. }
  1240. }
  1241. }
  1242. if(got_exit_signal)
  1243. break;
  1244. do {
  1245. rc = select((int)maxfd + 1, &input, &output, NULL, &timeout);
  1246. } while(rc < 0 && errno == EINTR && !got_exit_signal);
  1247. if(got_exit_signal)
  1248. break;
  1249. if(rc > 0) {
  1250. /* socket action */
  1251. bool tcp_fin_wr = FALSE;
  1252. timeout_count = 0;
  1253. /* ---------------------------------------------------------- */
  1254. /* passive mode FTP may establish a secondary tunnel */
  1255. if((clientfd[DATA] == CURL_SOCKET_BAD) &&
  1256. (serverfd[DATA] == CURL_SOCKET_BAD) && FD_ISSET(rootfd, &input)) {
  1257. /* a new connection on listener socket (most likely from client) */
  1258. curl_socket_t datafd = accept(rootfd, NULL, NULL);
  1259. if(datafd != CURL_SOCKET_BAD) {
  1260. struct httprequest req2;
  1261. int err = 0;
  1262. memset(&req2, 0, sizeof(req2));
  1263. logmsg("====> Client connect DATA");
  1264. #ifdef TCP_NODELAY
  1265. if(socket_domain_is_ip()) {
  1266. /* Disable the Nagle algorithm */
  1267. curl_socklen_t flag = 1;
  1268. if(0 != setsockopt(datafd, IPPROTO_TCP, TCP_NODELAY,
  1269. (void *)&flag, sizeof(flag)))
  1270. logmsg("====> TCP_NODELAY for client DATA connection failed");
  1271. }
  1272. #endif
  1273. init_httprequest(&req2);
  1274. while(!req2.done_processing) {
  1275. err = get_request(datafd, &req2);
  1276. if(err < 0) {
  1277. /* this socket must be closed, done or not */
  1278. break;
  1279. }
  1280. }
  1281. /* skip this and close the socket if err < 0 */
  1282. if(err >= 0) {
  1283. err = send_doc(datafd, &req2);
  1284. if(!err && req2.connect_request) {
  1285. /* sleep to prevent triggering libcurl known bug #39. */
  1286. for(loop = 2; (loop > 0) && !got_exit_signal; loop--)
  1287. wait_ms(250);
  1288. if(!got_exit_signal) {
  1289. /* connect to the server */
  1290. serverfd[DATA] = connect_to(ipaddr, req2.connect_port);
  1291. if(serverfd[DATA] != CURL_SOCKET_BAD) {
  1292. /* secondary tunnel established, now we have two
  1293. connections */
  1294. poll_client_rd[DATA] = TRUE;
  1295. poll_client_wr[DATA] = TRUE;
  1296. poll_server_rd[DATA] = TRUE;
  1297. poll_server_wr[DATA] = TRUE;
  1298. max_tunnel_idx = DATA;
  1299. secondary = TRUE;
  1300. toc[DATA] = 0;
  1301. tos[DATA] = 0;
  1302. clientfd[DATA] = datafd;
  1303. datafd = CURL_SOCKET_BAD;
  1304. }
  1305. }
  1306. }
  1307. }
  1308. if(datafd != CURL_SOCKET_BAD) {
  1309. /* secondary tunnel not established */
  1310. shutdown(datafd, SHUT_RDWR);
  1311. sclose(datafd);
  1312. }
  1313. }
  1314. if(got_exit_signal)
  1315. break;
  1316. }
  1317. /* ---------------------------------------------------------- */
  1318. /* react to tunnel endpoint readable/writable notifications */
  1319. for(i = 0; i <= max_tunnel_idx; i++) {
  1320. size_t len;
  1321. if(clientfd[i] != CURL_SOCKET_BAD) {
  1322. len = sizeof(readclient[i]) - tos[i];
  1323. if(len && FD_ISSET(clientfd[i], &input)) {
  1324. /* read from client */
  1325. rc = sread(clientfd[i], &readclient[i][tos[i]], len);
  1326. if(rc <= 0) {
  1327. logmsg("[%s] got %zd, STOP READING client", data_or_ctrl(i), rc);
  1328. shutdown(clientfd[i], SHUT_RD);
  1329. poll_client_rd[i] = FALSE;
  1330. }
  1331. else {
  1332. logmsg("[%s] READ %zd bytes from client", data_or_ctrl(i), rc);
  1333. logmsg("[%s] READ \"%s\"", data_or_ctrl(i),
  1334. data_to_hex(&readclient[i][tos[i]], rc));
  1335. tos[i] += rc;
  1336. }
  1337. }
  1338. }
  1339. if(serverfd[i] != CURL_SOCKET_BAD) {
  1340. len = sizeof(readserver[i])-toc[i];
  1341. if(len && FD_ISSET(serverfd[i], &input)) {
  1342. /* read from server */
  1343. rc = sread(serverfd[i], &readserver[i][toc[i]], len);
  1344. if(rc <= 0) {
  1345. logmsg("[%s] got %zd, STOP READING server", data_or_ctrl(i), rc);
  1346. shutdown(serverfd[i], SHUT_RD);
  1347. poll_server_rd[i] = FALSE;
  1348. }
  1349. else {
  1350. logmsg("[%s] READ %zd bytes from server", data_or_ctrl(i), rc);
  1351. logmsg("[%s] READ \"%s\"", data_or_ctrl(i),
  1352. data_to_hex(&readserver[i][toc[i]], rc));
  1353. toc[i] += rc;
  1354. }
  1355. }
  1356. }
  1357. if(clientfd[i] != CURL_SOCKET_BAD) {
  1358. if(toc[i] && FD_ISSET(clientfd[i], &output)) {
  1359. /* write to client */
  1360. rc = swrite(clientfd[i], readserver[i], toc[i]);
  1361. if(rc <= 0) {
  1362. logmsg("[%s] got %zd, STOP WRITING client", data_or_ctrl(i), rc);
  1363. shutdown(clientfd[i], SHUT_WR);
  1364. poll_client_wr[i] = FALSE;
  1365. tcp_fin_wr = TRUE;
  1366. }
  1367. else {
  1368. logmsg("[%s] SENT %zd bytes to client", data_or_ctrl(i), rc);
  1369. logmsg("[%s] SENT \"%s\"", data_or_ctrl(i),
  1370. data_to_hex(readserver[i], rc));
  1371. if(toc[i] - rc)
  1372. memmove(&readserver[i][0], &readserver[i][rc], toc[i]-rc);
  1373. toc[i] -= rc;
  1374. }
  1375. }
  1376. }
  1377. if(serverfd[i] != CURL_SOCKET_BAD) {
  1378. if(tos[i] && FD_ISSET(serverfd[i], &output)) {
  1379. /* write to server */
  1380. rc = swrite(serverfd[i], readclient[i], tos[i]);
  1381. if(rc <= 0) {
  1382. logmsg("[%s] got %zd, STOP WRITING server", data_or_ctrl(i), rc);
  1383. shutdown(serverfd[i], SHUT_WR);
  1384. poll_server_wr[i] = FALSE;
  1385. tcp_fin_wr = TRUE;
  1386. }
  1387. else {
  1388. logmsg("[%s] SENT %zd bytes to server", data_or_ctrl(i), rc);
  1389. logmsg("[%s] SENT \"%s\"", data_or_ctrl(i),
  1390. data_to_hex(readclient[i], rc));
  1391. if(tos[i] - rc)
  1392. memmove(&readclient[i][0], &readclient[i][rc], tos[i]-rc);
  1393. tos[i] -= rc;
  1394. }
  1395. }
  1396. }
  1397. }
  1398. if(got_exit_signal)
  1399. break;
  1400. /* ---------------------------------------------------------- */
  1401. /* endpoint read/write disabling, endpoint closing and tunnel teardown */
  1402. for(i = 0; i <= max_tunnel_idx; i++) {
  1403. for(loop = 2; loop > 0; loop--) {
  1404. /* loop twice to satisfy condition interdependencies without
  1405. having to await select timeout or another socket event */
  1406. if(clientfd[i] != CURL_SOCKET_BAD) {
  1407. if(poll_client_rd[i] && !poll_server_wr[i]) {
  1408. logmsg("[%s] DISABLED READING client", data_or_ctrl(i));
  1409. shutdown(clientfd[i], SHUT_RD);
  1410. poll_client_rd[i] = FALSE;
  1411. }
  1412. if(poll_client_wr[i] && !poll_server_rd[i] && !toc[i]) {
  1413. logmsg("[%s] DISABLED WRITING client", data_or_ctrl(i));
  1414. shutdown(clientfd[i], SHUT_WR);
  1415. poll_client_wr[i] = FALSE;
  1416. tcp_fin_wr = TRUE;
  1417. }
  1418. }
  1419. if(serverfd[i] != CURL_SOCKET_BAD) {
  1420. if(poll_server_rd[i] && !poll_client_wr[i]) {
  1421. logmsg("[%s] DISABLED READING server", data_or_ctrl(i));
  1422. shutdown(serverfd[i], SHUT_RD);
  1423. poll_server_rd[i] = FALSE;
  1424. }
  1425. if(poll_server_wr[i] && !poll_client_rd[i] && !tos[i]) {
  1426. logmsg("[%s] DISABLED WRITING server", data_or_ctrl(i));
  1427. shutdown(serverfd[i], SHUT_WR);
  1428. poll_server_wr[i] = FALSE;
  1429. tcp_fin_wr = TRUE;
  1430. }
  1431. }
  1432. }
  1433. }
  1434. if(tcp_fin_wr)
  1435. /* allow kernel to place FIN bit packet on the wire */
  1436. wait_ms(250);
  1437. /* socket clearing */
  1438. for(i = 0; i <= max_tunnel_idx; i++) {
  1439. for(loop = 2; loop > 0; loop--) {
  1440. if(clientfd[i] != CURL_SOCKET_BAD) {
  1441. if(!poll_client_wr[i] && !poll_client_rd[i]) {
  1442. logmsg("[%s] CLOSING client socket", data_or_ctrl(i));
  1443. sclose(clientfd[i]);
  1444. clientfd[i] = CURL_SOCKET_BAD;
  1445. if(serverfd[i] == CURL_SOCKET_BAD) {
  1446. logmsg("[%s] ENDING", data_or_ctrl(i));
  1447. if(i == DATA)
  1448. secondary = FALSE;
  1449. else
  1450. primary = FALSE;
  1451. }
  1452. }
  1453. }
  1454. if(serverfd[i] != CURL_SOCKET_BAD) {
  1455. if(!poll_server_wr[i] && !poll_server_rd[i]) {
  1456. logmsg("[%s] CLOSING server socket", data_or_ctrl(i));
  1457. sclose(serverfd[i]);
  1458. serverfd[i] = CURL_SOCKET_BAD;
  1459. if(clientfd[i] == CURL_SOCKET_BAD) {
  1460. logmsg("[%s] ENDING", data_or_ctrl(i));
  1461. if(i == DATA)
  1462. secondary = FALSE;
  1463. else
  1464. primary = FALSE;
  1465. }
  1466. }
  1467. }
  1468. }
  1469. }
  1470. /* ---------------------------------------------------------- */
  1471. max_tunnel_idx = secondary ? DATA : CTRL;
  1472. if(!primary)
  1473. /* exit loop upon primary tunnel teardown */
  1474. break;
  1475. } /* (rc > 0) */
  1476. else {
  1477. timeout_count++;
  1478. if(timeout_count > 5) {
  1479. logmsg("CONNECT proxy timeout after %d idle seconds!", timeout_count);
  1480. break;
  1481. }
  1482. }
  1483. }
  1484. http_connect_cleanup:
  1485. for(i = DATA; i >= CTRL; i--) {
  1486. if(serverfd[i] != CURL_SOCKET_BAD) {
  1487. logmsg("[%s] CLOSING server socket (cleanup)", data_or_ctrl(i));
  1488. shutdown(serverfd[i], SHUT_RDWR);
  1489. sclose(serverfd[i]);
  1490. }
  1491. if(clientfd[i] != CURL_SOCKET_BAD) {
  1492. logmsg("[%s] CLOSING client socket (cleanup)", data_or_ctrl(i));
  1493. shutdown(clientfd[i], SHUT_RDWR);
  1494. sclose(clientfd[i]);
  1495. }
  1496. if((serverfd[i] != CURL_SOCKET_BAD) ||
  1497. (clientfd[i] != CURL_SOCKET_BAD)) {
  1498. logmsg("[%s] ABORTING", data_or_ctrl(i));
  1499. }
  1500. }
  1501. *infdp = CURL_SOCKET_BAD;
  1502. }
  1503. static void http2(struct httprequest *req)
  1504. {
  1505. (void)req;
  1506. logmsg("switched to http2");
  1507. /* left to implement */
  1508. }
  1509. /* returns a socket handle, or 0 if there are no more waiting sockets,
  1510. or < 0 if there was an error */
  1511. static curl_socket_t accept_connection(curl_socket_t sock)
  1512. {
  1513. curl_socket_t msgsock = CURL_SOCKET_BAD;
  1514. int error;
  1515. int flag = 1;
  1516. if(MAX_SOCKETS == num_sockets) {
  1517. logmsg("Too many open sockets!");
  1518. return CURL_SOCKET_BAD;
  1519. }
  1520. msgsock = accept(sock, NULL, NULL);
  1521. if(got_exit_signal) {
  1522. if(CURL_SOCKET_BAD != msgsock)
  1523. sclose(msgsock);
  1524. return CURL_SOCKET_BAD;
  1525. }
  1526. if(CURL_SOCKET_BAD == msgsock) {
  1527. error = SOCKERRNO;
  1528. if(EAGAIN == error || EWOULDBLOCK == error) {
  1529. /* nothing to accept */
  1530. return 0;
  1531. }
  1532. logmsg("MAJOR ERROR: accept() failed with error: (%d) %s",
  1533. error, strerror(error));
  1534. return CURL_SOCKET_BAD;
  1535. }
  1536. if(0 != curlx_nonblock(msgsock, TRUE)) {
  1537. error = SOCKERRNO;
  1538. logmsg("curlx_nonblock failed with error: (%d) %s",
  1539. error, strerror(error));
  1540. sclose(msgsock);
  1541. return CURL_SOCKET_BAD;
  1542. }
  1543. if(0 != setsockopt(msgsock, SOL_SOCKET, SO_KEEPALIVE,
  1544. (void *)&flag, sizeof(flag))) {
  1545. error = SOCKERRNO;
  1546. logmsg("setsockopt(SO_KEEPALIVE) failed with error: (%d) %s",
  1547. error, strerror(error));
  1548. sclose(msgsock);
  1549. return CURL_SOCKET_BAD;
  1550. }
  1551. /*
  1552. ** As soon as this server accepts a connection from the test harness it
  1553. ** must set the server logs advisor read lock to indicate that server
  1554. ** logs should not be read until this lock is removed by this server.
  1555. */
  1556. if(!serverlogslocked)
  1557. set_advisor_read_lock(SERVERLOGS_LOCK);
  1558. serverlogslocked += 1;
  1559. logmsg("====> Client connect");
  1560. all_sockets[num_sockets] = msgsock;
  1561. num_sockets += 1;
  1562. #ifdef TCP_NODELAY
  1563. if(socket_domain_is_ip()) {
  1564. /*
  1565. * Disable the Nagle algorithm to make it easier to send out a large
  1566. * response in many small segments to torture the clients more.
  1567. */
  1568. if(0 != setsockopt(msgsock, IPPROTO_TCP, TCP_NODELAY,
  1569. (void *)&flag, sizeof(flag)))
  1570. logmsg("====> TCP_NODELAY failed");
  1571. }
  1572. #endif
  1573. return msgsock;
  1574. }
  1575. /* returns 1 if the connection should be serviced again immediately, 0 if there
  1576. is no data waiting, or < 0 if it should be closed */
  1577. static int service_connection(curl_socket_t msgsock, struct httprequest *req,
  1578. curl_socket_t listensock,
  1579. const char *connecthost)
  1580. {
  1581. if(got_exit_signal)
  1582. return -1;
  1583. while(!req->done_processing) {
  1584. int rc = get_request(msgsock, req);
  1585. if(rc <= 0) {
  1586. /* Nothing further to read now, possibly because the socket was closed */
  1587. return rc;
  1588. }
  1589. }
  1590. if(prevbounce) {
  1591. /* bounce treatment requested */
  1592. if((req->testno == prevtestno) &&
  1593. (req->partno == prevpartno)) {
  1594. req->partno++;
  1595. logmsg("BOUNCE part number to %ld", req->partno);
  1596. }
  1597. else {
  1598. prevbounce = FALSE;
  1599. prevtestno = -1;
  1600. prevpartno = -1;
  1601. }
  1602. }
  1603. send_doc(msgsock, req);
  1604. if(got_exit_signal)
  1605. return -1;
  1606. if(req->testno < 0) {
  1607. logmsg("special request received, no persistency");
  1608. return -1;
  1609. }
  1610. if(!req->open) {
  1611. logmsg("instructed to close connection after server-reply");
  1612. return -1;
  1613. }
  1614. if(req->connect_request) {
  1615. /* a CONNECT request, setup and talk the tunnel */
  1616. if(!is_proxy) {
  1617. logmsg("received CONNECT but isn't running as proxy!");
  1618. return 1;
  1619. }
  1620. else {
  1621. http_connect(&msgsock, listensock, connecthost, req->connect_port);
  1622. return -1;
  1623. }
  1624. }
  1625. if(req->upgrade_request) {
  1626. /* an upgrade request, switch to http2 here */
  1627. http2(req);
  1628. return -1;
  1629. }
  1630. /* if we got a CONNECT, loop and get another request as well! */
  1631. if(req->open) {
  1632. logmsg("=> persistent connection request ended, awaits new request\n");
  1633. return 1;
  1634. }
  1635. return -1;
  1636. }
  1637. int main(int argc, char *argv[])
  1638. {
  1639. srvr_sockaddr_union_t me;
  1640. curl_socket_t sock = CURL_SOCKET_BAD;
  1641. int wrotepidfile = 0;
  1642. int flag;
  1643. unsigned short port = DEFAULT_PORT;
  1644. #ifdef USE_UNIX_SOCKETS
  1645. const char *unix_socket = NULL;
  1646. bool unlink_socket = false;
  1647. #endif
  1648. const char *pidname = ".http.pid";
  1649. const char *portname = ".http.port";
  1650. struct httprequest req;
  1651. int rc = 0;
  1652. int error;
  1653. int arg = 1;
  1654. long pid;
  1655. const char *connecthost = "127.0.0.1";
  1656. const char *socket_type = "IPv4";
  1657. char port_str[11];
  1658. const char *location_str = port_str;
  1659. /* a default CONNECT port is basically pointless but still ... */
  1660. size_t socket_idx;
  1661. memset(&req, 0, sizeof(req));
  1662. while(argc>arg) {
  1663. if(!strcmp("--version", argv[arg])) {
  1664. puts("sws IPv4"
  1665. #ifdef ENABLE_IPV6
  1666. "/IPv6"
  1667. #endif
  1668. #ifdef USE_UNIX_SOCKETS
  1669. "/unix"
  1670. #endif
  1671. );
  1672. return 0;
  1673. }
  1674. else if(!strcmp("--pidfile", argv[arg])) {
  1675. arg++;
  1676. if(argc>arg)
  1677. pidname = argv[arg++];
  1678. }
  1679. else if(!strcmp("--portfile", argv[arg])) {
  1680. arg++;
  1681. if(argc>arg)
  1682. portname = argv[arg++];
  1683. }
  1684. else if(!strcmp("--logfile", argv[arg])) {
  1685. arg++;
  1686. if(argc>arg)
  1687. serverlogfile = argv[arg++];
  1688. }
  1689. else if(!strcmp("--cmdfile", argv[arg])) {
  1690. arg++;
  1691. if(argc>arg)
  1692. cmdfile = argv[arg++];
  1693. }
  1694. else if(!strcmp("--gopher", argv[arg])) {
  1695. arg++;
  1696. use_gopher = TRUE;
  1697. end_of_headers = "\r\n"; /* gopher style is much simpler */
  1698. }
  1699. else if(!strcmp("--ipv4", argv[arg])) {
  1700. socket_type = "IPv4";
  1701. socket_domain = AF_INET;
  1702. location_str = port_str;
  1703. arg++;
  1704. }
  1705. else if(!strcmp("--ipv6", argv[arg])) {
  1706. #ifdef ENABLE_IPV6
  1707. socket_type = "IPv6";
  1708. socket_domain = AF_INET6;
  1709. location_str = port_str;
  1710. #endif
  1711. arg++;
  1712. }
  1713. else if(!strcmp("--unix-socket", argv[arg])) {
  1714. arg++;
  1715. if(argc>arg) {
  1716. #ifdef USE_UNIX_SOCKETS
  1717. unix_socket = argv[arg];
  1718. if(strlen(unix_socket) >= sizeof(me.sau.sun_path)) {
  1719. fprintf(stderr, "sws: socket path must be shorter than %zu chars\n",
  1720. sizeof(me.sau.sun_path));
  1721. return 0;
  1722. }
  1723. socket_type = "unix";
  1724. socket_domain = AF_UNIX;
  1725. location_str = unix_socket;
  1726. #endif
  1727. arg++;
  1728. }
  1729. }
  1730. else if(!strcmp("--port", argv[arg])) {
  1731. arg++;
  1732. if(argc>arg) {
  1733. char *endptr;
  1734. unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
  1735. if((endptr != argv[arg] + strlen(argv[arg])) ||
  1736. (ulnum && ((ulnum < 1025UL) || (ulnum > 65535UL)))) {
  1737. fprintf(stderr, "sws: invalid --port argument (%s)\n",
  1738. argv[arg]);
  1739. return 0;
  1740. }
  1741. port = curlx_ultous(ulnum);
  1742. arg++;
  1743. }
  1744. }
  1745. else if(!strcmp("--srcdir", argv[arg])) {
  1746. arg++;
  1747. if(argc>arg) {
  1748. path = argv[arg];
  1749. arg++;
  1750. }
  1751. }
  1752. else if(!strcmp("--connect", argv[arg])) {
  1753. /* The connect host IP number that the proxy will connect to no matter
  1754. what the client asks for, but also use this as a hint that we run as
  1755. a proxy and do a few different internal choices */
  1756. arg++;
  1757. if(argc>arg) {
  1758. connecthost = argv[arg];
  1759. arg++;
  1760. is_proxy = TRUE;
  1761. logmsg("Run as proxy, CONNECT to host %s", connecthost);
  1762. }
  1763. }
  1764. else {
  1765. puts("Usage: sws [option]\n"
  1766. " --version\n"
  1767. " --logfile [file]\n"
  1768. " --pidfile [file]\n"
  1769. " --portfile [file]\n"
  1770. " --ipv4\n"
  1771. " --ipv6\n"
  1772. " --unix-socket [file]\n"
  1773. " --port [port]\n"
  1774. " --srcdir [path]\n"
  1775. " --connect [ip4-addr]\n"
  1776. " --gopher");
  1777. return 0;
  1778. }
  1779. }
  1780. #ifdef WIN32
  1781. win32_init();
  1782. atexit(win32_cleanup);
  1783. #endif
  1784. install_signal_handlers(false);
  1785. pid = (long)getpid();
  1786. sock = socket(socket_domain, SOCK_STREAM, 0);
  1787. all_sockets[0] = sock;
  1788. num_sockets = 1;
  1789. if(CURL_SOCKET_BAD == sock) {
  1790. error = SOCKERRNO;
  1791. logmsg("Error creating socket: (%d) %s",
  1792. error, strerror(error));
  1793. goto sws_cleanup;
  1794. }
  1795. flag = 1;
  1796. if(0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  1797. (void *)&flag, sizeof(flag))) {
  1798. error = SOCKERRNO;
  1799. logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
  1800. error, strerror(error));
  1801. goto sws_cleanup;
  1802. }
  1803. if(0 != curlx_nonblock(sock, TRUE)) {
  1804. error = SOCKERRNO;
  1805. logmsg("curlx_nonblock failed with error: (%d) %s",
  1806. error, strerror(error));
  1807. goto sws_cleanup;
  1808. }
  1809. switch(socket_domain) {
  1810. case AF_INET:
  1811. memset(&me.sa4, 0, sizeof(me.sa4));
  1812. me.sa4.sin_family = AF_INET;
  1813. me.sa4.sin_addr.s_addr = INADDR_ANY;
  1814. me.sa4.sin_port = htons(port);
  1815. rc = bind(sock, &me.sa, sizeof(me.sa4));
  1816. break;
  1817. #ifdef ENABLE_IPV6
  1818. case AF_INET6:
  1819. memset(&me.sa6, 0, sizeof(me.sa6));
  1820. me.sa6.sin6_family = AF_INET6;
  1821. me.sa6.sin6_addr = in6addr_any;
  1822. me.sa6.sin6_port = htons(port);
  1823. rc = bind(sock, &me.sa, sizeof(me.sa6));
  1824. break;
  1825. #endif /* ENABLE_IPV6 */
  1826. #ifdef USE_UNIX_SOCKETS
  1827. case AF_UNIX:
  1828. memset(&me.sau, 0, sizeof(me.sau));
  1829. me.sau.sun_family = AF_UNIX;
  1830. strncpy(me.sau.sun_path, unix_socket, sizeof(me.sau.sun_path) - 1);
  1831. rc = bind(sock, &me.sa, sizeof(me.sau));
  1832. if(0 != rc && errno == EADDRINUSE) {
  1833. struct stat statbuf;
  1834. /* socket already exists. Perhaps it is stale? */
  1835. int unixfd = socket(AF_UNIX, SOCK_STREAM, 0);
  1836. if(CURL_SOCKET_BAD == unixfd) {
  1837. error = SOCKERRNO;
  1838. logmsg("Error binding socket, failed to create socket at %s: (%d) %s",
  1839. unix_socket, error, strerror(error));
  1840. goto sws_cleanup;
  1841. }
  1842. /* check whether the server is alive */
  1843. rc = connect(unixfd, &me.sa, sizeof(me.sau));
  1844. error = errno;
  1845. close(unixfd);
  1846. if(ECONNREFUSED != error) {
  1847. logmsg("Error binding socket, failed to connect to %s: (%d) %s",
  1848. unix_socket, error, strerror(error));
  1849. goto sws_cleanup;
  1850. }
  1851. /* socket server is not alive, now check if it was actually a socket.
  1852. * Systems which have Unix sockets will also have lstat */
  1853. rc = lstat(unix_socket, &statbuf);
  1854. if(0 != rc) {
  1855. logmsg("Error binding socket, failed to stat %s: (%d) %s",
  1856. unix_socket, errno, strerror(errno));
  1857. goto sws_cleanup;
  1858. }
  1859. if((statbuf.st_mode & S_IFSOCK) != S_IFSOCK) {
  1860. logmsg("Error binding socket, failed to stat %s: (%d) %s",
  1861. unix_socket, error, strerror(error));
  1862. goto sws_cleanup;
  1863. }
  1864. /* dead socket, cleanup and retry bind */
  1865. rc = unlink(unix_socket);
  1866. if(0 != rc) {
  1867. logmsg("Error binding socket, failed to unlink %s: (%d) %s",
  1868. unix_socket, errno, strerror(errno));
  1869. goto sws_cleanup;
  1870. }
  1871. /* stale socket is gone, retry bind */
  1872. rc = bind(sock, &me.sa, sizeof(me.sau));
  1873. }
  1874. break;
  1875. #endif /* USE_UNIX_SOCKETS */
  1876. }
  1877. if(0 != rc) {
  1878. error = SOCKERRNO;
  1879. logmsg("Error binding socket: (%d) %s", error, strerror(error));
  1880. goto sws_cleanup;
  1881. }
  1882. if(!port) {
  1883. /* The system was supposed to choose a port number, figure out which
  1884. port we actually got and update the listener port value with it. */
  1885. curl_socklen_t la_size;
  1886. srvr_sockaddr_union_t localaddr;
  1887. #ifdef ENABLE_IPV6
  1888. if(socket_domain != AF_INET6)
  1889. #endif
  1890. la_size = sizeof(localaddr.sa4);
  1891. #ifdef ENABLE_IPV6
  1892. else
  1893. la_size = sizeof(localaddr.sa6);
  1894. #endif
  1895. memset(&localaddr.sa, 0, (size_t)la_size);
  1896. if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
  1897. error = SOCKERRNO;
  1898. logmsg("getsockname() failed with error: (%d) %s",
  1899. error, strerror(error));
  1900. sclose(sock);
  1901. goto sws_cleanup;
  1902. }
  1903. switch(localaddr.sa.sa_family) {
  1904. case AF_INET:
  1905. port = ntohs(localaddr.sa4.sin_port);
  1906. break;
  1907. #ifdef ENABLE_IPV6
  1908. case AF_INET6:
  1909. port = ntohs(localaddr.sa6.sin6_port);
  1910. break;
  1911. #endif
  1912. default:
  1913. break;
  1914. }
  1915. if(!port) {
  1916. /* Real failure, listener port shall not be zero beyond this point. */
  1917. logmsg("Apparently getsockname() succeeded, with listener port zero.");
  1918. logmsg("A valid reason for this failure is a binary built without");
  1919. logmsg("proper network library linkage. This might not be the only");
  1920. logmsg("reason, but double check it before anything else.");
  1921. sclose(sock);
  1922. goto sws_cleanup;
  1923. }
  1924. }
  1925. #ifdef USE_UNIX_SOCKETS
  1926. if(socket_domain != AF_UNIX)
  1927. #endif
  1928. msnprintf(port_str, sizeof(port_str), "port %hu", port);
  1929. logmsg("Running %s %s version on %s",
  1930. use_gopher?"GOPHER":"HTTP", socket_type, location_str);
  1931. /* start accepting connections */
  1932. rc = listen(sock, 5);
  1933. if(0 != rc) {
  1934. error = SOCKERRNO;
  1935. logmsg("listen() failed with error: (%d) %s",
  1936. error, strerror(error));
  1937. goto sws_cleanup;
  1938. }
  1939. #ifdef USE_UNIX_SOCKETS
  1940. /* listen succeeds, so let's assume a valid listening Unix socket */
  1941. unlink_socket = true;
  1942. #endif
  1943. /*
  1944. ** As soon as this server writes its pid file the test harness will
  1945. ** attempt to connect to this server and initiate its verification.
  1946. */
  1947. wrotepidfile = write_pidfile(pidname);
  1948. if(!wrotepidfile)
  1949. goto sws_cleanup;
  1950. wrotepidfile = write_portfile(portname, port);
  1951. if(!wrotepidfile)
  1952. goto sws_cleanup;
  1953. /* initialization of httprequest struct is done before get_request(), but
  1954. the pipelining struct field must be initialized previously to FALSE
  1955. every time a new connection arrives. */
  1956. init_httprequest(&req);
  1957. for(;;) {
  1958. fd_set input;
  1959. fd_set output;
  1960. struct timeval timeout = {0, 250000L}; /* 250 ms */
  1961. curl_socket_t maxfd = (curl_socket_t)-1;
  1962. int active;
  1963. /* Clear out closed sockets */
  1964. for(socket_idx = num_sockets - 1; socket_idx >= 1; --socket_idx) {
  1965. if(CURL_SOCKET_BAD == all_sockets[socket_idx]) {
  1966. char *dst = (char *) (all_sockets + socket_idx);
  1967. char *src = (char *) (all_sockets + socket_idx + 1);
  1968. char *end = (char *) (all_sockets + num_sockets);
  1969. memmove(dst, src, end - src);
  1970. num_sockets -= 1;
  1971. }
  1972. }
  1973. if(got_exit_signal)
  1974. goto sws_cleanup;
  1975. /* Set up for select */
  1976. FD_ZERO(&input);
  1977. FD_ZERO(&output);
  1978. for(socket_idx = 0; socket_idx < num_sockets; ++socket_idx) {
  1979. /* Listen on all sockets */
  1980. FD_SET(all_sockets[socket_idx], &input);
  1981. if(all_sockets[socket_idx] > maxfd)
  1982. maxfd = all_sockets[socket_idx];
  1983. }
  1984. if(got_exit_signal)
  1985. goto sws_cleanup;
  1986. do {
  1987. rc = select((int)maxfd + 1, &input, &output, NULL, &timeout);
  1988. } while(rc < 0 && errno == EINTR && !got_exit_signal);
  1989. if(got_exit_signal)
  1990. goto sws_cleanup;
  1991. if(rc < 0) {
  1992. error = SOCKERRNO;
  1993. logmsg("select() failed with error: (%d) %s",
  1994. error, strerror(error));
  1995. goto sws_cleanup;
  1996. }
  1997. if(rc == 0) {
  1998. /* Timed out - try again */
  1999. continue;
  2000. }
  2001. active = rc; /* a positive number */
  2002. /* Check if the listening socket is ready to accept */
  2003. if(FD_ISSET(all_sockets[0], &input)) {
  2004. /* Service all queued connections */
  2005. curl_socket_t msgsock;
  2006. do {
  2007. msgsock = accept_connection(sock);
  2008. logmsg("accept_connection %d returned %d", sock, msgsock);
  2009. if(CURL_SOCKET_BAD == msgsock)
  2010. goto sws_cleanup;
  2011. } while(msgsock > 0);
  2012. active--;
  2013. }
  2014. /* Service all connections that are ready */
  2015. for(socket_idx = 1; (socket_idx < num_sockets) && active; ++socket_idx) {
  2016. if(FD_ISSET(all_sockets[socket_idx], &input)) {
  2017. active--;
  2018. if(got_exit_signal)
  2019. goto sws_cleanup;
  2020. /* Service this connection until it has nothing available */
  2021. do {
  2022. rc = service_connection(all_sockets[socket_idx], &req, sock,
  2023. connecthost);
  2024. if(got_exit_signal)
  2025. goto sws_cleanup;
  2026. if(rc < 0) {
  2027. logmsg("====> Client disconnect %d", req.connmon);
  2028. if(req.connmon) {
  2029. const char *keepopen = "[DISCONNECT]\n";
  2030. storerequest(keepopen, strlen(keepopen));
  2031. }
  2032. if(!req.open)
  2033. /* When instructed to close connection after server-reply we
  2034. wait a very small amount of time before doing so. If this
  2035. is not done client might get an ECONNRESET before reading
  2036. a single byte of server-reply. */
  2037. wait_ms(50);
  2038. if(all_sockets[socket_idx] != CURL_SOCKET_BAD) {
  2039. sclose(all_sockets[socket_idx]);
  2040. all_sockets[socket_idx] = CURL_SOCKET_BAD;
  2041. }
  2042. serverlogslocked -= 1;
  2043. if(!serverlogslocked)
  2044. clear_advisor_read_lock(SERVERLOGS_LOCK);
  2045. if(req.testno == DOCNUMBER_QUIT)
  2046. goto sws_cleanup;
  2047. }
  2048. /* Reset the request, unless we're still in the middle of reading */
  2049. if(rc != 0)
  2050. init_httprequest(&req);
  2051. } while(rc > 0);
  2052. }
  2053. }
  2054. if(got_exit_signal)
  2055. goto sws_cleanup;
  2056. }
  2057. sws_cleanup:
  2058. for(socket_idx = 1; socket_idx < num_sockets; ++socket_idx)
  2059. if((all_sockets[socket_idx] != sock) &&
  2060. (all_sockets[socket_idx] != CURL_SOCKET_BAD))
  2061. sclose(all_sockets[socket_idx]);
  2062. if(sock != CURL_SOCKET_BAD)
  2063. sclose(sock);
  2064. #ifdef USE_UNIX_SOCKETS
  2065. if(unlink_socket && socket_domain == AF_UNIX) {
  2066. rc = unlink(unix_socket);
  2067. logmsg("unlink(%s) = %d (%s)", unix_socket, rc, strerror(rc));
  2068. }
  2069. #endif
  2070. if(got_exit_signal)
  2071. logmsg("signalled to die");
  2072. if(wrotepidfile)
  2073. unlink(pidname);
  2074. if(serverlogslocked) {
  2075. serverlogslocked = 0;
  2076. clear_advisor_read_lock(SERVERLOGS_LOCK);
  2077. }
  2078. restore_signal_handlers(false);
  2079. if(got_exit_signal) {
  2080. logmsg("========> %s sws (%s pid: %ld) exits with signal (%d)",
  2081. socket_type, location_str, pid, exit_signal);
  2082. /*
  2083. * To properly set the return status of the process we
  2084. * must raise the same signal SIGINT or SIGTERM that we
  2085. * caught and let the old handler take care of it.
  2086. */
  2087. raise(exit_signal);
  2088. }
  2089. logmsg("========> sws quits");
  2090. return 0;
  2091. }