From 116ba3b3da73fb857cf75b5c92c6aacd70d94dd9 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 8 Jun 2002 12:57:10 +0000 Subject: [PATCH] Cleanup: - Remove checks for specific OS's, instead check for #defines/#includes. - Use uint??_t where appropriate. - Mask handling functions use void pointers to get rid of silly casts. --- configure.in | 11 +++++---- src/conf.c | 6 ++--- src/graph.c | 4 ++-- src/net.c | 6 +++-- src/net.h | 16 ++++++++----- src/net_packet.c | 6 +++-- src/net_setup.c | 6 +++-- src/net_socket.c | 14 +++++++---- src/netutl.c | 20 +++++++++------- src/netutl.h | 11 ++++----- src/node.h | 10 +++++--- src/route.c | 60 ++++++++++++++++++++++++++---------------------- src/subnet.c | 12 +++++----- src/tincd.c | 4 ++-- 14 files changed, 107 insertions(+), 79 deletions(-) diff --git a/configure.in b/configure.in index d8b68e4c..5efcd870 100644 --- a/configure.in +++ b/configure.in @@ -1,6 +1,6 @@ dnl Process this file with autoconf to produce a configure script. -dnl $Id: configure.in,v 1.13.2.44 2002/04/19 14:06:40 guus Exp $ +dnl $Id: configure.in,v 1.13.2.45 2002/06/08 12:57:09 guus Exp $ AC_INIT(src/tincd.c) AM_INIT_AUTOMAKE(tinc, 1.0-cvs) @@ -69,8 +69,9 @@ dnl Checks for libraries. dnl Checks for header files. AC_HEADER_STDC -AC_CHECK_HEADERS([fcntl.h limits.h sys/ioctl.h syslog.h unistd.h \ -sys/time.h malloc.h strings.h sys/file.h]) +AC_CHECK_HEADERS([fcntl.h limits.h malloc.h stdint.h strings.h syslog.h unistd.h \ +net/ethernet.h net/if.h netinet/ip.h netinet/tcp.h \ +sys/file.h sys/ioctl.h sys/param.h sys/time.h]) dnl Checks for typedefs, structures, and compiler characteristics. AC_C_CONST @@ -95,8 +96,8 @@ dnl Checks for library functions. AC_FUNC_MEMCMP AC_FUNC_ALLOCA AC_TYPE_SIGNAL -AC_CHECK_FUNCS([ftime socket select strtol strerror flock unsetenv \ -asprintf putenv strdup fcloseall daemon strsignal get_current_dir_name]) +AC_CHECK_FUNCS([asprintf daemon fcloseall flock ftime get_current_dir_name \ +putenv select strdup strerror strsignal strtol unsetenv]) jm_FUNC_MALLOC jm_FUNC_REALLOC diff --git a/src/conf.c b/src/conf.c index bc29e071..96dbef35 100644 --- a/src/conf.c +++ b/src/conf.c @@ -19,7 +19,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: conf.c,v 1.9.4.55 2002/04/09 11:42:48 guus Exp $ + $Id: conf.c,v 1.9.4.56 2002/06/08 12:57:09 guus Exp $ */ #include "config.h" @@ -255,8 +255,8 @@ cp /* Teach newbies what subnets are... */ - if(((subnet->type == SUBNET_IPV4) && maskcheck((char *)&subnet->net.ipv4.address, subnet->net.ipv4.prefixlength, sizeof(ipv4_t))) - || ((subnet->type == SUBNET_IPV6) && maskcheck((char *)&subnet->net.ipv6.address, subnet->net.ipv6.prefixlength, sizeof(ipv6_t)))) + if(((subnet->type == SUBNET_IPV4) && maskcheck(&subnet->net.ipv4.address, subnet->net.ipv4.prefixlength, sizeof(ipv4_t))) + || ((subnet->type == SUBNET_IPV6) && maskcheck(&subnet->net.ipv6.address, subnet->net.ipv6.prefixlength, sizeof(ipv6_t)))) { syslog(LOG_ERR, _("Network address and prefix length do not match for configuration variable %s in %s line %d"), cfg->variable, cfg->file, cfg->line); diff --git a/src/graph.c b/src/graph.c index 53152a54..9d9bc1f3 100644 --- a/src/graph.c +++ b/src/graph.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: graph.c,v 1.1.2.11 2002/03/24 16:28:27 guus Exp $ + $Id: graph.c,v 1.1.2.12 2002/06/08 12:57:09 guus Exp $ */ /* We need to generate two trees from the graph: @@ -50,7 +50,7 @@ #include #include "config.h" #include -#if defined(HAVE_FREEBSD) || defined(HAVE_OPENBSD) +#ifdef HAVE_SYS_PARAM_H #include #endif #include diff --git a/src/net.c b/src/net.c index 358e7a9c..7ebbbd0f 100644 --- a/src/net.c +++ b/src/net.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net.c,v 1.35.4.171 2002/05/01 09:15:58 guus Exp $ + $Id: net.c,v 1.35.4.172 2002/06/08 12:57:09 guus Exp $ */ #include "config.h" @@ -26,8 +26,10 @@ #include #include #include -#ifdef HAVE_LINUX +#ifdef HAVE_NETINET_IP_H #include +#endif +#ifdef HAVE_NETINET_TCP_H #include #endif #include diff --git a/src/net.h b/src/net.h index 922c5cf1..29839a29 100644 --- a/src/net.h +++ b/src/net.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net.h,v 1.9.4.49 2002/03/27 15:01:36 guus Exp $ + $Id: net.h,v 1.9.4.50 2002/06/08 12:57:09 guus Exp $ */ #ifndef __TINC_NET_H__ @@ -28,6 +28,10 @@ #include #include +#ifdef HAVE_STDINT_H + #include +#endif + #include "config.h" #ifdef ENABLE_JUMBOGRAMS @@ -46,12 +50,12 @@ typedef struct mac_t { - unsigned char x[6]; + uint8_t x[6]; } mac_t; typedef struct ipv4_t { - unsigned char x[4]; + uint8_t x[4]; } ipv4_t; typedef struct ip_mask_t { @@ -61,7 +65,7 @@ typedef struct ip_mask_t { typedef struct ipv6_t { - unsigned short x[8]; + uint16_t x[8]; } ipv6_t; typedef unsigned short port_t; @@ -83,8 +87,8 @@ typedef union { typedef struct vpn_packet_t { length_t len; /* the actual number of bytes in the `data' field */ int priority; /* priority or TOS */ - unsigned int seqno; /* 32 bits sequence number (network byte order of course) */ - unsigned char data[MAXSIZE]; + uint32_t seqno; /* 32 bits sequence number (network byte order of course) */ + uint8_t data[MAXSIZE]; } vpn_packet_t; typedef struct queue_element_t { diff --git a/src/net_packet.c b/src/net_packet.c index f0423c10..8dac0d9c 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_packet.c,v 1.1.2.14 2002/04/18 20:09:05 zarq Exp $ + $Id: net_packet.c,v 1.1.2.15 2002/06/08 12:57:09 guus Exp $ */ #include "config.h" @@ -26,8 +26,10 @@ #include #include #include -#ifdef HAVE_LINUX +#ifdef HAVE_NETINET_IP_H #include +#endif +#ifdef HAVE_NETINET_TCP_H #include #endif #include diff --git a/src/net_setup.c b/src/net_setup.c index ec0700c3..d2a60f02 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_setup.c,v 1.1.2.16 2002/06/02 16:06:33 guus Exp $ + $Id: net_setup.c,v 1.1.2.17 2002/06/08 12:57:10 guus Exp $ */ #include "config.h" @@ -26,8 +26,10 @@ #include #include #include -#ifdef HAVE_LINUX +#ifdef HAVE_NETINET_IP_H #include +#endif +#ifdef HAVE_NETINET_TCP_H #include #endif #include diff --git a/src/net_socket.c b/src/net_socket.c index 5111ec25..74841875 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: net_socket.c,v 1.1.2.12 2002/04/18 20:09:05 zarq Exp $ + $Id: net_socket.c,v 1.1.2.13 2002/06/08 12:57:10 guus Exp $ */ #include "config.h" @@ -26,8 +26,10 @@ #include #include #include -#ifdef HAVE_LINUX +#ifdef HAVE_NETINET_IP_H #include +#endif +#ifdef HAVE_NETINET_TCP_H #include #endif #include @@ -241,10 +243,12 @@ cp /* Optimize TCP settings */ -#ifdef HAVE_LINUX +#if defined(SOL_TCP) && defined(TCP_NODELAY) option = 1; setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option)); +#endif +#if defined(SOL_IP) && defined(IP_TOS) option = IPTOS_LOWDELAY; setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option)); #endif @@ -337,10 +341,12 @@ begin: /* Optimize TCP settings */ -#ifdef HAVE_LINUX +#if defined(SOL_TCP) && defined(TCP_NODELAY) option = 1; setsockopt(c->socket, SOL_TCP, TCP_NODELAY, &option, sizeof(option)); +#endif +#if defined(SOL_IP) && defined(IP_TOS) option = IPTOS_LOWDELAY; setsockopt(c->socket, SOL_IP, IP_TOS, &option, sizeof(option)); #endif diff --git a/src/netutl.c b/src/netutl.c index 4d91bf44..82a4259b 100644 --- a/src/netutl.c +++ b/src/netutl.c @@ -17,7 +17,7 @@ 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.37 2002/06/07 11:14:05 wsl Exp $ + $Id: netutl.c,v 1.12.4.38 2002/06/08 12:57:10 guus Exp $ */ #include "config.h" @@ -27,7 +27,7 @@ #include #include #include -#ifndef HAVE_NETBSD +#ifndef HAVE_STDINT_H #include #endif #include @@ -115,10 +115,8 @@ cp exit(0); } -#ifdef HAVE_LINUX if((scopeid = strchr(address, '%'))) *scopeid = '\0'; /* Descope. */ -#endif *addrstr = xstrdup(address); *portstr = xstrdup(port); @@ -185,9 +183,11 @@ void sockaddrunmap(sockaddr_t *sa) /* Subnet mask handling */ -int maskcmp(char *a, char *b, int masklen, int len) +int maskcmp(void *va, void *vb, int masklen, int len) { int i, m, result; + char *a = va; + char *b = vb; cp for(m = masklen, i = 0; m >= 8; m -= 8, i++) if((result = a[i] - b[i])) @@ -199,9 +199,10 @@ cp return 0; } -void mask(char *a, int masklen, int len) +void mask(void *va, int masklen, int len) { int i; + char *a = va; cp i = masklen / 8; masklen %= 8; @@ -213,9 +214,11 @@ cp a[i] = 0; } -void maskcpy(char *a, char *b, int masklen, int len) +void maskcpy(void *va, void *vb, int masklen, int len) { int i, m; + char *a = va; + char *b = vb; cp for(m = masklen, i = 0; m >= 8; m -= 8, i++) a[i] = b[i]; @@ -230,9 +233,10 @@ cp a[i] = 0; } -int maskcheck(char *a, int masklen, int len) +int maskcheck(void *va, int masklen, int len) { int i; + char *a = va; cp i = masklen / 8; masklen %= 8; diff --git a/src/netutl.h b/src/netutl.h index 654d7ad4..7126e0c3 100644 --- a/src/netutl.h +++ b/src/netutl.h @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: netutl.h,v 1.2.4.11 2002/03/17 15:59:29 guus Exp $ + $Id: netutl.h,v 1.2.4.12 2002/06/08 12:57:10 guus Exp $ */ #ifndef __TINC_NETUTL_H__ @@ -31,16 +31,15 @@ extern int hostnames; -extern char *hostlookup(unsigned long); extern struct addrinfo *str2addrinfo(char *, char *, int); extern sockaddr_t str2sockaddr(char *, char *); extern void sockaddr2str(sockaddr_t *, char **, char **); extern char *sockaddr2hostname(sockaddr_t *); extern int sockaddrcmp(sockaddr_t *, sockaddr_t *); extern void sockaddrunmap(sockaddr_t *); -extern int maskcmp(char *, char *, int, int); -extern void maskcpy(char *, char *, int, int); -extern void mask(char *, int, int); -extern int maskcheck(char *, int, int); +extern int maskcmp(void *, void *, int, int); +extern void maskcpy(void *, void *, int, int); +extern void mask(void *, int, int); +extern int maskcheck(void *, int, int); #endif /* __TINC_NETUTL_H__ */ diff --git a/src/node.h b/src/node.h index 98147d3e..6f366c2a 100644 --- a/src/node.h +++ b/src/node.h @@ -17,12 +17,16 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: node.h,v 1.1.2.13 2002/03/19 22:48:25 guus Exp $ + $Id: node.h,v 1.1.2.14 2002/06/08 12:57:10 guus Exp $ */ #ifndef __TINC_NODE_H__ #define __TINC_NODE_H__ +#ifdef HAVE_STDINT_H + #include +#endif + #include #include "subnet.h" @@ -67,8 +71,8 @@ typedef struct node_t { struct connection_t *connection; /* Connection associated with this node (if a direct connection exists) */ - unsigned int sent_seqno; /* Sequence number last sent to this node */ - unsigned int received_seqno; /* Sequence number last received from this node */ + uint32_t sent_seqno; /* Sequence number last sent to this node */ + uint32_t received_seqno; /* Sequence number last received from this node */ } node_t; extern struct node_t *myself; diff --git a/src/route.c b/src/route.c index 48a14ca8..9ddb1d1c 100644 --- a/src/route.c +++ b/src/route.c @@ -17,20 +17,20 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: route.c,v 1.1.2.39 2002/06/05 00:25:55 guus Exp $ + $Id: route.c,v 1.1.2.40 2002/06/08 12:57:10 guus Exp $ */ #include "config.h" -#if defined(HAVE_FREEBSD) || defined(HAVE_OPENBSD) || defined(HAVE_NETBSD) +#ifdef HAVE_SYS_PARAM_H #include #endif #include #include -#if defined(HAVE_SOLARIS) || defined(HAVE_OPENBSD) || defined(HAVE_NETBSD) +#ifdef HAVE_NET_IF_H #include - #define ETHER_ADDR_LEN 6 -#else +#endif +#ifdef HAVE_NET_ETHERNET_H #include #endif #include @@ -40,7 +40,7 @@ #include #include #include -#ifndef HAVE_NETBSD +#ifdef HAVE_STDINT_H #include #endif @@ -55,6 +55,10 @@ #include "system.h" +#ifndef ETHER_ADDR_LEN + #define ETHER_ADDR_LEN 6 +#endif + int routing_mode = RMODE_ROUTER; int priorityinheritance = 0; int macexpire = 600; @@ -173,14 +177,14 @@ cp if(debug_lvl >= DEBUG_TRAFFIC) { syslog(LOG_WARNING, _("Cannot route packet: unknown IPv6 destination address %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx"), - ntohs(*(short unsigned int *)&packet->data[38]), - ntohs(*(short unsigned int *)&packet->data[40]), - ntohs(*(short unsigned int *)&packet->data[42]), - ntohs(*(short unsigned int *)&packet->data[44]), - ntohs(*(short unsigned int *)&packet->data[46]), - ntohs(*(short unsigned int *)&packet->data[48]), - ntohs(*(short unsigned int *)&packet->data[50]), - ntohs(*(short unsigned int *)&packet->data[52])); + ntohs(*(uint16_t *)&packet->data[38]), + ntohs(*(uint16_t *)&packet->data[40]), + ntohs(*(uint16_t *)&packet->data[42]), + ntohs(*(uint16_t *)&packet->data[44]), + ntohs(*(uint16_t *)&packet->data[46]), + ntohs(*(uint16_t *)&packet->data[48]), + ntohs(*(uint16_t *)&packet->data[50]), + ntohs(*(uint16_t *)&packet->data[52])); } return NULL; @@ -189,9 +193,9 @@ cp return subnet->owner; } -unsigned short int inet_checksum(unsigned short int *data, int len, unsigned short int prevsum) +uint16_t inet_checksum(uint16_t *data, int len, uint16_t prevsum) { - unsigned long int checksum = prevsum ^ 0xFFFF; + uint32_t checksum = prevsum ^ 0xFFFF; while(len--) checksum += ntohs(*data++); @@ -208,7 +212,7 @@ void route_neighborsol(vpn_packet_t *packet) struct nd_neighbor_solicit *ns; struct nd_opt_hdr *opt; subnet_t *subnet; - short unsigned int checksum; + uint16_t checksum; struct { struct in6_addr ip6_src; /* source address */ @@ -248,8 +252,8 @@ cp /* Generate checksum */ - checksum = inet_checksum((unsigned short int *)&pseudo, sizeof(pseudo)/2, ~0); - checksum = inet_checksum((unsigned short int *)ns, sizeof(*ns)/2 + 4, checksum); + checksum = inet_checksum((uint16_t *)&pseudo, sizeof(pseudo)/2, ~0); + checksum = inet_checksum((uint16_t *)ns, sizeof(*ns)/2 + 4, checksum); if(checksum) { @@ -305,8 +309,8 @@ cp /* Generate checksum */ - checksum = inet_checksum((unsigned short int *)&pseudo, sizeof(pseudo)/2, ~0); - checksum = inet_checksum((unsigned short int *)ns, sizeof(*ns)/2 + 4, checksum); + checksum = inet_checksum((uint16_t *)&pseudo, sizeof(pseudo)/2, ~0); + checksum = inet_checksum((uint16_t *)ns, sizeof(*ns)/2 + 4, checksum); ns->nd_ns_hdr.icmp6_cksum = htons(checksum); @@ -318,7 +322,7 @@ void route_arp(vpn_packet_t *packet) { struct ether_arp *arp; subnet_t *subnet; - unsigned char ipbuf[4]; + uint8_t ipbuf[4]; cp /* First, snatch the source address from the ARP packet */ @@ -335,8 +339,8 @@ cp if(ntohs(arp->arp_hrd) != ARPHRD_ETHER || ntohs(arp->arp_pro) != ETHERTYPE_IP || - (int) (arp->arp_hln) != ETHER_ADDR_LEN || - (int) (arp->arp_pln) != 4 || + arp->arp_hln != ETHER_ADDR_LEN || + arp->arp_pln != 4 || ntohs(arp->arp_op) != ARPOP_REQUEST ) { if(debug_lvl > DEBUG_TRAFFIC) @@ -383,7 +387,7 @@ cp void route_outgoing(vpn_packet_t *packet) { - unsigned short int type; + uint16_t type; node_t *n = NULL; cp /* FIXME: multicast? */ @@ -391,7 +395,7 @@ cp switch(routing_mode) { case RMODE_ROUTER: - type = ntohs(*((unsigned short*)(&packet->data[12]))); + type = ntohs(*((uint16_t *)(&packet->data[12]))); switch(type) { case 0x0800: @@ -440,9 +444,9 @@ void route_incoming(node_t *source, vpn_packet_t *packet) case RMODE_ROUTER: { node_t *n = NULL; - unsigned short int type; + uint16_t type; - type = ntohs(*((unsigned short*)(&packet->data[12]))); + type = ntohs(*((uint16_t *)(&packet->data[12]))); switch(type) { case 0x0800: diff --git a/src/subnet.c b/src/subnet.c index 9d13d7c5..d971a4d7 100644 --- a/src/subnet.c +++ b/src/subnet.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: subnet.c,v 1.1.2.35 2002/04/26 18:13:00 zarq Exp $ + $Id: subnet.c,v 1.1.2.36 2002/06/08 12:57:10 guus Exp $ */ #include "config.h" @@ -176,7 +176,7 @@ subnet_t *str2net(char *subnetstr) { int i, l; subnet_t *subnet; - unsigned short int x[8]; + uint16_t x[8]; cp subnet = new_subnet(); cp @@ -323,14 +323,14 @@ cp break; } - if (!maskcmp((char *)address, (char *)&p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t))) + if (!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength, sizeof(ipv4_t))) break; else { /* Otherwise, see if there is a bigger enclosing subnet */ subnet.net.ipv4.prefixlength = p->net.ipv4.prefixlength - 1; - maskcpy((char *)&subnet.net.ipv4.address, (char *)&p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t)); + maskcpy(&subnet.net.ipv4.address, &p->net.ipv4.address, subnet.net.ipv4.prefixlength, sizeof(ipv4_t)); } } } while (p); @@ -360,14 +360,14 @@ cp if(p->type != SUBNET_IPV6) return NULL; - if (!maskcmp((char *)address, (char *)&p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t))) + if (!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength, sizeof(ipv6_t))) break; else { /* Otherwise, see if there is a bigger enclosing subnet */ subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1; - maskcpy((char *)&subnet.net.ipv6.address, (char *)&p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t)); + maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof(ipv6_t)); } } } while (p); diff --git a/src/tincd.c b/src/tincd.c index b8b29596..0838e829 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: tincd.c,v 1.10.4.58 2002/03/11 11:23:04 guus Exp $ + $Id: tincd.c,v 1.10.4.59 2002/06/08 12:57:10 guus Exp $ */ #include "config.h" @@ -338,7 +338,7 @@ main(int argc, char **argv, char **envp) if(show_help) usage(0); -#ifdef HAVE_SOLARIS +#ifndef LOG_PERROR openlog("tinc", LOG_CONS, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */ #else openlog("tinc", LOG_PERROR, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */ -- 2.20.1