hermes.c

Go to the documentation of this file.
00001 /* Linux Prism II Stumbler - Utility Scan for 802_11 networks under Linux
00002  * 
00003  * File : hermes.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: hermes.c 174 2007-07-25 13:20:29Z poggij $
00026  */
00027 
00028 // A lot of think is get from kismet
00029 //  http://www.kismetwireless.net/
00030 
00031 #include <include.h>
00032 #include <src/hermes.h>
00033 #include <src/crt_io.h>
00034 #include <src/functions.h>
00035 
00036 static char *ID = "$Id: hermes.c 174 2007-07-25 13:20:29Z poggij $";
00037 
00038 // All extern value you want
00039 extern UINT8 SingleChannel;
00040 extern UINT8 TypeOfCard;
00041 
00042 static CaptureArg ca;
00043 static char errbuf[PCAP_ERRBUF_SIZE];
00044 static UINT8 wlan_payload[MAX_BUFFER_SIZE];
00045 
00046 //-------------
00047 int selectChannelHERMES(char *devname, int channel)
00048 {
00049   char str[80];
00050   int result = NO_ERROR;
00051 
00052 #if 0
00053   sprintf(str, "iwpriv %s monitor 1 %d 2>/dev/null 1>/dev/null", devname,
00054           channel);
00055   //debug(3, str, "\n");
00056   result += system(str);
00057 #endif
00058   Iwconfig_Set_IntPriv(devname, "monitor", 1, channel);
00059 
00060   //return result;
00061   // No error return, because it's possible to change to a forbidden channel
00062   //  So we don't want that the program stop because of this restriction
00063   return NO_ERROR;
00064 }
00065 
00066 int shutCardHERMES(char *devname)
00067 {
00068   char str[80];
00069   int result = NO_ERROR;
00070 
00071   // Turn off monitor mode
00072   result = IwconfigSetMode(devname, IW_MODE_INFRA);
00073   warning_if_error(result);
00074 
00075   result = IfconfigSetFlags(devname, 0);
00076   warning_if_error(result);
00077 
00078   sprintf(str, "iwpriv %s monitor 0 1", devname);
00079   debug(3, "%s\n", str);
00080   result += system(str);
00081 
00082   return result;
00083 }
00084 
00085 int openCardHERMES(char *devname)
00086 {
00087   char str[80];
00088   int result = NO_ERROR;
00089 
00090   // Turn on monitor mode
00091   result = IfconfigSetFlags(devname, IFF_UP + IFF_PROMISC);
00092   warning_if_error(result);
00093 
00094   IwconfigClearSSID(devname);
00095 
00096   sprintf(str, "iwpriv %s monitor 1 1", devname);
00097   debug(3, "%s\n", str);
00098   result += system(str);
00099 
00100   return result;
00101 }
00102 
00103 
00104 // Get packet from card
00105 int getPacketHERMES(p80211_caphdr_t * wlan_header, UINT8 * buf, int maxlen)
00106 {
00107   struct pcap_pkthdr pktHdr;
00108   u_char *ret;
00109   fd_set rs;
00110   p80211msg_lnxind_wlansniffrm_t *Sniff_Frame;
00111 
00112   FD_ZERO(&rs);
00113   FD_SET(0, &rs);
00114 
00115   ret = (u_char *) pcap_next(ca.pcap, &pktHdr);
00116   // If no problem and packet is enought big (with data)
00117   if ((ret)
00118       && (pktHdr.len >= sizeof(p80211msg_lnxind_wlansniffrm_t))) {
00119     if (memcpy_buff(buf, ret, pktHdr.len) == NULL)
00120       return 0;
00121     Sniff_Frame = (p80211msg_lnxind_wlansniffrm_t *) buf;
00122     // Fill Header
00123     wlan_header->version = 0;    // It's a reduced capture frame format
00124     wlan_header->length = 0;     // Not used for now
00125     wlan_header->mactime = Sniff_Frame->mactime.data;
00126     wlan_header->hosttime = Sniff_Frame->hosttime.data;
00127     wlan_header->phytype = phytype_dsss_dot11_b; // Not used for now
00128     wlan_header->channel = Sniff_Frame->channel.data;
00129     wlan_header->datarate = Sniff_Frame->rate.data * 5;  // datarate is in units of 100kbps.
00130     wlan_header->antenna = 0;    // Not used for now
00131     wlan_header->priority = 0;   // Not used for now
00132     wlan_header->ssi_type = 0;   // Not used for now
00133     wlan_header->ssi_signal = Sniff_Frame->signal.data;
00134     wlan_header->ssi_noise = Sniff_Frame->noise.data;
00135     wlan_header->preamble = 0;   // Not used for now
00136     wlan_header->encoding = 0;   // Not used for now
00137     // Fill data frame
00138     if (memcpy_buff(wlan_payload,
00139                     &buf[sizeof(p80211msg_lnxind_wlansniffrm_t)],
00140                     pktHdr.len - sizeof(p80211msg_lnxind_wlansniffrm_t)) ==
00141         NULL)
00142       return 0;
00143     if (memcpy_buff(buf, wlan_payload,
00144                     maxlen - sizeof(p80211msg_lnxind_wlansniffrm_t)) ==
00145         NULL)
00146       return 0;
00147 
00148     if (pktHdr.len <= sizeof(p80211msg_lnxind_wlansniffrm_t))
00149       // Don't return negative value
00150       return 0;
00151     else
00152       return (pktHdr.len - sizeof(p80211msg_lnxind_wlansniffrm_t));
00153   } else {
00154     return (0);                 /* Noting to read */
00155   }
00156 }
00157 
00158 int openPacketHERMES(char *devname)
00159 {
00160   ca.pcap = pcap_open_live(devname, 3000, 1, 0, errbuf);
00161   if (ca.pcap) {
00162     pcap_setnonblock(ca.pcap, 1, errbuf);
00163     ca.DataLink = pcap_datalink(ca.pcap);
00164     ca.offset = CalculateOffset(ca.DataLink);
00165     return NO_ERROR;
00166   }
00167   return ERROR_CANT_OPEN_PCAP;
00168 }
00169 
00170 void closePacketHERMES(void)
00171 {
00172   pcap_close(ca.pcap);
00173 }

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