X-Git-Url: https://www.tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Fnetutl.c;h=ea4839f077b984fc4ddc474dfb41bef32933aee2;hp=f3ef1341246b9a8f6f0ce963f902161bfe6cd225;hb=e8e69460a7090aaf6ecda8970d3060695de81b00;hpb=3831f51a53088bfcc1d148fd54b3083afe7fde32 diff --git a/src/netutl.c b/src/netutl.c index f3ef1341..ea4839f0 100644 --- a/src/netutl.c +++ b/src/netutl.c @@ -1,6 +1,7 @@ /* netutl.c -- some supporting network utility code - Copyright (C) 1998,1999,2000 Ivo Timmermans + Copyright (C) 1998-2002 Ivo Timmermans + 2000-2002 Guus Sliepen 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 @@ -16,238 +17,209 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: netutl.c,v 1.12.4.7 2000/08/17 16:51:07 guus Exp $ + $Id: netutl.c,v 1.12.4.25 2002/02/20 17:15:33 guus Exp $ */ #include "config.h" -#include +#include #include #include #include #include #include +#include #include #include +#include #include #include +#include "errno.h" #include "conf.h" -#include "encr.h" #include "net.h" #include "netutl.h" #include "system.h" -/* - look for a connection associated with the given vpn ip, - return its connection structure. - Skips connections that are not activated! -*/ -conn_list_t *lookup_conn(ip_t ip) -{ - conn_list_t *p = conn_list; -cp - /* Exact match suggested by James B. MacLean */ - for(p = conn_list; p != NULL; p = p->next) - if((ip == p->vpn_ip) && p->status.active) - return p; - for(p = conn_list; p != NULL; p = p->next) - if(((ip & p->vpn_mask) == (p->vpn_ip & p->vpn_mask)) && p->status.active) - return p; -cp - return NULL; -} +int hostnames = 0; /* - free a queue and all of its elements + Turn a string into a struct addrinfo. + Return NULL on failure. */ -void destroy_queue(packet_queue_t *pq) +struct addrinfo *str2addrinfo(char *address, char *service, int socktype) { - queue_element_t *p, *q; + struct addrinfo hint, *ai; + int err; cp - for(p = pq->head; p != NULL; p = q) + memset(&hint, 0, sizeof(hint)); + + hint.ai_family = addressfamily; + hint.ai_socktype = socktype; + + if((err = getaddrinfo(address, service, &hint, &ai))) { - q = p->next; - if(p->packet) - free(p->packet); - free(p); + if(debug_lvl >= DEBUG_ERROR) + syslog(LOG_WARNING, _("Error looking up %s port %s: %s\n"), address, service, gai_strerror(err)); + cp_trace(); + return NULL; } - free(pq); cp + return ai; } -/* - free a conn_list_t element and all its pointers -*/ -void free_conn_element(conn_list_t *p) +sockaddr_t str2sockaddr(char *address, char *port) { + struct addrinfo hint, *ai; + sockaddr_t result; + int err; cp - if(p->sq) - destroy_queue(p->sq); - if(p->rq) - destroy_queue(p->rq); - if(p->vpn_hostname) - free(p->vpn_hostname); - if(p->real_hostname) - free(p->real_hostname); - free_key(p->public_key); - free_key(p->key); - free(p); + memset(&hint, 0, sizeof(hint)); + + hint.ai_family = AF_UNSPEC; + hint.ai_flags = AI_NUMERICHOST; + hint.ai_socktype = SOCK_STREAM; + + if((err = getaddrinfo(address, port, &hint, &ai) || !ai)) + { + syslog(LOG_ERR, _("Error looking up %s port %s: %s\n"), address, port, gai_strerror(err)); + cp_trace(); + raise(SIGFPE); + exit(0); + } + + result = *(sockaddr_t *)ai->ai_addr; + freeaddrinfo(ai); cp + return result; } -/* - remove all marked connections -*/ -void prune_conn_list(void) +void sockaddr2str(sockaddr_t *sa, char **addrstr, char **portstr) { - conn_list_t *p, *prev = NULL, *next = NULL; + char address[NI_MAXHOST]; + char port[NI_MAXSERV]; + int err; cp - for(p = conn_list; p != NULL; ) + if((err = getnameinfo((struct sockaddr *)sa, sizeof(sockaddr_t), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST|NI_NUMERICSERV))) { - next = p->next; - - if(p->status.remove) - { - if(prev) - prev->next = next; - else - conn_list = next; - - free_conn_element(p); - } - else - prev = p; - - p = next; + syslog(LOG_ERR, _("Error while translating addresses: %s"), gai_strerror(err)); + cp_trace(); + raise(SIGFPE); + exit(0); } + + *addrstr = xstrdup(address); + *portstr = xstrdup(port); cp } -/* - creates new conn_list element, and initializes it -*/ -conn_list_t *new_conn_list(void) +char *sockaddr2hostname(sockaddr_t *sa) { - conn_list_t *p = xmalloc(sizeof(*p)); + char *str; + char address[NI_MAXHOST] = "unknown"; + char port[NI_MAXSERV] = "unknown"; + int err; cp - /* initialise all those stupid pointers at once */ - memset(p, '\0', sizeof(*p)); - p->vpn_mask = (ip_t)(~0L); /* If this isn't done, it would be a - wastebucket for all packets with - unknown destination. */ - p->nexthop = p; + if((err = getnameinfo((struct sockaddr *)sa, sizeof(sockaddr_t), address, sizeof(address), port, sizeof(port), hostnames?0:(NI_NUMERICHOST|NI_NUMERICSERV)))) + { + syslog(LOG_ERR, _("Error while looking up hostname: %s"), gai_strerror(err)); + } + + asprintf(&str, _("%s port %s"), address, port); cp - return p; + return str; } -/* - free all elements of conn_list -*/ -void destroy_conn_list(void) +int sockaddrcmp(sockaddr_t *a, sockaddr_t *b) { - conn_list_t *p, *next; -cp - for(p = conn_list; p != NULL; ) + int result; +cp + result = a->sa.sa_family - b->sa.sa_family; + + if(result) + return result; + + switch(a->sa.sa_family) { - next = p->next; - free_conn_element(p); - p = next; + case AF_UNSPEC: + return 0; + case AF_INET: + return memcmp(&a->in, &b->in, sizeof(a->in)); + case AF_INET6: + return memcmp(&a->in6, &b->in6, sizeof(a->in6)); + default: + syslog(LOG_ERR, _("sockaddrcmp() was called with unknown address family %d, exitting!"), a->sa.sa_family); + cp_trace(); + raise(SIGFPE); + exit(0); } - - conn_list = NULL; cp } -/* - look up the name associated with the ip - address `addr' -*/ +/* Subnet mask handling */ -char *hostlookup(unsigned long addr) +int maskcmp(char *a, char *b, int masklen, int len) { - char *name; - struct hostent *host = NULL; - struct in_addr in; - config_t const *cfg; - int lookup_hostname; + int i, m, result; cp - in.s_addr = addr; + for(m = masklen, i = 0; m >= 8; m -= 8, i++) + if((result = a[i] - b[i])) + return result; - lookup_hostname = 0; - if((cfg = get_config_val(resolve_dns)) != NULL) - if(cfg->data.val == stupid_true) - lookup_hostname = 1; + if(m) + return (a[i] & (0x100 - (m << 1))) - (b[i] & (0x100 - (m << 1))); - if(lookup_hostname) - host = gethostbyaddr((char *)&in, sizeof(in), AF_INET); + return 0; +} - if(!lookup_hostname || !host) - { - asprintf(&name, "%s", inet_ntoa(in)); - } - else - { - asprintf(&name, "%s", host->h_name); - } -cp - return name; +void mask(char *a, int masklen, int len) +{ + int i; +cp + i = masklen / 8; + masklen %= 8; + + if(masklen) + a[i++] &= (0x100 - (masklen << 1)); + + for(; i < len; i++) + a[i] = 0; } -/* - Turn a string into an IP addy with netmask - return NULL on failure -*/ -ip_mask_t *strtoip(char *str) +void maskcpy(char *a, char *b, int masklen, int len) { - ip_mask_t *ip; - int masker; - char *q, *p; - struct hostent *h; + int i, m; cp - p = str; - if((q = strchr(p, '/'))) - { - *q = '\0'; - q++; /* q now points to netmask part, or NULL if no mask */ - } - - if(!(h = gethostbyname(p))) - { - fprintf(stderr, _("Error looking up `%s': %s\n"), p, sys_errlist[h_errno]); - return NULL; - } + for(m = masklen, i = 0; m >= 8; m -= 8, i++) + a[i] = b[i]; - masker = 0; - if(q) + if(m) { - masker = strtol(q, &p, 10); - if(q == p || (*p)) - return NULL; + a[i] = b[i] & (0x100 - (m << 1)); + i++; } - ip = xmalloc(sizeof(*ip)); - ip->ip = ntohl(*((ip_t*)(h->h_addr_list[0]))); - - ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0; -cp - return ip; + for(; i < len; i++) + a[i] = 0; } -void dump_conn_list(void) +int maskcheck(char *a, int masklen, int len) { - conn_list_t *p; -cp - syslog(LOG_DEBUG, _("Connection list:")); - - for(p = conn_list; p != NULL; p = p->next) - { - syslog(LOG_DEBUG, _("%s netmask %d.%d.%d.%d at %s port %hd flags %d sockets %d, %d status %04x"), - p->vpn_hostname, IP_ADDR_V(p->vpn_mask), p->real_hostname, p->port, p->flags, - p->socket, p->meta_socket, p->status); - } -cp + int i; +cp + i = masklen / 8; + masklen %= 8; + + if(masklen) + if(a[i++] & ~(0x100 - (masklen << 1))) + return -1; + + for(; i < len; i++) + if(a[i] != 0) + return -1; + + return 0; }