The following commit has been merged in the master branch:
commit 2a07f5eec70399ea277abd92a8eb7b27e9307f99
Author: Axel Neumann <neumann(a)cgws.de>
Date: Mon Jan 15 23:27:07 2007 +0100
Modified Merge for batman-experimental, moved some functions from linux.c and posix.c to its -specific.c version
diff --git a/Makefile b/Makefile
index 61e51fc..3a6f4e3 100644
--- a/Makefile
+++ b/Makefile
@@ -25,7 +25,7 @@ LDFLAGS = -lpthread
UNAME=$(shell uname)
ifeq ($(UNAME),Linux)
-OS_OBJ= posix-specific.o posix.o linux.o allocate.o
+OS_OBJ= posix-specific.o posix.o linux-specific.o linux.o allocate.o
endif
ifeq ($(UNAME),Darwin)
@@ -40,6 +40,8 @@ ifeq ($(UNAME),OpenBSD)
OS_OBJ= posix-specific.o posix.o bsd.o allocate.o
endif
+all: batmand
+
batmand: batman.o $(OS_OBJ)
$(CC) -o $@ batman.o $(OS_OBJ) $(LDFLAGS)
diff --git a/linux.c b/linux-specific.c
similarity index 79%
copy from linux.c
copy to linux-specific.c
index 2858fab..7eefca4 100644
--- a/linux.c
+++ b/linux-specific.c
@@ -42,97 +42,6 @@
#include "os.h"
#include "batman-specific.h"
-void set_rp_filter(int state, char* dev)
-{
- FILE *f;
- char filename[100], *colon_ptr;
-
- /* if given interface is an alias use parent interface */
- if ( ( colon_ptr = strchr( dev, ':' ) ) != NULL )
- *colon_ptr = '\0';
-
- sprintf( filename, "/proc/sys/net/ipv4/conf/%s/rp_filter", dev);
-
- if((f = fopen(filename, "w")) == NULL)
- return;
-
- fprintf(f, "%d", state);
- fclose(f);
-
- if ( colon_ptr != NULL )
- *colon_ptr = ':';
-}
-
-int get_rp_filter(char *dev)
-{
- FILE *f;
- int state = 0;
- char filename[100], *colon_ptr;
-
- /* if given interface is an alias use parent interface */
- if ( ( colon_ptr = strchr( dev, ':' ) ) != NULL )
- *colon_ptr = '\0';
-
- sprintf( filename, "/proc/sys/net/ipv4/conf/%s/rp_filter", dev);
-
- if((f = fopen(filename, "r")) == NULL)
- return 0;
-
- fscanf(f, "%d", &state);
- fclose(f);
-
- if ( colon_ptr != NULL )
- *colon_ptr = ':';
-
- return state;
-}
-
-void set_forwarding(int state)
-{
- FILE *f;
-
- if((f = fopen("/proc/sys/net/ipv4/ip_forward", "w")) == NULL)
- return;
-
- fprintf(f, "%d", state);
- fclose(f);
-}
-
-int get_forwarding(void)
-{
- FILE *f;
- int state = 0;
-
- if((f = fopen("/proc/sys/net/ipv4/ip_forward", "r")) == NULL)
- return 0;
-
- fscanf(f, "%d", &state);
- fclose(f);
-
- return state;
-}
-
-int bind_to_iface( int udp_recv_sock, char *dev ) {
-
- char *colon_ptr;
-
- /* if given interface is an alias bind to parent interface */
- if ( ( colon_ptr = strchr( dev, ':' ) ) != NULL )
- *colon_ptr = '\0';
-
- if ( setsockopt( udp_recv_sock, SOL_SOCKET, SO_BINDTODEVICE, dev, strlen ( dev ) + 1 ) < 0 ) {
-
- fprintf( stderr, "Cannot bind socket to device %s : %s \n", dev, strerror(errno) );
- return -1;
-
- }
-
- if ( colon_ptr != NULL )
- *colon_ptr = ':';
-
- return 1;
-
-}
void add_del_route( unsigned int dest, unsigned int netmask, unsigned int router, int del, char *dev, int sock ) {
@@ -206,6 +115,8 @@ void add_del_route( unsigned int dest, unsigned int netmask, unsigned int router
}
+
+
/* Probe for tun interface availability */
int probe_tun()
{
@@ -239,6 +150,7 @@ int del_dev_tun( int fd ) {
}
+
int add_dev_tun( struct batman_if *batman_if, unsigned int tun_addr, char *tun_dev, size_t tun_dev_size, int *fd ) {
int tmp_fd;
diff --git a/linux.c b/linux.c
index 2858fab..20735c7 100644
--- a/linux.c
+++ b/linux.c
@@ -133,232 +133,3 @@ int bind_to_iface( int udp_recv_sock, char *dev ) {
return 1;
}
-
-void add_del_route( unsigned int dest, unsigned int netmask, unsigned int router, int del, char *dev, int sock ) {
-
- struct rtentry route;
- char str1[16], str2[16], log_str[100];
- struct sockaddr_in *addr;
-
- inet_ntop(AF_INET, &dest, str1, sizeof (str1));
- inet_ntop(AF_INET, &router, str2, sizeof (str2));
-
- memset(&route, 0, sizeof (struct rtentry));
-
- addr = (struct sockaddr_in *)&route.rt_dst;
-
- addr->sin_family = AF_INET;
- addr->sin_addr.s_addr = dest;
-
- addr = (struct sockaddr_in *)&route.rt_genmask;
-
- addr->sin_family = AF_INET;
- addr->sin_addr.s_addr = ( netmask == 32 ? 0xffffffff : htonl( ~ ( 0xffffffff >> netmask ) ) );
-
- route.rt_flags = ( netmask == 32 ? ( RTF_HOST | RTF_UP ) : RTF_UP );
- route.rt_metric = 1;
-
- if ( (dest != router) || ( ( dest == 0 ) && ( router == 0 ) ) )
- {
- addr = (struct sockaddr_in *)&route.rt_gateway;
-
- addr->sin_family = AF_INET;
- addr->sin_addr.s_addr = router;
-
- if ( ( dest == 0 ) && ( router == 0 ) ) {
-
- route.rt_metric = 0;
-
- if ( debug_level == 3 ) {
- printf("%s default route via %s\n", del ? "Deleting" : "Adding", dev);
- } else if ( debug_level == 4 ) {
- output("%s default route via %s\n", del ? "Deleting" : "Adding", dev);
- }
-
- } else {
-
- route.rt_flags |= RTF_GATEWAY;
-
- if ( debug_level == 3 ) {
- printf("%s route to %s/%i via %s (%s)\n", del ? "Deleting" : "Adding", str1, netmask, str2, dev);
- } else if ( debug_level == 4 ) {
- output("%s route to %s/%i via %s (%s)\n", del ? "Deleting" : "Adding", str1, netmask, str2, dev);
- }
-
- }
-
- } else {
-
- if ( debug_level == 3 ) {
- printf("%s route to %s via 0.0.0.0 (%s)\n", del ? "Deleting" : "Adding", str1, dev);
- } else if ( debug_level == 4 ) {
- 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 ) {
- snprintf( log_str, sizeof( log_str ), "Error - can't %s route to %s/%i via %s: %s\n", del ? "delete" : "add", str1, netmask, str2, strerror(errno) );
- do_log( log_str, strerror(errno) );
- }
-
-}
-
-/* Probe for tun interface availability */
-int probe_tun()
-{
- int fd;
-
- if ( ( fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) {
-
- do_log( "Error - could not open '/dev/net/tun' ! Is the tun kernel module loaded ?\n", strerror(errno) );
- return 0;
-
- }
-
- close( fd );
-
- return 1;
-
-}
-
-int del_dev_tun( int fd ) {
-
- if ( ioctl( fd, TUNSETPERSIST, 0 ) < 0 ) {
-
- do_log( "Error - can't delete tun device: %s\n", strerror(errno) );
- return -1;
-
- }
-
- close( fd );
-
- return 1;
-
-}
-
-int add_dev_tun( struct batman_if *batman_if, unsigned int tun_addr, char *tun_dev, size_t tun_dev_size, int *fd ) {
-
- int tmp_fd;
- struct ifreq ifr_tun, ifr_if;
- struct sockaddr_in addr;
-
- /* set up tunnel device */
- memset( &ifr_tun, 0, sizeof(ifr_tun) );
- memset( &ifr_if, 0, sizeof(ifr_if) );
- ifr_tun.ifr_flags = IFF_TUN | IFF_NO_PI;
-
- if ( ( *fd = open( "/dev/net/tun", O_RDWR ) ) < 0 ) {
-
- 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 ) {
-
- do_log( "Error - can't create tun device (TUNSETIFF): %s\n", strerror(errno) );
- close(*fd);
- return -1;
-
- }
-
- if ( ioctl( *fd, TUNSETPERSIST, 1 ) < 0 ) {
-
- do_log( "Error - can't create tun device (TUNSETPERSIST): %s\n", strerror(errno) );
- close(*fd);
- return -1;
-
- }
-
-
- tmp_fd = socket(AF_INET, SOCK_DGRAM, 0);
-
- if ( tmp_fd < 0 ) {
- do_log( "Error - can't create tun device (udp socket): %s\n", strerror(errno) );
- del_dev_tun( *fd );
- return -1;
- }
-
-
- /* set ip of this end point of tunnel */
- memset( &addr, 0, sizeof(addr) );
- addr.sin_addr.s_addr = tun_addr;
- addr.sin_family = AF_INET;
- memcpy( &ifr_tun.ifr_addr, &addr, sizeof(struct sockaddr) );
-
-
- if ( ioctl( tmp_fd, SIOCSIFADDR, &ifr_tun) < 0 ) {
-
- do_log( "Error - can't create tun device (SIOCSIFADDR): %s\n", strerror(errno) );
- del_dev_tun( *fd );
- close( tmp_fd );
- return -1;
-
- }
-
-
- if ( ioctl( tmp_fd, SIOCGIFFLAGS, &ifr_tun) < 0 ) {
-
- do_log( "Error - can't create tun device (SIOCGIFFLAGS): %s\n", strerror(errno) );
- del_dev_tun( *fd );
- close( tmp_fd );
- return -1;
-
- }
-
- ifr_tun.ifr_flags |= IFF_UP;
- ifr_tun.ifr_flags |= IFF_RUNNING;
-
- if ( ioctl( tmp_fd, SIOCSIFFLAGS, &ifr_tun) < 0 ) {
-
- do_log( "Error - can't create tun device (SIOCSIFFLAGS): %s\n", strerror(errno) );
- del_dev_tun( *fd );
- close( tmp_fd );
- return -1;
-
- }
-
- /* get MTU from real interface */
- strncpy( ifr_if.ifr_name, batman_if->dev, IFNAMSIZ - 1 );
-
- if ( ioctl( tmp_fd, SIOCGIFMTU, &ifr_if ) < 0 ) {
-
- do_log( "Error - can't create tun device (SIOCGIFMTU): %s\n", strerror(errno) );
- del_dev_tun( *fd );
- close( tmp_fd );
- return -1;
-
- }
-
- /* set MTU of tun interface: real MTU - 28 */
- if ( ifr_if.ifr_mtu < 100 ) {
-
- do_log( "Warning - MTU smaller than 100 -> can't reduce MTU anymore\n", strerror(errno) );
-
- } else {
-
- ifr_tun.ifr_mtu = ifr_if.ifr_mtu - 28;
-
- if ( ioctl( tmp_fd, SIOCSIFMTU, &ifr_tun ) < 0 ) {
-
- do_log( "Error - can't create tun device (SIOCSIFMTU): %s\n", strerror(errno) );
- del_dev_tun( *fd );
- close( tmp_fd );
- return -1;
-
- }
-
- }
-
-
- strncpy( tun_dev, ifr_tun.ifr_name, tun_dev_size - 1 );
- close( tmp_fd );
-
- return 1;
-
-}
-
-
diff --git a/os.h b/os.h
index 4b1403f..5f9a79d 100644
--- a/os.h
+++ b/os.h
@@ -17,8 +17,6 @@
*
*/
-
-
#ifndef _BATMAN_OS_H
#define _BATMAN_OS_H
@@ -36,7 +34,9 @@ void output(char *format, ...);
void add_del_route( unsigned int dest, unsigned int netmask, unsigned int router, int del, char *dev, int sock );
int is_aborted();
void addr_to_string(unsigned int addr, char *str, int len);
+
int receive_packet(unsigned char *packet_buff, int packet_buff_len, int *hna_buff_len, unsigned int *neigh, unsigned int timeout, struct batman_if **if_incoming);
+
int send_packet(unsigned char *buff, int len, struct sockaddr_in *broad, int sock);
int rand_num(int limit);
int bind_to_iface( int udp_recv_sock, char *dev );
@@ -50,4 +50,12 @@ int print_animation( void );
void close_all_sockets();
void *gw_listen( void *arg );
+void *client_to_gw_tun( void *arg );
+
+//int is_aborted();
+
+//static void handler(int sig);
+
+
+
#endif
diff --git a/posix-specific.c b/posix-specific.c
index 2361158..7250b4c 100644
--- a/posix-specific.c
+++ b/posix-specific.c
@@ -17,8 +17,6 @@
*
*/
-
-
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
@@ -45,6 +43,7 @@
+
void apply_init_args( int argc, char *argv[] ) {
struct in_addr tmp_ip_holder;
@@ -494,3 +493,632 @@ void apply_init_args( int argc, char *argv[] ) {
}
+
+
+
+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 ) {
+
+ struct gw_node *gw_node = (struct gw_node *)arg;
+ struct batman_if *curr_gateway_batman_if;
+ struct sockaddr_in gw_addr, my_addr, sender_addr;
+ struct timeval tv;
+ int res, max_sock, status, buff_len, curr_gateway_tcp_sock, curr_gateway_tun_sock, curr_gateway_tun_fd, server_keep_alive_timeout;
+ unsigned int addr_len, curr_gateway_ip;
+ char curr_gateway_tun_if[IFNAMSIZ], keep_alive_string[] = "ping\0";
+ unsigned char buff[1500];
+ fd_set wait_sockets, tmp_wait_sockets;
+
+
+ curr_gateway_ip = gw_node->orig_node->orig;
+ curr_gateway_batman_if = gw_node->orig_node->batman_if;
+ addr_len = sizeof (struct sockaddr_in);
+
+ memset( &gw_addr, 0, sizeof(struct sockaddr_in) );
+ memset( &my_addr, 0, sizeof(struct sockaddr_in) );
+
+ gw_addr.sin_family = AF_INET;
+ gw_addr.sin_port = htons(PORT + 1);
+ gw_addr.sin_addr.s_addr = curr_gateway_ip;
+
+ my_addr.sin_family = AF_INET;
+ my_addr.sin_port = htons(PORT + 1);
+ my_addr.sin_addr.s_addr = curr_gateway_batman_if->addr.sin_addr.s_addr;
+
+
+ /* connect to server (ask permission) */
+ if ( ( curr_gateway_tcp_sock = socket(PF_INET, SOCK_STREAM, 0) ) < 0 ) {
+
+ do_log( "Error - can't create tcp socket: %s\n", strerror(errno) );
+ curr_gateway = NULL;
+ return NULL;
+
+ }
+
+ if ( connect ( curr_gateway_tcp_sock, (struct sockaddr *)&gw_addr, sizeof(struct sockaddr) ) < 0 ) {
+
+ do_log( "Error - can't connect to gateway: %s\n", strerror(errno) );
+ close( curr_gateway_tcp_sock );
+
+ gw_node->last_failure = get_time();
+ gw_node->unavail_factor++;
+
+ curr_gateway = NULL;
+ return NULL;
+
+ }
+
+ server_keep_alive_timeout = get_time();
+
+
+ /* connect to server (establish udp tunnel) */
+ if ( ( curr_gateway_tun_sock = socket(PF_INET, SOCK_DGRAM, 0) ) < 0 ) {
+
+ 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) {
+
+ 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;
+
+ }
+
+
+ if ( add_dev_tun( curr_gateway_batman_if, curr_gateway_batman_if->addr.sin_addr.s_addr, curr_gateway_tun_if, sizeof(curr_gateway_tun_if), &curr_gateway_tun_fd ) > 0 ) {
+
+ add_del_route( 0, 0, 0, 0, curr_gateway_tun_if, curr_gateway_batman_if->udp_send_sock );
+
+ } else {
+
+ close( curr_gateway_tcp_sock );
+ close( curr_gateway_tun_sock );
+ curr_gateway = NULL;
+ return NULL;
+
+ }
+
+
+ FD_ZERO(&wait_sockets);
+ FD_SET(curr_gateway_tcp_sock, &wait_sockets);
+ FD_SET(curr_gateway_tun_sock, &wait_sockets);
+ FD_SET(curr_gateway_tun_fd, &wait_sockets);
+
+ max_sock = curr_gateway_tcp_sock;
+ if ( curr_gateway_tun_sock > max_sock )
+ max_sock = curr_gateway_tun_sock;
+ if ( curr_gateway_tun_fd > max_sock )
+ max_sock = curr_gateway_tun_fd;
+
+ while ( ( !is_aborted() ) && ( curr_gateway != NULL ) ) {
+
+
+ if ( server_keep_alive_timeout + 30000 < get_time() ) {
+
+ server_keep_alive_timeout = get_time();
+
+ if ( write( curr_gateway_tcp_sock, keep_alive_string, sizeof( keep_alive_string ) ) < 0 ) {
+
+ if ( debug_level == 3 )
+ printf( "server_keepalive failed: no connect to server\n" );
+
+ gw_node->last_failure = get_time();
+ gw_node->unavail_factor++;
+
+ break;
+
+ }
+
+ }
+
+
+ tv.tv_sec = 0;
+ tv.tv_usec = 250;
+
+ tmp_wait_sockets = wait_sockets;
+
+ res = select(max_sock + 1, &tmp_wait_sockets, NULL, NULL, &tv);
+
+ if ( res > 0 ) {
+
+ /* tcp message from server */
+ if ( FD_ISSET( curr_gateway_tcp_sock, &tmp_wait_sockets ) ) {
+
+ status = read( curr_gateway_tcp_sock, buff, sizeof( buff ) );
+
+ if ( status > 0 ) {
+
+ if ( debug_level == 3 )
+ printf( "server message ?\n" );
+
+ } else if ( status < 0 ) {
+
+ if ( debug_level == 3 )
+ printf( "Cannot read message from gateway: %s\n", strerror(errno) );
+
+ break;
+
+ } else if (status == 0) {
+
+ if ( debug_level == 3 )
+ printf( "Gateway closed connection - timeout ?\n" );
+
+ gw_node->last_failure = get_time();
+ gw_node->unavail_factor++;
+
+ break;
+
+ }
+
+ /* udp message (tunnel data) */
+ } else if ( FD_ISSET( curr_gateway_tun_sock, &tmp_wait_sockets ) ) {
+
+ if ( ( buff_len = recvfrom( curr_gateway_tun_sock, buff, sizeof( buff ), 0, (struct sockaddr *)&sender_addr, &addr_len ) ) < 0 ) {
+
+ do_log( "Error - can't receive packet: %s\n", strerror(errno) );
+
+ } else {
+
+ if ( write( curr_gateway_tun_fd, buff, buff_len ) < 0 ) {
+
+ do_log( "Error - can't write packet: %s\n", strerror(errno) );
+
+ }
+
+ }
+
+ } else if ( FD_ISSET( curr_gateway_tun_fd, &tmp_wait_sockets ) ) {
+
+ if ( ( buff_len = read( curr_gateway_tun_fd, buff, sizeof( buff ) ) ) < 0 ) {
+
+ 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 ) {
+ do_log( "Error - can't send to gateway: %s\n", strerror(errno) );
+ }
+
+ }
+
+ }
+
+ } else if ( ( res < 0 ) && (errno != EINTR) ) {
+
+ do_log( "Error - can't select: %s\n", strerror(errno) );
+ break;
+
+ }
+
+ }
+
+ /* cleanup */
+ add_del_route( 0, 0, 0, 1, curr_gateway_tun_if, curr_gateway_batman_if->udp_send_sock );
+
+ close( curr_gateway_tcp_sock );
+ close( curr_gateway_tun_sock );
+
+ del_dev_tun( curr_gateway_tun_fd );
+
+ curr_gateway = NULL;
+
+ return NULL;
+
+}
+
+
+void del_default_route() {
+
+ curr_gateway = NULL;
+
+ if ( curr_gateway_thread_id != 0 )
+ pthread_join( curr_gateway_thread_id, NULL );
+
+}
+
+
+
+int add_default_route() {
+
+ 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;
+
+}
+
+
+
+void close_all_sockets() {
+
+ struct list_head *if_pos, *if_pos_tmp;
+ struct batman_if *batman_if;
+
+ list_for_each_safe(if_pos, if_pos_tmp, &if_list) {
+
+ batman_if = list_entry(if_pos, struct batman_if, list);
+
+ if ( batman_if->listen_thread_id != 0 ) {
+
+ pthread_join( batman_if->listen_thread_id, NULL );
+ close(batman_if->tcp_gw_sock);
+
+ }
+
+ close(batman_if->udp_recv_sock);
+ close(batman_if->udp_send_sock);
+
+ list_del( if_pos );
+ debugFree( if_pos, 203 );
+
+ }
+
+ if ( ( routing_class != 0 ) && ( curr_gateway != NULL ) )
+ del_default_route();
+
+ if (vis_if.sock)
+ close(vis_if.sock);
+
+ if ( debug_level == 0 )
+ closelog();
+
+}
+
+
+
+int receive_packet( unsigned char *packet_buff, int packet_buff_len, int *hna_buff_len, unsigned int *neigh, unsigned int timeout, struct batman_if **if_incoming ) {
+
+ fd_set wait_set;
+ int res, max_sock = 0;
+ struct sockaddr_in addr;
+ unsigned int addr_len;
+ struct timeval tv;
+ struct list_head *if_pos;
+ struct batman_if *batman_if;
+
+
+ tv.tv_sec = timeout / 1000;
+ tv.tv_usec = (timeout % 1000) * 1000;
+
+
+ FD_ZERO(&wait_set);
+
+ list_for_each(if_pos, &if_list) {
+
+ batman_if = list_entry(if_pos, struct batman_if, list);
+
+ FD_SET(batman_if->udp_recv_sock, &wait_set);
+
+ if ( batman_if->udp_recv_sock > max_sock )
+ max_sock = batman_if->udp_recv_sock;
+
+ }
+
+ for (;;)
+ {
+ res = select( max_sock + 1, &wait_set, NULL, NULL, &tv );
+
+ if (res >= 0)
+ break;
+
+ if ( errno != EINTR ) {
+ do_log( "Error - can't select: %s\n", strerror(errno) );
+ return -1;
+ }
+ }
+
+ if ( res == 0 )
+ return 0;
+
+ addr_len = sizeof (struct sockaddr_in);
+
+ list_for_each(if_pos, &if_list) {
+ batman_if = list_entry(if_pos, struct batman_if, list);
+
+ if ( FD_ISSET( batman_if->udp_recv_sock, &wait_set) ) {
+
+ if ( ( *hna_buff_len = recvfrom( batman_if->udp_recv_sock, packet_buff, packet_buff_len - 1, 0, (struct sockaddr *)&addr, &addr_len ) ) < 0 ) {
+ do_log( "Error - can't receive packet: %s\n", strerror(errno) );
+ return -1;
+ }
+
+ if ( *hna_buff_len < sizeof(struct packet) )
+ return 0;
+
+ packet_buff[*hna_buff_len] = '\0';
+
+ (*if_incoming) = batman_if;
+ break;
+
+ }
+
+ }
+
+ *neigh = addr.sin_addr.s_addr;
+
+ return 1;
+
+}
+
+
+
+int send_packet(unsigned char *buff, int len, struct sockaddr_in *broad, int sock)
+{
+
+ char log_str[200];
+
+ if ( sendto( sock, buff, len, 0, (struct sockaddr *)broad, sizeof (struct sockaddr_in) ) < 0 ) {
+
+ if ( errno == 1 ) {
+
+ snprintf( log_str, sizeof( log_str ), "Error - can't send packet: %s.\nDoes your firewall allow outgoing packets on port %i ?\n", strerror(errno), ntohs(broad->sin_port ) );
+ do_log( log_str, strerror(errno) );
+
+ } else {
+
+ do_log( "Error - can't send packet: %s.\n", strerror(errno) );
+
+ }
+
+ return -1;
+
+ }
+
+ return 0;
+
+}
+
+void *gw_listen( void *arg ) {
+
+ struct batman_if *batman_if = (struct batman_if *)arg;
+ struct gw_client *gw_client;
+ struct list_head *client_pos, *client_pos_tmp;
+ struct timeval tv;
+ struct sockaddr_in addr;
+ struct in_addr tmp_ip_holder;
+ socklen_t sin_size = sizeof(struct sockaddr_in);
+ char gw_addr[16], str2[16], tun_dev[IFNAMSIZ], tun_ip[] = "104.255.255.254\0";
+ int res, status, max_sock_min, max_sock, buff_len, tun_fd;
+ unsigned int addr_len, client_timeout;
+ unsigned char buff[1500];
+ fd_set wait_sockets, tmp_wait_sockets;
+
+
+ addr_to_string(batman_if->addr.sin_addr.s_addr, gw_addr, sizeof (gw_addr));
+ addr_len = sizeof (struct sockaddr_in);
+ client_timeout = get_time();
+
+ if ( inet_pton(AF_INET, tun_ip, &tmp_ip_holder) < 1 ) {
+
+ 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 ) {
+ return NULL;
+ }
+
+
+ FD_ZERO(&wait_sockets);
+ FD_SET(batman_if->tcp_gw_sock, &wait_sockets);
+ FD_SET(batman_if->tunnel_sock, &wait_sockets);
+ FD_SET(tun_fd, &wait_sockets);
+
+ max_sock_min = batman_if->tcp_gw_sock;
+ if ( batman_if->tunnel_sock > max_sock_min )
+ max_sock_min = batman_if->tunnel_sock;
+ if ( tun_fd > max_sock_min )
+ max_sock_min = tun_fd;
+
+ max_sock = max_sock_min;
+
+ while (!is_aborted()) {
+
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+ tmp_wait_sockets = wait_sockets;
+
+ res = select(max_sock + 1, &tmp_wait_sockets, NULL, NULL, &tv);
+
+ if (res > 0) {
+
+ /* new client */
+ if ( FD_ISSET( batman_if->tcp_gw_sock, &tmp_wait_sockets ) ) {
+
+ gw_client = debugMalloc( sizeof(struct gw_client), 18 );
+ 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 ) {
+ do_log( "Error - can't accept client packet: %s\n", strerror(errno) );
+ continue;
+ }
+
+ INIT_LIST_HEAD(&gw_client->list);
+ gw_client->batman_if = batman_if;
+ gw_client->last_keep_alive = get_time();
+
+ FD_SET(gw_client->sock, &wait_sockets);
+ if ( gw_client->sock > max_sock )
+ max_sock = gw_client->sock;
+
+ list_add_tail(&gw_client->list, &batman_if->client_list);
+
+ if ( debug_level == 3 ) {
+ 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 );
+ }
+
+ /* tunnel activity */
+ } else if ( FD_ISSET( batman_if->tunnel_sock, &tmp_wait_sockets ) ) {
+
+ if ( ( buff_len = recvfrom( batman_if->tunnel_sock, buff, sizeof( buff ), 0, (struct sockaddr *)&addr, &addr_len ) ) < 0 ) {
+
+ do_log( "Error - can't receive packet: %s\n", strerror(errno) );
+
+ } else {
+
+ if ( write( tun_fd, buff, buff_len ) < 0 ) {
+
+ do_log( "Error - can't write packet: %s\n", strerror(errno) );
+
+ }
+
+ }
+
+ /* /dev/tunX activity */
+ } else if ( FD_ISSET( tun_fd, &tmp_wait_sockets ) ) {
+
+ /* not needed - kernel knows client adress and routes traffic directly */
+
+ do_log( "Warning - data coming through tun device: %s\n", tun_dev );
+
+ /*if ( ( buff_len = read( tun_fd, buff, sizeof( buff ) ) ) < 0 ) {
+
+ fprintf(stderr, "Could not read data from %s: %s\n", tun_dev, 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 client: %s\n", strerror(errno));
+ }
+
+ }*/
+
+ /* client sent keep alive */
+ } else {
+
+ max_sock = max_sock_min;
+
+ list_for_each_safe(client_pos, client_pos_tmp, &batman_if->client_list) {
+
+ gw_client = list_entry(client_pos, struct gw_client, list);
+
+ if ( FD_ISSET( gw_client->sock, &tmp_wait_sockets ) ) {
+
+ if ( debug_level >= 1 )
+ addr_to_string(gw_client->addr.sin_addr.s_addr, str2, sizeof (str2));
+
+ status = read( gw_client->sock, buff, sizeof( buff ) );
+
+ if ( status > 0 ) {
+
+ gw_client->last_keep_alive = get_time();
+
+ if ( gw_client->sock > max_sock )
+ max_sock = gw_client->sock;
+
+ if ( debug_level == 3 ) {
+ 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 );
+ }
+
+ } else {
+
+ if ( status < 0 ) {
+
+ do_log( "Error - can't read message: %s\n", strerror(errno) );
+
+ } else {
+
+ if ( debug_level == 3 )
+ printf( "Client %s closed connection ...\n", str2 );
+
+ }
+
+ FD_CLR(gw_client->sock, &wait_sockets);
+ close( gw_client->sock );
+
+ list_del( client_pos );
+ debugFree( client_pos, 201 );
+
+ }
+
+ } else {
+
+ if ( gw_client->sock > max_sock )
+ max_sock = gw_client->sock;
+
+ }
+
+ }
+
+ }
+
+ } else if ( ( res < 0 ) && (errno != EINTR) ) {
+
+ do_log( "Error - can't select: %s\n", strerror(errno) );
+ break;
+
+ }
+
+
+ /* close unresponsive client connections */
+ if ( ( client_timeout + 59000 ) < get_time() ) {
+
+ client_timeout = get_time();
+
+ max_sock = max_sock_min;
+
+ list_for_each_safe(client_pos, client_pos_tmp, &batman_if->client_list) {
+
+ gw_client = list_entry(client_pos, struct gw_client, list);
+
+ if ( ( gw_client->last_keep_alive + 120000 ) < client_timeout ) {
+
+ FD_CLR(gw_client->sock, &wait_sockets);
+ close( gw_client->sock );
+
+ if ( debug_level == 3 ) {
+ 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 );
+ }
+
+ list_del( client_pos );
+ debugFree( client_pos, 202 );
+
+ } else {
+
+ if ( gw_client->sock > max_sock )
+ max_sock = gw_client->sock;
+
+ }
+
+ }
+
+ }
+
+ }
+
+ /* delete tun devices on exit */
+ del_dev_tun( tun_fd );
+
+ return NULL;
+
+}
+
diff --git a/posix.c b/posix.c
index 798911b..c78a8c3 100644
--- a/posix.c
+++ b/posix.c
@@ -41,6 +41,9 @@
#include "list.h"
#include "allocate.h"
+
+
+
#define BAT_LOGO_PRINT(x,y,z) printf( "\x1B[%i;%iH%c", y + 1, x, z ) /* write char 'z' into column 'x', row 'y' */
#define BAT_LOGO_END(x,y) printf("\x1B[8;0H");fflush(NULL);bat_wait( x, y ); /* end of current picture */
@@ -92,22 +95,6 @@ void output(char *format, ...)
-void do_log( char *description, char *error_msg ) {
-
- if ( debug_level == 0 ) {
-
- syslog( LOG_ERR, description, error_msg );
-
- } else {
-
- printf( description, error_msg );
-
- }
-
-}
-
-
-
/* batman animation */
void sym_print( char x, char y, char *z ) {
@@ -239,284 +226,6 @@ int print_animation( void ) {
-void *client_to_gw_tun( void *arg ) {
-
- struct gw_node *gw_node = (struct gw_node *)arg;
- struct batman_if *curr_gateway_batman_if;
- struct sockaddr_in gw_addr, my_addr, sender_addr;
- struct timeval tv;
- int res, max_sock, status, buff_len, curr_gateway_tcp_sock, curr_gateway_tun_sock, curr_gateway_tun_fd, server_keep_alive_timeout;
- unsigned int addr_len, curr_gateway_ip;
- char curr_gateway_tun_if[IFNAMSIZ], keep_alive_string[] = "ping\0";
- unsigned char buff[1500];
- fd_set wait_sockets, tmp_wait_sockets;
-
-
- curr_gateway_ip = gw_node->orig_node->orig;
- curr_gateway_batman_if = gw_node->orig_node->batman_if;
- addr_len = sizeof (struct sockaddr_in);
-
- memset( &gw_addr, 0, sizeof(struct sockaddr_in) );
- memset( &my_addr, 0, sizeof(struct sockaddr_in) );
-
- gw_addr.sin_family = AF_INET;
- gw_addr.sin_port = htons(PORT + 1);
- gw_addr.sin_addr.s_addr = curr_gateway_ip;
-
- my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(PORT + 1);
- my_addr.sin_addr.s_addr = curr_gateway_batman_if->addr.sin_addr.s_addr;
-
-
- /* connect to server (ask permission) */
- if ( ( curr_gateway_tcp_sock = socket(PF_INET, SOCK_STREAM, 0) ) < 0 ) {
-
- do_log( "Error - can't create tcp socket: %s\n", strerror(errno) );
- curr_gateway = NULL;
- return NULL;
-
- }
-
- if ( connect ( curr_gateway_tcp_sock, (struct sockaddr *)&gw_addr, sizeof(struct sockaddr) ) < 0 ) {
-
- do_log( "Error - can't connect to gateway: %s\n", strerror(errno) );
- close( curr_gateway_tcp_sock );
-
- gw_node->last_failure = get_time();
- gw_node->unavail_factor++;
-
- curr_gateway = NULL;
- return NULL;
-
- }
-
- server_keep_alive_timeout = get_time();
-
-
- /* connect to server (establish udp tunnel) */
- if ( ( curr_gateway_tun_sock = socket(PF_INET, SOCK_DGRAM, 0) ) < 0 ) {
-
- 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) {
-
- 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;
-
- }
-
-
- if ( add_dev_tun( curr_gateway_batman_if, curr_gateway_batman_if->addr.sin_addr.s_addr, curr_gateway_tun_if, sizeof(curr_gateway_tun_if), &curr_gateway_tun_fd ) > 0 ) {
-
- add_del_route( 0, 0, 0, 0, curr_gateway_tun_if, curr_gateway_batman_if->udp_send_sock );
-
- } else {
-
- close( curr_gateway_tcp_sock );
- close( curr_gateway_tun_sock );
- curr_gateway = NULL;
- return NULL;
-
- }
-
-
- FD_ZERO(&wait_sockets);
- FD_SET(curr_gateway_tcp_sock, &wait_sockets);
- FD_SET(curr_gateway_tun_sock, &wait_sockets);
- FD_SET(curr_gateway_tun_fd, &wait_sockets);
-
- max_sock = curr_gateway_tcp_sock;
- if ( curr_gateway_tun_sock > max_sock )
- max_sock = curr_gateway_tun_sock;
- if ( curr_gateway_tun_fd > max_sock )
- max_sock = curr_gateway_tun_fd;
-
- while ( ( !is_aborted() ) && ( curr_gateway != NULL ) ) {
-
-
- if ( server_keep_alive_timeout + 30000 < get_time() ) {
-
- server_keep_alive_timeout = get_time();
-
- if ( write( curr_gateway_tcp_sock, keep_alive_string, sizeof( keep_alive_string ) ) < 0 ) {
-
- if ( debug_level == 3 )
- printf( "server_keepalive failed: no connect to server\n" );
-
- gw_node->last_failure = get_time();
- gw_node->unavail_factor++;
-
- break;
-
- }
-
- }
-
-
- tv.tv_sec = 0;
- tv.tv_usec = 250;
-
- tmp_wait_sockets = wait_sockets;
-
- res = select(max_sock + 1, &tmp_wait_sockets, NULL, NULL, &tv);
-
- if ( res > 0 ) {
-
- /* tcp message from server */
- if ( FD_ISSET( curr_gateway_tcp_sock, &tmp_wait_sockets ) ) {
-
- status = read( curr_gateway_tcp_sock, buff, sizeof( buff ) );
-
- if ( status > 0 ) {
-
- if ( debug_level == 3 )
- printf( "server message ?\n" );
-
- } else if ( status < 0 ) {
-
- if ( debug_level == 3 )
- printf( "Cannot read message from gateway: %s\n", strerror(errno) );
-
- break;
-
- } else if (status == 0) {
-
- if ( debug_level == 3 )
- printf( "Gateway closed connection - timeout ?\n" );
-
- gw_node->last_failure = get_time();
- gw_node->unavail_factor++;
-
- break;
-
- }
-
- /* udp message (tunnel data) */
- } else if ( FD_ISSET( curr_gateway_tun_sock, &tmp_wait_sockets ) ) {
-
- if ( ( buff_len = recvfrom( curr_gateway_tun_sock, buff, sizeof( buff ), 0, (struct sockaddr *)&sender_addr, &addr_len ) ) < 0 ) {
-
- do_log( "Error - can't receive packet: %s\n", strerror(errno) );
-
- } else {
-
- if ( write( curr_gateway_tun_fd, buff, buff_len ) < 0 ) {
-
- do_log( "Error - can't write packet: %s\n", strerror(errno) );
-
- }
-
- }
-
- } else if ( FD_ISSET( curr_gateway_tun_fd, &tmp_wait_sockets ) ) {
-
- if ( ( buff_len = read( curr_gateway_tun_fd, buff, sizeof( buff ) ) ) < 0 ) {
-
- 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 ) {
- do_log( "Error - can't send to gateway: %s\n", strerror(errno) );
- }
-
- }
-
- }
-
- } else if ( ( res < 0 ) && (errno != EINTR) ) {
-
- do_log( "Error - can't select: %s\n", strerror(errno) );
- break;
-
- }
-
- }
-
- /* cleanup */
- add_del_route( 0, 0, 0, 1, curr_gateway_tun_if, curr_gateway_batman_if->udp_send_sock );
-
- close( curr_gateway_tcp_sock );
- close( curr_gateway_tun_sock );
-
- del_dev_tun( curr_gateway_tun_fd );
-
- curr_gateway = NULL;
-
- return NULL;
-
-}
-
-void del_default_route() {
-
- curr_gateway = NULL;
-
- if ( curr_gateway_thread_id != 0 )
- pthread_join( curr_gateway_thread_id, NULL );
-
-}
-
-
-
-int add_default_route() {
-
- 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;
-
-}
-
-
-
-
-void close_all_sockets() {
-
- struct list_head *if_pos, *if_pos_tmp;
- struct batman_if *batman_if;
-
- list_for_each_safe(if_pos, if_pos_tmp, &if_list) {
-
- batman_if = list_entry(if_pos, struct batman_if, list);
-
- if ( batman_if->listen_thread_id != 0 ) {
-
- pthread_join( batman_if->listen_thread_id, NULL );
- close(batman_if->tcp_gw_sock);
-
- }
-
- close(batman_if->udp_recv_sock);
- close(batman_if->udp_send_sock);
-
- list_del( if_pos );
- debugFree( if_pos, 203 );
-
- }
-
- if ( ( routing_class != 0 ) && ( curr_gateway != NULL ) )
- del_default_route();
-
- if (vis_if.sock)
- close(vis_if.sock);
-
- if ( debug_level == 0 )
- closelog();
-
-}
-
int is_aborted()
{
return stop != 0;
@@ -532,349 +241,21 @@ int rand_num(int limit)
return ( limit == 0 ? 0 : rand() % limit );
}
-int receive_packet( unsigned char *packet_buff, int packet_buff_len, int *hna_buff_len, unsigned int *neigh, unsigned int timeout, struct batman_if **if_incoming ) {
-
- fd_set wait_set;
- int res, max_sock = 0;
- struct sockaddr_in addr;
- unsigned int addr_len;
- struct timeval tv;
- struct list_head *if_pos;
- struct batman_if *batman_if;
-
-
- tv.tv_sec = timeout / 1000;
- tv.tv_usec = (timeout % 1000) * 1000;
-
-
- FD_ZERO(&wait_set);
-
- list_for_each(if_pos, &if_list) {
-
- batman_if = list_entry(if_pos, struct batman_if, list);
-
- FD_SET(batman_if->udp_recv_sock, &wait_set);
-
- if ( batman_if->udp_recv_sock > max_sock )
- max_sock = batman_if->udp_recv_sock;
-
- }
-
- for (;;)
- {
- res = select( max_sock + 1, &wait_set, NULL, NULL, &tv );
-
- if (res >= 0)
- break;
-
- if ( errno != EINTR ) {
- do_log( "Error - can't select: %s\n", strerror(errno) );
- return -1;
- }
- }
-
- if ( res == 0 )
- return 0;
-
- addr_len = sizeof (struct sockaddr_in);
-
- list_for_each(if_pos, &if_list) {
- batman_if = list_entry(if_pos, struct batman_if, list);
-
- if ( FD_ISSET( batman_if->udp_recv_sock, &wait_set) ) {
-
- if ( ( *hna_buff_len = recvfrom( batman_if->udp_recv_sock, packet_buff, packet_buff_len - 1, 0, (struct sockaddr *)&addr, &addr_len ) ) < 0 ) {
- do_log( "Error - can't receive packet: %s\n", strerror(errno) );
- return -1;
- }
-
- if ( *hna_buff_len < sizeof(struct packet) )
- return 0;
-
- packet_buff[*hna_buff_len] = '\0';
-
- (*if_incoming) = batman_if;
- break;
-
- }
-
- }
-
- *neigh = addr.sin_addr.s_addr;
-
- return 1;
-
-}
-
-int send_packet(unsigned char *buff, int len, struct sockaddr_in *broad, int sock)
-{
-
- char log_str[200];
-
- if ( sendto( sock, buff, len, 0, (struct sockaddr *)broad, sizeof (struct sockaddr_in) ) < 0 ) {
-
- if ( errno == 1 ) {
-
- snprintf( log_str, sizeof( log_str ), "Error - can't send packet: %s.\nDoes your firewall allow outgoing packets on port %i ?\n", strerror(errno), ntohs(broad->sin_port ) );
- do_log( log_str, strerror(errno) );
-
- } else {
-
- do_log( "Error - can't send packet: %s.\n", strerror(errno) );
-
- }
-
- return -1;
-
- }
-
- return 0;
-
-}
static void handler(int sig)
{
stop = 1;
}
-void *gw_listen( void *arg ) {
-
- struct batman_if *batman_if = (struct batman_if *)arg;
- struct gw_client *gw_client;
- struct list_head *client_pos, *client_pos_tmp;
- struct timeval tv;
- struct sockaddr_in addr;
- struct in_addr tmp_ip_holder;
- socklen_t sin_size = sizeof(struct sockaddr_in);
- char gw_addr[16], str2[16], tun_dev[IFNAMSIZ], tun_ip[] = "104.255.255.254\0";
- int res, status, max_sock_min, max_sock, buff_len, tun_fd;
- unsigned int addr_len, client_timeout;
- unsigned char buff[1500];
- fd_set wait_sockets, tmp_wait_sockets;
-
-
- addr_to_string(batman_if->addr.sin_addr.s_addr, gw_addr, sizeof (gw_addr));
- addr_len = sizeof (struct sockaddr_in);
- client_timeout = get_time();
-
- if ( inet_pton(AF_INET, tun_ip, &tmp_ip_holder) < 1 ) {
-
- 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 ) {
- return NULL;
- }
-
-
- FD_ZERO(&wait_sockets);
- FD_SET(batman_if->tcp_gw_sock, &wait_sockets);
- FD_SET(batman_if->tunnel_sock, &wait_sockets);
- FD_SET(tun_fd, &wait_sockets);
-
- max_sock_min = batman_if->tcp_gw_sock;
- if ( batman_if->tunnel_sock > max_sock_min )
- max_sock_min = batman_if->tunnel_sock;
- if ( tun_fd > max_sock_min )
- max_sock_min = tun_fd;
-
- max_sock = max_sock_min;
-
- while (!is_aborted()) {
-
- tv.tv_sec = 1;
- tv.tv_usec = 0;
- tmp_wait_sockets = wait_sockets;
-
- res = select(max_sock + 1, &tmp_wait_sockets, NULL, NULL, &tv);
-
- if (res > 0) {
-
- /* new client */
- if ( FD_ISSET( batman_if->tcp_gw_sock, &tmp_wait_sockets ) ) {
-
- gw_client = debugMalloc( sizeof(struct gw_client), 18 );
- 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 ) {
- do_log( "Error - can't accept client packet: %s\n", strerror(errno) );
- continue;
- }
-
- INIT_LIST_HEAD(&gw_client->list);
- gw_client->batman_if = batman_if;
- gw_client->last_keep_alive = get_time();
-
- FD_SET(gw_client->sock, &wait_sockets);
- if ( gw_client->sock > max_sock )
- max_sock = gw_client->sock;
-
- list_add_tail(&gw_client->list, &batman_if->client_list);
-
- if ( debug_level == 3 ) {
- 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 );
- }
-
- /* tunnel activity */
- } else if ( FD_ISSET( batman_if->tunnel_sock, &tmp_wait_sockets ) ) {
-
- if ( ( buff_len = recvfrom( batman_if->tunnel_sock, buff, sizeof( buff ), 0, (struct sockaddr *)&addr, &addr_len ) ) < 0 ) {
-
- do_log( "Error - can't receive packet: %s\n", strerror(errno) );
-
- } else {
-
- if ( write( tun_fd, buff, buff_len ) < 0 ) {
-
- do_log( "Error - can't write packet: %s\n", strerror(errno) );
-
- }
-
- }
-
- /* /dev/tunX activity */
- } else if ( FD_ISSET( tun_fd, &tmp_wait_sockets ) ) {
-
- /* not needed - kernel knows client adress and routes traffic directly */
-
- do_log( "Warning - data coming through tun device: %s\n", tun_dev );
-
- /*if ( ( buff_len = read( tun_fd, buff, sizeof( buff ) ) ) < 0 ) {
-
- fprintf(stderr, "Could not read data from %s: %s\n", tun_dev, 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 client: %s\n", strerror(errno));
- }
-
- }*/
-
- /* client sent keep alive */
- } else {
-
- max_sock = max_sock_min;
-
- list_for_each_safe(client_pos, client_pos_tmp, &batman_if->client_list) {
-
- gw_client = list_entry(client_pos, struct gw_client, list);
-
- if ( FD_ISSET( gw_client->sock, &tmp_wait_sockets ) ) {
-
- if ( debug_level >= 1 )
- addr_to_string(gw_client->addr.sin_addr.s_addr, str2, sizeof (str2));
-
- status = read( gw_client->sock, buff, sizeof( buff ) );
-
- if ( status > 0 ) {
-
- gw_client->last_keep_alive = get_time();
-
- if ( gw_client->sock > max_sock )
- max_sock = gw_client->sock;
-
- if ( debug_level == 3 ) {
- 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 );
- }
-
- } else {
-
- if ( status < 0 ) {
-
- do_log( "Error - can't read message: %s\n", strerror(errno) );
-
- } else {
-
- if ( debug_level == 3 )
- printf( "Client %s closed connection ...\n", str2 );
-
- }
-
- FD_CLR(gw_client->sock, &wait_sockets);
- close( gw_client->sock );
-
- list_del( client_pos );
- debugFree( client_pos, 201 );
-
- }
-
- } else {
-
- if ( gw_client->sock > max_sock )
- max_sock = gw_client->sock;
-
- }
-
- }
-
- }
-
- } else if ( ( res < 0 ) && (errno != EINTR) ) {
-
- do_log( "Error - can't select: %s\n", strerror(errno) );
- break;
-
- }
-
-
- /* close unresponsive client connections */
- if ( ( client_timeout + 59000 ) < get_time() ) {
-
- client_timeout = get_time();
-
- max_sock = max_sock_min;
-
- list_for_each_safe(client_pos, client_pos_tmp, &batman_if->client_list) {
-
- gw_client = list_entry(client_pos, struct gw_client, list);
-
- if ( ( gw_client->last_keep_alive + 120000 ) < client_timeout ) {
-
- FD_CLR(gw_client->sock, &wait_sockets);
- close( gw_client->sock );
-
- if ( debug_level == 3 ) {
- 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 );
- }
-
- list_del( client_pos );
- debugFree( client_pos, 202 );
-
- } else {
-
- if ( gw_client->sock > max_sock )
- max_sock = gw_client->sock;
-
- }
-
- }
-
- }
-
- }
-
- /* delete tun devices on exit */
- del_dev_tun( tun_fd );
-
- return NULL;
-
-}
-
-
int main( int argc, char *argv[] ) {
short res;
-
-
stop = 0;
+// memset( &device_node_array[0], 0, sizeof( device_node_array ) );
+// printf("B.A.T.M.A.N-experimental %s\n", VERSION);
+
apply_init_args( argc, argv );
@@ -882,15 +263,18 @@ int main( int argc, char *argv[] ) {
signal(SIGTERM, handler);
gettimeofday(&start_time, NULL);
-
srand(getpid());
+// forward_old = get_forwarding();
+// set_forwarding(1);
+
res = batman();
- close_all_sockets();
+// delAllRoutes();
+// set_forwarding(forward_old);
+ close_all_sockets();
checkLeak();
-
return res;
}
--
batman; test conversation