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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
|
/*
* active port forwarder - software for secure forwarding
* Copyright (C) 2003-2007 jeremian <jeremian [at] poczta.fm>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <config.h>
#include "inet_ntop.h"
#include "network.h"
#include "activefor.h"
#include "stats.h"
#include "logging.h"
#include <string.h>
#include <errno.h>
#include <zlib.h>
#include <assert.h>
/*
* Function name: ip_listen
* Description: Creates the listening socket.
* Arguments: sockfd - the created socket
* host - the name of the host
* serv - the name of the service (port)
* addrlenp - pointer to the length of the sockaddr structure
* type - the type of the socket
* Returns: 0 - success,
* !0 - failure.
*/
int
ip_listen(int* sockfd, const char *host, const char *serv, socklen_t *addrlenp, const char type)
{
const int on = 1;
#if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
int n;
struct addrinfo hints, *res, *ressave;
aflog(LOG_T_INIT, LOG_I_DDEBUG,
"ip_listen: host=[%s] serv=[%s], type=[%d]", host, serv, type);
bzero(&hints, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE;
if (type & 0x02) {
hints.ai_family = AF_INET;
}
else if (type & 0x04) {
hints.ai_family = AF_INET6;
}
else {
hints.ai_family = AF_UNSPEC;
}
if (type & 0x01) {
hints.ai_socktype = SOCK_STREAM;
}
else {
hints.ai_socktype = SOCK_DGRAM;
}
if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0) {
return n;
}
ressave = res;
do {
(*sockfd) = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if ((*sockfd) < 0) {
continue; /* error, try next one */
}
if (type & 0x01) { /* tcp_listen */
setsockopt((*sockfd), SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
setsockopt((*sockfd), SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
}
if (bind((*sockfd), res->ai_addr, res->ai_addrlen) == 0) {
break; /* success */
}
close((*sockfd)); /* bind error, close and try next one */
} while ( (res = res->ai_next) != NULL);
if (res == NULL) { /* errno from final socket() or bind() */
return 1;
}
if (type & 0x01) { /* tcp_listen */
listen((*sockfd), 1);
}
if (addrlenp) {
*addrlenp = res->ai_addrlen; /* return size of protocol address */
}
freeaddrinfo(ressave);
#else
struct sockaddr_in servaddr;
struct hostent* hostaddr = NULL;
int port;
aflog(LOG_T_INIT, LOG_I_DDEBUG,
"ip_listen: host=[%s] serv=[%s], type=[%d]", host, serv, type);
if (type & 0x01) {
(*sockfd) = socket(AF_INET, SOCK_STREAM, 0);
}
else {
(*sockfd) = socket(AF_INET, SOCK_DGRAM, 0);
}
if ((*sockfd) == -1) {
return 1;
}
port = atoi(serv);
if (host) {
hostaddr = gethostbyname(host);
if (hostaddr == NULL) {
return 2;
}
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
if (host) {
memcpy(&servaddr.sin_addr.s_addr, hostaddr->h_addr_list[0], hostaddr->h_length);
}
else {
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
}
servaddr.sin_port = htons(port);
if (type & 0x01) { /* tcp_listen */
setsockopt((*sockfd), SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
setsockopt((*sockfd), SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
}
if (bind((*sockfd), (struct sockaddr*) &servaddr, sizeof(servaddr))){
printf("bind failed\n");
return 4;
}
if (type & 0x01) { /* tcp_listen */
if (listen((*sockfd), 5)){
return 5;
}
}
if (addrlenp) {
*addrlenp = sizeof(servaddr); /* return size of protocol address */
}
#endif
return(0);
}
/*
* Function name: ip_connect
* Description: Creates the connected socket.
* Arguments: sockfd - the connected socket
* host - the name of the host
* serv - the name of the service (port)
* type - the type of the socket
* lhost - the name of the local host (used for local bind of the socket)
* lserv - the name of the local service (port) (used for local bind of the socket)
* Returns: 0 - success,
* !0 - failure.
*/
int
ip_connect(int* sockfd, const char *host, const char *serv, const char type,
const char *lhost, const char *lserv)
{
const int on = 1;
#if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
int n;
int bindFailed;
struct addrinfo hints, *res, *ressave;
struct addrinfo lhints, *lres, *lressave = NULL;
aflog(LOG_T_INIT, LOG_I_DDEBUG,
"ip_connect: host=[%s] serv=[%s], type=[%d], lhost=[%s], lserv=[%s]", host, serv, type, lhost, lserv);
bzero(&hints, sizeof(struct addrinfo));
if (type & 0x02) {
hints.ai_family = AF_INET;
}
else if (type & 0x04) {
hints.ai_family = AF_INET6;
}
else {
hints.ai_family = AF_UNSPEC;
}
if (type & 0x01) {
hints.ai_socktype = SOCK_STREAM;
}
else {
hints.ai_socktype = SOCK_DGRAM;
}
lhints = hints;
if (lhost || lserv) {
if ( (n = getaddrinfo(lhost, lserv, &lhints, &lres)) != 0) {
return n;
}
lressave = lres;
}
if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0) {
return n;
}
ressave = res;
do {
(*sockfd) = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if ((*sockfd) < 0) {
continue; /* ignore this one */
}
if (type & 0x01) { /* tcp_connect */
setsockopt((*sockfd), SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
}
bindFailed = 0;
if (lhost || lserv) {
bindFailed = 1;
lres = lressave;
do {
if (bind((*sockfd), lres->ai_addr, lres->ai_addrlen) == 0) {
bindFailed = 0;
break; /* success */
}
} while ( (lres = lres->ai_next) != NULL);
}
if (bindFailed == 1) {
close((*sockfd)); /* ignore this one */
continue;
}
if (connect((*sockfd), res->ai_addr, res->ai_addrlen) == 0) {
break; /* success */
}
close((*sockfd)); /* ignore this one */
} while ( (res = res->ai_next) != NULL);
if (res == NULL) { /* errno set from final connect() */
return 1;
}
if (lhost || lserv) {
freeaddrinfo(lressave);
}
freeaddrinfo(ressave);
#else
struct sockaddr_in servaddr, lservaddr;
struct hostent* hostaddr;
struct hostent* lhostaddr;
int port, lport;
aflog(LOG_T_INIT, LOG_I_DDEBUG,
"ip_connect: host=[%s] serv=[%s], type=[%d], lhost=[%s], lserv=[%s]", host, serv, type, lhost, lserv);
if (type & 0x01) {
(*sockfd) = socket(AF_INET, SOCK_STREAM, 0);
}
else {
(*sockfd) = socket(AF_INET, SOCK_DGRAM, 0);
}
if ((*sockfd) == -1) {
return 1;
}
port = atoi(serv);
hostaddr = gethostbyname(host);
if (hostaddr == NULL) {
return 2;
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
memcpy(&servaddr.sin_addr.s_addr, hostaddr->h_addr_list[0], hostaddr->h_length);
if (type & 0x01) { /* tcp_connect */
setsockopt((*sockfd), SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
}
if (lhost || lserv) {
memset(&lservaddr, 0, sizeof(lservaddr));
lservaddr.sin_family = AF_INET;
if (lserv) {
lport = atoi(lserv);
lservaddr.sin_port = htons(lport);
}
if (lhost) {
lhostaddr = gethostbyname(lhost);
if (lhostaddr == NULL) {
return 3;
}
memcpy(&lservaddr.sin_addr.s_addr, lhostaddr->h_addr_list[0], lhostaddr->h_length);
}
else {
lservaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
}
if (bind((*sockfd), (struct sockaddr*) &lservaddr, sizeof(lservaddr))){
return 4;
}
}
if (connect((*sockfd), (struct sockaddr*) &servaddr, sizeof(servaddr))){
return 5;
}
#endif
return(0);
}
/*
* Function name: sock_ntop
* Description: Returns the string representing given socket address.
* Arguments: sa - pointer to sockaddr structure
* salen - size of the sockaddr structure
* namebuf - buffer for string representation of the host name
* portbuf - buffer for string representation of the host port
* type - if the socket address should be resolved to the the DNS name
* Returns: The string representing given socket address or NULL, if the address
* can't be established.
*/
char *
sock_ntop(const struct sockaddr *sa, socklen_t salen, char* namebuf, char* portbuf, char type)
{
char portstr[7];
static char str[136]; /* Unix domain is largest */
switch (sa->sa_family) {
case AF_INET: {
struct sockaddr_in *sin = (struct sockaddr_in *) sa;
if (type) {
#ifdef HAVE_GETNAMEINFO
if (getnameinfo(sa, salen, str, 128, NULL, 0, 0)) {
return NULL;
}
#else
struct hostent* hostname;
if ((hostname = gethostbyaddr((void*) &sin->sin_addr, sizeof(struct in_addr), AF_INET))) {
strncpy(str, hostname->h_name, 127);
str[127] = 0;
}
else {
if (inet_ntop(AF_INET, (void*) &sin->sin_addr, str, sizeof(str)) == NULL) {
return NULL;
}
}
#endif
}
else {
if (inet_ntop(AF_INET, (void*) &sin->sin_addr, str, sizeof(str)) == NULL) {
return NULL;
}
}
if (namebuf) {
memcpy(namebuf, str, 128);
}
if (ntohs(sin->sin_port) != 0) {
snprintf(portstr, sizeof(portstr), ".%d", ntohs(sin->sin_port));
if (portbuf) {
snprintf(portbuf, 7, "%d", ntohs(sin->sin_port));
}
strcat(str, portstr);
}
return(str);
}
#ifdef AF_INET6
case AF_INET6: {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
if (type) {
#ifdef HAVE_GETNAMEINFO
if (getnameinfo(sa, salen, str, 128, NULL, 0, 0)) {
return NULL;
}
#else
struct hostent* hostname;
if ((hostname = gethostbyaddr(&sin6->sin6_addr, sizeof(struct in6_addr), AF_INET6))) {
strncpy(str, hostname->h_name, 127);
str[127] = 0;
}
else {
if (inet_ntop(AF_INET6, &sin6->sin6_addr, str, sizeof(str)) == NULL) {
return NULL;
}
}
#endif
}
else {
if (inet_ntop(AF_INET6, &sin6->sin6_addr, str, sizeof(str)) == NULL) {
return NULL;
}
}
if (namebuf) {
memcpy(namebuf, str, 128);
}
if (ntohs(sin6->sin6_port) != 0) {
snprintf(portstr, sizeof(portstr), ".%d", ntohs(sin6->sin6_port));
if (portbuf) {
snprintf(portbuf, 7, "%d", ntohs(sin6->sin6_port));
}
strcat(str, portstr);
}
return(str);
}
#endif
default: {
snprintf(str, sizeof(str), "sock_ntop: unknown AF_xxx: %d, len %d", sa->sa_family, salen);
return(str);
}
}
return NULL;
}
/*
* Function name: SSL_writen
* Description: Writes the given amount of data to the SSL connection.
* Arguments: fd - the SSL connection
* buf - buffer with data to write
* amount - the size of the data
* Returns: The amount of bytes written to the SSL connection.
*/
int
SSL_writen(SSL* fd, unsigned char* buf, int amount)
{
int sent, n;
sent = 0;
assert(amount > 0);
while (sent < amount) {
n = SSL_write(fd, buf+sent, amount - sent);
assert(n != 0);
if (n != -1) {
sent += n;
}
if (n == -1) {
if (errno != EAGAIN)
return 0;
}
}
return amount;
}
/*
* Function name: SSL_readn
* Description: Reads the given amount of data from the SSL connection.
* Arguments: fd - the SSL connection
* buf - buffer for data
* amount - the size of the data to read
* Returns: The amount of bytes read from the SSL connection.
*/
int
SSL_readn(SSL* fd, unsigned char* buf, int amount)
{
int sent, n;
sent = 0;
assert(amount > 0);
while (sent < amount) {
n = SSL_read(fd, buf+sent, amount - sent);
if (n != -1) {
sent += n;
}
if (n == 0)
return 0;
if (n == -1) {
if (errno != EAGAIN)
return 0;
}
}
return amount;
}
/*
* Function name: writen
* Description: Writes the given amount of data to the file descriptor.
* Arguments: fd - the file descriptor
* buf - buffer with data to write
* amount - the size of the data
* Returns: The amount of bytes written to the file descriptor
*/
int
writen(int fd, unsigned char* buf, int amount)
{
int sent, n;
sent = 0;
assert(amount > 0);
while (sent < amount) {
n = write(fd, buf+sent, amount - sent);
assert(n != 0);
if (n != -1) {
sent += n;
}
if (n == -1) {
if (errno != EAGAIN)
return 0;
}
}
return amount;
}
/*
* Function name: readn
* Description: Reads the given amount of data from the file descriptor.
* Arguments: fd - the file descriptor
* buf - buffer for data
* amount - the size of the data to read
* Returns: The amount of bytes read from the file descriptor.
*/
int
readn(int fd, unsigned char* buf, int amount)
{
int sent, n;
sent = 0;
assert(amount > 0);
while (sent < amount) {
n = read(fd, buf+sent, amount - sent);
if (n != -1) {
sent += n;
}
if (n == 0)
return 0;
if (n == -1) {
if (errno != EAGAIN)
return 0;
}
}
return amount;
}
|