The following commit has been merged in the master branch:
commit fbe655f9812960d0e9c1edf254ee86f63395d78b
Author: Marek Lindner <lindner_marek(a)yahoo.de>
Date: Mon Jan 22 20:16:08 2007 +0100
complete rewrite of internal debug handling
diff --git a/batman.c b/batman.c
index ff1a546..0638adf 100644
--- a/batman.c
+++ b/batman.c
@@ -20,7 +20,7 @@
*/
-#define _GNU_SOURCE
+
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
@@ -89,8 +89,6 @@ unsigned int bidirectional_timeout = 0; /* bidirectional neighbour reply timeo
struct gw_node *curr_gateway = NULL;
pthread_t curr_gateway_thread_id = 0;
-pthread_mutex_t data_mutex = PTHREAD_MUTEX_INITIALIZER;
-
unsigned int pref_gateway = 0;
unsigned char *hna_buff = NULL;
@@ -109,6 +107,7 @@ static unsigned int last_own_packet;
struct vis_if vis_if;
struct unix_if unix_if;
+struct debug_clients debug_clients;
void usage(void)
{
@@ -182,8 +181,7 @@ struct orig_node *get_orig_node( unsigned int addr )
return orig_node;
}
- if (debug_level == 4)
- output( "Creating new originator\n" );
+ debug_output( 4, "Creating new originator\n" );
orig_node = debugMalloc( sizeof(struct orig_node), 1 );
memset(orig_node, 0, sizeof(struct orig_node));
@@ -246,8 +244,7 @@ static void choose_gw() {
if ( curr_gateway != NULL ) {
- if (debug_level == 3)
- printf( "Removing default route - no gateway in range\n" );
+ debug_output( 3, "Removing default route - no gateway in range\n" );
del_default_route();
@@ -302,10 +299,8 @@ static void choose_gw() {
tmp_curr_gw = gw_node;
- if (debug_level == 3) {
- addr_to_string( tmp_curr_gw->orig_node->orig, orig_str, ADDR_STR_LEN );
- printf( "Preferred gateway found: %s (%i,%i,%i)\n", orig_str, gw_node->orig_node->gwflags, gw_node->orig_node->router->packet_count, ( gw_node->orig_node->router->packet_count * gw_node->orig_node->gwflags ) );
- }
+ addr_to_string( tmp_curr_gw->orig_node->orig, orig_str, ADDR_STR_LEN );
+ debug_output( 3, "Preferred gateway found: %s (%i,%i,%i)\n", orig_str, gw_node->orig_node->gwflags, gw_node->orig_node->router->packet_count, ( gw_node->orig_node->router->packet_count * gw_node->orig_node->gwflags ) );
break;
@@ -318,8 +313,7 @@ static void choose_gw() {
if ( curr_gateway != NULL ) {
- if (debug_level == 3)
- printf( "Removing default route - better gateway found\n" );
+ debug_output( 3, "Removing default route - better gateway found\n" );
del_default_route();
@@ -330,10 +324,8 @@ static void choose_gw() {
/* may be the last gateway is now gone */
if ( ( curr_gateway != NULL ) && ( !is_aborted() ) ) {
- if (debug_level == 3) {
- addr_to_string( curr_gateway->orig_node->orig, orig_str, ADDR_STR_LEN );
- printf( "Adding default route to %s (%i,%i,%i)\n", orig_str, max_gw_class, max_packets, max_gw_factor );
- }
+ addr_to_string( curr_gateway->orig_node->orig, orig_str, ADDR_STR_LEN );
+ debug_output( 3, "Adding default route to %s (%i,%i,%i)\n", orig_str, max_gw_class, max_packets, max_gw_factor );
add_default_route();
@@ -350,29 +342,24 @@ static void update_routes( struct orig_node *orig_node, struct neigh_node *neigh
static char orig_str[ADDR_STR_LEN], next_str[ADDR_STR_LEN];
- if ( debug_level == 4 )
- output( "update_routes() \n" );
+ debug_output( 4, "update_routes() \n" );
if ( ( orig_node != NULL ) && ( orig_node->router != neigh_node ) ) {
- if ( ( debug_level == 4 ) && ( orig_node != NULL ) && ( neigh_node != NULL ) ) {
+ if ( ( orig_node != NULL ) && ( neigh_node != NULL ) ) {
addr_to_string( orig_node->orig, orig_str, ADDR_STR_LEN );
addr_to_string( neigh_node->addr, next_str, ADDR_STR_LEN );
- output( "Route to %s via %s\n", orig_str, next_str );
+ debug_output( 4, "Route to %s via %s\n", orig_str, next_str );
}
/* route altered or deleted */
if ( ( ( orig_node->router != NULL ) && ( neigh_node != NULL ) ) || ( neigh_node == NULL ) ) {
- if ( debug_level == 4 ) {
-
- if ( neigh_node == NULL ) {
- output( "Deleting previous route\n" );
- } else {
- output( "Route changed\n" );
- }
-
+ if ( neigh_node == NULL ) {
+ debug_output( 4, "Deleting previous route\n" );
+ } else {
+ debug_output( 4, "Route changed\n" );
}
/* remove old announced network(s) */
@@ -386,14 +373,10 @@ static void update_routes( struct orig_node *orig_node, struct neigh_node *neigh
/* route altered or new route added */
if ( ( ( orig_node->router != NULL ) && ( neigh_node != NULL ) ) || ( orig_node->router == NULL ) ) {
- if ( debug_level == 4 ) {
-
- if ( orig_node->router == NULL ) {
- output( "Adding new route\n" );
- } else {
- output( "Route changed\n" );
- }
-
+ if ( orig_node->router == NULL ) {
+ debug_output( 4, "Adding new route\n" );
+ } else {
+ debug_output( 4, "Route changed\n" );
}
add_del_route( orig_node->orig, 32, neigh_node->addr, 0, neigh_node->if_incoming->dev, neigh_node->if_incoming->udp_send_sock );
@@ -454,20 +437,15 @@ static void update_gw_list( struct orig_node *orig_node, unsigned char new_gwfla
if ( gw_node->orig_node == orig_node ) {
- if (debug_level == 3) {
-
- addr_to_string( gw_node->orig_node->orig, orig_str, ADDR_STR_LEN );
- printf( "Gateway class of originator %s changed from %i to %i\n", orig_str, gw_node->orig_node->gwflags, new_gwflags );
-
- }
+ addr_to_string( gw_node->orig_node->orig, orig_str, ADDR_STR_LEN );
+ debug_output( 3, "Gateway class of originator %s changed from %i to %i\n", orig_str, gw_node->orig_node->gwflags, new_gwflags );
if ( new_gwflags == 0 ) {
list_del( gw_pos );
debugFree( gw_pos, 102 );
- if (debug_level == 3)
- printf( "Gateway %s removed from gateway list\n", orig_str );
+ debug_output( 3, "Gateway %s removed from gateway list\n", orig_str );
} else {
@@ -482,10 +460,8 @@ static void update_gw_list( struct orig_node *orig_node, unsigned char new_gwfla
}
- if ( debug_level == 3 ) {
- addr_to_string( orig_node->orig, orig_str, ADDR_STR_LEN );
- printf( "Found new gateway %s -> class: %i - %s\n", orig_str, new_gwflags, gw2string[new_gwflags] );
- }
+ addr_to_string( orig_node->orig, orig_str, ADDR_STR_LEN );
+ debug_output( 3, "Found new gateway %s -> class: %i - %s\n", orig_str, new_gwflags, gw2string[new_gwflags] );
gw_node = debugMalloc(sizeof(struct gw_node), 5);
memset(gw_node, 0, sizeof(struct gw_node));
@@ -503,7 +479,7 @@ static void update_gw_list( struct orig_node *orig_node, unsigned char new_gwfla
-void debug( int fd, char send_clear, char max_output, char show_gw ) {
+void debug() {
struct list_head *forw_pos, *orig_pos, *neigh_pos;
struct forw_node *forw_node;
@@ -514,14 +490,13 @@ void debug( int fd, char send_clear, char max_output, char show_gw ) {
static char str[ADDR_STR_LEN], str2[ADDR_STR_LEN];
- if ( send_clear )
- system( "clear" );
+ if ( debug_clients.clients_num[1] > 0 ) {
- if ( show_gw ) {
+ debug_output( 2, "BOD\n" );
if ( list_empty(&gw_list) ) {
- dprintf( fd, "No gateways in range ...\n" );
+ debug_output( 2, "No gateways in range ...\n" );
} else {
@@ -532,29 +507,33 @@ void debug( int fd, char send_clear, char max_output, char show_gw ) {
addr_to_string( gw_node->orig_node->router->addr, str2, sizeof (str2) );
if ( curr_gateway == gw_node ) {
- dprintf( fd, "=> %s via: %s(%i), gw_class %i - %s, reliability: %i\n", str, str2, gw_node->orig_node->router->packet_count, gw_node->orig_node->gwflags, gw2string[gw_node->orig_node->gwflags], gw_node->unavail_factor );
+ debug_output( 2, "=> %s via: %s(%i), gw_class %i - %s, reliability: %i\n", str, str2, gw_node->orig_node->router->packet_count, gw_node->orig_node->gwflags, gw2string[gw_node->orig_node->gwflags], gw_node->unavail_factor );
} else {
- dprintf( fd, "%s via: %s(%i), gw_class %i - %s, reliability: %i\n", str, str2, gw_node->orig_node->router->packet_count, gw_node->orig_node->gwflags, gw2string[gw_node->orig_node->gwflags], gw_node->unavail_factor );
+ debug_output( 2, "%s via: %s(%i), gw_class %i - %s, reliability: %i\n", str, str2, gw_node->orig_node->router->packet_count, gw_node->orig_node->gwflags, gw2string[gw_node->orig_node->gwflags], gw_node->unavail_factor );
}
}
}
- } else {
+ }
+
+ if ( ( debug_clients.clients_num[0] > 0 ) || ( debug_clients.clients_num[3] > 0 ) ) {
- if ( max_output ) {
+ debug_output( 1, "BOD\n" );
- output( "------------------ DEBUG ------------------\n" );
- output( "Forward list\n" );
+ if ( debug_clients.clients_num[3] > 0 ) {
+
+ debug_output( 4, "------------------ DEBUG ------------------\n" );
+ debug_output( 4, "Forward list\n" );
list_for_each( forw_pos, &forw_list ) {
forw_node = list_entry( forw_pos, struct forw_node, list );
addr_to_string( ((struct packet *)forw_node->pack_buff)->orig, str, sizeof (str) );
- output( " %s at %u\n", str, forw_node->when );
+ debug_output( 4, " %s at %u\n", str, forw_node->when );
}
- output( "Originator list\n" );
+ debug_output( 4, "Originator list\n" );
}
@@ -569,42 +548,31 @@ void debug( int fd, char send_clear, char max_output, char show_gw ) {
addr_to_string( orig_node->orig, str, sizeof (str) );
addr_to_string( orig_node->router->addr, str2, sizeof (str2) );
- if ( max_output ) {
- output( "%s, GW: %s(%i), last_aware:%u via:\n", str, str2, orig_node->router->packet_count, orig_node->last_aware );
- } else {
- dprintf( fd, "%s, GW: %s(%i) via:", str, str2, orig_node->router->packet_count );
- }
+ debug_output( 1, "%s, GW: %s(%i) via:", str, str2, orig_node->router->packet_count );
+ debug_output( 4, "%s, GW: %s(%i), last_aware:%u via:\n", str, str2, orig_node->router->packet_count, orig_node->last_aware );
list_for_each(neigh_pos, &orig_node->neigh_list) {
neigh_node = list_entry(neigh_pos, struct neigh_node, list);
addr_to_string(neigh_node->addr, str, sizeof (str));
- if ( max_output ) {
- output( "\t\t%s (%d)\n", str, neigh_node->packet_count );
- } else {
- dprintf( fd, " %s(%i)", str, neigh_node->packet_count );
- }
+ debug_output( 1, " %s(%i)", str, neigh_node->packet_count );
+ debug_output( 4, "\t\t%s (%d)\n", str, neigh_node->packet_count );
}
- if ( ! max_output )
- dprintf( fd, "\n" );
+ debug_output( 1, "\n" );
}
if ( batman_count == 0 ) {
- if ( max_output ) {
- output( "No batman nodes in range ...\n" );
- } else {
- dprintf( fd, "No batman nodes in range ...\n" );
- }
+ debug_output( 1, "No batman nodes in range ...\n" );
+ debug_output( 4, "No batman nodes in range ...\n" );
}
- if ( max_output )
- output( "---------------------------------------------- END DEBUG\n" );
+ debug_output( 4, "---------------------------------------------- END DEBUG\n" );
}
@@ -661,8 +629,7 @@ void update_originator( struct orig_node *orig_node, struct packet *in, unsigned
int max_packet_count = 0;
- if ( debug_level == 4 )
- output("update_originator(): Searching and updating originator entry of received packet, \n");
+ debug_output( 4, "update_originator(): Searching and updating originator entry of received packet, \n" );
list_for_each( neigh_pos, &orig_node->neigh_list ) {
@@ -691,8 +658,7 @@ void update_originator( struct orig_node *orig_node, struct packet *in, unsigned
if ( neigh_node == NULL ) {
- if ( debug_level == 4 )
- output( "Creating new last-hop neighbour of originator\n" );
+ debug_output( 4, "Creating new last-hop neighbour of originator\n" );
neigh_node = debugMalloc( sizeof (struct neigh_node), 6 );
INIT_LIST_HEAD(&neigh_node->list);
@@ -702,9 +668,9 @@ void update_originator( struct orig_node *orig_node, struct packet *in, unsigned
list_add_tail(&neigh_node->list, &orig_node->neigh_list);
- } else if ( debug_level == 4 ) {
+ } else {
- output( "Updating existing last-hop neighbour of originator\n" );
+ debug_output( 4, "Updating existing last-hop neighbour of originator\n" );
}
@@ -740,13 +706,11 @@ void schedule_forward_packet( struct packet *in, int unidirectional, int directl
struct forw_node *forw_node_new;
- if ( debug_level == 4 )
- output( "schedule_forward_packet(): \n" );
+ debug_output( 4, "schedule_forward_packet(): \n" );
if ( in->ttl <= 1 ) {
- if ( debug_level == 4 )
- output( "ttl exceeded \n" );
+ debug_output( 4, "ttl exceeded \n" );
} else {
@@ -818,8 +782,7 @@ void send_outstanding_packets() {
if ( forw_node->when <= get_time() ) {
- if ( debug_level == 4 )
- addr_to_string( ((struct packet *)forw_node->pack_buff)->orig, orig_str, ADDR_STR_LEN );
+ addr_to_string( ((struct packet *)forw_node->pack_buff)->orig, orig_str, ADDR_STR_LEN );
directlink = ( ( ((struct packet *)forw_node->pack_buff)->flags & DIRECTLINK ) ? 1 : 0 );
@@ -828,8 +791,7 @@ void send_outstanding_packets() {
if ( forw_node->if_outgoing != NULL ) {
- if ( debug_level == 4 )
- output( "Forwarding packet (originator %s, seqno %d, TTL %d) on interface %s\n", orig_str, ((struct packet *)forw_node->pack_buff)->seqno, ((struct packet *)forw_node->pack_buff)->ttl, forw_node->if_outgoing->dev );
+ debug_output( 4, "Forwarding packet (originator %s, seqno %d, TTL %d) on interface %s\n", orig_str, ((struct packet *)forw_node->pack_buff)->seqno, ((struct packet *)forw_node->pack_buff)->ttl, forw_node->if_outgoing->dev );
if ( send_packet( forw_node->pack_buff, forw_node->pack_buff_len, &forw_node->if_outgoing->broad, forw_node->if_outgoing->udp_send_sock ) < 0 ) {
exit( -1 );
@@ -837,7 +799,7 @@ void send_outstanding_packets() {
} else {
- do_log( "Error - can't forward packet with UDF: outgoing iface not specified \n", "" );
+ debug_output( 0, "Error - can't forward packet with UDF: outgoing iface not specified \n" );
}
@@ -852,7 +814,7 @@ void send_outstanding_packets() {
} else {
- do_log( "Error - can't forward packet with IDF: outgoing iface not specified (multihomed) \n", "" );
+ debug_output( 0, "Error - can't forward packet with IDF: outgoing iface not specified (multihomed) \n" );
}
@@ -860,7 +822,7 @@ void send_outstanding_packets() {
if ( ( directlink ) && ( forw_node->if_outgoing == NULL ) ) {
- do_log( "Error - can't forward packet with IDF: outgoing iface not specified \n", "" );
+ debug_output( 0, "Error - can't forward packet with IDF: outgoing iface not specified \n" );
} else {
@@ -874,8 +836,7 @@ void send_outstanding_packets() {
((struct packet *)forw_node->pack_buff)->flags = 0x00;
}
- if ( debug_level == 4 )
- output( "Forwarding packet (originator %s, seqno %d, TTL %d) on interface %s\n", orig_str, ((struct packet *)forw_node->pack_buff)->seqno, ((struct packet *)forw_node->pack_buff)->ttl, batman_if->dev );
+ debug_output( 4, "Forwarding packet (originator %s, seqno %d, TTL %d) on interface %s\n", orig_str, ((struct packet *)forw_node->pack_buff)->seqno, ((struct packet *)forw_node->pack_buff)->ttl, batman_if->dev );
/* non-primary interfaces do not send hna information */
if ( ( forw_node->own ) && ( ((struct packet *)forw_node->pack_buff)->orig != ((struct batman_if *)if_list.next)->addr.sin_addr.s_addr ) ) {
@@ -972,8 +933,7 @@ void purge( unsigned int curr_time ) {
short gw_purged = 0;
static char orig_str[ADDR_STR_LEN];
- if ( debug_level == 4 )
- output( "purge() \n" );
+ debug_output( 4, "purge() \n" );
/* for all origins... */
list_for_each_safe(orig_pos, orig_temp, &orig_list) {
@@ -981,10 +941,8 @@ void purge( unsigned int curr_time ) {
if ( (int)( ( orig_node->last_aware + TIMEOUT ) < curr_time ) ) {
- if ( debug_level == 4 ) {
- addr_to_string(orig_node->orig, orig_str, ADDR_STR_LEN);
- output( "Orginator timeout: originator %s, last_aware %u)\n", orig_str, orig_node->last_aware );
- }
+ addr_to_string(orig_node->orig, orig_str, ADDR_STR_LEN);
+ debug_output( 4, "Orginator timeout: originator %s, last_aware %u)\n", orig_str, orig_node->last_aware );
/* for all neighbours towards this orginator ... */
list_for_each_safe( neigh_pos, neigh_temp, &orig_node->neigh_list ) {
@@ -1002,8 +960,7 @@ void purge( unsigned int curr_time ) {
if ( gw_node->orig_node == orig_node ) {
addr_to_string( gw_node->orig_node->orig, orig_str, ADDR_STR_LEN );
- if (debug_level == 3)
- printf( "Removing gateway %s from gateway list\n", orig_str );
+ debug_output( 3, "Removing gateway %s from gateway list\n", orig_str );
list_del( gw_pos );
debugFree( gw_pos, 107 );
@@ -1136,10 +1093,9 @@ int batman()
forward_old = get_forwarding();
set_forwarding(1);
- while (!is_aborted())
- {
- if (debug_level == 4)
- output(" \n \n");
+ while ( !is_aborted() ) {
+
+ debug_output( 4, " \n \n" );
if(vis_if.sock && time_count == 50)
{
@@ -1147,26 +1103,20 @@ int batman()
send_vis_packet();
}
- pthread_mutex_unlock( &data_mutex );
-
/* harden select_timeout against sudden time change (e.g. ntpdate) */
curr_time = get_time();
select_timeout = ( curr_time >= last_own_packet + orginator_interval - 10 ? orginator_interval : last_own_packet + orginator_interval - curr_time );
res = receive_packet( ( unsigned char *)&in, 1501, &hna_buff_len, &neigh, select_timeout, &if_incoming );
- pthread_mutex_lock( &data_mutex );
-
- if (res < 0)
+ if ( res < 0 )
return -1;
- if (res > 0)
- {
- if ( debug_level == 4 ) {
- addr_to_string( ((struct packet *)&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, ((struct packet *)&in)->seqno, ((struct packet *)&in)->ttl );
- }
+ if ( res > 0 ) {
+
+ addr_to_string( ((struct packet *)&in)->orig, orig_str, sizeof(orig_str) );
+ addr_to_string( neigh, neigh_str, sizeof(neigh_str) );
+ debug_output( 4, "Received BATMAN packet from %s (originator %s, seqno %d, TTL %d)\n", neigh_str, orig_str, ((struct packet *)&in)->seqno, ((struct packet *)&in)->ttl );
is_my_addr = is_my_orig = is_broadcast = is_duplicate = is_bidirectional = forward_duplicate_packet = 0;
@@ -1189,53 +1139,49 @@ int batman()
}
- if ( debug_level == 4 ) {
+ addr_to_string( ((struct packet *)&in)->orig, orig_str, sizeof (orig_str) );
+ addr_to_string( neigh, neigh_str, sizeof (neigh_str) );
+ debug_output( 4, "new packet - orig: %s, sender: %s\n", orig_str, neigh_str );
- addr_to_string( ((struct packet *)&in)->orig, orig_str, sizeof (orig_str) );
- addr_to_string( neigh, neigh_str, sizeof (neigh_str) );
- output( "new packet - orig: %s, sender: %s\n",orig_str , neigh_str );
+ /*if ( is_duplicate )
+ output("Duplicate packet \n");
- /*if ( is_duplicate )
- output("Duplicate packet \n");
+ if ( in.orig == neigh )
+ output("Originator packet from neighbour \n");
- if ( in.orig == neigh )
- output("Originator packet from neighbour \n");
+ if ( is_my_orig == 1 )
+ output("Originator packet from myself (via neighbour) \n");
- if ( is_my_orig == 1 )
- output("Originator packet from myself (via neighbour) \n");
+ if ( in.flags & UNIDIRECTIONAL )
+ output("Packet with unidirectional flag \n");
- if ( in.flags & UNIDIRECTIONAL )
- output("Packet with unidirectional flag \n");
+ if ( is_bidirectional )
+ output("received via bidirectional link \n");
- if ( is_bidirectional )
- output("received via bidirectional link \n");
+ if ( !( in.flags & UNIDIRECTIONAL ) && ( !is_bidirectional ) )
+ output("neighbour thinks connection is bidirectional - I disagree \n");*/
- if ( !( in.flags & UNIDIRECTIONAL ) && ( !is_bidirectional ) )
- output("neighbour thinks connection is bidirectional - I disagree \n");*/
+ if ( ((struct packet *)&in)->gwflags != 0 )
+ debug_output( 4, "Is an internet gateway (class %i) \n", ((struct packet *)&in)->gwflags );
- if ( ((struct packet *)&in)->gwflags != 0 )
- output( "Is an internet gateway (class %i) \n", ((struct packet *)&in)->gwflags );
+ if ( hna_buff_len > 4 ) {
- if ( hna_buff_len > 4 ) {
+ debug_output( 4, "HNA information received (%i HNA network%s):\n", hna_buff_len / 5, ( hna_buff_len / 5 > 1 ? "s": "" ) );
+ hna_buff_count = 0;
- output( "HNA information received (%i HNA network%s):\n", hna_buff_len / 5, ( hna_buff_len / 5 > 1 ? "s": "" ) );
- hna_buff_count = 0;
+ while ( ( hna_buff_count + 1 ) * 5 <= hna_buff_len ) {
- while ( ( hna_buff_count + 1 ) * 5 <= hna_buff_len ) {
+ memmove( &hna, ( unsigned int *)&hna_recv_buff[ hna_buff_count * 5 ], 4 );
+ netmask = ( unsigned int )hna_recv_buff[ ( hna_buff_count * 5 ) + 4 ];
- 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) );
- addr_to_string( hna, orig_str, sizeof (orig_str) );
+ if ( ( netmask > 0 ) && ( netmask < 33 ) )
+ debug_output( 4, "hna: %s/%i\n", orig_str, netmask );
+ else
+ debug_output( 4, "hna: %s/%i -> ignoring (invalid netmask)\n", orig_str, netmask );
- if ( ( netmask > 0 ) && ( netmask < 33 ) )
- output( "hna: %s/%i\n", orig_str, netmask );
- else
- output( "hna: %s/%i -> ignoring (invalid netmask)\n", orig_str, netmask );
-
- hna_buff_count++;
-
- }
+ hna_buff_count++;
}
@@ -1244,22 +1190,15 @@ int batman()
if ( ((struct packet *)&in)->version != COMPAT_VERSION ) {
- if ( debug_level == 4 )
- output( "Drop packet: incompatible batman version (%i) \n", ((struct packet *)&in)->version );
+ debug_output( 4, "Drop packet: incompatible batman version (%i) \n", ((struct packet *)&in)->version );
} else if ( is_my_addr ) {
- if ( debug_level == 4 ) {
- addr_to_string( neigh, neigh_str, sizeof (neigh_str) );
- output( "Drop packet: received my own broadcast (sender: %s)\n", neigh_str );
- }
+ debug_output( 4, "Drop packet: received my own broadcast (sender: %s)\n", neigh_str );
} else if ( is_broadcast ) {
- if ( debug_level == 4 ) {
- addr_to_string( neigh, neigh_str, sizeof (neigh) );
- output( "Drop packet: ignoring all packets with broadcast source IP (sender: %s)\n", neigh_str );
- }
+ debug_output( 4, "Drop packet: ignoring all packets with broadcast source IP (sender: %s)\n", neigh_str );
} else if ( is_my_orig ) {
@@ -1272,18 +1211,15 @@ int batman()
orig_neigh_node->bidirect_link[if_incoming->if_num] = get_time();
- if ( debug_level == 4 )
- output( "received my own packet from neighbour indicating bidirectional link, updating bidirect_link timestamp \n");
+ debug_output( 4, "received my own packet from neighbour indicating bidirectional link, updating bidirect_link timestamp \n");
}
- if ( debug_level == 4 )
- output( "Drop packet: originator packet from myself (via neighbour) \n" );
+ debug_output( 4, "Drop packet: originator packet from myself (via neighbour) \n" );
} else if ( ((struct packet *)&in)->flags & UNIDIRECTIONAL ) {
- if ( debug_level == 4 )
- output( "Drop packet: originator packet with unidirectional flag \n" );
+ debug_output( 4, "Drop packet: originator packet with unidirectional flag \n" );
} else {
@@ -1307,8 +1243,7 @@ int batman()
/* mark direct link on incoming interface */
schedule_forward_packet( (struct packet *)&in, 0, 1, orig_neigh_node, neigh, hna_recv_buff, hna_buff_len, if_incoming );
- if ( debug_level == 4 )
- output( "Forward packet: rebroadcast neighbour packet with direct link flag \n" );
+ debug_output( 4, "Forward packet: rebroadcast neighbour packet with direct link flag \n" );
/* if an unidirectional neighbour sends us a packet - retransmit it with unidirectional flag to tell him that we get its packets */
/* if a bidirectional neighbour sends us a packet - retransmit it with unidirectional flag if it is not our best link to it in order to prevent routing problems */
@@ -1316,8 +1251,7 @@ int batman()
schedule_forward_packet( (struct packet *)&in, 1, 1, orig_neigh_node, neigh, hna_recv_buff, hna_buff_len, if_incoming );
- if ( debug_level == 4 )
- output( "Forward packet: rebroadcast neighbour packet with direct link and unidirectional flag \n" );
+ debug_output( 4, "Forward packet: rebroadcast neighbour packet with direct link and unidirectional flag \n" );
}
@@ -1330,8 +1264,7 @@ int batman()
schedule_forward_packet( (struct packet *)&in, 0, 0, orig_neigh_node, neigh, hna_recv_buff, hna_buff_len, if_incoming );
- if ( debug_level == 4 )
- output( "Forward packet: rebroadcast orginator packet \n" );
+ debug_output( 4, "Forward packet: rebroadcast orginator packet \n" );
} else if ( orig_neigh_node->router->addr == neigh ) {
@@ -1355,27 +1288,23 @@ int batman()
schedule_forward_packet( (struct packet *)&in, 0, 0, orig_neigh_node, neigh, hna_recv_buff, hna_buff_len, if_incoming );
- if ( debug_level == 4 )
- output( "Forward packet: duplicate packet received via best neighbour with best ttl \n" );
+ debug_output( 4, "Forward packet: duplicate packet received via best neighbour with best ttl \n" );
} else {
- if ( debug_level == 4 )
- output( "Drop packet: duplicate packet received via best neighbour but not best ttl \n" );
+ debug_output( 4, "Drop packet: duplicate packet received via best neighbour but not best ttl \n" );
}
} else {
- if ( debug_level == 4 )
- output( "Drop packet: duplicate packet (not received via best neighbour) \n" );
+ debug_output( 4, "Drop packet: duplicate packet (not received via best neighbour) \n" );
}
} else {
- if ( debug_level == 4 )
- output( "Drop packet: received via unidirectional link \n" );
+ debug_output( 4, "Drop packet: received via unidirectional link \n" );
}
@@ -1398,8 +1327,7 @@ int batman()
debug_timeout = get_time();
- if ( ( debug_level != 0 ) && ( debug_level != 3 ) )
- debug( 1, ( debug_level == 4 ? 0 : 1 ), ( debug_level == 4 ? 1 : 0 ), ( debug_level == 2 ? 1 : 0 ) );
+ debug();
checkIntegrity();
diff --git a/batman.h b/batman.h
index a8681c5..a00a017 100644
--- a/batman.h
+++ b/batman.h
@@ -69,14 +69,13 @@ extern unsigned char *hna_buff;
extern struct gw_node *curr_gateway;
pthread_t curr_gateway_thread_id;
-extern pthread_mutex_t data_mutex;
-
extern short found_ifs;
extern struct list_head if_list;
extern struct list_head hna_list;
extern struct vis_if vis_if;
extern struct unix_if unix_if;
+extern struct debug_clients debug_clients;
struct packet
{
@@ -180,9 +179,19 @@ struct unix_if {
struct unix_client {
struct list_head list;
int sock;
+ char debug_level;
struct sockaddr_un addr;
};
+struct debug_clients {
+ void *fd_list[4];
+ short clients_num[4];
+};
+
+struct debug_level_info {
+ struct list_head list;
+ int fd;
+};
int batman();
diff --git a/bsd.c b/bsd.c
index fb29464..9410828 100644
--- a/bsd.c
+++ b/bsd.c
@@ -160,7 +160,7 @@ void add_del_route( unsigned int dest, unsigned int netmask, unsigned int router
#endif
}
- output("%s route to %s via %s\n", del ? "Deleting" : "Adding", str1, str2);
+ debug_output( 4, "%s route to %s via %s\n", del ? "Deleting" : "Adding", str1, str2 );
rt_sock = socket(PF_ROUTE, SOCK_RAW, AF_INET);
if (rt_sock < 0)
@@ -262,7 +262,7 @@ int probe_tun()
close(fd);
return 1;
*/
- do_log ("tun-support is broken on this platform...\n", "foo");
+ debug_output( 0, "tun-support is broken on this platform...\n" );
return 0;
}
diff --git a/linux-specific.c b/linux-specific.c
index 7eefca4..693e51f 100644
--- a/linux-specific.c
+++ b/linux-specific.c
@@ -46,7 +46,7 @@
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];
+ char str1[16], str2[16];
struct sockaddr_in *addr;
inet_ntop(AF_INET, &dest, str1, sizeof (str1));
@@ -78,40 +78,29 @@ void add_del_route( unsigned int dest, unsigned int netmask, unsigned int router
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);
- }
+ if ( debug_level > 2 )
+ debug_output( debug_level, "%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);
- }
+ if ( debug_level > 2 )
+ debug_output( debug_level, "%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);
- }
+ if ( debug_level > 2 )
+ debug_output( debug_level, "%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) );
- }
+ if ( ioctl( sock, del ? SIOCDELRT : SIOCADDRT, &route ) < 0 )
+ debug_output( 0, "Error - can't %s route to %s/%i via %s: %s\n", del ? "delete" : "add", str1, netmask, str2, strerror(errno) );
}
@@ -124,7 +113,7 @@ int probe_tun()
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) );
+ debug_output( 0, "Error - could not open '/dev/net/tun' ! Is the tun kernel module loaded ?\n" );
return 0;
}
@@ -139,7 +128,7 @@ int del_dev_tun( int fd ) {
if ( ioctl( fd, TUNSETPERSIST, 0 ) < 0 ) {
- do_log( "Error - can't delete tun device: %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't delete tun device: %s\n", strerror(errno) );
return -1;
}
@@ -164,14 +153,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 ) {
- do_log( "Error - can't create tun device (/dev/net/tun): %s\n", strerror(errno) );
+ debug_output( 0, "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) );
+ debug_output( 0, "Error - can't create tun device (TUNSETIFF): %s\n", strerror(errno) );
close(*fd);
return -1;
@@ -179,7 +168,7 @@ int add_dev_tun( struct batman_if *batman_if, unsigned int tun_addr, char *tun_d
if ( ioctl( *fd, TUNSETPERSIST, 1 ) < 0 ) {
- do_log( "Error - can't create tun device (TUNSETPERSIST): %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't create tun device (TUNSETPERSIST): %s\n", strerror(errno) );
close(*fd);
return -1;
@@ -189,7 +178,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 ) {
- do_log( "Error - can't create tun device (udp socket): %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't create tun device (udp socket): %s\n", strerror(errno) );
del_dev_tun( *fd );
return -1;
}
@@ -204,7 +193,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 ) {
- do_log( "Error - can't create tun device (SIOCSIFADDR): %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't create tun device (SIOCSIFADDR): %s\n", strerror(errno) );
del_dev_tun( *fd );
close( tmp_fd );
return -1;
@@ -214,7 +203,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 ) {
- do_log( "Error - can't create tun device (SIOCGIFFLAGS): %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't create tun device (SIOCGIFFLAGS): %s\n", strerror(errno) );
del_dev_tun( *fd );
close( tmp_fd );
return -1;
@@ -226,7 +215,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 ) {
- do_log( "Error - can't create tun device (SIOCSIFFLAGS): %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't create tun device (SIOCSIFFLAGS): %s\n", strerror(errno) );
del_dev_tun( *fd );
close( tmp_fd );
return -1;
@@ -238,7 +227,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 ) {
- do_log( "Error - can't create tun device (SIOCGIFMTU): %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't create tun device (SIOCGIFMTU): %s\n", strerror(errno) );
del_dev_tun( *fd );
close( tmp_fd );
return -1;
@@ -248,7 +237,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 ) {
- do_log( "Warning - MTU smaller than 100 -> can't reduce MTU anymore\n", strerror(errno) );
+ debug_output( 0, "Warning - MTU smaller than 100 -> can't reduce MTU anymore\n", strerror(errno) );
} else {
@@ -256,7 +245,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 ) {
- do_log( "Error - can't create tun device (SIOCSIFMTU): %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't create tun device (SIOCSIFMTU): %s\n", strerror(errno) );
del_dev_tun( *fd );
close( tmp_fd );
return -1;
diff --git a/linux.c b/linux.c
index 20735c7..efebbd4 100644
--- a/linux.c
+++ b/linux.c
@@ -122,7 +122,7 @@ int bind_to_iface( int udp_recv_sock, char *dev ) {
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) );
+ debug_output( 0, "Cannot bind socket to device %s : %s \n", dev, strerror(errno) );
return -1;
}
diff --git a/os.h b/os.h
index 702a1b6..0bb4530 100644
--- a/os.h
+++ b/os.h
@@ -30,7 +30,6 @@ int get_rp_filter(char *dev);
-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);
@@ -43,7 +42,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 );
void apply_init_args( int argc, char *argv[] );
void init_interface ( struct batman_if *batman_if );
@@ -55,11 +53,8 @@ void *gw_listen( void *arg );
void *client_to_gw_tun( void *arg );
-void debug( int fd, char send_clear, char max_output, char show_gw );
-
-//int is_aborted();
-
-//static void handler(int sig);
+void debug();
+void debug_output( short debug_prio, char *format, ... );
diff --git a/posix-specific.c b/posix-specific.c
index da41488..80d4729 100644
--- a/posix-specific.c
+++ b/posix-specific.c
@@ -17,6 +17,9 @@
*
*/
+
+
+#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
@@ -42,10 +45,80 @@
#include "allocate.h"
+void debug_output( short debug_prio, char *format, ... ) {
+
+ struct list_head *debug_pos;
+ struct debug_level_info *debug_level_info;
+ short debug_prio_intern;
+ va_list args;
+
+
+ if ( debug_prio == 0 ) {
+
+ if ( debug_level == 0 ) {
+
+ va_start( args, format );
+ vsyslog( LOG_ERR, format, args );
+ va_end( args );
+
+ } else if ( ( debug_level == 3 ) || ( debug_level == 4 ) ) {
+
+ if ( debug_level == 4 )
+ printf( "[%10u] ", get_time() );
+
+ va_start( args, format );
+ vprintf( format, args );
+ va_end( args );
+
+ }
+
+ debug_prio_intern = 3;
+
+ } else {
+
+ debug_prio_intern = debug_prio - 1;
+
+ }
+
+ if ( debug_clients.clients_num[ debug_prio_intern ] > 0 ) {
+
+ va_start( args, format );
+
+ list_for_each( debug_pos, (struct list_head *)debug_clients.fd_list[ debug_prio_intern ] ) {
+
+ debug_level_info = list_entry(debug_pos, struct debug_level_info, list);
+
+ if ( debug_level_info->fd == 0 )
+ continue;
+
+ if ( debug_prio_intern == 3 )
+ dprintf( debug_level_info->fd, "[%10u] ", get_time() );
+
+ if ( ( ( debug_level == 1 ) || ( debug_level == 2 ) ) && ( debug_level_info->fd == 1 ) && ( strcmp( format, "BOD\n" ) == 0 ) ) {
+
+ system( "clear" );
+
+ } else {
+
+ vdprintf( debug_level_info->fd, format, args );
+
+ }
+
+ }
+
+ va_end( args );
+
+ }
+
+}
+
+
+
void *unix_listen( void *arg ) {
struct unix_client *unix_client;
- struct list_head *unix_pos, *unix_pos_tmp;
+ struct debug_level_info *debug_level_info;
+ struct list_head *unix_pos, *unix_pos_tmp, *debug_pos, *debug_pos_tmp;
struct timeval tv;
int res, status, max_sock;
unsigned char buff[1500];
@@ -77,7 +150,7 @@ void *unix_listen( void *arg ) {
memset( unix_client, 0, sizeof(struct unix_client) );
if ( ( unix_client->sock = accept( unix_if.unix_sock, (struct sockaddr *)&unix_client->addr, &sun_size) ) == -1 ) {
- do_log( "Error - can't accept unix client: %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't accept unix client: %s\n", strerror(errno) );
continue;
}
@@ -89,8 +162,7 @@ void *unix_listen( void *arg ) {
list_add_tail(&unix_client->list, &unix_if.client_list);
- if ( debug_level == 3 )
- printf( "unix socket: got connection\n" );
+ debug_output( 3, "Unix socket: got connection\n" );
/* client sent data */
} else {
@@ -110,14 +182,46 @@ void *unix_listen( void *arg ) {
if ( unix_client->sock > max_sock )
max_sock = unix_client->sock;
- /* if ( debug_level == 3 )
- printf( "gateway: client sent data via unix socket: %s\n", buff ); */
+ /* debug_output( 3, "gateway: client sent data via unix socket: %s\n", buff ); */
+
+ if ( ( buff[2] == '1' ) || ( buff[2] == '2' ) || ( buff[2] == '3' ) || ( buff[2] == '4' ) ) {
+
+ if ( unix_client->debug_level != 0 ) {
+
+ list_for_each_safe( debug_pos, debug_pos_tmp, (struct list_head *)&debug_clients.fd_list[ (int)unix_client->debug_level - '1' ] ) {
+
+ debug_level_info = list_entry(debug_pos, struct debug_level_info, list);
+
+ if ( debug_level_info->fd == unix_client->sock ) {
+
+ list_del( debug_pos );
+ debug_clients.clients_num[ (int)unix_client->debug_level - '1' ]--;
+
+ debugFree( debug_pos, 263 );
+
+ break;
+
+ }
+
+ }
+
+ }
+
+ if ( unix_client->debug_level != buff[2] ) {
- if ( ( buff[2] == '1' ) || ( buff[2] == '2' ) ) {
+ debug_level_info = debugMalloc( sizeof(struct debug_level_info), 57 );
+ INIT_LIST_HEAD( &debug_level_info->list );
+ debug_level_info->fd = unix_client->sock;
+ list_add( &debug_level_info->list, (struct list_head *)debug_clients.fd_list[ (int)buff[2] - '1' ] );
+ debug_clients.clients_num[ (int)buff[2] - '1' ]++;
- pthread_mutex_lock( &data_mutex );
- debug( unix_client->sock, 0, 0, ( buff[2] == '2' ? 1 : 0 ) );
- pthread_mutex_unlock( &data_mutex );
+ unix_client->debug_level = (int)buff[2];
+
+ } else {
+
+ unix_client->debug_level = 0;
+
+ }
}
@@ -125,12 +229,32 @@ void *unix_listen( void *arg ) {
if ( status < 0 ) {
- do_log( "Error - can't read unix message: %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't read unix message: %s\n", strerror(errno) );
} else {
- if ( debug_level == 3 )
- printf( "Unix client closed connection ...\n" );
+ if ( unix_client->debug_level != 0 ) {
+
+ list_for_each_safe( debug_pos, debug_pos_tmp, (struct list_head *)&debug_clients.fd_list[ (int)unix_client->debug_level - '1' ] ) {
+
+ debug_level_info = list_entry(debug_pos, struct debug_level_info, list);
+
+ if ( debug_level_info->fd == unix_client->sock ) {
+
+ list_del( debug_pos );
+ debug_clients.clients_num[ (int)unix_client->debug_level - '1' ]--;
+
+ debugFree( debug_pos, 264 );
+
+ break;
+
+ }
+
+ }
+
+ }
+
+ debug_output( 3, "Unix client closed connection ...\n" );
}
@@ -155,7 +279,7 @@ void *unix_listen( void *arg ) {
} else if ( ( res < 0 ) && ( errno != EINTR ) ) {
- do_log( "Error - can't select: %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't select: %s\n", strerror(errno) );
break;
}
@@ -166,6 +290,27 @@ void *unix_listen( void *arg ) {
unix_client = list_entry(unix_pos, struct unix_client, list);
+ if ( unix_client->debug_level != 0 ) {
+
+ list_for_each_safe( debug_pos, debug_pos_tmp, (struct list_head *)&debug_clients.fd_list[ (int)unix_client->debug_level - '1' ] ) {
+
+ debug_level_info = list_entry(debug_pos, struct debug_level_info, list);
+
+ if ( debug_level_info->fd == unix_client->sock ) {
+
+ list_del( debug_pos );
+ debug_clients.clients_num[ (int)unix_client->debug_level - '1' ]--;
+
+ debugFree( debug_pos, 265 );
+
+ break;
+
+ }
+
+ }
+
+ }
+
list_del( unix_pos );
debugFree( unix_pos, 241 );
@@ -182,12 +327,14 @@ void apply_init_args( int argc, char *argv[] ) {
struct in_addr tmp_ip_holder;
struct batman_if *batman_if;
struct hna_node *hna_node;
- struct timeval tv;
- short found_args = 1, unix_client = 0, batch_mode = 0, netmask;
+ struct debug_level_info *debug_level_info;
+// struct timeval tv;
+ short found_args = 1, unix_client = 0, batch_mode = 0, batch_counter = 0, netmask;
+
int optchar, res, recv_buff_len;
- char str1[16], str2[16], *slash_ptr, unix_string[100], buff[1500];
+ char str1[16], str2[16], *slash_ptr, unix_string[100], buff[1500], *buff_ptr, *cr_ptr;
unsigned int vis_server = 0;
- fd_set wait_sockets, tmp_wait_sockets;
+// fd_set wait_sockets, tmp_wait_sockets;
memset(&tmp_ip_holder, 0, sizeof (struct in_addr));
@@ -416,6 +563,15 @@ void apply_init_args( int argc, char *argv[] ) {
exit(EXIT_FAILURE);
}
+ for ( res = 0; res < 4; res++ ) {
+
+ debug_clients.fd_list[res] = debugMalloc( sizeof(struct list_head), 97 );
+ INIT_LIST_HEAD( (struct list_head *)debug_clients.fd_list[res] );
+
+ }
+
+ memset( &debug_clients.clients_num, 0, sizeof(debug_clients.clients_num) );
+
/* daemonize */
if ( debug_level == 0 ) {
@@ -433,14 +589,20 @@ void apply_init_args( int argc, char *argv[] ) {
printf( "B.A.T.M.A.N.-III v%s (compability version %i)\n", SOURCE_VERSION, COMPAT_VERSION );
+ debug_clients.clients_num[ debug_level - 1 ]++;
+ debug_level_info = debugMalloc( sizeof(struct debug_level_info), 39 );
+ INIT_LIST_HEAD( &debug_level_info->list );
+ debug_level_info->fd = 1;
+ list_add( &debug_level_info->list, (struct list_head *)debug_clients.fd_list[ debug_level - 1 ] );
+
}
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 = 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;
@@ -453,7 +615,7 @@ void apply_init_args( int argc, char *argv[] ) {
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);
+ printf( "Using interface %s with address %s and broadcast address %s\n", batman_if->dev, str1, str2 );
if ( gateway_class != 0 ) {
@@ -487,13 +649,13 @@ void apply_init_args( int argc, char *argv[] ) {
strcpy( unix_if.addr.sun_path, UNIX_PATH );
if ( bind ( unix_if.unix_sock, (struct sockaddr *)&unix_if.addr, sizeof (struct sockaddr_un) ) < 0 ) {
- do_log( "Error - can't bind unix socket: %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't bind unix socket: %s\n", strerror(errno) );
close_all_sockets();
exit(EXIT_FAILURE);
}
if ( listen( unix_if.unix_sock, 10 ) < 0 ) {
- do_log( "Error - can't listen unix socket: %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't listen unix socket: %s\n", strerror(errno) );
close_all_sockets();
exit(EXIT_FAILURE);
}
@@ -503,7 +665,7 @@ void apply_init_args( int argc, char *argv[] ) {
if ( debug_level > 0 ) {
- printf("debug level: %i\n", debug_level);
+ printf( "debug level: %i\n", debug_level );
if ( orginator_interval != 1000 )
printf( "orginator interval: %i\n", orginator_interval );
@@ -529,7 +691,10 @@ void apply_init_args( int argc, char *argv[] ) {
/* connect to running batmand via unix socket */
} else {
- if ( ( debug_level == 1 ) || ( debug_level == 2 ) ) {
+ if ( ( debug_level == 1 ) || ( debug_level == 2 ) || ( debug_level == 3 ) || ( debug_level == 4 ) ) {
+
+ if ( ( ( debug_level == 3 ) || ( debug_level == 4 ) ) && ( batch_mode ) )
+ printf( "WARNING: Your chosen debug level (%i) does not support batch mode !\n", debug_level );
unix_if.unix_sock = socket(AF_LOCAL, SOCK_STREAM, 0);
@@ -548,66 +713,125 @@ void apply_init_args( int argc, char *argv[] ) {
snprintf( unix_string, sizeof( unix_string ), "d:%i", debug_level );
- FD_ZERO(&wait_sockets);
- FD_SET(unix_if.unix_sock, &wait_sockets);
+// FD_ZERO(&wait_sockets);
+// FD_SET(unix_if.unix_sock, &wait_sockets);
- while ( 1 ) {
+ if ( write( unix_if.unix_sock, unix_string, strlen( unix_string ) ) < 0 ) {
- if ( write( unix_if.unix_sock, unix_string, strlen( unix_string ) ) < 0 ) {
+ printf( "Error - can't write to unix socket: %s\n", strerror(errno) );
+ close( unix_if.unix_sock );
+ exit(EXIT_FAILURE);
- printf( "Error - can't write to unix socket: %s\n", strerror(errno) );
- close( unix_if.unix_sock );
- exit(EXIT_FAILURE);
+ }
- }
+ while ( ( recv_buff_len = read( unix_if.unix_sock, buff, sizeof( buff ) ) ) > 0 ) {
- if ( ! batch_mode )
- system( "clear" );
+ buff_ptr = buff;
- while ( 1 ) {
+ while ( ( cr_ptr = strchr( buff_ptr, '\n' ) ) != NULL ) {
- tv.tv_sec = 1;
- tv.tv_usec = 0;
- tmp_wait_sockets = wait_sockets;
+ *cr_ptr = '\0';
- res = select( unix_if.unix_sock + 1, &tmp_wait_sockets, NULL, NULL, &tv );
+ if ( strcmp( buff_ptr, "BOD" ) == 0 ) {
- if ( res > 0 ) {
+ if ( batch_mode ) {
- if ( ( recv_buff_len = read( unix_if.unix_sock, buff, sizeof( buff ) ) ) < 0 ) {
+ if ( batch_counter ) {
- printf( "Error - can't read from unix socket: %s\n", strerror(errno) );
- close( unix_if.unix_sock );
- exit(EXIT_FAILURE);
+ close( unix_if.unix_sock );
+ exit(EXIT_SUCCESS);
- } else if ( recv_buff_len > 0 ) {
+ }
- printf( "%s", buff );
+ batch_counter++;
- }
+ } else {
- /* timeout reached */
- } else if ( res == 0 ) {
+ system( "clear" );
- break;
+ }
- } else if ( ( res < 0 ) && ( errno != EINTR ) ) {
+ } else {
- printf( "Error - can't select: %s\n", strerror(errno) );
- close( unix_if.unix_sock );
- exit(EXIT_FAILURE);
+ printf( "%s\n", buff_ptr );
}
+ buff_ptr = cr_ptr + 1;
+
}
- if ( batch_mode )
- break;
+ }
+
+ if ( recv_buff_len < 0 ) {
- sleep( 1 );
+ printf( "Error - can't read from unix socket: %s\n", strerror(errno) );
+ close( unix_if.unix_sock );
+ exit(EXIT_FAILURE);
+
+ } else {
+
+ printf( "Connection terminated by remote host\n" );
}
+// while ( 1 ) {
+//
+// if ( write( unix_if.unix_sock, unix_string, strlen( unix_string ) ) < 0 ) {
+//
+// printf( "Error - can't write to unix socket: %s\n", strerror(errno) );
+// close( unix_if.unix_sock );
+// exit(EXIT_FAILURE);
+//
+// }
+//
+// if ( ! batch_mode )
+// system( "clear" );
+//
+// while ( 1 ) {
+//
+// tv.tv_sec = 1;
+// tv.tv_usec = 0;
+// tmp_wait_sockets = wait_sockets;
+//
+// res = select( unix_if.unix_sock + 1, &tmp_wait_sockets, NULL, NULL, &tv );
+//
+// if ( res > 0 ) {
+//
+// if ( ( recv_buff_len = read( unix_if.unix_sock, buff, sizeof( buff ) ) ) < 0 ) {
+//
+// printf( "Error - can't read from unix socket: %s\n", strerror(errno) );
+// close( unix_if.unix_sock );
+// exit(EXIT_FAILURE);
+//
+// } else if ( recv_buff_len > 0 ) {
+//
+// printf( "%s", buff );
+//
+// }
+//
+// /* timeout reached */
+// } else if ( res == 0 ) {
+//
+// break;
+//
+// } else if ( ( res < 0 ) && ( errno != EINTR ) ) {
+//
+// printf( "Error - can't select: %s\n", strerror(errno) );
+// close( unix_if.unix_sock );
+// exit(EXIT_FAILURE);
+//
+// }
+//
+// }
+//
+// if ( batch_mode )
+// break;
+//
+// sleep( 1 );
+//
+// }
+
close( unix_if.unix_sock );
}
@@ -620,173 +844,155 @@ void apply_init_args( int argc, char *argv[] ) {
-
-void init_interface ( struct batman_if *batman_if )
+void init_interface ( struct batman_if *batman_if )
{
struct ifreq int_req;
short on = 1;
- 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);
- }
+ if ( strlen(batman_if->dev) > IFNAMSIZ - 1 ) {
+ debug_output( 0, "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_send_sock = socket(PF_INET, SOCK_DGRAM, 0);
+ if (batman_if->udp_send_sock < 0) {
+ debug_output( 0, "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 ) {
+ 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);
+ debug_output( 0, "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);
+ 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 ) {
+ 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);
+ debug_output( 0, "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;
+ 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 ) {
+ 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);
+ debug_output( 0, "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;
+ 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 ) {
+ 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);
+ debug_output( 0, "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 ) {
+ if ( bind_to_iface( batman_if->udp_send_sock, batman_if->dev ) < 0 ) {
- close_all_sockets();
- exit(EXIT_FAILURE);
+ close_all_sockets();
+ exit(EXIT_FAILURE);
- }
+ }
- if ( bind( batman_if->udp_send_sock, (struct sockaddr *)&batman_if->addr, sizeof (struct sockaddr_in) ) < 0 ) {
+ 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);
+ debug_output( 0, "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 ) {
+ if ( bind_to_iface( batman_if->udp_recv_sock, batman_if->dev ) < 0 ) {
- close_all_sockets();
- exit(EXIT_FAILURE);
+ close_all_sockets();
+ exit(EXIT_FAILURE);
- }
+ }
- if ( bind( batman_if->udp_recv_sock, (struct sockaddr *)&batman_if->broad, sizeof (struct sockaddr_in) ) < 0 ) {
+ 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);
+ debug_output( 0, "Error - can't bind receive socket: %s\n", strerror(errno) );
+ close_all_sockets();
+ exit(EXIT_FAILURE);
- }
+ }
}
-
+
void init_interface_gw ( struct batman_if *batman_if )
{
- short on = 1;
-
- batman_if->addr.sin_port = htons(PORT + 1);
+ short on = 1;
- batman_if->tcp_gw_sock = socket(PF_INET, SOCK_STREAM, 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 ( batman_if->tcp_gw_sock < 0 ) {
+ debug_output( 0, "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 ( setsockopt( batman_if->tcp_gw_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int) ) < 0 ) {
+ debug_output( 0, "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 ( bind( batman_if->tcp_gw_sock, (struct sockaddr*)&batman_if->addr, sizeof(struct sockaddr_in) ) < 0 ) {
+ debug_output( 0, "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);
- }
+ if ( listen( batman_if->tcp_gw_sock, 10 ) < 0 ) {
+ debug_output( 0, "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);
- }
+ batman_if->tunnel_sock = socket(PF_INET, SOCK_DGRAM, 0);
+ if ( batman_if->tunnel_sock < 0 ) {
+ debug_output( 0, "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);
- }
+ if ( bind( batman_if->tunnel_sock, (struct sockaddr *)&batman_if->addr, sizeof (struct sockaddr_in) ) < 0 ) {
+ debug_output( 0, "Error - can't bind tunnel socket: %s\n", strerror(errno) );
+ close_all_sockets();
+ exit(EXIT_FAILURE);
+ }
- batman_if->addr.sin_port = htons(PORT);
+ batman_if->addr.sin_port = htons(PORT);
- pthread_create(&batman_if->listen_thread_id, NULL, &gw_listen, batman_if);
+ pthread_create( &batman_if->listen_thread_id, NULL, &gw_listen, batman_if );
}
-
-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;
@@ -819,7 +1025,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 ) {
- do_log( "Error - can't create tcp socket: %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't create tcp socket: %s\n", strerror(errno) );
curr_gateway = NULL;
return NULL;
@@ -827,7 +1033,7 @@ void *client_to_gw_tun( void *arg ) {
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) );
+ debug_output( 0, "Error - can't connect to gateway: %s\n", strerror(errno) );
close( curr_gateway_tcp_sock );
gw_node->last_failure = get_time();
@@ -844,7 +1050,7 @@ 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 ) {
- do_log( "Error - can't create udp socket: %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't create udp socket: %s\n", strerror(errno) );
close( curr_gateway_tcp_sock );
curr_gateway = NULL;
return NULL;
@@ -853,7 +1059,7 @@ void *client_to_gw_tun( void *arg ) {
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) );
+ debug_output( 0, "Error - can't bind tunnel socket: %s\n", strerror(errno) );
close( curr_gateway_tcp_sock );
close( curr_gateway_tun_sock );
curr_gateway = NULL;
@@ -896,8 +1102,7 @@ void *client_to_gw_tun( void *arg ) {
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" );
+ debug_output( 3, "server_keepalive failed: %s\n", strerror(errno) );
gw_node->last_failure = get_time();
gw_node->unavail_factor++;
@@ -925,20 +1130,17 @@ void *client_to_gw_tun( void *arg ) {
if ( status > 0 ) {
- if ( debug_level == 3 )
- printf( "server message ?\n" );
+ debug_output( 3, "server message ?\n" );
} else if ( status < 0 ) {
- if ( debug_level == 3 )
- printf( "Cannot read message from gateway: %s\n", strerror(errno) );
+ debug_output( 3, "Cannot read message from gateway: %s\n", strerror(errno) );
break;
} else if (status == 0) {
- if ( debug_level == 3 )
- printf( "Gateway closed connection - timeout ?\n" );
+ debug_output( 3, "Gateway closed connection - timeout ?\n" );
gw_node->last_failure = get_time();
gw_node->unavail_factor++;
@@ -952,13 +1154,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 ) {
- do_log( "Error - can't receive packet: %s\n", strerror(errno) );
+ debug_output( 0, "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) );
+ debug_output( 0, "Error - can't write packet: %s\n", strerror(errno) );
}
@@ -968,12 +1170,12 @@ void *client_to_gw_tun( void *arg ) {
if ( ( buff_len = read( curr_gateway_tun_fd, buff, sizeof( buff ) ) ) < 0 ) {
- do_log( "Error - couldn't read data: %s\n", strerror(errno) );
+ debug_output( 0, "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) );
+ debug_output( 0, "Error - can't send to gateway: %s\n", strerror(errno) );
}
}
@@ -982,7 +1184,7 @@ void *client_to_gw_tun( void *arg ) {
} else if ( ( res < 0 ) && (errno != EINTR) ) {
- do_log( "Error - can't select: %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't select: %s\n", strerror(errno) );
break;
}
@@ -1019,7 +1221,7 @@ 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) );
+ debug_output( 0, "Error - couldn't spawn thread: %s\n", strerror(errno) );
curr_gateway = NULL;
}
@@ -1111,7 +1313,7 @@ int receive_packet( unsigned char *packet_buff, int packet_buff_len, int *hna_bu
break;
if ( errno != EINTR ) {
- do_log( "Error - can't select: %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't select: %s\n", strerror(errno) );
return -1;
}
}
@@ -1127,7 +1329,7 @@ int receive_packet( unsigned char *packet_buff, int packet_buff_len, int *hna_bu
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) );
+ debug_output( 0, "Error - can't receive packet: %s\n", strerror(errno) );
return -1;
}
@@ -1154,18 +1356,15 @@ int receive_packet( unsigned char *packet_buff, int packet_buff_len, int *hna_bu
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) );
+ debug_output( 0, "Error - can't send packet: %s.\nDoes your firewall allow outgoing packets on port %i ?\n", strerror(errno), ntohs( broad->sin_port ) );
} else {
- do_log( "Error - can't send packet: %s.\n", strerror(errno) );
+ debug_output( 0, "Error - can't send packet: %s.\n", strerror(errno) );
}
@@ -1199,7 +1398,7 @@ void *gw_listen( void *arg ) {
if ( inet_pton(AF_INET, tun_ip, &tmp_ip_holder) < 1 ) {
- do_log( "Error - invalid tunnel IP specified: %s\n", tun_ip );
+ debug_output( 0, "Error - invalid tunnel IP specified: %s\n", tun_ip );
exit(EXIT_FAILURE);
}
@@ -1239,7 +1438,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 ) {
- do_log( "Error - can't accept client packet: %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't accept client packet: %s\n", strerror(errno) );
continue;
}
@@ -1253,23 +1452,21 @@ void *gw_listen( void *arg ) {
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 );
- }
+ addr_to_string(gw_client->addr.sin_addr.s_addr, str2, sizeof (str2));
+ debug_output( 3, "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) );
+ debug_output( 0, "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) );
+ debug_output( 0, "Error - can't write packet: %s\n", strerror(errno) );
}
@@ -1280,7 +1477,7 @@ void *gw_listen( void *arg ) {
/* not needed - kernel knows client adress and routes traffic directly */
- do_log( "Warning - data coming through tun device: %s\n", tun_dev );
+ debug_output( 0, "Warning - data coming through tun device: %s\n", tun_dev );
/*if ( ( buff_len = read( tun_fd, buff, sizeof( buff ) ) ) < 0 ) {
@@ -1305,8 +1502,7 @@ void *gw_listen( void *arg ) {
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));
+ addr_to_string(gw_client->addr.sin_addr.s_addr, str2, sizeof (str2));
status = read( gw_client->sock, buff, sizeof( buff ) );
@@ -1317,21 +1513,17 @@ void *gw_listen( void *arg ) {
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 );
- }
+ debug_output( 3, "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) );
+ debug_output( 0, "Error - can't read message: %s\n", strerror(errno) );
} else {
- if ( debug_level == 3 )
- printf( "Client %s closed connection ...\n", str2 );
+ debug_output( 3, "Client %s closed connection ...\n", str2 );
}
@@ -1356,7 +1548,7 @@ void *gw_listen( void *arg ) {
} else if ( ( res < 0 ) && (errno != EINTR) ) {
- do_log( "Error - can't select: %s\n", strerror(errno) );
+ debug_output( 0, "Error - can't select: %s\n", strerror(errno) );
break;
}
@@ -1378,10 +1570,8 @@ void *gw_listen( void *arg ) {
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 );
- }
+ addr_to_string(gw_client->addr.sin_addr.s_addr, str2, sizeof (str2));
+ debug_output( 3, "gateway: client %s timeout on interface %s\n", str2, batman_if->dev );
list_del( client_pos );
debugFree( client_pos, 202 );
diff --git a/posix.c b/posix.c
index c78a8c3..e9faa77 100644
--- a/posix.c
+++ b/posix.c
@@ -82,17 +82,6 @@ unsigned int get_time(void)
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);
-}
-
/* batman animation */
--
batman; test conversation