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 178 2007-08-11 16:57:24Z poggij $
00026  */
00027 
00028 #include <include.h>
00029 #include <src/crt_io.h>
00030 #include <src/interface.h>
00031 #include <src/errmsg.h>
00032 #ifdef WITH_SYSLOG
00033 #include <syslog.h>
00034 #endif
00035 
00036 extern UINT8 CursesIsInUse;
00037 extern ConfigStruct config;
00038 
00039 #ifdef HAVE_LIBNCURSES
00040 extern WINDOW *RealTime_WND;
00041 #endif
00042 #ifdef WITH_THREAD
00043 extern pthread_mutex_t screen_mutex;
00044 #endif
00045 
00046 
00047 /*
00048  * Some internal forward refs
00049  */
00050 
00051 /*----------------------------------------------------------------
00052    Function to write a message and exit (fatal error)
00053    Write with and without curses
00054    exit curses environment before all thinks but after 3 secondes
00055      so we can read screen before death condition
00056 ----------------------------------------------------------------*/
00057 void fatal(int error_code, char *fmt, ...)
00058 {
00059   va_list ap;
00060 
00061 #ifdef HAVE_LIBNCURSES
00062   if (CursesIsInUse == TRUE)
00063     RefreshAllWND();            // Refresh All Windows
00064   if (config.DebugLevel >= 1)
00065     sleep(3);                   // Sleep for 3 seconds
00066 #else
00067   fflush(stdout);               // Refresh stdout buffer
00068 #endif
00069   va_start(ap, fmt);
00070 #ifdef HAVE_LIBNCURSES
00071   if (CursesIsInUse == TRUE)
00072     EndCurses();
00073 #endif
00074   vfprintf(stderr, fmt, ap);
00075   vfprintf(stderr, ">> %s\n", ErrorsMsg[error_code]);
00076   va_end(ap);
00077   exit(error_code);
00078 }
00079 
00080 /*----------------------------------------------------------------
00081    Function to write DEBUG informations
00082    Write with and without curses
00083    If DebugLevel is greater than 0 we refresh screen else not
00084 ----------------------------------------------------------------*/
00085 void debug(UINT8 DL, char *fmt, ...)
00086 {
00087   va_list ap;
00088 
00089   if (config.DebugLevel < DL)
00090     return;                     // Return if debug level is not enougth
00091   if (CursesIsInUse == FALSE)
00092     fflush(stdout);             // Refresh stdout buffer
00093   va_start(ap, fmt);
00094 #ifdef HAVE_LIBNCURSES
00095   if (CursesIsInUse == TRUE) {
00096 #ifdef WITH_THREAD
00097     pthread_mutex_lock(&screen_mutex);
00098 #endif
00099     vwprintw(RealTime_WND, fmt, ap);
00100     if (config.DebugLevel > 0)
00101       wrefresh(RealTime_WND);
00102 #ifdef WITH_THREAD
00103     pthread_mutex_unlock(&screen_mutex);
00104 #endif
00105   } else {
00106 #endif
00107     vfprintf(stderr, fmt, ap);
00108     fflush(stderr);
00109 #ifdef HAVE_LIBNCURSES
00110   }
00111 #endif
00112   va_end(ap);
00113 }
00114 
00115 /*----------------------------------------------------------------
00116    Function to write DEBUG informations with the time stamps in ms
00117    Write with and without curses
00118    If DebugLevel is greater than 0 we refresh screen else not
00119 ----------------------------------------------------------------*/
00120 void debugTS(UINT8 DL, char *fmt, ...)
00121 {
00122   va_list ap;
00123   struct timeb binary_now;
00124 
00125   //struct tm *ascii_now = NULL;
00126 
00127   ftime(&binary_now);
00128   //ascii_now = localtime (&binary_now.time);
00129 
00130   if (config.DebugLevel < DL)
00131     return;                     // Return if debug level is not enougth
00132   if (CursesIsInUse == FALSE)
00133     fflush(stdout);             // Refresh stdout buffer
00134   va_start(ap, fmt);
00135 #ifdef HAVE_LIBNCURSES
00136   if (CursesIsInUse == TRUE) {
00137 #ifdef WITH_THREAD
00138     pthread_mutex_lock(&screen_mutex);
00139 #endif
00140     wprintw(RealTime_WND, "%d.%03d:", (int) binary_now.time,
00141             binary_now.millitm);
00142     vwprintw(RealTime_WND, fmt, ap);
00143     if (config.DebugLevel > 0)
00144       wrefresh(RealTime_WND);
00145 #ifdef WITH_THREAD
00146     pthread_mutex_unlock(&screen_mutex);
00147 #endif
00148   } else {
00149 #endif
00150     vfprintf(stderr, fmt, ap);
00151     fflush(stderr);
00152 #ifdef HAVE_LIBNCURSES
00153   }
00154 #endif
00155   va_end(ap);
00156 }
00157 
00158 /*----------------------------------------------------------------
00159    Function to write WARNING informations
00160    Write with and without curses
00161 ----------------------------------------------------------------*/
00162 void warning(char *fmt, ...)
00163 {
00164   va_list ap;
00165 
00166   if (CursesIsInUse == FALSE)
00167     fflush(stdout);             // Refresh stdout buffer
00168   va_start(ap, fmt);
00169 #ifdef HAVE_LIBNCURSES
00170   if (CursesIsInUse == TRUE) {
00171 #ifdef WITH_THREAD
00172     pthread_mutex_lock(&screen_mutex);
00173 #endif
00174     vwprintw(RealTime_WND, fmt, ap);
00175     if (config.DebugLevel > 0)
00176       wrefresh(RealTime_WND);
00177 #ifdef WITH_THREAD
00178     pthread_mutex_unlock(&screen_mutex);
00179 #endif
00180   } else {
00181 #endif
00182     vfprintf(stderr, fmt, ap);
00183     fflush(stderr);
00184 #ifdef HAVE_LIBNCURSES
00185   }
00186 #endif
00187   va_end(ap);
00188 }
00189 
00190 /*----------------------------------------------------------------
00191    Function to write WARNING informations only if error code is not
00192     NO_ERROR
00193    Write with and without curses
00194 ----------------------------------------------------------------*/
00195 void warning_if_error(int errorcode)
00196 {
00197   if (errorcode != NO_ERROR) {
00198     if (CursesIsInUse == FALSE)
00199       fflush(stdout);           // Refresh stdout buffer
00200 #ifdef HAVE_LIBNCURSES
00201     if (CursesIsInUse == TRUE) {
00202 #ifdef WITH_THREAD
00203       pthread_mutex_lock(&screen_mutex);
00204 #endif
00205       wprintw(RealTime_WND, "Warning : %s\n", ErrorsMsg[errorcode]);
00206       if (config.DebugLevel > 0)
00207         wrefresh(RealTime_WND);
00208 #ifdef WITH_THREAD
00209       pthread_mutex_unlock(&screen_mutex);
00210 #endif
00211     } else {
00212 #endif
00213       fprintf(stderr, "Warning : %s\n", ErrorsMsg[errorcode]);
00214       fflush(stderr);
00215 #ifdef HAVE_LIBNCURSES
00216     }
00217 #endif
00218   }
00219 }
00220 
00221 /********************/
00222 void DumpHexPaquets(WINDOW * RealTime_WND, UINT8 msgbuf[], int recvlen)
00223 {
00224   unsigned int n, i, end;
00225 
00226 #ifdef WITH_THREAD
00227   pthread_mutex_lock(&screen_mutex);
00228 #endif
00229   wprintw(RealTime_WND, "MSG=\n");
00230 
00231   for (n = 0; n < recvlen; n += 16) {
00232     if (n > MAX_BUFFER_SIZE)
00233       break;                    // Please ... No core dump :-)
00234     end = MIN_OF((recvlen - n), 16);
00235     wprintw(RealTime_WND, "0x%04X - ", n);
00236 
00237     // Print Hex part
00238     for (i = 0; i < 16; i++) {
00239       /* Write only packet, not garbage ;-) */
00240       if (i < end)
00241         wprintw(RealTime_WND, "%02X", (UINT8) msgbuf[n + i]);
00242       else
00243         wprintw(RealTime_WND, "  ");
00244       /* Separator every 2 bytes */
00245       if (!((i + 1) % 2))
00246         wprintw(RealTime_WND, " ");
00247     }
00248 
00249     // Print ASCI part
00250     for (i = 0; i < end; i++) {
00251       // Print caractere if it's printable
00252       if (msgbuf[n + i] >= 0x20 && msgbuf[n + i] <= 0x7E)
00253         wprintw(RealTime_WND, "%c", (char) msgbuf[n + i]);
00254       else
00255         wprintw(RealTime_WND, ".");
00256     }
00257     /* End of line each 16 bytes */
00258     wprintw(RealTime_WND, "\n");
00259   }
00260   wrefresh(RealTime_WND);
00261 #ifdef WITH_THREAD
00262   pthread_mutex_unlock(&screen_mutex);
00263 #endif
00264 }
00265 
00266 /*----------------------------------------------------------------
00267    Function to write IDS informations
00268    Write to screen and if necessary to Syslog
00269 ----------------------------------------------------------------*/
00270 void Send_IDS_Warning(char *message)
00271 {
00272   warning("%s", message);
00273 #ifdef WITH_SYSLOG
00274   if (config.SendAlert2Syslog == TRUE)
00275     syslog(LOG_USER || LOG_WARNING, "%s", message);
00276 #endif
00277 }

Generated on Fri Jul 25 17:10:33 2008 for WifiScanner by  doxygen 1.5.5