The following commit has been merged in the master branch: commit 3e5f348d08e306b3cc6cae51296e0589f0c32df7 Author: Marek Lindner lindner_marek@yahoo.de Date: Fri Oct 20 11:35:37 2006 +0200
hna feature added
diff --git a/CHANGELOG b/CHANGELOG index 06709f4..5f54fd5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -19,6 +19,8 @@ new functions: - batman version check added - preferred gateway funtion added - UDP tunnel implemented +- daemonizing batman -> batmand +- improving output / logging to syslog added - FreeBSD support added - Mac OS X support added
diff --git a/batman.c b/batman.c index 6a6ba59..3e54451 100755 --- a/batman.c +++ b/batman.c @@ -72,6 +72,10 @@ struct gw_node *curr_gateway = NULL; pthread_t curr_gateway_thread_id = 0;
unsigned int pref_gateway = 0; + +unsigned char *hna_buff = NULL; + +int num_hna = 0; int found_ifs = 0;
@@ -80,6 +84,7 @@ static LIST_HEAD(orig_list); static LIST_HEAD(forw_list); static LIST_HEAD(gw_list); LIST_HEAD(if_list); +LIST_HEAD(hna_list); static unsigned int next_own;
struct vis_if vis_if; @@ -200,11 +205,12 @@ static void choose_gw() switch ( routing_class ) {
case 1: /* fast connection */ - if ( ( gw_node->orig_node->gwflags > max_gw_class ) || ( ( gw_node->orig_node->gwflags == max_gw_class ) && ( gw_node->orig_node->packet_count > max_packets ) ) ) + if ( ( ( gw_node->orig_node->packet_count * gw_node->orig_node->gwflags ) > max_gw_factor ) || ( ( ( gw_node->orig_node->packet_count * gw_node->orig_node->gwflags ) == max_gw_factor ) && ( gw_node->orig_node->packet_count > max_packets ) ) ) tmp_curr_gw = gw_node; break;
case 2: /* stable connection */ + /* FIXME - not implemented yet */ if ( ( ( gw_node->orig_node->packet_count * gw_node->orig_node->gwflags ) > max_gw_factor ) || ( ( ( gw_node->orig_node->packet_count * gw_node->orig_node->gwflags ) == max_gw_factor ) && ( gw_node->orig_node->packet_count > max_packets ) ) ) tmp_curr_gw = gw_node; break; @@ -678,8 +684,8 @@ void send_outstanding_packets()
batman_if = list_entry(if_pos, struct batman_if, list);
- if (send_packet((unsigned char *)pack, sizeof (struct packet), &batman_if->broad, batman_if->udp_send_sock) < 0) { - output("ERROR: send_packet returned -1 \n"); + if (send_packet((unsigned char *)pack, ( num_hna > 0 ? sizeof (struct packet) + num_hna * 5 * sizeof( unsigned char ) : sizeof (struct packet) ), &batman_if->broad, batman_if->udp_send_sock) < 0) { + do_log( "ERROR: %s returned -1\n", "send_packet" ); exit( -1); }
@@ -714,10 +720,18 @@ void schedule_own_packet() {
batman_if = list_entry(if_pos, struct batman_if, list);
- forw_node_new = alloc_memory(sizeof (struct forw_node)); + if ( num_hna > 0 ) + forw_node_new = alloc_memory(sizeof (struct forw_node) + num_hna * 5 * sizeof( unsigned char ) ); + else + forw_node_new = alloc_memory(sizeof (struct forw_node)); + INIT_LIST_HEAD(&forw_node_new->list);
memcpy(&forw_node_new->pack, &batman_if->out, sizeof (struct packet)); + + if ( num_hna > 0 ) + memcpy(&forw_node_new->pack + sizeof (struct packet), hna_buff, num_hna * 5 * sizeof( unsigned char ) ); + forw_node_new->when = next_own;
list_add(&forw_node_new->list, (forw_node == NULL ? &forw_list : forw_pos)); @@ -865,22 +879,41 @@ void send_vis_packet()
int batman() { - struct list_head *forw_pos, *orig_pos, *if_pos, *neigh_pos; + struct list_head *forw_pos, *orig_pos, *if_pos, *neigh_pos, *hna_pos; struct forw_node *forw_node; struct orig_node *orig_node, *orig_neigh_node; struct batman_if *batman_if, *if_incoming; struct neigh_node *neigh_node; + struct hna_node *hna_node; struct packet in; - int res; - unsigned int neigh; + unsigned int neigh, hna; + unsigned char hna_recv_buff[1500 - sizeof (struct packet)]; static char orig_str[ADDR_STR_LEN], neigh_str[ADDR_STR_LEN]; - int forward_old; + int forward_old, res, hna_buff_len, hna_buff_count, netmask; int is_my_addr, is_my_orig, is_broadcast, is_duplicate, forward_duplicate_packet; int time_count = 0;
next_own = 0;
+ if ( !( list_empty(&hna_list) ) ) { + + list_for_each(hna_pos, &hna_list) { + + hna_node = list_entry(hna_pos, struct hna_node, list); + + hna_buff = realloc_memory( hna_buff, ( num_hna + 1 ) * 5 * sizeof( unsigned char ) ); + + memmove( &hna_buff[ num_hna * 5 ], ( unsigned char *)&hna_node->addr, 4 ); + hna_buff[ ( num_hna * 5 ) + 4 ] = ( unsigned char ) hna_node->netmask; + + num_hna++; + + } + + } + list_for_each(if_pos, &if_list) { + batman_if = list_entry(if_pos, struct batman_if, list);
batman_if->out.orig = batman_if->addr.sin_addr.s_addr; @@ -889,6 +922,7 @@ int batman() batman_if->out.seqno = 0; batman_if->out.gwflags = gateway_class; batman_if->out.version = BATMAN_VERSION; + }
forward_old = get_forwarding(); @@ -908,7 +942,8 @@ int batman()
list_for_each(forw_pos, &forw_list) { forw_node = list_entry(forw_pos, struct forw_node, list); - res = receive_packet((unsigned char *)&in, sizeof (struct packet), &neigh, forw_node->when, &if_incoming); + hna_buff_len = 1500 - sizeof (struct packet); + res = receive_packet((unsigned char *)&in, sizeof (struct packet), hna_recv_buff, &hna_buff_len, &neigh, forw_node->when, &if_incoming); break; }
@@ -917,6 +952,25 @@ int batman()
if (res > 0) { + if ( hna_buff_len > 0 ) { + + printf( "hna message received .. \n" ); + hna_buff_count = 0; + + while ( ( hna_buff_count + 1 ) * 5 >= hna_buff_len ) { + + hna_buff_count++; + + memmove( &hna, ( unsigned int *)&hna_recv_buff[ hna_buff_count * 5 ], 4 ); + netmask = ( unsigned int )hna_recv_buff[ ( hna_buff_count * 5 ) + 4 ]; + + addr_to_string(hna, orig_str, sizeof (orig_str)); + printf( "hna: %s/%i\n", orig_str, netmask ); + + } + + } + if (debug_level == 3) { addr_to_string(in.orig, orig_str, sizeof (orig_str)); addr_to_string(neigh, neigh_str, sizeof (neigh_str)); diff --git a/batman.h b/batman.h index 00d5fae..1f65f5d 100755 --- a/batman.h +++ b/batman.h @@ -47,14 +47,18 @@ extern int debug_level; extern int orginator_interval; extern int gateway_class; extern int routing_class; +extern int num_hna; extern unsigned int pref_gateway;
+extern unsigned char *hna_buff; + extern struct gw_node *curr_gateway; pthread_t curr_gateway_thread_id;
extern int found_ifs;
extern struct list_head if_list; +extern struct list_head hna_list; extern struct vis_if vis_if;
struct packet @@ -80,6 +84,7 @@ struct orig_node /* structure for orig_list maintaining nodes of unsigned char flags; unsigned char gwflags; /* flags related to gateway functions: gateway class */ struct list_head neigh_list; + struct list_head hna_list; };
struct neigh_node @@ -90,6 +95,14 @@ struct neigh_node struct list_head pack_list; };
+struct hna_node +{ + struct list_head list; + unsigned int addr; + unsigned int netmask; +}; + + struct pack_node { struct list_head list; diff --git a/os.h b/os.h index 2fbb879..6827559 100755 --- a/os.h +++ b/os.h @@ -32,7 +32,7 @@ void *alloc_memory(int len); void *realloc_memory(void *ptr, int len); void free_memory(void *mem); void addr_to_string(unsigned int addr, char *str, int len); -int receive_packet(unsigned char *buff, int len, unsigned int *neigh, unsigned int timeout, struct batman_if **if_incoming); +int receive_packet(unsigned char *packet_buff, int packet_buff_len, unsigned char *hna_buff, 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 ); diff --git a/posix.c b/posix.c index 936cb93..da9eac1 100644 --- a/posix.c +++ b/posix.c @@ -422,12 +422,13 @@ 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) +int receive_packet(unsigned char *packet_buff, int packet_buff_len, unsigned char *hna_buff, 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; + unsigned int addr_len, bytes_read; + unsigned char *buff = NULL; struct timeval tv; struct list_head *if_pos; struct batman_if *batman_if; @@ -440,6 +441,8 @@ int receive_packet(unsigned char *buff, int len, unsigned int *neigh, unsigned i tv.tv_sec = diff / 1000; tv.tv_usec = (diff % 1000) * 1000;
+ buff = alloc_memory( packet_buff_len + *hna_buff_len ); + FD_ZERO(&wait_set);
list_for_each(if_pos, &if_list) { @@ -476,12 +479,29 @@ int receive_packet(unsigned char *buff, int len, unsigned int *neigh, unsigned i
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) + if ( ( bytes_read = recvfrom( batman_if->udp_recv_sock, buff, packet_buff_len + *hna_buff_len, 0, (struct sockaddr *)&addr, &addr_len ) ) < 0 ) { do_log( "Error - can't receive packet: %s\n", strerror(errno) ); return -1; }
+ if ( bytes_read == packet_buff_len ) { + + memmove( packet_buff, buff, bytes_read ); + *hna_buff_len = 0; + + } else if ( bytes_read > packet_buff_len ) { + + memmove( packet_buff, buff, packet_buff_len ); + memmove( hna_buff, buff + packet_buff_len, bytes_read - packet_buff_len ); + *hna_buff_len = bytes_read - packet_buff_len; + + } else { + + return 0; + + } + (*if_incoming) = batman_if; break;
@@ -745,19 +765,67 @@ 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; - int on = 1, res, optchar, found_args = 1, pid; - char str1[16], str2[16], *dev; + int on = 1, res, optchar, found_args = 1, pid, netmask; + 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));
- while ( ( optchar = getopt ( argc, argv, "d:hHo:g:p:r:s:" ) ) != -1 ) { + while ( ( optchar = getopt ( argc, argv, "a:d:hHo:g:p:r:s:" ) ) != -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 && (netmask == LONG_MAX || netmask == LONG_MIN) ) || (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 = alloc_memory(sizeof(struct hna_node)); + 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); + + found_args += 2; + break; + case 'd':
errno = 0; @@ -779,15 +847,15 @@ int main(int argc, char *argv[]) case 'g':
errno = 0; - gateway_class = strtol (optarg, NULL , 10); + 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 ); + 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); }