OSDN Git Service

v23
[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
32 /* This is our header selection. Try to hide the mess and the misery :-(
33  * Don't look, you would go blind ;-) */
34
35 #ifndef LINUX_VERSION_CODE
36 #include <linux/version.h>
37 #endif
38
39 /* Kernel headers 2.4.X + Glibc 2.2 - Mandrake 8.0, Debian 2.3, RH 7.1 */
40 #if defined(__GLIBC__) \
41     && __GLIBC__ == 2 \
42     && __GLIBC_MINOR__ >= 2 \
43     && LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
44 #define GLIBC22_HEADERS
45
46 /* Kernel headers 2.4.X + Glibc 2.1 - Debian 2.2 upgraded, RH 7.0
47  * Kernel headers 2.2.X + Glibc 2.1 - Debian 2.2, RH 6.1 */
48 #elif defined(__GLIBC__) \
49       && __GLIBC__ == 2 \
50       && __GLIBC_MINOR__ == 1 \
51       && LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
52 #define GLIBC_HEADERS
53
54 /* Kernel headers 2.2.X + Glibc 2.0 - Debian 2.1 */
55 #elif defined(__GLIBC__) \
56       && __GLIBC__ == 2 \
57       && __GLIBC_MINOR__ == 0 \
58       && LINUX_VERSION_CODE >= KERNEL_VERSION(2,0,0) \
59       && LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
60 #define GLIBC_HEADERS
61 #define KLUDGE_HEADERS
62
63 /* Note : is it really worth supporting kernel 2.0.X, knowing that
64  * we require WE v9, which is only available in 2.2.X and higher ?
65  * I guess one could use 2.0.x with an upgraded wireless.h... */
66
67 /* Kernel headers 2.0.X + Glibc 2.0 - Debian 2.0, RH 5 */
68 #elif defined(__GLIBC__) \
69       && __GLIBC__ == 2 \
70       && __GLIBC_MINOR__ == 0 \
71       && LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0) \
72       && LINUX_VERSION_CODE >= KERNEL_VERSION(2,0,0)
73 #define GLIBC_HEADERS
74
75 /* Kernel headers 2.0.X + libc5 - old systems */
76 #elif defined(_LINUX_C_LIB_VERSION_MAJOR) \
77       && _LINUX_C_LIB_VERSION_MAJOR == 5 \
78       && LINUX_VERSION_CODE >= KERNEL_VERSION(2,0,0) \
79       && LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
80 #define LIBC5_HEADERS
81
82 /* Unsupported combination */
83 #else
84 #error "Your kernel/libc combination is not supported"
85 #endif
86
87 #ifdef GLIBC22_HEADERS 
88 /* Added by Ross G. Miller <Ross_Miller@baylor.edu>, 3/28/01 */
89 #include <linux/if_arp.h>       /* For ARPHRD_ETHER */
90 #include <linux/socket.h>       /* For AF_INET & struct sockaddr */
91 #include <sys/socket.h>
92 #endif /* GLIBC22_HEADERS */    
93
94 #ifdef KLUDGE_HEADERS
95 #include <socketbits.h>
96 #endif  /* KLUDGE_HEADERS */
97
98 #ifdef GLIBC_HEADERS
99 #include <linux/if_arp.h>       /* For ARPHRD_ETHER */
100 #include <linux/socket.h>       /* For AF_INET & struct sockaddr */
101 #include <linux/in.h>           /* For struct sockaddr_in */
102 #endif  /* KLUDGE_HEADERS || GLIBC_HEADERS */
103
104 #ifdef LIBC5_HEADERS
105 #include <sys/socket.h>         /* For AF_INET & struct sockaddr & socket() */
106 #include <linux/if_arp.h>       /* For ARPHRD_ETHER */
107 #include <linux/in.h>           /* For struct sockaddr_in */
108 #endif  /* LIBC5_HEADERS */
109
110 #ifdef PRIVATE_WE_HEADER
111 /* Private copy of Wireless extensions */
112 #include "wireless.h"
113 #else   /* PRIVATE_WE_HEADER */
114 /* System wide Wireless extensions */
115 #include <linux/wireless.h>
116 #endif  /* PRIVATE_WE_HEADER */
117
118 #if WIRELESS_EXT < 9
119 #error "Wireless Extension v9 or newer required :-(\
120 Use Wireless Tools v19 or update your kernel headers"
121 #endif
122 #if WIRELESS_EXT < 12
123 #warning "Wireless Extension v12 recommended...\
124 You may update your kernel and/or system headers to get the new features..."
125 #endif
126
127 /****************************** DEBUG ******************************/
128
129
130 /************************ CONSTANTS & MACROS ************************/
131
132 /* Some usefull constants */
133 #define KILO    1e3
134 #define MEGA    1e6
135 #define GIGA    1e9
136
137 /* Backward compatibility for Wireless Extension 9 */
138 #ifndef IW_POWER_MODIFIER
139 #define IW_POWER_MODIFIER       0x000F  /* Modify a parameter */
140 #define IW_POWER_MIN            0x0001  /* Value is a minimum  */
141 #define IW_POWER_MAX            0x0002  /* Value is a maximum */
142 #define IW_POWER_RELATIVE       0x0004  /* Value is not in seconds/ms/us */
143 #endif /* IW_POWER_MODIFIER */
144
145 #ifndef IW_ENCODE_NOKEY
146 #define IW_ENCODE_NOKEY         0x0800  /* Key is write only, so not here */
147 #define IW_ENCODE_MODE          0xF000  /* Modes defined below */
148 #endif /* IW_ENCODE_NOKEY */
149
150 /* More backward compatibility */
151 #ifndef SIOCSIWCOMMIT
152 #define SIOCSIWCOMMIT   SIOCSIWNAME
153 #endif /* SIOCSIWCOMMIT */
154
155 /****************************** TYPES ******************************/
156
157 /* Shortcuts */
158 typedef struct iw_statistics    iwstats;
159 typedef struct iw_range         iwrange;
160 typedef struct iw_param         iwparam;
161 typedef struct iw_freq          iwfreq;
162 typedef struct iw_quality       iwqual;
163 typedef struct iw_priv_args     iwprivargs;
164 typedef struct sockaddr         sockaddr;
165
166 /* Structure for storing all wireless information for each device
167  * This is pretty exhaustive... */
168 typedef struct wireless_info
169 {
170   char          name[IFNAMSIZ];         /* Wireless/protocol name */
171   int           has_nwid;
172   iwparam       nwid;                   /* Network ID */
173   int           has_freq;
174   float         freq;                   /* Frequency/channel */
175   int           has_sens;
176   iwparam       sens;                   /* sensitivity */
177   int           has_key;
178   unsigned char key[IW_ENCODING_TOKEN_MAX];     /* Encoding key used */
179   int           key_size;               /* Number of bytes */
180   int           key_flags;              /* Various flags */
181   int           has_essid;
182   int           essid_on;
183   char          essid[IW_ESSID_MAX_SIZE + 1];   /* ESSID (extended network) */
184   int           has_nickname;
185   char          nickname[IW_ESSID_MAX_SIZE + 1]; /* NickName */
186   int           has_ap_addr;
187   sockaddr      ap_addr;                /* Access point address */
188   int           has_bitrate;
189   iwparam       bitrate;                /* Bit rate in bps */
190   int           has_rts;
191   iwparam       rts;                    /* RTS threshold in bytes */
192   int           has_frag;
193   iwparam       frag;                   /* Fragmentation threshold in bytes */
194   int           has_mode;
195   int           mode;                   /* Operation mode */
196   int           has_power;
197   iwparam       power;                  /* Power management parameters */
198   int           has_txpower;
199   iwparam       txpower;                /* Transmit Power in dBm */
200   int           has_retry;
201   iwparam       retry;                  /* Retry limit or lifetime */
202
203   /* Stats */
204   iwstats       stats;
205   int           has_stats;
206   iwrange       range;
207   int           has_range;
208 } wireless_info;
209
210 /* Structure for storing all wireless information for each device
211  * This is a cut down version of the one above, containing only
212  * the things *truly* needed to configure a card.
213  * Don't add other junk, I'll remove it... */
214 typedef struct wireless_config
215 {
216   char          name[IFNAMSIZ];         /* Wireless/protocol name */
217   int           has_nwid;
218   iwparam       nwid;                   /* Network ID */
219   int           has_freq;
220   float         freq;                   /* Frequency/channel */
221   int           has_key;
222   unsigned char key[IW_ENCODING_TOKEN_MAX];     /* Encoding key used */
223   int           key_size;               /* Number of bytes */
224   int           key_flags;              /* Various flags */
225   int           has_essid;
226   int           essid_on;
227   char          essid[IW_ESSID_MAX_SIZE + 1];   /* ESSID (extended network) */
228   int           has_mode;
229   int           mode;                   /* Operation mode */
230 } wireless_config;
231
232 /**************************** PROTOTYPES ****************************/
233 /*
234  * All the functions in iwcommon.c
235  */
236 /* ---------------------- SOCKET SUBROUTINES -----------------------*/
237 int
238         iw_sockets_open(void);
239 /* --------------------- WIRELESS SUBROUTINES ----------------------*/
240 int
241         iw_get_range_info(int           skfd,
242                           char *        ifname,
243                           iwrange *     range);
244 int
245         iw_get_priv_info(int            skfd,
246                          char *         ifname,
247                          iwprivargs *   priv);
248 int
249         iw_get_basic_config(int                 skfd,
250                             char *              ifname,
251                             wireless_config *   info);
252 int
253         iw_set_basic_config(int                 skfd,
254                             char *              ifname,
255                             wireless_config *   info);
256 /* -------------------- FREQUENCY SUBROUTINES --------------------- */
257 void
258         iw_float2freq(double    in,
259                    iwfreq *     out);
260 double
261         iw_freq2float(iwfreq *  in);
262 /* ---------------------- POWER SUBROUTINES ----------------------- */
263 int
264         iw_dbm2mwatt(int        in);
265 int
266         iw_mwatt2dbm(int        in);
267 /* -------------------- STATISTICS SUBROUTINES -------------------- */
268 int
269         iw_get_stats(int        skfd,
270                      char *     ifname,
271                      iwstats *  stats);
272 void
273         iw_print_stats(char *           buffer,
274                        iwqual *         qual,
275                        iwrange *        range,
276                        int              has_range);
277 /* --------------------- ENCODING SUBROUTINES --------------------- */
278 void
279         iw_print_key(char *             buffer,
280                      unsigned char *    key,
281                      int                key_size,
282                      int                key_flags);
283 /* ----------------- POWER MANAGEMENT SUBROUTINES ----------------- */
284 void
285         iw_print_pm_value(char *        buffer,
286                           int           value,
287                           int           flags);
288 void
289         iw_print_pm_mode(char *         buffer,
290                          int            flags);
291 /* --------------- RETRY LIMIT/LIFETIME SUBROUTINES --------------- */
292 #if WIRELESS_EXT > 10
293 void
294         iw_print_retry_value(char *     buffer,
295                              int        value,
296                              int        flags);
297 #endif
298 /* --------------------- ADDRESS SUBROUTINES ---------------------- */
299 int
300         iw_check_mac_addr_type(int      skfd,
301                                char *   ifname);
302 int
303         iw_check_if_addr_type(int       skfd,
304                               char *    ifname);
305 #if 0
306 int
307         iw_check_addr_type(int  skfd,
308                         char *  ifname);
309 #endif
310 char *
311         iw_pr_ether(char *buffer, unsigned char *ptr);
312 int
313         iw_in_ether(char *bufp, struct sockaddr *sap);
314 int
315         iw_in_inet(char *bufp, struct sockaddr *sap);
316 int
317         iw_in_addr(int                  skfd,
318                    char *               ifname,
319                    char *               bufp,
320                    struct sockaddr *    sap);
321 /* ----------------------- MISC SUBROUTINES ------------------------ */
322 int
323         iw_byte_size(int                args);
324
325 /**************************** VARIABLES ****************************/
326
327 extern const char *     iw_operation_mode[];
328 #define IW_NUM_OPER_MODE        6
329
330 /************************* INLINE FUNTIONS *************************/
331 /*
332  * Function that are so simple that it's more efficient inlining them
333  */
334
335 /*
336  * Note : I've defined wrapper for the ioctl request so that
337  * it will be easier to migrate to other kernel API if needed
338  */
339
340 /*------------------------------------------------------------------*/
341 /*
342  * Wrapper to push some Wireless Parameter in the driver
343  */
344 static inline int
345 iw_set_ext(int                  skfd,           /* Socket to the kernel */
346            char *               ifname,         /* Device name */
347            int                  request,        /* WE ID */
348            struct iwreq *       pwrq)           /* Fixed part of the request */
349 {
350   /* Set device name */
351   strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
352   /* Do the request */
353   return(ioctl(skfd, request, pwrq));
354 }
355
356 /*------------------------------------------------------------------*/
357 /*
358  * Wrapper to extract some Wireless Parameter out of the driver
359  */
360 static inline int
361 iw_get_ext(int                  skfd,           /* Socket to the kernel */
362            char *               ifname,         /* Device name */
363            int                  request,        /* WE ID */
364            struct iwreq *       pwrq)           /* Fixed part of the request */
365 {
366   /* Set device name */
367   strncpy(pwrq->ifr_name, ifname, IFNAMSIZ);
368   /* Do the request */
369   return(ioctl(skfd, request, pwrq));
370 }
371
372 #endif  /* IWLIB_H */