Conversion to struct addrinfo is almost complete for this file.

This commit is contained in:
Ivo Timmermans 2001-11-16 22:41:38 +00:00
parent 4f47da5b87
commit c2752b961c

View file

@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: net.c,v 1.35.4.150 2001/11/16 17:38:39 zarq Exp $ $Id: net.c,v 1.35.4.151 2001/11/16 22:41:38 zarq Exp $
*/ */
#include "config.h" #include "config.h"
@ -87,21 +87,19 @@ int do_prune = 0;
char *hostlookup(struct sockaddr *addr, int numericonly) char *hostlookup(struct sockaddr *addr, int numericonly)
{ {
char *name; char *hostname;
struct hostent *host = NULL;
struct in_addr in;
config_t const *cfg;
int flags = 0; int flags = 0;
int r;
cp cp
if(numericonly if(numericonly
|| ((cfg = get_config_val(config, resolve_dns)) == NULL || (get_config_bool(lookup_config(config_tree, "ResolveDNS"), &r)
|| cfg->data.val != stupid_true)) || !r ))
flags |= NI_NUMERICHOST; flags |= NI_NUMERICHOST;
hostname = xmalloc(NI_MAXHOST); hostname = xmalloc(NI_MAXHOST);
if((r = getnameinfo(addr, sizeof(*addr), &hostname, NI_MAXHOST, NULL, 0, flags)) != 0) if((r = getnameinfo(addr, sizeof(*addr), hostname, NI_MAXHOST, NULL, 0, flags)) != 0)
{ {
free(hostname); free(hostname);
if(flags & NI_NUMERICHOST) if(flags & NI_NUMERICHOST)
@ -161,8 +159,6 @@ void send_udppacket(node_t *n, vpn_packet_t *inpkt)
vpn_packet_t outpkt; vpn_packet_t outpkt;
int outlen, outpad; int outlen, outpad;
EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX ctx;
struct sockaddr_in to;
socklen_t tolen = sizeof(to);
vpn_packet_t *copy; vpn_packet_t *copy;
cp cp
if(!n->status.validkey) if(!n->status.validkey)
@ -349,7 +345,7 @@ cp
if(bind(nfd, ai->ai_addr, ai->ai_addrlen)) if(bind(nfd, ai->ai_addr, ai->ai_addrlen))
{ {
close(nfd); close(nfd);
syslog(LOG_ERR, _("Can't bind to %s port %d/tcp: %m"), syslog(LOG_ERR, _("Can't bind to %s port %s/tcp: %m"),
ai->ai_canonname, n->port); ai->ai_canonname, n->port);
continue; continue;
} }
@ -447,7 +443,7 @@ cp
if(bind(nfd, ai->ai_addr, ai->ai_addrlen)) if(bind(nfd, ai->ai_addr, ai->ai_addrlen))
{ {
close(nfd); close(nfd);
syslog(LOG_ERR, _("Can't bind to %s port %d/tcp: %m"), syslog(LOG_ERR, _("Can't bind to %s port %s/tcp: %m"),
ai->ai_canonname, n->port); ai->ai_canonname, n->port);
continue; continue;
} }
@ -467,7 +463,6 @@ cp
int setup_outgoing_socket(connection_t *c) int setup_outgoing_socket(connection_t *c)
{ {
int flags; int flags;
struct sockaddr_in a;
cp cp
if(debug_lvl >= DEBUG_CONNECTIONS) if(debug_lvl >= DEBUG_CONNECTIONS)
syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname); syslog(LOG_INFO, _("Trying to connect to %s (%s)"), c->name, c->hostname);
@ -476,7 +471,7 @@ cp
if(c->socket == -1) if(c->socket == -1)
{ {
syslog(LOG_ERR, _("Creating socket for %s port %d failed: %m"), syslog(LOG_ERR, _("Creating socket for %s port %s failed: %m"),
c->hostname, c->port); c->hostname, c->port);
return -1; return -1;
} }
@ -863,7 +858,7 @@ cp
myself->status.active = 1; myself->status.active = 1;
node_add(myself); node_add(myself);
syslog(LOG_NOTICE, _("Ready: listening on port %hd"), myself->port); syslog(LOG_NOTICE, _("Ready: listening on port %s"), myself->port);
cp cp
return 0; return 0;
} }
@ -957,14 +952,21 @@ cp
return NULL; return NULL;
} }
asprintf(&(c->address), " = ntohl(ci.sin_addr.s_addr); c->address = sockaddr_to_addrinfo(ci);
c->hostname = hostlookup(ci.sin_addr.s_addr);
c->hostname = xmalloc(INET6_ADDRSTRLEN);
if((inet_ntop(ci.sin_family, &(ci.sin_addr), c->hostname, INET6_ADDRSTRLEN)) == NULL)
{
syslog(LOG_ERR, _("Couldn't convert address to string: %m"));
free(c->hostname);
return NULL;
}
asprintf(&(c->port), "%d", htons(ci.sin_port)); asprintf(&(c->port), "%d", htons(ci.sin_port));
c->socket = sfd; c->socket = sfd;
c->last_ping_time = time(NULL); c->last_ping_time = time(NULL);
if(debug_lvl >= DEBUG_CONNECTIONS) if(debug_lvl >= DEBUG_CONNECTIONS)
syslog(LOG_NOTICE, _("Connection from %s port %d"), syslog(LOG_NOTICE, _("Connection from %s port %s"),
c->hostname, c->port); c->hostname, c->port);
c->allow_request = ID; c->allow_request = ID;
@ -1025,11 +1027,11 @@ cp
return; return;
} }
n = lookup_node_udp(ntohl(from.sin_addr.s_addr), ntohs(from.sin_port)); n = lookup_node_udp(sockaddr_to_addrinfo(&from));
if(!n) if(!n)
{ {
syslog(LOG_WARNING, _("Received UDP packet on port %hd from unknown source %x:%hd"), myself->port, ntohl(from.sin_addr.s_addr), ntohs(from.sin_port)); syslog(LOG_WARNING, _("Received UDP packet on port %s from unknown source %x:%hd"), myself->port, ntohl(from.sin_addr.s_addr), ntohs(from.sin_port));
return; return;
} }
/* /*
@ -1221,7 +1223,7 @@ cp
if(setup_outgoing_connection(name)) /* function returns 0 when there are no problems */ if(setup_outgoing_connection(name)) /* function returns 0 when there are no problems */
retry = 1; retry = 1;
cfg = lookup_config_next(config._tree, cfg); /* Next time skip to next ConnectTo line */ cfg = lookup_config_next(config_tree, cfg); /* Next time skip to next ConnectTo line */
} }
get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout); get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout);