1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
/*
common.c
Copyright (C) 1999, 2000 Lars Brinkhoff. See COPYING for terms and conditions.
Code common to both htc and hts.
*/
#include <time.h>
#include <stdio.h>
#include <netdb_.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <syslog_.h>
#include <termios.h>
#include <sys/poll_.h>
#include "tunnel.h"
#include "common.h"
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#ifdef DEBUG_MODE
static void
log_level (int level, char *fmt0, va_list ap)
{
if (debug_level >= level)
{
struct tm *t2;
char s[40];
time_t t;
int i;
time (&t);
t2 = localtime (&t);
strftime (s, sizeof s, "%Y%m%d %H%M%S ", t2);
fprintf (debug_file, "%s", s);
for (i = 1; i < level; i++)
fprintf (debug_file, " ");
vfprintf (debug_file, fmt0, ap);
fprintf (debug_file, "\n");
fflush (debug_file);
}
}
#endif
void
log_exit (int status)
{
log_notice ("exit with status = %d", status);
exit (status);
}
void
log_notice (char *fmt0, ...)
{
va_list ap;
va_start(ap, fmt0);
#ifdef DEBUG_MODE
log_level (1, fmt0, ap);
#else
vsyslog (LOG_NOTICE, fmt0, ap);
#endif
va_end(ap);
}
void
log_error (char *fmt0, ...)
{
va_list ap;
va_start(ap, fmt0);
#ifdef DEBUG_MODE
log_level (2, fmt0, ap);
#else
vsyslog (LOG_ERROR, fmt0, ap);
#endif
va_end(ap);
}
#ifdef DEBUG_MODE
void
log_debug (char *fmt0, ...)
{
va_list ap;
va_start(ap, fmt0);
log_level (3, fmt0, ap);
va_end(ap);
}
#endif
#ifdef DEBUG_MODE
void
log_verbose (char *fmt0, ...)
{
va_list ap;
va_start(ap, fmt0);
log_level (4, fmt0, ap);
va_end(ap);
}
#endif
#ifdef DEBUG_MODE
void
log_annoying (char *fmt0, ...)
{
va_list ap;
va_start (ap, fmt0);
log_level (5, fmt0, ap);
va_end(ap);
}
#endif
int
server_socket (struct in_addr addr, int port, int backlog)
{
struct sockaddr_in address;
int i, s;
s = socket (PF_INET, SOCK_STREAM, 0);
if (s == -1)
return -1;
i = 1;
if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (void *)&i, sizeof i) == -1)
{
log_error ("server_socket: setsockopt SO_REUSEADDR: %s",
strerror (errno));
}
memset (&address, '\0', sizeof address);
#if defined(__FreeBSD__) || defined(__OpenBSD__)
address.sin_len = sizeof address;
#endif
address.sin_family = PF_INET;
address.sin_port = htons ((short)port);
address.sin_addr = addr;
if (bind (s, (struct sockaddr *)&address, sizeof (address)) == -1)
{
close (s);
return -1;
}
if (listen (s, (unsigned)backlog) == -1)
{
close (s);
return -1;
}
return s;
}
int
set_address (struct sockaddr_in *address, const char *host, int port)
{
memset (address, '\0', sizeof *address);
#if defined(__FreeBSD__) || defined(__OpenBSD__)
address->sin_len = sizeof *address;
#endif
address->sin_family = PF_INET;
address->sin_port = htons ((u_short)port);
address->sin_addr.s_addr = inet_addr (host);
if (address->sin_addr.s_addr == INADDR_NONE)
{
struct hostent *ent;
unsigned int ip;
log_annoying ("set_address: gethostbyname (\"%s\")", host);
ent = gethostbyname (host);
log_annoying ("set_address: ent = %p", ent);
if (ent == 0)
return -1;
memcpy(&address->sin_addr.s_addr, ent->h_addr, (unsigned)ent->h_length);
ip = ntohl (address->sin_addr.s_addr);
log_annoying ("set_address: host = %d.%d.%d.%d",
ntohl (ip) >> 24,
(ntohl (ip) >> 16) & 0xff,
(ntohl (ip) >> 8) & 0xff,
ntohl (ip) & 0xff);
}
return 0;
}
int
open_device (char *device)
{
struct termios t;
int fd;
fd = open (device, O_RDWR | O_NONBLOCK);
if (fd == -1)
return -1;
if (tcgetattr (fd, &t) == -1)
{
if (errno == ENOTTY || errno == EINVAL)
return fd;
else
return -1;
}
t.c_iflag = 0;
t.c_oflag = 0;
t.c_lflag = 0;
if (tcsetattr (fd, TCSANOW, &t) == -1)
return -1;
return fd;
}
#ifdef DEBUG_MODE
void
dump_buf (FILE *f, unsigned char *buf, size_t len)
{
const int N = 20;
int i, j;
for (i = 0; i < len;)
{
fputc ('[', f);
for (j = 0; j < N && i + j < len; j++)
fprintf (f, "%02x", buf[i + j]);
for (; j < N; j++)
fprintf (f, " ");
fputc (']', f);
fputc ('[', f);
for (j = 0; j < N && i + j < len; j++)
{
int c = buf[i + j];
if (c < ' ' || c > 126)
fputc ('.', f);
else
fputc (c, f);
}
fputc (']', f);
fputc ('\n', f);
i += j;
}
}
#endif
int
handle_device_input (Tunnel *tunnel, int fd, int events)
{
unsigned char buf[10240];
ssize_t n, m;
if (events & POLLIN)
{
n = read (fd, buf, sizeof buf);
if (n == 0 || n == -1)
{
if (n == -1 && errno != EAGAIN)
log_error ("handle_device_input: read() error: %s",
strerror (errno));
return n;
}
#ifdef DEBUG_MODE
log_annoying ("read %d bytes from device:", n);
if (debug_level >= 5)
dump_buf (debug_file, buf, (size_t)n);
#endif
m = tunnel_write (tunnel, buf, (size_t)n);
log_annoying ("tunnel_write (%p, %p, %d) = %d", tunnel, buf, n, m);
return m;
}
else if (events & POLLHUP)
{
log_error ("handle_device_input: POLLHUP");
sleep (5);
}
else if (events & POLLERR)
log_error ("handle_device_input: POLLERR");
else if (events & POLLNVAL)
log_error ("handle_device_input: POLLINVAL");
else
log_error ("handle_device_input: none of the above");
errno = EIO;
return -1;
}
int
handle_tunnel_input (Tunnel *tunnel, int fd, int events)
{
unsigned char buf[10240];
ssize_t n, m;
if (events & POLLIN)
{
n = tunnel_read (tunnel, buf, sizeof buf);
if (n <= 0)
{
log_annoying ("handle_tunnel_input: tunnel_read() = %d\n", n);
if (n == -1 && errno != EAGAIN)
log_error ("handle_tunnel_input: tunnel_read() error: %s",
strerror (errno));
return n;
}
#ifdef DEBUG_MODE
log_annoying ("read %d bytes from tunnel:", n);
if (debug_level >= 5)
dump_buf (debug_file, buf, (size_t)n);
#endif
/* If fd == 0, then we are using --stdin-stdout so write to stdout,
* not fd. */
m = write_all (fd ? fd : 0, buf, (size_t)n);
log_annoying ("write_all (%d, %p, %d) = %d", fd ? fd : 1, buf, n, m);
return m;
}
else if (events & POLLHUP)
log_error ("handle_device_input: POLLHUP");
else if (events & POLLERR)
log_error ("handle_device_input: PULLERR");
else if (events & POLLNVAL)
log_error ("handle_device_input: PULLINVAL");
else
log_error ("handle_device_input: none of the above");
errno = EIO;
return -1;
}
void
name_and_port (const char *nameport, char **name, int *port)
{
char *p;
*name = strdup (nameport);
if (*name == NULL)
{
fprintf (stderr, "Out of memory\n");
exit (1);
}
p = strchr (*name, ':');
if (p != NULL)
{
*port = atoi (p + 1);
*p = '\0';
}
}
int
atoi_with_postfix (const char *s_)
{
char *s = strdup (s_);
int n = strlen (s);
int factor = 1;
int x;
if (s == NULL)
{
fprintf (stderr, "Out of memory\n");
exit (1);
}
switch (s[n - 1])
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
break;
case 'k':
case 'K':
factor = 1024;
break;
case 'M':
factor = 1024 * 1024;
break;
case 'G':
factor = 1024 * 1024 * 1024;
break;
default:
fprintf (stderr, "Unknown postfix: %c\n", s[n - 1]);
exit (1);
}
if (factor != 1)
s[n - 1] = '\0';
x = factor * atoi (s);
free (s);
return x;
}
#ifdef DEBUG_MODE
RETSIGTYPE
log_sigpipe (int sig)
{
log_debug ("caught SIGPIPE");
signal (SIGPIPE, log_sigpipe);
}
#endif
|