The following commit has been merged in the master branch: commit 1895cca1721b4df55d42005e36d814ac002e7a7d Author: Marek Lindner lindner_marek@yahoo.de Date: Thu Oct 19 22:45:09 2006 +0200
daemonize batman -> batmand ;)
diff --git a/Makefile b/Makefile index ee5e254..ff4bae2 100755 --- a/Makefile +++ b/Makefile @@ -39,7 +39,7 @@ ifeq ($(UNAME),OpenBSD) OS_OBJ= posix.o bsd.o endif
-batman: batman.o $(OS_OBJ) +batmand: batman.o $(OS_OBJ) $(CC) -o $@ batman.o $(OS_OBJ) $(LDFLAGS)
clean: diff --git a/batman.c b/batman.c index 1982118..a3ee37c 100755 --- a/batman.c +++ b/batman.c @@ -143,7 +143,7 @@ struct orig_node *get_orig_node( unsigned int addr ) return orig_node; }
- if (debug_level >= 2) + if (debug_level == 3) output("Creating new originator\n");
orig_node = alloc_memory(sizeof(struct orig_node)); @@ -177,13 +177,11 @@ static void choose_gw()
if ( curr_gateway != NULL ) {
- if (debug_level >= 1) - output( "Removing default route - no gateway in range\n" ); + if (debug_level == 1) + printf( "Removing default route - no gateway in range\n" );
del_default_route();
- curr_gateway = NULL; - }
return; @@ -227,9 +225,9 @@ static void choose_gw()
tmp_curr_gw = gw_node;
- if (debug_level >= 1) { + if (debug_level == 1) { addr_to_string( tmp_curr_gw->orig_node->orig, orig_str, ADDR_STR_LEN ); - output( "Preferred gateway found: %s (%i,%i,%i)\n", orig_str, gw_node->orig_node->gwflags, gw_node->orig_node->packet_count, ( gw_node->orig_node->packet_count * gw_node->orig_node->gwflags ) ); + printf( "Preferred gateway found: %s (%i,%i,%i)\n", orig_str, gw_node->orig_node->gwflags, gw_node->orig_node->packet_count, ( gw_node->orig_node->packet_count * gw_node->orig_node->gwflags ) ); }
break; @@ -243,8 +241,8 @@ static void choose_gw()
if ( curr_gateway != NULL ) {
- if (debug_level >= 1) - output( "Removing default route - better gateway found\n" ); + if (debug_level == 1) + printf( "Removing default route - better gateway found\n" );
del_default_route();
@@ -255,16 +253,15 @@ static void choose_gw() /* may be the last gateway is now gone */ if ( ( curr_gateway != NULL ) && ( !is_aborted() ) ) {
- if (debug_level >= 1) { + if (debug_level == 1) { addr_to_string( curr_gateway->orig_node->orig, orig_str, ADDR_STR_LEN ); - output( "Adding default route to %s (%i,%i,%i)\n", orig_str, max_gw_class, max_packets, max_gw_factor ); + printf( "Adding default route to %s (%i,%i,%i)\n", orig_str, max_gw_class, max_packets, max_gw_factor ); }
- } - - if ( !is_aborted() ) add_default_route();
+ } + }
} @@ -281,7 +278,7 @@ static void update_routes( struct orig_node *orig_node ) int max_pack, max_ttl, neigh_ttl[found_ifs], neigh_pkts[found_ifs]; static char orig_str[ADDR_STR_LEN], next_str[ADDR_STR_LEN];
- if (debug_level >= 2) + if (debug_level == 3) output("update_routes() \n");
max_ttl = 0; @@ -313,13 +310,13 @@ static void update_routes( struct orig_node *orig_node ) max_ttl = neigh_ttl[max_if->if_num];
next_hop = neigh_node; - if (debug_level >= 2) + if (debug_level == 3) output("%d living received packets via selected router \n", neigh_pkts[max_if->if_num] ); } }
if (next_hop != NULL) { - if (debug_level >= 2) { + if (debug_level == 3) { addr_to_string(orig_node->orig, orig_str, ADDR_STR_LEN); addr_to_string(next_hop->addr, next_str, ADDR_STR_LEN); output("Route to %s via %s\n", orig_str, next_str); @@ -328,17 +325,18 @@ static void update_routes( struct orig_node *orig_node ) orig_node->packet_count = neigh_pkts[max_if->if_num];
if (orig_node->router != next_hop->addr) { - if (debug_level >= 2) + if (debug_level == 3) output("Route changed\n");
if (orig_node->router != 0) { - if (debug_level >= 2) + if (debug_level == 3) output("Deleting previous route\n");
add_del_route(orig_node->orig, orig_node->router, 1, orig_node->batman_if->dev, orig_node->batman_if->udp_send_sock); }
- if (debug_level >= 2) { output("Adding new route\n"); } + if (debug_level == 3) + output("Adding new route\n");
/* TODO: maybe the order delete, add should be changed ???? */ @@ -364,10 +362,10 @@ static void update_gw_list( struct orig_node *orig_node, unsigned char new_gwfla
if ( gw_node->orig_node == orig_node ) {
- if (debug_level >= 1) { + if (debug_level == 1) {
addr_to_string( gw_node->orig_node->orig, orig_str, ADDR_STR_LEN ); - output( "Gateway class of originator %s changed from %i to %i\n", orig_str, gw_node->orig_node->gwflags, new_gwflags ); + printf( "Gateway class of originator %s changed from %i to %i\n", orig_str, gw_node->orig_node->gwflags, new_gwflags );
}
@@ -376,8 +374,8 @@ static void update_gw_list( struct orig_node *orig_node, unsigned char new_gwfla list_del(gw_pos); free_memory(gw_pos);
- if (debug_level >= 1) - output( "Gateway %s removed from gateway list\n", orig_str ); + if (debug_level == 1) + printf( "Gateway %s removed from gateway list\n", orig_str );
} else {
@@ -392,9 +390,9 @@ static void update_gw_list( struct orig_node *orig_node, unsigned char new_gwfla
}
- if (debug_level >= 1) { + if (debug_level == 1) { addr_to_string( orig_node->orig, orig_str, ADDR_STR_LEN ); - output( "Found new gateway %s with class %i\n", orig_str, new_gwflags ); + printf( "Found new gateway %s with class %i\n", orig_str, new_gwflags ); }
gw_node = alloc_memory(sizeof(struct gw_node)); @@ -418,10 +416,10 @@ static void debug() static char str[ADDR_STR_LEN]; int l;
- if (debug_level < 2) + if (debug_level != 2) return;
- if (debug_level >= 2) { + if (debug_level == 2) { output("------------------ DEBUG ------------------\n"); output("Forward list\n");
@@ -489,7 +487,6 @@ int isDuplicate(unsigned int orig, unsigned short seqno) pack_node = list_entry(pack_pos, struct pack_node, list);
if (orig_node->orig == orig && pack_node->seqno == seqno){ - /* if (debug_level >= 2) { output("isDuplicate(): YES \n"); } */ return 1; }
@@ -524,7 +521,7 @@ struct orig_node *update_last_hop(struct packet *in, unsigned int neigh) { struct orig_node *orig_neigh_node;
- if (debug_level >= 3) { + if (debug_level == 3) { output("update_last_hop(): Searching originator entry of last-hop neighbour of received packet \n"); } orig_neigh_node = get_orig_node( neigh );
@@ -541,7 +538,7 @@ void update_originator(struct packet *in, unsigned int neigh, struct batman_if * struct neigh_node *neigh_node = NULL; struct pack_node *pack_node = NULL;
- if (debug_level >= 3) + if (debug_level == 3) output("update_originator(): Searching and updating originator entry of received packet, \n");
orig_node = get_orig_node( in->orig ); @@ -562,7 +559,7 @@ void update_originator(struct packet *in, unsigned int neigh, struct batman_if * }
if (neigh_node == NULL) { - if (debug_level >= 2) + if (debug_level == 3) output("Creating new last-hop neighbour of originator\n");
neigh_node = alloc_memory(sizeof (struct neigh_node)); @@ -572,7 +569,7 @@ void update_originator(struct packet *in, unsigned int neigh, struct batman_if * neigh_node->addr = neigh;
list_add_tail(&neigh_node->list, &orig_node->neigh_list); - } else if (debug_level >= 2) + } else if (debug_level == 3) output("Updating existing last-hop neighbour of originator\n");
list_for_each(pack_pos, &neigh_node->pack_list) { @@ -583,7 +580,7 @@ void update_originator(struct packet *in, unsigned int neigh, struct batman_if * }
if (pack_node == NULL) { - if (debug_level >= 2) + if (debug_level == 3) output("Creating new packet entry for last-hop neighbor of originator \n");
pack_node = alloc_memory(sizeof (struct pack_node)); @@ -593,7 +590,8 @@ void update_originator(struct packet *in, unsigned int neigh, struct batman_if * pack_node->if_incoming = if_incoming; list_add_tail(&pack_node->list, &neigh_node->pack_list); } else - output("ERROR - Updating existing packet\n"); + if (debug_level == 3) + output("ERROR - Updating existing packet\n");
neigh_node->best_ttl = in->ttl; pack_node->ttl = in->ttl; @@ -608,14 +606,14 @@ void schedule_forward_packet( struct packet *in, int unidirectional, struct orig struct forw_node *forw_node, *forw_node_new; struct list_head *forw_pos;
- if (debug_level >= 2) + if (debug_level == 3) output("schedule_forward_packet(): \n");
if (in->ttl <= 1) { - if (debug_level >= 2) + if (debug_level == 3) output("ttl exceeded \n"); } else if ( ( orig_node->router != neigh ) && ( orig_node->router != 0 ) ) { - if (debug_level >= 2) + if (debug_level == 3) output("not my best neighbour\n"); } else {
@@ -627,7 +625,7 @@ void schedule_forward_packet( struct packet *in, int unidirectional, struct orig forw_node_new->pack.ttl--;
if (unidirectional) { - if (debug_level >= 2) + if (debug_level == 3) output("sending with unidirectional flag \n");
forw_node_new->pack.flags = (forw_node_new->pack.flags | UNIDIRECTIONAL); @@ -663,7 +661,7 @@ void send_outstanding_packets() { pack = &forw_node->pack;
- if (debug_level >= 2) { + if (debug_level == 3) { addr_to_string(pack->orig, orig_str, ADDR_STR_LEN); output("Forwarding packet (originator %s, seqno %d, TTL %d)\n", orig_str, pack->seqno, pack->ttl); @@ -736,7 +734,7 @@ void purge() int gw_purged = 0; static char orig_str[ADDR_STR_LEN], neigh_str[ADDR_STR_LEN];
- if (debug_level >= 2) + if (debug_level == 3) output("purge() \n");
/* for all origins... */ @@ -754,7 +752,7 @@ void purge() /* remove them if outdated */ if ((int)((pack_node->time + TIMEOUT) < get_time())) { - if (debug_level >= 2) { + if (debug_level == 3) { addr_to_string(orig_node->orig, orig_str, ADDR_STR_LEN); addr_to_string(neigh_node->addr, neigh_str, ADDR_STR_LEN); output("Packet timeout (originator %s, neighbour %s, seqno %d, TTL %d, time %u)\n", @@ -772,7 +770,7 @@ void purge()
/* if no more packets, remove neighbour (next hop) towards given origin */ if (list_empty(&neigh_node->pack_list)) { - if (debug_level >= 2) { + if (debug_level == 3) { addr_to_string(neigh_node->addr, neigh_str, sizeof (neigh_str)); addr_to_string(orig_node->orig, orig_str, sizeof (orig_str)); output("Removing orphaned neighbour %s for originator %s\n", neigh_str, orig_str); @@ -784,7 +782,7 @@ void purge()
/* if no more neighbours (next hops) towards given origin, remove origin */ if (list_empty(&orig_node->neigh_list) && ((int)(orig_node->last_aware) + TIMEOUT <= ((int)(get_time())))) { - if (debug_level >= 2) { + if (debug_level == 3) { addr_to_string(orig_node->orig, orig_str, sizeof (orig_str)); output("Removing orphaned originator %s\n", orig_str); } @@ -796,8 +794,8 @@ void purge() if ( gw_node->orig_node == orig_node ) {
addr_to_string( gw_node->orig_node->orig, orig_str, ADDR_STR_LEN ); - if (debug_level >= 1) - output( "Removing gateway %s from gateway list\n", orig_str ); + if (debug_level == 1) + printf( "Removing gateway %s from gateway list\n", orig_str );
list_del(gw_pos); free_memory(gw_pos); @@ -814,7 +812,7 @@ void purge()
if ( orig_node->router != 0 ) {
- if (debug_level >= 2) + if (debug_level == 3) output("Deleting route to originator \n");
add_del_route(orig_node->orig, 0, 1, orig_node->batman_if->dev, orig_node->batman_if->udp_send_sock); @@ -891,7 +889,7 @@ int batman()
while (!is_aborted()) { - if (debug_level >= 2) + if (debug_level == 3) output(" \n \n");
schedule_own_packet(); @@ -912,7 +910,7 @@ int batman()
if (res > 0) { - if (debug_level >= 3) { + if (debug_level == 3) { addr_to_string(in.orig, orig_str, sizeof (orig_str)); addr_to_string(neigh, neigh_str, sizeof (neigh_str)); output("Received BATMAN packet from %s (originator %s, seqno %d, TTL %d)\n", neigh_str, orig_str, in.seqno, in.ttl); @@ -932,14 +930,14 @@ int batman()
if (is_my_addr == 1 /* && in.orig == my_addr */) {
- if (debug_level >= 3) { + if (debug_level == 3) { addr_to_string(neigh, neigh_str, sizeof (neigh_str)); output("Ignoring all (zero-hop) packets send by me (sender: %s)\n", neigh_str); }
} else if (is_broadcast == 1) {
- if (debug_level >= 0) { + if (debug_level == 3) { addr_to_string(neigh, neigh_str, sizeof (neigh)); output("Ignoring all packets with broadcast source IP (sender: %s)\n", neigh_str); } @@ -968,7 +966,7 @@ int batman()
}
- if (debug_level >= 2) { + if (debug_level == 3) {
addr_to_string(in.orig, orig_str, sizeof (orig_str)); addr_to_string(neigh, neigh_str, sizeof (neigh_str)); @@ -997,7 +995,7 @@ int batman()
if ( in.version != BATMAN_VERSION ) {
- if (debug_level >= 1) + if (debug_level == 1) output( "Incompatible batman version (%i) - ignoring packet... \n", in.version );
} else if ( in.flags & UNIDIRECTIONAL ) { @@ -1006,7 +1004,7 @@ int batman()
orig_neigh_node->last_reply = get_time();
- if (debug_level >= 2) + if (debug_level == 3) output("received my own packet from neighbour indicating bidirectional link, updating last_reply stamp \n");
} @@ -1038,7 +1036,7 @@ int batman()
} else {
- if (debug_level >= 2) + if (debug_level == 3) output("Ignoring packet... \n");
} @@ -1056,7 +1054,8 @@ int batman() time_count++; }
- output("Deleting all BATMAN routes\n"); + if ( debug_level > 0 ) + printf("Deleting all BATMAN routes\n");
list_for_each(orig_pos, &orig_list) { orig_node = list_entry(orig_pos, struct orig_node, list); diff --git a/linux.c b/linux.c index b4cd116..4b73911 100755 --- a/linux.c +++ b/linux.c @@ -92,7 +92,7 @@ int bind_to_iface( int udp_recv_sock, char *dev ) { void add_del_route(unsigned int dest, unsigned int router, int del, char *dev, int sock) { struct rtentry route; - char str1[16], str2[16]; + char str1[16], str2[16], log_str[100]; struct sockaddr_in *addr;
inet_ntop(AF_INET, &dest, str1, sizeof (str1)); @@ -124,27 +124,43 @@ void add_del_route(unsigned int dest, unsigned int router, int del, char *dev, i
route.rt_metric = 0; route.rt_flags = RTF_UP; - output("%s default route via %s\n", del ? "Deleting" : "Adding", dev); + + if ( debug_level == 1 ) { + printf("%s default route via %s\n", del ? "Deleting" : "Adding", dev); + } else if ( debug_level == 3 ) { + output("%s default route via %s\n", del ? "Deleting" : "Adding", dev); + }
} else {
route.rt_flags |= RTF_GATEWAY; - output("route to %s via %s (%s)\n", del ? "Deleting" : "Adding", str1, str2, dev); + + if ( debug_level == 1 ) { + printf("%s route to %s via %s (%s)\n", del ? "Deleting" : "Adding", str1, str2, dev); + } else if ( debug_level == 3 ) { + output("%s route to %s via %s (%s)\n", del ? "Deleting" : "Adding", str1, str2, dev); + }
}
} else { - output("%s route to %s via 0.0.0.0 (%s)\n", del ? "Deleting" : "Adding", str1, dev); + + if ( debug_level == 1 ) { + printf("%s route to %s via 0.0.0.0 (%s)\n", del ? "Deleting" : "Adding", str1, dev); + } else if ( debug_level == 3 ) { + output("%s route to %s via 0.0.0.0 (%s)\n", del ? "Deleting" : "Adding", str1, dev); + } + }
route.rt_dev = dev;
- if (ioctl(sock, del ? SIOCDELRT : SIOCADDRT, &route) < 0) - { - fprintf(stderr, "Cannot %s route to %s via %s: %s\n", - del ? "delete" : "add", str1, str2, strerror(errno)); + if ( ioctl( sock, del ? SIOCDELRT : SIOCADDRT, &route ) < 0 ) { + snprintf( log_str, sizeof( log_str ), "Error - can't %s route to %s via %s: %s\n", del ? "delete" : "add", str1, str2, strerror(errno) ); + do_log( log_str, strerror(errno) ); } + }
/* Probe for tun interface availability */ @@ -154,7 +170,7 @@ int probe_tun()
if ( ( fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) {
- fprintf( stderr, "Error - could not open '/dev/net/tun' ! Is the tun kernel module loaded ?\n" ); + do_log( "Error - could not open '/dev/net/tun' ! Is the tun kernel module loaded ?\n", strerror(errno) ); return 0;
} @@ -169,7 +185,7 @@ int del_dev_tun( int fd ) {
if ( ioctl( fd, TUNSETPERSIST, 0 ) < 0 ) {
- perror("TUNSETPERSIST"); + do_log( "Error - can't delete tun device: %s\n", strerror(errno) ); return -1;
} @@ -193,14 +209,14 @@ int add_dev_tun( struct batman_if *batman_if, unsigned int tun_addr, char *tun_d
if ( ( *fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) {
- perror("/dev/net/tun"); + do_log( "Error - can't create tun device (/dev/net/tun): %s\n", strerror(errno) ); return -1;
}
if ( ( ioctl( *fd, TUNSETIFF, (void *) &ifr_tun ) ) < 0 ) {
- perror("TUNSETIFF"); + do_log( "Error - can't create tun device (TUNSETIFF): %s\n", strerror(errno) ); close(*fd); return -1;
@@ -208,7 +224,7 @@ int add_dev_tun( struct batman_if *batman_if, unsigned int tun_addr, char *tun_d
if ( ioctl( *fd, TUNSETPERSIST, 1 ) < 0 ) {
- perror("TUNSETPERSIST"); + do_log( "Error - can't create tun device (TUNSETPERSIST): %s\n", strerror(errno) ); close(*fd); return -1;
@@ -218,7 +234,7 @@ int add_dev_tun( struct batman_if *batman_if, unsigned int tun_addr, char *tun_d tmp_fd = socket(AF_INET, SOCK_DGRAM, 0);
if ( tmp_fd < 0 ) { - fprintf(stderr, "Cannot create send socket: %s", strerror(errno)); + do_log( "Error - can't create tun device (udp socket): %s\n", strerror(errno) ); del_dev_tun( *fd ); return -1; } @@ -233,7 +249,7 @@ int add_dev_tun( struct batman_if *batman_if, unsigned int tun_addr, char *tun_d
if ( ioctl( tmp_fd, SIOCSIFADDR, &ifr_tun) < 0 ) {
- perror("SIOCSIFADDR"); + do_log( "Error - can't create tun device (SIOCSIFADDR): %s\n", strerror(errno) ); del_dev_tun( *fd ); close( tmp_fd ); return -1; @@ -243,7 +259,7 @@ int add_dev_tun( struct batman_if *batman_if, unsigned int tun_addr, char *tun_d
if ( ioctl( tmp_fd, SIOCGIFFLAGS, &ifr_tun) < 0 ) {
- perror("SIOCGIFFLAGS"); + do_log( "Error - can't create tun device (SIOCGIFFLAGS): %s\n", strerror(errno) ); del_dev_tun( *fd ); close( tmp_fd ); return -1; @@ -255,7 +271,7 @@ int add_dev_tun( struct batman_if *batman_if, unsigned int tun_addr, char *tun_d
if ( ioctl( tmp_fd, SIOCSIFFLAGS, &ifr_tun) < 0 ) {
- perror("SIOCSIFFLAGS"); + do_log( "Error - can't create tun device (SIOCSIFFLAGS): %s\n", strerror(errno) ); del_dev_tun( *fd ); close( tmp_fd ); return -1; @@ -267,7 +283,7 @@ int add_dev_tun( struct batman_if *batman_if, unsigned int tun_addr, char *tun_d
if ( ioctl( tmp_fd, SIOCGIFMTU, &ifr_if ) < 0 ) {
- perror("SIOCGIFMTU"); + do_log( "Error - can't create tun device (SIOCGIFMTU): %s\n", strerror(errno) ); del_dev_tun( *fd ); close( tmp_fd ); return -1; @@ -277,7 +293,7 @@ int add_dev_tun( struct batman_if *batman_if, unsigned int tun_addr, char *tun_d /* set MTU of tun interface: real MTU - 28 */ if ( ifr_if.ifr_mtu < 100 ) {
- fprintf(stderr, "Warning: MTU smaller than 100 - cannot reduce MTU anymore\n" ); + do_log( "Warning - MTU smaller than 100 -> can't reduce MTU anymore\n", strerror(errno) );
} else {
@@ -285,7 +301,7 @@ int add_dev_tun( struct batman_if *batman_if, unsigned int tun_addr, char *tun_d
if ( ioctl( tmp_fd, SIOCSIFMTU, &ifr_tun ) < 0 ) {
- perror("SIOCSIFMTU"); + do_log( "Error - can't create tun device (SIOCSIFMTU): %s\n", strerror(errno) ); del_dev_tun( *fd ); close( tmp_fd ); return -1; diff --git a/os.h b/os.h index 4f18e9e..2fbb879 100755 --- a/os.h +++ b/os.h @@ -39,5 +39,6 @@ int bind_to_iface( int udp_recv_sock, char *dev ); int probe_tun(); int del_dev_tun( int fd ); int add_dev_tun( struct batman_if *batman_if, unsigned int dest_addr, char *tun_dev, size_t tun_dev_size, int *fd ); +void do_log( char *description, char *error_msg );
#endif diff --git a/posix.c b/posix.c index ab18981..8292664 100644 --- a/posix.c +++ b/posix.c @@ -34,6 +34,7 @@ #include <stdlib.h> #include <sys/stat.h> #include <fcntl.h> +#include <syslog.h>
#include "os.h" #include "batman.h" @@ -84,6 +85,20 @@ void output(char *format, ...) va_end(args); }
+void do_log( char *description, char *error_msg ) { + + if ( debug_level == 0 ) { + + syslog( LOG_ERR, description, error_msg ); + + } else { + + printf( description, error_msg ); + + } + +} +
void *client_to_gw_tun( void *arg ) { @@ -118,7 +133,7 @@ void *client_to_gw_tun( void *arg ) { /* connect to server (ask permission) */ if ( ( curr_gateway_tcp_sock = socket(PF_INET, SOCK_STREAM, 0) ) < 0 ) {
- perror("socket"); + do_log( "Error - can't create tcp socket: %s\n", strerror(errno) ); curr_gateway = NULL; return NULL;
@@ -126,9 +141,9 @@ void *client_to_gw_tun( void *arg ) {
if ( connect ( curr_gateway_tcp_sock, (struct sockaddr *)&gw_addr, sizeof(struct sockaddr) ) < 0 ) {
- perror("connect"); - curr_gateway = NULL; + do_log( "Error - can't connect to gateway: %s\n", strerror(errno) ); close( curr_gateway_tcp_sock ); + curr_gateway = NULL; return NULL;
} @@ -139,19 +154,21 @@ void *client_to_gw_tun( void *arg ) { /* connect to server (establish udp tunnel) */ if ( ( curr_gateway_tun_sock = socket(PF_INET, SOCK_DGRAM, 0) ) < 0 ) {
- perror("connect"); - curr_gateway = NULL; + do_log( "Error - can't create udp socket: %s\n", strerror(errno) ); close( curr_gateway_tcp_sock ); + curr_gateway = NULL; return NULL;
}
if ( bind( curr_gateway_tun_sock, (struct sockaddr *)&my_addr, sizeof (struct sockaddr_in) ) < 0) { - fprintf(stderr, "Cannot bind tunnel socket: %s\n", strerror(errno)); - curr_gateway = NULL; + + do_log( "Error - can't bind tunnel socket: %s\n", strerror(errno) ); close( curr_gateway_tcp_sock ); close( curr_gateway_tun_sock ); + curr_gateway = NULL; return NULL; + }
@@ -161,10 +178,9 @@ void *client_to_gw_tun( void *arg ) {
} else {
- fprintf(stderr, "Cannot create tunnel device: %s\n", strerror(errno)); - curr_gateway = NULL; close( curr_gateway_tcp_sock ); close( curr_gateway_tun_sock ); + curr_gateway = NULL; return NULL;
} @@ -189,7 +205,8 @@ void *client_to_gw_tun( void *arg ) { server_keep_alive_timeout = get_time();
if ( write( curr_gateway_tcp_sock, keep_alive_string, sizeof( keep_alive_string ) ) < 0 ) { - fprintf(stderr, "server_keepalive failed: no connect to server\n"); + if ( debug_level == 1 ) + printf( "server_keepalive failed: no connect to server\n" ); break; }
@@ -212,19 +229,19 @@ void *client_to_gw_tun( void *arg ) {
if ( status > 0 ) {
- if ( debug_level >= 1 ) + if ( debug_level == 1 ) printf( "server message ?\n" );
} else if ( status < 0 ) {
- if ( debug_level >= 1 ) + if ( debug_level == 1 ) printf( "Cannot read message from gateway: %s\n", strerror(errno) );
break;
} else if (status == 0) {
- if ( debug_level >= 1 ) + if ( debug_level == 1 ) printf( "Gateway closed connection - timeout ?\n" );
break; @@ -236,13 +253,13 @@ void *client_to_gw_tun( void *arg ) {
if ( ( buff_len = recvfrom( curr_gateway_tun_sock, buff, sizeof( buff ), 0, (struct sockaddr *)&sender_addr, &addr_len ) ) < 0 ) {
- fprintf(stderr, "Cannot receive packet: %s\n", strerror(errno)); + do_log( "Error - can't receive packet: %s\n", strerror(errno) );
} else {
if ( write( curr_gateway_tun_fd, buff, buff_len ) < 0 ) {
- fprintf(stderr, "Cannot write packet into %s: %s\n", curr_gateway_tun_if, strerror(errno)); + do_log( "Error - can't write packet: %s\n", strerror(errno) );
}
@@ -252,12 +269,12 @@ void *client_to_gw_tun( void *arg ) {
if ( ( buff_len = read( curr_gateway_tun_fd, buff, sizeof( buff ) ) ) < 0 ) {
- fprintf(stderr, "Could not read data from %s: %s\n", curr_gateway_tun_if, strerror(errno)); + do_log( "Error - couldn't read data: %s\n", strerror(errno) );
} else {
if ( sendto(curr_gateway_tun_sock, buff, buff_len, 0, (struct sockaddr *)&gw_addr, sizeof (struct sockaddr_in) ) < 0 ) { - fprintf(stderr, "Cannot send to gateway: %s\n", strerror(errno)); + do_log( "Error - can't send to gateway: %s\n", strerror(errno) ); }
} @@ -266,9 +283,8 @@ void *client_to_gw_tun( void *arg ) {
} else if ( ( res < 0 ) && (errno != EINTR) ) {
- fprintf(stderr, "Cannot select: %s\n", strerror(errno)); - del_default_route(); - return NULL; + do_log( "Error - can't select: %s\n", strerror(errno) ); + break;
}
@@ -277,13 +293,13 @@ void *client_to_gw_tun( void *arg ) { /* cleanup */ add_del_route( 0, 0, 1, curr_gateway_tun_if, curr_gateway_batman_if->udp_send_sock );
- curr_gateway = NULL; - close( curr_gateway_tcp_sock ); close( curr_gateway_tun_sock );
del_dev_tun( curr_gateway_tun_fd );
+ curr_gateway = NULL; + return NULL;
} @@ -301,9 +317,11 @@ void del_default_route() {
int add_default_route() {
- if (pthread_create( &curr_gateway_thread_id, NULL, &client_to_gw_tun, curr_gateway ) != 0) { - perror("Could not spawn thread"); + if ( pthread_create( &curr_gateway_thread_id, NULL, &client_to_gw_tun, curr_gateway ) != 0 ) { + + do_log( "Error - couldn't not spawn thread: %s\n", strerror(errno) ); curr_gateway = NULL; + }
return 1; @@ -340,6 +358,9 @@ void close_all_sockets() { if (vis_if.sock) close(vis_if.sock);
+ if ( debug_level == 0 ) + closelog(); + }
int is_aborted() @@ -353,7 +374,7 @@ void *alloc_memory(int len)
if (res == NULL) { - fprintf(stderr, "Out of memory\n"); + do_log( "Error - out of memory: %s\n", strerror(errno) ); exit(1); }
@@ -366,7 +387,7 @@ void *realloc_memory(void *ptr, int len)
if (res == NULL) { - fprintf(stderr, "Out of memory\n"); + do_log( "Error - out of memory: %s\n", strerror(errno) ); exit(1); }
@@ -427,7 +448,7 @@ int receive_packet(unsigned char *buff, int len, unsigned int *neigh, unsigned i
if (errno != EINTR) { - fprintf(stderr, "Cannot select: %s\n", strerror(errno)); + do_log( "Error - can't select: %s\n", strerror(errno) ); return -1; } } @@ -444,7 +465,7 @@ int receive_packet(unsigned char *buff, int len, unsigned int *neigh, unsigned i
if (recvfrom(batman_if->udp_recv_sock, buff, len, 0, (struct sockaddr *)&addr, &addr_len) < 0) { - fprintf(stderr, "Cannot receive packet: %s\n", strerror(errno)); + do_log( "Error - can't receive packet: %s\n", strerror(errno) ); return -1; }
@@ -465,7 +486,7 @@ int send_packet(unsigned char *buff, int len, struct sockaddr_in *broad, int soc { if (sendto(sock, buff, len, 0, (struct sockaddr *)broad, sizeof (struct sockaddr_in)) < 0) { - fprintf(stderr, "Cannot send packet: %s\n", strerror(errno)); + do_log( "Error - can't send packet: %s\n", strerror(errno) ); return -1; }
@@ -498,13 +519,12 @@ void *gw_listen( void *arg ) {
if ( inet_pton(AF_INET, tun_ip, &tmp_ip_holder) < 1 ) {
- printf( "Invalid tunnel IP specified: %s\n", tun_ip ); + do_log( "Error - invalid tunnel IP specified: %s\n", tun_ip ); exit(EXIT_FAILURE);
}
if ( add_dev_tun( batman_if, tmp_ip_holder.s_addr, tun_dev, sizeof(tun_dev), &tun_fd ) < 0 ) { - printf( "Could not open tun device on interface: %s\n", gw_addr ); return NULL; }
@@ -539,7 +559,7 @@ void *gw_listen( void *arg ) { memset( gw_client, 0, sizeof(struct gw_client) );
if ( ( gw_client->sock = accept(batman_if->tcp_gw_sock, (struct sockaddr *)&gw_client->addr, &sin_size) ) == -1 ) { - perror("accept"); + do_log( "Error - can't accept client packet: %s\n", strerror(errno) ); continue; }
@@ -553,7 +573,7 @@ void *gw_listen( void *arg ) {
list_add_tail(&gw_client->list, &batman_if->client_list);
- if ( debug_level >= 1 ) { + if ( debug_level == 1 ) { addr_to_string(gw_client->addr.sin_addr.s_addr, str2, sizeof (str2)); printf( "gateway: %s (%s) got connection from %s (internet via %s)\n", gw_addr, batman_if->dev, str2, tun_dev ); } @@ -563,13 +583,13 @@ void *gw_listen( void *arg ) {
if ( ( buff_len = recvfrom( batman_if->tunnel_sock, buff, sizeof( buff ), 0, (struct sockaddr *)&addr, &addr_len ) ) < 0 ) {
- fprintf(stderr, "Cannot receive packet: %s\n", strerror(errno)); + do_log( "Error - can't receive packet: %s\n", strerror(errno) );
} else {
if ( write( tun_fd, buff, buff_len ) < 0 ) {
- fprintf(stderr, "Cannot write packet into %s: %s\n", tun_dev, strerror(errno)); + do_log( "Error - can't write packet: %s\n", strerror(errno) );
}
@@ -580,7 +600,7 @@ void *gw_listen( void *arg ) {
/* not needed - kernel knows client adress and routes traffic directly */
- fprintf(stderr, "Warning: data coming through tun device %s ?!\n", tun_dev); + do_log( "Warning - data coming through tun device: %s\n", tun_dev );
/*if ( ( buff_len = read( tun_fd, buff, sizeof( buff ) ) ) < 0 ) {
@@ -617,7 +637,7 @@ void *gw_listen( void *arg ) { if ( gw_client->sock > max_sock ) max_sock = gw_client->sock;
- if ( debug_level >= 1 ) { + if ( debug_level == 1 ) { addr_to_string(gw_client->addr.sin_addr.s_addr, str2, sizeof (str2)); printf( "gateway: client %s sent keep alive on interface %s\n", str2, batman_if->dev ); } @@ -626,12 +646,11 @@ void *gw_listen( void *arg ) {
if ( status < 0 ) {
- if ( debug_level >= 1 ) - printf( "Cannot read message from client %s: %s\n", str2, strerror(errno) ); + do_log( "Error - can't read message: %s\n", strerror(errno) );
} else {
- if ( debug_level >= 1 ) + if ( debug_level == 1 ) printf( "Client %s closed connection ...\n", str2 );
} @@ -657,8 +676,8 @@ void *gw_listen( void *arg ) {
} else if ( ( res < 0 ) && (errno != EINTR) ) {
- fprintf(stderr, "Cannot select: %s\n", strerror(errno)); - return NULL; + do_log( "Error - can't select: %s\n", strerror(errno) ); + break;
}
@@ -679,7 +698,7 @@ void *gw_listen( void *arg ) { FD_CLR(gw_client->sock, &wait_sockets); close( gw_client->sock );
- if ( debug_level >= 1 ) { + if ( debug_level == 1 ) { addr_to_string(gw_client->addr.sin_addr.s_addr, str2, sizeof (str2)); printf( "gateway: client %s timeout on interface %s\n", str2, batman_if->dev ); } @@ -714,7 +733,7 @@ int main(int argc, char *argv[]) struct in_addr tmp_ip_holder; struct batman_if *batman_if; struct ifreq int_req; - int on = 1, res, optchar, found_args = 1; + int on = 1, res, optchar, found_args = 1, pid; char str1[16], str2[16], *dev; unsigned int vis_server = 0;
@@ -722,8 +741,6 @@ int main(int argc, char *argv[]) dev = NULL; memset(&tmp_ip_holder, 0, sizeof (struct in_addr));
- printf( "B.A.T.M.A.N-III v%s (internal version %i)\n", VERSION, BATMAN_VERSION ); - while ( ( optchar = getopt ( argc, argv, "d:hHo:g:p:r:s:" ) ) != -1 ) {
switch ( optchar ) { @@ -844,6 +861,78 @@ int main(int argc, char *argv[])
}
+ if ( ( gateway_class != 0 ) && ( routing_class != 0 ) ) + { + fprintf(stderr, "Error - routing class can't be set while gateway class is in use !\n"); + usage(); + close_all_sockets(); + exit(EXIT_FAILURE); + } + + if ( ( gateway_class != 0 ) && ( pref_gateway != 0 ) ) + { + fprintf(stderr, "Error - preferred gateway can't be set while gateway class is in use !\n"); + usage(); + close_all_sockets(); + exit(EXIT_FAILURE); + } + + if ( ( routing_class == 0 ) && ( pref_gateway != 0 ) ) + { + fprintf(stderr, "Error - preferred gateway can't be set without specifying routing class !\n"); + usage(); + close_all_sockets(); + exit(EXIT_FAILURE); + } + + if ( ( ( routing_class != 0 ) || ( gateway_class != 0 ) ) && ( !probe_tun() ) ) { + close_all_sockets(); + exit( EXIT_FAILURE ); + } + + + /* daemonize */ + if ( debug_level == 0 ) { + + switch( pid = fork() ) { + + case -1: /* error */ + printf( "Error - can't fork to background: %s\n", strerror(errno) ); + close_all_sockets(); + exit(EXIT_FAILURE); + break; + + case 0: /* child process - new daemon */ + openlog( "batmand", LOG_PID, LOG_DAEMON ); + break; + + case 1: /* parent process */ + exit( EXIT_SUCCESS ); + break; + + } + + if ( setsid() < 0 ) { + + do_log( "Error - can't create new session: %s\n", strerror(errno) ); + close_all_sockets(); + exit( EXIT_FAILURE ); + + } + + close( STDIN_FILENO ); + close( STDOUT_FILENO ); + close( STDERR_FILENO ); + + chdir( "/" ); + + umask( 0 ); + + } else if ( debug_level > 0 ) { + + printf( "B.A.T.M.A.N-III v%s (internal version %i)\n", VERSION, BATMAN_VERSION ); + + }
while ( argc > found_args ) {
@@ -858,14 +947,14 @@ int main(int argc, char *argv[]) list_add_tail(&batman_if->list, &if_list);
if ( strlen(batman_if->dev) > IFNAMSIZ - 1 ) { - fprintf(stderr, "Interface name too long: %s\n", batman_if->dev); + do_log( "Error - interface name too long: %s\n", batman_if->dev ); close_all_sockets(); exit(EXIT_FAILURE); }
batman_if->udp_send_sock = socket(PF_INET, SOCK_DGRAM, 0); if (batman_if->udp_send_sock < 0) { - fprintf(stderr, "Cannot create send socket: %s", strerror(errno)); + do_log( "Error - can't create send socket: %s", strerror(errno) ); close_all_sockets(); exit(EXIT_FAILURE); } @@ -873,7 +962,7 @@ int main(int argc, char *argv[]) batman_if->udp_recv_sock = socket(PF_INET, SOCK_DGRAM, 0); if (batman_if->udp_recv_sock < 0) { - fprintf(stderr, "Cannot create recieve socket: %s", strerror(errno)); + do_log( "Error - can't create recieve socket: %s", strerror(errno) ); close_all_sockets(); exit(EXIT_FAILURE); } @@ -883,7 +972,7 @@ int main(int argc, char *argv[])
if (ioctl(batman_if->udp_recv_sock, SIOCGIFADDR, &int_req) < 0) { - fprintf(stderr, "Cannot get IP address of interface %s\n", batman_if->dev); + do_log( "Error - can't get IP address of interface %s\n", batman_if->dev ); close_all_sockets(); exit(EXIT_FAILURE); } @@ -894,7 +983,7 @@ int main(int argc, char *argv[])
if (ioctl(batman_if->udp_recv_sock, SIOCGIFBRDADDR, &int_req) < 0) { - fprintf(stderr, "Cannot get broadcast IP address of interface %s\n", batman_if->dev); + do_log( "Error - can't get broadcast IP address of interface %s\n", batman_if->dev ); close_all_sockets(); exit(EXIT_FAILURE); } @@ -905,14 +994,14 @@ int main(int argc, char *argv[])
if (setsockopt(batman_if->udp_send_sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (int)) < 0) { - fprintf(stderr, "Cannot enable broadcasts: %s\n", strerror(errno)); + do_log( "Error - can't enable broadcasts: %s\n", strerror(errno) ); close_all_sockets(); exit(EXIT_FAILURE); }
if (bind(batman_if->udp_send_sock, (struct sockaddr *)&batman_if->addr, sizeof (struct sockaddr_in)) < 0) { - fprintf(stderr, "Cannot bind send socket: %s\n", strerror(errno)); + do_log( "Error - can't bind send socket: %s\n", strerror(errno) ); close_all_sockets(); exit(EXIT_FAILURE); } @@ -926,7 +1015,7 @@ int main(int argc, char *argv[])
if (bind(batman_if->udp_recv_sock, (struct sockaddr *)&batman_if->broad, sizeof (struct sockaddr_in)) < 0) { - fprintf(stderr, "Cannot bind receive socket: %s\n", strerror(errno)); + do_log( "Error - can't bind receive socket: %s\n", strerror(errno) ); close_all_sockets(); exit(EXIT_FAILURE); } @@ -934,7 +1023,8 @@ int main(int argc, char *argv[]) addr_to_string(batman_if->addr.sin_addr.s_addr, str1, sizeof (str1)); addr_to_string(batman_if->broad.sin_addr.s_addr, str2, sizeof (str2));
- printf("Using interface %s with address %s and broadcast address %s\n", batman_if->dev, str1, str2); + if ( debug_level > 0 ) + printf("Using interface %s with address %s and broadcast address %s\n", batman_if->dev, str1, str2);
if ( gateway_class != 0 ) {
@@ -942,40 +1032,39 @@ int main(int argc, char *argv[])
batman_if->tcp_gw_sock = socket(PF_INET, SOCK_STREAM, 0);
- if (batman_if->tcp_gw_sock < 0) { - fprintf(stderr, "Cannot create socket: %s", strerror(errno)); + if ( batman_if->tcp_gw_sock < 0 ) { + do_log( "Error - can't create socket: %s", strerror(errno) ); close_all_sockets(); exit(EXIT_FAILURE); }
- if (setsockopt(batman_if->tcp_gw_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) < 0) { - printf("Cannot enable reuse of address: %s\n", strerror(errno)); + if ( setsockopt(batman_if->tcp_gw_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) < 0 ) { + do_log( "Error - can't enable reuse of address: %s\n", strerror(errno) ); close_all_sockets(); exit(EXIT_FAILURE); }
- if (bind( batman_if->tcp_gw_sock, (struct sockaddr*)&batman_if->addr, sizeof(struct sockaddr_in)) < 0) { - printf("Cannot bind socket: %s\n",strerror(errno)); + if ( bind( batman_if->tcp_gw_sock, (struct sockaddr*)&batman_if->addr, sizeof(struct sockaddr_in)) < 0 ) { + do_log( "Error - can't bind socket: %s\n", strerror(errno) ); close_all_sockets(); exit(EXIT_FAILURE); }
- if (listen( batman_if->tcp_gw_sock, 10 ) < 0) { - printf("Cannot listen socket: %s\n",strerror(errno)); + if ( listen( batman_if->tcp_gw_sock, 10 ) < 0 ) { + do_log( "Error - can't listen socket: %s\n", strerror(errno) ); close_all_sockets(); exit(EXIT_FAILURE); }
batman_if->tunnel_sock = socket(PF_INET, SOCK_DGRAM, 0); - if (batman_if->tunnel_sock < 0) { - fprintf(stderr, "Cannot create tunnel socket: %s", strerror(errno)); + if ( batman_if->tunnel_sock < 0 ) { + do_log( "Error - can't create tunnel socket: %s", strerror(errno) ); close_all_sockets(); exit(EXIT_FAILURE); }
- if (bind(batman_if->tunnel_sock, (struct sockaddr *)&batman_if->addr, sizeof (struct sockaddr_in)) < 0) - { - fprintf(stderr, "Cannot bind tunnel socket: %s\n", strerror(errno)); + if ( bind(batman_if->tunnel_sock, (struct sockaddr *)&batman_if->addr, sizeof (struct sockaddr_in)) < 0 ) { + do_log( "Error - can't bind tunnel socket: %s\n", strerror(errno) ); close_all_sockets(); exit(EXIT_FAILURE); } @@ -1002,59 +1091,33 @@ int main(int argc, char *argv[]) memset(&vis_if, 0, sizeof(vis_if));
- if (found_ifs == 0) + if ( found_ifs == 0 ) { - fprintf(stderr, "Error - no interface specified\n"); + do_log( "Error - no interface specified\n", strerror(errno) ); usage(); close_all_sockets(); exit(EXIT_FAILURE); }
- if ( ( gateway_class != 0 ) && ( routing_class != 0 ) ) - { - fprintf(stderr, "Error - routing class can't be set while gateway class is in use !\n"); - usage(); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - if ( ( gateway_class != 0 ) && ( pref_gateway != 0 ) ) - { - fprintf(stderr, "Error - preferred gateway can't be set while gateway class is in use !\n"); - usage(); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - if ( ( routing_class == 0 ) && ( pref_gateway != 0 ) ) - { - fprintf(stderr, "Error - preferred gateway can't be set without specifying routing class !\n"); - usage(); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - if ( ( ( routing_class != 0 ) || ( gateway_class != 0 ) ) && ( !probe_tun() ) ) { - close_all_sockets(); - exit(EXIT_FAILURE); - }
+ if ( debug_level > 0 ) {
- if ( debug_level > 0 ) printf("debug level: %i\n", debug_level);
- if ( ( debug_level > 0 ) && ( orginator_interval != 1000 ) ) - printf( "orginator interval: %i\n", orginator_interval ); + if ( orginator_interval != 1000 ) + printf( "orginator interval: %i\n", orginator_interval );
- if ( ( debug_level > 0 ) && ( gateway_class > 0 ) ) - printf( "gateway class: %i\n", gateway_class ); + if ( gateway_class > 0 ) + printf( "gateway class: %i\n", gateway_class );
- if ( ( debug_level > 0 ) && ( routing_class > 0 ) ) - printf( "routing class: %i\n", routing_class ); + if ( routing_class > 0 ) + printf( "routing class: %i\n", routing_class ); + + if ( pref_gateway > 0 ) { + addr_to_string(pref_gateway, str1, sizeof (str1)); + printf( "preferred gateway: %s\n", str1 ); + }
- if ( ( debug_level > 0 ) && ( pref_gateway > 0 ) ) { - addr_to_string(pref_gateway, str1, sizeof (str1)); - printf( "preferred gateway: %s\n", str1 ); }
@@ -1067,6 +1130,7 @@ int main(int argc, char *argv[]) res = batman();
close_all_sockets(); + return res;
}