rtspd.c 40 KB

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