hostap.c

Go to the documentation of this file.
00001 /* Linux Prism II Stumbler - Utility Scan for 802_11 networks under Linu
00002  * 
00003  * File : $Name$
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: hostap.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/hostap.h>
00033 #include <src/crt_io.h>
00034 #include <src/functions.h>
00035 
00036 static char *ID = "$Id: hostap.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 selectChannelHOSTAP(char *devname, int channel)
00048 {
00049   int result = NO_ERROR;
00050 
00051   result = IwconfigSetChannel(devname, channel);
00052 
00053   //return result;
00054   // No error return, because it's possible to change to a forbidden channel
00055   //  So we don't wand that the program stop because of this restriction
00056   return NO_ERROR;
00057 }
00058 
00059 int shutCardHOSTAP(char *devname)
00060 {
00061   int result = NO_ERROR;
00062 
00063   // Turn off monitor mode
00064   result = IwconfigSetMode(devname, IW_MODE_INFRA);
00065   warning_if_error(result);
00066   result = IwconfigSetChannel(devname, 11);
00067   warning_if_error(result);
00068   result = IfconfigSetFlags(devname, IFF_UP);
00069   warning_if_error(result);
00070 
00071   return result;
00072 }
00073 
00074 int openCardHOSTAP(char *devname)
00075 {
00076   char str[80];
00077   int result = NO_ERROR;
00078 
00079   // Turn on monitor mode
00080   sprintf(str, "prism2_param %s monitor_type 1", devname);
00081   debug(3, "%s\n", str);
00082   result += system(str);
00083 
00084   result = IwconfigSetMode(devname, IW_MODE_MONITOR);
00085   warning_if_error(result);
00086   result = IwconfigSetChannel(devname, 1);
00087   warning_if_error(result);
00088   result = IfconfigSetFlags(devname, IFF_UP + IFF_PROMISC);
00089   warning_if_error(result);
00090 
00091   return result;
00092 }
00093 
00094 
00095 // Get packet from card
00096 // Return the size of the packet
00097 int getPacketHOSTAP(p80211_caphdr_t * wlan_header, UINT8 * buf, int maxlen)
00098 {
00099   struct pcap_pkthdr pktHdr;
00100   u_char *RadioPacket;
00101   fd_set rs;
00102 
00103   FD_ZERO(&rs);
00104   FD_SET(0, &rs);
00105 
00106   RadioPacket = (u_char *) pcap_next(ca.pcap, &pktHdr);
00107 
00108   if (RadioPacket != NULL) {
00109     // If no problem and packet is enought big (with data)
00110     if (pktHdr.len >= sizeof(p80211msg_lnxind_wlansniffrm_t)) {
00111       if (pktHdr.len > MAX_BUFFER_SIZE) {
00112         debug(1, "ERROR : Packet is TOOO BIG size=%d\n", pktHdr.len);
00113         // Sometimes Wlan-NG return an enormous empty packet ...
00114         //DumpHexPaquets(RealTime_WND, buf, 0x1B0);
00115         return 0;
00116       } else {
00117         FillRadioData(wlan_header, ca.DataLink, RadioPacket, pktHdr.len);
00118         memcpy_buff(buf, (RadioPacket + ca.offset),
00119                     MIN_OF((pktHdr.len - ca.offset), maxlen));
00120         return MAX_OF(0,
00121                       (pktHdr.len -
00122                        sizeof(p80211msg_lnxind_wlansniffrm_t)));
00123       }
00124     }
00125   }
00126   return (0);                   /* Noting to read */
00127 }
00128 
00129 int openPacketHOSTAP(char *devname)
00130 {
00131   ca.pcap = pcap_open_live(devname, 3000, TRUE, 1000, errbuf);
00132   if (ca.pcap) {
00133     pcap_setnonblock(ca.pcap, 1, errbuf);
00134     ca.DataLink = pcap_datalink(ca.pcap);
00135     ca.offset = CalculateOffset(ca.DataLink);
00136     return NO_ERROR;
00137   } else {
00138     warning("ERROR when pcap_open_live : %s\n", errbuf);
00139   }
00140   return ERROR_CANT_OPEN_PCAP;
00141 }
00142 
00143 void closePacketHOSTAP(void)
00144 {
00145   pcap_close(ca.pcap);
00146 }

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