rtspd.c 42 KB

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