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.
21
***************************************************************************/
24
* curl's test suite Real Time Streaming Protocol (RTSP) server.
26
* This source file was started based on curl's HTTP test suite server.
29
#define CURL_NO_OLDIES
31
#include "setup.h" /* portability help from the lib directory */
39
#ifdef HAVE_SYS_SOCKET_H
40
#include <sys/socket.h>
42
#ifdef HAVE_NETINET_IN_H
43
#include <netinet/in.h>
45
#ifdef HAVE_ARPA_INET_H
46
#include <arpa/inet.h>
51
#ifdef HAVE_NETINET_TCP_H
52
#include <netinet/tcp.h> /* for TCP_NODELAY */
55
#define ENABLE_CURLX_PRINTF
56
/* make the curlx header define all printf() functions to use the curlx_*
58
#include "curlx.h" /* from the private lib dir */
62
/* include memdebug.h last */
66
static bool use_ipv6 = FALSE;
68
static const char *ipv_inuse = "IPv4";
69
static int serverlogslocked = 0;
71
#define REQBUFSIZ 150000
72
#define REQBUFSIZ_TXT "149999"
74
static long prevtestno=-1; /* previous test number we served */
75
static long prevpartno=-1; /* previous part number we served */
76
static bool prevbounce=FALSE; /* instructs the server to increase the part
77
number for a test in case the identical
78
testno+partno request shows up again */
80
#define RCMD_NORMALREQ 0 /* default request, use the tests file normally */
81
#define RCMD_IDLE 1 /* told to sit idle */
82
#define RCMD_STREAM 2 /* told to stream */
90
#define SET_RTP_PKT_CHN(p,c) ((p)[1] = (unsigned char)((c) & 0xFF))
92
#define SET_RTP_PKT_LEN(p,l) (((p)[2] = (unsigned char)(((l) >> 8) & 0xFF)), \
93
((p)[3] = (unsigned char)((l) & 0xFF)))
96
char reqbuf[REQBUFSIZ]; /* buffer area for the incoming request */
97
size_t checkindex; /* where to start checking of the request */
98
size_t offset; /* size of the incoming request */
99
long testno; /* test number found in the request */
100
long partno; /* part number found in the request */
101
bool open; /* keep connection open info, as found in the request */
102
bool auth_req; /* authentication required, don't wait for body unless
103
there's an Authorization header */
104
bool auth; /* Authorization header present in the incoming request */
105
size_t cl; /* Content-Length of the incoming request */
106
bool digest; /* Authorization digest header found */
107
bool ntlm; /* Authorization ntlm header found */
108
int pipe; /* if non-zero, expect this many requests to do a "piped"
110
int skip; /* if non-zero, the server is instructed to not read this
111
many bytes from a PUT/POST request. Ie the client sends N
112
bytes said in Content-Length, but the server only reads N
114
int rcmd; /* doing a special command, see defines above */
115
reqprot_t protocol; /* request protocol, HTTP or RTSP */
116
int prot_version; /* HTTP or RTSP version (major*10 + minor) */
117
bool pipelining; /* true if request is pipelined */
119
size_t rtp_buffersize;
122
static int ProcessRequest(struct httprequest *req);
123
static void storerequest(char *reqbuf, size_t totalsize);
125
#define DEFAULT_PORT 8999
127
#ifndef DEFAULT_LOGFILE
128
#define DEFAULT_LOGFILE "log/rtspd.log"
131
const char *serverlogfile = DEFAULT_LOGFILE;
133
#define RTSPDVERSION "cURL test suite RTSP server/0.1"
135
#define REQUEST_DUMP "log/server.input"
136
#define RESPONSE_DUMP "log/server.response"
138
/* very-big-path support */
139
#define MAXDOCNAMELEN 140000
140
#define MAXDOCNAMELEN_TXT "139999"
142
#define REQUEST_KEYWORD_SIZE 256
143
#define REQUEST_KEYWORD_SIZE_TXT "255"
145
#define CMD_AUTH_REQUIRED "auth_required"
147
/* 'idle' means that it will accept the request fine but never respond
148
any data. Just keep the connection alive. */
149
#define CMD_IDLE "idle"
151
/* 'stream' means to send a never-ending stream of data */
152
#define CMD_STREAM "stream"
154
#define END_OF_HEADERS "\r\n\r\n"
157
DOCNUMBER_NOTHING = -7,
159
DOCNUMBER_BADCONNECT = -5,
160
DOCNUMBER_INTERNAL= -4,
161
DOCNUMBER_CONNECT = -3,
162
DOCNUMBER_WERULEZ = -2,
167
/* sent as reply to a QUIT */
168
static const char *docquit =
169
"HTTP/1.1 200 Goodbye" END_OF_HEADERS;
171
/* sent as reply to a CONNECT */
172
static const char *docconnect =
173
"HTTP/1.1 200 Mighty fine indeed" END_OF_HEADERS;
175
/* sent as reply to a "bad" CONNECT */
176
static const char *docbadconnect =
177
"HTTP/1.1 501 Forbidden you fool" END_OF_HEADERS;
179
/* send back this on HTTP 404 file not found */
180
static const char *doc404_HTTP = "HTTP/1.1 404 Not Found\r\n"
181
"Server: " RTSPDVERSION "\r\n"
182
"Connection: close\r\n"
183
"Content-Type: text/html"
185
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
187
"<TITLE>404 Not Found</TITLE>\n"
189
"<H1>Not Found</H1>\n"
190
"The requested URL was not found on this server.\n"
191
"<P><HR><ADDRESS>" RTSPDVERSION "</ADDRESS>\n" "</BODY></HTML>\n";
193
/* send back this on RTSP 404 file not found */
194
static const char *doc404_RTSP = "RTSP/1.0 404 Not Found\r\n"
195
"Server: " RTSPDVERSION
198
/* Default size to send away fake RTP data */
199
#define RTP_DATA_SIZE 12
200
static const char *RTP_DATA = "$_1234\n\0asdf";
202
/* do-nothing macro replacement for systems which lack siginterrupt() */
204
#ifndef HAVE_SIGINTERRUPT
205
#define siginterrupt(x,y) do {} while(0)
208
/* vars used to keep around previous signal handlers */
210
typedef RETSIGTYPE (*SIGHANDLER_T)(int);
213
static SIGHANDLER_T old_sighup_handler = SIG_ERR;
217
static SIGHANDLER_T old_sigpipe_handler = SIG_ERR;
221
static SIGHANDLER_T old_sigalrm_handler = SIG_ERR;
225
static SIGHANDLER_T old_sigint_handler = SIG_ERR;
229
static SIGHANDLER_T old_sigterm_handler = SIG_ERR;
232
/* var which if set indicates that the program should finish execution */
234
SIG_ATOMIC_T got_exit_signal = 0;
236
/* if next is set indicates the first signal handled in exit_signal_handler */
238
static volatile int exit_signal = 0;
240
/* signal handler that will be triggered to indicate that the program
241
should finish its execution in a controlled manner as soon as possible.
242
The first time this is called it will set got_exit_signal to one and
243
store in exit_signal the signal that triggered its execution. */
245
static RETSIGTYPE exit_signal_handler(int signum)
247
int old_errno = ERRNO;
248
if(got_exit_signal == 0) {
250
exit_signal = signum;
252
(void)signal(signum, exit_signal_handler);
253
SET_ERRNO(old_errno);
256
static void install_signal_handlers(void)
259
/* ignore SIGHUP signal */
260
if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
261
logmsg("cannot install SIGHUP handler: %s", strerror(ERRNO));
264
/* ignore SIGPIPE signal */
265
if((old_sigpipe_handler = signal(SIGPIPE, SIG_IGN)) == SIG_ERR)
266
logmsg("cannot install SIGPIPE handler: %s", strerror(ERRNO));
269
/* ignore SIGALRM signal */
270
if((old_sigalrm_handler = signal(SIGALRM, SIG_IGN)) == SIG_ERR)
271
logmsg("cannot install SIGALRM handler: %s", strerror(ERRNO));
274
/* handle SIGINT signal with our exit_signal_handler */
275
if((old_sigint_handler = signal(SIGINT, exit_signal_handler)) == SIG_ERR)
276
logmsg("cannot install SIGINT handler: %s", strerror(ERRNO));
278
siginterrupt(SIGINT, 1);
281
/* handle SIGTERM signal with our exit_signal_handler */
282
if((old_sigterm_handler = signal(SIGTERM, exit_signal_handler)) == SIG_ERR)
283
logmsg("cannot install SIGTERM handler: %s", strerror(ERRNO));
285
siginterrupt(SIGTERM, 1);
289
static void restore_signal_handlers(void)
292
if(SIG_ERR != old_sighup_handler)
293
(void)signal(SIGHUP, old_sighup_handler);
296
if(SIG_ERR != old_sigpipe_handler)
297
(void)signal(SIGPIPE, old_sigpipe_handler);
300
if(SIG_ERR != old_sigalrm_handler)
301
(void)signal(SIGALRM, old_sigalrm_handler);
304
if(SIG_ERR != old_sigint_handler)
305
(void)signal(SIGINT, old_sigint_handler);
308
if(SIG_ERR != old_sigterm_handler)
309
(void)signal(SIGTERM, old_sigterm_handler);
313
static int ProcessRequest(struct httprequest *req)
315
char *line=&req->reqbuf[req->checkindex];
316
bool chunked = FALSE;
317
static char request[REQUEST_KEYWORD_SIZE];
318
static char doc[MAXDOCNAMELEN];
319
static char prot_str[5];
321
int prot_major, prot_minor;
324
end = strstr(line, END_OF_HEADERS);
326
logmsg("ProcessRequest() called with testno %ld and line [%s]",
329
/* try to figure out the request characteristics as soon as possible, but
331
if((req->testno == DOCNUMBER_NOTHING) &&
333
"%" REQUEST_KEYWORD_SIZE_TXT"s %" MAXDOCNAMELEN_TXT "s %4s/%d.%d",
341
if(!strcmp(prot_str, "HTTP")) {
342
req->protocol = RPROT_HTTP;
344
else if(!strcmp(prot_str, "RTSP")) {
345
req->protocol = RPROT_RTSP;
348
req->protocol = RPROT_NONE;
349
logmsg("got unknown protocol %s", prot_str);
353
req->prot_version = prot_major*10 + prot_minor;
355
/* find the last slash */
356
ptr = strrchr(doc, '/');
358
/* get the number after it */
363
if((strlen(doc) + strlen(request)) < 200)
364
sprintf(logbuf, "Got request: %s %s %s/%d.%d",
365
request, doc, prot_str, prot_major, prot_minor);
367
sprintf(logbuf, "Got a *HUGE* request %s/%d.%d",
368
prot_str, prot_major, prot_minor);
369
logmsg("%s", logbuf);
371
if(!strncmp("/verifiedserver", ptr, 15)) {
372
logmsg("Are-we-friendly question received");
373
req->testno = DOCNUMBER_WERULEZ;
377
if(!strncmp("/quit", ptr, 5)) {
378
logmsg("Request-to-quit received");
379
req->testno = DOCNUMBER_QUIT;
383
ptr++; /* skip the slash */
385
/* skip all non-numericals following the slash */
386
while(*ptr && !ISDIGIT(*ptr))
389
req->testno = strtol(ptr, &ptr, 10);
391
if(req->testno > 10000) {
392
req->partno = req->testno % 10000;
393
req->testno /= 10000;
398
sprintf(logbuf, "Requested test number %ld part %ld",
399
req->testno, req->partno);
400
logmsg("%s", logbuf);
402
filename = test2file(req->testno);
404
stream=fopen(filename, "rb");
407
logmsg("fopen() failed with error: %d %s", error, strerror(error));
408
logmsg("Error opening file: %s", filename);
409
logmsg("Couldn't open test file %ld", req->testno);
410
req->open = FALSE; /* closes connection */
422
char *rtp_scratch = NULL;
424
/* get the custom server control "commands" */
425
error = getpart(&cmd, &cmdsize, "reply", "servercmd", stream);
428
logmsg("getpart() failed with error: %d", error);
429
req->open = FALSE; /* closes connection */
435
logmsg("Found a reply-servercmd section!");
437
if(!strncmp(CMD_AUTH_REQUIRED, ptr, strlen(CMD_AUTH_REQUIRED))) {
438
logmsg("instructed to require authorization header");
439
req->auth_req = TRUE;
441
else if(!strncmp(CMD_IDLE, ptr, strlen(CMD_IDLE))) {
442
logmsg("instructed to idle");
443
req->rcmd = RCMD_IDLE;
446
else if(!strncmp(CMD_STREAM, ptr, strlen(CMD_STREAM))) {
447
logmsg("instructed to stream");
448
req->rcmd = RCMD_STREAM;
450
else if(1 == sscanf(ptr, "pipe: %d", &num)) {
451
logmsg("instructed to allow a pipe size of %d", num);
453
logmsg("negative pipe size ignored");
455
req->pipe = num-1; /* decrease by one since we don't count the
456
first request in this number */
458
else if(1 == sscanf(ptr, "skip: %d", &num)) {
459
logmsg("instructed to skip this number of bytes %d", num);
462
else if(3 == sscanf(ptr, "rtp: part %d channel %d size %d",
463
&rtp_partno, &rtp_channel, &rtp_size)) {
465
if(rtp_partno == req->partno) {
466
logmsg("RTP: part %d channel %d size %d",
467
rtp_partno, rtp_channel, rtp_size);
469
/* Make our scratch buffer enough to fit all the
470
* desired data and one for padding */
471
rtp_scratch = malloc(rtp_size + 4 + RTP_DATA_SIZE);
473
/* RTP is signalled with a $ */
474
rtp_scratch[0] = '$';
476
/* The channel follows and is one byte */
477
SET_RTP_PKT_CHN(rtp_scratch ,rtp_channel);
479
/* Length follows and is a two byte short in network order */
480
SET_RTP_PKT_LEN(rtp_scratch, rtp_size);
482
/* Fill it with junk data */
483
for(i = 0; i < rtp_size; i+= RTP_DATA_SIZE) {
484
memcpy(rtp_scratch + 4 + i, RTP_DATA, RTP_DATA_SIZE);
487
if(req->rtp_buffer == NULL) {
488
req->rtp_buffer = rtp_scratch;
489
req->rtp_buffersize = rtp_size + 4;
491
req->rtp_buffer = realloc(req->rtp_buffer, req->rtp_buffersize + rtp_size + 4);
492
memcpy(req->rtp_buffer + req->rtp_buffersize, rtp_scratch, rtp_size + 4);
493
req->rtp_buffersize += rtp_size + 4;
496
logmsg("rtp_buffersize is %zu, rtp_size is %d.", req->rtp_buffersize, rtp_size);
501
logmsg("funny instruction found: %s", ptr);
504
ptr = strchr(ptr, '\n');
509
} while(ptr && *ptr);
510
logmsg("Done parsing server commands");
517
if(sscanf(req->reqbuf, "CONNECT %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
518
doc, &prot_major, &prot_minor) == 3) {
519
sprintf(logbuf, "Received a CONNECT %s HTTP/%d.%d request",
520
doc, prot_major, prot_minor);
521
logmsg("%s", logbuf);
523
if(req->prot_version == 10)
524
req->open = FALSE; /* HTTP 1.0 closes connection by default */
526
if(!strncmp(doc, "bad", 3))
527
/* if the host name starts with bad, we fake an error here */
528
req->testno = DOCNUMBER_BADCONNECT;
529
else if(!strncmp(doc, "test", 4)) {
530
/* if the host name starts with test, the port number used in the
531
CONNECT line will be used as test number! */
532
char *portp = strchr(doc, ':');
534
req->testno = atoi(portp+1);
536
req->testno = DOCNUMBER_CONNECT;
539
req->testno = DOCNUMBER_CONNECT;
542
logmsg("Did not find test number in PATH");
543
req->testno = DOCNUMBER_404;
549
/* we don't have a complete request yet! */
550
logmsg("ProcessRequest returned without a complete request");
551
return 0; /* not complete yet */
553
logmsg("ProcessRequest found a complete request");
556
/* we do have a full set, advance the checkindex to after the end of the
557
headers, for the pipelining case mostly */
558
req->checkindex += (end - line) + strlen(END_OF_HEADERS);
560
/* **** Persistence ****
562
* If the request is a HTTP/1.0 one, we close the connection unconditionally
565
* If the request is a HTTP/1.1 one, we MUST check for a "Connection:"
566
* header that might say "close". If it does, we close a connection when
567
* this request is processed. Otherwise, we keep the connection alive for X
575
if((req->cl==0) && curlx_strnequal("Content-Length:", line, 15)) {
576
/* If we don't ignore content-length, we read it and we read the whole
577
request including the body before we return. If we've been told to
578
ignore the content-length, we will return as soon as all headers
579
have been received */
581
char *ptr = line + 15;
582
unsigned long clen = 0;
583
while(*ptr && ISSPACE(*ptr))
587
clen = strtoul(ptr, &endptr, 10);
588
if((ptr == endptr) || !ISSPACE(*endptr) || (ERANGE == ERRNO)) {
589
/* this assumes that a zero Content-Length is valid */
590
logmsg("Found invalid Content-Length: (%s) in the request", ptr);
591
req->open = FALSE; /* closes connection */
594
req->cl = clen - req->skip;
596
logmsg("Found Content-Length: %lu in the request", clen);
598
logmsg("... but will abort after %zu bytes", req->cl);
601
else if(curlx_strnequal("Transfer-Encoding: chunked", line,
602
strlen("Transfer-Encoding: chunked"))) {
603
/* chunked data coming in */
608
if(strstr(req->reqbuf, "\r\n0\r\n\r\n"))
609
/* end of chunks reached */
612
return 0; /* not done */
615
line = strchr(line, '\n');
621
if(!req->auth && strstr(req->reqbuf, "Authorization:")) {
622
req->auth = TRUE; /* Authorization: header present! */
624
logmsg("Authorization header found, as required");
627
if(!req->digest && strstr(req->reqbuf, "Authorization: Digest")) {
628
/* If the client is passing this Digest-header, we set the part number
629
to 1000. Not only to spice up the complexity of this, but to make
630
Digest stuff to work in the test suite. */
632
req->digest = TRUE; /* header found */
633
logmsg("Received Digest request, sending back data %ld", req->partno);
635
else if(!req->ntlm &&
636
strstr(req->reqbuf, "Authorization: NTLM TlRMTVNTUAAD")) {
637
/* If the client is passing this type-3 NTLM header */
639
req->ntlm = TRUE; /* NTLM found */
640
logmsg("Received NTLM type-3, sending back data %ld", req->partno);
642
logmsg(" Expecting %zu POSTed bytes", req->cl);
645
else if(!req->ntlm &&
646
strstr(req->reqbuf, "Authorization: NTLM TlRMTVNTUAAB")) {
647
/* If the client is passing this type-1 NTLM header */
649
req->ntlm = TRUE; /* NTLM found */
650
logmsg("Received NTLM type-1, sending back data %ld", req->partno);
652
else if((req->partno >= 1000) && strstr(req->reqbuf, "Authorization: Basic")) {
653
/* If the client is passing this Basic-header and the part number is already
654
>=1000, we add 1 to the part number. This allows simple Basic authentication
655
negotiation to work in the test suite. */
657
logmsg("Received Basic request, sending back data %ld", req->partno);
659
if(strstr(req->reqbuf, "Connection: close"))
660
req->open = FALSE; /* close connection after this request */
664
req->prot_version >= 11 &&
666
req->reqbuf + req->offset > end + strlen(END_OF_HEADERS) &&
667
(!strncmp(req->reqbuf, "GET", strlen("GET")) ||
668
!strncmp(req->reqbuf, "HEAD", strlen("HEAD")))) {
669
/* If we have a persistent connection, HTTP version >= 1.1
670
and GET/HEAD request, enable pipelining. */
671
req->checkindex = (end - req->reqbuf) + strlen(END_OF_HEADERS);
672
req->pipelining = TRUE;
678
/* scan for more header ends within this chunk */
679
line = &req->reqbuf[req->checkindex];
680
end = strstr(line, END_OF_HEADERS);
683
req->checkindex += (end - line) + strlen(END_OF_HEADERS);
687
/* If authentication is required and no auth was provided, end now. This
688
makes the server NOT wait for PUT/POST data and you can then make the
689
test case send a rejection before any such data has been sent. Test case
691
if(req->auth_req && !req->auth)
695
if(req->cl <= req->offset - (end - req->reqbuf) - strlen(END_OF_HEADERS))
698
return 0; /* not complete yet */
704
/* store the entire request in a file */
705
static void storerequest(char *reqbuf, size_t totalsize)
719
dump = fopen(REQUEST_DUMP, "ab");
720
} while ((dump == NULL) && ((error = ERRNO) == EINTR));
722
logmsg("Error opening file %s error: %d %s",
723
REQUEST_DUMP, error, strerror(error));
724
logmsg("Failed to write request input to " REQUEST_DUMP);
728
writeleft = totalsize;
730
written = fwrite(&reqbuf[totalsize-writeleft],
733
goto storerequest_cleanup;
735
writeleft -= written;
736
} while ((writeleft > 0) && ((error = ERRNO) == EINTR));
739
logmsg("Wrote request (%zu bytes) input to " REQUEST_DUMP, totalsize);
740
else if(writeleft > 0) {
741
logmsg("Error writing file %s error: %d %s",
742
REQUEST_DUMP, error, strerror(error));
743
logmsg("Wrote only (%zu bytes) of (%zu bytes) request input to %s",
744
totalsize-writeleft, totalsize, REQUEST_DUMP);
747
storerequest_cleanup:
751
} while(res && ((error = ERRNO) == EINTR));
753
logmsg("Error closing file %s error: %d %s",
754
REQUEST_DUMP, error, strerror(error));
757
/* return 0 on success, non-zero on failure */
758
static int get_request(curl_socket_t sock, struct httprequest *req)
762
int done_processing = 0;
763
char *reqbuf = req->reqbuf;
766
char *pipereq = NULL;
767
size_t pipereq_length = 0;
769
if(req->pipelining) {
770
pipereq = reqbuf + req->checkindex;
771
pipereq_length = req->offset - req->checkindex;
774
/*** Init the httprequest structure properly for the upcoming request ***/
778
req->testno = DOCNUMBER_NOTHING;
781
req->auth_req = FALSE;
788
req->rcmd = RCMD_NORMALREQ;
789
req->protocol = RPROT_NONE;
790
req->prot_version = 0;
791
req->pipelining = FALSE;
792
req->rtp_buffer = NULL;
793
req->rtp_buffersize = 0;
795
/*** end of httprequest init ***/
797
while(!done_processing && (req->offset < REQBUFSIZ-1)) {
798
if(pipereq_length && pipereq) {
799
memmove(reqbuf, pipereq, pipereq_length);
800
got = pipereq_length;
805
/* we are instructed to not read the entire thing, so we make sure to only
806
read what we're supposed to and NOT read the enire thing the client
808
got = sread(sock, reqbuf + req->offset, req->cl);
810
got = sread(sock, reqbuf + req->offset, REQBUFSIZ-1 - req->offset);
815
logmsg("Connection closed by client");
820
logmsg("recv() returned error: (%d) %s", error, strerror(error));
824
/* dump the request received so far to the external file */
825
reqbuf[req->offset] = '\0';
826
storerequest(reqbuf, req->offset);
830
logmsg("Read %zd bytes", got);
832
req->offset += (size_t)got;
833
reqbuf[req->offset] = '\0';
835
done_processing = ProcessRequest(req);
838
if(done_processing && req->pipe) {
839
logmsg("Waiting for another piped request");
845
if((req->offset == REQBUFSIZ-1) && (got > 0)) {
846
logmsg("Request would overflow buffer, closing connection");
847
/* dump request received so far to external file anyway */
848
reqbuf[REQBUFSIZ-1] = '\0';
851
else if(req->offset > REQBUFSIZ-1) {
852
logmsg("Request buffer overflow, closing connection");
853
/* dump request received so far to external file anyway */
854
reqbuf[REQBUFSIZ-1] = '\0';
858
reqbuf[req->offset] = '\0';
860
/* dump the request to an external file */
861
storerequest(reqbuf, req->pipelining ? req->checkindex : req->offset);
865
return fail; /* return 0 on success */
868
/* returns -1 on failure */
869
static int send_doc(curl_socket_t sock, struct httprequest *req)
879
bool persistant = TRUE;
880
bool sendfailure = FALSE;
885
static char weare[256];
887
char partbuf[80]="data";
889
logmsg("Send response number %ld part %ld", req->testno, req->partno);
894
break; /* continue with business as usual */
896
#define STREAMTHIS "a string to stream 01234567890\n"
897
count = strlen(STREAMTHIS);
899
written = swrite(sock, STREAMTHIS, count);
902
if(written != (ssize_t)count) {
903
logmsg("Stopped streaming");
909
/* Do nothing. Sit idle. Pretend it rains. */
915
if(req->testno < 0) {
919
switch(req->testno) {
921
logmsg("Replying to QUIT");
924
case DOCNUMBER_WERULEZ:
925
/* we got a "friends?" question, reply back that we sure are */
926
logmsg("Identifying ourselves as friends");
927
sprintf(msgbuf, "RTSP_SERVER WE ROOLZ: %ld\r\n", (long)getpid());
928
msglen = strlen(msgbuf);
929
sprintf(weare, "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
933
case DOCNUMBER_INTERNAL:
934
logmsg("Bailing out due to internal error");
936
case DOCNUMBER_CONNECT:
937
logmsg("Replying to CONNECT");
940
case DOCNUMBER_BADCONNECT:
941
logmsg("Replying to a bad CONNECT");
942
buffer = docbadconnect;
946
logmsg("Replying to with a 404");
947
if(req->protocol == RPROT_HTTP) {
948
buffer = doc404_HTTP;
950
buffer = doc404_RTSP;
955
count = strlen(buffer);
958
char *filename = test2file(req->testno);
961
sprintf(partbuf, "data%ld", req->partno);
963
stream=fopen(filename, "rb");
966
logmsg("fopen() failed with error: %d %s", error, strerror(error));
967
logmsg("Error opening file: %s", filename);
968
logmsg("Couldn't open test file");
972
error = getpart(&ptr, &count, "reply", partbuf, stream);
975
logmsg("getpart() failed with error: %d", error);
981
if(got_exit_signal) {
987
/* re-open the same file again */
988
stream=fopen(filename, "rb");
991
logmsg("fopen() failed with error: %d %s", error, strerror(error));
992
logmsg("Error opening file: %s", filename);
993
logmsg("Couldn't open test file");
999
/* get the custom server control "commands" */
1000
error = getpart(&cmd, &cmdsize, "reply", "postcmd", stream);
1003
logmsg("getpart() failed with error: %d", error);
1011
if(got_exit_signal) {
1019
/* If the word 'swsclose' is present anywhere in the reply chunk, the
1020
connection will be closed after the data has been sent to the requesting
1022
if(strstr(buffer, "swsclose") || !count) {
1024
logmsg("connection close instruction \"swsclose\" found in response");
1026
if(strstr(buffer, "swsbounce")) {
1028
logmsg("enable \"swsbounce\" in the next request");
1033
dump = fopen(RESPONSE_DUMP, "ab");
1036
logmsg("fopen() failed with error: %d %s", error, strerror(error));
1037
logmsg("Error opening file: %s", RESPONSE_DUMP);
1038
logmsg("couldn't create logfile: " RESPONSE_DUMP);
1046
responsesize = count;
1048
/* Ok, we send no more than 200 bytes at a time, just to make sure that
1049
larger chunks are split up so that the client will need to do multiple
1050
recv() calls to get it and thus we exercise that code better */
1054
written = swrite(sock, buffer, num);
1060
logmsg("Sent off %zd bytes", written);
1062
/* write to file as well */
1063
fwrite(buffer, 1, (size_t)written, dump);
1071
/* Send out any RTP data */
1072
if(req->rtp_buffer) {
1073
logmsg("About to write %zu RTP bytes", req->rtp_buffersize);
1074
count = req->rtp_buffersize;
1079
written = swrite(sock, req->rtp_buffer + (req->rtp_buffersize - count), num);
1087
free(req->rtp_buffer);
1088
req->rtp_buffersize = 0;
1093
} while(res && ((error = ERRNO) == EINTR));
1095
logmsg("Error closing file %s error: %d %s",
1096
RESPONSE_DUMP, error, strerror(error));
1098
if(got_exit_signal) {
1107
logmsg("Sending response failed. Only (%zu bytes) of (%zu bytes) were sent",
1108
responsesize-count, responsesize);
1116
logmsg("Response sent (%zu bytes) and written to " RESPONSE_DUMP,
1128
if(2 == sscanf(ptr, "%31s %d", command, &num)) {
1129
if(!strcmp("wait", command)) {
1130
logmsg("Told to sleep for %d seconds", num);
1132
while(quarters > 0) {
1138
/* should not happen */
1140
logmsg("wait_ms() failed with error: (%d) %s",
1141
error, strerror(error));
1146
logmsg("Continuing after sleeping %d seconds", num);
1149
logmsg("Unknown command in reply command section");
1151
ptr = strchr(ptr, '\n');
1156
} while(ptr && *ptr);
1161
req->open = persistant;
1163
prevtestno = req->testno;
1164
prevpartno = req->partno;
1170
int main(int argc, char *argv[])
1172
struct sockaddr_in me;
1174
struct sockaddr_in6 me6;
1175
#endif /* ENABLE_IPV6 */
1176
curl_socket_t sock = CURL_SOCKET_BAD;
1177
curl_socket_t msgsock = CURL_SOCKET_BAD;
1178
int wrotepidfile = 0;
1180
unsigned short port = DEFAULT_PORT;
1181
char *pidname= (char *)".rtsp.pid";
1182
struct httprequest req;
1189
if(!strcmp("--version", argv[arg])) {
1190
printf("rtspd IPv4%s"
1201
else if(!strcmp("--pidfile", argv[arg])) {
1204
pidname = argv[arg++];
1206
else if(!strcmp("--logfile", argv[arg])) {
1209
serverlogfile = argv[arg++];
1211
else if(!strcmp("--ipv4", argv[arg])) {
1218
else if(!strcmp("--ipv6", argv[arg])) {
1225
else if(!strcmp("--port", argv[arg])) {
1229
unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
1230
if((endptr != argv[arg] + strlen(argv[arg])) ||
1231
(ulnum < 1025UL) || (ulnum > 65535UL)) {
1232
fprintf(stderr, "rtspd: invalid --port argument (%s)\n",
1236
port = curlx_ultous(ulnum);
1240
else if(!strcmp("--srcdir", argv[arg])) {
1248
puts("Usage: rtspd [option]\n"
1250
" --logfile [file]\n"
1251
" --pidfile [file]\n"
1255
" --srcdir [path]");
1262
atexit(win32_cleanup);
1265
install_signal_handlers();
1267
pid = (long)getpid();
1272
sock = socket(AF_INET, SOCK_STREAM, 0);
1275
sock = socket(AF_INET6, SOCK_STREAM, 0);
1278
if(CURL_SOCKET_BAD == sock) {
1280
logmsg("Error creating socket: (%d) %s",
1281
error, strerror(error));
1282
goto server_cleanup;
1286
if (0 != setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1287
(void *)&flag, sizeof(flag))) {
1289
logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
1290
error, strerror(error));
1291
goto server_cleanup;
1297
memset(&me, 0, sizeof(me));
1298
me.sin_family = AF_INET;
1299
me.sin_addr.s_addr = INADDR_ANY;
1300
me.sin_port = htons(port);
1301
rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
1305
memset(&me6, 0, sizeof(me6));
1306
me6.sin6_family = AF_INET6;
1307
me6.sin6_addr = in6addr_any;
1308
me6.sin6_port = htons(port);
1309
rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
1311
#endif /* ENABLE_IPV6 */
1314
logmsg("Error binding socket on port %hu: (%d) %s",
1315
port, error, strerror(error));
1316
goto server_cleanup;
1319
logmsg("Running %s version on port %d", ipv_inuse, (int)port);
1321
/* start accepting connections */
1322
rc = listen(sock, 5);
1325
logmsg("listen() failed with error: (%d) %s",
1326
error, strerror(error));
1327
goto server_cleanup;
1331
** As soon as this server writes its pid file the test harness will
1332
** attempt to connect to this server and initiate its verification.
1335
wrotepidfile = write_pidfile(pidname);
1337
goto server_cleanup;
1340
msgsock = accept(sock, NULL, NULL);
1344
if (CURL_SOCKET_BAD == msgsock) {
1346
logmsg("MAJOR ERROR: accept() failed with error: (%d) %s",
1347
error, strerror(error));
1352
** As soon as this server acepts a connection from the test harness it
1353
** must set the server logs advisor read lock to indicate that server
1354
** logs should not be read until this lock is removed by this server.
1357
set_advisor_read_lock(SERVERLOGS_LOCK);
1358
serverlogslocked = 1;
1360
logmsg("====> Client connect");
1364
* Disable the Nagle algorithm to make it easier to send out a large
1365
* response in many small segments to torture the clients more.
1368
if (setsockopt(msgsock, IPPROTO_TCP, TCP_NODELAY,
1369
(void *)&flag, sizeof(flag)) == -1) {
1370
logmsg("====> TCP_NODELAY failed");
1374
/* initialization of httprequest struct is done in get_request(), but due
1375
to pipelining treatment the pipelining struct field must be initialized
1376
previously to FALSE every time a new connection arrives. */
1378
req.pipelining = FALSE;
1384
if(get_request(msgsock, &req))
1385
/* non-zero means error, break out of loop */
1389
/* bounce treatment requested */
1390
if((req.testno == prevtestno) &&
1391
(req.partno == prevpartno)) {
1393
logmsg("BOUNCE part number to %ld", req.partno);
1402
send_doc(msgsock, &req);
1406
if((req.testno < 0) && (req.testno != DOCNUMBER_CONNECT)) {
1407
logmsg("special request received, no persistency");
1411
logmsg("instructed to close connection after server-reply");
1416
logmsg("=> persistant connection request ended, awaits new request");
1417
/* if we got a CONNECT, loop and get another request as well! */
1418
} while(req.open || (req.testno == DOCNUMBER_CONNECT));
1423
logmsg("====> Client disconnect");
1425
msgsock = CURL_SOCKET_BAD;
1427
if(serverlogslocked) {
1428
serverlogslocked = 0;
1429
clear_advisor_read_lock(SERVERLOGS_LOCK);
1432
if (req.testno == DOCNUMBER_QUIT)
1438
if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
1441
if(sock != CURL_SOCKET_BAD)
1445
logmsg("signalled to die");
1450
if(serverlogslocked) {
1451
serverlogslocked = 0;
1452
clear_advisor_read_lock(SERVERLOGS_LOCK);
1455
restore_signal_handlers();
1457
if(got_exit_signal) {
1458
logmsg("========> %s rtspd (port: %d pid: %ld) exits with signal (%d)",
1459
ipv_inuse, (int)port, pid, exit_signal);
1461
* To properly set the return status of the process we
1462
* must raise the same signal SIGINT or SIGTERM that we
1463
* caught and let the old handler take care of it.
1468
logmsg("========> rtspd quits");