rtspd.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "server_setup.h"
  25. /*
  26. * curl's test suite Real Time Streaming Protocol (RTSP) server.
  27. *
  28. * This source file was started based on curl's HTTP test suite server.
  29. */
  30. #ifdef HAVE_SIGNAL_H
  31. #include <signal.h>
  32. #endif
  33. #ifdef HAVE_NETINET_IN_H
  34. #include <netinet/in.h>
  35. #endif
  36. #ifdef HAVE_NETINET_IN6_H
  37. #include <netinet/in6.h>
  38. #endif
  39. #ifdef HAVE_ARPA_INET_H
  40. #include <arpa/inet.h>
  41. #endif
  42. #ifdef HAVE_NETDB_H
  43. #include <netdb.h>
  44. #endif
  45. #ifdef HAVE_NETINET_TCP_H
  46. #include <netinet/tcp.h> /* for TCP_NODELAY */
  47. #endif
  48. #define ENABLE_CURLX_PRINTF
  49. /* make the curlx header define all printf() functions to use the curlx_*
  50. versions instead */
  51. #include "curlx.h" /* from the private lib dir */
  52. #include "getpart.h"
  53. #include "util.h"
  54. #include "server_sockaddr.h"
  55. /* include memdebug.h last */
  56. #include "memdebug.h"
  57. #ifdef USE_WINSOCK
  58. #undef EINTR
  59. #define EINTR 4 /* errno.h value */
  60. #undef ERANGE
  61. #define ERANGE 34 /* errno.h value */
  62. #endif
  63. #ifdef ENABLE_IPV6
  64. static bool use_ipv6 = FALSE;
  65. #endif
  66. static const char *ipv_inuse = "IPv4";
  67. static int serverlogslocked = 0;
  68. #define REQBUFSIZ 150000
  69. #define REQBUFSIZ_TXT "149999"
  70. static long prevtestno = -1; /* previous test number we served */
  71. static long prevpartno = -1; /* previous part number we served */
  72. static bool prevbounce = FALSE; /* instructs the server to increase the part
  73. number for a test in case the identical
  74. testno+partno request shows up again */
  75. #define RCMD_NORMALREQ 0 /* default request, use the tests file normally */
  76. #define RCMD_IDLE 1 /* told to sit idle */
  77. #define RCMD_STREAM 2 /* told to stream */
  78. typedef enum {
  79. RPROT_NONE = 0,
  80. RPROT_RTSP = 1,
  81. RPROT_HTTP = 2
  82. } reqprot_t;
  83. #define SET_RTP_PKT_CHN(p,c) ((p)[1] = (unsigned char)((c) & 0xFF))
  84. #define SET_RTP_PKT_LEN(p,l) (((p)[2] = (unsigned char)(((l) >> 8) & 0xFF)), \
  85. ((p)[3] = (unsigned char)((l) & 0xFF)))
  86. struct httprequest {
  87. char reqbuf[REQBUFSIZ]; /* buffer area for the incoming request */
  88. size_t checkindex; /* where to start checking of the request */
  89. size_t offset; /* size of the incoming request */
  90. long testno; /* test number found in the request */
  91. long partno; /* part number found in the request */
  92. bool open; /* keep connection open info, as found in the request */
  93. bool auth_req; /* authentication required, don't wait for body unless
  94. there's an Authorization header */
  95. bool auth; /* Authorization header present in the incoming request */
  96. size_t cl; /* Content-Length of the incoming request */
  97. bool digest; /* Authorization digest header found */
  98. bool ntlm; /* Authorization ntlm header found */
  99. int pipe; /* if non-zero, expect this many requests to do a "piped"
  100. request/response */
  101. int skip; /* if non-zero, the server is instructed to not read this
  102. many bytes from a PUT/POST request. Ie the client sends N
  103. bytes said in Content-Length, but the server only reads N
  104. - skip bytes. */
  105. int rcmd; /* doing a special command, see defines above */
  106. reqprot_t protocol; /* request protocol, HTTP or RTSP */
  107. int prot_version; /* HTTP or RTSP version (major*10 + minor) */
  108. bool pipelining; /* true if request is pipelined */
  109. char *rtp_buffer;
  110. size_t rtp_buffersize;
  111. };
  112. static int ProcessRequest(struct httprequest *req);
  113. static void storerequest(char *reqbuf, size_t totalsize);
  114. #define DEFAULT_PORT 8999
  115. #ifndef DEFAULT_LOGFILE
  116. #define DEFAULT_LOGFILE "log/rtspd.log"
  117. #endif
  118. const char *serverlogfile = DEFAULT_LOGFILE;
  119. #define RTSPDVERSION "curl test suite RTSP server/0.1"
  120. #define REQUEST_DUMP "log/server.input"
  121. #define RESPONSE_DUMP "log/server.response"
  122. /* very-big-path support */
  123. #define MAXDOCNAMELEN 140000
  124. #define MAXDOCNAMELEN_TXT "139999"
  125. #define REQUEST_KEYWORD_SIZE 256
  126. #define REQUEST_KEYWORD_SIZE_TXT "255"
  127. #define CMD_AUTH_REQUIRED "auth_required"
  128. /* 'idle' means that it will accept the request fine but never respond
  129. any data. Just keep the connection alive. */
  130. #define CMD_IDLE "idle"
  131. /* 'stream' means to send a never-ending stream of data */
  132. #define CMD_STREAM "stream"
  133. #define END_OF_HEADERS "\r\n\r\n"
  134. enum {
  135. DOCNUMBER_NOTHING = -7,
  136. DOCNUMBER_QUIT = -6,
  137. DOCNUMBER_BADCONNECT = -5,
  138. DOCNUMBER_INTERNAL = -4,
  139. DOCNUMBER_CONNECT = -3,
  140. DOCNUMBER_WERULEZ = -2,
  141. DOCNUMBER_404 = -1
  142. };
  143. /* sent as reply to a QUIT */
  144. static const char *docquit =
  145. "HTTP/1.1 200 Goodbye" END_OF_HEADERS;
  146. /* sent as reply to a CONNECT */
  147. static const char *docconnect =
  148. "HTTP/1.1 200 Mighty fine indeed" END_OF_HEADERS;
  149. /* sent as reply to a "bad" CONNECT */
  150. static const char *docbadconnect =
  151. "HTTP/1.1 501 Forbidden you fool" END_OF_HEADERS;
  152. /* send back this on HTTP 404 file not found */
  153. static const char *doc404_HTTP = "HTTP/1.1 404 Not Found\r\n"
  154. "Server: " RTSPDVERSION "\r\n"
  155. "Connection: close\r\n"
  156. "Content-Type: text/html"
  157. END_OF_HEADERS
  158. "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
  159. "<HTML><HEAD>\n"
  160. "<TITLE>404 Not Found</TITLE>\n"
  161. "</HEAD><BODY>\n"
  162. "<H1>Not Found</H1>\n"
  163. "The requested URL was not found on this server.\n"
  164. "<P><HR><ADDRESS>" RTSPDVERSION "</ADDRESS>\n" "</BODY></HTML>\n";
  165. /* send back this on RTSP 404 file not found */
  166. static const char *doc404_RTSP = "RTSP/1.0 404 Not Found\r\n"
  167. "Server: " RTSPDVERSION
  168. END_OF_HEADERS;
  169. /* Default size to send away fake RTP data */
  170. #define RTP_DATA_SIZE 12
  171. static const char *RTP_DATA = "$_1234\n\0asdf";
  172. static int ProcessRequest(struct httprequest *req)
  173. {
  174. char *line = &req->reqbuf[req->checkindex];
  175. bool chunked = FALSE;
  176. static char request[REQUEST_KEYWORD_SIZE];
  177. static char doc[MAXDOCNAMELEN];
  178. static char prot_str[5];
  179. int prot_major, prot_minor;
  180. char *end = strstr(line, END_OF_HEADERS);
  181. logmsg("ProcessRequest() called with testno %ld and line [%s]",
  182. req->testno, line);
  183. /* try to figure out the request characteristics as soon as possible, but
  184. only once! */
  185. if((req->testno == DOCNUMBER_NOTHING) &&
  186. sscanf(line,
  187. "%" REQUEST_KEYWORD_SIZE_TXT"s %" MAXDOCNAMELEN_TXT "s %4s/%d.%d",
  188. request,
  189. doc,
  190. prot_str,
  191. &prot_major,
  192. &prot_minor) == 5) {
  193. char *ptr;
  194. char logbuf[256];
  195. if(!strcmp(prot_str, "HTTP")) {
  196. req->protocol = RPROT_HTTP;
  197. }
  198. else if(!strcmp(prot_str, "RTSP")) {
  199. req->protocol = RPROT_RTSP;
  200. }
  201. else {
  202. req->protocol = RPROT_NONE;
  203. logmsg("got unknown protocol %s", prot_str);
  204. return 1;
  205. }
  206. req->prot_version = prot_major*10 + prot_minor;
  207. /* find the last slash */
  208. ptr = strrchr(doc, '/');
  209. /* get the number after it */
  210. if(ptr) {
  211. FILE *stream;
  212. if((strlen(doc) + strlen(request)) < 200)
  213. msnprintf(logbuf, sizeof(logbuf), "Got request: %s %s %s/%d.%d",
  214. request, doc, prot_str, prot_major, prot_minor);
  215. else
  216. msnprintf(logbuf, sizeof(logbuf), "Got a *HUGE* request %s/%d.%d",
  217. prot_str, prot_major, prot_minor);
  218. logmsg("%s", logbuf);
  219. if(!strncmp("/verifiedserver", ptr, 15)) {
  220. logmsg("Are-we-friendly question received");
  221. req->testno = DOCNUMBER_WERULEZ;
  222. return 1; /* done */
  223. }
  224. if(!strncmp("/quit", ptr, 5)) {
  225. logmsg("Request-to-quit received");
  226. req->testno = DOCNUMBER_QUIT;
  227. return 1; /* done */
  228. }
  229. ptr++; /* skip the slash */
  230. /* skip all non-numericals following the slash */
  231. while(*ptr && !ISDIGIT(*ptr))
  232. ptr++;
  233. req->testno = strtol(ptr, &ptr, 10);
  234. if(req->testno > 10000) {
  235. req->partno = req->testno % 10000;
  236. req->testno /= 10000;
  237. }
  238. else
  239. req->partno = 0;
  240. msnprintf(logbuf, sizeof(logbuf), "Requested test number %ld part %ld",
  241. req->testno, req->partno);
  242. logmsg("%s", logbuf);
  243. stream = test2fopen(req->testno);
  244. if(!stream) {
  245. int error = errno;
  246. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  247. logmsg("Couldn't open test file %ld", req->testno);
  248. req->open = FALSE; /* closes connection */
  249. return 1; /* done */
  250. }
  251. else {
  252. char *cmd = NULL;
  253. size_t cmdsize = 0;
  254. int num = 0;
  255. int rtp_channel = 0;
  256. int rtp_size = 0;
  257. int rtp_partno = -1;
  258. char *rtp_scratch = NULL;
  259. /* get the custom server control "commands" */
  260. int error = getpart(&cmd, &cmdsize, "reply", "servercmd", stream);
  261. fclose(stream);
  262. if(error) {
  263. logmsg("getpart() failed with error: %d", error);
  264. req->open = FALSE; /* closes connection */
  265. return 1; /* done */
  266. }
  267. ptr = cmd;
  268. if(cmdsize) {
  269. logmsg("Found a reply-servercmd section!");
  270. do {
  271. if(!strncmp(CMD_AUTH_REQUIRED, ptr, strlen(CMD_AUTH_REQUIRED))) {
  272. logmsg("instructed to require authorization header");
  273. req->auth_req = TRUE;
  274. }
  275. else if(!strncmp(CMD_IDLE, ptr, strlen(CMD_IDLE))) {
  276. logmsg("instructed to idle");
  277. req->rcmd = RCMD_IDLE;
  278. req->open = TRUE;
  279. }
  280. else if(!strncmp(CMD_STREAM, ptr, strlen(CMD_STREAM))) {
  281. logmsg("instructed to stream");
  282. req->rcmd = RCMD_STREAM;
  283. }
  284. else if(1 == sscanf(ptr, "pipe: %d", &num)) {
  285. logmsg("instructed to allow a pipe size of %d", num);
  286. if(num < 0)
  287. logmsg("negative pipe size ignored");
  288. else if(num > 0)
  289. req->pipe = num-1; /* decrease by one since we don't count the
  290. first request in this number */
  291. }
  292. else if(1 == sscanf(ptr, "skip: %d", &num)) {
  293. logmsg("instructed to skip this number of bytes %d", num);
  294. req->skip = num;
  295. }
  296. else if(3 == sscanf(ptr, "rtp: part %d channel %d size %d",
  297. &rtp_partno, &rtp_channel, &rtp_size)) {
  298. if(rtp_partno == req->partno) {
  299. int i = 0;
  300. logmsg("RTP: part %d channel %d size %d",
  301. rtp_partno, rtp_channel, rtp_size);
  302. /* Make our scratch buffer enough to fit all the
  303. * desired data and one for padding */
  304. rtp_scratch = malloc(rtp_size + 4 + RTP_DATA_SIZE);
  305. /* RTP is signalled with a $ */
  306. rtp_scratch[0] = '$';
  307. /* The channel follows and is one byte */
  308. SET_RTP_PKT_CHN(rtp_scratch, rtp_channel);
  309. /* Length follows and is a two byte short in network order */
  310. SET_RTP_PKT_LEN(rtp_scratch, rtp_size);
  311. /* Fill it with junk data */
  312. for(i = 0; i < rtp_size; i += RTP_DATA_SIZE) {
  313. memcpy(rtp_scratch + 4 + i, RTP_DATA, RTP_DATA_SIZE);
  314. }
  315. if(!req->rtp_buffer) {
  316. req->rtp_buffer = rtp_scratch;
  317. req->rtp_buffersize = rtp_size + 4;
  318. }
  319. else {
  320. req->rtp_buffer = realloc(req->rtp_buffer,
  321. req->rtp_buffersize +
  322. rtp_size + 4);
  323. memcpy(req->rtp_buffer + req->rtp_buffersize, rtp_scratch,
  324. rtp_size + 4);
  325. req->rtp_buffersize += rtp_size + 4;
  326. free(rtp_scratch);
  327. }
  328. logmsg("rtp_buffersize is %zu, rtp_size is %d.",
  329. req->rtp_buffersize, rtp_size);
  330. }
  331. }
  332. else {
  333. logmsg("funny instruction found: %s", ptr);
  334. }
  335. ptr = strchr(ptr, '\n');
  336. if(ptr)
  337. ptr++;
  338. else
  339. ptr = NULL;
  340. } while(ptr && *ptr);
  341. logmsg("Done parsing server commands");
  342. }
  343. free(cmd);
  344. }
  345. }
  346. else {
  347. if(sscanf(req->reqbuf, "CONNECT %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
  348. doc, &prot_major, &prot_minor) == 3) {
  349. msnprintf(logbuf, sizeof(logbuf),
  350. "Received a CONNECT %s HTTP/%d.%d request",
  351. doc, prot_major, prot_minor);
  352. logmsg("%s", logbuf);
  353. if(req->prot_version == 10)
  354. req->open = FALSE; /* HTTP 1.0 closes connection by default */
  355. if(!strncmp(doc, "bad", 3))
  356. /* if the host name starts with bad, we fake an error here */
  357. req->testno = DOCNUMBER_BADCONNECT;
  358. else if(!strncmp(doc, "test", 4)) {
  359. /* if the host name starts with test, the port number used in the
  360. CONNECT line will be used as test number! */
  361. char *portp = strchr(doc, ':');
  362. if(portp && (*(portp + 1) != '\0') && ISDIGIT(*(portp + 1)))
  363. req->testno = strtol(portp + 1, NULL, 10);
  364. else
  365. req->testno = DOCNUMBER_CONNECT;
  366. }
  367. else
  368. req->testno = DOCNUMBER_CONNECT;
  369. }
  370. else {
  371. logmsg("Did not find test number in PATH");
  372. req->testno = DOCNUMBER_404;
  373. }
  374. }
  375. }
  376. if(!end) {
  377. /* we don't have a complete request yet! */
  378. logmsg("ProcessRequest returned without a complete request");
  379. return 0; /* not complete yet */
  380. }
  381. logmsg("ProcessRequest found a complete request");
  382. if(req->pipe)
  383. /* we do have a full set, advance the checkindex to after the end of the
  384. headers, for the pipelining case mostly */
  385. req->checkindex += (end - line) + strlen(END_OF_HEADERS);
  386. /* **** Persistence ****
  387. *
  388. * If the request is a HTTP/1.0 one, we close the connection unconditionally
  389. * when we're done.
  390. *
  391. * If the request is a HTTP/1.1 one, we MUST check for a "Connection:"
  392. * header that might say "close". If it does, we close a connection when
  393. * this request is processed. Otherwise, we keep the connection alive for X
  394. * seconds.
  395. */
  396. do {
  397. if(got_exit_signal)
  398. return 1; /* done */
  399. if((req->cl == 0) && strncasecompare("Content-Length:", line, 15)) {
  400. /* If we don't ignore content-length, we read it and we read the whole
  401. request including the body before we return. If we've been told to
  402. ignore the content-length, we will return as soon as all headers
  403. have been received */
  404. char *endptr;
  405. char *ptr = line + 15;
  406. unsigned long clen = 0;
  407. while(*ptr && ISSPACE(*ptr))
  408. ptr++;
  409. endptr = ptr;
  410. errno = 0;
  411. clen = strtoul(ptr, &endptr, 10);
  412. if((ptr == endptr) || !ISSPACE(*endptr) || (ERANGE == errno)) {
  413. /* this assumes that a zero Content-Length is valid */
  414. logmsg("Found invalid Content-Length: (%s) in the request", ptr);
  415. req->open = FALSE; /* closes connection */
  416. return 1; /* done */
  417. }
  418. req->cl = clen - req->skip;
  419. logmsg("Found Content-Length: %lu in the request", clen);
  420. if(req->skip)
  421. logmsg("... but will abort after %zu bytes", req->cl);
  422. break;
  423. }
  424. else if(strncasecompare("Transfer-Encoding: chunked", line,
  425. strlen("Transfer-Encoding: chunked"))) {
  426. /* chunked data coming in */
  427. chunked = TRUE;
  428. }
  429. if(chunked) {
  430. if(strstr(req->reqbuf, "\r\n0\r\n\r\n"))
  431. /* end of chunks reached */
  432. return 1; /* done */
  433. else
  434. return 0; /* not done */
  435. }
  436. line = strchr(line, '\n');
  437. if(line)
  438. line++;
  439. } while(line);
  440. if(!req->auth && strstr(req->reqbuf, "Authorization:")) {
  441. req->auth = TRUE; /* Authorization: header present! */
  442. if(req->auth_req)
  443. logmsg("Authorization header found, as required");
  444. }
  445. if(!req->digest && strstr(req->reqbuf, "Authorization: Digest")) {
  446. /* If the client is passing this Digest-header, we set the part number
  447. to 1000. Not only to spice up the complexity of this, but to make
  448. Digest stuff to work in the test suite. */
  449. req->partno += 1000;
  450. req->digest = TRUE; /* header found */
  451. logmsg("Received Digest request, sending back data %ld", req->partno);
  452. }
  453. else if(!req->ntlm &&
  454. strstr(req->reqbuf, "Authorization: NTLM TlRMTVNTUAAD")) {
  455. /* If the client is passing this type-3 NTLM header */
  456. req->partno += 1002;
  457. req->ntlm = TRUE; /* NTLM found */
  458. logmsg("Received NTLM type-3, sending back data %ld", req->partno);
  459. if(req->cl) {
  460. logmsg(" Expecting %zu POSTed bytes", req->cl);
  461. }
  462. }
  463. else if(!req->ntlm &&
  464. strstr(req->reqbuf, "Authorization: NTLM TlRMTVNTUAAB")) {
  465. /* If the client is passing this type-1 NTLM header */
  466. req->partno += 1001;
  467. req->ntlm = TRUE; /* NTLM found */
  468. logmsg("Received NTLM type-1, sending back data %ld", req->partno);
  469. }
  470. else if((req->partno >= 1000) &&
  471. strstr(req->reqbuf, "Authorization: Basic")) {
  472. /* If the client is passing this Basic-header and the part number is
  473. already >=1000, we add 1 to the part number. This allows simple Basic
  474. authentication negotiation to work in the test suite. */
  475. req->partno += 1;
  476. logmsg("Received Basic request, sending back data %ld", req->partno);
  477. }
  478. if(strstr(req->reqbuf, "Connection: close"))
  479. req->open = FALSE; /* close connection after this request */
  480. if(!req->pipe &&
  481. req->open &&
  482. req->prot_version >= 11 &&
  483. req->reqbuf + req->offset > end + strlen(END_OF_HEADERS) &&
  484. (!strncmp(req->reqbuf, "GET", strlen("GET")) ||
  485. !strncmp(req->reqbuf, "HEAD", strlen("HEAD")))) {
  486. /* If we have a persistent connection, HTTP version >= 1.1
  487. and GET/HEAD request, enable pipelining. */
  488. req->checkindex = (end - req->reqbuf) + strlen(END_OF_HEADERS);
  489. req->pipelining = TRUE;
  490. }
  491. while(req->pipe) {
  492. if(got_exit_signal)
  493. return 1; /* done */
  494. /* scan for more header ends within this chunk */
  495. line = &req->reqbuf[req->checkindex];
  496. end = strstr(line, END_OF_HEADERS);
  497. if(!end)
  498. break;
  499. req->checkindex += (end - line) + strlen(END_OF_HEADERS);
  500. req->pipe--;
  501. }
  502. /* If authentication is required and no auth was provided, end now. This
  503. makes the server NOT wait for PUT/POST data and you can then make the
  504. test case send a rejection before any such data has been sent. Test case
  505. 154 uses this.*/
  506. if(req->auth_req && !req->auth)
  507. return 1; /* done */
  508. if(req->cl > 0) {
  509. if(req->cl <= req->offset - (end - req->reqbuf) - strlen(END_OF_HEADERS))
  510. return 1; /* done */
  511. else
  512. return 0; /* not complete yet */
  513. }
  514. return 1; /* done */
  515. }
  516. /* store the entire request in a file */
  517. static void storerequest(char *reqbuf, size_t totalsize)
  518. {
  519. int res;
  520. int error = 0;
  521. size_t written;
  522. size_t writeleft;
  523. FILE *dump;
  524. if(!reqbuf)
  525. return;
  526. if(totalsize == 0)
  527. return;
  528. do {
  529. dump = fopen(REQUEST_DUMP, "ab");
  530. } while(!dump && ((error = errno) == EINTR));
  531. if(!dump) {
  532. logmsg("Error opening file %s error: %d %s",
  533. REQUEST_DUMP, error, strerror(error));
  534. logmsg("Failed to write request input to " REQUEST_DUMP);
  535. return;
  536. }
  537. writeleft = totalsize;
  538. do {
  539. written = fwrite(&reqbuf[totalsize-writeleft],
  540. 1, writeleft, dump);
  541. if(got_exit_signal)
  542. goto storerequest_cleanup;
  543. if(written > 0)
  544. writeleft -= written;
  545. } while((writeleft > 0) && ((error = errno) == EINTR));
  546. if(writeleft == 0)
  547. logmsg("Wrote request (%zu bytes) input to " REQUEST_DUMP, totalsize);
  548. else if(writeleft > 0) {
  549. logmsg("Error writing file %s error: %d %s",
  550. REQUEST_DUMP, error, strerror(error));
  551. logmsg("Wrote only (%zu bytes) of (%zu bytes) request input to %s",
  552. totalsize-writeleft, totalsize, REQUEST_DUMP);
  553. }
  554. storerequest_cleanup:
  555. do {
  556. res = fclose(dump);
  557. } while(res && ((error = errno) == EINTR));
  558. if(res)
  559. logmsg("Error closing file %s error: %d %s",
  560. REQUEST_DUMP, error, strerror(error));
  561. }
  562. /* return 0 on success, non-zero on failure */
  563. static int get_request(curl_socket_t sock, struct httprequest *req)
  564. {
  565. int error;
  566. int fail = 0;
  567. int done_processing = 0;
  568. char *reqbuf = req->reqbuf;
  569. ssize_t got = 0;
  570. char *pipereq = NULL;
  571. size_t pipereq_length = 0;
  572. if(req->pipelining) {
  573. pipereq = reqbuf + req->checkindex;
  574. pipereq_length = req->offset - req->checkindex;
  575. }
  576. /*** Init the httprequest structure properly for the upcoming request ***/
  577. req->checkindex = 0;
  578. req->offset = 0;
  579. req->testno = DOCNUMBER_NOTHING;
  580. req->partno = 0;
  581. req->open = TRUE;
  582. req->auth_req = FALSE;
  583. req->auth = FALSE;
  584. req->cl = 0;
  585. req->digest = FALSE;
  586. req->ntlm = FALSE;
  587. req->pipe = 0;
  588. req->skip = 0;
  589. req->rcmd = RCMD_NORMALREQ;
  590. req->protocol = RPROT_NONE;
  591. req->prot_version = 0;
  592. req->pipelining = FALSE;
  593. req->rtp_buffer = NULL;
  594. req->rtp_buffersize = 0;
  595. /*** end of httprequest init ***/
  596. while(!done_processing && (req->offset < REQBUFSIZ-1)) {
  597. if(pipereq_length && pipereq) {
  598. memmove(reqbuf, pipereq, pipereq_length);
  599. got = curlx_uztosz(pipereq_length);
  600. pipereq_length = 0;
  601. }
  602. else {
  603. if(req->skip)
  604. /* we are instructed to not read the entire thing, so we make sure to
  605. only read what we're supposed to and NOT read the enire thing the
  606. client wants to send! */
  607. got = sread(sock, reqbuf + req->offset, req->cl);
  608. else
  609. got = sread(sock, reqbuf + req->offset, REQBUFSIZ-1 - req->offset);
  610. }
  611. if(got_exit_signal)
  612. return 1;
  613. if(got == 0) {
  614. logmsg("Connection closed by client");
  615. fail = 1;
  616. }
  617. else if(got < 0) {
  618. error = SOCKERRNO;
  619. logmsg("recv() returned error: (%d) %s", error, strerror(error));
  620. fail = 1;
  621. }
  622. if(fail) {
  623. /* dump the request received so far to the external file */
  624. reqbuf[req->offset] = '\0';
  625. storerequest(reqbuf, req->offset);
  626. return 1;
  627. }
  628. logmsg("Read %zd bytes", got);
  629. req->offset += (size_t)got;
  630. reqbuf[req->offset] = '\0';
  631. done_processing = ProcessRequest(req);
  632. if(got_exit_signal)
  633. return 1;
  634. if(done_processing && req->pipe) {
  635. logmsg("Waiting for another piped request");
  636. done_processing = 0;
  637. req->pipe--;
  638. }
  639. }
  640. if((req->offset == REQBUFSIZ-1) && (got > 0)) {
  641. logmsg("Request would overflow buffer, closing connection");
  642. /* dump request received so far to external file anyway */
  643. reqbuf[REQBUFSIZ-1] = '\0';
  644. fail = 1;
  645. }
  646. else if(req->offset > REQBUFSIZ-1) {
  647. logmsg("Request buffer overflow, closing connection");
  648. /* dump request received so far to external file anyway */
  649. reqbuf[REQBUFSIZ-1] = '\0';
  650. fail = 1;
  651. }
  652. else
  653. reqbuf[req->offset] = '\0';
  654. /* dump the request to an external file */
  655. storerequest(reqbuf, req->pipelining ? req->checkindex : req->offset);
  656. if(got_exit_signal)
  657. return 1;
  658. return fail; /* return 0 on success */
  659. }
  660. /* returns -1 on failure */
  661. static int send_doc(curl_socket_t sock, struct httprequest *req)
  662. {
  663. ssize_t written;
  664. size_t count;
  665. const char *buffer;
  666. char *ptr = NULL;
  667. char *cmd = NULL;
  668. size_t cmdsize = 0;
  669. FILE *dump;
  670. bool persistent = TRUE;
  671. bool sendfailure = FALSE;
  672. size_t responsesize;
  673. int error = 0;
  674. int res;
  675. static char weare[256];
  676. logmsg("Send response number %ld part %ld", req->testno, req->partno);
  677. switch(req->rcmd) {
  678. default:
  679. case RCMD_NORMALREQ:
  680. break; /* continue with business as usual */
  681. case RCMD_STREAM:
  682. #define STREAMTHIS "a string to stream 01234567890\n"
  683. count = strlen(STREAMTHIS);
  684. for(;;) {
  685. written = swrite(sock, STREAMTHIS, count);
  686. if(got_exit_signal)
  687. return -1;
  688. if(written != (ssize_t)count) {
  689. logmsg("Stopped streaming");
  690. break;
  691. }
  692. }
  693. return -1;
  694. case RCMD_IDLE:
  695. /* Do nothing. Sit idle. Pretend it rains. */
  696. return 0;
  697. }
  698. req->open = FALSE;
  699. if(req->testno < 0) {
  700. size_t msglen;
  701. char msgbuf[64];
  702. switch(req->testno) {
  703. case DOCNUMBER_QUIT:
  704. logmsg("Replying to QUIT");
  705. buffer = docquit;
  706. break;
  707. case DOCNUMBER_WERULEZ:
  708. /* we got a "friends?" question, reply back that we sure are */
  709. logmsg("Identifying ourselves as friends");
  710. msnprintf(msgbuf, sizeof(msgbuf), "RTSP_SERVER WE ROOLZ: %"
  711. CURL_FORMAT_CURL_OFF_T "\r\n", our_getpid());
  712. msglen = strlen(msgbuf);
  713. msnprintf(weare, sizeof(weare),
  714. "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
  715. msglen, msgbuf);
  716. buffer = weare;
  717. break;
  718. case DOCNUMBER_INTERNAL:
  719. logmsg("Bailing out due to internal error");
  720. return -1;
  721. case DOCNUMBER_CONNECT:
  722. logmsg("Replying to CONNECT");
  723. buffer = docconnect;
  724. break;
  725. case DOCNUMBER_BADCONNECT:
  726. logmsg("Replying to a bad CONNECT");
  727. buffer = docbadconnect;
  728. break;
  729. case DOCNUMBER_404:
  730. default:
  731. logmsg("Replying to with a 404");
  732. if(req->protocol == RPROT_HTTP) {
  733. buffer = doc404_HTTP;
  734. }
  735. else {
  736. buffer = doc404_RTSP;
  737. }
  738. break;
  739. }
  740. count = strlen(buffer);
  741. }
  742. else {
  743. FILE *stream = test2fopen(req->testno);
  744. char partbuf[80]="data";
  745. if(0 != req->partno)
  746. msnprintf(partbuf, sizeof(partbuf), "data%ld", req->partno);
  747. if(!stream) {
  748. error = errno;
  749. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  750. logmsg("Couldn't open test file");
  751. return 0;
  752. }
  753. else {
  754. error = getpart(&ptr, &count, "reply", partbuf, stream);
  755. fclose(stream);
  756. if(error) {
  757. logmsg("getpart() failed with error: %d", error);
  758. return 0;
  759. }
  760. buffer = ptr;
  761. }
  762. if(got_exit_signal) {
  763. free(ptr);
  764. return -1;
  765. }
  766. /* re-open the same file again */
  767. stream = test2fopen(req->testno);
  768. if(!stream) {
  769. error = errno;
  770. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  771. logmsg("Couldn't open test file");
  772. free(ptr);
  773. return 0;
  774. }
  775. else {
  776. /* get the custom server control "commands" */
  777. error = getpart(&cmd, &cmdsize, "reply", "postcmd", stream);
  778. fclose(stream);
  779. if(error) {
  780. logmsg("getpart() failed with error: %d", error);
  781. free(ptr);
  782. return 0;
  783. }
  784. }
  785. }
  786. if(got_exit_signal) {
  787. free(ptr);
  788. free(cmd);
  789. return -1;
  790. }
  791. /* If the word 'swsclose' is present anywhere in the reply chunk, the
  792. connection will be closed after the data has been sent to the requesting
  793. client... */
  794. if(strstr(buffer, "swsclose") || !count) {
  795. persistent = FALSE;
  796. logmsg("connection close instruction \"swsclose\" found in response");
  797. }
  798. if(strstr(buffer, "swsbounce")) {
  799. prevbounce = TRUE;
  800. logmsg("enable \"swsbounce\" in the next request");
  801. }
  802. else
  803. prevbounce = FALSE;
  804. dump = fopen(RESPONSE_DUMP, "ab");
  805. if(!dump) {
  806. error = errno;
  807. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  808. logmsg("Error opening file: %s", RESPONSE_DUMP);
  809. logmsg("couldn't create logfile: " RESPONSE_DUMP);
  810. free(ptr);
  811. free(cmd);
  812. return -1;
  813. }
  814. responsesize = count;
  815. do {
  816. /* Ok, we send no more than 200 bytes at a time, just to make sure that
  817. larger chunks are split up so that the client will need to do multiple
  818. recv() calls to get it and thus we exercise that code better */
  819. size_t num = count;
  820. if(num > 200)
  821. num = 200;
  822. written = swrite(sock, buffer, num);
  823. if(written < 0) {
  824. sendfailure = TRUE;
  825. break;
  826. }
  827. else {
  828. logmsg("Sent off %zd bytes", written);
  829. }
  830. /* write to file as well */
  831. fwrite(buffer, 1, (size_t)written, dump);
  832. if(got_exit_signal)
  833. break;
  834. count -= written;
  835. buffer += written;
  836. } while(count>0);
  837. /* Send out any RTP data */
  838. if(req->rtp_buffer) {
  839. logmsg("About to write %zu RTP bytes", req->rtp_buffersize);
  840. count = req->rtp_buffersize;
  841. do {
  842. size_t num = count;
  843. if(num > 200)
  844. num = 200;
  845. written = swrite(sock, req->rtp_buffer + (req->rtp_buffersize - count),
  846. num);
  847. if(written < 0) {
  848. sendfailure = TRUE;
  849. break;
  850. }
  851. count -= written;
  852. } while(count > 0);
  853. free(req->rtp_buffer);
  854. req->rtp_buffersize = 0;
  855. }
  856. do {
  857. res = fclose(dump);
  858. } while(res && ((error = errno) == EINTR));
  859. if(res)
  860. logmsg("Error closing file %s error: %d %s",
  861. RESPONSE_DUMP, error, strerror(error));
  862. if(got_exit_signal) {
  863. free(ptr);
  864. free(cmd);
  865. return -1;
  866. }
  867. if(sendfailure) {
  868. logmsg("Sending response failed. Only (%zu bytes) of "
  869. "(%zu bytes) were sent",
  870. responsesize-count, responsesize);
  871. free(ptr);
  872. free(cmd);
  873. return -1;
  874. }
  875. logmsg("Response sent (%zu bytes) and written to " RESPONSE_DUMP,
  876. responsesize);
  877. free(ptr);
  878. if(cmdsize > 0) {
  879. char command[32];
  880. int quarters;
  881. int num;
  882. ptr = cmd;
  883. do {
  884. if(2 == sscanf(ptr, "%31s %d", command, &num)) {
  885. if(!strcmp("wait", command)) {
  886. logmsg("Told to sleep for %d seconds", num);
  887. quarters = num * 4;
  888. while(quarters > 0) {
  889. quarters--;
  890. res = wait_ms(250);
  891. if(got_exit_signal)
  892. break;
  893. if(res) {
  894. /* should not happen */
  895. error = errno;
  896. logmsg("wait_ms() failed with error: (%d) %s",
  897. error, strerror(error));
  898. break;
  899. }
  900. }
  901. if(!quarters)
  902. logmsg("Continuing after sleeping %d seconds", num);
  903. }
  904. else
  905. logmsg("Unknown command in reply command section");
  906. }
  907. ptr = strchr(ptr, '\n');
  908. if(ptr)
  909. ptr++;
  910. else
  911. ptr = NULL;
  912. } while(ptr && *ptr);
  913. }
  914. free(cmd);
  915. req->open = persistent;
  916. prevtestno = req->testno;
  917. prevpartno = req->partno;
  918. return 0;
  919. }
  920. int main(int argc, char *argv[])
  921. {
  922. srvr_sockaddr_union_t me;
  923. curl_socket_t sock = CURL_SOCKET_BAD;
  924. curl_socket_t msgsock = CURL_SOCKET_BAD;
  925. int wrotepidfile = 0;
  926. int wroteportfile = 0;
  927. int flag;
  928. unsigned short port = DEFAULT_PORT;
  929. const char *pidname = ".rtsp.pid";
  930. const char *portname = NULL; /* none by default */
  931. struct httprequest req;
  932. int rc;
  933. int error;
  934. int arg = 1;
  935. memset(&req, 0, sizeof(req));
  936. while(argc>arg) {
  937. if(!strcmp("--version", argv[arg])) {
  938. printf("rtspd IPv4%s"
  939. "\n"
  940. ,
  941. #ifdef ENABLE_IPV6
  942. "/IPv6"
  943. #else
  944. ""
  945. #endif
  946. );
  947. return 0;
  948. }
  949. else if(!strcmp("--pidfile", argv[arg])) {
  950. arg++;
  951. if(argc>arg)
  952. pidname = argv[arg++];
  953. }
  954. else if(!strcmp("--portfile", argv[arg])) {
  955. arg++;
  956. if(argc>arg)
  957. portname = argv[arg++];
  958. }
  959. else if(!strcmp("--logfile", argv[arg])) {
  960. arg++;
  961. if(argc>arg)
  962. serverlogfile = argv[arg++];
  963. }
  964. else if(!strcmp("--ipv4", argv[arg])) {
  965. #ifdef ENABLE_IPV6
  966. ipv_inuse = "IPv4";
  967. use_ipv6 = FALSE;
  968. #endif
  969. arg++;
  970. }
  971. else if(!strcmp("--ipv6", argv[arg])) {
  972. #ifdef ENABLE_IPV6
  973. ipv_inuse = "IPv6";
  974. use_ipv6 = TRUE;
  975. #endif
  976. arg++;
  977. }
  978. else if(!strcmp("--port", argv[arg])) {
  979. arg++;
  980. if(argc>arg) {
  981. char *endptr;
  982. unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
  983. port = curlx_ultous(ulnum);
  984. arg++;
  985. }
  986. }
  987. else if(!strcmp("--srcdir", argv[arg])) {
  988. arg++;
  989. if(argc>arg) {
  990. path = argv[arg];
  991. arg++;
  992. }
  993. }
  994. else {
  995. puts("Usage: rtspd [option]\n"
  996. " --version\n"
  997. " --logfile [file]\n"
  998. " --pidfile [file]\n"
  999. " --portfile [file]\n"
  1000. " --ipv4\n"
  1001. " --ipv6\n"
  1002. " --port [port]\n"
  1003. " --srcdir [path]");
  1004. return 0;
  1005. }
  1006. }
  1007. #ifdef WIN32
  1008. win32_init();
  1009. atexit(win32_cleanup);
  1010. #endif
  1011. install_signal_handlers(false);
  1012. #ifdef ENABLE_IPV6
  1013. if(!use_ipv6)
  1014. #endif
  1015. sock = socket(AF_INET, SOCK_STREAM, 0);
  1016. #ifdef ENABLE_IPV6
  1017. else
  1018. sock = socket(AF_INET6, SOCK_STREAM, 0);
  1019. #endif
  1020. if(CURL_SOCKET_BAD == sock) {
  1021. error = SOCKERRNO;
  1022. logmsg("Error creating socket: (%d) %s",
  1023. error, strerror(error));
  1024. goto server_cleanup;
  1025. }
  1026. flag = 1;
  1027. if(0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  1028. (void *)&flag, sizeof(flag))) {
  1029. error = SOCKERRNO;
  1030. logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
  1031. error, strerror(error));
  1032. goto server_cleanup;
  1033. }
  1034. #ifdef ENABLE_IPV6
  1035. if(!use_ipv6) {
  1036. #endif
  1037. memset(&me.sa4, 0, sizeof(me.sa4));
  1038. me.sa4.sin_family = AF_INET;
  1039. me.sa4.sin_addr.s_addr = INADDR_ANY;
  1040. me.sa4.sin_port = htons(port);
  1041. rc = bind(sock, &me.sa, sizeof(me.sa4));
  1042. #ifdef ENABLE_IPV6
  1043. }
  1044. else {
  1045. memset(&me.sa6, 0, sizeof(me.sa6));
  1046. me.sa6.sin6_family = AF_INET6;
  1047. me.sa6.sin6_addr = in6addr_any;
  1048. me.sa6.sin6_port = htons(port);
  1049. rc = bind(sock, &me.sa, sizeof(me.sa6));
  1050. }
  1051. #endif /* ENABLE_IPV6 */
  1052. if(0 != rc) {
  1053. error = SOCKERRNO;
  1054. logmsg("Error binding socket on port %hu: (%d) %s",
  1055. port, error, strerror(error));
  1056. goto server_cleanup;
  1057. }
  1058. if(!port) {
  1059. /* The system was supposed to choose a port number, figure out which
  1060. port we actually got and update the listener port value with it. */
  1061. curl_socklen_t la_size;
  1062. srvr_sockaddr_union_t localaddr;
  1063. #ifdef ENABLE_IPV6
  1064. if(!use_ipv6)
  1065. #endif
  1066. la_size = sizeof(localaddr.sa4);
  1067. #ifdef ENABLE_IPV6
  1068. else
  1069. la_size = sizeof(localaddr.sa6);
  1070. #endif
  1071. memset(&localaddr.sa, 0, (size_t)la_size);
  1072. if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
  1073. error = SOCKERRNO;
  1074. logmsg("getsockname() failed with error: (%d) %s",
  1075. error, strerror(error));
  1076. sclose(sock);
  1077. goto server_cleanup;
  1078. }
  1079. switch(localaddr.sa.sa_family) {
  1080. case AF_INET:
  1081. port = ntohs(localaddr.sa4.sin_port);
  1082. break;
  1083. #ifdef ENABLE_IPV6
  1084. case AF_INET6:
  1085. port = ntohs(localaddr.sa6.sin6_port);
  1086. break;
  1087. #endif
  1088. default:
  1089. break;
  1090. }
  1091. if(!port) {
  1092. /* Real failure, listener port shall not be zero beyond this point. */
  1093. logmsg("Apparently getsockname() succeeded, with listener port zero.");
  1094. logmsg("A valid reason for this failure is a binary built without");
  1095. logmsg("proper network library linkage. This might not be the only");
  1096. logmsg("reason, but double check it before anything else.");
  1097. sclose(sock);
  1098. goto server_cleanup;
  1099. }
  1100. }
  1101. logmsg("Running %s version on port %d", ipv_inuse, (int)port);
  1102. /* start accepting connections */
  1103. rc = listen(sock, 5);
  1104. if(0 != rc) {
  1105. error = SOCKERRNO;
  1106. logmsg("listen() failed with error: (%d) %s",
  1107. error, strerror(error));
  1108. goto server_cleanup;
  1109. }
  1110. /*
  1111. ** As soon as this server writes its pid file the test harness will
  1112. ** attempt to connect to this server and initiate its verification.
  1113. */
  1114. wrotepidfile = write_pidfile(pidname);
  1115. if(!wrotepidfile)
  1116. goto server_cleanup;
  1117. if(portname) {
  1118. wroteportfile = write_portfile(portname, port);
  1119. if(!wroteportfile)
  1120. goto server_cleanup;
  1121. }
  1122. for(;;) {
  1123. msgsock = accept(sock, NULL, NULL);
  1124. if(got_exit_signal)
  1125. break;
  1126. if(CURL_SOCKET_BAD == msgsock) {
  1127. error = SOCKERRNO;
  1128. logmsg("MAJOR ERROR: accept() failed with error: (%d) %s",
  1129. error, strerror(error));
  1130. break;
  1131. }
  1132. /*
  1133. ** As soon as this server acepts a connection from the test harness it
  1134. ** must set the server logs advisor read lock to indicate that server
  1135. ** logs should not be read until this lock is removed by this server.
  1136. */
  1137. set_advisor_read_lock(SERVERLOGS_LOCK);
  1138. serverlogslocked = 1;
  1139. logmsg("====> Client connect");
  1140. #ifdef TCP_NODELAY
  1141. /*
  1142. * Disable the Nagle algorithm to make it easier to send out a large
  1143. * response in many small segments to torture the clients more.
  1144. */
  1145. flag = 1;
  1146. if(setsockopt(msgsock, IPPROTO_TCP, TCP_NODELAY,
  1147. (void *)&flag, sizeof(flag)) == -1) {
  1148. logmsg("====> TCP_NODELAY failed");
  1149. }
  1150. #endif
  1151. /* initialization of httprequest struct is done in get_request(), but due
  1152. to pipelining treatment the pipelining struct field must be initialized
  1153. previously to FALSE every time a new connection arrives. */
  1154. req.pipelining = FALSE;
  1155. do {
  1156. if(got_exit_signal)
  1157. break;
  1158. if(get_request(msgsock, &req))
  1159. /* non-zero means error, break out of loop */
  1160. break;
  1161. if(prevbounce) {
  1162. /* bounce treatment requested */
  1163. if((req.testno == prevtestno) &&
  1164. (req.partno == prevpartno)) {
  1165. req.partno++;
  1166. logmsg("BOUNCE part number to %ld", req.partno);
  1167. }
  1168. else {
  1169. prevbounce = FALSE;
  1170. prevtestno = -1;
  1171. prevpartno = -1;
  1172. }
  1173. }
  1174. send_doc(msgsock, &req);
  1175. if(got_exit_signal)
  1176. break;
  1177. if((req.testno < 0) && (req.testno != DOCNUMBER_CONNECT)) {
  1178. logmsg("special request received, no persistency");
  1179. break;
  1180. }
  1181. if(!req.open) {
  1182. logmsg("instructed to close connection after server-reply");
  1183. break;
  1184. }
  1185. if(req.open)
  1186. logmsg("=> persistent connection request ended, awaits new request");
  1187. /* if we got a CONNECT, loop and get another request as well! */
  1188. } while(req.open || (req.testno == DOCNUMBER_CONNECT));
  1189. if(got_exit_signal)
  1190. break;
  1191. logmsg("====> Client disconnect");
  1192. sclose(msgsock);
  1193. msgsock = CURL_SOCKET_BAD;
  1194. if(serverlogslocked) {
  1195. serverlogslocked = 0;
  1196. clear_advisor_read_lock(SERVERLOGS_LOCK);
  1197. }
  1198. if(req.testno == DOCNUMBER_QUIT)
  1199. break;
  1200. }
  1201. server_cleanup:
  1202. if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
  1203. sclose(msgsock);
  1204. if(sock != CURL_SOCKET_BAD)
  1205. sclose(sock);
  1206. if(got_exit_signal)
  1207. logmsg("signalled to die");
  1208. if(wrotepidfile)
  1209. unlink(pidname);
  1210. if(wroteportfile)
  1211. unlink(portname);
  1212. if(serverlogslocked) {
  1213. serverlogslocked = 0;
  1214. clear_advisor_read_lock(SERVERLOGS_LOCK);
  1215. }
  1216. restore_signal_handlers(false);
  1217. if(got_exit_signal) {
  1218. logmsg("========> %s rtspd (port: %d pid: %ld) exits with signal (%d)",
  1219. ipv_inuse, (int)port, (long)getpid(), exit_signal);
  1220. /*
  1221. * To properly set the return status of the process we
  1222. * must raise the same signal SIGINT or SIGTERM that we
  1223. * caught and let the old handler take care of it.
  1224. */
  1225. raise(exit_signal);
  1226. }
  1227. logmsg("========> rtspd quits");
  1228. return 0;
  1229. }