Converted the various socket functions to work with both IPv4 and IPv6

addresses.
This commit is contained in:
Robert James Kaes 2004-02-18 20:18:53 +00:00
parent c21df1ea0f
commit 5ecd9157ce
2 changed files with 90 additions and 91 deletions

View File

@ -1,4 +1,4 @@
/* $Id: sock.c,v 1.39 2002-10-03 20:50:59 rjkaes Exp $ /* $Id: sock.c,v 1.40 2004-02-18 20:18:53 rjkaes Exp $
* *
* Sockets are created and destroyed here. When a new connection comes in from * 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 * a client, we need to copy the socket and the create a second socket to the
@ -7,7 +7,7 @@
* actually is. * actually is.
* *
* Copyright (C) 1998 Steven Young * Copyright (C) 1998 Steven Young
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com) * Copyright (C) 1999,2004 Robert James Kaes (rjkaes@users.sourceforge.net)
* Copyright (C) 2000 Chris Lightfoot (chris@ex-parrot.com) * Copyright (C) 2000 Chris Lightfoot (chris@ex-parrot.com)
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
@ -25,96 +25,103 @@
#include "log.h" #include "log.h"
#include "heap.h" #include "heap.h"
#include "network.h"
#include "sock.h" #include "sock.h"
#include "text.h" #include "text.h"
/* /*
* Take a string host address and return a struct in_addr so we can connect * Bind the given socket to the supplied address. The socket is
* to the remote host. * returned if the bind succeeded. Otherwise, -1 is returned
* * to indicate an error.
* Return a negative if there is a problem.
*/ */
static int static int
lookup_domain(struct in_addr *addr, const char *domain) bind_socket(int sockfd, const char* addr)
{ {
struct hostent* result; struct addrinfo hints, *res, *ressave;
assert(domain != NULL); assert(sockfd >= 0);
assert(addr != NULL && strlen(addr) != 0);
result = gethostbyname(domain); memset(&hints, 0, sizeof(struct addrinfo));
if (result) { hints.ai_family = AF_UNSPEC;
memcpy(addr, result->h_addr_list[0], result->h_length); hints.ai_socktype = SOCK_STREAM;
return 0;
} else /* The local port it not important */
if (getaddrinfo(addr, NULL, &hints, &res) != 0)
return -1; return -1;
ressave = res;
/* 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);
freeaddrinfo(ressave);
if (res == NULL) /* was not able to bind to any address */
return -1;
return sockfd;
} }
/* 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 * Open a connection to a remote host. It's been re-written to use
* header was bad magic yet. anyway, this routine opens a connection to a * the getaddrinfo() library function, which allows for a protocol
* system and returns the fd. * independent implementation (mostly for IPv4 and IPv6 addresses.)
* - steve
*
* 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
* dotted-decimal form before it does a name lookup.
* - rjkaes
*/ */
int int
opensock(char *ip_addr, uint16_t port) opensock(const char* host, int port)
{ {
int sock_fd; int sockfd, n;
struct sockaddr_in port_info; struct addrinfo hints, *res, *ressave;
struct sockaddr_in bind_addr; char portstr[6];
int ret;
assert(ip_addr != NULL); assert(host != NULL);
assert(port > 0); assert(port > 0);
memset((struct sockaddr *) &port_info, 0, sizeof(port_info)); memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
port_info.sin_family = AF_INET; snprintf(portstr, sizeof(portstr), "%d", port);
/* Lookup and return the address if possible */ n = getaddrinfo(host, portstr, &hints, &res);
ret = lookup_domain(&port_info.sin_addr, ip_addr); if (n != 0) {
log_message(LOG_ERR, "opensock: Could not retrieve info for %s",
if (ret < 0) { host);
log_message(LOG_ERR,
"opensock: Could not lookup address \"%s\".",
ip_addr);
return -1; return -1;
} }
port_info.sin_port = htons(port); ressave = res;
do {
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
log_message(LOG_ERR, "opensock: socket() error \"%s\".", if (sockfd < 0)
strerror(errno)); continue; /* ignore this one */
return -1;
}
/* Bind to the specified address */ /* Bind to the specified address */
if (config.bind_address) { if (config.bind_address) {
memset(&bind_addr, 0, sizeof(bind_addr)); if (bind_socket(sockfd, config.bind_address) < 0) {
bind_addr.sin_family = AF_INET; close(sockfd);
bind_addr.sin_addr.s_addr = inet_addr(config.bind_address); continue; /* can't bind, so try again */
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",
config.bind_address,
strerror(errno));
return -1;
} }
} }
if (connect(sock_fd, (struct sockaddr *) &port_info, sizeof(port_info)) < 0) { if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
log_message(LOG_ERR, "opensock: connect() error \"%s\".", break; /* success */
strerror(errno));
close(sockfd);
} while ((res = res->ai_next) != NULL);
freeaddrinfo(ressave);
if (res == NULL) {
log_message(LOG_ERR,
"opensock: Could not establish a connection to %s",
host);
return -1; return -1;
} }
return sock_fd; return sockfd;
} }
/* /*
@ -197,35 +204,26 @@ listen_sock(uint16_t port, socklen_t* addrlen)
int int
getpeer_information(int fd, char* ipaddr, char* string_addr) getpeer_information(int fd, char* ipaddr, char* string_addr)
{ {
struct sockaddr_in name; struct sockaddr sa;
size_t namelen = sizeof(name); size_t salen = sizeof(struct sockaddr);
struct hostent* result;
assert(fd >= 0); assert(fd >= 0);
assert(ipaddr != NULL); assert(ipaddr != NULL);
assert(string_addr != NULL); assert(string_addr != NULL);
/* /* Set the strings to default values */
* Clear the memory. ipaddr[0] = '\0';
*/ strlcpy(string_addr, "[unknown]", PEER_STRING_LENGTH);
memset(ipaddr, '\0', PEER_IP_LENGTH);
memset(string_addr, '\0', PEER_STRING_LENGTH);
if (getpeername(fd, (struct sockaddr *)&name, &namelen) != 0) { /* Look up the IP address */
log_message(LOG_ERR, "getpeer_information: getpeername() error: %s", if (getpeername(fd, &sa, &salen) != 0)
strerror(errno));
return -1; return -1;
} else {
strlcpy(ipaddr,
inet_ntoa(*(struct in_addr *)&name.sin_addr.s_addr),
PEER_IP_LENGTH);
}
result = gethostbyaddr((char *)&name.sin_addr.s_addr, 4, AF_INET); if (get_ip_string(&sa, ipaddr, PEER_IP_LENGTH) == NULL)
if (result) {
strlcpy(string_addr, result->h_name, PEER_STRING_LENGTH);
return 0;
} else {
return -1; return -1;
}
/* Get the full host name */
return getnameinfo(&sa, salen,
string_addr, PEER_STRING_LENGTH,
NULL, 0, 0);
} }

View File

@ -1,9 +1,9 @@
/* $Id: sock.h,v 1.11 2003-01-27 18:45:25 rjkaes Exp $ /* $Id: sock.h,v 1.12 2004-02-18 20:18:53 rjkaes Exp $
* *
* See 'sock.c' for a detailed description. * See 'sock.c' for a detailed description.
* *
* Copyright (C) 1998 Steven Young * Copyright (C) 1998 Steven Young
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com) * Copyright (C) 1999,2004 Robert James Kaes (rjkaes@users.sourceforge.net)
* *
* This program is free software; you can redistribute it and/or modify it * 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 * under the terms of the GNU General Public License as published by the
@ -19,12 +19,13 @@
#ifndef TINYPROXY_SOCK_H #ifndef TINYPROXY_SOCK_H
#define TINYPROXY_SOCK_H #define TINYPROXY_SOCK_H
#define PEER_IP_LENGTH 16 /* The IP length is set to 48, since IPv6 can be that long */
#define PEER_STRING_LENGTH 256 #define PEER_IP_LENGTH 48
#define PEER_STRING_LENGTH 1024
#define MAXLINE (1024 * 4) #define MAXLINE (1024 * 4)
extern int opensock(char *ip_addr, uint16_t port); extern int opensock(const char* host, int port);
extern int listen_sock(uint16_t port, socklen_t* addrlen); extern int listen_sock(uint16_t port, socklen_t* addrlen);
extern int socket_nonblocking(int sock); extern int socket_nonblocking(int sock);