rtspd.c 40 KB

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