Chinaunix首页 | 论坛 | 博客
  • 博客访问: 517242
  • 博文数量: 77
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 689
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-12 08:40
文章分类

全部博文(77)

文章存档

2018年(1)

2016年(3)

2015年(24)

2014年(49)

我的朋友

分类: 嵌入式

2014-09-04 11:54:31


以下为自己实现:

点击(此处)折叠或打开


  1. 源码的本质其实也就是调用了iw_get_ext()里的ioctl()

  2.     int skfd1 = 0;

  3.     skfd1 = iw_sockets_open();
  4.     if(skfd1 < 0)
  5.     {
  6.         printf("%s:%d Open socket error!\n", __FILE__, __LINE__);
  7.     iw_sockets_close(skfd1);
  8.     exit(-1);
  9.     }

  10.    /*Get AP address*/
  11.     int i;

  12.     ioctl(fd,SIOCGIWAP,&wrq);
  13.     printf("wrq.u.ap_addr:");
  14.     unsigned char *APaddr = (unsigned char *)wrq.u.ap_addr.sa_data;
  15.     for(i = 0;i < 6;i++)
  16.     {
  17.     printf("%02x",(int)APaddr[i]);
  18.     if(i != 5)
  19.      printf("%c",':');
  20.     else
  21.     printf("\n");
  22.     }

点击(此处)折叠或打开

  1. /* Get ESSID */

  2.    char buffer[32];
  3.     memset(buffer,0,32);
  4.     wreq->u.essid.pointer = buffer;
  5.     wreq->u.essid.length = 32;
  6.     ioctl(skfd1,SIOCGIWESSID,wreq);
  7.     printf("ESSID:%s\n",(char *)wreq->u.essid.pointer);

  8.     if(strcmp((char *)wrq.u.essid.pointer,"") == 0)
  9.     {
  10.     return -1;
  11.     }

iwconfig 源码: (iwconfig wlan0)

点击(此处)折叠或打开

  1. typedef struct wireless_config
  2. {
  3.   char        name[IFNAMSIZ + 1];    /* Wireless/protocol name */
  4.   int        has_nwid;
  5.   iwparam    nwid;            /* Network ID */
  6.   int        has_freq;
  7.   double    freq;            /* Frequency/channel */
  8.   int        freq_flags;
  9.   int        has_key;
  10.   unsigned char    key[IW_ENCODING_TOKEN_MAX];    /* Encoding key used */
  11.   int        key_size;        /* Number of bytes */
  12.   int        key_flags;        /* Various flags */
  13.   int        has_essid;
  14.   int        essid_on;
  15.   char        essid[IW_ESSID_MAX_SIZE + 1];    /* ESSID (extended network) */
  16.   int        has_mode;
  17.   int        mode;            /* Operation mode */
  18. } wireless_config;

点击(此处)折叠或打开

  1. typedef struct wireless_info
  2. {
  3.     struct wireless_config    b;    /* Basic information */

  4.     int        has_sens;
  5.     iwparam    sens;            /* sensitivity */
  6.     int        has_nickname;
  7.     char        nickname[IW_ESSID_MAX_SIZE + 1]; /* NickName */
  8.     int        has_ap_addr;
  9.     sockaddr    ap_addr;        /* Access point address */
  10.     int        has_bitrate;
  11.     iwparam    bitrate;        /* Bit rate in bps */
  12.     int        has_rts;
  13.     iwparam    rts;            /* RTS threshold in bytes */
  14.     int        has_frag;
  15.     iwparam    frag;            /* Fragmentation threshold in bytes */
  16.     int        has_power;
  17.     iwparam    power;            /* Power management parameters */
  18.     int        has_txpower;
  19.     iwparam    txpower;        /* Transmit Power in dBm */
  20.     int        has_retry;
  21.     iwparam    retry;            /* Retry limit or lifetime */

  22.     /* Stats */
  23.     iwstats    stats;
  24.     int        has_stats;
  25.     iwrange    range;
  26.     int        has_range;

  27.     /* Auth params for WPA/802.1x/802.11i */
  28.     int        auth_key_mgmt;
  29.     int        has_auth_key_mgmt;
  30.     int        auth_cipher_pairwise;
  31.     int        has_auth_cipher_pairwise;
  32.     int        auth_cipher_group;
  33.     int        has_auth_cipher_group;
  34. } wireless_info;

  35. struct sockaddr {
  36.     sa_family_t    sa_family;    /* address family, AF_xxx    */
  37.     char        sa_data[14];    /* 14 bytes of protocol address    */
  38. };

  39. union    iwreq_data
  40. {
  41.     /* Config - generic */
  42.     char        name[IFNAMSIZ];
  43.     /* Name : used to verify the presence of wireless extensions.
  44.      * Name of the protocol/provider... */

  45.     struct iw_point    essid;        /* Extended network name */
  46.     struct iw_param    nwid;        /* network id (or domain - the cell) */
  47.     struct iw_freq    freq;        /* frequency or channel :
  48.                      * 0-1000 = channel
  49.                      * > 1000 = frequency in Hz */

  50.     struct iw_param    sens;        /* signal level threshold */
  51.     struct iw_param    bitrate;    /* default bit rate */
  52.     struct iw_param    txpower;    /* default transmit power */
  53.     struct iw_param    rts;        /* RTS threshold threshold */
  54.     struct iw_param    frag;        /* Fragmentation threshold */
  55.     __u32        mode;        /* Operation mode */
  56.     struct iw_param    retry;        /* Retry limits & lifetime */

  57.     struct iw_point    encoding;    /* Encoding stuff : tokens */
  58.     struct iw_param    power;        /* PM duration/timeout */
  59.     struct iw_quality qual;        /* Quality part of statistics */

  60.     struct sockaddr    ap_addr;    /* Access point address */
  61.     struct sockaddr    addr;        /* Destination address (hw/mac) */

  62.     struct iw_param    param;        /* Other small parameters */
  63.     struct iw_point    data;        /* Other large parameters */
  64. };

  65. struct    iwreq
  66. {
  67.     union
  68.     {
  69.         char    ifrn_name[IFNAMSIZ];    /* if name, e.g. "eth0" */
  70.     } ifr_ifrn;

  71.     /* Data part (defined just above) */
  72.     union    iwreq_data    u;
  73. };

  74. /*
  75.  * Display an Ethernet address in readable format.
  76.  */
  77. void iw_ether_ntop(const struct ether_addr *eth,char *buf)
  78. {
  79.     sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
  80.             eth->ether_addr_octet[0], eth->ether_addr_octet[1],
  81.             eth->ether_addr_octet[2], eth->ether_addr_octet[3],
  82.             eth->ether_addr_octet[4], eth->ether_addr_octet[5]);
  83. }

  84. char *iw_sawap_ntop(const struct sockaddr *sap,char *buf)
  85. {
  86.     const struct ether_addr ether_zero = {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }};
  87.     const struct ether_addr ether_bcast = {{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }};
  88.     const struct ether_addr ether_hack = {{ 0x44, 0x44, 0x44, 0x44, 0x44, 0x44 }};
  89.     const struct ether_addr * ether_wap = (const struct ether_addr *) sap->sa_data;

  90.     if(!iw_ether_cmp(ether_wap, &ether_zero))
  91.         sprintf(buf, "Not-Associated");
  92.     else
  93.         //............
  94.         iw_ether_ntop(ether_wap, buf);
  95.     return(buf);
  96. }

  97. /*------------------------------------------------------------------*/
  98. /*
  99.  * Wrapper to extract some Wireless Parameter out of the driver
  100.  */
  101. static inline int iw_get_ext(int            skfd,        /* Socket to the kernel */
  102.         const char *        ifname,        /* Device name */
  103.         int            request,    /* WE ID */
  104.         struct iwreq *    pwrq)        /* Fixed part of the request */
  105. {
  106.     /* Set device name */
  107.     strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
  108.     /* Do the request */
  109.     return(ioctl(skfd, request, pwrq));
  110. }

  111. int iw_get_basic_config(int    skfd,const char *ifname,wireless_config *info)
  112. {
  113.     struct iwreq        wrq;

  114.     memset((char *) info, 0, sizeof(struct wireless_config));
  115.     if(iw_get_ext(skfd, ifname, SIOCGIWNAME, &wrq) < 0)
  116.     {
  117.     }
  118.     /* Get wireless name */
  119.     /* Get network ID */
  120.     /* Get frequency / channel */
  121.     /* Get encryption information */
  122.     /* Get ESSID */
  123.     //.............

  124. }

  125. /*
  126.  * Print on the screen in a neat fashion all the info we have collected
  127.  * on a device.
  128.  */
  129. static void display_info(struct wireless_info *info,char *    ifname)
  130. {
  131.     /* Display the address of the current Access Point */
  132.     if(info->has_ap_addr)
  133.     {
  134.         /* A bit of clever formatting */
  135.         if(tokens > 8)
  136.         {
  137.             printf("\n ");
  138.             tokens = 0;
  139.         }
  140.         tokens +=6;

  141.         /* Oups ! No Access Point in Ad-Hoc mode */
  142.         if((info->b.has_mode) && (info->b.mode == IW_MODE_ADHOC))
  143.             printf("Cell:");
  144.         else
  145.             printf("Access Point:");
  146.         printf(" %s ", iw_sawap_ntop(&info->ap_addr, buffer));
  147.     }
  148. }

  149. static int get_info(int    skfd,char *ifname,struct wireless_info *info)
  150. {
  151.     struct iwreq    wrq;

  152.     memset((char *) info, 0, sizeof(struct wireless_info));

  153.     /* Get basic information */
  154.     if(iw_get_basic_config(skfd, ifname, &(info->b)) < 0)
  155.     {
  156.     }
  157.     
  158.      /* Get AP address */
  159.     if(iw_get_ext(skfd, ifname, SIOCGIWAP, &wrq) >= 0)
  160.     {
  161.         info->has_ap_addr = 1;
  162.         memcpy(&(info->ap_addr), &(wrq.u.ap_addr), sizeof (sockaddr));
  163.     }


  164. static int print_info(int    skfd,char *ifname,char *args[],int count)
  165. {
  166.     struct wireless_info    info;
  167.     int    rc;

  168.     /* Avoid "Unused parameter" warning */
  169.     args = args; count = count;

  170.     rc = get_info(skfd, ifname, &info);
  171.     switch(rc)
  172.     {
  173.     case 0:    /* Success */
  174.         /* Display it ! */
  175.         display_info(&info, ifname);
  176.         break;

  177.         return(rc);
  178.     }

  179. // iwconfig waln0
  180. int main(int argc,char **argv)
  181.     {
  182.         int skfd;        /* generic raw socket desc.    */

  183.         /* Create a channel to the NET kernel. */
  184.         if((skfd = iw_sockets_open()) < 0)
  185.         {
  186.             perror("socket");
  187.             exit(-1);
  188.         }
  189.         /* The device name must be the first argument */
  190.         if(argc == 2)
  191.             print_info(skfd, argv[1], NULL, 0);

  192.         /* Close the socket. */
  193.         iw_sockets_close(skfd);
  194.     }

/* -------------------------- IOCTL LIST -------------------------- */

点击(此处)折叠或打开

  1. #define SIOCGIWNAME    0x8B01        /* get name == wireless protocol */

  2. #define SIOCSIWFREQ    0x8B04        /* set channel/frequency (Hz) */
  3. #define SIOCGIWFREQ    0x8B05        /* get channel/frequency (Hz) */
  4. #define SIOCSIWMODE    0x8B06        /* set operation mode */
  5. #define SIOCGIWMODE    0x8B07        /* get operation mode */

  6. #define SIOCSIWAP    0x8B14        /* set access point MAC addresses */
  7. #define SIOCGIWAP    0x8B15        /* get access point MAC addresses */

  8. #define SIOCSIWESSID    0x8B1A        /* set ESSID (network name) */
  9. #define SIOCGIWESSID    0x8B1B        /* get ESSID */

  10. ...................



阅读(3565) | 评论(0) | 转发(0) |
0

上一篇:ftok的陷阱

下一篇:Linux串口(/dev/tty*)通信

给主人留下些什么吧!~~