rtspd.c 41 KB

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