home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / iwlib.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-06-22  |  16.5 KB  |  601 lines

  1. /*
  2.  *    Wireless Tools
  3.  *
  4.  *        Jean II - HPLB 97->99 - HPL 99->07
  5.  *
  6.  * Common header for the Wireless Extension library...
  7.  *
  8.  * This file is released under the GPL license.
  9.  *     Copyright (c) 1997-2007 Jean Tourrilhes <jt@hpl.hp.com>
  10.  */
  11.  
  12. #ifndef IWLIB_H
  13. #define IWLIB_H
  14.  
  15. /*#include "CHANGELOG.h"*/
  16.  
  17. /***************************** INCLUDES *****************************/
  18.  
  19. /* Standard headers */
  20. #include <sys/types.h>
  21. #include <sys/ioctl.h>
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <ctype.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <netdb.h>        /* gethostbyname, getnetbyname */
  31. #include <net/ethernet.h>    /* struct ether_addr */
  32. #include <sys/time.h>        /* struct timeval */
  33. #include <unistd.h>
  34.  
  35. /* This is our header selection. Try to hide the mess and the misery :-(
  36.  * Don't look, you would go blind ;-)
  37.  * Note : compatibility with *old* distributions has been removed,
  38.  * you will need Glibc 2.2 and older to compile (which means 
  39.  * Mandrake 8.0, Debian 2.3, RH 7.1 or older).
  40.  */
  41.  
  42. /* Set of headers proposed by Dr. Michael Rietz <rietz@mail.amps.de>, 27.3.2 */
  43. #include <net/if_arp.h>        /* For ARPHRD_ETHER */
  44. #include <sys/socket.h>        /* For AF_INET & struct sockaddr */
  45. #include <netinet/in.h>         /* For struct sockaddr_in */
  46. #include <netinet/if_ether.h>
  47.  
  48. /* Fixup to be able to include kernel includes in userspace.
  49.  * Basically, kill the sparse annotations... Jean II */
  50. #ifndef __user
  51. #define __user
  52. #endif
  53.  
  54. #include <linux/types.h>        /* for "caddr_t" et al        */
  55.  
  56. /* Glibc systems headers are supposedly less problematic than kernel ones */
  57. #include <sys/socket.h>            /* for "struct sockaddr" et al    */
  58. #include <net/if.h>            /* for IFNAMSIZ and co... */
  59.  
  60. /* Private copy of Wireless extensions (in this directoty) */
  61. #include "wireless.h"
  62.  
  63. /* Make gcc understant that when we say inline, we mean it.
  64.  * I really hate when the compiler is trying to be more clever than me,
  65.  * because in this case gcc is not able to figure out functions with a
  66.  * single call site, so not only I have to tag those functions inline
  67.  * by hand, but then it refuse to inline them properly.
  68.  * Total saving for iwevent : 150B = 0.7%.
  69.  * Fortunately, in gcc 3.4, they now automatically inline static functions
  70.  * with a single call site. Hurrah !
  71.  * Jean II */
  72. #undef IW_GCC_HAS_BROKEN_INLINE
  73. #if __GNUC__ == 3
  74. #if __GNUC_MINOR__ >= 1 && __GNUC_MINOR__ < 4
  75. #define IW_GCC_HAS_BROKEN_INLINE    1
  76. #endif    /* __GNUC_MINOR__ */
  77. #endif    /* __GNUC__ */
  78. /* However, gcc 4.0 has introduce a new "feature", when compiling with
  79.  * '-Os', it does not want to inline iw_ether_cmp() and friends.
  80.  * So, we need to fix inline again !
  81.  * Jean II */
  82. #if __GNUC__ == 4
  83. #define IW_GCC_HAS_BROKEN_INLINE    1
  84. #endif    /* __GNUC__ */
  85. /* Now, really fix the inline */
  86. #ifdef IW_GCC_HAS_BROKEN_INLINE
  87. #ifdef inline
  88. #undef inline
  89. #endif    /* inline */
  90. #define inline        inline        __attribute__((always_inline))
  91. #endif    /* IW_GCC_HAS_BROKEN_INLINE */
  92.  
  93. #ifdef __cplusplus
  94. extern "C" {
  95. #endif
  96.  
  97. /****************************** DEBUG ******************************/
  98.  
  99. //#define DEBUG 1
  100.  
  101. /************************ CONSTANTS & MACROS ************************/
  102.  
  103. /* Various versions information */
  104. /* Recommended Wireless Extension version */
  105. #define WE_VERSION    21
  106. /* Maximum forward compatibility built in this version of WT */
  107. #define WE_MAX_VERSION    22
  108. /* Version of Wireless Tools */
  109. #define WT_VERSION    29
  110.  
  111. /* Paths */
  112. #define PROC_NET_WIRELESS    "/proc/net/wireless"
  113. #define PROC_NET_DEV        "/proc/net/dev"
  114.  
  115. /* Some usefull constants */
  116. #define KILO    1e3
  117. #define MEGA    1e6
  118. #define GIGA    1e9
  119. /* For doing log10/exp10 without libm */
  120. #define LOG10_MAGIC    1.25892541179
  121.  
  122. /* Backward compatibility for network headers */
  123. #ifndef ARPHRD_IEEE80211
  124. #define ARPHRD_IEEE80211 801        /* IEEE 802.11            */
  125. #endif /* ARPHRD_IEEE80211 */
  126.  
  127. #ifndef IW_EV_LCP_PK_LEN
  128. /* Size of the Event prefix when packed in stream */
  129. #define IW_EV_LCP_PK_LEN    (4)
  130. /* Size of the various events when packed in stream */
  131. #define IW_EV_CHAR_PK_LEN    (IW_EV_LCP_PK_LEN + IFNAMSIZ)
  132. #define IW_EV_UINT_PK_LEN    (IW_EV_LCP_PK_LEN + sizeof(__u32))
  133. #define IW_EV_FREQ_PK_LEN    (IW_EV_LCP_PK_LEN + sizeof(struct iw_freq))
  134. #define IW_EV_PARAM_PK_LEN    (IW_EV_LCP_PK_LEN + sizeof(struct iw_param))
  135. #define IW_EV_ADDR_PK_LEN    (IW_EV_LCP_PK_LEN + sizeof(struct sockaddr))
  136. #define IW_EV_QUAL_PK_LEN    (IW_EV_LCP_PK_LEN + sizeof(struct iw_quality))
  137. #define IW_EV_POINT_PK_LEN    (IW_EV_LCP_PK_LEN + 4)
  138.  
  139. struct iw_pk_event
  140. {
  141.     __u16        len;            /* Real lenght of this stuff */
  142.     __u16        cmd;            /* Wireless IOCTL */
  143.     union iwreq_data    u;        /* IOCTL fixed payload */
  144. } __attribute__ ((packed));
  145. struct    iw_pk_point
  146. {
  147.   void __user    *pointer;    /* Pointer to the data  (in user space) */
  148.   __u16        length;        /* number of fields or size in bytes */
  149.   __u16        flags;        /* Optional params */
  150. } __attribute__ ((packed));
  151.  
  152. #define IW_EV_LCP_PK2_LEN    (sizeof(struct iw_pk_event) - sizeof(union iwreq_data))
  153. #define IW_EV_POINT_PK2_LEN    (IW_EV_LCP_PK2_LEN + sizeof(struct iw_pk_point) - IW_EV_POINT_OFF)
  154.  
  155. #endif    /* IW_EV_LCP_PK_LEN */
  156.  
  157. /****************************** TYPES ******************************/
  158.  
  159. /* Shortcuts */
  160. typedef struct iw_statistics    iwstats;
  161. typedef struct iw_range        iwrange;
  162. typedef struct iw_param        iwparam;
  163. typedef struct iw_freq        iwfreq;
  164. typedef struct iw_quality    iwqual;
  165. typedef struct iw_priv_args    iwprivargs;
  166. typedef struct sockaddr        sockaddr;
  167.  
  168. /* Structure for storing all wireless information for each device
  169.  * This is a cut down version of the one above, containing only
  170.  * the things *truly* needed to configure a card.
  171.  * Don't add other junk, I'll remove it... */
  172. typedef struct wireless_config
  173. {
  174.   char        name[IFNAMSIZ + 1];    /* Wireless/protocol name */
  175.   int        has_nwid;
  176.   iwparam    nwid;            /* Network ID */
  177.   int        has_freq;
  178.   double    freq;            /* Frequency/channel */
  179.   int        freq_flags;
  180.   int        has_key;
  181.   unsigned char    key[IW_ENCODING_TOKEN_MAX];    /* Encoding key used */
  182.   int        key_size;        /* Number of bytes */
  183.   int        key_flags;        /* Various flags */
  184.   int        has_essid;
  185.   int        essid_on;
  186.   char        essid[IW_ESSID_MAX_SIZE + 1];    /* ESSID (extended network) */
  187.   int        has_mode;
  188.   int        mode;            /* Operation mode */
  189. } wireless_config;
  190.  
  191. /* Structure for storing all wireless information for each device
  192.  * This is pretty exhaustive... */
  193. typedef struct wireless_info
  194. {
  195.   struct wireless_config    b;    /* Basic information */
  196.  
  197.   int        has_sens;
  198.   iwparam    sens;            /* sensitivity */
  199.   int        has_nickname;
  200.   char        nickname[IW_ESSID_MAX_SIZE + 1]; /* NickName */
  201.   int        has_ap_addr;
  202.   sockaddr    ap_addr;        /* Access point address */
  203.   int        has_bitrate;
  204.   iwparam    bitrate;        /* Bit rate in bps */
  205.   int        has_rts;
  206.   iwparam    rts;            /* RTS threshold in bytes */
  207.   int        has_frag;
  208.   iwparam    frag;            /* Fragmentation threshold in bytes */
  209.   int        has_power;
  210.   iwparam    power;            /* Power management parameters */
  211.   int        has_txpower;
  212.   iwparam    txpower;        /* Transmit Power in dBm */
  213.   int        has_retry;
  214.   iwparam    retry;            /* Retry limit or lifetime */
  215.  
  216.   /* Stats */
  217.   iwstats    stats;
  218.   int        has_stats;
  219.   iwrange    range;
  220.   int        has_range;
  221.  
  222.   /* Auth params for WPA/802.1x/802.11i */
  223.   int        auth_key_mgmt;
  224.   int        has_auth_key_mgmt;
  225.   int        auth_cipher_pairwise;
  226.   int        has_auth_cipher_pairwise;
  227.   int        auth_cipher_group;
  228.   int        has_auth_cipher_group;
  229. } wireless_info;
  230.  
  231. /* Structure for storing an entry of a wireless scan.
  232.  * This is only a subset of all possible information, the flexible
  233.  * structure of scan results make it impossible to capture all
  234.  * information in such a static structure. */
  235. typedef struct wireless_scan
  236. {
  237.   /* Linked list */
  238.   struct wireless_scan *    next;
  239.  
  240.   /* Cell identifiaction */
  241.   int        has_ap_addr;
  242.   sockaddr    ap_addr;        /* Access point address */
  243.  
  244.   /* Other information */
  245.   struct wireless_config    b;    /* Basic information */
  246.   iwstats    stats;            /* Signal strength */
  247.   int        has_stats;
  248.   iwparam    maxbitrate;        /* Max bit rate in bps */
  249.   int        has_maxbitrate;
  250. } wireless_scan;
  251.  
  252. /*
  253.  * Context used for non-blocking scan.
  254.  */
  255. typedef struct wireless_scan_head
  256. {
  257.   wireless_scan *    result;        /* Result of the scan */
  258.   int            retry;        /* Retry level */
  259. } wireless_scan_head;
  260.  
  261. /* Structure used for parsing event streams, such as Wireless Events
  262.  * and scan results */
  263. typedef struct stream_descr
  264. {
  265.   char *    end;        /* End of the stream */
  266.   char *    current;    /* Current event in stream of events */
  267.   char *    value;        /* Current value in event */
  268. } stream_descr;
  269.  
  270. /* Prototype for handling display of each single interface on the
  271.  * system - see iw_enum_devices() */
  272. typedef int (*iw_enum_handler)(int    skfd,
  273.                    char *    ifname,
  274.                    char *    args[],
  275.                    int    count);
  276.  
  277. /* Describe a modulation */
  278. typedef struct iw_modul_descr
  279. {
  280.   unsigned int        mask;        /* Modulation bitmask */
  281.   char            cmd[8];        /* Short name */
  282.   char *        verbose;    /* Verbose description */
  283. } iw_modul_descr;
  284.  
  285. /**************************** PROTOTYPES ****************************/
  286. /*
  287.  * All the functions in iwcommon.c
  288.  */
  289.  
  290. /* ---------------------- SOCKET SUBROUTINES -----------------------*/
  291. int
  292.     iw_sockets_open(void);
  293. void
  294.     iw_enum_devices(int        skfd,
  295.             iw_enum_handler fn,
  296.             char *        args[],
  297.             int        count);
  298. /* --------------------- WIRELESS SUBROUTINES ----------------------*/
  299. int
  300.     iw_get_kernel_we_version(void);
  301. int
  302.     iw_print_version_info(const char *    toolname);
  303. int
  304.     iw_get_range_info(int        skfd,
  305.               const char *    ifname,
  306.               iwrange *    range);
  307. int
  308.     iw_get_priv_info(int        skfd,
  309.              const char *    ifname,
  310.              iwprivargs **    ppriv);
  311. int
  312.     iw_get_basic_config(int            skfd,
  313.                 const char *    ifname,
  314.                 wireless_config *    info);
  315. int
  316.     iw_set_basic_config(int            skfd,
  317.                 const char *    ifname,
  318.                 wireless_config *    info);
  319. /* --------------------- PROTOCOL SUBROUTINES --------------------- */
  320. int
  321.     iw_protocol_compare(const char *    protocol1,
  322.                 const char *    protocol2);
  323. /* -------------------- FREQUENCY SUBROUTINES --------------------- */
  324. void
  325.     iw_float2freq(double    in,
  326.               iwfreq *    out);
  327. double
  328.     iw_freq2float(const iwfreq *    in);
  329. void
  330.     iw_print_freq_value(char *    buffer,
  331.                 int        buflen,
  332.                 double    freq);
  333. void
  334.     iw_print_freq(char *    buffer,
  335.               int    buflen,
  336.               double    freq,
  337.               int    channel,
  338.               int    freq_flags);
  339. int
  340.     iw_freq_to_channel(double            freq,
  341.                const struct iw_range *    range);
  342. int
  343.     iw_channel_to_freq(int                channel,
  344.                double *            pfreq,
  345.                const struct iw_range *    range);
  346. void
  347.     iw_print_bitrate(char *    buffer,
  348.              int    buflen,
  349.              int    bitrate);
  350. /* ---------------------- POWER SUBROUTINES ----------------------- */
  351. int
  352.     iw_dbm2mwatt(int    in);
  353. int
  354.     iw_mwatt2dbm(int    in);
  355. void
  356.     iw_print_txpower(char *            buffer,
  357.              int            buflen,
  358.              struct iw_param *    txpower);
  359. /* -------------------- STATISTICS SUBROUTINES -------------------- */
  360. int
  361.     iw_get_stats(int        skfd,
  362.              const char *    ifname,
  363.              iwstats *        stats,
  364.              const iwrange *    range,
  365.              int        has_range);
  366. void
  367.     iw_print_stats(char *        buffer,
  368.                int        buflen,
  369.                const iwqual *    qual,
  370.                const iwrange *    range,
  371.                int        has_range);
  372. /* --------------------- ENCODING SUBROUTINES --------------------- */
  373. void
  374.     iw_print_key(char *            buffer,
  375.              int            buflen,
  376.              const unsigned char *    key,
  377.              int            key_size,
  378.              int            key_flags);
  379. int
  380.     iw_in_key(const char *        input,
  381.           unsigned char *    key);
  382. int
  383.     iw_in_key_full(int        skfd,
  384.                const char *    ifname,
  385.                const char *    input,
  386.                unsigned char *    key,
  387.                __u16 *        flags);
  388. /* ----------------- POWER MANAGEMENT SUBROUTINES ----------------- */
  389. void
  390.     iw_print_pm_value(char *    buffer,
  391.               int        buflen,
  392.               int        value,
  393.               int        flags,
  394.               int        we_version);
  395. void
  396.     iw_print_pm_mode(char *        buffer,
  397.              int        buflen,
  398.              int        flags);
  399. /* --------------- RETRY LIMIT/LIFETIME SUBROUTINES --------------- */
  400. void
  401.     iw_print_retry_value(char *    buffer,
  402.                  int    buflen,
  403.                  int    value,
  404.                  int    flags,
  405.                  int    we_version);
  406. /* ----------------------- TIME SUBROUTINES ----------------------- */
  407. void
  408.     iw_print_timeval(char *                buffer,
  409.              int                buflen,
  410.              const struct timeval *        time,
  411.              const struct timezone *    tz);
  412. /* --------------------- ADDRESS SUBROUTINES ---------------------- */
  413. int
  414.     iw_check_mac_addr_type(int        skfd,
  415.                    const char *    ifname);
  416. int
  417.     iw_check_if_addr_type(int        skfd,
  418.                   const char *    ifname);
  419. #if 0
  420. int
  421.     iw_check_addr_type(int        skfd,
  422.                const char *    ifname);
  423. #endif
  424. #if 0
  425. int
  426.     iw_get_mac_addr(int            skfd,
  427.             const char *        name,
  428.             struct ether_addr *    eth,
  429.             unsigned short *    ptype);
  430. #endif
  431. char *
  432.     iw_mac_ntop(const unsigned char *    mac,
  433.             int                maclen,
  434.             char *            buf,
  435.             int                buflen);
  436. void
  437.     iw_ether_ntop(const struct ether_addr *    eth,
  438.               char *            buf);
  439. char *
  440.     iw_sawap_ntop(const struct sockaddr *    sap,
  441.               char *            buf);
  442. int
  443.     iw_mac_aton(const char *    orig,
  444.             unsigned char *    mac,
  445.             int            macmax);
  446. int
  447.     iw_ether_aton(const char* bufp, struct ether_addr* eth);
  448. int
  449.     iw_in_inet(char *bufp, struct sockaddr *sap);
  450. int
  451.     iw_in_addr(int            skfd,
  452.            const char *        ifname,
  453.            char *        bufp,
  454.            struct sockaddr *    sap);
  455. /* ----------------------- MISC SUBROUTINES ------------------------ */
  456. int
  457.     iw_get_priv_size(int        args);
  458.  
  459. /* ---------------------- EVENT SUBROUTINES ---------------------- */
  460. void
  461.     iw_init_event_stream(struct stream_descr *    stream,
  462.                  char *            data,
  463.                  int            len);
  464. int
  465.     iw_extract_event_stream(struct stream_descr *    stream,
  466.                 struct iw_event *    iwe,
  467.                 int            we_version);
  468. /* --------------------- SCANNING SUBROUTINES --------------------- */
  469. int
  470.     iw_process_scan(int            skfd,
  471.             char *            ifname,
  472.             int            we_version,
  473.             wireless_scan_head *    context);
  474. int
  475.     iw_scan(int            skfd,
  476.         char *            ifname,
  477.         int            we_version,
  478.         wireless_scan_head *    context);
  479.  
  480. /**************************** VARIABLES ****************************/
  481.  
  482. /* Modes as human readable strings */
  483. extern const char * const    iw_operation_mode[];
  484. #define IW_NUM_OPER_MODE    7
  485. #define IW_NUM_OPER_MODE_EXT    8
  486.  
  487. /* Modulations as human readable strings */
  488. extern const struct iw_modul_descr    iw_modul_list[];
  489. #define IW_SIZE_MODUL_LIST    16
  490.  
  491. /************************* INLINE FUNTIONS *************************/
  492. /*
  493.  * Functions that are so simple that it's more efficient inlining them
  494.  */
  495.  
  496. /*
  497.  * Note : I've defined wrapper for the ioctl request so that
  498.  * it will be easier to migrate to other kernel API if needed
  499.  */
  500.  
  501. /*------------------------------------------------------------------*/
  502. /*
  503.  * Wrapper to push some Wireless Parameter in the driver
  504.  */
  505. static inline int
  506. iw_set_ext(int            skfd,        /* Socket to the kernel */
  507.        const char *        ifname,        /* Device name */
  508.        int            request,    /* WE ID */
  509.        struct iwreq *    pwrq)        /* Fixed part of the request */
  510. {
  511.   /* Set device name */
  512.   strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
  513.   /* Do the request */
  514.   return(ioctl(skfd, request, pwrq));
  515. }
  516.  
  517. /*------------------------------------------------------------------*/
  518. /*
  519.  * Wrapper to extract some Wireless Parameter out of the driver
  520.  */
  521. static inline int
  522. iw_get_ext(int            skfd,        /* Socket to the kernel */
  523.        const char *        ifname,        /* Device name */
  524.        int            request,    /* WE ID */
  525.        struct iwreq *    pwrq)        /* Fixed part of the request */
  526. {
  527.   /* Set device name */
  528.   strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
  529.   /* Do the request */
  530.   return(ioctl(skfd, request, pwrq));
  531. }
  532.  
  533. /*------------------------------------------------------------------*/
  534. /*
  535.  * Close the socket used for ioctl.
  536.  */
  537. static inline void
  538. iw_sockets_close(int    skfd)
  539. {
  540.   close(skfd);
  541. }
  542.  
  543. /*------------------------------------------------------------------*/
  544. /*
  545.  * Display an Ethernet Socket Address in readable format.
  546.  */
  547. static inline char *
  548. iw_saether_ntop(const struct sockaddr *sap, char* bufp)
  549. {
  550.   iw_ether_ntop((const struct ether_addr *) sap->sa_data, bufp);
  551.   return bufp;
  552. }
  553. /*------------------------------------------------------------------*/
  554. /*
  555.  * Input an Ethernet Socket Address and convert to binary.
  556.  */
  557. static inline int
  558. iw_saether_aton(const char *bufp, struct sockaddr *sap)
  559. {
  560.   sap->sa_family = ARPHRD_ETHER;
  561.   return iw_ether_aton(bufp, (struct ether_addr *) sap->sa_data);
  562. }
  563.  
  564. /*------------------------------------------------------------------*/
  565. /*
  566.  * Create an Ethernet broadcast address
  567.  */
  568. static inline void
  569. iw_broad_ether(struct sockaddr *sap)
  570. {
  571.   sap->sa_family = ARPHRD_ETHER;
  572.   memset((char *) sap->sa_data, 0xFF, ETH_ALEN);
  573. }
  574.  
  575. /*------------------------------------------------------------------*/
  576. /*
  577.  * Create an Ethernet NULL address
  578.  */
  579. static inline void
  580. iw_null_ether(struct sockaddr *sap)
  581. {
  582.   sap->sa_family = ARPHRD_ETHER;
  583.   memset((char *) sap->sa_data, 0x00, ETH_ALEN);
  584. }
  585.  
  586. /*------------------------------------------------------------------*/
  587. /*
  588.  * Compare two ethernet addresses
  589.  */
  590. static inline int
  591. iw_ether_cmp(const struct ether_addr* eth1, const struct ether_addr* eth2)
  592. {
  593.   return memcmp(eth1, eth2, sizeof(*eth1));
  594. }
  595.  
  596. #ifdef __cplusplus
  597. }
  598. #endif
  599.  
  600. #endif    /* IWLIB_H */
  601.