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