madwifi-ng.c File Reference

#include <include.h>
#include <src/madwifi-ng.h>
#include <src/wlan-ng.h>
#include <src/crt_io.h>
#include <src/functions.h>
#include <dirent.h>

Include dependency graph for madwifi-ng.c:

Go to the source code of this file.

Defines

#define STR_MAX   256

Functions

int DestroyVAP_MADWIFI_NG (char *ifname)
int selectChannelMADWIFI_NG (char *devname, int channel)
int shutCardMADWIFI_NG (char *devname)
int findAvailableIF (char *devname)
int openCardMADWIFI_NG (char *devname)
int getPacketMADWIFI_NG (p80211_caphdr_t *wlan_header, UINT8 *buf, int maxlen)
int openPacketMADWIFI_NG (char *devname)
void closePacketMADWIFI_NG (void)

Variables

static char * ID = "$Id: MADWIFI-NG.c 145 2006-04-10 08:13:38Z poggij $"
ConfigStruct config
WINDOW * Sum_WND
WINDOW * RealTime_WND
static CaptureArg ca
static char errbuf [PCAP_ERRBUF_SIZE]
static char devname1 [16]
static char devname2 [16]


Define Documentation

#define STR_MAX   256

Definition at line 56 of file madwifi-ng.c.


Function Documentation

int DestroyVAP_MADWIFI_NG ( char *  ifname  ) 

Definition at line 75 of file madwifi-ng.c.

References MADWIFING_ERROR_CANT_CREATE_SOCKET, MADWIFING_ERROR_CANT_DESTROY_VAP, SIOC80211IFDESTROY, and warning().

Referenced by openCardMADWIFI_NG(), and shutCardMADWIFI_NG().

00076 {
00077   struct ifreq ifr;
00078   int sock;
00079 
00080   // Connect to the madwifidriver
00081   if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00082     warning("Failed to create socket to madwifi: %s\n", strerror(errno));
00083     return MADWIFING_ERROR_CANT_CREATE_SOCKET;
00084   }
00085   // Destroy the VAP
00086   memset(&ifr, 0, sizeof(ifr));
00087   strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
00088   if (ioctl(sock, SIOC80211IFDESTROY, &ifr) < 0) {
00089     warning("Failed to destroy VAP: %s\n", strerror(errno));
00090     close(sock);
00091     return MADWIFING_ERROR_CANT_DESTROY_VAP;
00092   }
00093   close(sock);
00094 
00095   return 0;
00096 }

int selectChannelMADWIFI_NG ( char *  devname,
int  channel 
)

Definition at line 59 of file madwifi-ng.c.

References IwconfigSetChannel().

Referenced by selectChannel().

00060 {
00061   int result = 0;
00062 
00063   result = IwconfigSetChannel(devname, channel);
00064 
00065   //return NO_ERROR;                     // XXX no error if channel is forbidden
00066   return result;
00067 }

int shutCardMADWIFI_NG ( char *  devname  ) 

Definition at line 69 of file madwifi-ng.c.

References DestroyVAP_MADWIFI_NG().

Referenced by shutCard().

00070 {
00071   // Destroy the created VAP
00072   return (DestroyVAP_MADWIFI_NG(devname));
00073 }

int findAvailableIF ( char *  devname  ) 

Definition at line 98 of file madwifi-ng.c.

References debug, ERROR_CANT_CREATE_SOCKET, ERROR_COULDNT_FIND_A_FREE_IFACE, NO_ERROR, and warning().

Referenced by openCardMADWIFI_NG().

00099 {
00100   UINT16 n = 0;
00101   struct ifreq ifr;
00102   int skfd;
00103   char tnam[IFNAMSIZ];
00104 
00105   if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00106     warning("Failed to create AF_INET DGRAM socket.\n");
00107     return ERROR_CANT_CREATE_SOCKET;
00108   }
00109 
00110   tnam[0] = '\0';
00111   for (n = 0; (n < 10) && (tnam[0] == '\0'); n++) {
00112     snprintf(tnam, IFNAMSIZ, "ath%d", n);
00113 
00114     memset(&ifr, 0, sizeof(ifr));
00115     strncpy(ifr.ifr_name, tnam, sizeof(ifr.ifr_name) - 1);
00116     if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
00117       debug(3, "Interface %s is free\n", tnam);
00118       // Good it's not an existant IF, so its a free named IF
00119       strncpy(devname, tnam, IFNAMSIZ);
00120       close(skfd);
00121       return NO_ERROR;
00122     } else {
00123       // FOUND an existant IF
00124       // try to find an another free
00125       tnam[0] = '\0';
00126     }
00127   }
00128   close(skfd);
00129 
00130   return ERROR_COULDNT_FIND_A_FREE_IFACE;
00131 }

int openCardMADWIFI_NG ( char *  devname  ) 

Definition at line 133 of file madwifi-ng.c.

References debug, DestroyVAP_MADWIFI_NG(), devname1, devname2, findAvailableIF(), IEEE80211_CLONE_BSSID, IEEE80211_M_MONITOR, IfconfigSetFlags(), MADWIFING_ERROR_CANT_CREATE_SOCKET, MADWIFING_ERROR_NO_DEVICE_PRESENT, MADWIFING_ERROR_NO_INTERFACE_AVAILABLE, MADWIFING_ERROR_NO_VAP_CREATED, SIOC80211IFCREATE, STR_MAX, and warning().

Referenced by openCard().

00134 {
00135   char dirpath[STR_MAX];
00136   DIR *devdir;
00137   struct dirent *devfile;
00138 
00139   struct ieee80211_clone_params {
00140     char icp_name[IFNAMSIZ];
00141     UINT16 icp_opmode;
00142     UINT16 icp_flags;
00143   };
00144 
00145   struct ieee80211_clone_params cp;
00146   struct ifreq ifr;
00147   int sock;
00148 
00149   // -- Search if /sys arborescence is available
00150   snprintf(dirpath, STR_MAX, "/sys/class/net/%s/device/", devname);
00151   if ((devdir = opendir(dirpath)) == NULL) {
00152     warning("Device %s is not present !!!\n", devname);
00153     return MADWIFING_ERROR_NO_DEVICE_PRESENT;
00154   }
00155   // -- Determine ath and wifi interface number
00156   //    and destroy all VAP if exists
00157   while ((devfile = readdir(devdir)) != NULL) {
00158     if (strncmp("net:ath", devfile->d_name, 7) == 0) {
00159       strncpy(devname1, devfile->d_name + 4, IFNAMSIZ);
00160 #if 0
00161       DestroyVAP_MADWIFI_NG(devname1);
00162       debug(3, "Interface %s is destroyed\n", devname1);
00163 #else
00164       debug(3, "Interface %s is skiped\n", devname1);
00165 #endif
00166     }
00167 
00168     if (strncmp("net:wifi", devfile->d_name, 8) == 0) {
00169       strncpy(devname2, devfile->d_name + 4, IFNAMSIZ);
00170       debug(3, "Found a good Interface %s\n", devname2);
00171     }
00172   }
00173   closedir(devdir);
00174 
00175   // Now all is clean
00176   // We can create a VAP
00177 
00178   // First Step : Find a ath? interface available
00179   if (findAvailableIF(devname1) != 0) {
00180     warning("No available VAP found :-(\n");
00181     return MADWIFING_ERROR_NO_INTERFACE_AVAILABLE;
00182   }
00183   // Master interface is now devname2
00184   strncpy(devname, devname2, IFNAMSIZ);
00185   debug(3,
00186         "We now use %s as the master insterface for madwifi-ng driver\n",
00187         devname);
00188 
00189   // Second Step Create VAP and put it in MONITOR mode
00190   memset(&cp, 0, sizeof(cp));
00191   strncpy(cp.icp_name, devname1, IFNAMSIZ);
00192   cp.icp_opmode = IEEE80211_M_MONITOR;
00193   cp.icp_flags = IEEE80211_CLONE_BSSID;
00194 
00195   memset(&ifr, 0, sizeof(ifr));
00196   strncpy(ifr.ifr_name, devname2, IFNAMSIZ);
00197   ifr.ifr_data = (caddr_t) & cp;
00198 
00199   if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00200     warning("Failed to create socket to madwifi: %s\n", strerror(errno));
00201     return MADWIFING_ERROR_CANT_CREATE_SOCKET;
00202   }
00203 
00204   if (ioctl(sock, SIOC80211IFCREATE, &ifr) < 0) {
00205     warning("Unable to create VAP: %s\n", strerror(errno));
00206     close(sock);
00207     return MADWIFING_ERROR_NO_VAP_CREATED;
00208   }
00209 
00210   strncpy(devname1, ifr.ifr_name, IFNAMSIZ);
00211   debug(3, "I create a VAP in monitor mode in %s\n", ifr.ifr_name);
00212   close(sock);
00213 
00214   // Slave interface and working interface is now the same
00215   strncpy(devname, devname1, IFNAMSIZ);
00216   debug(3, "We now use %s as the slave insterface for madwifi-ng driver\n",
00217         devname1);
00218 
00219   // last step : UP the interface
00220   return IfconfigSetFlags(devname, IFF_UP);
00221 }

int getPacketMADWIFI_NG ( p80211_caphdr_t wlan_header,
UINT8 buf,
int  maxlen 
)

Definition at line 227 of file madwifi-ng.c.

References CaptureArg_t::DataLink, debug, FillRadioData(), MAX_BUFFER_SIZE, MAX_OF, memcpy_buff(), MIN_OF, and CaptureArg_t::pcap.

Referenced by getPacket().

00228 {
00229   struct pcap_pkthdr pktHdr;
00230   u_char *RadioPacket;
00231   fd_set rs;
00232   int real_size_of_headers = 0;
00233 
00234   FD_ZERO(&rs);
00235   FD_SET(0, &rs);
00236 
00237   RadioPacket = (u_char *) pcap_next(ca.pcap, &pktHdr);
00238 
00239   if (RadioPacket != NULL) {
00240   // If no problem and packet is enought big (with data)
00241     if (pktHdr.len >= sizeof(p80211msg_lnxind_wlansniffrm_t)) {
00242       if (pktHdr.len > MAX_BUFFER_SIZE) {
00243   debug(1, "ERROR : Packet is TOOO BIG size=%d\n", pktHdr.len);
00244   //DumpHexPaquets(RealTime_WND, buf, 0x1B0);
00245   return 0;
00246       } else {
00247   real_size_of_headers = FillRadioData(wlan_header, ca.DataLink, RadioPacket, pktHdr.len);
00248   memcpy_buff(buf, (RadioPacket + real_size_of_headers), MIN_OF((pktHdr.len - real_size_of_headers), maxlen));
00249   return MAX_OF(0, (pktHdr.len - real_size_of_headers));
00250       }
00251     }
00252   }
00253   return (0);                   /* Noting to read */
00254 }

int openPacketMADWIFI_NG ( char *  devname  ) 

Definition at line 256 of file madwifi-ng.c.

References CalculateOffset(), CaptureArg_t::DataLink, errbuf, ERROR_CANT_OPEN_PCAP, NO_ERROR, CaptureArg_t::offset, and CaptureArg_t::pcap.

Referenced by openPacket().

00257 {
00258   ca.pcap = pcap_open_live(devname, 3000, 1, 0, errbuf);
00259   if (ca.pcap) {
00260     pcap_setnonblock(ca.pcap, 1, errbuf);
00261     ca.DataLink = pcap_datalink(ca.pcap);
00262     ca.offset = CalculateOffset(ca.DataLink);
00263     return NO_ERROR;
00264   }
00265   return ERROR_CANT_OPEN_PCAP;
00266 }

void closePacketMADWIFI_NG ( void   ) 

Definition at line 268 of file madwifi-ng.c.

References CaptureArg_t::pcap.

Referenced by closePacket().

00269 {
00270   pcap_close(ca.pcap);
00271 }


Variable Documentation

char* ID = "$Id: MADWIFI-NG.c 145 2006-04-10 08:13:38Z poggij $" [static]

Definition at line 39 of file madwifi-ng.c.

Definition at line 58 of file scanner.c.

WINDOW* Sum_WND

Definition at line 61 of file scanner.c.

WINDOW * RealTime_WND

Definition at line 61 of file scanner.c.

CaptureArg ca [static]

Definition at line 48 of file madwifi-ng.c.

char errbuf[PCAP_ERRBUF_SIZE] [static]

Definition at line 49 of file madwifi-ng.c.

char devname1[16] [static]

Definition at line 51 of file madwifi-ng.c.

char devname2[16] [static]

Definition at line 52 of file madwifi-ng.c.


Generated on Fri Jul 25 17:11:09 2008 for WifiScanner by  doxygen 1.5.5