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