iwlib.h
Go to the documentation of this file.
00001 /*
00002  *      Wireless Tools
00003  *
00004  *              Jean II - HPLB 97->99 - HPL 99->07
00005  *
00006  * Common header for the Wireless Extension library...
00007  *
00008  * This file is released under the GPL license.
00009  *     Copyright (c) 1997-2007 Jean Tourrilhes <jt@hpl.hp.com>
00010  */
00011 
00012 #ifndef IWLIB_H
00013 #define IWLIB_H
00014 
00015 /*#include "CHANGELOG.h"*/
00016 
00017 /***************************** INCLUDES *****************************/
00018 
00019 /* Standard headers */
00020 #include <sys/types.h>
00021 #include <sys/ioctl.h>
00022 #include <stdio.h>
00023 #include <math.h>
00024 #include <errno.h>
00025 #include <fcntl.h>
00026 #include <ctype.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <unistd.h>
00030 #include <netdb.h>              /* gethostbyname, getnetbyname */
00031 #include <net/ethernet.h>       /* struct ether_addr */
00032 #include <sys/time.h>           /* struct timeval */
00033 #include <unistd.h>
00034 
00035 /* This is our header selection. Try to hide the mess and the misery :-(
00036  * Don't look, you would go blind ;-)
00037  * Note : compatibility with *old* distributions has been removed,
00038  * you will need Glibc 2.2 and older to compile (which means 
00039  * Mandrake 8.0, Debian 2.3, RH 7.1 or older).
00040  */
00041 
00042 /* Set of headers proposed by Dr. Michael Rietz <rietz@mail.amps.de>, 27.3.2 */
00043 #include <net/if_arp.h>         /* For ARPHRD_ETHER */
00044 #include <sys/socket.h>         /* For AF_INET & struct sockaddr */
00045 #include <netinet/in.h>         /* For struct sockaddr_in */
00046 #include <netinet/if_ether.h>
00047 
00048 /* Fixup to be able to include kernel includes in userspace.
00049  * Basically, kill the sparse annotations... Jean II */
00050 #ifndef __user
00051 #define __user
00052 #endif
00053 
00054 #include <linux/types.h>                /* for "caddr_t" et al          */
00055 
00056 /* Glibc systems headers are supposedly less problematic than kernel ones */
00057 #include <sys/socket.h>                 /* for "struct sockaddr" et al  */
00058 #include <net/if.h>                     /* for IFNAMSIZ and co... */
00059 
00060 /* Private copy of Wireless extensions (in this directoty) */
00061 #include "wireless.h"
00062 
00063 /* Make gcc understant that when we say inline, we mean it.
00064  * I really hate when the compiler is trying to be more clever than me,
00065  * because in this case gcc is not able to figure out functions with a
00066  * single call site, so not only I have to tag those functions inline
00067  * by hand, but then it refuse to inline them properly.
00068  * Total saving for iwevent : 150B = 0.7%.
00069  * Fortunately, in gcc 3.4, they now automatically inline static functions
00070  * with a single call site. Hurrah !
00071  * Jean II */
00072 #undef IW_GCC_HAS_BROKEN_INLINE
00073 #if __GNUC__ == 3
00074 #if __GNUC_MINOR__ >= 1 && __GNUC_MINOR__ < 4
00075 #define IW_GCC_HAS_BROKEN_INLINE        1
00076 #endif  /* __GNUC_MINOR__ */
00077 #endif  /* __GNUC__ */
00078 /* However, gcc 4.0 has introduce a new "feature", when compiling with
00079  * '-Os', it does not want to inline iw_ether_cmp() and friends.
00080  * So, we need to fix inline again !
00081  * Jean II */
00082 #if __GNUC__ == 4
00083 #define IW_GCC_HAS_BROKEN_INLINE        1
00084 #endif  /* __GNUC__ */
00085 /* Now, really fix the inline */
00086 #ifdef IW_GCC_HAS_BROKEN_INLINE
00087 #ifdef inline
00088 #undef inline
00089 #endif  /* inline */
00090 #define inline          inline          __attribute__((always_inline))
00091 #endif  /* IW_GCC_HAS_BROKEN_INLINE */
00092 
00093 #ifdef __cplusplus
00094 extern "C" {
00095 #endif
00096 
00097 /****************************** DEBUG ******************************/
00098 
00099 //#define DEBUG 1
00100 
00101 /************************ CONSTANTS & MACROS ************************/
00102 
00103 /* Various versions information */
00104 /* Recommended Wireless Extension version */
00105 #define WE_VERSION      21
00106 /* Maximum forward compatibility built in this version of WT */
00107 #define WE_MAX_VERSION  22
00108 /* Version of Wireless Tools */
00109 #define WT_VERSION      29
00110 
00111 /* Paths */
00112 #define PROC_NET_WIRELESS       "/proc/net/wireless"
00113 #define PROC_NET_DEV            "/proc/net/dev"
00114 
00115 /* Some usefull constants */
00116 #define KILO    1e3
00117 #define MEGA    1e6
00118 #define GIGA    1e9
00119 /* For doing log10/exp10 without libm */
00120 #define LOG10_MAGIC     1.25892541179
00121 
00122 /* Backward compatibility for network headers */
00123 #ifndef ARPHRD_IEEE80211
00124 #define ARPHRD_IEEE80211 801            /* IEEE 802.11                  */
00125 #endif /* ARPHRD_IEEE80211 */
00126 
00127 #ifndef IW_EV_LCP_PK_LEN
00128 /* Size of the Event prefix when packed in stream */
00129 #define IW_EV_LCP_PK_LEN        (4)
00130 /* Size of the various events when packed in stream */
00131 #define IW_EV_CHAR_PK_LEN       (IW_EV_LCP_PK_LEN + IFNAMSIZ)
00132 #define IW_EV_UINT_PK_LEN       (IW_EV_LCP_PK_LEN + sizeof(__u32))
00133 #define IW_EV_FREQ_PK_LEN       (IW_EV_LCP_PK_LEN + sizeof(struct iw_freq))
00134 #define IW_EV_PARAM_PK_LEN      (IW_EV_LCP_PK_LEN + sizeof(struct iw_param))
00135 #define IW_EV_ADDR_PK_LEN       (IW_EV_LCP_PK_LEN + sizeof(struct sockaddr))
00136 #define IW_EV_QUAL_PK_LEN       (IW_EV_LCP_PK_LEN + sizeof(struct iw_quality))
00137 #define IW_EV_POINT_PK_LEN      (IW_EV_LCP_PK_LEN + 4)
00138 
00139 struct iw_pk_event
00140 {
00141         __u16           len;                    /* Real lenght of this stuff */
00142         __u16           cmd;                    /* Wireless IOCTL */
00143         union iwreq_data        u;              /* IOCTL fixed payload */
00144 } __attribute__ ((packed));
00145 struct  iw_pk_point
00146 {
00147   void __user   *pointer;       /* Pointer to the data  (in user space) */
00148   __u16         length;         /* number of fields or size in bytes */
00149   __u16         flags;          /* Optional params */
00150 } __attribute__ ((packed));
00151 
00152 #define IW_EV_LCP_PK2_LEN       (sizeof(struct iw_pk_event) - sizeof(union iwreq_data))
00153 #define IW_EV_POINT_PK2_LEN     (IW_EV_LCP_PK2_LEN + sizeof(struct iw_pk_point) - IW_EV_POINT_OFF)
00154 
00155 #endif  /* IW_EV_LCP_PK_LEN */
00156 
00157 /****************************** TYPES ******************************/
00158 
00159 /* Shortcuts */
00160 typedef struct iw_statistics    iwstats;
00161 typedef struct iw_range         iwrange;
00162 typedef struct iw_param         iwparam;
00163 typedef struct iw_freq          iwfreq;
00164 typedef struct iw_quality       iwqual;
00165 typedef struct iw_priv_args     iwprivargs;
00166 typedef struct sockaddr         sockaddr;
00167 
00168 /* Structure for storing all wireless information for each device
00169  * This is a cut down version of the one above, containing only
00170  * the things *truly* needed to configure a card.
00171  * Don't add other junk, I'll remove it... */
00172 typedef struct wireless_config
00173 {
00174   char          name[IFNAMSIZ + 1];     /* Wireless/protocol name */
00175   int           has_nwid;
00176   iwparam       nwid;                   /* Network ID */
00177   int           has_freq;
00178   double        freq;                   /* Frequency/channel */
00179   int           freq_flags;
00180   int           has_key;
00181   unsigned char key[IW_ENCODING_TOKEN_MAX];     /* Encoding key used */
00182   int           key_size;               /* Number of bytes */
00183   int           key_flags;              /* Various flags */
00184   int           has_essid;
00185   int           essid_on;
00186   char          essid[IW_ESSID_MAX_SIZE + 1];   /* ESSID (extended network) */
00187   int           has_mode;
00188   int           mode;                   /* Operation mode */
00189 } wireless_config;
00190 
00191 /* Structure for storing all wireless information for each device
00192  * This is pretty exhaustive... */
00193 typedef struct wireless_info
00194 {
00195   struct wireless_config        b;      /* Basic information */
00196 
00197   int           has_sens;
00198   iwparam       sens;                   /* sensitivity */
00199   int           has_nickname;
00200   char          nickname[IW_ESSID_MAX_SIZE + 1]; /* NickName */
00201   int           has_ap_addr;
00202   sockaddr      ap_addr;                /* Access point address */
00203   int           has_bitrate;
00204   iwparam       bitrate;                /* Bit rate in bps */
00205   int           has_rts;
00206   iwparam       rts;                    /* RTS threshold in bytes */
00207   int           has_frag;
00208   iwparam       frag;                   /* Fragmentation threshold in bytes */
00209   int           has_power;
00210   iwparam       power;                  /* Power management parameters */
00211   int           has_txpower;
00212   iwparam       txpower;                /* Transmit Power in dBm */
00213   int           has_retry;
00214   iwparam       retry;                  /* Retry limit or lifetime */
00215 
00216   /* Stats */
00217   iwstats       stats;
00218   int           has_stats;
00219   iwrange       range;
00220   int           has_range;
00221 
00222   /* Auth params for WPA/802.1x/802.11i */
00223   int           auth_key_mgmt;
00224   int           has_auth_key_mgmt;
00225   int           auth_cipher_pairwise;
00226   int           has_auth_cipher_pairwise;
00227   int           auth_cipher_group;
00228   int           has_auth_cipher_group;
00229 } wireless_info;
00230 
00231 /* Structure for storing an entry of a wireless scan.
00232  * This is only a subset of all possible information, the flexible
00233  * structure of scan results make it impossible to capture all
00234  * information in such a static structure. */
00235 typedef struct wireless_scan
00236 {
00237   /* Linked list */
00238   struct wireless_scan *        next;
00239 
00240   /* Cell identifiaction */
00241   int           has_ap_addr;
00242   sockaddr      ap_addr;                /* Access point address */
00243 
00244   /* Other information */
00245   struct wireless_config        b;      /* Basic information */
00246   iwstats       stats;                  /* Signal strength */
00247   int           has_stats;
00248   iwparam       maxbitrate;             /* Max bit rate in bps */
00249   int           has_maxbitrate;
00250 } wireless_scan;
00251 
00252 /*
00253  * Context used for non-blocking scan.
00254  */
00255 typedef struct wireless_scan_head
00256 {
00257   wireless_scan *       result;         /* Result of the scan */
00258   int                   retry;          /* Retry level */
00259 } wireless_scan_head;
00260 
00261 /* Structure used for parsing event streams, such as Wireless Events
00262  * and scan results */
00263 typedef struct stream_descr
00264 {
00265   char *        end;            /* End of the stream */
00266   char *        current;        /* Current event in stream of events */
00267   char *        value;          /* Current value in event */
00268 } stream_descr;
00269 
00270 /* Prototype for handling display of each single interface on the
00271  * system - see iw_enum_devices() */
00272 typedef int (*iw_enum_handler)(int      skfd,
00273                                char *   ifname,
00274                                char *   args[],
00275                                int      count);
00276 
00277 /* Describe a modulation */
00278 typedef struct iw_modul_descr
00279 {
00280   unsigned int          mask;           /* Modulation bitmask */
00281   char                  cmd[8];         /* Short name */
00282   char *                verbose;        /* Verbose description */
00283 } iw_modul_descr;
00284 
00285 /**************************** PROTOTYPES ****************************/
00286 /*
00287  * All the functions in iwcommon.c
00288  */
00289 
00290 /* ---------------------- SOCKET SUBROUTINES -----------------------*/
00291 int
00292         iw_sockets_open(void);
00293 void
00294         iw_enum_devices(int             skfd,
00295                         iw_enum_handler fn,
00296                         char *          args[],
00297                         int             count);
00298 /* --------------------- WIRELESS SUBROUTINES ----------------------*/
00299 int
00300         iw_get_kernel_we_version(void);
00301 int
00302         iw_print_version_info(const char *      toolname);
00303 int
00304         iw_get_range_info(int           skfd,
00305                           const char *  ifname,
00306                           iwrange *     range);
00307 int
00308         iw_get_priv_info(int            skfd,
00309                          const char *   ifname,
00310                          iwprivargs **  ppriv);
00311 int
00312         iw_get_basic_config(int                 skfd,
00313                             const char *        ifname,
00314                             wireless_config *   info);
00315 int
00316         iw_set_basic_config(int                 skfd,
00317                             const char *        ifname,
00318                             wireless_config *   info);
00319 /* --------------------- PROTOCOL SUBROUTINES --------------------- */
00320 int
00321         iw_protocol_compare(const char *        protocol1,
00322                             const char *        protocol2);
00323 /* -------------------- FREQUENCY SUBROUTINES --------------------- */
00324 void
00325         iw_float2freq(double    in,
00326                       iwfreq *  out);
00327 double
00328         iw_freq2float(const iwfreq *    in);
00329 void
00330         iw_print_freq_value(char *      buffer,
00331                             int         buflen,
00332                             double      freq);
00333 void
00334         iw_print_freq(char *    buffer,
00335                       int       buflen,
00336                       double    freq,
00337                       int       channel,
00338                       int       freq_flags);
00339 int
00340         iw_freq_to_channel(double                       freq,
00341                            const struct iw_range *      range);
00342 int
00343         iw_channel_to_freq(int                          channel,
00344                            double *                     pfreq,
00345                            const struct iw_range *      range);
00346 void
00347         iw_print_bitrate(char * buffer,
00348                          int    buflen,
00349                          int    bitrate);
00350 /* ---------------------- POWER SUBROUTINES ----------------------- */
00351 int
00352         iw_dbm2mwatt(int        in);
00353 int
00354         iw_mwatt2dbm(int        in);
00355 void
00356         iw_print_txpower(char *                 buffer,
00357                          int                    buflen,
00358                          struct iw_param *      txpower);
00359 /* -------------------- STATISTICS SUBROUTINES -------------------- */
00360 int
00361         iw_get_stats(int                skfd,
00362                      const char *       ifname,
00363                      iwstats *          stats,
00364                      const iwrange *    range,
00365                      int                has_range);
00366 void
00367         iw_print_stats(char *           buffer,
00368                        int              buflen,
00369                        const iwqual *   qual,
00370                        const iwrange *  range,
00371                        int              has_range);
00372 /* --------------------- ENCODING SUBROUTINES --------------------- */
00373 void
00374         iw_print_key(char *                     buffer,
00375                      int                        buflen,
00376                      const unsigned char *      key,
00377                      int                        key_size,
00378                      int                        key_flags);
00379 int
00380         iw_in_key(const char *          input,
00381                   unsigned char *       key);
00382 int
00383         iw_in_key_full(int              skfd,
00384                        const char *     ifname,
00385                        const char *     input,
00386                        unsigned char *  key,
00387                        __u16 *          flags);
00388 /* ----------------- POWER MANAGEMENT SUBROUTINES ----------------- */
00389 void
00390         iw_print_pm_value(char *        buffer,
00391                           int           buflen,
00392                           int           value,
00393                           int           flags,
00394                           int           we_version);
00395 void
00396         iw_print_pm_mode(char *         buffer,
00397                          int            buflen,
00398                          int            flags);
00399 /* --------------- RETRY LIMIT/LIFETIME SUBROUTINES --------------- */
00400 void
00401         iw_print_retry_value(char *     buffer,
00402                              int        buflen,
00403                              int        value,
00404                              int        flags,
00405                              int        we_version);
00406 /* ----------------------- TIME SUBROUTINES ----------------------- */
00407 void
00408         iw_print_timeval(char *                         buffer,
00409                          int                            buflen,
00410                          const struct timeval *         time,
00411                          const struct timezone *        tz);
00412 /* --------------------- ADDRESS SUBROUTINES ---------------------- */
00413 int
00414         iw_check_mac_addr_type(int              skfd,
00415                                const char *     ifname);
00416 int
00417         iw_check_if_addr_type(int               skfd,
00418                               const char *      ifname);
00419 #if 0
00420 int
00421         iw_check_addr_type(int          skfd,
00422                            const char * ifname);
00423 #endif
00424 #if 0
00425 int
00426         iw_get_mac_addr(int                     skfd,
00427                         const char *            name,
00428                         struct ether_addr *     eth,
00429                         unsigned short *        ptype);
00430 #endif
00431 char *
00432         iw_mac_ntop(const unsigned char *       mac,
00433                     int                         maclen,
00434                     char *                      buf,
00435                     int                         buflen);
00436 void
00437         iw_ether_ntop(const struct ether_addr * eth,
00438                       char *                    buf);
00439 char *
00440         iw_sawap_ntop(const struct sockaddr *   sap,
00441                       char *                    buf);
00442 int
00443         iw_mac_aton(const char *        orig,
00444                     unsigned char *     mac,
00445                     int                 macmax);
00446 int
00447         iw_ether_aton(const char* bufp, struct ether_addr* eth);
00448 int
00449         iw_in_inet(char *bufp, struct sockaddr *sap);
00450 int
00451         iw_in_addr(int                  skfd,
00452                    const char *         ifname,
00453                    char *               bufp,
00454                    struct sockaddr *    sap);
00455 /* ----------------------- MISC SUBROUTINES ------------------------ */
00456 int
00457         iw_get_priv_size(int            args);
00458 
00459 /* ---------------------- EVENT SUBROUTINES ---------------------- */
00460 void
00461         iw_init_event_stream(struct stream_descr *      stream,
00462                              char *                     data,
00463                              int                        len);
00464 int
00465         iw_extract_event_stream(struct stream_descr *   stream,
00466                                 struct iw_event *       iwe,
00467                                 int                     we_version);
00468 /* --------------------- SCANNING SUBROUTINES --------------------- */
00469 int
00470         iw_process_scan(int                     skfd,
00471                         char *                  ifname,
00472                         int                     we_version,
00473                         wireless_scan_head *    context);
00474 int
00475         iw_scan(int                     skfd,
00476                 char *                  ifname,
00477                 int                     we_version,
00478                 wireless_scan_head *    context);
00479 
00480 /**************************** VARIABLES ****************************/
00481 
00482 /* Modes as human readable strings */
00483 extern const char * const       iw_operation_mode[];
00484 #define IW_NUM_OPER_MODE        7
00485 #define IW_NUM_OPER_MODE_EXT    8
00486 
00487 /* Modulations as human readable strings */
00488 extern const struct iw_modul_descr      iw_modul_list[];
00489 #define IW_SIZE_MODUL_LIST      16
00490 
00491 /************************* INLINE FUNTIONS *************************/
00492 /*
00493  * Functions that are so simple that it's more efficient inlining them
00494  */
00495 
00496 /*
00497  * Note : I've defined wrapper for the ioctl request so that
00498  * it will be easier to migrate to other kernel API if needed
00499  */
00500 
00501 /*------------------------------------------------------------------*/
00502 /*
00503  * Wrapper to push some Wireless Parameter in the driver
00504  */
00505 static inline int
00506 iw_set_ext(int                  skfd,           /* Socket to the kernel */
00507            const char *         ifname,         /* Device name */
00508            int                  request,        /* WE ID */
00509            struct iwreq *       pwrq)           /* Fixed part of the request */
00510 {
00511   /* Set device name */
00512   strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
00513   /* Do the request */
00514   return(ioctl(skfd, request, pwrq));
00515 }
00516 
00517 /*------------------------------------------------------------------*/
00518 /*
00519  * Wrapper to extract some Wireless Parameter out of the driver
00520  */
00521 static inline int
00522 iw_get_ext(int                  skfd,           /* Socket to the kernel */
00523            const char *         ifname,         /* Device name */
00524            int                  request,        /* WE ID */
00525            struct iwreq *       pwrq)           /* Fixed part of the request */
00526 {
00527   /* Set device name */
00528   strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
00529   /* Do the request */
00530   return(ioctl(skfd, request, pwrq));
00531 }
00532 
00533 /*------------------------------------------------------------------*/
00534 /*
00535  * Close the socket used for ioctl.
00536  */
00537 static inline void
00538 iw_sockets_close(int    skfd)
00539 {
00540   close(skfd);
00541 }
00542 
00543 /*------------------------------------------------------------------*/
00544 /*
00545  * Display an Ethernet Socket Address in readable format.
00546  */
00547 static inline char *
00548 iw_saether_ntop(const struct sockaddr *sap, char* bufp)
00549 {
00550   iw_ether_ntop((const struct ether_addr *) sap->sa_data, bufp);
00551   return bufp;
00552 }
00553 /*------------------------------------------------------------------*/
00554 /*
00555  * Input an Ethernet Socket Address and convert to binary.
00556  */
00557 static inline int
00558 iw_saether_aton(const char *bufp, struct sockaddr *sap)
00559 {
00560   sap->sa_family = ARPHRD_ETHER;
00561   return iw_ether_aton(bufp, (struct ether_addr *) sap->sa_data);
00562 }
00563 
00564 /*------------------------------------------------------------------*/
00565 /*
00566  * Create an Ethernet broadcast address
00567  */
00568 static inline void
00569 iw_broad_ether(struct sockaddr *sap)
00570 {
00571   sap->sa_family = ARPHRD_ETHER;
00572   memset((char *) sap->sa_data, 0xFF, ETH_ALEN);
00573 }
00574 
00575 /*------------------------------------------------------------------*/
00576 /*
00577  * Create an Ethernet NULL address
00578  */
00579 static inline void
00580 iw_null_ether(struct sockaddr *sap)
00581 {
00582   sap->sa_family = ARPHRD_ETHER;
00583   memset((char *) sap->sa_data, 0x00, ETH_ALEN);
00584 }
00585 
00586 /*------------------------------------------------------------------*/
00587 /*
00588  * Compare two ethernet addresses
00589  */
00590 static inline int
00591 iw_ether_cmp(const struct ether_addr* eth1, const struct ether_addr* eth2)
00592 {
00593   return memcmp(eth1, eth2, sizeof(*eth1));
00594 }
00595 
00596 #ifdef __cplusplus
00597 }
00598 #endif
00599 
00600 #endif  /* IWLIB_H */


heatmap
Author(s): Adrian Bauer
autogenerated on Thu Feb 11 2016 23:05:26