Main Page | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

crt_io.c

Go to the documentation of this file.
00001 /* Linux Prism II Stumbler - Utility Scan for 802_11 networks under Linux
00002  * 
00003  * File : crt_io.c
00004  * Project : WifiScanner (c) 2002 Hervé Schauer Consultants
00005  * Usage : This utility is written for use with IEEE 802.11 adapters based
00006  * on Intersil's PRISM II chipset (PCMCIA).
00007  * 
00008  * Base code was from prismstumbler Jan Fernquist <Jan.B.Fernquist@telia.com>
00009  * and wlanctl from www.linux-wlan.com
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  * 
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  * 
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00024  *
00025  * $Id: crt_io.c,v 1.12 2005/02/23 11:36:53 poggij Exp $
00026  */
00027 
00028 #include <include.h>
00029 #include <src/crt_io.h>
00030 #include <src/interface.h>
00031 #ifdef WITH_SYSLOG
00032 #include <syslog.h>
00033 #endif
00034 
00035 extern UINT8 CursesIsInUse;
00036 extern ConfigStruct config;
00037 
00038 #ifdef HAVE_LIBNCURSES
00039 extern WINDOW *RealTime_WND;
00040 #endif
00041 #ifdef WITH_THREAD
00042 extern pthread_mutex_t screen_mutex;
00043 #endif
00044 
00045 
00046 /*
00047  * Some internal forward refs
00048  */
00049 
00050 /*----------------------------------------------------------------
00051    Function to write a message and exit (fatal error)
00052    Write with and without curses
00053    exit curses environment before all thinks but after 3 secondes
00054      so we can read screen before death condition
00055 ----------------------------------------------------------------*/
00056 void fatal(char *fmt, ...)
00057 {
00058   va_list ap;
00059 
00060 #ifdef HAVE_LIBNCURSES
00061   if (CursesIsInUse == TRUE)
00062     RefreshAllWND();            // Refresh All Windows
00063   if (config.DebugLevel >= 1)
00064     sleep(3);                   // Sleep for 3 seconds
00065 #else
00066   fflush(stdout);               // Refresh stdout buffer
00067 #endif
00068   va_start(ap, fmt);
00069 #ifdef HAVE_LIBNCURSES
00070   if (CursesIsInUse == TRUE)
00071     EndCurses();
00072 #endif
00073   vfprintf(stderr, fmt, ap);
00074   va_end(ap);
00075   exit(EXIT_FAILURE);
00076 }
00077 
00078 /*----------------------------------------------------------------
00079    Function to write DEBUG informations
00080    Write with and without curses
00081    If DebugLevel is greater than 0 we refresh screen else not
00082 ----------------------------------------------------------------*/
00083 void debug(UINT8 DL, char *fmt, ...)
00084 {
00085   va_list ap;
00086 
00087   if (config.DebugLevel < DL)
00088     return;                     // Return if debug level is not enougth
00089   if (CursesIsInUse == FALSE)
00090     fflush(stdout);             // Refresh stdout buffer
00091   va_start(ap, fmt);
00092 #ifdef HAVE_LIBNCURSES
00093   if (CursesIsInUse == TRUE) {
00094 #ifdef WITH_THREAD
00095     pthread_mutex_lock(&screen_mutex);
00096 #endif
00097     vwprintw(RealTime_WND, fmt, ap);
00098     if (config.DebugLevel > 0)
00099       wrefresh(RealTime_WND);
00100 #ifdef WITH_THREAD
00101     pthread_mutex_unlock(&screen_mutex);
00102 #endif
00103   } else {
00104 #endif
00105     vfprintf(stderr, fmt, ap);
00106     fflush(stderr);
00107 #ifdef HAVE_LIBNCURSES
00108   }
00109 #endif
00110   va_end(ap);
00111 }
00112 
00113 /*----------------------------------------------------------------
00114    Function to write DEBUG informations with the time stamps in ms
00115    Write with and without curses
00116    If DebugLevel is greater than 0 we refresh screen else not
00117 ----------------------------------------------------------------*/
00118 void debugTS(UINT8 DL, char *fmt, ...)
00119 {
00120   va_list ap;
00121   struct timeb binary_now;
00122 
00123   //struct tm *ascii_now = NULL;
00124 
00125   ftime(&binary_now);
00126   //ascii_now = localtime (&binary_now.time);
00127 
00128   if (config.DebugLevel < DL)
00129     return;                     // Return if debug level is not enougth
00130   if (CursesIsInUse == FALSE)
00131     fflush(stdout);             // Refresh stdout buffer
00132   va_start(ap, fmt);
00133 #ifdef HAVE_LIBNCURSES
00134   if (CursesIsInUse == TRUE) {
00135 #ifdef WITH_THREAD
00136     pthread_mutex_lock(&screen_mutex);
00137 #endif
00138     wprintw(RealTime_WND, "%d.%03d:", (int) binary_now.time,
00139             binary_now.millitm);
00140     vwprintw(RealTime_WND, fmt, ap);
00141     if (config.DebugLevel > 0)
00142       wrefresh(RealTime_WND);
00143 #ifdef WITH_THREAD
00144     pthread_mutex_unlock(&screen_mutex);
00145 #endif
00146   } else {
00147 #endif
00148     vfprintf(stderr, fmt, ap);
00149     fflush(stderr);
00150 #ifdef HAVE_LIBNCURSES
00151   }
00152 #endif
00153   va_end(ap);
00154 }
00155 
00156 /*----------------------------------------------------------------
00157    Function to write WARNING informations
00158    Write with and without curses
00159 ----------------------------------------------------------------*/
00160 void warning(char *fmt, ...)
00161 {
00162   va_list ap;
00163 
00164   if (CursesIsInUse == FALSE)
00165     fflush(stdout);             // Refresh stdout buffer
00166   va_start(ap, fmt);
00167 #ifdef HAVE_LIBNCURSES
00168   if (CursesIsInUse == TRUE) {
00169 #ifdef WITH_THREAD
00170     pthread_mutex_lock(&screen_mutex);
00171 #endif
00172     vwprintw(RealTime_WND, fmt, ap);
00173     if (config.DebugLevel > 0)
00174       wrefresh(RealTime_WND);
00175 #ifdef WITH_THREAD
00176     pthread_mutex_unlock(&screen_mutex);
00177 #endif
00178   } else {
00179 #endif
00180     vfprintf(stderr, fmt, ap);
00181     fflush(stderr);
00182 #ifdef HAVE_LIBNCURSES
00183   }
00184 #endif
00185   va_end(ap);
00186 }
00187 
00188 /********************/
00189 void
00190 DumpHexPaquets(WINDOW * RealTime_WND, unsigned char msgbuf[], int recvlen)
00191 {
00192   int n, i, end;
00193 
00194 #ifdef WITH_THREAD
00195   pthread_mutex_lock(&screen_mutex);
00196 #endif
00197   wprintw(RealTime_WND, "MSG=\n");
00198 
00199   for (n = 0; n < recvlen; n += 16) {
00200     if (n > MAX_BUFFER_SIZE)
00201       break;                    // Please ... No core dump
00202     end = recvlen - n;
00203     if (end > 16)
00204       end = 16;
00205     wprintw(RealTime_WND, "0x%04X - ", n);
00206 
00207     // Print Hex part
00208     for (i = 0; i < 16; i++) {
00209       /* Write only packet, not garbage ;-) */
00210       if (i < end)
00211         wprintw(RealTime_WND, "%02X", (UINT8) msgbuf[n + i]);
00212       else
00213         wprintw(RealTime_WND, "  ");
00214       /* Separator every 2 bytes */
00215       if (!((i + 1) % 2))
00216         wprintw(RealTime_WND, " ");
00217     }
00218 
00219     // Print ASCI part
00220     for (i = 0; i < end; i++) {
00221       // Print caractere if it's printable
00222       if (msgbuf[n + i] >= 0x20 && msgbuf[n + i] <= 0x7E)
00223         wprintw(RealTime_WND, "%c", (char) msgbuf[n + i]);
00224       else
00225         wprintw(RealTime_WND, ".");
00226     }
00227     /* End of line each 16 bytes */
00228     wprintw(RealTime_WND, "\n");
00229   }
00230   wrefresh(RealTime_WND);
00231 #ifdef WITH_THREAD
00232   pthread_mutex_unlock(&screen_mutex);
00233 #endif
00234 }
00235 
00236 /*----------------------------------------------------------------
00237    Function to write IDS informations
00238    Write to screen and if necessary to Syslog
00239 ----------------------------------------------------------------*/
00240 void Send_IDS_Warning(char *message)
00241 {
00242   warning("%s", message);
00243 #ifdef WITH_SYSLOG
00244   if (config.SendAlert2Syslog == TRUE)
00245     syslog(LOG_USER || LOG_WARNING, "%s", message);
00246 #endif
00247 }

Generated on Fri Feb 25 12:02:37 2005 for WifiScanner by  doxygen 1.4.1