The following commit has been merged in the master branch: commit 6f7c64b3d9763a518969e22bed9e1b3e60c433e0 Author: Stefan Sperling stsp@stsp.in-berlin.de Date: Thu Aug 31 00:20:43 2006 +0200
Move common code from Linux and FreeBSD versions into posix.c.
diff --git a/Makefile b/Makefile index f23f72e..f3c8db7 100755 --- a/Makefile +++ b/Makefile @@ -23,15 +23,15 @@ LDFLAGS = -lpthread UNAME=$(shell uname)
ifeq ($(UNAME),Linux) -OS_OBJ= linux.o +OS_OBJ= posix.o linux.o endif
ifeq ($(UNAME),Darwin) -OS_OBJ= freebsd.o +OS_OBJ= posix.o freebsd.o endif
ifeq ($(UNAME),FreeBSD) -OS_OBJ= freebsd.o +OS_OBJ= posix.o freebsd.o endif
batman: batman.o $(OS_OBJ) diff --git a/freebsd.c b/freebsd.c index 268068f..4d7595d 100644 --- a/freebsd.c +++ b/freebsd.c @@ -1,6 +1,6 @@ /* - * Copyright (C) 2006 BATMAN contributors: - * Thomas Lopatic + * Copyright (C) 2006 BATMAN contributors. + * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. @@ -44,65 +44,9 @@
#include "os.h" #include "batman.h" -#include "list.h" - -static struct timeval start_time; -static int stop; - -/* Message structure used to interface the kernel routing table. - * See route(4) for details on the message passing interface for - * manipulating the kernel routing table in FreeBSD. - * We transmit at most two addresses at once: a destination host - * and a gateway. - */ -struct rt_msg -{ - struct rt_msghdr hdr; - struct sockaddr_in dest; - struct sockaddr_in gateway; -};
#define SYSCTL_FORWARDING "net.inet.ip.forwarding"
-static void get_time_internal(struct timeval *tv) -{ - int sec; - int usec; - gettimeofday(tv, NULL); - - sec = tv->tv_sec - start_time.tv_sec; - usec = tv->tv_usec - start_time.tv_usec; - - if (usec < 0) - { - sec--; - usec += 1000000; - } - - tv->tv_sec = sec; - tv->tv_usec = usec; -} - -unsigned int get_time(void) -{ - struct timeval tv; - - get_time_internal(&tv); - - return tv.tv_sec * 1000 + tv.tv_usec / 1000; -} - -void output(char *format, ...) -{ - va_list args; - - printf("[%10u] ", get_time()); - - va_start(args, format); - vprintf(format, args); - va_end(args); -} - void set_forwarding(int state) { /* FreeBSD allows us to set the boolean IP forwarding @@ -132,17 +76,18 @@ int get_forwarding(void) return state; }
-void close_all_sockets() +/* Message structure used to interface the kernel routing table. + * See route(4) for details on the message passing interface for + * manipulating the kernel routing table in FreeBSD. + * We transmit at most two addresses at once: a destination host + * and a gateway. + */ +struct rt_msg { - struct list_head *if_pos; - struct batman_if *batman_if; - - list_for_each(if_pos, &if_list) { - batman_if = list_entry(if_pos, struct batman_if, list); - close(batman_if->udp_send_sock); - close(batman_if->udp_recv_sock); - } -} + struct rt_msghdr hdr; + struct sockaddr_in dest; + struct sockaddr_in gateway; +};
void add_del_route(unsigned int dest, unsigned int router, int del, char *dev, int sock) @@ -246,384 +191,3 @@ void add_del_route(unsigned int dest, unsigned int router, int del, } }
-int is_aborted() -{ - return stop != 0; -} - -void *alloc_memory(int len) -{ - void *res = malloc(len); - - if (res == NULL) - { - fprintf(stderr, "Out of memory\n"); - exit(1); - } - - return res; -} - -void *realloc_memory(void *ptr, int len) -{ - void *res = realloc(ptr,len); - - if (res == NULL) - { - fprintf(stderr, "Out of memory\n"); - exit(1); - } - - return res; -} - -void free_memory(void *mem) -{ - free(mem); -} - -void addr_to_string(unsigned int addr, char *str, int len) -{ - inet_ntop(AF_INET, &addr, str, len); -} - -int rand_num(int limit) -{ - return rand() % limit; -} - -int receive_packet(unsigned char *buff, int 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; - - int diff = timeout - get_time(); - - if (diff < 0) - return 0; - - tv.tv_sec = diff / 1000; - tv.tv_usec = (diff % 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) - { - fprintf(stderr, "Cannot 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 (recvfrom(batman_if->udp_recv_sock, buff, len, 0, (struct sockaddr *)&addr, &addr_len) < 0) - { - fprintf(stderr, "Cannot receive packet: %s\n", strerror(errno)); - return -1; - } - - (*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) -{ - if (sendto(sock, buff, len, 0, (struct sockaddr *)broad, sizeof (struct sockaddr_in)) < 0) - { - fprintf(stderr, "Cannot send packet: %s\n", strerror(errno)); - return -1; - } - - return 0; -} - -static void handler(int sig) -{ - stop = 1; -} - -int main(int argc, char *argv[]) -{ - struct in_addr tmp_pref_gw; - struct batman_if *batman_if; - struct ifreq int_req; - int on = 1, res, optchar, found_args = 1; - char str1[16], str2[16], *dev; - - dev = NULL; - memset(&tmp_pref_gw, 0, sizeof (struct in_addr)); - - printf( "B.A.T.M.A.N-II v%s (internal version %i)\n", VERSION, BATMAN_VERSION ); - - while ( ( optchar = getopt ( argc, argv, "d:hHo:g:p:r:s:" ) ) != -1 ) { - - switch ( optchar ) { - - case 'd': - - errno = 0; - debug_level = strtol (optarg, NULL , 10); - - if ( (errno == ERANGE && (debug_level == LONG_MAX || debug_level == LONG_MIN) ) || (errno != 0 && debug_level == 0) ) { - perror("strtol"); - exit(EXIT_FAILURE); - } - - if ( debug_level < 0 || debug_level > 3 ) { - printf( "Invalid debug level: %i\nDebug level has to be between 0 and 3.\n", debug_level ); - exit(EXIT_FAILURE); - } - - found_args += 2; - break; - - case 'g': - - errno = 0; - gateway_class = strtol (optarg, NULL , 10); - - if ( (errno == ERANGE && (gateway_class == LONG_MAX || gateway_class == LONG_MIN) ) || (errno != 0 && gateway_class == 0) ) { - perror("strtol"); - exit(EXIT_FAILURE); - } - - if ( gateway_class < 0 || gateway_class > 32 ) { - printf( "Invalid gateway class specified: %i.\nThe class is a value between 0 and 32.\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_pref_gw) < 1 ) { - - printf( "Invalid preferred gateway IP specified: %s\n", optarg ); - exit(EXIT_FAILURE); - - } - - pref_gateway = tmp_pref_gw.s_addr; - - found_args += 2; - break; - - case 's': - - errno = 0; - if ( inet_pton(AF_INET, optarg, &tmp_pref_gw) < 1 ) { - - printf( "Invalid preferred gateway IP specified: %s\n", optarg ); - exit(EXIT_FAILURE); - - } - - pref_gateway = tmp_pref_gw.s_addr; - - - found_args += 2; - break; - - case 'h': - default: - usage(); - return (0); - - } - - } - - - while ( argc > found_args ) { - - batman_if = alloc_memory(sizeof(struct batman_if)); - memset(batman_if, 0, sizeof(struct batman_if)); - INIT_LIST_HEAD(&batman_if->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 ) { - fprintf(stderr, "Interface name too long: %s\n", batman_if->dev); - 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)); - exit(EXIT_FAILURE); - } - - batman_if->udp_recv_sock = socket(PF_INET, SOCK_DGRAM, 0); - if (batman_if->udp_recv_sock < 0) - { - fprintf(stderr, "Cannot create receive socket: %s", strerror(errno)); - exit(EXIT_FAILURE); - } - - memset(&int_req, 0, sizeof (struct ifreq)); - strcpy(int_req.ifr_name, batman_if->dev); - - if (ioctl(batman_if->udp_recv_sock, SIOCGIFADDR, &int_req) < 0) - { - fprintf(stderr, "Cannot 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; - - 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); - 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) - { - fprintf(stderr, "Cannot 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)); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - 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)); - 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)); - - printf("Using interface %s with address %s and broadcast address %s\n", batman_if->dev, str1, str2); - - found_ifs++; - found_args++; - - } - - - if (found_ifs == 0) - { - fprintf(stderr, "Error - no interface specified\n"); - usage(); - return 1; - } - - if ( ( gateway_class != 0 ) && ( routing_class != 0 ) ) - { - fprintf(stderr, "Error - routing class can't be set while gateway class is in use !\n"); - usage(); - return 1; - } - - if ( ( gateway_class != 0 ) && ( pref_gateway != 0 ) ) - { - fprintf(stderr, "Error - preferred gateway can't be set while gateway class is in use !\n"); - usage(); - return 1; - } - - 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 ( ( debug_level > 0 ) && ( 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 ( ( debug_level > 0 ) && ( pref_gateway > 0 ) ) { - addr_to_string(pref_gateway, str1, sizeof (str1)); - printf( "preferred gateway: %s\n", str1 ); - } - - - stop = 0; - - signal(SIGINT, handler); - - gettimeofday(&start_time, NULL); - - srand(getpid()); - - res = batman(); - - close_all_sockets(); - return res; -} diff --git a/linux.c b/linux.c index c9242a0..c25bf97 100755 --- a/linux.c +++ b/linux.c @@ -35,52 +35,6 @@
#include "os.h" #include "batman.h" -#include "list.h" - -extern struct vis_if vis_if; - -static struct timeval start_time; -static int stop; - - -static void get_time_internal(struct timeval *tv) -{ - int sec; - int usec; - gettimeofday(tv, NULL); - - sec = tv->tv_sec - start_time.tv_sec; - usec = tv->tv_usec - start_time.tv_usec; - - if (usec < 0) - { - sec--; - usec += 1000000; - } - - tv->tv_sec = sec; - tv->tv_usec = usec; -} - -unsigned int get_time(void) -{ - struct timeval tv; - - get_time_internal(&tv); - - return tv.tv_sec * 1000 + tv.tv_usec / 1000; -} - -void output(char *format, ...) -{ - va_list args; - - printf("[%10u] ", get_time()); - - va_start(args, format); - vprintf(format, args); - va_end(args); -}
void set_forwarding(int state) { @@ -107,22 +61,6 @@ int get_forwarding(void) return state; }
-void close_all_sockets() -{ - struct list_head *if_pos; - struct batman_if *batman_if; - - list_for_each(if_pos, &if_list) { - batman_if = list_entry(if_pos, struct batman_if, list); -// pthread_join( batman_if->listen_thread_id, NULL ); - close(batman_if->tcp_gw_sock); - close(batman_if->udp_recv_sock); - } - - if(vis_if.sock) - close(vis_if.sock); -} - void add_del_route(unsigned int dest, unsigned int router, int del, char *dev, int sock) { struct rtentry route; @@ -171,453 +109,3 @@ void add_del_route(unsigned int dest, unsigned int router, int del, char *dev, i } }
-int is_aborted() -{ - return stop != 0; -} - -void *alloc_memory(int len) -{ - void *res = malloc(len); - - if (res == NULL) - { - fprintf(stderr, "Out of memory\n"); - exit(1); - } - - return res; -} - -void *realloc_memory(void *ptr, int len) -{ - void *res = realloc(ptr,len); - - if (res == NULL) - { - fprintf(stderr, "Out of memory\n"); - exit(1); - } - - return res; -} - -void free_memory(void *mem) -{ - free(mem); -} - -void addr_to_string(unsigned int addr, char *str, int len) -{ - inet_ntop(AF_INET, &addr, str, len); -} - -int rand_num(int limit) -{ - return rand() % limit; -} - -int receive_packet(unsigned char *buff, int 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; - - int diff = timeout - get_time(); - - if (diff < 0) - return 0; - - tv.tv_sec = diff / 1000; - tv.tv_usec = (diff % 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) - { - fprintf(stderr, "Cannot 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 (recvfrom(batman_if->udp_recv_sock, buff, len, 0, (struct sockaddr *)&addr, &addr_len) < 0) - { - fprintf(stderr, "Cannot receive packet: %s\n", strerror(errno)); - return -1; - } - - (*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) -{ - if (sendto(sock, buff, len, 0, (struct sockaddr *)broad, sizeof (struct sockaddr_in)) < 0) - { - fprintf(stderr, "Cannot 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 sockaddr_in client_addr; - socklen_t sin_size = sizeof(struct sockaddr_in); - char str1[16], str2[16]; - int client_fd; - - while (!is_aborted()) { - - if ( ( client_fd = accept(batman_if->tcp_gw_sock, (struct sockaddr *)&client_addr, &sin_size) ) == -1 ) { - perror("accept"); - continue; - } - - if ( debug_level >= 0 ) { - addr_to_string(batman_if->addr.sin_addr.s_addr, str1, sizeof (str1)); - addr_to_string(client_addr.sin_addr.s_addr, str2, sizeof (str2)); - printf( "gateway: %s got connection from %s\n", str1, str2 ); - } - - close( client_fd ); - - } - - return NULL; - -} - -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; - char str1[16], str2[16], *dev; - unsigned int vis_server = 0; - - stop = 0; - dev = NULL; - memset(&tmp_ip_holder, 0, sizeof (struct in_addr)); - - printf( "B.A.T.M.A.N-II v%s (internal version %i)\n", VERSION, BATMAN_VERSION ); - - while ( ( optchar = getopt ( argc, argv, "d:hHo:g:p:r:s:" ) ) != -1 ) { - - switch ( optchar ) { - - case 'd': - - errno = 0; - debug_level = strtol (optarg, NULL , 10); - - if ( (errno == ERANGE && (debug_level == LONG_MAX || debug_level == LONG_MIN) ) || (errno != 0 && debug_level == 0) ) { - perror("strtol"); - exit(EXIT_FAILURE); - } - - if ( debug_level < 0 || debug_level > 3 ) { - printf( "Invalid debug level: %i\nDebug level has to be between 0 and 3.\n", debug_level ); - exit(EXIT_FAILURE); - } - - found_args += 2; - break; - - case 'g': - - errno = 0; - gateway_class = strtol (optarg, NULL , 10); - - if ( (errno == ERANGE && (gateway_class == LONG_MAX || gateway_class == LONG_MIN) ) || (errno != 0 && gateway_class == 0) ) { - perror("strtol"); - exit(EXIT_FAILURE); - } - - if ( gateway_class < 0 || gateway_class > 32 ) { - printf( "Invalid gateway class specified: %i.\nThe class is a value between 0 and 32.\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 '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 'h': - default: - usage(); - return (0); - - } - - } - - - while ( argc > found_args ) { - - batman_if = alloc_memory(sizeof(struct batman_if)); - memset(batman_if, 0, sizeof(struct batman_if)); - INIT_LIST_HEAD(&batman_if->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 ) { - fprintf(stderr, "Interface name too long: %s\n", batman_if->dev); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - batman_if->udp_recv_sock = socket(PF_INET, SOCK_DGRAM, 0); - - if (batman_if->udp_recv_sock < 0) - { - fprintf(stderr, "Cannot create socket: %s", strerror(errno)); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - memset(&int_req, 0, sizeof (struct ifreq)); - strcpy(int_req.ifr_name, batman_if->dev); - - if (ioctl(batman_if->udp_recv_sock, SIOCGIFADDR, &int_req) < 0) - { - fprintf(stderr, "Cannot 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; - - 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); - 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_recv_sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (int)) < 0) - { - fprintf(stderr, "Cannot enable broadcasts: %s\n", strerror(errno)); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - if (bind(batman_if->udp_recv_sock, (struct sockaddr *)&batman_if->broad, sizeof (struct sockaddr_in)) < 0) - { - fprintf(stderr, "Cannot bind socket: %s\n", strerror(errno)); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - batman_if->udp_send_sock = batman_if->udp_recv_sock; - - 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 ( gateway_class != 0 ) { - - 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)); - 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)); - 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)); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - if (listen( batman_if->tcp_gw_sock, 10 ) < 0) { - printf("Cannot listen socket: %s\n",strerror(errno)); - close_all_sockets(); - exit(EXIT_FAILURE); - } - - 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); - } else - memset(&vis_if, 0, sizeof(vis_if)); - - - if (found_ifs == 0) - { - fprintf(stderr, "Error - no interface specified\n"); - usage(); - return 1; - } - - if ( ( gateway_class != 0 ) && ( routing_class != 0 ) ) - { - fprintf(stderr, "Error - routing class can't be set while gateway class is in use !\n"); - usage(); - return 1; - } - - if ( ( gateway_class != 0 ) && ( pref_gateway != 0 ) ) - { - fprintf(stderr, "Error - preferred gateway can't be set while gateway class is in use !\n"); - usage(); - return 1; - } - - if ( ( routing_class == 0 ) && ( pref_gateway != 0 ) ) - { - fprintf(stderr, "Error - preferred gateway can't be set without specifying routing class !\n"); - usage(); - return 1; - } - - 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 ( ( debug_level > 0 ) && ( 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 ( ( debug_level > 0 ) && ( pref_gateway > 0 ) ) { - addr_to_string(pref_gateway, str1, sizeof (str1)); - printf( "preferred gateway: %s\n", str1 ); - } - - signal(SIGINT, handler); - - gettimeofday(&start_time, NULL); - - srand(getpid()); - - res = batman(); - - close_all_sockets(); - return res; - -} diff --git a/linux.c b/posix.c old mode 100755 new mode 100644 similarity index 89% copy from linux.c copy to posix.c index c9242a0..cc99f14 --- a/linux.c +++ b/posix.c @@ -23,6 +23,7 @@ #include <sys/ioctl.h> #include <arpa/inet.h> #include <net/route.h> +#include <net/if.h> #include <stdio.h> #include <time.h> #include <string.h> @@ -31,7 +32,6 @@ #include <unistd.h> #include <signal.h> #include <stdlib.h> -#include <linux/if.h>
#include "os.h" #include "batman.h" @@ -82,31 +82,6 @@ void output(char *format, ...) va_end(args); }
-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; -} - void close_all_sockets() { struct list_head *if_pos; @@ -123,54 +98,6 @@ void close_all_sockets() close(vis_if.sock); }
-void add_del_route(unsigned int dest, unsigned int router, int del, char *dev, int sock) -{ - struct rtentry route; - char str1[16], str2[16]; - 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 = 0xffffffff; - - route.rt_flags = RTF_HOST | RTF_UP; - - if (dest != router) - { - addr = (struct sockaddr_in *)&route.rt_gateway; - - addr->sin_family = AF_INET; - addr->sin_addr.s_addr = router; - - route.rt_flags |= RTF_GATEWAY; - - output("%s route to %s via %s\n", del ? "Deleting" : "Adding", str1, str2); - } else { - output("%s route to %s via 0.0.0.0\n", del ? "Deleting" : "Adding", str1); - } - - route.rt_metric = 1; - - 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)); - } -} - int is_aborted() { return stop != 0;