OSDN Git Service

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