1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
22 ***************************************************************************/
25 * curl's test suite Real Time Streaming Protocol (RTSP) server.
27 * This source file was started based on curl's HTTP test suite server.
30 #define CURL_NO_OLDIES
32 #include "setup.h" /* portability help from the lib directory */
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
43 #ifdef HAVE_NETINET_IN_H
44 #include <netinet/in.h>
46 #ifdef HAVE_ARPA_INET_H
47 #include <arpa/inet.h>
52 #ifdef HAVE_NETINET_TCP_H
53 #include <netinet/tcp.h> /* for TCP_NODELAY */
56 #define ENABLE_CURLX_PRINTF
57 /* make the curlx header define all printf() functions to use the curlx_*
59 #include "curlx.h" /* from the private lib dir */
63 /* include memdebug.h last */
67 static bool use_ipv6 = FALSE;
69 static const char *ipv_inuse = "IPv4";
70 static int serverlogslocked = 0;
72 #define REQBUFSIZ 150000
73 #define REQBUFSIZ_TXT "149999"
75 static long prevtestno=-1; /* previous test number we served */
76 static long prevpartno=-1; /* previous part number we served */
77 static bool prevbounce=FALSE; /* instructs the server to increase the part
78 number for a test in case the identical
79 testno+partno request shows up again */
81 #define RCMD_NORMALREQ 0 /* default request, use the tests file normally */
82 #define RCMD_IDLE 1 /* told to sit idle */
83 #define RCMD_STREAM 2 /* told to stream */
92 char reqbuf[REQBUFSIZ]; /* buffer area for the incoming request */
93 size_t checkindex; /* where to start checking of the request */
94 size_t offset; /* size of the incoming request */
95 long testno; /* test number found in the request */
96 long partno; /* part number found in the request */
97 bool open; /* keep connection open info, as found in the request */
98 bool auth_req; /* authentication required, don't wait for body unless
99 there's an Authorization header */
100 bool auth; /* Authorization header present in the incoming request */
101 size_t cl; /* Content-Length of the incoming request */
102 bool digest; /* Authorization digest header found */
103 bool ntlm; /* Authorization ntlm header found */
104 int pipe; /* if non-zero, expect this many requests to do a "piped"
106 int skip; /* if non-zero, the server is instructed to not read this
107 many bytes from a PUT/POST request. Ie the client sends N
108 bytes said in Content-Length, but the server only reads N
110 int rcmd; /* doing a special command, see defines above */
111 reqprot_t protocol; /* request protocol, HTTP or RTSP */
112 int prot_version; /* HTTP or RTSP version (major*10 + minor) */
113 bool pipelining; /* true if request is pipelined */
115 size_t rtp_buffersize;
118 static int ProcessRequest(struct httprequest *req);
119 static void storerequest(char *reqbuf, size_t totalsize);
121 #define DEFAULT_PORT 8999
123 #ifndef DEFAULT_LOGFILE
124 #define DEFAULT_LOGFILE "log/rtspd.log"
127 const char *serverlogfile = DEFAULT_LOGFILE;
129 #define RTSPDVERSION "cURL test suite RTSP server/0.1"
131 #define REQUEST_DUMP "log/server.input"
132 #define RESPONSE_DUMP "log/server.response"
134 /* very-big-path support */
135 #define MAXDOCNAMELEN 140000
136 #define MAXDOCNAMELEN_TXT "139999"
138 #define REQUEST_KEYWORD_SIZE 256
139 #define REQUEST_KEYWORD_SIZE_TXT "255"
141 #define CMD_AUTH_REQUIRED "auth_required"
143 /* 'idle' means that it will accept the request fine but never respond
144 any data. Just keep the connection alive. */
145 #define CMD_IDLE "idle"
147 /* 'stream' means to send a never-ending stream of data */
148 #define CMD_STREAM "stream"
150 #define END_OF_HEADERS "\r\n\r\n"
153 DOCNUMBER_NOTHING = -7,
155 DOCNUMBER_BADCONNECT = -5,
156 DOCNUMBER_INTERNAL= -4,
157 DOCNUMBER_CONNECT = -3,
158 DOCNUMBER_WERULEZ = -2,
163 /* sent as reply to a QUIT */
164 static const char *docquit =
165 "HTTP/1.1 200 Goodbye" END_OF_HEADERS;
167 /* sent as reply to a CONNECT */
168 static const char *docconnect =
169 "HTTP/1.1 200 Mighty fine indeed" END_OF_HEADERS;
171 /* sent as reply to a "bad" CONNECT */
172 static const char *docbadconnect =
173 "HTTP/1.1 501 Forbidden you fool" END_OF_HEADERS;
175 /* send back this on HTTP 404 file not found */
176 static const char *doc404_HTTP = "HTTP/1.1 404 Not Found\r\n"
177 "Server: " RTSPDVERSION "\r\n"
178 "Connection: close\r\n"
179 "Content-Type: text/html"
181 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
183 "<TITLE>404 Not Found</TITLE>\n"
185 "<H1>Not Found</H1>\n"
186 "The requested URL was not found on this server.\n"
187 "<P><HR><ADDRESS>" RTSPDVERSION "</ADDRESS>\n" "</BODY></HTML>\n";
189 /* send back this on RTSP 404 file not found */
190 static const char *doc404_RTSP = "RTSP/1.0 404 Not Found\r\n"
191 "Server: " RTSPDVERSION "\r\n"
194 /* Default size to send away fake RTP data */
195 #define RTP_DATA_SIZE 12
196 static const char *RTP_DATA = "$_1234\n\0asdf";
198 /* do-nothing macro replacement for systems which lack siginterrupt() */
200 #ifndef HAVE_SIGINTERRUPT
201 #define siginterrupt(x,y) do {} while(0)
204 /* vars used to keep around previous signal handlers */
206 typedef RETSIGTYPE (*SIGHANDLER_T)(int);
209 static SIGHANDLER_T old_sighup_handler = SIG_ERR;
213 static SIGHANDLER_T old_sigpipe_handler = SIG_ERR;
217 static SIGHANDLER_T old_sigalrm_handler = SIG_ERR;
221 static SIGHANDLER_T old_sigint_handler = SIG_ERR;
225 static SIGHANDLER_T old_sigterm_handler = SIG_ERR;
228 /* var which if set indicates that the program should finish execution */
230 SIG_ATOMIC_T got_exit_signal = 0;
232 /* if next is set indicates the first signal handled in exit_signal_handler */
234 static volatile int exit_signal = 0;
236 /* signal handler that will be triggered to indicate that the program
237 should finish its execution in a controlled manner as soon as possible.
238 The first time this is called it will set got_exit_signal to one and
239 store in exit_signal the signal that triggered its execution. */
241 static RETSIGTYPE exit_signal_handler(int signum)
243 int old_errno = ERRNO;
244 if(got_exit_signal == 0) {
246 exit_signal = signum;
248 (void)signal(signum, exit_signal_handler);
249 SET_ERRNO(old_errno);
252 static void install_signal_handlers(void)
255 /* ignore SIGHUP signal */
256 if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
257 logmsg("cannot install SIGHUP handler: %s", strerror(ERRNO));
260 /* ignore SIGPIPE signal */
261 if((old_sigpipe_handler = signal(SIGPIPE, SIG_IGN)) == SIG_ERR)
262 logmsg("cannot install SIGPIPE handler: %s", strerror(ERRNO));
265 /* ignore SIGALRM signal */
266 if((old_sigalrm_handler = signal(SIGALRM, SIG_IGN)) == SIG_ERR)
267 logmsg("cannot install SIGALRM handler: %s", strerror(ERRNO));
270 /* handle SIGINT signal with our exit_signal_handler */
271 if((old_sigint_handler = signal(SIGINT, exit_signal_handler)) == SIG_ERR)
272 logmsg("cannot install SIGINT handler: %s", strerror(ERRNO));
274 siginterrupt(SIGINT, 1);
277 /* handle SIGTERM signal with our exit_signal_handler */
278 if((old_sigterm_handler = signal(SIGTERM, exit_signal_handler)) == SIG_ERR)
279 logmsg("cannot install SIGTERM handler: %s", strerror(ERRNO));
281 siginterrupt(SIGTERM, 1);
285 static void restore_signal_handlers(void)
288 if(SIG_ERR != old_sighup_handler)
289 (void)signal(SIGHUP, old_sighup_handler);
292 if(SIG_ERR != old_sigpipe_handler)
293 (void)signal(SIGPIPE, old_sigpipe_handler);
296 if(SIG_ERR != old_sigalrm_handler)
297 (void)signal(SIGALRM, old_sigalrm_handler);
300 if(SIG_ERR != old_sigint_handler)
301 (void)signal(SIGINT, old_sigint_handler);
304 if(SIG_ERR != old_sigterm_handler)
305 (void)signal(SIGTERM, old_sigterm_handler);
309 static int ProcessRequest(struct httprequest *req)
311 char *line=&req->reqbuf[req->checkindex];
312 bool chunked = FALSE;
313 static char request[REQUEST_KEYWORD_SIZE];
314 static char doc[MAXDOCNAMELEN];
315 static char prot_str[5];
317 int prot_major, prot_minor;
320 end = strstr(line, END_OF_HEADERS);
322 logmsg("ProcessRequest() called with testno %ld and line [%s]",
325 /* try to figure out the request characteristics as soon as possible, but
327 if((req->testno == DOCNUMBER_NOTHING) &&
329 "%" REQUEST_KEYWORD_SIZE_TXT"s %" MAXDOCNAMELEN_TXT "s %4s/%d.%d",
337 if(!strcmp(prot_str, "HTTP")) {
338 req->protocol = RPROT_HTTP;
340 else if(!strcmp(prot_str, "RTSP")) {
341 req->protocol = RPROT_RTSP;
344 req->protocol = RPROT_NONE;
345 logmsg("got unknown protocol %s", prot_str);
349 req->prot_version = prot_major*10 + prot_minor;
351 /* find the last slash */
352 ptr = strrchr(doc, '/');
354 /* get the number after it */
359 if((strlen(doc) + strlen(request)) < 200)
360 sprintf(logbuf, "Got request: %s %s %s/%d.%d",
361 request, doc, prot_str, prot_major, prot_minor);
363 sprintf(logbuf, "Got a *HUGE* request %s/%d.%d",
364 prot_str, prot_major, prot_minor);
365 logmsg("%s", logbuf);
367 if(!strncmp("/verifiedserver", ptr, 15)) {
368 logmsg("Are-we-friendly question received");
369 req->testno = DOCNUMBER_WERULEZ;
373 if(!strncmp("/quit", ptr, 5)) {
374 logmsg("Request-to-quit received");
375 req->testno = DOCNUMBER_QUIT;
379 ptr++; /* skip the slash */
381 /* skip all non-numericals following the slash */
382 while(*ptr && !ISDIGIT(*ptr))
385 req->testno = strtol(ptr, &ptr, 10);
387 if(req->testno > 10000) {
388 req->partno = req->testno % 10000;
389 req->testno /= 10000;
394 sprintf(logbuf, "Requested test number %ld part %ld",
395 req->testno, req->partno);
396 logmsg("%s", logbuf);
398 filename = test2file(req->testno);
400 stream=fopen(filename, "rb");
403 logmsg("fopen() failed with error: %d %s", error, strerror(error));
404 logmsg("Error opening file: %s", filename);
405 logmsg("Couldn't open test file %ld", req->testno);
406 req->open = FALSE; /* closes connection */
418 char *rtp_scratch = NULL;
420 /* get the custom server control "commands" */
421 cmd = (char *)spitout(stream, "reply", "servercmd", &cmdsize);
426 logmsg("Found a reply-servercmd section!");
428 if(!strncmp(CMD_AUTH_REQUIRED, ptr, strlen(CMD_AUTH_REQUIRED))) {
429 logmsg("instructed to require authorization header");
430 req->auth_req = TRUE;
432 else if(!strncmp(CMD_IDLE, ptr, strlen(CMD_IDLE))) {
433 logmsg("instructed to idle");
434 req->rcmd = RCMD_IDLE;
437 else if(!strncmp(CMD_STREAM, ptr, strlen(CMD_STREAM))) {
438 logmsg("instructed to stream");
439 req->rcmd = RCMD_STREAM;
441 else if(1 == sscanf(ptr, "pipe: %d", &num)) {
442 logmsg("instructed to allow a pipe size of %d", num);
444 logmsg("negative pipe size ignored");
446 req->pipe = num-1; /* decrease by one since we don't count the
447 first request in this number */
449 else if(1 == sscanf(ptr, "skip: %d", &num)) {
450 logmsg("instructed to skip this number of bytes %d", num);
453 else if(3 == sscanf(ptr, "rtp: part %d channel %d size %d",
454 &rtp_partno, &rtp_channel, &rtp_size)) {
456 if(rtp_partno == req->partno) {
457 logmsg("RTP: part %d channel %d size %d",
458 rtp_partno, rtp_channel, rtp_size);
460 /* Make our scratch buffer enough to fit all the
461 * desired data and one for padding */
462 rtp_scratch = malloc(rtp_size + 4 + RTP_DATA_SIZE);
464 /* RTP is signalled with a $ */
465 rtp_scratch[0] = '$';
467 /* The channel follows and is one byte */
468 rtp_scratch[1] = (char)(rtp_channel & 0xFF);
470 /* Length follows and is a two byte short in network order */
471 *((unsigned short *)(&rtp_scratch[2])) =
472 htons((unsigned short)rtp_size);
474 /* Fill it with junk data */
475 for(i = 0; i < rtp_size; i+= RTP_DATA_SIZE) {
476 memcpy(rtp_scratch + 4 + i, RTP_DATA, RTP_DATA_SIZE);
479 if(req->rtp_buffer == NULL) {
480 req->rtp_buffer = rtp_scratch;
481 req->rtp_buffersize = rtp_size + 4;
483 req->rtp_buffer = realloc(req->rtp_buffer, req->rtp_buffersize + rtp_size + 4);
484 memcpy(req->rtp_buffer + req->rtp_buffersize, rtp_scratch, rtp_size + 4);
485 req->rtp_buffersize += rtp_size + 4;
488 logmsg("rtp_buffersize is %zu, rtp_size is %d.", req->rtp_buffersize, rtp_size);
493 logmsg("funny instruction found: %s", ptr);
496 ptr = strchr(ptr, '\n');
501 } while(ptr && *ptr);
502 logmsg("Done parsing server commands");
507 if(sscanf(req->reqbuf, "CONNECT %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
508 doc, &prot_major, &prot_minor) == 3) {
509 sprintf(logbuf, "Received a CONNECT %s HTTP/%d.%d request",
510 doc, prot_major, prot_minor);
511 logmsg("%s", logbuf);
513 if(req->prot_version == 10)
514 req->open = FALSE; /* HTTP 1.0 closes connection by default */
516 if(!strncmp(doc, "bad", 3))
517 /* if the host name starts with bad, we fake an error here */
518 req->testno = DOCNUMBER_BADCONNECT;
519 else if(!strncmp(doc, "test", 4)) {
520 /* if the host name starts with test, the port number used in the
521 CONNECT line will be used as test number! */
522 char *portp = strchr(doc, ':');
524 req->testno = atoi(portp+1);
526 req->testno = DOCNUMBER_CONNECT;
529 req->testno = DOCNUMBER_CONNECT;
532 logmsg("Did not find test number in PATH");
533 req->testno = DOCNUMBER_404;
539 /* we don't have a complete request yet! */
540 logmsg("ProcessRequest returned without a complete request");
541 return 0; /* not complete yet */
543 logmsg("ProcessRequest found a complete request");
546 /* we do have a full set, advance the checkindex to after the end of the
547 headers, for the pipelining case mostly */
548 req->checkindex += (end - line) + strlen(END_OF_HEADERS);
550 /* **** Persistence ****
552 * If the request is a HTTP/1.0 one, we close the connection unconditionally
555 * If the request is a HTTP/1.1 one, we MUST check for a "Connection:"
556 * header that might say "close". If it does, we close a connection when
557 * this request is processed. Otherwise, we keep the connection alive for X
565 if((req->cl==0) && curlx_strnequal("Content-Length:", line, 15)) {
566 /* If we don't ignore content-length, we read it and we read the whole
567 request including the body before we return. If we've been told to
568 ignore the content-length, we will return as soon as all headers
569 have been received */
570 size_t cl = strtol(line+15, &line, 10);
571 req->cl = cl - req->skip;
573 logmsg("Found Content-Length: %zu in the request", cl);
575 logmsg("... but will abort after %zu bytes", req->cl);
578 else if(curlx_strnequal("Transfer-Encoding: chunked", line,
579 strlen("Transfer-Encoding: chunked"))) {
580 /* chunked data coming in */
585 if(strstr(req->reqbuf, "\r\n0\r\n\r\n"))
586 /* end of chunks reached */
589 return 0; /* not done */
592 line = strchr(line, '\n');
598 if(!req->auth && strstr(req->reqbuf, "Authorization:")) {
599 req->auth = TRUE; /* Authorization: header present! */
601 logmsg("Authorization header found, as required");
604 if(!req->digest && strstr(req->reqbuf, "Authorization: Digest")) {
605 /* If the client is passing this Digest-header, we set the part number
606 to 1000. Not only to spice up the complexity of this, but to make
607 Digest stuff to work in the test suite. */
609 req->digest = TRUE; /* header found */
610 logmsg("Received Digest request, sending back data %ld", req->partno);
612 else if(!req->ntlm &&
613 strstr(req->reqbuf, "Authorization: NTLM TlRMTVNTUAAD")) {
614 /* If the client is passing this type-3 NTLM header */
616 req->ntlm = TRUE; /* NTLM found */
617 logmsg("Received NTLM type-3, sending back data %ld", req->partno);
619 logmsg(" Expecting %zu POSTed bytes", req->cl);
622 else if(!req->ntlm &&
623 strstr(req->reqbuf, "Authorization: NTLM TlRMTVNTUAAB")) {
624 /* If the client is passing this type-1 NTLM header */
626 req->ntlm = TRUE; /* NTLM found */
627 logmsg("Received NTLM type-1, sending back data %ld", req->partno);
629 else if((req->partno >= 1000) && strstr(req->reqbuf, "Authorization: Basic")) {
630 /* If the client is passing this Basic-header and the part number is already
631 >=1000, we add 1 to the part number. This allows simple Basic authentication
632 negotiation to work in the test suite. */
634 logmsg("Received Basic request, sending back data %ld", req->partno);
636 if(strstr(req->reqbuf, "Connection: close"))
637 req->open = FALSE; /* close connection after this request */
641 req->prot_version >= 11 &&
643 req->reqbuf + req->offset > end + strlen(END_OF_HEADERS) &&
644 (!strncmp(req->reqbuf, "GET", strlen("GET")) ||
645 !strncmp(req->reqbuf, "HEAD", strlen("HEAD")))) {
646 /* If we have a persistent connection, HTTP version >= 1.1
647 and GET/HEAD request, enable pipelining. */
648 req->checkindex = (end - req->reqbuf) + strlen(END_OF_HEADERS);
649 req->pipelining = TRUE;
655 /* scan for more header ends within this chunk */
656 line = &req->reqbuf[req->checkindex];
657 end = strstr(line, END_OF_HEADERS);
660 req->checkindex += (end - line) + strlen(END_OF_HEADERS);
664 /* If authentication is required and no auth was provided, end now. This
665 makes the server NOT wait for PUT/POST data and you can then make the
666 test case send a rejection before any such data has been sent. Test case
668 if(req->auth_req && !req->auth)
672 if(req->cl <= req->offset - (end - req->reqbuf) - strlen(END_OF_HEADERS))
675 return 0; /* not complete yet */
681 /* store the entire request in a file */
682 static void storerequest(char *reqbuf, size_t totalsize)
696 dump = fopen(REQUEST_DUMP, "ab");
697 } while ((dump == NULL) && ((error = ERRNO) == EINTR));
699 logmsg("Error opening file %s error: %d %s",
700 REQUEST_DUMP, error, strerror(error));
701 logmsg("Failed to write request input to " REQUEST_DUMP);
705 writeleft = totalsize;
707 written = fwrite(&reqbuf[totalsize-writeleft],
710 goto storerequest_cleanup;
712 writeleft -= written;
713 } while ((writeleft > 0) && ((error = ERRNO) == EINTR));
716 logmsg("Wrote request (%zu bytes) input to " REQUEST_DUMP, totalsize);
717 else if(writeleft > 0) {
718 logmsg("Error writing file %s error: %d %s",
719 REQUEST_DUMP, error, strerror(error));
720 logmsg("Wrote only (%zu bytes) of (%zu bytes) request input to %s",
721 totalsize-writeleft, totalsize, REQUEST_DUMP);
724 storerequest_cleanup:
728 } while(res && ((error = ERRNO) == EINTR));
730 logmsg("Error closing file %s error: %d %s",
731 REQUEST_DUMP, error, strerror(error));
734 /* return 0 on success, non-zero on failure */
735 static int get_request(curl_socket_t sock, struct httprequest *req)
739 int done_processing = 0;
740 char *reqbuf = req->reqbuf;
743 char *pipereq = NULL;
744 size_t pipereq_length = 0;
746 if(req->pipelining) {
747 pipereq = reqbuf + req->checkindex;
748 pipereq_length = req->offset - req->checkindex;
751 /*** Init the httprequest structure properly for the upcoming request ***/
755 req->testno = DOCNUMBER_NOTHING;
758 req->auth_req = FALSE;
765 req->rcmd = RCMD_NORMALREQ;
766 req->protocol = RPROT_NONE;
767 req->prot_version = 0;
768 req->pipelining = FALSE;
769 req->rtp_buffer = NULL;
770 req->rtp_buffersize = 0;
772 /*** end of httprequest init ***/
774 while(!done_processing && (req->offset < REQBUFSIZ-1)) {
775 if(pipereq_length && pipereq) {
776 memmove(reqbuf, pipereq, pipereq_length);
777 got = pipereq_length;
782 /* we are instructed to not read the entire thing, so we make sure to only
783 read what we're supposed to and NOT read the enire thing the client
785 got = sread(sock, reqbuf + req->offset, req->cl);
787 got = sread(sock, reqbuf + req->offset, REQBUFSIZ-1 - req->offset);
792 logmsg("Connection closed by client");
797 logmsg("recv() returned error: (%d) %s", error, strerror(error));
801 /* dump the request received so far to the external file */
802 reqbuf[req->offset] = '\0';
803 storerequest(reqbuf, req->offset);
807 logmsg("Read %zd bytes", got);
809 req->offset += (size_t)got;
810 reqbuf[req->offset] = '\0';
812 done_processing = ProcessRequest(req);
815 if(done_processing && req->pipe) {
816 logmsg("Waiting for another piped request");
822 if((req->offset == REQBUFSIZ-1) && (got > 0)) {
823 logmsg("Request would overflow buffer, closing connection");
824 /* dump request received so far to external file anyway */
825 reqbuf[REQBUFSIZ-1] = '\0';
828 else if(req->offset > REQBUFSIZ-1) {
829 logmsg("Request buffer overflow, closing connection");
830 /* dump request received so far to external file anyway */
831 reqbuf[REQBUFSIZ-1] = '\0';
835 reqbuf[req->offset] = '\0';
837 /* dump the request to an external file */
838 storerequest(reqbuf, req->pipelining ? req->checkindex : req->offset);
842 return fail; /* return 0 on success */
845 /* returns -1 on failure */
846 static int send_doc(curl_socket_t sock, struct httprequest *req)
856 bool persistant = TRUE;
857 bool sendfailure = FALSE;
862 static char weare[256];
864 char partbuf[80]="data";
866 logmsg("Send response number %ld part %ld", req->testno, req->partno);
871 break; /* continue with business as usual */
873 #define STREAMTHIS "a string to stream 01234567890\n"
874 count = strlen(STREAMTHIS);
876 written = swrite(sock, STREAMTHIS, count);
879 if(written != (ssize_t)count) {
880 logmsg("Stopped streaming");
886 /* Do nothing. Sit idle. Pretend it rains. */
892 if(req->testno < 0) {
896 switch(req->testno) {
898 logmsg("Replying to QUIT");
901 case DOCNUMBER_WERULEZ:
902 /* we got a "friends?" question, reply back that we sure are */
903 logmsg("Identifying ourselves as friends");
904 sprintf(msgbuf, "RTSP_SERVER WE ROOLZ: %ld\r\n", (long)getpid());
905 msglen = strlen(msgbuf);
906 sprintf(weare, "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
910 case DOCNUMBER_INTERNAL:
911 logmsg("Bailing out due to internal error");
913 case DOCNUMBER_CONNECT:
914 logmsg("Replying to CONNECT");
917 case DOCNUMBER_BADCONNECT:
918 logmsg("Replying to a bad CONNECT");
919 buffer = docbadconnect;
923 logmsg("Replying to with a 404");
924 if(req->protocol == RPROT_HTTP) {
925 buffer = doc404_HTTP;
927 buffer = doc404_RTSP;
932 count = strlen(buffer);
935 char *filename = test2file(req->testno);
938 sprintf(partbuf, "data%ld", req->partno);
940 stream=fopen(filename, "rb");
943 logmsg("fopen() failed with error: %d %s", error, strerror(error));
944 logmsg("Error opening file: %s", filename);
945 logmsg("Couldn't open test file");
949 buffer = spitout(stream, "reply", partbuf, &count);
950 ptr = (char *)buffer;
957 /* re-open the same file again */
958 stream=fopen(filename, "rb");
961 logmsg("fopen() failed with error: %d %s", error, strerror(error));
962 logmsg("Error opening file: %s", filename);
963 logmsg("Couldn't open test file");
967 /* get the custom server control "commands" */
968 cmd = (char *)spitout(stream, "reply", "postcmd", &cmdsize);
976 /* If the word 'swsclose' is present anywhere in the reply chunk, the
977 connection will be closed after the data has been sent to the requesting
979 if(strstr(buffer, "swsclose") || !count) {
981 logmsg("connection close instruction \"swsclose\" found in response");
983 if(strstr(buffer, "swsbounce")) {
985 logmsg("enable \"swsbounce\" in the next request");
990 dump = fopen(RESPONSE_DUMP, "ab");
993 logmsg("fopen() failed with error: %d %s", error, strerror(error));
994 logmsg("Error opening file: %s", RESPONSE_DUMP);
995 logmsg("couldn't create logfile: " RESPONSE_DUMP);
999 responsesize = count;
1001 /* Ok, we send no more than 200 bytes at a time, just to make sure that
1002 larger chunks are split up so that the client will need to do multiple
1003 recv() calls to get it and thus we exercise that code better */
1007 written = swrite(sock, buffer, num);
1013 logmsg("Sent off %zd bytes", written);
1015 /* write to file as well */
1016 fwrite(buffer, 1, (size_t)written, dump);
1024 /* Send out any RTP data */
1025 if(req->rtp_buffer) {
1026 logmsg("About to write %zu RTP bytes", req->rtp_buffersize);
1027 count = req->rtp_buffersize;
1032 written = swrite(sock, req->rtp_buffer + (req->rtp_buffersize - count), num);
1040 free(req->rtp_buffer);
1041 req->rtp_buffersize = 0;
1047 } while(res && ((error = ERRNO) == EINTR));
1049 logmsg("Error closing file %s error: %d %s",
1050 RESPONSE_DUMP, error, strerror(error));
1056 logmsg("Sending response failed. Only (%zu bytes) of (%zu bytes) were sent",
1057 responsesize-count, responsesize);
1065 logmsg("Response sent (%zu bytes) and written to " RESPONSE_DUMP,
1077 if(2 == sscanf(ptr, "%31s %d", command, &num)) {
1078 if(!strcmp("wait", command)) {
1079 logmsg("Told to sleep for %d seconds", num);
1081 while(quarters > 0) {
1087 /* should not happen */
1089 logmsg("wait_ms() failed with error: (%d) %s",
1090 error, strerror(error));
1095 logmsg("Continuing after sleeping %d seconds", num);
1098 logmsg("Unknown command in reply command section");
1100 ptr = strchr(ptr, '\n');
1105 } while(ptr && *ptr);
1110 req->open = persistant;
1112 prevtestno = req->testno;
1113 prevpartno = req->partno;
1119 int main(int argc, char *argv[])
1121 struct sockaddr_in me;
1123 struct sockaddr_in6 me6;
1124 #endif /* ENABLE_IPV6 */
1125 curl_socket_t sock = CURL_SOCKET_BAD;
1126 curl_socket_t msgsock = CURL_SOCKET_BAD;
1127 int wrotepidfile = 0;
1129 unsigned short port = DEFAULT_PORT;
1130 char *pidname= (char *)".rtsp.pid";
1131 struct httprequest req;
1138 if(!strcmp("--version", argv[arg])) {
1139 printf("rtspd IPv4%s"
1150 else if(!strcmp("--pidfile", argv[arg])) {
1153 pidname = argv[arg++];
1155 else if(!strcmp("--logfile", argv[arg])) {
1158 serverlogfile = argv[arg++];
1160 else if(!strcmp("--ipv4", argv[arg])) {
1167 else if(!strcmp("--ipv6", argv[arg])) {
1174 else if(!strcmp("--port", argv[arg])) {
1177 port = (unsigned short)atoi(argv[arg]);
1181 else if(!strcmp("--srcdir", argv[arg])) {
1189 puts("Usage: rtspd [option]\n"
1191 " --logfile [file]\n"
1192 " --pidfile [file]\n"
1196 " --srcdir [path]");
1203 atexit(win32_cleanup);
1206 install_signal_handlers();
1208 pid = (long)getpid();
1213 sock = socket(AF_INET, SOCK_STREAM, 0);
1216 sock = socket(AF_INET6, SOCK_STREAM, 0);
1219 if(CURL_SOCKET_BAD == sock) {
1221 logmsg("Error creating socket: (%d) %s",
1222 error, strerror(error));
1223 goto server_cleanup;
1227 if (0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1228 (void *)&flag, sizeof(flag))) {
1230 logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
1231 error, strerror(error));
1232 goto server_cleanup;
1238 memset(&me, 0, sizeof(me));
1239 me.sin_family = AF_INET;
1240 me.sin_addr.s_addr = INADDR_ANY;
1241 me.sin_port = htons(port);
1242 rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
1246 memset(&me6, 0, sizeof(me6));
1247 me6.sin6_family = AF_INET6;
1248 me6.sin6_addr = in6addr_any;
1249 me6.sin6_port = htons(port);
1250 rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
1252 #endif /* ENABLE_IPV6 */
1255 logmsg("Error binding socket on port %hu: (%d) %s",
1256 port, error, strerror(error));
1257 goto server_cleanup;
1260 logmsg("Running %s version on port %d", ipv_inuse, (int)port);
1262 /* start accepting connections */
1263 rc = listen(sock, 5);
1266 logmsg("listen() failed with error: (%d) %s",
1267 error, strerror(error));
1268 goto server_cleanup;
1272 ** As soon as this server writes its pid file the test harness will
1273 ** attempt to connect to this server and initiate its verification.
1276 wrotepidfile = write_pidfile(pidname);
1278 goto server_cleanup;
1281 msgsock = accept(sock, NULL, NULL);
1285 if (CURL_SOCKET_BAD == msgsock) {
1287 logmsg("MAJOR ERROR: accept() failed with error: (%d) %s",
1288 error, strerror(error));
1293 ** As soon as this server acepts a connection from the test harness it
1294 ** must set the server logs advisor read lock to indicate that server
1295 ** logs should not be read until this lock is removed by this server.
1298 set_advisor_read_lock(SERVERLOGS_LOCK);
1299 serverlogslocked = 1;
1301 logmsg("====> Client connect");
1305 * Disable the Nagle algorithm to make it easier to send out a large
1306 * response in many small segments to torture the clients more.
1309 if (setsockopt(msgsock, IPPROTO_TCP, TCP_NODELAY,
1310 (void *)&flag, sizeof(flag)) == -1) {
1311 logmsg("====> TCP_NODELAY failed");
1315 /* initialization of httprequest struct is done in get_request(), but due
1316 to pipelining treatment the pipelining struct field must be initialized
1317 previously to FALSE every time a new connection arrives. */
1319 req.pipelining = FALSE;
1325 if(get_request(msgsock, &req))
1326 /* non-zero means error, break out of loop */
1330 /* bounce treatment requested */
1331 if((req.testno == prevtestno) &&
1332 (req.partno == prevpartno)) {
1334 logmsg("BOUNCE part number to %ld", req.partno);
1343 send_doc(msgsock, &req);
1347 if((req.testno < 0) && (req.testno != DOCNUMBER_CONNECT)) {
1348 logmsg("special request received, no persistency");
1352 logmsg("instructed to close connection after server-reply");
1357 logmsg("=> persistant connection request ended, awaits new request");
1358 /* if we got a CONNECT, loop and get another request as well! */
1359 } while(req.open || (req.testno == DOCNUMBER_CONNECT));
1364 logmsg("====> Client disconnect");
1366 msgsock = CURL_SOCKET_BAD;
1368 if(serverlogslocked) {
1369 serverlogslocked = 0;
1370 clear_advisor_read_lock(SERVERLOGS_LOCK);
1373 if (req.testno == DOCNUMBER_QUIT)
1379 if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
1382 if(sock != CURL_SOCKET_BAD)
1386 logmsg("signalled to die");
1391 if(serverlogslocked) {
1392 serverlogslocked = 0;
1393 clear_advisor_read_lock(SERVERLOGS_LOCK);
1396 restore_signal_handlers();
1398 if(got_exit_signal) {
1399 logmsg("========> %s rtspd (port: %d pid: %ld) exits with signal (%d)",
1400 ipv_inuse, (int)port, pid, exit_signal);
1402 * To properly set the return status of the process we
1403 * must raise the same signal SIGINT or SIGTERM that we
1404 * caught and let the old handler take care of it.
1409 logmsg("========> rtspd quits");