OSDN Git Service

67d6ff1ad29881ea8ddd716a6bf07feab3f18cd1
[android-x86/external-wireless-tools.git] / wireless_tools / iwlib.h
1 /*
2  *      Wireless Tools
3  *
4  *              Jean II - HPLB 97->99 - HPL 99->04
5  *
6  * Common header for the Wireless Extension library...
7  *
8  * This file is released under the GPL license.
9  *     Copyright (c) 1997-2004 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
38 #ifndef LINUX_VERSION_CODE
39 #include <linux/version.h>
40 #endif
41
42 /* Kernel headers 2.4.X + Glibc 2.2 - Mandrake 8.0, Debian 2.3, RH 7.1
43  * Kernel headers 2.2.X + Glibc 2.2 - Slackware 8.0 */
44 #if defined(__GLIBC__) \
45     && __GLIBC__ == 2 \
46     && __GLIBC_MINOR__ >= 2 \
47     && LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
48 //#define GLIBC22_HEADERS
49 #define GENERIC_HEADERS
50
51 /* Kernel headers 2.4.X + Glibc 2.1 - Debian 2.2 upgraded, RH 7.0
52  * Kernel headers 2.2.X + Glibc 2.1 - Debian 2.2, RH 6.1 */
53 #elif defined(__GLIBC__) \
54       && __GLIBC__ == 2 \
55       && __GLIBC_MINOR__ == 1 \
56       && LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
57 //#define GLIBC_HEADERS
58 #define GENERIC_HEADERS
59
60 /* Kernel headers 2.2.X + Glibc 2.0 - Debian 2.1 */
61 #elif defined(__GLIBC__) \
62       && __GLIBC__ == 2 \
63       && __GLIBC_MINOR__ == 0 \
64       && LINUX_VERSION_CODE >= KERNEL_VERSION(2,0,0) \
65       && LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
66 #define GLIBC_HEADERS
67 #define KLUDGE_HEADERS
68
69 /* Note : is it really worth supporting kernel 2.0.X, knowing that
70  * we require WE v9, which is only available in 2.2.X and higher ?
71  * I guess one could use 2.0.x with an upgraded wireless.h... */
72
73 /* Kernel headers 2.0.X + Glibc 2.0 - Debian 2.0, RH 5 */
74 #elif defined(__GLIBC__) \
75       && __GLIBC__ == 2 \
76       && __GLIBC_MINOR__ == 0 \
77       && LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0) \
78       && LINUX_VERSION_CODE >= KERNEL_VERSION(2,0,0)
79 #define GLIBC_HEADERS
80
81 /* Kernel headers 2.0.X + libc5 - old systems */
82 #elif defined(_LINUX_C_LIB_VERSION_MAJOR) \
83       && _LINUX_C_LIB_VERSION_MAJOR == 5 \
84       && LINUX_VERSION_CODE >= KERNEL_VERSION(2,0,0) \
85       && LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
86 #define LIBC5_HEADERS
87
88 /* Unsupported combination */
89 #else
90 #error "Your kernel/libc combination is not supported"
91 #endif
92
93 #ifdef GENERIC_HEADERS 
94 /* Proposed by Dr. Michael Rietz <rietz@mail.amps.de>, 27.3.2 */
95 /* If this works for all, it might be more stable on the long term - Jean II */
96 #include <net/if_arp.h>         /* For ARPHRD_ETHER */
97 #include <sys/socket.h>         /* For AF_INET & struct sockaddr */
98 #include <netinet/in.h>         /* For struct sockaddr_in */
99 #include <netinet/if_ether.h>
100 #endif /* GENERIC_HEADERS */    
101
102 #ifdef GLIBC22_HEADERS 
103 /* Added by Ross G. Miller <Ross_Miller@baylor.edu>, 3/28/01 */
104 #include <linux/if_arp.h>       /* For ARPHRD_ETHER */
105 #include <linux/socket.h>       /* For AF_INET & struct sockaddr */
106 #include <sys/socket.h>
107 #endif /* GLIBC22_HEADERS */    
108
109 #ifdef KLUDGE_HEADERS
110 #include <socketbits.h>
111 #endif  /* KLUDGE_HEADERS */
112
113 #ifdef GLIBC_HEADERS
114 #include <linux/if_arp.h>       /* For ARPHRD_ETHER */
115 #include <linux/socket.h>       /* For AF_INET & struct sockaddr */
116 #include <linux/in.h>           /* For struct sockaddr_in */
117 #endif  /* KLUDGE_HEADERS || GLIBC_HEADERS */
118
119 #ifdef LIBC5_HEADERS
120 #include <sys/socket.h>         /* For AF_INET & struct sockaddr & socket() */
121 #include <linux/if_arp.h>       /* For ARPHRD_ETHER */
122 #include <linux/in.h>           /* For struct sockaddr_in */
123 #endif  /* LIBC5_HEADERS */
124
125 /* Those 3 headers were previously included in wireless.h */
126 #include <linux/types.h>                /* for "caddr_t" et al          */
127 #include <linux/socket.h>               /* for "struct sockaddr" et al  */
128 #include <linux/if.h>                   /* for IFNAMSIZ and co... */
129
130 /* Private copy of Wireless extensions */
131 #include <wireless.h>
132
133 /* Make gcc understant that when we say inline, we mean it.
134  * I really hate when the compiler is trying to be more clever than me,
135  * because in this case gcc is not able to figure out functions with a
136  * single call site, so not only I have to tag those functions inline
137  * by hand, but then it refuse to inline them properly.
138  * Total saving for iwevent : 150B = 0.7%.
139  * Fortunately, in gcc 3.4, they now automatically inline static functions
140  * with a single call site. Hurrah !
141  * Jean II */
142 #if __GNUC__ == 3
143 #if __GNUC_MINOR__ >= 1 && __GNUC_MINOR__ < 4
144 #define inline          inline          __attribute__((always_inline))
145 #endif
146 #endif /* __GNUC__ */
147
148 #ifdef __cplusplus
149 extern "C" {
150 #endif
151
152 /****************************** DEBUG ******************************/
153
154
155 /************************ CONSTANTS & MACROS ************************/
156
157 /* Various versions information */
158 /* Recommended Wireless Extension version */
159 #define WE_VERSION      17
160 /* Version of Wireless Tools */
161 #define WT_VERSION      27
162
163 /* Paths */
164 #define PROC_NET_WIRELESS       "/proc/net/wireless"
165 #define PROC_NET_DEV            "/proc/net/dev"
166
167 /* Some usefull constants */
168 #define KILO    1e3
169 #define MEGA    1e6
170 #define GIGA    1e9
171 /* For doing log10/exp10 without libm */
172 #define LOG10_MAGIC     1.25892541179
173
174 /* Backward compatibility for Wireless Extension 9 */
175 #ifndef IW_POWER_MODIFIER
176 #define IW_POWER_MODIFIER       0x000F  /* Modify a parameter */
177 #define IW_POWER_MIN            0x0001  /* Value is a minimum  */
178 #define IW_POWER_MAX            0x0002  /* Value is a maximum */
179 #define IW_POWER_RELATIVE       0x0004  /* Value is not in seconds/ms/us */
180 #endif /* IW_POWER_MODIFIER */
181
182 #ifndef IW_ENCODE_NOKEY
183 #define IW_ENCODE_NOKEY         0x0800  /* Key is write only, so not here */
184 #define IW_ENCODE_MODE          0xF000  /* Modes defined below */
185 #endif /* IW_ENCODE_NOKEY */
186 #ifndef IW_ENCODE_TEMP
187 #define IW_ENCODE_TEMP          0x0400  /* Temporary key */
188 #endif /* IW_ENCODE_TEMP */
189
190 /* More backward compatibility */
191 #ifndef SIOCSIWCOMMIT
192 #define SIOCSIWCOMMIT   SIOCSIWNAME
193 #endif /* SIOCSIWCOMMIT */
194
195 /* Still more backward compatibility */
196 #ifndef IW_FREQ_FIXED
197 #define IW_FREQ_FIXED   0x01
198 #endif /* IW_FREQ_FIXED */
199
200 /****************************** TYPES ******************************/
201
202 /* Shortcuts */
203 typedef struct iw_statistics    iwstats;
204 typedef struct iw_range         iwrange;
205 typedef struct iw_param         iwparam;
206 typedef struct iw_freq          iwfreq;
207 typedef struct iw_quality       iwqual;
208 typedef struct iw_priv_args     iwprivargs;
209 typedef struct sockaddr         sockaddr;
210
211 /* Structure for storing all wireless information for each device
212  * This is a cut down version of the one above, containing only
213  * the things *truly* needed to configure a card.
214  * Don't add other junk, I'll remove it... */
215 typedef struct wireless_config
216 {
217   char          name[IFNAMSIZ + 1];     /* Wireless/protocol name */
218   int           has_nwid;
219   iwparam       nwid;                   /* Network ID */
220   int           has_freq;
221   double        freq;                   /* Frequency/channel */
222   int           freq_flags;
223   int           has_key;
224   unsigned char key[IW_ENCODING_TOKEN_MAX];     /* Encoding key used */
225   int           key_size;               /* Number of bytes */
226   int           key_flags;              /* Various flags */
227   int           has_essid;
228   int           essid_on;
229   char          essid[IW_ESSID_MAX_SIZE + 1];   /* ESSID (extended network) */
230   int           has_mode;
231   int           mode;                   /* Operation mode */
232 } wireless_config;
233
234 /* Structure for storing all wireless information for each device
235  * This is pretty exhaustive... */
236 typedef struct wireless_info
237 {
238   struct wireless_config        b;      /* Basic information */
239
240   int           has_sens;
241   iwparam       sens;                   /* sensitivity */
242   int           has_nickname;
243   char          nickname[IW_ESSID_MAX_SIZE + 1]; /* NickName */
244   int           has_ap_addr;
245   sockaddr      ap_addr;                /* Access point address */
246   int           has_bitrate;
247   iwparam       bitrate;                /* Bit rate in bps */
248   int           has_rts;
249   iwparam       rts;                    /* RTS threshold in bytes */
250   int           has_frag;
251   iwparam       frag;                   /* Fragmentation threshold in bytes */
252   int           has_power;
253   iwparam       power;                  /* Power management parameters */
254   int           has_txpower;
255   iwparam       txpower;                /* Transmit Power in dBm */
256   int           has_retry;
257   iwparam       retry;                  /* Retry limit or lifetime */
258
259   /* Stats */
260   iwstats       stats;
261   int           has_stats;
262   iwrange       range;
263   int           has_range;
264 } wireless_info;
265
266 /* Structure for storing an entry of a wireless scan.
267  * This is only a subset of all possible information, the flexible
268  * structure of scan results make it impossible to capture all
269  * information in such a static structure. */
270 typedef struct wireless_scan
271 {
272   /* Linked list */
273   struct wireless_scan *        next;
274
275   /* Cell identifiaction */
276   int           has_ap_addr;
277   sockaddr      ap_addr;                /* Access point address */
278
279   /* Other information */
280   struct wireless_config        b;      /* Basic information */
281   iwstats       stats;                  /* Signal strength */
282   int           has_stats;
283 } wireless_scan;
284
285 /*
286  * Context used for non-blocking scan.
287  */
288 typedef struct wireless_scan_head
289 {
290   wireless_scan *       result;         /* Result of the scan */
291   int                   retry;          /* Retry level */
292 } wireless_scan_head;
293
294 /* Structure used for parsing event streams, such as Wireless Events
295  * and scan results */
296 typedef struct stream_descr
297 {
298   char *        end;            /* End of the stream */
299   char *        current;        /* Current event in stream of events */
300   char *        value;          /* Current value in event */
301 } stream_descr;
302
303 /* Prototype for handling display of each single interface on the
304  * system - see iw_enum_devices() */
305 typedef int (*iw_enum_handler)(int      skfd,
306                                char *   ifname,
307                                char *   args[],
308                                int      count);
309
310 /**************************** PROTOTYPES ****************************/
311 /*
312  * All the functions in iwcommon.c
313  */
314
315 /* ---------------------- SOCKET SUBROUTINES -----------------------*/
316 int
317         iw_sockets_open(void);
318 void
319         iw_enum_devices(int             skfd,
320                         iw_enum_handler fn,
321                         char *          args[],
322                         int             count);
323 /* --------------------- WIRELESS SUBROUTINES ----------------------*/
324 int
325         iw_get_kernel_we_version(void);
326 int
327         iw_print_version_info(const char *      toolname);
328 int
329         iw_get_range_info(int           skfd,
330                           const char *  ifname,
331                           iwrange *     range);
332 int
333         iw_get_priv_info(int            skfd,
334                          const char *   ifname,
335                          iwprivargs **  ppriv);
336 int
337         iw_get_basic_config(int                 skfd,
338                             const char *        ifname,
339                             wireless_config *   info);
340 int
341         iw_set_basic_config(int                 skfd,
342                             const char *        ifname,
343                             wireless_config *   info);
344 /* --------------------- PROTOCOL SUBROUTINES --------------------- */
345 int
346         iw_protocol_compare(const char *        protocol1,
347                             const char *        protocol2);
348 /* -------------------- FREQUENCY SUBROUTINES --------------------- */
349 void
350         iw_float2freq(double    in,
351                       iwfreq *  out);
352 double
353         iw_freq2float(const iwfreq *    in);
354 void
355         iw_print_freq_value(char *      buffer,
356                             int         buflen,
357                             double      freq);
358 void
359         iw_print_freq(char *    buffer,
360                       int       buflen,
361                       double    freq,
362                       int       channel,
363                       int       freq_flags);
364 int
365         iw_freq_to_channel(double                       freq,
366                            const struct iw_range *      range);
367 int
368         iw_channel_to_freq(int                          channel,
369                            double *                     pfreq,
370                            const struct iw_range *      range);
371 void
372         iw_print_bitrate(char * buffer,
373                          int    buflen,
374                          int    bitrate);
375 /* ---------------------- POWER SUBROUTINES ----------------------- */
376 int
377         iw_dbm2mwatt(int        in);
378 int
379         iw_mwatt2dbm(int        in);
380 void
381         iw_print_txpower(char *                 buffer,
382                          int                    buflen,
383                          struct iw_param *      txpower);
384 /* -------------------- STATISTICS SUBROUTINES -------------------- */
385 int
386         iw_get_stats(int                skfd,
387                      const char *       ifname,
388                      iwstats *          stats,
389                      const iwrange *    range,
390                      int                has_range);
391 void
392         iw_print_stats(char *           buffer,
393                        int              buflen,
394                        const iwqual *   qual,
395                        const iwrange *  range,
396                        int              has_range);
397 /* --------------------- ENCODING SUBROUTINES --------------------- */
398 void
399         iw_print_key(char *                     buffer,
400                      int                        buflen,
401                      const unsigned char *      key,
402                      int                        key_size,
403                      int                        key_flags);
404 int
405         iw_in_key(const char *          input,
406                   unsigned char *       key);
407 int
408         iw_in_key_full(int              skfd,
409                        const char *     ifname,
410                        const char *     input,
411                        unsigned char *  key,
412                        __u16 *          flags);
413 /* ----------------- POWER MANAGEMENT SUBROUTINES ----------------- */
414 void
415         iw_print_pm_value(char *        buffer,
416                           int           buflen,
417                           int           value,
418                           int           flags);
419 void
420         iw_print_pm_mode(char *         buffer,
421                          int            buflen,
422                          int            flags);
423 /* --------------- RETRY LIMIT/LIFETIME SUBROUTINES --------------- */
424 void
425         iw_print_retry_value(char *     buffer,
426                              int        buflen,
427                              int        value,
428                              int        flags);
429 /* ----------------------- TIME SUBROUTINES ----------------------- */
430 void
431         iw_print_timeval(char *                 buffer,
432                          int                    buflen,
433                          const struct timeval * time);
434 /* --------------------- ADDRESS SUBROUTINES ---------------------- */
435 int
436         iw_check_mac_addr_type(int      skfd,
437                                char *   ifname);
438 int
439         iw_check_if_addr_type(int       skfd,
440                               char *    ifname);
441 #if 0
442 int
443         iw_check_addr_type(int          skfd,
444                            char *       ifname);
445 #endif
446 int
447         iw_get_mac_addr(int                     skfd,
448                         const char *            name,
449                         struct ether_addr *     eth,
450                         unsigned short *        ptype);
451 void
452         iw_ether_ntop(const struct ether_addr* eth, char* buf);
453 char*
454         iw_ether_ntoa(const struct ether_addr* eth);
455 int
456         iw_ether_aton(const char* bufp, struct ether_addr* eth);
457 int
458         iw_in_inet(char *bufp, struct sockaddr *sap);
459 int
460         iw_in_addr(int                  skfd,
461                    char *               ifname,
462                    char *               bufp,
463                    struct sockaddr *    sap);
464 /* ----------------------- MISC SUBROUTINES ------------------------ */
465 int
466         iw_get_priv_size(int            args);
467
468 /* ---------------------- EVENT SUBROUTINES ---------------------- */
469 void
470         iw_init_event_stream(struct stream_descr *      stream,
471                              char *                     data,
472                              int                        len);
473 int
474         iw_extract_event_stream(struct stream_descr *   stream,
475                                 struct iw_event *       iwe,
476                                 int                     we_version);
477 /* --------------------- SCANNING SUBROUTINES --------------------- */
478 int
479         iw_process_scan(int                     skfd,
480                         char *                  ifname,
481                         int                     we_version,
482                         wireless_scan_head *    context);
483 int
484         iw_scan(int                     skfd,
485                 char *                  ifname,
486                 int                     we_version,
487                 wireless_scan_head *    context);
488
489 /**************************** VARIABLES ****************************/
490
491 /* Modes as human readable strings */
492 extern const char * const       iw_operation_mode[];
493 #define IW_NUM_OPER_MODE        7
494
495 /************************* INLINE FUNTIONS *************************/
496 /*
497  * Functions that are so simple that it's more efficient inlining them
498  */
499
500 /*
501  * Note : I've defined wrapper for the ioctl request so that
502  * it will be easier to migrate to other kernel API if needed
503  */
504
505 /*------------------------------------------------------------------*/
506 /*
507  * Wrapper to push some Wireless Parameter in the driver
508  */
509 static inline int
510 iw_set_ext(int                  skfd,           /* Socket to the kernel */
511            const char *         ifname,         /* Device name */
512            int                  request,        /* WE ID */
513            struct iwreq *       pwrq)           /* Fixed part of the request */
514 {
515   /* Set device name */
516   strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
517   /* Do the request */
518   return(ioctl(skfd, request, pwrq));
519 }
520
521 /*------------------------------------------------------------------*/
522 /*
523  * Wrapper to extract some Wireless Parameter out of the driver
524  */
525 static inline int
526 iw_get_ext(int                  skfd,           /* Socket to the kernel */
527            const char *         ifname,         /* Device name */
528            int                  request,        /* WE ID */
529            struct iwreq *       pwrq)           /* Fixed part of the request */
530 {
531   /* Set device name */
532   strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
533   /* Do the request */
534   return(ioctl(skfd, request, pwrq));
535 }
536
537 /*------------------------------------------------------------------*/
538 /*
539  * Close the socket used for ioctl.
540  */
541 static inline void
542 iw_sockets_close(int    skfd)
543 {
544   close(skfd);
545 }
546
547 /*------------------------------------------------------------------*/
548 /* Backwards compatability
549  * Actually, those form are much easier to use when dealing with
550  * struct sockaddr... */
551 static inline char*
552 iw_pr_ether(char* bufp, const unsigned char* addr)
553 {
554   iw_ether_ntop((const struct ether_addr *) addr, bufp);
555   return bufp;
556 }
557 /* Backwards compatability */
558 static inline int
559 iw_in_ether(const char *bufp, struct sockaddr *sap)
560 {
561   sap->sa_family = ARPHRD_ETHER;
562   return iw_ether_aton(bufp, (struct ether_addr *) sap->sa_data) ? 0 : -1;
563 }
564
565 /*------------------------------------------------------------------*/
566 /*
567  * Create an Ethernet broadcast address
568  */
569 static inline void
570 iw_broad_ether(struct sockaddr *sap)
571 {
572   sap->sa_family = ARPHRD_ETHER;
573   memset((char *) sap->sa_data, 0xFF, ETH_ALEN);
574 }
575
576 /*------------------------------------------------------------------*/
577 /*
578  * Create an Ethernet NULL address
579  */
580 static inline void
581 iw_null_ether(struct sockaddr *sap)
582 {
583   sap->sa_family = ARPHRD_ETHER;
584   memset((char *) sap->sa_data, 0x00, ETH_ALEN);
585 }
586
587 #ifdef __cplusplus
588 }
589 #endif
590
591 #endif  /* IWLIB_H */