The following commit has been merged in the master branch: commit 8f37059085830a6245733563774ebe49ce325d0e Author: Marek Lindner lindner_marek@yahoo.de Date: Mon Jan 15 14:35:15 2007 +0100
merge batman && batman-experimental continued
diff --git a/Makefile b/Makefile index afb8340..61e51fc 100644 --- a/Makefile +++ b/Makefile @@ -25,19 +25,19 @@ LDFLAGS = -lpthread UNAME=$(shell uname)
ifeq ($(UNAME),Linux) -OS_OBJ= posix.o linux.o allocate.o +OS_OBJ= posix-specific.o posix.o linux.o allocate.o endif
ifeq ($(UNAME),Darwin) -OS_OBJ= posix.o bsd.o allocate.o +OS_OBJ= posix-specific.o posix.o bsd.o allocate.o endif
ifeq ($(UNAME),FreeBSD) -OS_OBJ= posix.o bsd.o allocate.o +OS_OBJ= posix-specific.o posix.o bsd.o allocate.o endif
ifeq ($(UNAME),OpenBSD) -OS_OBJ= posix.o bsd.o allocate.o +OS_OBJ= posix-specific.o posix.o bsd.o allocate.o endif
batmand: batman.o $(OS_OBJ) diff --git a/batman.c b/batman.c index c8376cb..0de4f58 100644 --- a/batman.c +++ b/batman.c @@ -1455,7 +1455,8 @@ int batman()
}
- debugFree( hna_buff, 115 ); + if ( hna_buff != NULL ) + debugFree( hna_buff, 115 );
list_for_each_safe( forw_pos, forw_pos_tmp, &forw_list ) { diff --git a/os.h b/os.h index 1c79aa3..4b1403f 100644 --- a/os.h +++ b/os.h @@ -45,4 +45,9 @@ 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 );
+void apply_init_args( int argc, char *argv[] ); +int print_animation( void ); +void close_all_sockets(); +void *gw_listen( void *arg ); + #endif diff --git a/posix.c b/posix.c index aa844aa..798911b 100644 --- a/posix.c +++ b/posix.c @@ -868,454 +868,14 @@ void *gw_listen( void *arg ) {
-int main(int argc, char *argv[]) -{ - struct in_addr tmp_ip_holder; - struct batman_if *batman_if; - struct hna_node *hna_node; - struct ifreq int_req; - short on = 1, res, found_args = 1, netmask; - int optchar; - char str1[16], str2[16], *dev, *slash_ptr; - unsigned int vis_server = 0; - - stop = 0; - dev = NULL; - memset(&tmp_ip_holder, 0, sizeof (struct in_addr)); - - printf( "WARNING: You are using the unstable batman branch. If you are interested in *using* batman get the latest stable release !\n" ); - - while ( ( optchar = getopt ( argc, argv, "a:d:hHo:g:p:r:s:vV" ) ) != -1 ) { - - switch ( optchar ) { - - case 'a': - - if ( ( slash_ptr = strchr( optarg, '/' ) ) == NULL ) { - - printf( "Invalid announced network (netmask is missing): %s\n", optarg ); - exit(EXIT_FAILURE); - - } - - *slash_ptr = '\0'; - - if ( inet_pton(AF_INET, optarg, &tmp_ip_holder) < 1 ) { - - *slash_ptr = '/'; - printf( "Invalid announced network (IP is invalid): %s\n", optarg ); - exit(EXIT_FAILURE); - - } - - errno = 0; - netmask = strtol(slash_ptr + 1, NULL , 10); - - if ( ( errno == ERANGE ) || ( errno != 0 && netmask == 0 ) ) { - perror("strtol"); - exit(EXIT_FAILURE); - } - - if ( netmask < 0 || netmask > 32 ) { - - *slash_ptr = '/'; - printf( "Invalid announced network (netmask is invalid): %s\n", optarg ); - exit(EXIT_FAILURE); - - } - - hna_node = debugMalloc(sizeof(struct hna_node), 16); - memset(hna_node, 0, sizeof(struct hna_node)); - INIT_LIST_HEAD(&hna_node->list); - - hna_node->addr = tmp_ip_holder.s_addr; - hna_node->netmask = netmask; - - list_add_tail(&hna_node->list, &hna_list); - - *slash_ptr = '/'; - found_args += 2; - break; - - case 'd': - - errno = 0; - debug_level = strtol (optarg, NULL , 10); - - if ( ( errno == ERANGE ) || ( errno != 0 && debug_level == 0 ) ) { - perror("strtol"); - exit(EXIT_FAILURE); - } - - if ( debug_level < 0 || debug_level > 4 ) { - printf( "Invalid debug level: %i\nDebug level has to be between 0 and 4.\n", debug_level ); - exit(EXIT_FAILURE); - } - - found_args += 2; - break; - - case 'g': - - errno = 0; - gateway_class = strtol(optarg, NULL , 10); - - if ( ( errno == ERANGE ) || ( errno != 0 && gateway_class == 0 ) ) { - perror("strtol"); - exit(EXIT_FAILURE); - } - - if ( gateway_class < 0 || gateway_class > 11 ) { - printf( "Invalid gateway class specified: %i.\nThe class is a value between 0 and 11.\n", gateway_class ); - exit(EXIT_FAILURE); - } - - found_args += 2; - break; - - case 'H': - verbose_usage(); - return (0); - - case 'o': - - errno = 0; - orginator_interval = strtol (optarg, NULL , 10); - - if ( (errno == ERANGE && (orginator_interval == LONG_MAX || orginator_interval == LONG_MIN) ) || (errno != 0 && orginator_interval == 0) ) { - perror("strtol"); - exit(EXIT_FAILURE); - } - - if (orginator_interval < 1) { - printf( "Invalid orginator interval specified: %i.\nThe Interval has to be greater than 0.\n", orginator_interval ); - exit(EXIT_FAILURE); - } - - found_args += 2; - break; - - case 'p': - - errno = 0; - if ( inet_pton(AF_INET, optarg, &tmp_ip_holder) < 1 ) { - - printf( "Invalid preferred gateway IP specified: %s\n", optarg ); - exit(EXIT_FAILURE); - - } - - pref_gateway = tmp_ip_holder.s_addr; - - found_args += 2; - break; - - case 'r': - - errno = 0; - routing_class = strtol (optarg, NULL , 10); - - if ( ( errno == ERANGE ) || ( errno != 0 && routing_class == 0 ) ) { - perror("strtol"); - exit(EXIT_FAILURE); - } - - if (routing_class < 0 || routing_class > 3) { - printf( "Invalid routing class specified: %i.\nThe class is a value between 0 and 3.\n", routing_class ); - exit(EXIT_FAILURE); - } - - found_args += 2; - break; - - case 's': - - errno = 0; - if ( inet_pton(AF_INET, optarg, &tmp_ip_holder) < 1 ) { - - printf( "Invalid preferred visualation server IP specified: %s\n", optarg ); - exit(EXIT_FAILURE); - - } - - vis_server = tmp_ip_holder.s_addr; - - - found_args += 2; - break; - - case 'v': - - printf( "B.A.T.M.A.N.-III v%s (compability version %i)\n", SOURCE_VERSION, COMPAT_VERSION ); - return (0); - - case 'V': - - print_animation(); - - printf( "\x1B[0;0HB.A.T.M.A.N.-III v%s (compability version %i)\n", SOURCE_VERSION, COMPAT_VERSION ); - printf( "\x1B[9;0H \t May the bat guide your path ...\n\n\n" ); - - return (0); - - case 'h': - default: - usage(); - return (0); - - } - - } - - 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( 1 ); - } - - if ( argc <= found_args ) { - fprintf( stderr, "Error - no interface specified\n" ); - usage(); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - - /* daemonize */ - if ( debug_level == 0 ) { - - if ( daemon( 0, 0 ) < 0 ) { - - printf( "Error - can't fork to background: %s\n", strerror(errno) ); - close_all_sockets(); - exit(EXIT_FAILURE); - - } - - openlog( "batmand", LOG_PID, LOG_DAEMON ); - - } else { - - printf( "B.A.T.M.A.N.-III v%s (compability version %i)\n", SOURCE_VERSION, COMPAT_VERSION ); - - } - - while ( argc > found_args ) { - - batman_if = debugMalloc(sizeof(struct batman_if), 17); - memset(batman_if, 0, sizeof(struct batman_if)); - INIT_LIST_HEAD(&batman_if->list); - INIT_LIST_HEAD(&batman_if->client_list); - - batman_if->dev = argv[found_args]; - batman_if->if_num = found_ifs; - - list_add_tail(&batman_if->list, &if_list); - - if ( strlen(batman_if->dev) > IFNAMSIZ - 1 ) { - 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) { - do_log( "Error - can't create send socket: %s", strerror(errno) ); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - batman_if->udp_recv_sock = socket(PF_INET, SOCK_DGRAM, 0); - if ( batman_if->udp_recv_sock < 0 ) { - - do_log( "Error - can't create recieve socket: %s", strerror(errno) ); - close_all_sockets(); - exit(EXIT_FAILURE); - - } - - memset(&int_req, 0, sizeof (struct ifreq)); - strncpy(int_req.ifr_name, batman_if->dev, IFNAMSIZ - 1); - - if ( ioctl( batman_if->udp_recv_sock, SIOCGIFADDR, &int_req ) < 0 ) { - - do_log( "Error - can't get IP address of interface %s\n", batman_if->dev ); - close_all_sockets(); - exit(EXIT_FAILURE); - - } - - batman_if->addr.sin_family = AF_INET; - batman_if->addr.sin_port = htons(PORT); - batman_if->addr.sin_addr.s_addr = ((struct sockaddr_in *)&int_req.ifr_addr)->sin_addr.s_addr; +int main( int argc, char *argv[] ) {
- if ( ioctl( batman_if->udp_recv_sock, SIOCGIFBRDADDR, &int_req ) < 0 ) { - - do_log( "Error - can't get broadcast IP address of interface %s\n", batman_if->dev ); - close_all_sockets(); - exit(EXIT_FAILURE); - - } - - batman_if->broad.sin_family = AF_INET; - batman_if->broad.sin_port = htons(PORT); - batman_if->broad.sin_addr.s_addr = ((struct sockaddr_in *)&int_req.ifr_broadaddr)->sin_addr.s_addr; - - if ( setsockopt( batman_if->udp_send_sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (int) ) < 0 ) { - - do_log( "Error - can't enable broadcasts: %s\n", strerror(errno) ); - close_all_sockets(); - exit(EXIT_FAILURE); - - } - - if ( bind_to_iface( batman_if->udp_send_sock, batman_if->dev ) < 0 ) { - - close_all_sockets(); - exit(EXIT_FAILURE); - - } + short res;
- if ( bind( batman_if->udp_send_sock, (struct sockaddr *)&batman_if->addr, sizeof (struct sockaddr_in) ) < 0 ) {
- do_log( "Error - can't bind send socket: %s\n", strerror(errno) ); - close_all_sockets(); - exit(EXIT_FAILURE); - - } - - if ( bind_to_iface( batman_if->udp_recv_sock, batman_if->dev ) < 0 ) { - - close_all_sockets(); - exit(EXIT_FAILURE); - - } - - if ( bind( batman_if->udp_recv_sock, (struct sockaddr *)&batman_if->broad, sizeof (struct sockaddr_in) ) < 0 ) { - - do_log( "Error - can't bind receive socket: %s\n", strerror(errno) ); - close_all_sockets(); - exit(EXIT_FAILURE); - - } - - - 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)); - - 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 ) { - - batman_if->addr.sin_port = htons(PORT + 1); - - batman_if->tcp_gw_sock = socket(PF_INET, SOCK_STREAM, 0); - - 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 ) { - 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 ) { - 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 ) { - 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 ) { - 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 ) { - do_log( "Error - can't bind tunnel socket: %s\n", strerror(errno) ); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - batman_if->addr.sin_port = htons(PORT); - - pthread_create(&batman_if->listen_thread_id, NULL, &gw_listen, batman_if); - - } - - found_ifs++; - found_args++; - - } - - if(vis_server) - { - memset(&vis_if.addr, 0, sizeof(vis_if.addr)); - vis_if.addr.sin_family = AF_INET; - vis_if.addr.sin_port = htons(1967); - vis_if.addr.sin_addr.s_addr = vis_server; - /*vis_if.sock = socket( PF_INET, SOCK_DGRAM, 0);*/ - vis_if.sock = ((struct batman_if *)if_list.next)->udp_send_sock; - } else - memset(&vis_if, 0, sizeof(vis_if)); - - - if ( debug_level > 0 ) { - - printf("debug level: %i\n", debug_level); - - if ( orginator_interval != 1000 ) - printf( "orginator interval: %i\n", orginator_interval ); - - if ( gateway_class > 0 ) - printf( "gateway class: %i\n", gateway_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 ( vis_server > 0 ) { - addr_to_string(vis_server, str1, sizeof (str1)); - printf( "visualisation server: %s\n", str1 ); - } + stop = 0;
- } + apply_init_args( argc, argv );
signal(SIGINT, handler);