rtspd.c 41 KB

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