2004-04-28 02:53:14 +08:00
|
|
|
/* $Id: sock.c,v 1.41 2004-04-27 18:53:14 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
|
2004-02-19 04:18:53 +08:00
|
|
|
* Copyright (C) 1999,2004 Robert James Kaes (rjkaes@users.sourceforge.net)
|
2000-02-17 01:32:49 +08:00
|
|
|
* 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"
|
2004-02-19 04:18:53 +08:00
|
|
|
#include "network.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
|
|
|
/*
|
2004-02-19 04:18:53 +08:00
|
|
|
* Bind the given socket to the supplied address. The socket is
|
|
|
|
* returned if the bind succeeded. Otherwise, -1 is returned
|
|
|
|
* to indicate an error.
|
2001-12-15 13:58:30 +08:00
|
|
|
*/
|
|
|
|
static int
|
2004-02-19 04:18:53 +08:00
|
|
|
bind_socket(int sockfd, const char* addr)
|
|
|
|
{
|
|
|
|
struct addrinfo hints, *res, *ressave;
|
|
|
|
|
|
|
|
assert(sockfd >= 0);
|
|
|
|
assert(addr != NULL && strlen(addr) != 0);
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
/* The local port it not important */
|
|
|
|
if (getaddrinfo(addr, NULL, &hints, &res) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ressave = res;
|
2001-12-15 13:58:30 +08:00
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
/* Loop through the addresses and try to bind to each */
|
|
|
|
do {
|
|
|
|
if (bind(sockfd, res->ai_addr, res->ai_addrlen) == 0)
|
|
|
|
break; /* success */
|
|
|
|
} while ((res = res->ai_next) != NULL);
|
2001-12-15 13:58:30 +08:00
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
freeaddrinfo(ressave);
|
|
|
|
if (res == NULL) /* was not able to bind to any address */
|
2002-05-27 02:51:17 +08:00
|
|
|
return -1;
|
2004-02-19 04:18:53 +08:00
|
|
|
|
|
|
|
return sockfd;
|
2001-12-15 13:58:30 +08:00
|
|
|
}
|
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
/*
|
|
|
|
* Open a connection to a remote host. It's been re-written to use
|
|
|
|
* the getaddrinfo() library function, which allows for a protocol
|
|
|
|
* independent implementation (mostly for IPv4 and IPv6 addresses.)
|
2000-02-17 01:32:49 +08:00
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
int
|
2004-04-28 02:53:14 +08:00
|
|
|
opensock(const char* host, int port, const char* bind_to)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2004-02-19 04:18:53 +08:00
|
|
|
int sockfd, n;
|
|
|
|
struct addrinfo hints, *res, *ressave;
|
|
|
|
char portstr[6];
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
assert(host != NULL);
|
2001-05-24 02:01:23 +08:00
|
|
|
assert(port > 0);
|
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
snprintf(portstr, sizeof(portstr), "%d", port);
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
n = getaddrinfo(host, portstr, &hints, &res);
|
|
|
|
if (n != 0) {
|
|
|
|
log_message(LOG_ERR, "opensock: Could not retrieve info for %s",
|
|
|
|
host);
|
2000-02-17 01:32:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
ressave = res;
|
|
|
|
do {
|
|
|
|
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
|
|
if (sockfd < 0)
|
|
|
|
continue; /* ignore this one */
|
|
|
|
|
|
|
|
/* Bind to the specified address */
|
2004-04-28 02:53:14 +08:00
|
|
|
if (bind_to) {
|
|
|
|
if (bind_socket(sockfd, bind_to) < 0) {
|
|
|
|
close(sockfd);
|
|
|
|
continue; /* can't bind, so try again */
|
|
|
|
}
|
|
|
|
} else if (config.bind_address) {
|
2004-02-19 04:18:53 +08:00
|
|
|
if (bind_socket(sockfd, config.bind_address) < 0) {
|
|
|
|
close(sockfd);
|
|
|
|
continue; /* can't bind, so try again */
|
|
|
|
}
|
|
|
|
}
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
|
|
|
|
break; /* success */
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
close(sockfd);
|
|
|
|
} while ((res = res->ai_next) != NULL);
|
2002-04-13 13:20:19 +08:00
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
freeaddrinfo(ressave);
|
|
|
|
if (res == NULL) {
|
|
|
|
log_message(LOG_ERR,
|
|
|
|
"opensock: Could not establish a connection to %s",
|
|
|
|
host);
|
2000-09-12 07:56:32 +08:00
|
|
|
return -1;
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
return sockfd;
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
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
|
2002-10-04 04:50:59 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-04-28 02:53:14 +08:00
|
|
|
/*
|
|
|
|
* Takes a socket descriptor and returns the socket's IP address.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
getsock_ip(int fd, char* ipaddr)
|
|
|
|
{
|
|
|
|
struct sockaddr_storage name;
|
|
|
|
int namelen = sizeof(name);
|
|
|
|
|
|
|
|
assert(fd >= 0);
|
|
|
|
|
|
|
|
if (getsockname(fd, (struct sockaddr *) &name, &namelen) != 0) {
|
|
|
|
log_message(LOG_ERR, "getsock_ip: getsockname() error: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_ip_string((struct sockaddr *)&name, ipaddr, IP_LENGTH) == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2004-04-28 02:53:14 +08:00
|
|
|
struct sockaddr_storage sa;
|
|
|
|
size_t salen = sizeof(sa);
|
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
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
/* Set the strings to default values */
|
|
|
|
ipaddr[0] = '\0';
|
2004-04-28 02:53:14 +08:00
|
|
|
strlcpy(string_addr, "[unknown]", HOSTNAME_LENGTH);
|
2001-10-23 11:57:34 +08:00
|
|
|
|
2004-02-19 04:18:53 +08:00
|
|
|
/* Look up the IP address */
|
2004-04-28 02:53:14 +08:00
|
|
|
if (getpeername(fd, (struct sockaddr *)&sa, &salen) != 0)
|
2002-05-24 02:25:55 +08:00
|
|
|
return -1;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2004-04-28 02:53:14 +08:00
|
|
|
if (get_ip_string((struct sockaddr *)&sa, ipaddr, IP_LENGTH) == NULL)
|
2002-05-24 02:25:55 +08:00
|
|
|
return -1;
|
2004-02-19 04:18:53 +08:00
|
|
|
|
|
|
|
/* Get the full host name */
|
2004-04-28 02:53:14 +08:00
|
|
|
return getnameinfo((struct sockaddr *)&sa, salen,
|
|
|
|
string_addr, HOSTNAME_LENGTH,
|
2004-02-19 04:18:53 +08:00
|
|
|
NULL, 0, 0);
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|