OSDN Git Service

v26
[android-x86/external-wireless-tools.git] / wireless_tools / iwlib.h
1 /*
2  *      Wireless Tools
3  *
4  *              Jean II - HPLB 97->99 - HPL 99->02
5  *
6  * Common header for the Wireless Extension library...
7  *
8  * This file is released under the GPL license.
9  *     Copyright (c) 1997-2002 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 #ifdef WEXT_HEADER
131 /* Private copy of Wireless extensions */
132 #include WEXT_HEADER
133 #else   /* !WEXT_HEADER */
134 /* System wide Wireless extensions */
135 #include <linux/wireless.h>
136 #endif  /* !WEXT_HEADER */
137
138 #ifdef __cplusplus
139 extern "C" {
140 #endif
141
142 /****************************** DEBUG ******************************/
143
144
145 /************************ CONSTANTS & MACROS ************************/
146
147 /* Paths */
148 #define PROC_NET_WIRELESS       "/proc/net/wireless"
149 #define PROC_NET_DEV            "/proc/net/dev"
150
151 /* Some usefull constants */
152 #define KILO    1e3
153 #define MEGA    1e6
154 #define GIGA    1e9
155 /* For doing log10/exp10 without libm */
156 #define LOG10_MAGIC     1.25892541179
157
158 /* Backward compatibility for Wireless Extension 9 */
159 #ifndef IW_POWER_MODIFIER
160 #define IW_POWER_MODIFIER       0x000F  /* Modify a parameter */
161 #define IW_POWER_MIN            0x0001  /* Value is a minimum  */
162 #define IW_POWER_MAX            0x0002  /* Value is a maximum */
163 #define IW_POWER_RELATIVE       0x0004  /* Value is not in seconds/ms/us */
164 #endif /* IW_POWER_MODIFIER */
165
166 #ifndef IW_ENCODE_NOKEY
167 #define IW_ENCODE_NOKEY         0x0800  /* Key is write only, so not here */
168 #define IW_ENCODE_MODE          0xF000  /* Modes defined below */
169 #endif /* IW_ENCODE_NOKEY */
170 #ifndef IW_ENCODE_TEMP
171 #define IW_ENCODE_TEMP          0x0400  /* Temporary key */
172 #endif /* IW_ENCODE_TEMP */
173
174 /* More backward compatibility */
175 #ifndef SIOCSIWCOMMIT
176 #define SIOCSIWCOMMIT   SIOCSIWNAME
177 #endif /* SIOCSIWCOMMIT */
178
179 /****************************** TYPES ******************************/
180
181 /* Shortcuts */
182 typedef struct iw_statistics    iwstats;
183 typedef struct iw_range         iwrange;
184 typedef struct iw_param         iwparam;
185 typedef struct iw_freq          iwfreq;
186 typedef struct iw_quality       iwqual;
187 typedef struct iw_priv_args     iwprivargs;
188 typedef struct sockaddr         sockaddr;
189
190 /* Structure for storing all wireless information for each device
191  * This is pretty exhaustive... */
192 typedef struct wireless_info
193 {
194   char          name[IFNAMSIZ + 1];     /* Wireless/protocol name */
195   int           has_nwid;
196   iwparam       nwid;                   /* Network ID */
197   int           has_freq;
198   double        freq;                   /* Frequency/channel */
199   int           has_sens;
200   iwparam       sens;                   /* sensitivity */
201   int           has_key;
202   unsigned char key[IW_ENCODING_TOKEN_MAX];     /* Encoding key used */
203   int           key_size;               /* Number of bytes */
204   int           key_flags;              /* Various flags */
205   int           has_essid;
206   int           essid_on;
207   char          essid[IW_ESSID_MAX_SIZE + 1];   /* ESSID (extended network) */
208   int           has_nickname;
209   char          nickname[IW_ESSID_MAX_SIZE + 1]; /* NickName */
210   int           has_ap_addr;
211   sockaddr      ap_addr;                /* Access point address */
212   int           has_bitrate;
213   iwparam       bitrate;                /* Bit rate in bps */
214   int           has_rts;
215   iwparam       rts;                    /* RTS threshold in bytes */
216   int           has_frag;
217   iwparam       frag;                   /* Fragmentation threshold in bytes */
218   int           has_mode;
219   int           mode;                   /* Operation mode */
220   int           has_power;
221   iwparam       power;                  /* Power management parameters */
222   int           has_txpower;
223   iwparam       txpower;                /* Transmit Power in dBm */
224   int           has_retry;
225   iwparam       retry;                  /* Retry limit or lifetime */
226
227   /* Stats */
228   iwstats       stats;
229   int           has_stats;
230   iwrange       range;
231   int           has_range;
232 } wireless_info;
233
234 /* Structure for storing all wireless information for each device
235  * This is a cut down version of the one above, containing only
236  * the things *truly* needed to configure a card.
237  * Don't add other junk, I'll remove it... */
238 typedef struct wireless_config
239 {
240   char          name[IFNAMSIZ + 1];     /* Wireless/protocol name */
241   int           has_nwid;
242   iwparam       nwid;                   /* Network ID */
243   int           has_freq;
244   double        freq;                   /* Frequency/channel */
245   int           has_key;
246   unsigned char key[IW_ENCODING_TOKEN_MAX];     /* Encoding key used */
247   int           key_size;               /* Number of bytes */
248   int           key_flags;              /* Various flags */
249   int           has_essid;
250   int           essid_on;
251   char          essid[IW_ESSID_MAX_SIZE + 1];   /* ESSID (extended network) */
252   int           has_mode;
253   int           mode;                   /* Operation mode */
254 } wireless_config;
255
256 typedef struct stream_descr
257 {
258   char *        end;            /* End of the stream */
259   char *        current;        /* Current event in stream of events */
260   char *        value;          /* Current value in event */
261 } stream_descr;
262
263 /* Prototype for handling display of each single interface on the
264  * system - see iw_enum_devices() */
265 typedef int (*iw_enum_handler)(int      skfd,
266                                char *   ifname,
267                                char *   args[],
268                                int      count);
269
270 /**************************** PROTOTYPES ****************************/
271 /*
272  * All the functions in iwcommon.c
273  */
274
275 /* ---------------------- SOCKET SUBROUTINES -----------------------*/
276 int
277         iw_sockets_open(void);
278 void
279         iw_enum_devices(int             skfd,
280                         iw_enum_handler fn,
281                         char *          args[],
282                         int             count);
283 /* --------------------- WIRELESS SUBROUTINES ----------------------*/
284 int
285         iw_get_range_info(int           skfd,
286                           char *        ifname,
287                           iwrange *     range);
288 int
289         iw_print_version_info(char *    toolname);
290 int
291         iw_get_priv_info(int            skfd,
292                          char *         ifname,
293                          iwprivargs *   priv,
294                          int            maxpriv);
295 int
296         iw_get_basic_config(int                 skfd,
297                             char *              ifname,
298                             wireless_config *   info);
299 int
300         iw_set_basic_config(int                 skfd,
301                             char *              ifname,
302                             wireless_config *   info);
303 /* --------------------- PROTOCOL SUBROUTINES --------------------- */
304 int
305         iw_protocol_compare(char *      protocol1,
306                             char *      protocol2);
307 /* -------------------- FREQUENCY SUBROUTINES --------------------- */
308 void
309         iw_float2freq(double    in,
310                    iwfreq *     out);
311 double
312         iw_freq2float(iwfreq *  in);
313 void
314         iw_print_freq(char *    buffer,
315                       double    freq);
316 int
317         iw_freq_to_channel(double               freq,
318                            struct iw_range *    range);
319 void
320         iw_print_bitrate(char * buffer,
321                          int    bitrate);
322 /* ---------------------- POWER SUBROUTINES ----------------------- */
323 int
324         iw_dbm2mwatt(int        in);
325 int
326         iw_mwatt2dbm(int        in);
327 /* -------------------- STATISTICS SUBROUTINES -------------------- */
328 int
329         iw_get_stats(int        skfd,
330                      char *     ifname,
331                      iwstats *  stats);
332 void
333         iw_print_stats(char *           buffer,
334                        iwqual *         qual,
335                        iwrange *        range,
336                        int              has_range);
337 /* --------------------- ENCODING SUBROUTINES --------------------- */
338 void
339         iw_print_key(char *             buffer,
340                      unsigned char *    key,
341                      int                key_size,
342                      int                key_flags);
343 int
344         iw_in_key(char *                input,
345                   unsigned char *       key);
346 int
347         iw_in_key_full(int              skfd,
348                        char *           ifname,
349                        char *           input,
350                        unsigned char *  key,
351                        __u16 *          flags);
352 /* ----------------- POWER MANAGEMENT SUBROUTINES ----------------- */
353 void
354         iw_print_pm_value(char *        buffer,
355                           int           value,
356                           int           flags);
357 void
358         iw_print_pm_mode(char *         buffer,
359                          int            flags);
360 /* --------------- RETRY LIMIT/LIFETIME SUBROUTINES --------------- */
361 #if WIRELESS_EXT > 10
362 void
363         iw_print_retry_value(char *     buffer,
364                              int        value,
365                              int        flags);
366 #endif
367 /* ----------------------- TIME SUBROUTINES ----------------------- */
368 void
369         iw_print_timeval(char *                 buffer,
370                          const struct timeval * time);
371 /* --------------------- ADDRESS SUBROUTINES ---------------------- */
372 int
373         iw_check_mac_addr_type(int      skfd,
374                                char *   ifname);
375 int
376         iw_check_if_addr_type(int       skfd,
377                               char *    ifname);
378 #if 0
379 int
380         iw_check_addr_type(int          skfd,
381                            char *       ifname);
382 #endif
383 void
384         iw_ether_ntop(const struct ether_addr* eth, char* buf);
385 char*
386         iw_ether_ntoa(const struct ether_addr* eth);
387 int
388         iw_ether_aton(const char* bufp, struct ether_addr* eth);
389 int
390         iw_in_inet(char *bufp, struct sockaddr *sap);
391 int
392         iw_in_addr(int                  skfd,
393                    char *               ifname,
394                    char *               bufp,
395                    struct sockaddr *    sap);
396 /* ----------------------- MISC SUBROUTINES ------------------------ */
397 int
398         iw_get_priv_size(int            args);
399
400 #if WIRELESS_EXT > 13
401 /* ---------------------- EVENT SUBROUTINES ---------------------- */
402 void
403         iw_init_event_stream(struct stream_descr *      stream,
404                              char *                     data,
405                              int                        len);
406 int
407         iw_extract_event_stream(struct stream_descr *   stream,
408                                 struct iw_event *       iwe);
409 #endif /* WIRELESS_EXT > 13 */
410
411 /**************************** VARIABLES ****************************/
412
413 extern const char * const       iw_operation_mode[];
414 #define IW_NUM_OPER_MODE        7
415
416 /************************* INLINE FUNTIONS *************************/
417 /*
418  * Functions that are so simple that it's more efficient inlining them
419  */
420
421 /*
422  * Note : I've defined wrapper for the ioctl request so that
423  * it will be easier to migrate to other kernel API if needed
424  */
425
426 /*------------------------------------------------------------------*/
427 /*
428  * Wrapper to push some Wireless Parameter in the driver
429  */
430 static inline int
431 iw_set_ext(int                  skfd,           /* Socket to the kernel */
432            char *               ifname,         /* Device name */
433            int                  request,        /* WE ID */
434            struct iwreq *       pwrq)           /* Fixed part of the request */
435 {
436   /* Set device name */
437   strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
438   /* Do the request */
439   return(ioctl(skfd, request, pwrq));
440 }
441
442 /*------------------------------------------------------------------*/
443 /*
444  * Wrapper to extract some Wireless Parameter out of the driver
445  */
446 static inline int
447 iw_get_ext(int                  skfd,           /* Socket to the kernel */
448            char *               ifname,         /* Device name */
449            int                  request,        /* WE ID */
450            struct iwreq *       pwrq)           /* Fixed part of the request */
451 {
452   /* Set device name */
453   strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
454   /* Do the request */
455   return(ioctl(skfd, request, pwrq));
456 }
457
458 /*------------------------------------------------------------------*/
459 /* Backwards compatability
460  * Actually, those form are much easier to use when dealing with
461  * struct sockaddr... */
462 static inline char*
463 iw_pr_ether(char* bufp, const unsigned char* addr)
464 {
465   iw_ether_ntop((const struct ether_addr *) addr, bufp);
466   return bufp;
467 }
468 /* Backwards compatability */
469 static inline int
470 iw_in_ether(const char *bufp, struct sockaddr *sap)
471 {
472   sap->sa_family = ARPHRD_ETHER;
473   return iw_ether_aton(bufp, (struct ether_addr *) sap->sa_data) ? 0 : -1;
474 }
475
476 /*------------------------------------------------------------------*/
477 /*
478  * Create an Ethernet broadcast address
479  */
480 static inline void
481 iw_broad_ether(struct sockaddr *sap)
482 {
483   sap->sa_family = ARPHRD_ETHER;
484   memset((char *) sap->sa_data, 0xFF, ETH_ALEN);
485 }
486
487 /*------------------------------------------------------------------*/
488 /*
489  * Create an Ethernet NULL address
490  */
491 static inline void
492 iw_null_ether(struct sockaddr *sap)
493 {
494   sap->sa_family = ARPHRD_ETHER;
495   memset((char *) sap->sa_data, 0x00, ETH_ALEN);
496 }
497
498 #ifdef __cplusplus
499 }
500 #endif
501
502 #endif  /* IWLIB_H */