sockfilt.c 25 KB

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