rtspd.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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) {
  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. req->reqbuf + req->offset > end + strlen(END_OF_HEADERS) &&
  482. (!strncmp(req->reqbuf, "GET", strlen("GET")) ||
  483. !strncmp(req->reqbuf, "HEAD", strlen("HEAD")))) {
  484. /* If we have a persistent connection, HTTP version >= 1.1
  485. and GET/HEAD request, enable pipelining. */
  486. req->checkindex = (end - req->reqbuf) + strlen(END_OF_HEADERS);
  487. req->pipelining = TRUE;
  488. }
  489. while(req->pipe) {
  490. if(got_exit_signal)
  491. return 1; /* done */
  492. /* scan for more header ends within this chunk */
  493. line = &req->reqbuf[req->checkindex];
  494. end = strstr(line, END_OF_HEADERS);
  495. if(!end)
  496. break;
  497. req->checkindex += (end - line) + strlen(END_OF_HEADERS);
  498. req->pipe--;
  499. }
  500. /* If authentication is required and no auth was provided, end now. This
  501. makes the server NOT wait for PUT/POST data and you can then make the
  502. test case send a rejection before any such data has been sent. Test case
  503. 154 uses this.*/
  504. if(req->auth_req && !req->auth)
  505. return 1; /* done */
  506. if(req->cl > 0) {
  507. if(req->cl <= req->offset - (end - req->reqbuf) - strlen(END_OF_HEADERS))
  508. return 1; /* done */
  509. else
  510. return 0; /* not complete yet */
  511. }
  512. return 1; /* done */
  513. }
  514. /* store the entire request in a file */
  515. static void storerequest(char *reqbuf, size_t totalsize)
  516. {
  517. int res;
  518. int error = 0;
  519. size_t written;
  520. size_t writeleft;
  521. FILE *dump;
  522. if(!reqbuf)
  523. return;
  524. if(totalsize == 0)
  525. return;
  526. do {
  527. dump = fopen(REQUEST_DUMP, "ab");
  528. } while(!dump && ((error = errno) == EINTR));
  529. if(!dump) {
  530. logmsg("Error opening file %s error: %d %s",
  531. REQUEST_DUMP, error, strerror(error));
  532. logmsg("Failed to write request input to " REQUEST_DUMP);
  533. return;
  534. }
  535. writeleft = totalsize;
  536. do {
  537. written = fwrite(&reqbuf[totalsize-writeleft],
  538. 1, writeleft, dump);
  539. if(got_exit_signal)
  540. goto storerequest_cleanup;
  541. if(written > 0)
  542. writeleft -= written;
  543. } while((writeleft > 0) && ((error = errno) == EINTR));
  544. if(writeleft == 0)
  545. logmsg("Wrote request (%zu bytes) input to " REQUEST_DUMP, totalsize);
  546. else if(writeleft > 0) {
  547. logmsg("Error writing file %s error: %d %s",
  548. REQUEST_DUMP, error, strerror(error));
  549. logmsg("Wrote only (%zu bytes) of (%zu bytes) request input to %s",
  550. totalsize-writeleft, totalsize, REQUEST_DUMP);
  551. }
  552. storerequest_cleanup:
  553. do {
  554. res = fclose(dump);
  555. } while(res && ((error = errno) == EINTR));
  556. if(res)
  557. logmsg("Error closing file %s error: %d %s",
  558. REQUEST_DUMP, error, strerror(error));
  559. }
  560. /* return 0 on success, non-zero on failure */
  561. static int get_request(curl_socket_t sock, struct httprequest *req)
  562. {
  563. int error;
  564. int fail = 0;
  565. int done_processing = 0;
  566. char *reqbuf = req->reqbuf;
  567. ssize_t got = 0;
  568. char *pipereq = NULL;
  569. size_t pipereq_length = 0;
  570. if(req->pipelining) {
  571. pipereq = reqbuf + req->checkindex;
  572. pipereq_length = req->offset - req->checkindex;
  573. }
  574. /*** Init the httprequest structure properly for the upcoming request ***/
  575. req->checkindex = 0;
  576. req->offset = 0;
  577. req->testno = DOCNUMBER_NOTHING;
  578. req->partno = 0;
  579. req->open = TRUE;
  580. req->auth_req = FALSE;
  581. req->auth = FALSE;
  582. req->cl = 0;
  583. req->digest = FALSE;
  584. req->ntlm = FALSE;
  585. req->pipe = 0;
  586. req->skip = 0;
  587. req->rcmd = RCMD_NORMALREQ;
  588. req->protocol = RPROT_NONE;
  589. req->prot_version = 0;
  590. req->pipelining = FALSE;
  591. req->rtp_buffer = NULL;
  592. req->rtp_buffersize = 0;
  593. /*** end of httprequest init ***/
  594. while(!done_processing && (req->offset < REQBUFSIZ-1)) {
  595. if(pipereq_length && pipereq) {
  596. memmove(reqbuf, pipereq, pipereq_length);
  597. got = curlx_uztosz(pipereq_length);
  598. pipereq_length = 0;
  599. }
  600. else {
  601. if(req->skip)
  602. /* we are instructed to not read the entire thing, so we make sure to
  603. only read what we're supposed to and NOT read the enire thing the
  604. client wants to send! */
  605. got = sread(sock, reqbuf + req->offset, req->cl);
  606. else
  607. got = sread(sock, reqbuf + req->offset, REQBUFSIZ-1 - req->offset);
  608. }
  609. if(got_exit_signal)
  610. return 1;
  611. if(got == 0) {
  612. logmsg("Connection closed by client");
  613. fail = 1;
  614. }
  615. else if(got < 0) {
  616. error = SOCKERRNO;
  617. logmsg("recv() returned error: (%d) %s", error, strerror(error));
  618. fail = 1;
  619. }
  620. if(fail) {
  621. /* dump the request received so far to the external file */
  622. reqbuf[req->offset] = '\0';
  623. storerequest(reqbuf, req->offset);
  624. return 1;
  625. }
  626. logmsg("Read %zd bytes", got);
  627. req->offset += (size_t)got;
  628. reqbuf[req->offset] = '\0';
  629. done_processing = ProcessRequest(req);
  630. if(got_exit_signal)
  631. return 1;
  632. if(done_processing && req->pipe) {
  633. logmsg("Waiting for another piped request");
  634. done_processing = 0;
  635. req->pipe--;
  636. }
  637. }
  638. if((req->offset == REQBUFSIZ-1) && (got > 0)) {
  639. logmsg("Request would overflow buffer, closing connection");
  640. /* dump request received so far to external file anyway */
  641. reqbuf[REQBUFSIZ-1] = '\0';
  642. fail = 1;
  643. }
  644. else if(req->offset > REQBUFSIZ-1) {
  645. logmsg("Request buffer overflow, closing connection");
  646. /* dump request received so far to external file anyway */
  647. reqbuf[REQBUFSIZ-1] = '\0';
  648. fail = 1;
  649. }
  650. else
  651. reqbuf[req->offset] = '\0';
  652. /* dump the request to an external file */
  653. storerequest(reqbuf, req->pipelining ? req->checkindex : req->offset);
  654. if(got_exit_signal)
  655. return 1;
  656. return fail; /* return 0 on success */
  657. }
  658. /* returns -1 on failure */
  659. static int send_doc(curl_socket_t sock, struct httprequest *req)
  660. {
  661. ssize_t written;
  662. size_t count;
  663. const char *buffer;
  664. char *ptr = NULL;
  665. char *cmd = NULL;
  666. size_t cmdsize = 0;
  667. FILE *dump;
  668. bool persistent = TRUE;
  669. bool sendfailure = FALSE;
  670. size_t responsesize;
  671. int error = 0;
  672. int res;
  673. static char weare[256];
  674. logmsg("Send response number %ld part %ld", req->testno, req->partno);
  675. switch(req->rcmd) {
  676. default:
  677. case RCMD_NORMALREQ:
  678. break; /* continue with business as usual */
  679. case RCMD_STREAM:
  680. #define STREAMTHIS "a string to stream 01234567890\n"
  681. count = strlen(STREAMTHIS);
  682. for(;;) {
  683. written = swrite(sock, STREAMTHIS, count);
  684. if(got_exit_signal)
  685. return -1;
  686. if(written != (ssize_t)count) {
  687. logmsg("Stopped streaming");
  688. break;
  689. }
  690. }
  691. return -1;
  692. case RCMD_IDLE:
  693. /* Do nothing. Sit idle. Pretend it rains. */
  694. return 0;
  695. }
  696. req->open = FALSE;
  697. if(req->testno < 0) {
  698. size_t msglen;
  699. char msgbuf[64];
  700. switch(req->testno) {
  701. case DOCNUMBER_QUIT:
  702. logmsg("Replying to QUIT");
  703. buffer = docquit;
  704. break;
  705. case DOCNUMBER_WERULEZ:
  706. /* we got a "friends?" question, reply back that we sure are */
  707. logmsg("Identifying ourselves as friends");
  708. msnprintf(msgbuf, sizeof(msgbuf), "RTSP_SERVER WE ROOLZ: %ld\r\n",
  709. (long)getpid());
  710. msglen = strlen(msgbuf);
  711. msnprintf(weare, sizeof(weare),
  712. "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
  713. msglen, msgbuf);
  714. buffer = weare;
  715. break;
  716. case DOCNUMBER_INTERNAL:
  717. logmsg("Bailing out due to internal error");
  718. return -1;
  719. case DOCNUMBER_CONNECT:
  720. logmsg("Replying to CONNECT");
  721. buffer = docconnect;
  722. break;
  723. case DOCNUMBER_BADCONNECT:
  724. logmsg("Replying to a bad CONNECT");
  725. buffer = docbadconnect;
  726. break;
  727. case DOCNUMBER_404:
  728. default:
  729. logmsg("Replying to with a 404");
  730. if(req->protocol == RPROT_HTTP) {
  731. buffer = doc404_HTTP;
  732. }
  733. else {
  734. buffer = doc404_RTSP;
  735. }
  736. break;
  737. }
  738. count = strlen(buffer);
  739. }
  740. else {
  741. FILE *stream = test2fopen(req->testno);
  742. char partbuf[80]="data";
  743. if(0 != req->partno)
  744. msnprintf(partbuf, sizeof(partbuf), "data%ld", req->partno);
  745. if(!stream) {
  746. error = errno;
  747. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  748. logmsg("Couldn't open test file");
  749. return 0;
  750. }
  751. else {
  752. error = getpart(&ptr, &count, "reply", partbuf, stream);
  753. fclose(stream);
  754. if(error) {
  755. logmsg("getpart() failed with error: %d", error);
  756. return 0;
  757. }
  758. buffer = ptr;
  759. }
  760. if(got_exit_signal) {
  761. free(ptr);
  762. return -1;
  763. }
  764. /* re-open the same file again */
  765. stream = test2fopen(req->testno);
  766. if(!stream) {
  767. error = errno;
  768. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  769. logmsg("Couldn't open test file");
  770. free(ptr);
  771. return 0;
  772. }
  773. else {
  774. /* get the custom server control "commands" */
  775. error = getpart(&cmd, &cmdsize, "reply", "postcmd", stream);
  776. fclose(stream);
  777. if(error) {
  778. logmsg("getpart() failed with error: %d", error);
  779. free(ptr);
  780. return 0;
  781. }
  782. }
  783. }
  784. if(got_exit_signal) {
  785. free(ptr);
  786. free(cmd);
  787. return -1;
  788. }
  789. /* If the word 'swsclose' is present anywhere in the reply chunk, the
  790. connection will be closed after the data has been sent to the requesting
  791. client... */
  792. if(strstr(buffer, "swsclose") || !count) {
  793. persistent = FALSE;
  794. logmsg("connection close instruction \"swsclose\" found in response");
  795. }
  796. if(strstr(buffer, "swsbounce")) {
  797. prevbounce = TRUE;
  798. logmsg("enable \"swsbounce\" in the next request");
  799. }
  800. else
  801. prevbounce = FALSE;
  802. dump = fopen(RESPONSE_DUMP, "ab");
  803. if(!dump) {
  804. error = errno;
  805. logmsg("fopen() failed with error: %d %s", error, strerror(error));
  806. logmsg("Error opening file: %s", RESPONSE_DUMP);
  807. logmsg("couldn't create logfile: " RESPONSE_DUMP);
  808. free(ptr);
  809. free(cmd);
  810. return -1;
  811. }
  812. responsesize = count;
  813. do {
  814. /* Ok, we send no more than 200 bytes at a time, just to make sure that
  815. larger chunks are split up so that the client will need to do multiple
  816. recv() calls to get it and thus we exercise that code better */
  817. size_t num = count;
  818. if(num > 200)
  819. num = 200;
  820. written = swrite(sock, buffer, num);
  821. if(written < 0) {
  822. sendfailure = TRUE;
  823. break;
  824. }
  825. else {
  826. logmsg("Sent off %zd bytes", written);
  827. }
  828. /* write to file as well */
  829. fwrite(buffer, 1, (size_t)written, dump);
  830. if(got_exit_signal)
  831. break;
  832. count -= written;
  833. buffer += written;
  834. } while(count>0);
  835. /* Send out any RTP data */
  836. if(req->rtp_buffer) {
  837. logmsg("About to write %zu RTP bytes", req->rtp_buffersize);
  838. count = req->rtp_buffersize;
  839. do {
  840. size_t num = count;
  841. if(num > 200)
  842. num = 200;
  843. written = swrite(sock, req->rtp_buffer + (req->rtp_buffersize - count),
  844. num);
  845. if(written < 0) {
  846. sendfailure = TRUE;
  847. break;
  848. }
  849. count -= written;
  850. } while(count > 0);
  851. free(req->rtp_buffer);
  852. req->rtp_buffersize = 0;
  853. }
  854. do {
  855. res = fclose(dump);
  856. } while(res && ((error = errno) == EINTR));
  857. if(res)
  858. logmsg("Error closing file %s error: %d %s",
  859. RESPONSE_DUMP, error, strerror(error));
  860. if(got_exit_signal) {
  861. free(ptr);
  862. free(cmd);
  863. return -1;
  864. }
  865. if(sendfailure) {
  866. logmsg("Sending response failed. Only (%zu bytes) of "
  867. "(%zu bytes) were sent",
  868. responsesize-count, responsesize);
  869. free(ptr);
  870. free(cmd);
  871. return -1;
  872. }
  873. logmsg("Response sent (%zu bytes) and written to " RESPONSE_DUMP,
  874. responsesize);
  875. free(ptr);
  876. if(cmdsize > 0) {
  877. char command[32];
  878. int quarters;
  879. int num;
  880. ptr = cmd;
  881. do {
  882. if(2 == sscanf(ptr, "%31s %d", command, &num)) {
  883. if(!strcmp("wait", command)) {
  884. logmsg("Told to sleep for %d seconds", num);
  885. quarters = num * 4;
  886. while(quarters > 0) {
  887. quarters--;
  888. res = wait_ms(250);
  889. if(got_exit_signal)
  890. break;
  891. if(res) {
  892. /* should not happen */
  893. error = errno;
  894. logmsg("wait_ms() failed with error: (%d) %s",
  895. error, strerror(error));
  896. break;
  897. }
  898. }
  899. if(!quarters)
  900. logmsg("Continuing after sleeping %d seconds", num);
  901. }
  902. else
  903. logmsg("Unknown command in reply command section");
  904. }
  905. ptr = strchr(ptr, '\n');
  906. if(ptr)
  907. ptr++;
  908. else
  909. ptr = NULL;
  910. } while(ptr && *ptr);
  911. }
  912. free(cmd);
  913. req->open = persistent;
  914. prevtestno = req->testno;
  915. prevpartno = req->partno;
  916. return 0;
  917. }
  918. int main(int argc, char *argv[])
  919. {
  920. srvr_sockaddr_union_t me;
  921. curl_socket_t sock = CURL_SOCKET_BAD;
  922. curl_socket_t msgsock = CURL_SOCKET_BAD;
  923. int wrotepidfile = 0;
  924. int flag;
  925. unsigned short port = DEFAULT_PORT;
  926. const char *pidname = ".rtsp.pid";
  927. const char *portfile = NULL;
  928. struct httprequest req;
  929. int rc;
  930. int error;
  931. int arg = 1;
  932. long pid;
  933. memset(&req, 0, sizeof(req));
  934. while(argc>arg) {
  935. if(!strcmp("--version", argv[arg])) {
  936. printf("rtspd IPv4%s"
  937. "\n"
  938. ,
  939. #ifdef ENABLE_IPV6
  940. "/IPv6"
  941. #else
  942. ""
  943. #endif
  944. );
  945. return 0;
  946. }
  947. else if(!strcmp("--pidfile", argv[arg])) {
  948. arg++;
  949. if(argc>arg)
  950. pidname = argv[arg++];
  951. }
  952. else if(!strcmp("--portfile", argv[arg])) {
  953. arg++;
  954. if(argc>arg)
  955. portfile = argv[arg++];
  956. }
  957. else if(!strcmp("--logfile", argv[arg])) {
  958. arg++;
  959. if(argc>arg)
  960. serverlogfile = argv[arg++];
  961. }
  962. else if(!strcmp("--ipv4", argv[arg])) {
  963. #ifdef ENABLE_IPV6
  964. ipv_inuse = "IPv4";
  965. use_ipv6 = FALSE;
  966. #endif
  967. arg++;
  968. }
  969. else if(!strcmp("--ipv6", argv[arg])) {
  970. #ifdef ENABLE_IPV6
  971. ipv_inuse = "IPv6";
  972. use_ipv6 = TRUE;
  973. #endif
  974. arg++;
  975. }
  976. else if(!strcmp("--port", argv[arg])) {
  977. arg++;
  978. if(argc>arg) {
  979. char *endptr;
  980. unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
  981. port = curlx_ultous(ulnum);
  982. arg++;
  983. }
  984. }
  985. else if(!strcmp("--srcdir", argv[arg])) {
  986. arg++;
  987. if(argc>arg) {
  988. path = argv[arg];
  989. arg++;
  990. }
  991. }
  992. else {
  993. puts("Usage: rtspd [option]\n"
  994. " --version\n"
  995. " --logfile [file]\n"
  996. " --pidfile [file]\n"
  997. " --portfile [file]\n"
  998. " --ipv4\n"
  999. " --ipv6\n"
  1000. " --port [port]\n"
  1001. " --srcdir [path]");
  1002. return 0;
  1003. }
  1004. }
  1005. #ifdef WIN32
  1006. win32_init();
  1007. atexit(win32_cleanup);
  1008. #endif
  1009. install_signal_handlers(false);
  1010. pid = (long)getpid();
  1011. #ifdef ENABLE_IPV6
  1012. if(!use_ipv6)
  1013. #endif
  1014. sock = socket(AF_INET, SOCK_STREAM, 0);
  1015. #ifdef ENABLE_IPV6
  1016. else
  1017. sock = socket(AF_INET6, SOCK_STREAM, 0);
  1018. #endif
  1019. if(CURL_SOCKET_BAD == sock) {
  1020. error = SOCKERRNO;
  1021. logmsg("Error creating socket: (%d) %s",
  1022. error, strerror(error));
  1023. goto server_cleanup;
  1024. }
  1025. flag = 1;
  1026. if(0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  1027. (void *)&flag, sizeof(flag))) {
  1028. error = SOCKERRNO;
  1029. logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
  1030. error, strerror(error));
  1031. goto server_cleanup;
  1032. }
  1033. #ifdef ENABLE_IPV6
  1034. if(!use_ipv6) {
  1035. #endif
  1036. memset(&me.sa4, 0, sizeof(me.sa4));
  1037. me.sa4.sin_family = AF_INET;
  1038. me.sa4.sin_addr.s_addr = INADDR_ANY;
  1039. me.sa4.sin_port = htons(port);
  1040. rc = bind(sock, &me.sa, sizeof(me.sa4));
  1041. #ifdef ENABLE_IPV6
  1042. }
  1043. else {
  1044. memset(&me.sa6, 0, sizeof(me.sa6));
  1045. me.sa6.sin6_family = AF_INET6;
  1046. me.sa6.sin6_addr = in6addr_any;
  1047. me.sa6.sin6_port = htons(port);
  1048. rc = bind(sock, &me.sa, sizeof(me.sa6));
  1049. }
  1050. #endif /* ENABLE_IPV6 */
  1051. if(0 != rc) {
  1052. error = SOCKERRNO;
  1053. logmsg("Error binding socket on port %hu: (%d) %s",
  1054. port, error, strerror(error));
  1055. goto server_cleanup;
  1056. }
  1057. if(!port) {
  1058. /* The system was supposed to choose a port number, figure out which
  1059. port we actually got and update the listener port value with it. */
  1060. curl_socklen_t la_size;
  1061. srvr_sockaddr_union_t localaddr;
  1062. #ifdef ENABLE_IPV6
  1063. if(!use_ipv6)
  1064. #endif
  1065. la_size = sizeof(localaddr.sa4);
  1066. #ifdef ENABLE_IPV6
  1067. else
  1068. la_size = sizeof(localaddr.sa6);
  1069. #endif
  1070. memset(&localaddr.sa, 0, (size_t)la_size);
  1071. if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
  1072. error = SOCKERRNO;
  1073. logmsg("getsockname() failed with error: (%d) %s",
  1074. error, strerror(error));
  1075. sclose(sock);
  1076. goto server_cleanup;
  1077. }
  1078. switch(localaddr.sa.sa_family) {
  1079. case AF_INET:
  1080. port = ntohs(localaddr.sa4.sin_port);
  1081. break;
  1082. #ifdef ENABLE_IPV6
  1083. case AF_INET6:
  1084. port = ntohs(localaddr.sa6.sin6_port);
  1085. break;
  1086. #endif
  1087. default:
  1088. break;
  1089. }
  1090. if(!port) {
  1091. /* Real failure, listener port shall not be zero beyond this point. */
  1092. logmsg("Apparently getsockname() succeeded, with listener port zero.");
  1093. logmsg("A valid reason for this failure is a binary built without");
  1094. logmsg("proper network library linkage. This might not be the only");
  1095. logmsg("reason, but double check it before anything else.");
  1096. sclose(sock);
  1097. goto server_cleanup;
  1098. }
  1099. }
  1100. logmsg("Running %s version on port %d", ipv_inuse, (int)port);
  1101. /* start accepting connections */
  1102. rc = listen(sock, 5);
  1103. if(0 != rc) {
  1104. error = SOCKERRNO;
  1105. logmsg("listen() failed with error: (%d) %s",
  1106. error, strerror(error));
  1107. goto server_cleanup;
  1108. }
  1109. /*
  1110. ** As soon as this server writes its pid file the test harness will
  1111. ** attempt to connect to this server and initiate its verification.
  1112. */
  1113. wrotepidfile = write_pidfile(pidname);
  1114. if(!wrotepidfile)
  1115. goto server_cleanup;
  1116. if(portfile) {
  1117. wrotepidfile = write_portfile(portfile, port);
  1118. if(!wrotepidfile)
  1119. goto server_cleanup;
  1120. }
  1121. for(;;) {
  1122. msgsock = accept(sock, NULL, NULL);
  1123. if(got_exit_signal)
  1124. break;
  1125. if(CURL_SOCKET_BAD == msgsock) {
  1126. error = SOCKERRNO;
  1127. logmsg("MAJOR ERROR: accept() failed with error: (%d) %s",
  1128. error, strerror(error));
  1129. break;
  1130. }
  1131. /*
  1132. ** As soon as this server acepts a connection from the test harness it
  1133. ** must set the server logs advisor read lock to indicate that server
  1134. ** logs should not be read until this lock is removed by this server.
  1135. */
  1136. set_advisor_read_lock(SERVERLOGS_LOCK);
  1137. serverlogslocked = 1;
  1138. logmsg("====> Client connect");
  1139. #ifdef TCP_NODELAY
  1140. /*
  1141. * Disable the Nagle algorithm to make it easier to send out a large
  1142. * response in many small segments to torture the clients more.
  1143. */
  1144. flag = 1;
  1145. if(setsockopt(msgsock, IPPROTO_TCP, TCP_NODELAY,
  1146. (void *)&flag, sizeof(flag)) == -1) {
  1147. logmsg("====> TCP_NODELAY failed");
  1148. }
  1149. #endif
  1150. /* initialization of httprequest struct is done in get_request(), but due
  1151. to pipelining treatment the pipelining struct field must be initialized
  1152. previously to FALSE every time a new connection arrives. */
  1153. req.pipelining = FALSE;
  1154. do {
  1155. if(got_exit_signal)
  1156. break;
  1157. if(get_request(msgsock, &req))
  1158. /* non-zero means error, break out of loop */
  1159. break;
  1160. if(prevbounce) {
  1161. /* bounce treatment requested */
  1162. if((req.testno == prevtestno) &&
  1163. (req.partno == prevpartno)) {
  1164. req.partno++;
  1165. logmsg("BOUNCE part number to %ld", req.partno);
  1166. }
  1167. else {
  1168. prevbounce = FALSE;
  1169. prevtestno = -1;
  1170. prevpartno = -1;
  1171. }
  1172. }
  1173. send_doc(msgsock, &req);
  1174. if(got_exit_signal)
  1175. break;
  1176. if((req.testno < 0) && (req.testno != DOCNUMBER_CONNECT)) {
  1177. logmsg("special request received, no persistency");
  1178. break;
  1179. }
  1180. if(!req.open) {
  1181. logmsg("instructed to close connection after server-reply");
  1182. break;
  1183. }
  1184. if(req.open)
  1185. logmsg("=> persistent connection request ended, awaits new request");
  1186. /* if we got a CONNECT, loop and get another request as well! */
  1187. } while(req.open || (req.testno == DOCNUMBER_CONNECT));
  1188. if(got_exit_signal)
  1189. break;
  1190. logmsg("====> Client disconnect");
  1191. sclose(msgsock);
  1192. msgsock = CURL_SOCKET_BAD;
  1193. if(serverlogslocked) {
  1194. serverlogslocked = 0;
  1195. clear_advisor_read_lock(SERVERLOGS_LOCK);
  1196. }
  1197. if(req.testno == DOCNUMBER_QUIT)
  1198. break;
  1199. }
  1200. server_cleanup:
  1201. if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
  1202. sclose(msgsock);
  1203. if(sock != CURL_SOCKET_BAD)
  1204. sclose(sock);
  1205. if(got_exit_signal)
  1206. logmsg("signalled to die");
  1207. if(wrotepidfile)
  1208. unlink(pidname);
  1209. if(serverlogslocked) {
  1210. serverlogslocked = 0;
  1211. clear_advisor_read_lock(SERVERLOGS_LOCK);
  1212. }
  1213. restore_signal_handlers(false);
  1214. if(got_exit_signal) {
  1215. logmsg("========> %s rtspd (port: %d pid: %ld) exits with signal (%d)",
  1216. ipv_inuse, (int)port, pid, exit_signal);
  1217. /*
  1218. * To properly set the return status of the process we
  1219. * must raise the same signal SIGINT or SIGTERM that we
  1220. * caught and let the old handler take care of it.
  1221. */
  1222. raise(exit_signal);
  1223. }
  1224. logmsg("========> rtspd quits");
  1225. return 0;
  1226. }