sockfilt.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "server_setup.h"
  23. /* 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. #ifdef HAVE_SIGNAL_H
  81. #include <signal.h>
  82. #endif
  83. #ifdef HAVE_UNISTD_H
  84. #include <unistd.h>
  85. #endif
  86. #ifdef HAVE_SYS_SOCKET_H
  87. #include <sys/socket.h>
  88. #endif
  89. #ifdef HAVE_NETINET_IN_H
  90. #include <netinet/in.h>
  91. #endif
  92. #ifdef HAVE_ARPA_INET_H
  93. #include <arpa/inet.h>
  94. #endif
  95. #ifdef HAVE_NETDB_H
  96. #include <netdb.h>
  97. #endif
  98. #define ENABLE_CURLX_PRINTF
  99. /* make the curlx header define all printf() functions to use the curlx_*
  100. versions instead */
  101. #include "curlx.h" /* from the private lib dir */
  102. #include "getpart.h"
  103. #include "inet_pton.h"
  104. #include "util.h"
  105. #include "server_sockaddr.h"
  106. /* include memdebug.h last */
  107. #include "memdebug.h"
  108. #define DEFAULT_PORT 8999
  109. #ifndef DEFAULT_LOGFILE
  110. #define DEFAULT_LOGFILE "log/sockfilt.log"
  111. #endif
  112. const char *serverlogfile = DEFAULT_LOGFILE;
  113. static bool verbose = FALSE;
  114. static bool bind_only = 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. #ifdef SIGHUP
  134. static SIGHANDLER_T old_sighup_handler = SIG_ERR;
  135. #endif
  136. #ifdef SIGPIPE
  137. static SIGHANDLER_T old_sigpipe_handler = SIG_ERR;
  138. #endif
  139. #ifdef SIGALRM
  140. static SIGHANDLER_T old_sigalrm_handler = SIG_ERR;
  141. #endif
  142. #ifdef SIGINT
  143. static SIGHANDLER_T old_sigint_handler = SIG_ERR;
  144. #endif
  145. #ifdef SIGTERM
  146. static SIGHANDLER_T old_sigterm_handler = SIG_ERR;
  147. #endif
  148. /* var which if set indicates that the program should finish execution */
  149. SIG_ATOMIC_T got_exit_signal = 0;
  150. /* if next is set indicates the first signal handled in exit_signal_handler */
  151. static volatile int exit_signal = 0;
  152. /* signal handler that will be triggered to indicate that the program
  153. should finish its execution in a controlled manner as soon as possible.
  154. The first time this is called it will set got_exit_signal to one and
  155. store in exit_signal the signal that triggered its execution. */
  156. static RETSIGTYPE exit_signal_handler(int signum)
  157. {
  158. int old_errno = ERRNO;
  159. if(got_exit_signal == 0) {
  160. got_exit_signal = 1;
  161. exit_signal = signum;
  162. }
  163. (void)signal(signum, exit_signal_handler);
  164. SET_ERRNO(old_errno);
  165. }
  166. static void install_signal_handlers(void)
  167. {
  168. #ifdef SIGHUP
  169. /* ignore SIGHUP signal */
  170. if((old_sighup_handler = signal(SIGHUP, SIG_IGN)) == SIG_ERR)
  171. logmsg("cannot install SIGHUP handler: %s", strerror(ERRNO));
  172. #endif
  173. #ifdef SIGPIPE
  174. /* ignore SIGPIPE signal */
  175. if((old_sigpipe_handler = signal(SIGPIPE, SIG_IGN)) == SIG_ERR)
  176. logmsg("cannot install SIGPIPE handler: %s", strerror(ERRNO));
  177. #endif
  178. #ifdef SIGALRM
  179. /* ignore SIGALRM signal */
  180. if((old_sigalrm_handler = signal(SIGALRM, SIG_IGN)) == SIG_ERR)
  181. logmsg("cannot install SIGALRM handler: %s", strerror(ERRNO));
  182. #endif
  183. #ifdef SIGINT
  184. /* handle SIGINT signal with our exit_signal_handler */
  185. if((old_sigint_handler = signal(SIGINT, exit_signal_handler)) == SIG_ERR)
  186. logmsg("cannot install SIGINT handler: %s", strerror(ERRNO));
  187. else
  188. siginterrupt(SIGINT, 1);
  189. #endif
  190. #ifdef SIGTERM
  191. /* handle SIGTERM signal with our exit_signal_handler */
  192. if((old_sigterm_handler = signal(SIGTERM, exit_signal_handler)) == SIG_ERR)
  193. logmsg("cannot install SIGTERM handler: %s", strerror(ERRNO));
  194. else
  195. siginterrupt(SIGTERM, 1);
  196. #endif
  197. }
  198. static void restore_signal_handlers(void)
  199. {
  200. #ifdef SIGHUP
  201. if(SIG_ERR != old_sighup_handler)
  202. (void)signal(SIGHUP, old_sighup_handler);
  203. #endif
  204. #ifdef SIGPIPE
  205. if(SIG_ERR != old_sigpipe_handler)
  206. (void)signal(SIGPIPE, old_sigpipe_handler);
  207. #endif
  208. #ifdef SIGALRM
  209. if(SIG_ERR != old_sigalrm_handler)
  210. (void)signal(SIGALRM, old_sigalrm_handler);
  211. #endif
  212. #ifdef SIGINT
  213. if(SIG_ERR != old_sigint_handler)
  214. (void)signal(SIGINT, old_sigint_handler);
  215. #endif
  216. #ifdef SIGTERM
  217. if(SIG_ERR != old_sigterm_handler)
  218. (void)signal(SIGTERM, old_sigterm_handler);
  219. #endif
  220. }
  221. /*
  222. * fullread is a wrapper around the read() function. This will repeat the call
  223. * to read() until it actually has read the complete number of bytes indicated
  224. * in nbytes or it fails with a condition that cannot be handled with a simple
  225. * retry of the read call.
  226. */
  227. static ssize_t fullread(int filedes, void *buffer, size_t nbytes)
  228. {
  229. int error;
  230. ssize_t rc;
  231. ssize_t nread = 0;
  232. do {
  233. rc = read(filedes, (unsigned char *)buffer + nread, nbytes - nread);
  234. if(got_exit_signal) {
  235. logmsg("signalled to die");
  236. return -1;
  237. }
  238. if(rc < 0) {
  239. error = ERRNO;
  240. if((error == EINTR) || (error == EAGAIN))
  241. continue;
  242. logmsg("unrecoverable read() failure: %s", strerror(error));
  243. return -1;
  244. }
  245. if(rc == 0) {
  246. logmsg("got 0 reading from stdin");
  247. return 0;
  248. }
  249. nread += rc;
  250. } while((size_t)nread < nbytes);
  251. if(verbose)
  252. logmsg("read %zd bytes", nread);
  253. return nread;
  254. }
  255. /*
  256. * fullwrite is a wrapper around the write() function. This will repeat the
  257. * call to write() until it actually has written the complete number of bytes
  258. * indicated in nbytes or it fails with a condition that cannot be handled
  259. * with a simple retry of the write call.
  260. */
  261. static ssize_t fullwrite(int filedes, const void *buffer, size_t nbytes)
  262. {
  263. int error;
  264. ssize_t wc;
  265. ssize_t nwrite = 0;
  266. do {
  267. wc = write(filedes, (unsigned char *)buffer + nwrite, nbytes - nwrite);
  268. if(got_exit_signal) {
  269. logmsg("signalled to die");
  270. return -1;
  271. }
  272. if(wc < 0) {
  273. error = ERRNO;
  274. if((error == EINTR) || (error == EAGAIN))
  275. continue;
  276. logmsg("unrecoverable write() failure: %s", strerror(error));
  277. return -1;
  278. }
  279. if(wc == 0) {
  280. logmsg("put 0 writing to stdout");
  281. return 0;
  282. }
  283. nwrite += wc;
  284. } while((size_t)nwrite < nbytes);
  285. if(verbose)
  286. logmsg("wrote %zd bytes", nwrite);
  287. return nwrite;
  288. }
  289. /*
  290. * read_stdin tries to read from stdin nbytes into the given buffer. This is a
  291. * blocking function that will only return TRUE when nbytes have actually been
  292. * read or FALSE when an unrecoverable error has been detected. Failure of this
  293. * function is an indication that the sockfilt process should terminate.
  294. */
  295. static bool read_stdin(void *buffer, size_t nbytes)
  296. {
  297. ssize_t nread = fullread(fileno(stdin), buffer, nbytes);
  298. if(nread != (ssize_t)nbytes) {
  299. logmsg("exiting...");
  300. return FALSE;
  301. }
  302. return TRUE;
  303. }
  304. /*
  305. * write_stdout tries to write to stdio nbytes from the given buffer. This is a
  306. * blocking function that will only return TRUE when nbytes have actually been
  307. * written or FALSE when an unrecoverable error has been detected. Failure of
  308. * this function is an indication that the sockfilt process should terminate.
  309. */
  310. static bool write_stdout(const void *buffer, size_t nbytes)
  311. {
  312. ssize_t nwrite = fullwrite(fileno(stdout), buffer, nbytes);
  313. if(nwrite != (ssize_t)nbytes) {
  314. logmsg("exiting...");
  315. return FALSE;
  316. }
  317. return TRUE;
  318. }
  319. static void lograw(unsigned char *buffer, ssize_t len)
  320. {
  321. char data[120];
  322. ssize_t i;
  323. unsigned char *ptr = buffer;
  324. char *optr = data;
  325. ssize_t width=0;
  326. for(i=0; i<len; i++) {
  327. switch(ptr[i]) {
  328. case '\n':
  329. sprintf(optr, "\\n");
  330. width += 2;
  331. optr += 2;
  332. break;
  333. case '\r':
  334. sprintf(optr, "\\r");
  335. width += 2;
  336. optr += 2;
  337. break;
  338. default:
  339. sprintf(optr, "%c", (ISGRAPH(ptr[i]) || ptr[i]==0x20) ?ptr[i]:'.');
  340. width++;
  341. optr++;
  342. break;
  343. }
  344. if(width>60) {
  345. logmsg("'%s'", data);
  346. width = 0;
  347. optr = data;
  348. }
  349. }
  350. if(width)
  351. logmsg("'%s'", data);
  352. }
  353. /*
  354. sockfdp is a pointer to an established stream or CURL_SOCKET_BAD
  355. if sockfd is CURL_SOCKET_BAD, listendfd is a listening socket we must
  356. accept()
  357. */
  358. static bool juggle(curl_socket_t *sockfdp,
  359. curl_socket_t listenfd,
  360. enum sockmode *mode)
  361. {
  362. struct timeval timeout;
  363. fd_set fds_read;
  364. fd_set fds_write;
  365. fd_set fds_err;
  366. curl_socket_t sockfd = CURL_SOCKET_BAD;
  367. curl_socket_t maxfd = CURL_SOCKET_BAD;
  368. ssize_t rc;
  369. ssize_t nread_socket;
  370. ssize_t bytes_written;
  371. ssize_t buffer_len;
  372. int error = 0;
  373. /* 'buffer' is this excessively large only to be able to support things like
  374. test 1003 which tests exceedingly large server response lines */
  375. unsigned char buffer[17010];
  376. char data[16];
  377. if(got_exit_signal) {
  378. logmsg("signalled to die, exiting...");
  379. return FALSE;
  380. }
  381. #ifdef HAVE_GETPPID
  382. /* As a last resort, quit if sockfilt process becomes orphan. Just in case
  383. parent ftpserver process has died without killing its sockfilt children */
  384. if(getppid() <= 1) {
  385. logmsg("process becomes orphan, exiting");
  386. return FALSE;
  387. }
  388. #endif
  389. timeout.tv_sec = 120;
  390. timeout.tv_usec = 0;
  391. FD_ZERO(&fds_read);
  392. FD_ZERO(&fds_write);
  393. FD_ZERO(&fds_err);
  394. #ifdef USE_WINSOCK
  395. /*
  396. ** WinSock select() does not support standard file descriptors,
  397. ** it can only check SOCKETs. Since this program in its current
  398. ** state will not work on WinSock based systems, next line is
  399. ** commented out to allow warning-free compilation awaiting the
  400. ** day it will be fixed to also run on WinSock systems.
  401. */
  402. #else
  403. FD_SET(fileno(stdin), &fds_read);
  404. #endif
  405. switch(*mode) {
  406. case PASSIVE_LISTEN:
  407. /* server mode */
  408. sockfd = listenfd;
  409. /* there's always a socket to wait for */
  410. FD_SET(sockfd, &fds_read);
  411. maxfd = sockfd;
  412. break;
  413. case PASSIVE_CONNECT:
  414. sockfd = *sockfdp;
  415. if(CURL_SOCKET_BAD == sockfd) {
  416. /* eeek, we are supposedly connected and then this cannot be -1 ! */
  417. logmsg("socket is -1! on %s:%d", __FILE__, __LINE__);
  418. maxfd = 0; /* stdin */
  419. }
  420. else {
  421. /* there's always a socket to wait for */
  422. FD_SET(sockfd, &fds_read);
  423. maxfd = sockfd;
  424. }
  425. break;
  426. case ACTIVE:
  427. sockfd = *sockfdp;
  428. /* sockfd turns CURL_SOCKET_BAD when our connection has been closed */
  429. if(CURL_SOCKET_BAD != sockfd) {
  430. FD_SET(sockfd, &fds_read);
  431. maxfd = sockfd;
  432. }
  433. else {
  434. logmsg("No socket to read on");
  435. maxfd = 0;
  436. }
  437. break;
  438. case ACTIVE_DISCONNECT:
  439. logmsg("disconnected, no socket to read on");
  440. maxfd = 0;
  441. sockfd = CURL_SOCKET_BAD;
  442. break;
  443. } /* switch(*mode) */
  444. do {
  445. rc = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, &timeout);
  446. if(got_exit_signal) {
  447. logmsg("signalled to die, exiting...");
  448. return FALSE;
  449. }
  450. } while((rc == -1) && ((error = SOCKERRNO) == EINTR));
  451. if(rc < 0) {
  452. logmsg("select() failed with error: (%d) %s",
  453. error, strerror(error));
  454. return FALSE;
  455. }
  456. if(rc == 0)
  457. /* timeout */
  458. return TRUE;
  459. if(FD_ISSET(fileno(stdin), &fds_read)) {
  460. /* read from stdin, commands/data to be dealt with and possibly passed on
  461. to the socket
  462. protocol:
  463. 4 letter command + LF [mandatory]
  464. 4-digit hexadecimal data length + LF [if the command takes data]
  465. data [the data being as long as set above]
  466. Commands:
  467. DATA - plain pass-thru data
  468. */
  469. if(!read_stdin(buffer, 5))
  470. return FALSE;
  471. logmsg("Received %c%c%c%c (on stdin)",
  472. buffer[0], buffer[1], buffer[2], buffer[3] );
  473. if(!memcmp("PING", buffer, 4)) {
  474. /* send reply on stdout, just proving we are alive */
  475. if(!write_stdout("PONG\n", 5))
  476. return FALSE;
  477. }
  478. else if(!memcmp("PORT", buffer, 4)) {
  479. /* Question asking us what PORT number we are listening to.
  480. Replies to PORT with "IPv[num]/[port]" */
  481. sprintf((char *)buffer, "%s/%hu\n", ipv_inuse, port);
  482. buffer_len = (ssize_t)strlen((char *)buffer);
  483. snprintf(data, sizeof(data), "PORT\n%04zx\n", buffer_len);
  484. if(!write_stdout(data, 10))
  485. return FALSE;
  486. if(!write_stdout(buffer, buffer_len))
  487. return FALSE;
  488. }
  489. else if(!memcmp("QUIT", buffer, 4)) {
  490. /* just die */
  491. logmsg("quits");
  492. return FALSE;
  493. }
  494. else if(!memcmp("DATA", buffer, 4)) {
  495. /* data IN => data OUT */
  496. if(!read_stdin(buffer, 5))
  497. return FALSE;
  498. buffer[5] = '\0';
  499. buffer_len = (ssize_t)strtol((char *)buffer, NULL, 16);
  500. if (buffer_len > (ssize_t)sizeof(buffer)) {
  501. logmsg("ERROR: Buffer size (%zu bytes) too small for data size "
  502. "(%zd bytes)", sizeof(buffer), buffer_len);
  503. return FALSE;
  504. }
  505. logmsg("> %zd bytes data, server => client", buffer_len);
  506. if(!read_stdin(buffer, buffer_len))
  507. return FALSE;
  508. lograw(buffer, buffer_len);
  509. if(*mode == PASSIVE_LISTEN) {
  510. logmsg("*** We are disconnected!");
  511. if(!write_stdout("DISC\n", 5))
  512. return FALSE;
  513. }
  514. else {
  515. /* send away on the socket */
  516. bytes_written = swrite(sockfd, buffer, buffer_len);
  517. if(bytes_written != buffer_len) {
  518. logmsg("Not all data was sent. Bytes to send: %zd sent: %zd",
  519. buffer_len, bytes_written);
  520. }
  521. }
  522. }
  523. else if(!memcmp("DISC", buffer, 4)) {
  524. /* disconnect! */
  525. if(!write_stdout("DISC\n", 5))
  526. return FALSE;
  527. if(sockfd != CURL_SOCKET_BAD) {
  528. logmsg("====> Client forcibly disconnected");
  529. sclose(sockfd);
  530. *sockfdp = CURL_SOCKET_BAD;
  531. if(*mode == PASSIVE_CONNECT)
  532. *mode = PASSIVE_LISTEN;
  533. else
  534. *mode = ACTIVE_DISCONNECT;
  535. }
  536. else
  537. logmsg("attempt to close already dead connection");
  538. return TRUE;
  539. }
  540. }
  541. if((sockfd != CURL_SOCKET_BAD) && (FD_ISSET(sockfd, &fds_read)) ) {
  542. if(*mode == PASSIVE_LISTEN) {
  543. /* there's no stream set up yet, this is an indication that there's a
  544. client connecting. */
  545. sockfd = accept(sockfd, NULL, NULL);
  546. if(CURL_SOCKET_BAD == sockfd)
  547. logmsg("accept() failed");
  548. else {
  549. logmsg("====> Client connect");
  550. if(!write_stdout("CNCT\n", 5))
  551. return FALSE;
  552. *sockfdp = sockfd; /* store the new socket */
  553. *mode = PASSIVE_CONNECT; /* we have connected */
  554. }
  555. return TRUE;
  556. }
  557. /* read from socket, pass on data to stdout */
  558. nread_socket = sread(sockfd, buffer, sizeof(buffer));
  559. if(nread_socket <= 0) {
  560. logmsg("====> Client disconnect");
  561. if(!write_stdout("DISC\n", 5))
  562. return FALSE;
  563. sclose(sockfd);
  564. *sockfdp = CURL_SOCKET_BAD;
  565. if(*mode == PASSIVE_CONNECT)
  566. *mode = PASSIVE_LISTEN;
  567. else
  568. *mode = ACTIVE_DISCONNECT;
  569. return TRUE;
  570. }
  571. snprintf(data, sizeof(data), "DATA\n%04zx\n", nread_socket);
  572. if(!write_stdout(data, 10))
  573. return FALSE;
  574. if(!write_stdout(buffer, nread_socket))
  575. return FALSE;
  576. logmsg("< %zd bytes data, client => server", nread_socket);
  577. lograw(buffer, nread_socket);
  578. }
  579. return TRUE;
  580. }
  581. static curl_socket_t sockdaemon(curl_socket_t sock,
  582. unsigned short *listenport)
  583. {
  584. /* passive daemon style */
  585. srvr_sockaddr_union_t listener;
  586. int flag;
  587. int rc;
  588. int totdelay = 0;
  589. int maxretr = 10;
  590. int delay= 20;
  591. int attempt = 0;
  592. int error = 0;
  593. do {
  594. attempt++;
  595. flag = 1;
  596. rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  597. (void *)&flag, sizeof(flag));
  598. if(rc) {
  599. error = SOCKERRNO;
  600. logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
  601. error, strerror(error));
  602. if(maxretr) {
  603. rc = wait_ms(delay);
  604. if(rc) {
  605. /* should not happen */
  606. error = SOCKERRNO;
  607. logmsg("wait_ms() failed with error: (%d) %s",
  608. error, strerror(error));
  609. sclose(sock);
  610. return CURL_SOCKET_BAD;
  611. }
  612. if(got_exit_signal) {
  613. logmsg("signalled to die, exiting...");
  614. sclose(sock);
  615. return CURL_SOCKET_BAD;
  616. }
  617. totdelay += delay;
  618. delay *= 2; /* double the sleep for next attempt */
  619. }
  620. }
  621. } while(rc && maxretr--);
  622. if(rc) {
  623. logmsg("setsockopt(SO_REUSEADDR) failed %d times in %d ms. Error: (%d) %s",
  624. attempt, totdelay, error, strerror(error));
  625. logmsg("Continuing anyway...");
  626. }
  627. /* When the specified listener port is zero, it is actually a
  628. request to let the system choose a non-zero available port. */
  629. #ifdef ENABLE_IPV6
  630. if(!use_ipv6) {
  631. #endif
  632. memset(&listener.sa4, 0, sizeof(listener.sa4));
  633. listener.sa4.sin_family = AF_INET;
  634. listener.sa4.sin_addr.s_addr = INADDR_ANY;
  635. listener.sa4.sin_port = htons(*listenport);
  636. rc = bind(sock, &listener.sa, sizeof(listener.sa4));
  637. #ifdef ENABLE_IPV6
  638. }
  639. else {
  640. memset(&listener.sa6, 0, sizeof(listener.sa6));
  641. listener.sa6.sin6_family = AF_INET6;
  642. listener.sa6.sin6_addr = in6addr_any;
  643. listener.sa6.sin6_port = htons(*listenport);
  644. rc = bind(sock, &listener.sa, sizeof(listener.sa6));
  645. }
  646. #endif /* ENABLE_IPV6 */
  647. if(rc) {
  648. error = SOCKERRNO;
  649. logmsg("Error binding socket on port %hu: (%d) %s",
  650. *listenport, error, strerror(error));
  651. sclose(sock);
  652. return CURL_SOCKET_BAD;
  653. }
  654. if(!*listenport) {
  655. /* The system was supposed to choose a port number, figure out which
  656. port we actually got and update the listener port value with it. */
  657. curl_socklen_t la_size;
  658. srvr_sockaddr_union_t localaddr;
  659. #ifdef ENABLE_IPV6
  660. if(!use_ipv6)
  661. #endif
  662. la_size = sizeof(localaddr.sa4);
  663. #ifdef ENABLE_IPV6
  664. else
  665. la_size = sizeof(localaddr.sa6);
  666. #endif
  667. memset(&localaddr.sa, 0, (size_t)la_size);
  668. if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
  669. error = SOCKERRNO;
  670. logmsg("getsockname() failed with error: (%d) %s",
  671. error, strerror(error));
  672. sclose(sock);
  673. return CURL_SOCKET_BAD;
  674. }
  675. switch (localaddr.sa.sa_family) {
  676. case AF_INET:
  677. *listenport = ntohs(localaddr.sa4.sin_port);
  678. break;
  679. #ifdef ENABLE_IPV6
  680. case AF_INET6:
  681. *listenport = ntohs(localaddr.sa6.sin6_port);
  682. break;
  683. #endif
  684. default:
  685. break;
  686. }
  687. if(!*listenport) {
  688. /* Real failure, listener port shall not be zero beyond this point. */
  689. logmsg("Apparently getsockname() succeeded, with listener port zero.");
  690. logmsg("A valid reason for this failure is a binary built without");
  691. logmsg("proper network library linkage. This might not be the only");
  692. logmsg("reason, but double check it before anything else.");
  693. sclose(sock);
  694. return CURL_SOCKET_BAD;
  695. }
  696. }
  697. /* bindonly option forces no listening */
  698. if(bind_only) {
  699. logmsg("instructed to bind port without listening");
  700. return sock;
  701. }
  702. /* start accepting connections */
  703. rc = listen(sock, 5);
  704. if(0 != rc) {
  705. error = SOCKERRNO;
  706. logmsg("listen() failed with error: (%d) %s",
  707. error, strerror(error));
  708. sclose(sock);
  709. return CURL_SOCKET_BAD;
  710. }
  711. return sock;
  712. }
  713. int main(int argc, char *argv[])
  714. {
  715. srvr_sockaddr_union_t me;
  716. curl_socket_t sock = CURL_SOCKET_BAD;
  717. curl_socket_t msgsock = CURL_SOCKET_BAD;
  718. int wrotepidfile = 0;
  719. char *pidname= (char *)".sockfilt.pid";
  720. bool juggle_again;
  721. int rc;
  722. int error;
  723. int arg=1;
  724. enum sockmode mode = PASSIVE_LISTEN; /* default */
  725. const char *addr = NULL;
  726. while(argc>arg) {
  727. if(!strcmp("--version", argv[arg])) {
  728. printf("sockfilt IPv4%s\n",
  729. #ifdef ENABLE_IPV6
  730. "/IPv6"
  731. #else
  732. ""
  733. #endif
  734. );
  735. return 0;
  736. }
  737. else if(!strcmp("--verbose", argv[arg])) {
  738. verbose = TRUE;
  739. arg++;
  740. }
  741. else if(!strcmp("--pidfile", argv[arg])) {
  742. arg++;
  743. if(argc>arg)
  744. pidname = argv[arg++];
  745. }
  746. else if(!strcmp("--logfile", argv[arg])) {
  747. arg++;
  748. if(argc>arg)
  749. serverlogfile = argv[arg++];
  750. }
  751. else if(!strcmp("--ipv6", argv[arg])) {
  752. #ifdef ENABLE_IPV6
  753. ipv_inuse = "IPv6";
  754. use_ipv6 = TRUE;
  755. #endif
  756. arg++;
  757. }
  758. else if(!strcmp("--ipv4", argv[arg])) {
  759. /* for completeness, we support this option as well */
  760. #ifdef ENABLE_IPV6
  761. ipv_inuse = "IPv4";
  762. use_ipv6 = FALSE;
  763. #endif
  764. arg++;
  765. }
  766. else if(!strcmp("--bindonly", argv[arg])) {
  767. bind_only = TRUE;
  768. arg++;
  769. }
  770. else if(!strcmp("--port", argv[arg])) {
  771. arg++;
  772. if(argc>arg) {
  773. char *endptr;
  774. unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
  775. if((endptr != argv[arg] + strlen(argv[arg])) ||
  776. ((ulnum != 0UL) && ((ulnum < 1025UL) || (ulnum > 65535UL)))) {
  777. fprintf(stderr, "sockfilt: invalid --port argument (%s)\n",
  778. argv[arg]);
  779. return 0;
  780. }
  781. port = curlx_ultous(ulnum);
  782. arg++;
  783. }
  784. }
  785. else if(!strcmp("--connect", argv[arg])) {
  786. /* Asked to actively connect to the specified local port instead of
  787. doing a passive server-style listening. */
  788. arg++;
  789. if(argc>arg) {
  790. char *endptr;
  791. unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
  792. if((endptr != argv[arg] + strlen(argv[arg])) ||
  793. (ulnum < 1025UL) || (ulnum > 65535UL)) {
  794. fprintf(stderr, "sockfilt: invalid --connect argument (%s)\n",
  795. argv[arg]);
  796. return 0;
  797. }
  798. connectport = curlx_ultous(ulnum);
  799. arg++;
  800. }
  801. }
  802. else if(!strcmp("--addr", argv[arg])) {
  803. /* Set an IP address to use with --connect; otherwise use localhost */
  804. arg++;
  805. if(argc>arg) {
  806. addr = argv[arg];
  807. arg++;
  808. }
  809. }
  810. else {
  811. puts("Usage: sockfilt [option]\n"
  812. " --version\n"
  813. " --verbose\n"
  814. " --logfile [file]\n"
  815. " --pidfile [file]\n"
  816. " --ipv4\n"
  817. " --ipv6\n"
  818. " --bindonly\n"
  819. " --port [port]\n"
  820. " --connect [port]\n"
  821. " --addr [address]");
  822. return 0;
  823. }
  824. }
  825. #ifdef WIN32
  826. win32_init();
  827. atexit(win32_cleanup);
  828. #endif
  829. install_signal_handlers();
  830. #ifdef ENABLE_IPV6
  831. if(!use_ipv6)
  832. #endif
  833. sock = socket(AF_INET, SOCK_STREAM, 0);
  834. #ifdef ENABLE_IPV6
  835. else
  836. sock = socket(AF_INET6, SOCK_STREAM, 0);
  837. #endif
  838. if(CURL_SOCKET_BAD == sock) {
  839. error = SOCKERRNO;
  840. logmsg("Error creating socket: (%d) %s",
  841. error, strerror(error));
  842. write_stdout("FAIL\n", 5);
  843. goto sockfilt_cleanup;
  844. }
  845. if(connectport) {
  846. /* Active mode, we should connect to the given port number */
  847. mode = ACTIVE;
  848. #ifdef ENABLE_IPV6
  849. if(!use_ipv6) {
  850. #endif
  851. memset(&me.sa4, 0, sizeof(me.sa4));
  852. me.sa4.sin_family = AF_INET;
  853. me.sa4.sin_port = htons(connectport);
  854. me.sa4.sin_addr.s_addr = INADDR_ANY;
  855. if (!addr)
  856. addr = "127.0.0.1";
  857. Curl_inet_pton(AF_INET, addr, &me.sa4.sin_addr);
  858. rc = connect(sock, &me.sa, sizeof(me.sa4));
  859. #ifdef ENABLE_IPV6
  860. }
  861. else {
  862. memset(&me.sa6, 0, sizeof(me.sa6));
  863. me.sa6.sin6_family = AF_INET6;
  864. me.sa6.sin6_port = htons(connectport);
  865. if (!addr)
  866. addr = "::1";
  867. Curl_inet_pton(AF_INET6, addr, &me.sa6.sin6_addr);
  868. rc = connect(sock, &me.sa, sizeof(me.sa6));
  869. }
  870. #endif /* ENABLE_IPV6 */
  871. if(rc) {
  872. error = SOCKERRNO;
  873. logmsg("Error connecting to port %hu: (%d) %s",
  874. connectport, error, strerror(error));
  875. write_stdout("FAIL\n", 5);
  876. goto sockfilt_cleanup;
  877. }
  878. logmsg("====> Client connect");
  879. msgsock = sock; /* use this as stream */
  880. }
  881. else {
  882. /* passive daemon style */
  883. sock = sockdaemon(sock, &port);
  884. if(CURL_SOCKET_BAD == sock) {
  885. write_stdout("FAIL\n", 5);
  886. goto sockfilt_cleanup;
  887. }
  888. msgsock = CURL_SOCKET_BAD; /* no stream socket yet */
  889. }
  890. logmsg("Running %s version", ipv_inuse);
  891. if(connectport)
  892. logmsg("Connected to port %hu", connectport);
  893. else if(bind_only)
  894. logmsg("Bound without listening on port %hu", port);
  895. else
  896. logmsg("Listening on port %hu", port);
  897. wrotepidfile = write_pidfile(pidname);
  898. if(!wrotepidfile) {
  899. write_stdout("FAIL\n", 5);
  900. goto sockfilt_cleanup;
  901. }
  902. do {
  903. juggle_again = juggle(&msgsock, sock, &mode);
  904. } while(juggle_again);
  905. sockfilt_cleanup:
  906. if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
  907. sclose(msgsock);
  908. if(sock != CURL_SOCKET_BAD)
  909. sclose(sock);
  910. if(wrotepidfile)
  911. unlink(pidname);
  912. restore_signal_handlers();
  913. if(got_exit_signal) {
  914. logmsg("============> sockfilt exits with signal (%d)", exit_signal);
  915. /*
  916. * To properly set the return status of the process we
  917. * must raise the same signal SIGINT or SIGTERM that we
  918. * caught and let the old handler take care of it.
  919. */
  920. raise(exit_signal);
  921. }
  922. logmsg("============> sockfilt quits");
  923. return 0;
  924. }