| 1 |
/* |
| 2 |
hts.c |
| 3 |
|
| 4 |
Copyright (C) 1999, 2000 Lars Brinkhoff. See COPYING for terms and conditions. |
| 5 |
|
| 6 |
hts is the server half of httptunnel. httptunnel creates a virtual |
| 7 |
two-way data path tunneled in HTTP requests. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <unistd_.h> |
| 13 |
#include <signal.h> |
| 14 |
#include <sys/poll_.h> |
| 15 |
#include <sys/time.h> |
| 16 |
|
| 17 |
#include "common.h" |
| 18 |
|
| 19 |
typedef struct |
| 20 |
{ |
| 21 |
char *me; |
| 22 |
char *device; |
| 23 |
const char *host; |
| 24 |
int port; |
| 25 |
char *forward_host; |
| 26 |
int forward_port; |
| 27 |
size_t content_length; |
| 28 |
char *pid_filename; |
| 29 |
int use_std; |
| 30 |
int use_daemon; |
| 31 |
int strict_content_length; |
| 32 |
int keep_alive; |
| 33 |
int max_connection_age; |
| 34 |
} Arguments; |
| 35 |
|
| 36 |
int debug_level = 0; |
| 37 |
FILE *debug_file = NULL; |
| 38 |
|
| 39 |
static void |
| 40 |
usage (FILE *f, const char *me) |
| 41 |
{ |
| 42 |
fprintf (f, |
| 43 |
"Usage: %s [OPTION]... [PORT]\n" |
| 44 |
"Listen for incoming httptunnel connections at PORT (default port is %d).\n" |
| 45 |
"When a connection is made, I/O is redirected to the destination specified\n" |
| 46 |
"by the --device, --forward-port or --stdin-stdout switch.\n" |
| 47 |
"\n" |
| 48 |
" -c, --content-length BYTES use HTTP PUT requests of BYTES size\n" |
| 49 |
" (k, M, and G postfixes recognized)\n" |
| 50 |
" -d, --device DEVICE use DEVICE for input and output\n" |
| 51 |
#ifdef DEBUG_MODE |
| 52 |
" -D, --debug [LEVEL] enable debug mode\n" |
| 53 |
#endif |
| 54 |
" -F, --forward-port HOST:PORT connect to PORT at HOST and use it for \n" |
| 55 |
" input and output\n" |
| 56 |
" -h, --help display this help and exit\n" |
| 57 |
" -k, --keep-alive SECONDS send keepalive bytes every SECONDS seconds\n" |
| 58 |
" (default is %d)\n" |
| 59 |
#ifdef DEBUG_MODE |
| 60 |
" -l, --logfile FILE specify logfile for debug output\n" |
| 61 |
#endif |
| 62 |
" -M, --max-connection-age SEC maximum time a connection will stay\n" |
| 63 |
" open is SEC seconds (default is %d)\n" |
| 64 |
" -s, --stdin-stdout use stdin/stdout for communication\n" |
| 65 |
" (implies --no-daemon)\n" |
| 66 |
" -S, --strict-content-length always write Content-Length bytes in requests\n" |
| 67 |
" -V, --version output version information and exit\n" |
| 68 |
" -w, --no-daemon don't fork into the background\n" |
| 69 |
" -p, --pid-file LOCATION write a PID file to LOCATION\n" |
| 70 |
"\n" |
| 71 |
"Report bugs to %s.\n", |
| 72 |
me, DEFAULT_HOST_PORT, DEFAULT_KEEP_ALIVE, |
| 73 |
DEFAULT_MAX_CONNECTION_AGE, BUG_REPORT_EMAIL); |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
parse_arguments (int argc, char **argv, Arguments *arg) |
| 78 |
{ |
| 79 |
int c; |
| 80 |
|
| 81 |
/* defaults */ |
| 82 |
|
| 83 |
arg->me = argv[0]; |
| 84 |
arg->host = NULL; |
| 85 |
arg->port = DEFAULT_HOST_PORT; |
| 86 |
arg->device = NULL; |
| 87 |
arg->forward_host = NULL; |
| 88 |
arg->forward_port = -1; |
| 89 |
arg->content_length = DEFAULT_CONTENT_LENGTH; |
| 90 |
arg->pid_filename = NULL; |
| 91 |
arg->use_std = FALSE; |
| 92 |
arg->use_daemon = TRUE; |
| 93 |
arg->strict_content_length = FALSE; |
| 94 |
arg->keep_alive = DEFAULT_KEEP_ALIVE; |
| 95 |
arg->max_connection_age = DEFAULT_CONNECTION_MAX_TIME; |
| 96 |
|
| 97 |
for (;;) |
| 98 |
{ |
| 99 |
int option_index = 0; |
| 100 |
static struct option long_options[] = |
| 101 |
{ |
| 102 |
{ "help", no_argument, 0, 'h' }, |
| 103 |
{ "stdin-stdout", no_argument, 0, 's' }, |
| 104 |
{ "strict-content-length", no_argument, 0, 'S' }, |
| 105 |
{ "version", no_argument, 0, 'V' }, |
| 106 |
{ "no-daemon", no_argument, 0, 'w' }, |
| 107 |
#ifdef DEBUG_MODE |
| 108 |
{ "debug", optional_argument, 0, 'D' }, |
| 109 |
{ "logfile", required_argument, 0, 'l' }, |
| 110 |
#endif |
| 111 |
{ "device", required_argument, 0, 'd' }, |
| 112 |
{ "pid-file", required_argument, 0, 'p' }, |
| 113 |
{ "keep-alive", required_argument, 0, 'k' }, |
| 114 |
{ "forward-port", required_argument, 0, 'F' }, |
| 115 |
{ "content-length", required_argument, 0, 'c' }, |
| 116 |
{ "max-connection-age", required_argument, 0, 'M' }, |
| 117 |
{ 0, 0, 0, 0 } |
| 118 |
}; |
| 119 |
|
| 120 |
static const char *short_options = "c:d:F:hk:M:p:sSVw" |
| 121 |
#ifdef DEBUG_MODE |
| 122 |
"D:l:" |
| 123 |
#endif |
| 124 |
; |
| 125 |
|
| 126 |
c = getopt_long (argc, argv, short_options, |
| 127 |
long_options, &option_index); |
| 128 |
if (c == -1) |
| 129 |
break; |
| 130 |
|
| 131 |
switch (c) |
| 132 |
{ |
| 133 |
case 0: |
| 134 |
fprintf (stderr, "option %s", long_options[option_index].name); |
| 135 |
if (optarg) |
| 136 |
fprintf (stderr, " with arg %s", optarg); |
| 137 |
fprintf (stderr, "\n"); |
| 138 |
break; |
| 139 |
|
| 140 |
case 'c': |
| 141 |
arg->content_length = atoi_with_postfix (optarg); |
| 142 |
break; |
| 143 |
|
| 144 |
case 'd': |
| 145 |
arg->device = optarg; |
| 146 |
break; |
| 147 |
|
| 148 |
#ifdef DEBUG_MODE |
| 149 |
case 'D': |
| 150 |
if (optarg) |
| 151 |
debug_level = atoi (optarg); |
| 152 |
else |
| 153 |
debug_level = 1; |
| 154 |
break; |
| 155 |
|
| 156 |
case 'l': |
| 157 |
debug_file = fopen (optarg, "w"); |
| 158 |
if (debug_file == NULL) |
| 159 |
{ |
| 160 |
fprintf (stderr, "%s: couldn't open file %s for writing\n", |
| 161 |
arg->me, optarg); |
| 162 |
log_exit (1); |
| 163 |
} |
| 164 |
break; |
| 165 |
#endif /* DEBUG_MODE */ |
| 166 |
|
| 167 |
case 'F': |
| 168 |
name_and_port (optarg, &arg->forward_host, &arg->forward_port); |
| 169 |
if (arg->forward_port == -1) |
| 170 |
{ |
| 171 |
fprintf (stderr, "%s: you must specify a port number.\n" |
| 172 |
"%s: try '%s --help' for help.\n", |
| 173 |
arg->me, arg->me, arg->me); |
| 174 |
exit (1); |
| 175 |
} |
| 176 |
break; |
| 177 |
|
| 178 |
case 'h': |
| 179 |
usage (stdout, arg->me); |
| 180 |
exit (0); |
| 181 |
|
| 182 |
case 'k': |
| 183 |
arg->keep_alive = atoi (optarg); |
| 184 |
break; |
| 185 |
|
| 186 |
case 'M': |
| 187 |
arg->max_connection_age = atoi (optarg); |
| 188 |
break; |
| 189 |
|
| 190 |
case 's': |
| 191 |
arg->use_std=TRUE; |
| 192 |
arg->use_daemon=FALSE; |
| 193 |
break; |
| 194 |
|
| 195 |
case 'S': |
| 196 |
arg->strict_content_length = TRUE; |
| 197 |
break; |
| 198 |
|
| 199 |
case 'V': |
| 200 |
printf ("hts (%s) %s\n", PACKAGE, VERSION); |
| 201 |
exit (0); |
| 202 |
|
| 203 |
case 'p': |
| 204 |
arg->pid_filename = optarg; |
| 205 |
break; |
| 206 |
|
| 207 |
case 'w': |
| 208 |
arg->use_daemon = FALSE; |
| 209 |
break; |
| 210 |
|
| 211 |
case '?': |
| 212 |
break; |
| 213 |
|
| 214 |
default: |
| 215 |
fprintf (stderr, "?? getopt returned character code 0%o ??\n", c); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if (argc - 1 == optind) |
| 220 |
{ |
| 221 |
char *colon = strchr (argv[optind], ':'); |
| 222 |
|
| 223 |
if (colon) |
| 224 |
{ |
| 225 |
*colon = '\0'; |
| 226 |
arg->host = argv[optind]; |
| 227 |
arg->port = atoi (colon + 1); |
| 228 |
} |
| 229 |
else |
| 230 |
arg->port = atoi (argv[optind]); |
| 231 |
} |
| 232 |
else if (argc - 1 > optind) |
| 233 |
{ |
| 234 |
usage (stderr, arg->me); |
| 235 |
exit (1); |
| 236 |
} |
| 237 |
|
| 238 |
if (arg->device == NULL && arg->forward_port == -1 && !arg->use_std) |
| 239 |
{ |
| 240 |
fprintf (stderr, "%s: one of --device, --forward-port or --stdin-stdout must be used.\n" |
| 241 |
"%s: try '%s -help' for help.\n", |
| 242 |
arg->me, arg->me, arg->me); |
| 243 |
exit (1); |
| 244 |
} |
| 245 |
|
| 246 |
if ((arg->device != NULL && arg->forward_port != -1) || |
| 247 |
(arg->device != NULL && arg->use_std) || |
| 248 |
(arg->forward_port != -1 && arg->use_std)) |
| 249 |
{ |
| 250 |
fprintf (stderr, "%s: only one of --device, --forward-port or --stdin-stdout can be used.\n" |
| 251 |
"%s: try '%s --help' for help.\n", |
| 252 |
arg->me, arg->me, arg->me); |
| 253 |
exit (1); |
| 254 |
} |
| 255 |
|
| 256 |
if (debug_level == 0 && debug_file != NULL) |
| 257 |
{ |
| 258 |
fprintf (stderr, "%s: --logfile can't be used without debugging\n", |
| 259 |
arg->me); |
| 260 |
exit (1); |
| 261 |
} |
| 262 |
|
| 263 |
/* Removed test ((arg->device == NULL) == (arg->forward_port == -1)) |
| 264 |
* by Sampo Niskanen - those have been tested already! */ |
| 265 |
if (arg->port == -1 || |
| 266 |
((arg->forward_host == NULL) != (arg->forward_port == -1))) |
| 267 |
{ |
| 268 |
usage (stderr, arg->me); |
| 269 |
exit (1); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
int |
| 274 |
main (int argc, char **argv) |
| 275 |
{ |
| 276 |
int closed; |
| 277 |
int fd = -1; |
| 278 |
Arguments arg; |
| 279 |
Tunnel *tunnel; |
| 280 |
FILE *pid_file; |
| 281 |
|
| 282 |
parse_arguments (argc, argv, &arg); |
| 283 |
|
| 284 |
if ((debug_level == 0 || debug_file != NULL) && arg.use_daemon) |
| 285 |
daemon (0, 1); |
| 286 |
|
| 287 |
#ifdef DEBUG_MODE |
| 288 |
if (debug_level != 0 && debug_file == NULL) |
| 289 |
debug_file = stderr; |
| 290 |
#else |
| 291 |
openlog ("hts", LOG_PID, LOG_DAEMON); |
| 292 |
#endif |
| 293 |
|
| 294 |
log_notice ("hts (%s) %s started with arguments:", PACKAGE, VERSION); |
| 295 |
log_notice (" me = %s", arg.me); |
| 296 |
log_notice (" device = %s", arg.device ? arg.device : "(null)"); |
| 297 |
if (arg.host) |
| 298 |
log_notice (" port = %s:%d", arg.host, arg.port); |
| 299 |
else |
| 300 |
log_notice (" port = %d", arg.port); |
| 301 |
log_notice (" forward_port = %d", arg.forward_port); |
| 302 |
log_notice (" forward_host = %s", |
| 303 |
arg.forward_host ? arg.forward_host : "(null)"); |
| 304 |
log_notice (" content_length = %d", arg.content_length); |
| 305 |
log_notice (" strict_content_length = %d", arg.strict_content_length); |
| 306 |
log_notice (" use_std = %d", arg.use_std); |
| 307 |
log_notice (" debug_level = %d", debug_level); |
| 308 |
log_notice (" pid_filename = %s", |
| 309 |
arg.pid_filename ? arg.pid_filename : "(null)"); |
| 310 |
|
| 311 |
tunnel = tunnel_new_server (arg.host, arg.port, arg.content_length); |
| 312 |
if (tunnel == NULL) |
| 313 |
{ |
| 314 |
log_error ("couldn't create tunnel", argv[0]); |
| 315 |
log_exit (1); |
| 316 |
} |
| 317 |
|
| 318 |
if (tunnel_setopt (tunnel, "strict_content_length", |
| 319 |
&arg.strict_content_length) == -1) |
| 320 |
log_debug ("tunnel_setopt strict_content_length error: %s", |
| 321 |
strerror (errno)); |
| 322 |
|
| 323 |
if (tunnel_setopt (tunnel, "keep_alive", |
| 324 |
&arg.keep_alive) == -1) |
| 325 |
log_debug ("tunnel_setopt keep_alive error: %s", strerror (errno)); |
| 326 |
|
| 327 |
if (tunnel_setopt (tunnel, "max_connection_age", |
| 328 |
&arg.max_connection_age) == -1) |
| 329 |
log_debug ("tunnel_setopt max_connection_age error: %s", strerror (errno)); |
| 330 |
|
| 331 |
#ifdef DEBUG_MODE |
| 332 |
signal (SIGPIPE, log_sigpipe); |
| 333 |
#else |
| 334 |
signal (SIGPIPE, SIG_IGN); |
| 335 |
#endif |
| 336 |
|
| 337 |
if (arg.pid_filename != NULL) |
| 338 |
{ |
| 339 |
pid_file = fopen (arg.pid_filename, "w+"); |
| 340 |
if (pid_file == NULL) |
| 341 |
{ |
| 342 |
fprintf (stderr, "Couldn't open pid file %s: %s\n", |
| 343 |
arg.pid_filename, strerror (errno)); |
| 344 |
} |
| 345 |
else |
| 346 |
{ |
| 347 |
fprintf (pid_file, "%d\n", (int)getpid ()); |
| 348 |
if (fclose (pid_file)) |
| 349 |
{ |
| 350 |
fprintf (stderr, "Error closing pid file: %s\n", |
| 351 |
strerror (errno)); |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
for (;;) |
| 357 |
{ |
| 358 |
time_t last_tunnel_write; |
| 359 |
|
| 360 |
log_debug ("waiting for tunnel connection"); |
| 361 |
|
| 362 |
if (arg.device != NULL) |
| 363 |
{ |
| 364 |
fd = open_device (arg.device); |
| 365 |
log_debug ("open_device (\"%s\") = %d", arg.device, fd); |
| 366 |
if (fd == -1) |
| 367 |
{ |
| 368 |
log_error ("couldn't open %s: %s", |
| 369 |
arg.device, strerror (errno)); |
| 370 |
log_exit (1); |
| 371 |
} |
| 372 |
/* Check that fd is not 0 (clash with --stdin-stdout) */ |
| 373 |
if (fd == 0) |
| 374 |
{ |
| 375 |
log_notice("changing fd from %d to 3",fd); |
| 376 |
if (dup2(fd,3) != 3) |
| 377 |
{ |
| 378 |
log_error ("couldn't dup2(%d,3): %s",fd,strerror(errno)); |
| 379 |
log_exit (1); |
| 380 |
} |
| 381 |
} |
| 382 |
} else if (arg.use_std) { |
| 383 |
log_debug ("using stdin as fd"); |
| 384 |
fd = 0; |
| 385 |
if (fcntl(fd,F_SETFL,O_NONBLOCK)==-1) |
| 386 |
{ |
| 387 |
log_error ("couldn't set stdin to non-blocking mode: %s", |
| 388 |
strerror(errno)); |
| 389 |
log_exit (1); |
| 390 |
} |
| 391 |
/* Usage of stdout (fd = 1) is checked later. */ |
| 392 |
} |
| 393 |
|
| 394 |
if (tunnel_accept (tunnel) == -1) |
| 395 |
{ |
| 396 |
log_notice ("couldn't accept connection: %s", strerror (errno)); |
| 397 |
continue; |
| 398 |
} |
| 399 |
|
| 400 |
if (arg.forward_port != -1) |
| 401 |
{ |
| 402 |
struct sockaddr_in addr; |
| 403 |
|
| 404 |
if (set_address (&addr, arg.forward_host, arg.forward_port) == -1) |
| 405 |
{ |
| 406 |
log_error ("couldn't forward port to %s:%d: %s\n", |
| 407 |
arg.forward_host, arg.forward_port, strerror (errno)); |
| 408 |
log_exit (1); |
| 409 |
} |
| 410 |
|
| 411 |
fd = do_connect (&addr); |
| 412 |
log_debug ("do_connect (\"%s:%d\") = %d", |
| 413 |
arg.forward_host, arg.forward_port, fd); |
| 414 |
if (fd == -1) |
| 415 |
{ |
| 416 |
log_error ("couldn't connect to %s:%d: %s\n", |
| 417 |
arg.forward_host, arg.forward_port, strerror (errno)); |
| 418 |
log_exit (1); |
| 419 |
} |
| 420 |
/* Check that fd is not 0 (clash with --stdin-stdout) */ |
| 421 |
if (fd == 0) |
| 422 |
{ |
| 423 |
log_notice("changing fd from %d to 3",fd); |
| 424 |
if (dup2(fd,3) != 3) |
| 425 |
{ |
| 426 |
log_error ("couldn't dup2(%d,3): %s",fd,strerror(errno)); |
| 427 |
log_exit (1); |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
closed = FALSE; |
| 433 |
time (&last_tunnel_write); |
| 434 |
while (!closed) |
| 435 |
{ |
| 436 |
struct pollfd pollfd[2]; |
| 437 |
int timeout; |
| 438 |
time_t t; |
| 439 |
int n; |
| 440 |
|
| 441 |
pollfd[0].fd = fd; |
| 442 |
pollfd[0].events = POLLIN; |
| 443 |
pollfd[1].fd = tunnel_pollin_fd (tunnel); |
| 444 |
pollfd[1].events = POLLIN; |
| 445 |
|
| 446 |
time (&t); |
| 447 |
timeout = 1000 * (arg.keep_alive - (t - last_tunnel_write)); |
| 448 |
if (timeout < 0) |
| 449 |
timeout = 0; |
| 450 |
|
| 451 |
log_annoying ("poll () ..."); |
| 452 |
n = poll (pollfd, 2, timeout); |
| 453 |
log_annoying ("... = %d", n); |
| 454 |
if (n == -1) |
| 455 |
{ |
| 456 |
log_error ("poll error: %s\n", strerror (errno)); |
| 457 |
log_exit (1); |
| 458 |
} |
| 459 |
else if (n == 0) |
| 460 |
{ |
| 461 |
log_verbose ("poll() timed out"); |
| 462 |
tunnel_padding (tunnel, 1); |
| 463 |
time (&last_tunnel_write); |
| 464 |
continue; |
| 465 |
} |
| 466 |
|
| 467 |
log_annoying ("revents[0] = %x, revents[1] = %x, POLLIN = %x", |
| 468 |
pollfd[0].revents, pollfd[1].revents, POLLIN); |
| 469 |
handle_input ("device or port", tunnel, fd, pollfd[0].revents, |
| 470 |
handle_device_input, &closed); |
| 471 |
handle_input ("tunnel", tunnel, fd, pollfd[1].revents, |
| 472 |
handle_tunnel_input, &closed); |
| 473 |
|
| 474 |
if (pollfd[0].revents & POLLIN) |
| 475 |
time (&last_tunnel_write); |
| 476 |
} |
| 477 |
|
| 478 |
log_debug ("closing tunnel"); |
| 479 |
if (fd != 0) |
| 480 |
{ |
| 481 |
close (fd); |
| 482 |
} |
| 483 |
tunnel_close (tunnel); |
| 484 |
log_notice ("disconnected from FIXME:hostname:port"); |
| 485 |
} |
| 486 |
|
| 487 |
log_debug ("destroying tunnel"); |
| 488 |
tunnel_destroy (tunnel); |
| 489 |
|
| 490 |
log_exit (0); |
| 491 |
} |