⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wireless_utility.cpp

📁 270的linux说明
💻 CPP
字号:
/*

Copyright (c) 2008, Intel Corporation. 

All rights reserved.

 

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:


    * Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation and/or 
other materials provided with the distribution.

    * Neither the name of Intel Corporation nor the names of its contributors 
may be used to endorse or promote products derived from this software without 
specific prior written permission.

 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.

*/#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>		/* gethostbyname, getnetbyname */
#include <net/ethernet.h>	/* struct ether_addr */
#include <sys/time.h>		/* struct timeval */
#include <unistd.h>#include <net/if_arp.h>		/* For ARPHRD_ETHER */
#include <netinet/in.h>         /* For struct sockaddr_in */
#include <sys/ioctl.h> 
#include <sys/socket.h> 
#include <net/if.h> 
#include <netinet/if_ether.h> #include <sys/time.h>
#include <string.h>
#include <vector.h>using namespace std;

#include "NetworkHelp.h"#include "inc/network/network_Protocol802_11InstanceObject.h"#include "netdev_lib.h"

/*------------------------------------------------------------------*/
double iw_freq2float(const iw_freq *	in)
{
#ifdef WE_NOLIBM
  /* Version without libm : slower */
  int		i;
  double	res = (double) in->m;
  for(i = 0; i < in->e; i++)
    res *= 10;
  return(res);
#else	/* WE_NOLIBM */
  /* Version with libm : faster */
  return ((double) in->m) * pow(10,in->e);
#endif	/* WE_NOLIBM */
}



int iw_scan_result(char* ifname,int skfd , vector<SApStruct>& apList ) 
{

    int f;
	int totallength;
	int hs;
    struct iwreq req;
    struct iw_event *e;
    uint8_t buffer[8192];
	  

	printf("iwscan_result begin,ifname = %s,sdfd = %d ,WIRELESS_EXT = %d\r\n",ifname,skfd,WIRELESS_EXT);

	apList.clear();
    memset(&req, 0, sizeof(req));
    strncpy(req.ifr_ifrn.ifrn_name, ifname, IFNAMSIZ);
    
    req.u.data.pointer = buffer;
    req.u.data.flags = 0;
    req.u.data.length = sizeof(buffer);
    

    if (ioctl(skfd, SIOCGIWSCAN, &req) < 0) 
	{
		if (errno == EAGAIN)
           return 1;
        printf("ioctl(SIOCGIWSCAN): %s \r\n", strerror(errno) );
        return -1;
    }

	printf("ioctl ok \r\n");

    e = (struct iw_event*) req.u.data.pointer;
    totallength = req.u.data.length;
    f = 0;
    hs = sizeof(struct iw_event)-sizeof(union iwreq_data);

	struct SApStruct theAP;
	// init the APStruct structure
	theAP.bEncrypted = false;
	theAP.byOperatingMode = 0;
	theAP.uiChannel = 0;
	theAP.byEncryptionMode = 0;
	theAP.byBand = 0;
	theAP.szSsid = "";

    while (totallength >= sizeof(struct iw_event)) 
	{

        if (e->len < hs) {
            printf("wireless event is receved");
            return -1;
        }



		

        switch (e->cmd) 
		{
            
            case SIOCGIWAP: 

                f = 1;

                if (e->len < hs + sizeof(struct sockaddr)) {
                    printf(" scan result (1) error");
                    return -1;
                }
				ether_addr* eth ;
				uint8_t addr[ETH_ALEN];
				memcpy(addr, e->u.ap_addr.sa_data, ETH_ALEN);
				printf("ap mac address =");
				eth = (struct ether_addr *)(addr);
				for( int aa = 0;aa < 6;aa++)
				{
					theAP.abyBssid[aa] = eth->ether_addr_octet[aa];
					printf("%02x",theAP.abyBssid[aa]);
				}				
				printf("\r\n");
					
                break;


            case SIOCGIWESSID:
                if (e->len < hs + sizeof(struct iw_point)) {
                    printf("Corrupt scan result (2)");
                    return -1;
                }
               // memset(&ap.essid, 0, sizeof(ap.essid));
               // memcpy(&ap.essid, ((uint8_t*) e)+hs+sizeof(struct iw_point), MIN(sizeof(ap.essid)-1, e->len-hs-sizeof(struct iw_point)));
				char tempessid[0x200];
				memset(tempessid, 0, sizeof(tempessid));
                memcpy(tempessid, ((uint8_t*) e)+IW_EV_LCP_LEN + IW_EV_POINT_OFF, MIN(sizeof(tempessid)-1, e->len ));
				printf("tenpessid = %s\r\n",tempessid);
				
				theAP.szSsid = tempessid;
                
                f |= 2;
                
                break;
                
            case SIOCGIWMODE:

                if (e->len < hs + sizeof(__u32)) {
                    printf( "Corrupt scan result (3)");
                    return -1;
                }
                
                if ((e->u.mode != IW_MODE_INFRA) &&
                    (e->u.mode != IW_MODE_MASTER))
                    f = 0;                          // Ignore non-APs
                else
                    f |= 4;
				if( e->u.mode |=  IW_MODE_INFRA)
				{
					theAP.byOperatingMode = 2; /*Infrastructure; */
					printf("operationMode = Infrastructure\r\n");
				}
				else if( e->u.mode |=  IW_MODE_ADHOC)
				{
					theAP.byOperatingMode = 1; /*Adhoc; */
					printf("operationMode = Adhoc\r\n");
				}
				else
				{
					theAP.byOperatingMode = 0; /*Unknown; */
					printf("operationMode = Unknow\r\n");
				}
                break;

                
            case SIOCGIWFREQ:
                if (e->len < hs + sizeof(struct iw_freq)) {
                    printf( "Corrupt scan result (4)");
                    return -1;
                }

				double		freq;			/* Frequency/channel */
				int		channel ;
				channel = -1;		/* Converted to channel */
				freq = iw_freq2float(&(e->u.freq));
				printf("frequence = %f means channel = ??\r\n",freq);

				// TO BE DUPATED
				/** CAN use this to convert the frequecen to channenl */
				//netdev_ret wnetdev_get_channel(netdev_dev &dev, wnetdev_channel &channel)
				// CAN use the frequence pair 802_11a/b/g to convert frequence to bands */
				if( freq >0 && freq <0x30)
				{
					theAP.uiChannel = freq;
				}
				
                f |= 8;
                break;
			case IWEVQUAL:
				  theAP.iRssi = e->u.qual.qual;
				  printf("theAP.iRssi = %d\r\n",theAP.iRssi);
				// we assume this is the last valid useful command 
				  apList.push_back(theAP);


				  break;
			case SIOCGIWENCODE:
				  theAP.bEncrypted = true;
				  printf("encriped = true \r\n");
				  // TO BE UPDATED
				  break;
			default:
				printf("e->cmd = %x\r\n",e->cmd);
				break;
        }
		
		
       totallength -= e->len;
        e = (struct iw_event*) (((uint8_t*) e) + e->len);
    }
    return 0;
}

int netdev_scan_ap_process(int			skfd, char *			ifname, int			we_version,		vector<SApStruct>& apList)
{
  printf("called netdev_scan_ap_process, skfd = %d,ifname = %s \r\n",skfd,ifname);
  struct iwreq		wrq;

  printf(" begin set APScaning \r\n");
  
  memset(&wrq, 0, sizeof(wrq));
  strncpy(wrq.ifr_ifrn.ifrn_name, ifname, IFNAMSIZ);   

  wrq.u.data.pointer = NULL;		/* Later */
  wrq.u.data.flags = 0;
  wrq.u.data.length = 0;
  
  if((_set_iwreq(ifname, SIOCSIWSCAN, wrq) < 0)	 && (errno != EPERM))
  {
	  printf("will set_iwreq failure \r\n");
	   printf("ioctl(SIOCSIWSCAN): %s \r\n", strerror(errno) );
		return(-1);
  }
	  
   iw_scan_result(ifname, skfd, apList);
   
  return(0);	
}

int netdev_scan_ap(int			skfd, 	char *			ifname,	int			we_version,	vector<SApStruct>& apList)
{
	printf("netdev_scan_ap\r\n");
  int		delay;	

	
  while(1)
  {
      delay = netdev_scan_ap_process(skfd, ifname, we_version, apList);

      if(delay <= 0)
		break;

      usleep(delay * 1000);
   }

  printf("return netdev_scan_ap \r\n");
   return(delay);
}


vector<SApStruct> ScanWifiNetwork( const char *ifname )
{    vector<SApStruct> networks;

	int skfd;

	printf("called ScanWifiNetwork\r\n");
	if(_get_socket(skfd) != NETDEV_SUCCESS)
	{
		return networks;
	}	
	
    int ret = netdev_scan_ap( skfd, ifname, WIRELESS_EXT, networks );
    if ( ( ret == -1 ) && ( errno == ENODATA ) ) {
        // wait for device for one more second :).
        sleep( 1 );

        ret = netdev_scan_ap( skfd, ifname, WIRELESS_EXT, networks );

        if ( ret == -1 ) {
            close ( skfd );
            return networks;
        }
    }

    return networks;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -