2
0

sockfilt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2008, 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. * $Id$
  22. ***************************************************************************/
  23. /* Purpose
  24. *
  25. * 1. Accept a TCP connection on a custom port (ipv4 or ipv6), or connect
  26. * to a given (localhost) port.
  27. *
  28. * 2. Get commands on STDIN. Pass data on to the TCP stream.
  29. * Get data from TCP stream and pass on to STDOUT.
  30. *
  31. * This program is made to perform all the socket/stream/connection stuff for
  32. * the test suite's (perl) FTP server. Previously the perl code did all of
  33. * this by its own, but I decided to let this program do the socket layer
  34. * because of several things:
  35. *
  36. * o We want the perl code to work with rather old perl installations, thus
  37. * we cannot use recent perl modules or features.
  38. *
  39. * o We want IPv6 support for systems that provide it, and doing optional IPv6
  40. * support in perl seems if not impossible so at least awkward.
  41. *
  42. * o We want FTP-SSL support, which means that a connection that starts with
  43. * plain sockets needs to be able to "go SSL" in the midst. This would also
  44. * require some nasty perl stuff I'd rather avoid.
  45. *
  46. * (Source originally based on sws.c)
  47. */
  48. /*
  49. * Signal handling notes for sockfilt
  50. * ----------------------------------
  51. *
  52. * This program is a single-threaded process.
  53. *
  54. * This program is intended to be highly portable and as such it must be kept as
  55. * simple as possible, due to this the only signal handling mechanisms used will
  56. * be those of ANSI C, and used only in the most basic form which is good enough
  57. * for the purpose of this program.
  58. *
  59. * For the above reason and the specific needs of this program signals SIGHUP,
  60. * SIGPIPE and SIGALRM will be simply ignored on systems where this can be done.
  61. * If possible, signals SIGINT and SIGTERM will be handled by this program as an
  62. * indication to cleanup and finish execution as soon as possible. This will be
  63. * achieved with a single signal handler 'exit_signal_handler' for both signals.
  64. *
  65. * The 'exit_signal_handler' upon the first SIGINT or SIGTERM received signal
  66. * will just set to one the global var 'got_exit_signal' storing in global var
  67. * 'exit_signal' the signal that triggered this change.
  68. *
  69. * Nothing fancy that could introduce problems is used, the program at certain
  70. * points in its normal flow checks if var 'got_exit_signal' is set and in case
  71. * this is true it just makes its way out of loops and functions in structured
  72. * and well behaved manner to achieve proper program cleanup and termination.
  73. *
  74. * Even with the above mechanism implemented it is worthwile to note that other
  75. * signals might still be received, or that there might be systems on which it
  76. * is not possible to trap and ignore some of the above signals. This implies
  77. * that for increased portability and reliability the program must be coded as
  78. * if no signal was being ignored or handled at all. Enjoy it!
  79. */
  80. #include "setup.h" /* portability help from the lib directory */
  81. #ifdef HAVE_SIGNAL_H
  82. #include <signal.h>
  83. #endif
  84. #ifdef HAVE_UNISTD_H
  85. #include <unistd.h>
  86. #endif
  87. #ifdef HAVE_SYS_SOCKET_H
  88. #include <sys/socket.h>
  89. #endif
  90. #ifdef HAVE_NETINET_IN_H
  91. #include <netinet/in.h>
  92. #endif
  93. #ifdef _XOPEN_SOURCE_EXTENDED
  94. /* This define is "almost" required to build on HPUX 11 */
  95. #include <arpa/inet.h>
  96. #endif
  97. #ifdef HAVE_NETDB_H
  98. #include <netdb.h>
  99. #endif
  100. #define ENABLE_CURLX_PRINTF
  101. /* make the curlx header define all printf() functions to use the curlx_*
  102. versions instead */
  103. #include "curlx.h" /* from the private lib dir */
  104. #include "getpart.h"
  105. #include "inet_pton.h"
  106. #include "util.h"
  107. /* include memdebug.h last */
  108. #include "memdebug.h"
  109. #define DEFAULT_PORT 8999
  110. #ifndef DEFAULT_LOGFILE
  111. #define DEFAULT_LOGFILE "log/sockfilt.log"
  112. #endif
  113. const char *serverlogfile = DEFAULT_LOGFILE;
  114. static bool verbose = FALSE;
  115. #ifdef ENABLE_IPV6
  116. static bool use_ipv6 = FALSE;
  117. #endif
  118. static const char *ipv_inuse = "IPv4";
  119. static unsigned short port = DEFAULT_PORT;
  120. static unsigned short connectport = 0; /* if non-zero, we activate this mode */
  121. enum sockmode {
  122. PASSIVE_LISTEN, /* as a server waiting for connections */
  123. PASSIVE_CONNECT, /* as a server, connected to a client */
  124. ACTIVE, /* as a client, connected to a server */
  125. ACTIVE_DISCONNECT /* as a client, disconnected from server */
  126. };
  127. /* do-nothing macro replacement for systems which lack siginterrupt() */
  128. #ifndef HAVE_SIGINTERRUPT
  129. #define siginterrupt(x,y) do {} while(0)
  130. #endif
  131. /* vars used to keep around previous signal handlers */
  132. typedef RETSIGTYPE (*SIGHANDLER_T)(int);
  133. static SIGHANDLER_T old_sighup_handler = SIG_ERR;
  134. static SIGHANDLER_T old_sigpipe_handler = SIG_ERR;
  135. static SIGHANDLER_T old_sigalrm_handler = SIG_ERR;
  136. static SIGHANDLER_T old_sigint_handler = SIG_ERR;
  137. static SIGHANDLER_T old_sigterm_handler = SIG_ERR;
  138. /* var which if set indicates that the program should finish execution */
  139. SIG_ATOMIC_T got_exit_signal = 0;
  140. /* if next is set indicates the first signal handled in exit_signal_handler */
  141. static volatile int exit_signal = 0;
  142. /* signal handler that will be triggered to indicate that the program
  143. should finish its execution in a controlled manner as soon as possible.
  144. The first time this is called it will set got_exit_signal to one and
  145. store in exit_signal the signal that triggered its execution. */
  146. static RETSIGTYPE exit_signal_handler(int signum)
  147. {
  148. int old_errno = ERRNO;
  149. if(got_exit_signal == 0) {
  150. got_exit_signal = 1;
  151. exit_signal = signum;
  152. }
  153. (void)signal(signum, exit_signal_handler);
  154. SET_ERRNO(old_errno);
  155. }
  156. static void install_signal_handlers(void)
  157. {
  158. #ifdef SIGHUP
  159. /* ignore SIGHUP signal */
  160. if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
  161. logmsg("cannot install SIGHUP handler: %s", strerror(ERRNO));
  162. #endif
  163. #ifdef SIGPIPE
  164. /* ignore SIGPIPE signal */
  165. if((old_sigpipe_handler = signal(SIGPIPE, SIG_IGN)) == SIG_ERR)
  166. logmsg("cannot install SIGPIPE handler: %s", strerror(ERRNO));
  167. #endif
  168. #ifdef SIGALRM
  169. /* ignore SIGALRM signal */
  170. if((old_sigalrm_handler = signal(SIGALRM, SIG_IGN)) == SIG_ERR)
  171. logmsg("cannot install SIGALRM handler: %s", strerror(ERRNO));
  172. #endif
  173. #ifdef SIGINT
  174. /* handle SIGINT signal with our exit_signal_handler */
  175. if((old_sigint_handler = signal(SIGINT, exit_signal_handler)) == SIG_ERR)
  176. logmsg("cannot install SIGINT handler: %s", strerror(ERRNO));
  177. else
  178. siginterrupt(SIGINT, 1);
  179. #endif
  180. #ifdef SIGTERM
  181. /* handle SIGTERM signal with our exit_signal_handler */
  182. if((old_sigterm_handler = signal(SIGTERM, exit_signal_handler)) == SIG_ERR)
  183. logmsg("cannot install SIGTERM handler: %s", strerror(ERRNO));
  184. else
  185. siginterrupt(SIGTERM, 1);
  186. #endif
  187. }
  188. static void restore_signal_handlers(void)
  189. {
  190. #ifdef SIGHUP
  191. if(SIG_ERR != old_sighup_handler)
  192. (void)signal(SIGHUP, old_sighup_handler);
  193. #endif
  194. #ifdef SIGPIPE
  195. if(SIG_ERR != old_sigpipe_handler)
  196. (void)signal(SIGPIPE, old_sigpipe_handler);
  197. #endif
  198. #ifdef SIGALRM
  199. if(SIG_ERR != old_sigalrm_handler)
  200. (void)signal(SIGALRM, old_sigalrm_handler);
  201. #endif
  202. #ifdef SIGINT
  203. if(SIG_ERR != old_sigint_handler)
  204. (void)signal(SIGINT, old_sigint_handler);
  205. #endif
  206. #ifdef SIGTERM
  207. if(SIG_ERR != old_sigterm_handler)
  208. (void)signal(SIGTERM, old_sigterm_handler);
  209. #endif
  210. }
  211. /*
  212. * fullread is a wrapper around the read() function. This will repeat the call
  213. * to read() until it actually has read the complete number of bytes indicated
  214. * in nbytes or it fails with a condition that cannot be handled with a simple
  215. * retry of the read call.
  216. */
  217. static ssize_t fullread(int filedes, void *buffer, size_t nbytes)
  218. {
  219. int error;
  220. ssize_t rc;
  221. ssize_t nread = 0;
  222. do {
  223. rc = read(filedes, (unsigned char *)buffer + nread, nbytes - nread);
  224. if(got_exit_signal) {
  225. logmsg("signalled to die");
  226. return -1;
  227. }
  228. if(rc < 0) {
  229. error = ERRNO;
  230. if((error == EINTR) || (error == EAGAIN))
  231. continue;
  232. logmsg("unrecoverable read() failure: %s", strerror(error));
  233. return -1;
  234. }
  235. if(rc == 0) {
  236. logmsg("got 0 reading from stdin");
  237. return 0;
  238. }
  239. nread += rc;
  240. } while((size_t)nread < nbytes);
  241. if(verbose)
  242. logmsg("read %zd bytes", nread);
  243. return nread;
  244. }
  245. /*
  246. * fullwrite is a wrapper around the write() function. This will repeat the
  247. * call to write() until it actually has written the complete number of bytes
  248. * indicated in nbytes or it fails with a condition that cannot be handled
  249. * with a simple retry of the write call.
  250. */
  251. static ssize_t fullwrite(int filedes, const void *buffer, size_t nbytes)
  252. {
  253. int error;
  254. ssize_t wc;
  255. ssize_t nwrite = 0;
  256. do {
  257. wc = write(filedes, (unsigned char *)buffer + nwrite, nbytes - nwrite);
  258. if(got_exit_signal) {
  259. logmsg("signalled to die");
  260. return -1;
  261. }
  262. if(wc < 0) {
  263. error = ERRNO;
  264. if((error == EINTR) || (error == EAGAIN))
  265. continue;
  266. logmsg("unrecoverable write() failure: %s", strerror(error));
  267. return -1;
  268. }
  269. if(wc == 0) {
  270. logmsg("put 0 writing to stdout");
  271. return 0;
  272. }
  273. nwrite += wc;
  274. } while((size_t)nwrite < nbytes);
  275. if(verbose)
  276. logmsg("wrote %zd bytes", nwrite);
  277. return nwrite;
  278. }
  279. /*
  280. * read_stdin tries to read from stdin nbytes into the given buffer. This is a
  281. * blocking function that will only return TRUE when nbytes have actually been
  282. * read or FALSE when an unrecoverable error has been detected. Failure of this
  283. * function is an indication that the sockfilt process should terminate.
  284. */
  285. static bool read_stdin(void *buffer, size_t nbytes)
  286. {
  287. ssize_t nread = fullread(fileno(stdin), buffer, nbytes);
  288. if(nread != (ssize_t)nbytes) {
  289. logmsg("exiting...");
  290. return FALSE;
  291. }
  292. return TRUE;
  293. }
  294. /*
  295. * write_stdout tries to write to stdio nbytes from the given buffer. This is a
  296. * blocking function that will only return TRUE when nbytes have actually been
  297. * written or FALSE when an unrecoverable error has been detected. Failure of
  298. * this function is an indication that the sockfilt process should terminate.
  299. */
  300. static bool write_stdout(const void *buffer, size_t nbytes)
  301. {
  302. ssize_t nwrite = fullwrite(fileno(stdout), buffer, nbytes);
  303. if(nwrite != (ssize_t)nbytes) {
  304. logmsg("exiting...");
  305. return FALSE;
  306. }
  307. return TRUE;
  308. }
  309. static void lograw(unsigned char *buffer, ssize_t len)
  310. {
  311. char data[120];
  312. ssize_t i;
  313. unsigned char *ptr = buffer;
  314. char *optr = data;
  315. ssize_t width=0;
  316. for(i=0; i<len; i++) {
  317. switch(ptr[i]) {
  318. case '\n':
  319. sprintf(optr, "\\n");
  320. width += 2;
  321. optr += 2;
  322. break;
  323. case '\r':
  324. sprintf(optr, "\\r");
  325. width += 2;
  326. optr += 2;
  327. break;
  328. default:
  329. sprintf(optr, "%c", (ISGRAPH(ptr[i]) || ptr[i]==0x20) ?ptr[i]:'.');
  330. width++;
  331. optr++;
  332. break;
  333. }
  334. if(width>60) {
  335. logmsg("'%s'", data);
  336. width = 0;
  337. optr = data;
  338. }
  339. }
  340. if(width)
  341. logmsg("'%s'", data);
  342. }
  343. /*
  344. sockfdp is a pointer to an established stream or CURL_SOCKET_BAD
  345. if sockfd is CURL_SOCKET_BAD, listendfd is a listening socket we must
  346. accept()
  347. */
  348. static bool juggle(curl_socket_t *sockfdp,
  349. curl_socket_t listenfd,
  350. enum sockmode *mode)
  351. {
  352. struct timeval timeout;
  353. fd_set fds_read;
  354. fd_set fds_write;
  355. fd_set fds_err;
  356. curl_socket_t sockfd = CURL_SOCKET_BAD;
  357. curl_socket_t maxfd = CURL_SOCKET_BAD;
  358. ssize_t rc;
  359. ssize_t nread_socket;
  360. ssize_t bytes_written;
  361. ssize_t buffer_len;
  362. int error = 0;
  363. /* 'buffer' is this excessively large only to be able to support things like
  364. test 1003 which tests exceedingly large server response lines */
  365. unsigned char buffer[17010];
  366. char data[16];
  367. if(got_exit_signal) {
  368. logmsg("signalled to die, exiting...");
  369. return FALSE;
  370. }
  371. #ifdef HAVE_GETPPID
  372. /* As a last resort, quit if sockfilt process becomes orphan. Just in case
  373. parent ftpserver process has died without killing its sockfilt children */
  374. if(getppid() <= 1) {
  375. logmsg("process becomes orphan, exiting");
  376. return FALSE;
  377. }
  378. #endif
  379. timeout.tv_sec = 120;
  380. timeout.tv_usec = 0;
  381. FD_ZERO(&fds_read);
  382. FD_ZERO(&fds_write);
  383. FD_ZERO(&fds_err);
  384. FD_SET(fileno(stdin), &fds_read);
  385. switch(*mode) {
  386. case PASSIVE_LISTEN:
  387. /* server mode */
  388. sockfd = listenfd;
  389. /* there's always a socket to wait for */
  390. FD_SET(sockfd, &fds_read);
  391. maxfd = sockfd;
  392. break;
  393. case PASSIVE_CONNECT:
  394. sockfd = *sockfdp;
  395. if(CURL_SOCKET_BAD == sockfd) {
  396. /* eeek, we are supposedly connected and then this cannot be -1 ! */
  397. logmsg("socket is -1! on %s:%d", __FILE__, __LINE__);
  398. maxfd = 0; /* stdin */
  399. }
  400. else {
  401. /* there's always a socket to wait for */
  402. FD_SET(sockfd, &fds_read);
  403. maxfd = sockfd;
  404. }
  405. break;
  406. case ACTIVE:
  407. sockfd = *sockfdp;
  408. /* sockfd turns CURL_SOCKET_BAD when our connection has been closed */
  409. if(CURL_SOCKET_BAD != sockfd) {
  410. FD_SET(sockfd, &fds_read);
  411. maxfd = sockfd;
  412. }
  413. else {
  414. logmsg("No socket to read on");
  415. maxfd = 0;
  416. }
  417. break;
  418. case ACTIVE_DISCONNECT:
  419. logmsg("disconnected, no socket to read on");
  420. maxfd = 0;
  421. sockfd = CURL_SOCKET_BAD;
  422. break;
  423. } /* switch(*mode) */
  424. do {
  425. rc = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, &timeout);
  426. if(got_exit_signal) {
  427. logmsg("signalled to die, exiting...");
  428. return FALSE;
  429. }
  430. } while((rc == -1) && ((error = SOCKERRNO) == EINTR));
  431. if(rc < 0) {
  432. logmsg("select() failed with error: (%d) %s",
  433. error, strerror(error));
  434. return FALSE;
  435. }
  436. if(rc == 0)
  437. /* timeout */
  438. return TRUE;
  439. if(FD_ISSET(fileno(stdin), &fds_read)) {
  440. /* read from stdin, commands/data to be dealt with and possibly passed on
  441. to the socket
  442. protocol:
  443. 4 letter command + LF [mandatory]
  444. 4-digit hexadecimal data length + LF [if the command takes data]
  445. data [the data being as long as set above]
  446. Commands:
  447. DATA - plain pass-thru data
  448. */
  449. if(!read_stdin(buffer, 5))
  450. return FALSE;
  451. logmsg("Received %c%c%c%c (on stdin)",
  452. buffer[0], buffer[1], buffer[2], buffer[3] );
  453. if(!memcmp("PING", buffer, 4)) {
  454. /* send reply on stdout, just proving we are alive */
  455. if(!write_stdout("PONG\n", 5))
  456. return FALSE;
  457. }
  458. else if(!memcmp("PORT", buffer, 4)) {
  459. /* Question asking us what PORT number we are listening to.
  460. Replies to PORT with "IPv[num]/[port]" */
  461. sprintf((char *)buffer, "%s/%d\n", ipv_inuse, (int)port);
  462. buffer_len = (ssize_t)strlen((char *)buffer);
  463. snprintf(data, sizeof(data), "PORT\n%04x\n", buffer_len);
  464. if(!write_stdout(data, 10))
  465. return FALSE;
  466. if(!write_stdout(buffer, buffer_len))
  467. return FALSE;
  468. }
  469. else if(!memcmp("QUIT", buffer, 4)) {
  470. /* just die */
  471. logmsg("quits");
  472. return FALSE;
  473. }
  474. else if(!memcmp("DATA", buffer, 4)) {
  475. /* data IN => data OUT */
  476. if(!read_stdin(buffer, 5))
  477. return FALSE;
  478. buffer[5] = '\0';
  479. buffer_len = (ssize_t)strtol((char *)buffer, NULL, 16);
  480. if (buffer_len > (ssize_t)sizeof(buffer)) {
  481. logmsg("ERROR: Buffer size (%zu bytes) too small for data size "
  482. "(%zd bytes)", sizeof(buffer), buffer_len);
  483. return FALSE;
  484. }
  485. logmsg("> %zd bytes data, server => client", buffer_len);
  486. if(!read_stdin(buffer, buffer_len))
  487. return FALSE;
  488. lograw(buffer, buffer_len);
  489. if(*mode == PASSIVE_LISTEN) {
  490. logmsg("*** We are disconnected!");
  491. if(!write_stdout("DISC\n", 5))
  492. return FALSE;
  493. }
  494. else {
  495. /* send away on the socket */
  496. bytes_written = swrite(sockfd, buffer, buffer_len);
  497. if(bytes_written != buffer_len) {
  498. logmsg("Not all data was sent. Bytes to send: %zd sent: %zd",
  499. buffer_len, bytes_written);
  500. }
  501. }
  502. }
  503. else if(!memcmp("DISC", buffer, 4)) {
  504. /* disconnect! */
  505. if(!write_stdout("DISC\n", 5))
  506. return FALSE;
  507. if(sockfd != CURL_SOCKET_BAD) {
  508. logmsg("====> Client forcibly disconnected");
  509. sclose(sockfd);
  510. *sockfdp = CURL_SOCKET_BAD;
  511. if(*mode == PASSIVE_CONNECT)
  512. *mode = PASSIVE_LISTEN;
  513. else
  514. *mode = ACTIVE_DISCONNECT;
  515. }
  516. else
  517. logmsg("attempt to close already dead connection");
  518. return TRUE;
  519. }
  520. }
  521. if((sockfd != CURL_SOCKET_BAD) && (FD_ISSET(sockfd, &fds_read)) ) {
  522. if(*mode == PASSIVE_LISTEN) {
  523. /* there's no stream set up yet, this is an indication that there's a
  524. client connecting. */
  525. sockfd = accept(sockfd, NULL, NULL);
  526. if(CURL_SOCKET_BAD == sockfd)
  527. logmsg("accept() failed");
  528. else {
  529. logmsg("====> Client connect");
  530. if(!write_stdout("CNCT\n", 5))
  531. return FALSE;
  532. *sockfdp = sockfd; /* store the new socket */
  533. *mode = PASSIVE_CONNECT; /* we have connected */
  534. }
  535. return TRUE;
  536. }
  537. /* read from socket, pass on data to stdout */
  538. nread_socket = sread(sockfd, buffer, sizeof(buffer));
  539. if(nread_socket <= 0) {
  540. logmsg("====> Client disconnect");
  541. if(!write_stdout("DISC\n", 5))
  542. return FALSE;
  543. sclose(sockfd);
  544. *sockfdp = CURL_SOCKET_BAD;
  545. if(*mode == PASSIVE_CONNECT)
  546. *mode = PASSIVE_LISTEN;
  547. else
  548. *mode = ACTIVE_DISCONNECT;
  549. return TRUE;
  550. }
  551. snprintf(data, sizeof(data), "DATA\n%04x\n", nread_socket);
  552. if(!write_stdout(data, 10))
  553. return FALSE;
  554. if(!write_stdout(buffer, nread_socket))
  555. return FALSE;
  556. logmsg("< %zd bytes data, client => server", nread_socket);
  557. lograw(buffer, nread_socket);
  558. }
  559. return TRUE;
  560. }
  561. static curl_socket_t sockdaemon(curl_socket_t sock,
  562. unsigned short *listenport)
  563. {
  564. /* passive daemon style */
  565. struct sockaddr_in me;
  566. #ifdef ENABLE_IPV6
  567. struct sockaddr_in6 me6;
  568. #endif /* ENABLE_IPV6 */
  569. int flag = 1;
  570. int rc;
  571. int totdelay = 0;
  572. int maxretr = 10;
  573. int delay= 20;
  574. int attempt = 0;
  575. int error = 0;
  576. do {
  577. attempt++;
  578. rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  579. (void *)&flag, sizeof(flag));
  580. if(rc) {
  581. error = SOCKERRNO;
  582. if(maxretr) {
  583. rc = wait_ms(delay);
  584. if(rc) {
  585. /* should not happen */
  586. error = SOCKERRNO;
  587. logmsg("wait_ms() failed: (%d) %s", error, strerror(error));
  588. sclose(sock);
  589. return CURL_SOCKET_BAD;
  590. }
  591. if(got_exit_signal) {
  592. logmsg("signalled to die, exiting...");
  593. sclose(sock);
  594. return CURL_SOCKET_BAD;
  595. }
  596. totdelay += delay;
  597. delay *= 2; /* double the sleep for next attempt */
  598. }
  599. }
  600. } while(rc && maxretr--);
  601. if(rc) {
  602. logmsg("setsockopt(SO_REUSEADDR) failed %d times in %d ms. Error: (%d) %s",
  603. attempt, totdelay, error, strerror(error));
  604. logmsg("Continuing anyway...");
  605. }
  606. #ifdef ENABLE_IPV6
  607. if(!use_ipv6) {
  608. #endif
  609. memset(&me, 0, sizeof(me));
  610. me.sin_family = AF_INET;
  611. me.sin_addr.s_addr = INADDR_ANY;
  612. me.sin_port = htons(*listenport);
  613. rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
  614. #ifdef ENABLE_IPV6
  615. }
  616. else {
  617. memset(&me6, 0, sizeof(me6));
  618. me6.sin6_family = AF_INET6;
  619. me6.sin6_addr = in6addr_any;
  620. me6.sin6_port = htons(*listenport);
  621. rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
  622. }
  623. #endif /* ENABLE_IPV6 */
  624. if(rc) {
  625. error = SOCKERRNO;
  626. logmsg("Error binding socket: (%d) %s", error, strerror(error));
  627. sclose(sock);
  628. return CURL_SOCKET_BAD;
  629. }
  630. if(!*listenport) {
  631. /* The system picked a port number, now figure out which port we actually
  632. got */
  633. /* we succeeded to bind */
  634. struct sockaddr_in add;
  635. socklen_t socksize = sizeof(add);
  636. if(getsockname(sock, (struct sockaddr *) &add,
  637. &socksize)<0) {
  638. error = SOCKERRNO;
  639. logmsg("getsockname() failed with error: (%d) %s",
  640. error, strerror(error));
  641. sclose(sock);
  642. return CURL_SOCKET_BAD;
  643. }
  644. *listenport = ntohs(add.sin_port);
  645. }
  646. /* start accepting connections */
  647. rc = listen(sock, 5);
  648. if(0 != rc) {
  649. error = SOCKERRNO;
  650. logmsg("listen() failed with error: (%d) %s",
  651. error, strerror(error));
  652. sclose(sock);
  653. return CURL_SOCKET_BAD;
  654. }
  655. return sock;
  656. }
  657. int main(int argc, char *argv[])
  658. {
  659. struct sockaddr_in me;
  660. #ifdef ENABLE_IPV6
  661. struct sockaddr_in6 me6;
  662. #endif /* ENABLE_IPV6 */
  663. curl_socket_t sock = CURL_SOCKET_BAD;
  664. curl_socket_t msgsock = CURL_SOCKET_BAD;
  665. int wrotepidfile = 0;
  666. char *pidname= (char *)".sockfilt.pid";
  667. int rc;
  668. int error;
  669. int arg=1;
  670. enum sockmode mode = PASSIVE_LISTEN; /* default */
  671. const char *addr = NULL;
  672. while(argc>arg) {
  673. if(!strcmp("--version", argv[arg])) {
  674. printf("sockfilt IPv4%s\n",
  675. #ifdef ENABLE_IPV6
  676. "/IPv6"
  677. #else
  678. ""
  679. #endif
  680. );
  681. return 0;
  682. }
  683. else if(!strcmp("--verbose", argv[arg])) {
  684. verbose = TRUE;
  685. arg++;
  686. }
  687. else if(!strcmp("--pidfile", argv[arg])) {
  688. arg++;
  689. if(argc>arg)
  690. pidname = argv[arg++];
  691. }
  692. else if(!strcmp("--logfile", argv[arg])) {
  693. arg++;
  694. if(argc>arg)
  695. serverlogfile = argv[arg++];
  696. }
  697. else if(!strcmp("--ipv6", argv[arg])) {
  698. #ifdef ENABLE_IPV6
  699. ipv_inuse = "IPv6";
  700. use_ipv6 = TRUE;
  701. #endif
  702. arg++;
  703. }
  704. else if(!strcmp("--ipv4", argv[arg])) {
  705. /* for completeness, we support this option as well */
  706. #ifdef ENABLE_IPV6
  707. ipv_inuse = "IPv4";
  708. use_ipv6 = FALSE;
  709. #endif
  710. arg++;
  711. }
  712. else if(!strcmp("--port", argv[arg])) {
  713. arg++;
  714. if(argc>arg) {
  715. port = (unsigned short)atoi(argv[arg]);
  716. arg++;
  717. }
  718. }
  719. else if(!strcmp("--connect", argv[arg])) {
  720. /* Asked to actively connect to the specified local port instead of
  721. doing a passive server-style listening. */
  722. arg++;
  723. if(argc>arg) {
  724. connectport = (unsigned short)atoi(argv[arg]);
  725. arg++;
  726. }
  727. }
  728. else if(!strcmp("--addr", argv[arg])) {
  729. /* Set an IP address to use with --connect; otherwise use localhost */
  730. arg++;
  731. if(argc>arg) {
  732. addr = argv[arg];
  733. arg++;
  734. }
  735. }
  736. else {
  737. puts("Usage: sockfilt [option]\n"
  738. " --version\n"
  739. " --verbose\n"
  740. " --logfile [file]\n"
  741. " --pidfile [file]\n"
  742. " --ipv4\n"
  743. " --ipv6\n"
  744. " --port [port]\n"
  745. " --connect [port]\n"
  746. " --addr [address]");
  747. return 0;
  748. }
  749. }
  750. #ifdef WIN32
  751. win32_init();
  752. atexit(win32_cleanup);
  753. #endif
  754. install_signal_handlers();
  755. #ifdef ENABLE_IPV6
  756. if(!use_ipv6)
  757. #endif
  758. sock = socket(AF_INET, SOCK_STREAM, 0);
  759. #ifdef ENABLE_IPV6
  760. else
  761. sock = socket(AF_INET6, SOCK_STREAM, 0);
  762. #endif
  763. if(CURL_SOCKET_BAD == sock) {
  764. error = SOCKERRNO;
  765. logmsg("Error creating socket: (%d) %s",
  766. error, strerror(error));
  767. goto sockfilt_cleanup;
  768. }
  769. if(connectport) {
  770. /* Active mode, we should connect to the given port number */
  771. mode = ACTIVE;
  772. #ifdef ENABLE_IPV6
  773. if(!use_ipv6) {
  774. #endif
  775. memset(&me, 0, sizeof(me));
  776. me.sin_family = AF_INET;
  777. me.sin_port = htons(connectport);
  778. me.sin_addr.s_addr = INADDR_ANY;
  779. if (!addr)
  780. addr = "127.0.0.1";
  781. Curl_inet_pton(AF_INET, addr, &me.sin_addr);
  782. rc = connect(sock, (struct sockaddr *) &me, sizeof(me));
  783. #ifdef ENABLE_IPV6
  784. }
  785. else {
  786. memset(&me6, 0, sizeof(me6));
  787. me6.sin6_family = AF_INET6;
  788. me6.sin6_port = htons(connectport);
  789. if (!addr)
  790. addr = "::1";
  791. Curl_inet_pton(AF_INET6, addr, &me6.sin6_addr);
  792. rc = connect(sock, (struct sockaddr *) &me6, sizeof(me6));
  793. }
  794. #endif /* ENABLE_IPV6 */
  795. if(rc) {
  796. error = SOCKERRNO;
  797. logmsg("Error connecting to port %hu: (%d) %s",
  798. connectport, error, strerror(error));
  799. goto sockfilt_cleanup;
  800. }
  801. logmsg("====> Client connect");
  802. msgsock = sock; /* use this as stream */
  803. }
  804. else {
  805. /* passive daemon style */
  806. sock = sockdaemon(sock, &port);
  807. if(CURL_SOCKET_BAD == sock)
  808. goto sockfilt_cleanup;
  809. msgsock = CURL_SOCKET_BAD; /* no stream socket yet */
  810. }
  811. logmsg("Running %s version", ipv_inuse);
  812. if(connectport)
  813. logmsg("Connected to port %hu", connectport);
  814. else
  815. logmsg("Listening on port %hu", port);
  816. wrotepidfile = write_pidfile(pidname);
  817. if(!wrotepidfile)
  818. goto sockfilt_cleanup;
  819. while(juggle(&msgsock, sock, &mode));
  820. sockfilt_cleanup:
  821. if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
  822. sclose(msgsock);
  823. if(sock != CURL_SOCKET_BAD)
  824. sclose(sock);
  825. if(wrotepidfile)
  826. unlink(pidname);
  827. restore_signal_handlers();
  828. if(got_exit_signal) {
  829. logmsg("============> sockfilt exits with signal (%d)", exit_signal);
  830. /*
  831. * To properly set the return status of the process we
  832. * must raise the same signal SIGINT or SIGTERM that we
  833. * caught and let the old handler take care of it.
  834. */
  835. raise(exit_signal);
  836. }
  837. logmsg("============> sockfilt quits");
  838. return 0;
  839. }