OSDN Git Service

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