2002-05-27 02:51:17 +08:00
|
|
|
/* $Id: sock.c,v 1.36 2002-05-26 18:51:17 rjkaes Exp $
|
2000-02-17 01:32:49 +08:00
|
|
|
*
|
|
|
|
* Sockets are created and destroyed here. When a new connection comes in from
|
|
|
|
* a client, we need to copy the socket and the create a second socket to the
|
|
|
|
* remote server the client is trying to connect to. Also, the listening
|
|
|
|
* socket is created and destroyed here. Sounds more impressive than it
|
|
|
|
* actually is.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Steven Young
|
|
|
|
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
|
|
|
* Copyright (C) 2000 Chris Lightfoot (chris@ex-parrot.com)
|
|
|
|
*
|
|
|
|
* 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, 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tinyproxy.h"
|
2000-09-12 07:56:32 +08:00
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
#include "log.h"
|
2002-05-24 02:25:55 +08:00
|
|
|
#include "heap.h"
|
2000-09-12 07:56:32 +08:00
|
|
|
#include "sock.h"
|
2002-05-24 02:25:55 +08:00
|
|
|
#include "text.h"
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-12-15 13:58:30 +08:00
|
|
|
/*
|
|
|
|
* Take a string host address and return a struct in_addr so we can connect
|
|
|
|
* to the remote host.
|
|
|
|
*
|
|
|
|
* Return a negative if there is a problem.
|
|
|
|
*/
|
|
|
|
static int
|
2002-04-16 11:21:46 +08:00
|
|
|
lookup_domain(struct in_addr *addr, const char *domain)
|
2002-05-24 02:25:55 +08:00
|
|
|
{
|
2002-05-27 02:51:17 +08:00
|
|
|
struct hostent* result;
|
2001-12-15 13:58:30 +08:00
|
|
|
|
2002-05-27 02:51:17 +08:00
|
|
|
assert(domain != NULL);
|
2001-12-15 13:58:30 +08:00
|
|
|
|
2002-05-27 02:51:17 +08:00
|
|
|
result = gethostbyname(domain);
|
|
|
|
if (result) {
|
|
|
|
memcpy(addr, result->h_addr_list[0], result->h_length);
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
return -1;
|
2001-12-15 13:58:30 +08:00
|
|
|
}
|
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
/* This routine is so old I can't even remember writing it. But I do
|
|
|
|
* remember that it was an .h file because I didn't know putting code in a
|
|
|
|
* header was bad magic yet. anyway, this routine opens a connection to a
|
|
|
|
* system and returns the fd.
|
2000-09-12 07:56:32 +08:00
|
|
|
* - steve
|
|
|
|
*
|
2000-02-17 01:32:49 +08:00
|
|
|
* Cleaned up some of the code to use memory routines which are now the
|
|
|
|
* default. Also, the routine first checks to see if the address is in
|
2000-09-12 07:56:32 +08:00
|
|
|
* dotted-decimal form before it does a name lookup.
|
2000-02-17 01:32:49 +08:00
|
|
|
* - rjkaes
|
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
int
|
|
|
|
opensock(char *ip_addr, uint16_t port)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2000-09-12 07:56:32 +08:00
|
|
|
int sock_fd;
|
2000-02-17 01:32:49 +08:00
|
|
|
struct sockaddr_in port_info;
|
2002-04-13 13:20:19 +08:00
|
|
|
struct sockaddr_in bind_addr;
|
2000-09-12 07:56:32 +08:00
|
|
|
int ret;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-05-24 02:01:23 +08:00
|
|
|
assert(ip_addr != NULL);
|
|
|
|
assert(port > 0);
|
|
|
|
|
2001-11-22 08:19:18 +08:00
|
|
|
memset((struct sockaddr *) &port_info, 0, sizeof(port_info));
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
port_info.sin_family = AF_INET;
|
|
|
|
|
2001-08-29 12:00:22 +08:00
|
|
|
/* Lookup and return the address if possible */
|
2001-12-15 13:58:30 +08:00
|
|
|
ret = lookup_domain(&port_info.sin_addr, ip_addr);
|
2000-09-12 07:56:32 +08:00
|
|
|
|
|
|
|
if (ret < 0) {
|
2001-11-22 08:19:18 +08:00
|
|
|
log_message(LOG_ERR,
|
|
|
|
"opensock: Could not lookup address \"%s\".",
|
|
|
|
ip_addr);
|
2000-02-17 01:32:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
port_info.sin_port = htons(port);
|
|
|
|
|
|
|
|
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
2001-11-22 08:19:18 +08:00
|
|
|
log_message(LOG_ERR, "opensock: socket() error \"%s\".",
|
|
|
|
strerror(errno));
|
2000-02-17 01:32:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-04-13 13:20:19 +08:00
|
|
|
/* Bind to our listening address*/
|
2002-04-25 00:48:34 +08:00
|
|
|
if (config.bind_address) {
|
2002-04-13 13:20:19 +08:00
|
|
|
memset(&bind_addr, 0, sizeof(bind_addr));
|
|
|
|
bind_addr.sin_family = AF_INET;
|
2002-04-25 00:48:34 +08:00
|
|
|
bind_addr.sin_addr.s_addr = inet_addr(config.bind_address);
|
2002-04-13 13:20:19 +08:00
|
|
|
|
|
|
|
ret = bind(sock_fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
|
|
|
|
if (ret < 0) {
|
|
|
|
log_message(LOG_ERR, "Could not bind local address \"%\" because of %s",
|
2002-04-25 00:48:34 +08:00
|
|
|
config.bind_address,
|
2002-04-13 13:20:19 +08:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-23 09:18:43 +08:00
|
|
|
if (connect(sock_fd, (struct sockaddr *) &port_info, sizeof(port_info)) < 0) {
|
2001-11-22 08:19:18 +08:00
|
|
|
log_message(LOG_ERR, "opensock: connect() error \"%s\".",
|
|
|
|
strerror(errno));
|
2000-09-12 07:56:32 +08:00
|
|
|
return -1;
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return sock_fd;
|
|
|
|
}
|
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
/*
|
|
|
|
* Set the socket to non blocking -rjkaes
|
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
int
|
|
|
|
socket_nonblocking(int sock)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2000-09-12 07:56:32 +08:00
|
|
|
int flags;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-05-24 02:01:23 +08:00
|
|
|
assert(sock >= 0);
|
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
flags = fcntl(sock, F_GETFL, 0);
|
|
|
|
return fcntl(sock, F_SETFL, flags | O_NONBLOCK);
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-09-12 07:56:32 +08:00
|
|
|
* Set the socket to blocking -rjkaes
|
2000-02-17 01:32:49 +08:00
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
int
|
|
|
|
socket_blocking(int sock)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2000-09-12 07:56:32 +08:00
|
|
|
int flags;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-05-24 02:01:23 +08:00
|
|
|
assert(sock >= 0);
|
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
flags = fcntl(sock, F_GETFL, 0);
|
|
|
|
return fcntl(sock, F_SETFL, flags & ~O_NONBLOCK);
|
|
|
|
}
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
/*
|
|
|
|
* Start listening to a socket. Create a socket with the selected port.
|
|
|
|
* The size of the socket address will be returned to the caller through
|
|
|
|
* the pointer, while the socket is returned as a default return.
|
|
|
|
* - rjkaes
|
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
int
|
|
|
|
listen_sock(uint16_t port, socklen_t * addrlen)
|
2000-09-12 07:56:32 +08:00
|
|
|
{
|
|
|
|
int listenfd;
|
|
|
|
const int on = 1;
|
|
|
|
struct sockaddr_in addr;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-05-24 02:01:23 +08:00
|
|
|
assert(port > 0);
|
|
|
|
assert(addrlen != NULL);
|
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
if (config.ipAddr) {
|
2000-09-12 07:56:32 +08:00
|
|
|
addr.sin_addr.s_addr = inet_addr(config.ipAddr);
|
2000-02-17 01:32:49 +08:00
|
|
|
} else {
|
2000-09-12 07:56:32 +08:00
|
|
|
addr.sin_addr.s_addr = inet_addr("0.0.0.0");
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
2001-11-22 08:19:18 +08:00
|
|
|
|
2002-04-14 03:03:18 +08:00
|
|
|
if (bind(listenfd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
|
|
log_message(LOG_ERR, "Unable to bind listening socket because of %s",
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2002-04-14 03:03:18 +08:00
|
|
|
if (listen(listenfd, MAXLISTEN) < 0) {
|
|
|
|
log_message(LOG_ERR, "Unable to start listening socket because of %s",
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
*addrlen = sizeof(addr);
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
return listenfd;
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-05-24 02:25:55 +08:00
|
|
|
* Return the peer's socket information.
|
2000-02-17 01:32:49 +08:00
|
|
|
*/
|
2002-05-24 02:25:55 +08:00
|
|
|
int
|
|
|
|
getpeer_information(int fd, char* ipaddr, char* string_addr)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_in name;
|
2001-05-27 10:31:20 +08:00
|
|
|
size_t namelen = sizeof(name);
|
2002-05-27 02:51:17 +08:00
|
|
|
struct hostent* result;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-05-24 02:01:23 +08:00
|
|
|
assert(fd >= 0);
|
|
|
|
assert(ipaddr != NULL);
|
2002-05-24 02:25:55 +08:00
|
|
|
assert(string_addr != NULL);
|
2001-05-24 02:01:23 +08:00
|
|
|
|
2001-10-23 11:57:34 +08:00
|
|
|
/*
|
2002-05-24 02:25:55 +08:00
|
|
|
* Clear the memory.
|
2001-10-23 11:57:34 +08:00
|
|
|
*/
|
2002-05-24 02:25:55 +08:00
|
|
|
memset(ipaddr, '\0', PEER_IP_LENGTH);
|
|
|
|
memset(string_addr, '\0', PEER_STRING_LENGTH);
|
2001-10-23 11:57:34 +08:00
|
|
|
|
2002-05-24 02:25:55 +08:00
|
|
|
if (getpeername(fd, (struct sockaddr *)&name, &namelen) != 0) {
|
|
|
|
log_message(LOG_ERR, "getpeer_information: getpeername() error: %s",
|
2001-11-22 08:19:18 +08:00
|
|
|
strerror(errno));
|
2002-05-24 02:25:55 +08:00
|
|
|
return -1;
|
2000-02-17 01:32:49 +08:00
|
|
|
} else {
|
2000-09-12 07:56:32 +08:00
|
|
|
strlcpy(ipaddr,
|
2002-05-24 02:25:55 +08:00
|
|
|
inet_ntoa(*(struct in_addr *)&name.sin_addr.s_addr),
|
2000-09-12 07:56:32 +08:00
|
|
|
PEER_IP_LENGTH);
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
2002-05-27 02:51:17 +08:00
|
|
|
result = gethostbyaddr(ipaddr, strlen(ipaddr), AF_INET);
|
|
|
|
if (result) {
|
|
|
|
strlcpy(string_addr, result->h_name, PEER_STRING_LENGTH);
|
|
|
|
return 0;
|
|
|
|
} else
|
2002-05-24 02:25:55 +08:00
|
|
|
return -1;
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|