rtspd.c 39 KB

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