Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

misc.c

Go to the documentation of this file.
00001 /* $Id$ */
00002 /*
00003 ** Copyright (C) 2002 Martin Roesch <roesch@sourcefire.com>
00004 **
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 **
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 **
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 */
00019 
00020 
00021 #include "snort.h"
00022 #include "util.h"
00023 
00024 
00025 /****************************************************************************
00026  *
00027  * Function: gettimeofday(struct timeval *, struct timezone *)
00028  *
00029  * Purpose:  Get current time of day.
00030  *
00031  * Arguments: tv => Place to store the curent time of day.
00032  *            tz => Ignored.
00033  *
00034  * Returns: 0 => Success.
00035  *
00036  ****************************************************************************/
00037 
00038 int gettimeofday(struct timeval *tv, struct timezone *tz)
00039 {
00040     struct _timeb tb;
00041 
00042         if(tv==NULL)
00043     {
00044         return -1;
00045     }
00046         _ftime(&tb);
00047         tv->tv_sec = tb.time;
00048         tv->tv_usec = ((int) tb.millitm) * 1000;
00049         return 0;
00050 }
00051 
00052 /****************************************************************************
00053  *
00054  * Function: GetAdapterFromList(void *,int)
00055  *
00056  * Purpose:  Get a specific adapter from the list of adapters on the system.
00057  *
00058  * Arguments: device => Device to look for.
00059  *            index => Adapter number.
00060  *
00061  * Returns: Adapter if device was valid.
00062  *
00063  * Comments: Shamelessly ripped from WinDump.
00064  *
00065  ****************************************************************************/
00066 
00067 void *GetAdapterFromList(void *device,int index)
00068 {
00069     DWORD dwVersion;
00070     DWORD dwWindowsMajorVersion;
00071     char *Adapter95;
00072     WCHAR *Adapter;
00073     int i;
00074 
00075     dwVersion = GetVersion();
00076     dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
00077 
00078     /* Windows 95. */
00079     if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4)
00080     {
00081         Adapter95 = device;
00082         for( i=0; i<index-1; i++ )
00083         {
00084             while(*Adapter95++ != 0)
00085             {
00086             }
00087             if( *Adapter95 == 0 )
00088             {
00089                                 return NULL;
00090             }
00091         }
00092         return  Adapter95;
00093     }
00094     else
00095     {
00096                 /* NT. */
00097         Adapter = (WCHAR *) device;
00098         for( i=0; i<index-1; i++ )
00099         {
00100             while( *Adapter++ != 0 )
00101             {
00102             }
00103             if( *Adapter == 0 )
00104             {
00105                 return NULL;
00106             }
00107         }
00108         return Adapter;
00109     }
00110 }
00111 
00112 /****************************************************************************
00113  *
00114  * Function: print_interface(char *)
00115  *
00116  * Purpose:  Print the interface number. Platform Independent.
00117  *
00118  * Arguments: interface => Name of Interface to print.
00119  *
00120  * Returns: Correct character format of Interface for the current platform.
00121  *
00122  * Comments: Shamelessly ripped from WinDump.
00123  *
00124  ****************************************************************************/
00125 
00126 char *print_interface(char *szInterface)
00127 {
00128     static char device[128];
00129 
00130     /* Device always ends with a double \0, so this way to
00131        determine its length should be always valid */
00132     if(IsTextUnicode(szInterface, wcslen((short*)szInterface), NULL))
00133         sprintf(device, "%ws", szInterface);
00134     else
00135         sprintf(device, "%s", szInterface);
00136 
00137     return(device);
00138 }
00139 
00140 /****************************************************************************
00141  *
00142  * Function: PrintDeviceList(const char *)
00143  *
00144  * Purpose:  Print all interfaces forund on the system that we can listen on.
00145  *
00146  * Arguments: device => List of all devices to listen on.
00147  *
00148  * Returns: void function.
00149  *
00150  * Comments: Shamelessly ripped from WinDump.
00151  *
00152  ****************************************************************************/
00153 
00154 void PrintDeviceList(const char *device)
00155 {
00156     DWORD dwVersion;
00157     DWORD dwWindowsMajorVersion;
00158     const WCHAR* t;
00159     const char* t95;
00160     int i=0;
00161     int DescPos=0;
00162     char *Desc;
00163     int n=1;
00164 
00165     dwVersion=GetVersion();
00166     dwWindowsMajorVersion =  (DWORD)(LOBYTE(LOWORD(dwVersion)));
00167 
00168     /* Windows 95. */
00169     if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4)
00170     {
00171         t95 = (char*)device;
00172 
00173         while(*(t95+DescPos)!=0 || *(t95+DescPos-1)!=0)
00174         {
00175             DescPos++;
00176         }
00177 
00178         Desc=(char*)t95+DescPos+1;
00179         printf("\nInterface\tDevice\t\tDescription\n-------------------------------------------\n");
00180         printf("%d  ",n++);
00181 
00182         while ( ! (t95[i]==0 && t95[i-1]==0) )
00183         {
00184             if ( t95[i] == 0 )
00185             {
00186                 putchar(' ');
00187                 putchar('(');
00188                 while( *Desc !=0 )
00189                 {
00190                     putchar(*Desc);
00191                     Desc++;
00192                 }
00193                 Desc++;
00194                 putchar(')');
00195                 putchar('\n');
00196             }
00197             else
00198             {
00199                 putchar(t95[i]);
00200             }
00201 
00202             if( (t95[i]==0) && (t95[i+1]!=0) )
00203             {
00204                 printf("%d ",n++);
00205             }
00206 
00207             i++;
00208         }
00209         putchar('\n');
00210     }
00211     else
00212     {
00213         /* WinNT. */
00214         t = (WCHAR*) device;
00215         while( *(t+DescPos)!=0 || *(t+DescPos-1)!=0 )
00216         {
00217                 DescPos++;
00218         }
00219         DescPos <<= 1;
00220         Desc = (char*)t+DescPos+2;
00221         printf("\nInterface\tDevice\t\tDescription\n-------------------------------------------\n");
00222         printf("%d  ",n++);
00223 
00224         while ( ! ( t[i]==0 && t[i-1]==0 ) )
00225         {
00226             if ( t[i] == 0 )
00227             {
00228                 putchar(' ');
00229                 putchar('(');
00230                 while( *Desc != 0 )
00231                 {
00232                     putchar(*Desc);
00233                     Desc++;
00234                 }
00235                 Desc++;
00236                 putchar(')');
00237                 putchar('\n');
00238             }
00239             else
00240             {
00241                 putchar(t[i]);
00242             }
00243 
00244             if( t[i]==0 && t[i+1]!=0 )
00245             {
00246                 printf("%d ",n++);
00247             }
00248 
00249             i++;
00250         }
00251         putchar('\n');
00252     }
00253 }
00254 
00255 /****************************************************************************
00256  *
00257  * Function: init_winsock(void)
00258  *
00259  * Purpose:  Initialize winsock.
00260  *
00261  * Arguments: None.
00262  *
00263  * Returns: 0 => Initilization failed.
00264  *          1 => Initilization succeeded.
00265  *
00266  ****************************************************************************/
00267 
00268 int init_winsock(void)
00269 {
00270     WORD wVersionRequested = MAKEWORD(1, 1);
00271     WSADATA wsaData;
00272 
00273     if (WSAStartup(wVersionRequested, &wsaData))
00274     {
00275         FatalError("[!] ERROR: Unable to find a usable Winsock.\n");
00276         return 0;
00277     }
00278 
00279     if (LOBYTE(wsaData.wVersion) < 1 || HIBYTE(wsaData.wVersion) < 1)
00280     {
00281         FatalError("[!] ERROR: Unable to find Winsock version 1.1 or greater. You have version %d.%d.\n",
00282                        LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
00283         WSACleanup();
00284         return 0;
00285     }
00286 
00287     return 1;
00288 }       
00289 
00290 int geteuid(void)
00291 {
00292         return 0;
00293 }

Generated on Sun May 14 14:51:19 2006 by  doxygen 1.4.2