ipw.c

Go to the documentation of this file.
00001 /* Linux Prism II Stumbler - Utility Scan for 802_11 networks under Linux
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  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  * 
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  *
00022  * $Id: ipw.c 174 2007-07-25 13:20:29Z poggij $
00023  */
00024 
00025 #include <include.h>
00026 #include <src/ipw.h>
00027 #include <src/wlan-ng.h>
00028 #include <src/functions.h>
00029 #include <src/crt_io.h>
00030 
00031 static char *ID = "$Id: ipw.c 174 2007-07-25 13:20:29Z poggij $";
00032 
00033 // All extern value you want
00034 //extern unsigned int DebugLevel;
00035 //extern UINT8 SingleChannel;
00036 //extern UINT8 TypeOfCard;
00037 extern ConfigStruct config;
00038 extern WINDOW *Sum_WND, *RealTime_WND;
00039 
00040 static CaptureArg ca;
00041 static char errbuf[PCAP_ERRBUF_SIZE];
00042 
00043 //-------------
00044 int selectChannelIPW(char *devname, int channel)
00045 {
00046 #define STR_MAX 80
00047   char str[STR_MAX];
00048   int result = NO_ERROR;
00049 
00050   //result = IwconfigSetMode(devname, IW_MODE_MONITOR);
00051   //result = IwconfigSetChannel(devname, channel);
00052   snprintf(str, STR_MAX, "iwpriv %s monitor 1 %d 2>/dev/null",
00053            devname, channel);
00054   debug(3, "%s\n", str);
00055   result += system(str);
00056   //return NO_ERROR;
00057   return result;
00058 }
00059 
00060 int shutCardIPW(char *devname)
00061 {
00062   int result = NO_ERROR;
00063 
00064   result = IwconfigSetMode(devname, IW_MODE_INFRA);
00065   warning_if_error(result);
00066   result = IfconfigSetFlags(devname, IFF_UP);
00067   warning_if_error(result);
00068   return result;
00069 }
00070 
00071 int openCardIPW(char *devname)
00072 {
00073   int result = NO_ERROR;
00074 
00075   // Turn on monitor mode
00076   result = IwconfigSetMode(devname, IW_MODE_MONITOR);
00077   warning_if_error(result);
00078   result = IfconfigSetFlags(devname, IFF_UP + IFF_PROMISC);
00079   warning_if_error(result);
00080   return result;
00081 }
00082 
00083 // Get packet from card
00084 int getPacketIPW(p80211_caphdr_t * wlan_header, UINT8 * buf, int maxlen)
00085 {
00086   struct pcap_pkthdr pktHdr;
00087   u_char *RadioPacket;
00088   fd_set rs;
00089 
00090   FD_ZERO(&rs);
00091   FD_SET(0, &rs);
00092 
00093   RadioPacket = (u_char *) pcap_next(ca.pcap, &pktHdr);
00094   if (RadioPacket != NULL) {
00095 #if 0
00096     // Function to dump in hexa the message send or sent to the driver
00097     DumpHexPaquets(RealTime_WND, RadioPacket, 0x40);
00098 #endif
00099     if (ca.DataLink == DLT_IEEE802_11_RADIO) {
00100       // calculate the offset = RadioPacket[2];
00101       ca.offset = (UINT16) RadioPacket[2];
00102       debug(3, "DLT_IEEE802_11_RADIO >> ca.offset = %x\n", ca.offset);
00103     }
00104     debug(3, "ca.offset = %x - pktHdr.len = %x\n", ca.offset, pktHdr.len);
00105     // If no problem and packet is enought big (with data)
00106     if (pktHdr.len > MAX_BUFFER_SIZE) {
00107       debug(1, "ERROR : Packet is TOOO BIG size=%d\n", pktHdr.len);
00108       // Sometimes return an enormous empty packet ...
00109       //DumpHexPaquets(RealTime_WND, buf, 0x1B0);
00110       return 0;
00111     } else {
00112       memcpy_buff(buf, (RadioPacket + ca.offset),
00113                   MIN_OF((pktHdr.len - ca.offset), maxlen));
00114       FillRadioData(wlan_header, ca.DataLink, RadioPacket, pktHdr.len);
00115       return MAX_OF(0, (pktHdr.len - ca.offset));
00116     }
00117   }
00118   return (0);                   /* Noting to read */
00119 }
00120 
00121 int openPacketIPW(char *devname)
00122 {
00123   ca.pcap = pcap_open_live(devname, MAX_PACKET_LEN, 0, 1000, errbuf);
00124 
00125   if (ca.pcap) {
00126     pcap_setnonblock(ca.pcap, 1, errbuf);
00127     ca.DataLink = pcap_datalink(ca.pcap);
00128     ca.offset = CalculateOffset(ca.DataLink);
00129     return NO_ERROR;
00130   }
00131   return ERROR_CANT_OPEN_PCAP;
00132 }
00133 
00134 void closePacketIPW(void)
00135 {
00136   pcap_close(ca.pcap);
00137 }

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