Logo Search packages:      
Sourcecode: batmand version File versions  Download package

posix.c

/*
 * Copyright (C) 2006 BATMAN contributors:
 * Thomas Lopatic, Marek Lindner
 * 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.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 *
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#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>
#include <errno.h>
#include <stdarg.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <syslog.h>

#include "os.h"
#include "batman-specific.h"




#define BAT_LOGO_PRINT(x,y,z) printf( "\x1B[%i;%iH%c", y + 1, x, z )                      /* write char 'z' into column 'x', row 'y' */
#define BAT_LOGO_END(x,y) printf("\x1B[8;0H");fflush(NULL);bat_wait( x, y );              /* end of current picture */


extern struct vis_if vis_if;

static struct timeval start_time;

static uint32_t last_time_sec = 0;
static uint32_t corrected_start_time_sec = 0;


static void get_time_internal( struct timeval *tv ) {

      int32_t sec, 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;

}

uint32_t get_time( void ) {

      struct timeval tv;

      get_time_internal(&tv);

      return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

uint32_t get_uptime_sec( void ) {
      struct timeval tv;

// Inititialization of corrected_start_time_sec and last_uptime_sec:

      gettimeofday( &tv, NULL );

      if ( corrected_start_time_sec == 0 ) 
            corrected_start_time_sec = tv.tv_sec; 

      if ( last_time_sec == 0 ) 
            last_time_sec = tv.tv_sec; 


//Checking for large time drifts (greater 10 secs since last call) and correcting to previous uptime:

      if( last_time_sec + MAX_TIME_DRIFT_SEC < tv.tv_sec ) {

            corrected_start_time_sec = corrected_start_time_sec + ( tv.tv_sec - last_time_sec );

      } else if ( last_time_sec > tv.tv_sec + MAX_TIME_DRIFT_SEC ) {

            corrected_start_time_sec = corrected_start_time_sec - ( last_time_sec - tv.tv_sec );

      }

      last_time_sec = tv.tv_sec; 

      return ( tv.tv_sec - corrected_start_time_sec );
}


/* batman animation */
void sym_print( char x, char y, char *z ) {

      char i = 0, Z;

      do{

            BAT_LOGO_PRINT( 25 + (int)x + (int)i, (int)y, z[(int)i] );

            switch ( z[(int)i] ) {

                  case 92:
                        Z = 47;   // "\" --> "/"
                        break;

                  case 47:
                        Z = 92;   // "/" --> "\"
                        break;

                  case 41:
                        Z = 40;   // ")" --> "("
                        break;

                  default:
                        Z = z[(int)i];
                        break;

            }

            BAT_LOGO_PRINT( 24 - (int)x - (int)i, (int)y, Z );
            i++;

      } while( z[(int)i - 1] );

      return;

}



void bat_wait( int32_t T, int32_t t ) {

      struct timeval time;

      time.tv_sec = T;
      time.tv_usec = ( t * 10000 );

      select( 0, NULL, NULL, NULL, &time );

      return;

}



void print_animation( void ) {

      system( "clear" );
      BAT_LOGO_END( 0, 50 );

      sym_print( 0, 3, "." );
      BAT_LOGO_END( 1, 0 );

      sym_print( 0, 4, "v" );
      BAT_LOGO_END( 0, 20 );

      sym_print( 1, 3, "^" );
      BAT_LOGO_END( 0, 20 );

      sym_print( 1, 4, "/" );
      sym_print( 0, 5, "/" );
      BAT_LOGO_END( 0, 10 );

      sym_print( 2, 3, "\\" );
      sym_print( 2, 5, "/" );
      sym_print( 0, 6, ")/" );
      BAT_LOGO_END( 0, 10 );

      sym_print( 2, 3, "_\\" );
      sym_print( 4, 4, ")" );
      sym_print( 2, 5, " /" );
      sym_print( 0, 6, " )/" );
      BAT_LOGO_END( 0, 10 );

      sym_print( 4, 2, "'\\" );
      sym_print( 2, 3, "__/ \\" );
      sym_print( 4, 4, "   )" );
      sym_print( 1, 5, "   " );
      sym_print( 2, 6, "   /" );
      sym_print( 3, 7, "\\" );
      BAT_LOGO_END( 0, 15 );

      sym_print( 6, 3, " \\" );
      sym_print( 3, 4, "_ \\   \\" );
      sym_print( 10, 5, "\\" );
      sym_print( 1, 6, "          \\" );
      sym_print( 3, 7, " " );
      BAT_LOGO_END( 0, 20 );

      sym_print( 7, 1, "____________" );
      sym_print( 7, 3, " _   \\" );
      sym_print( 3, 4, "_      " );
      sym_print( 10, 5, " " );
      sym_print( 11, 6, " " );
      BAT_LOGO_END( 0, 25 );

      sym_print( 3, 1, "____________    " );
      sym_print( 1, 2, "'|\\   \\" );
      sym_print( 2, 3, " /         " );
      sym_print( 3, 4, " " );
      BAT_LOGO_END( 0, 25 );

      sym_print( 3, 1, "    ____________" );
      sym_print( 1, 2, "    '\\   " );
      sym_print( 2, 3, "__/  _   \\" );
      sym_print( 3, 4, "_" );
      BAT_LOGO_END( 0, 35 );

      sym_print( 7, 1, "            " );
      sym_print( 7, 3, " \\   " );
      sym_print( 5, 4, "\\    \\" );
      sym_print( 11, 5, "\\" );
      sym_print( 12, 6, "\\" );
      BAT_LOGO_END( 0 ,35 );

}



void addr_to_string( uint32_t addr, char *str, int32_t len ) {

      inet_ntop( AF_INET, &addr, str, len );

}



int32_t rand_num( int32_t limit ) {

      return ( limit == 0 ? 0 : rand() % limit );

}



int main( int argc, char *argv[] ) {

      int8_t res;


      /* check if user is root */
      if ( ( getuid() ) || ( getgid() ) ) {

            fprintf( stderr, "Error - you must be root to run %s !\n", argv[0] );
            exit(EXIT_FAILURE);

      }


      apply_init_args( argc, argv );


      gettimeofday( &start_time, NULL );
      srand( getpid() );

      res = batman();

      restore_defaults();
      cleanup();
      checkLeak();
      return res;

}

Generated by  Doxygen 1.6.0   Back to index