9 #include <net/ethernet.h>
11 #include <linux/socket.h>
12 #include <linux/wireless.h>
15 * This is my quick and dirty way of accessing the Linux Wireless Extensions
16 * from the /proc/net/wireless interface.
17 * Open the file, and get the Interface name, link, level, and noise.
21 int get_wifi_info(struct wifi
*wfi
)
24 struct iw_range range
;
31 if (fp
= fopen("/proc/net/wireless", "r")) {
32 //if (fp = fopen("./demo.txt", "r")) {
33 for (i
= 0; i
< (wfi
->ifnum
+ 3); i
++) {
34 fgets(buff
, sizeof(buff
), fp
);
35 buff
[strlen(buff
) + 1] = '\0';
41 "ERROR: Cannot Open Linux Wireless Extensions!\n\b");
45 sscanf(buff
, "%s %*s %f %d", &wfi
->ifname
, &wfi
->link
, &wfi
->level
);
46 /* Don't know why I have to make a second call to sscanf
47 * to pull wfi->noise, but for some wierd reason I cannot
48 * pull it above along with the other stats.
50 sscanf(buff
, "%*s %*s %*s %*s %d", &wfi
->noise
);
52 /* Thanks to Jeroen Nijhof <jnijhof@nijhofnet.nl>
53 * for catching this, I guess I overlooked it.
54 * Strip out the trailing ':' if exists.
56 if (wfi
->ifname
[strlen(wfi
->ifname
) - 1] == ':') {
57 wfi
->ifname
[strlen(wfi
->ifname
) - 1] = '\0';
60 memset(&wfi
->essid
, 0, IW_ESSID_MAX_SIZE
+ 1);
62 skfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
64 memset(&range
, 0, sizeof(struct iw_range
));
66 wrq
.u
.data
.pointer
= (caddr_t
) & range
;
67 wrq
.u
.data
.length
= sizeof(struct iw_range
);
69 strncpy(wrq
.ifr_name
, wfi
->ifname
, IFNAMSIZ
);
70 ret
= ioctl(skfd
, SIOCGIWRANGE
, &wrq
);
72 ret
= range
.max_qual
.qual
;
74 fprintf(stdout
, "ERROR\n");
75 /* Maybe put auto-learning HERE? */
80 wrq
.u
.essid
.pointer
= (caddr_t
) wfi
->essid
;
81 wrq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+ 1;
82 wrq
.u
.essid
.flags
= 0;
83 ret
= ioctl(skfd
, SIOCGIWESSID
, &wrq
);
85 ret
= ioctl(skfd
, SIOCGIWRATE
, &wrq
);
86 memcpy(&(wfi
->bitrate
), &(wrq
.u
.bitrate
), sizeof(struct iw_param
));
88 if (strlen(wfi
->essid
) < 1) {
89 strcpy(wfi
->essid
, "not_set");
92 if (wfi
->link
> wfi
->max_link
) {
93 wfi
->max_link
= wfi
->link
;
95 if (wfi
->max_link
> wfi
->max_qual
) {
96 wfi
->max_qual
= wfi
->max_link
;
99 * For debugging purposes.
100 * Check these values against /proc/net/wireless and
103 fprintf(stdout, "wfi->ifname -> %s\n", wfi->ifname);
104 fprintf(stdout, "wfi->max_qual -> %f\n", wfi->max_qual);
105 fprintf(stdout, "wfi->essid -> %s\n", wfi->essid);
106 fprintf(stdout, "wfi->link -> %f\n", wfi->link);
107 fprintf(stdout, "wfi->level -> %d\n", wfi->level);
108 fprintf(stdout, "wfi->noise -> %d\n", wfi->noise - 0x100);
109 fprintf(stdout, "wfi->bitrate -> %d\n", wfi->bitrate.value);
110 fprintf(stdout, "wfi->max_link -> %.f\n", wfi->max_link);
119 void next_if(struct wifi
*wfi
)
126 if (wfi
->ifnum
< max
) {
132 /* Not used anymore, how many mouse button's do
135 void last_if(struct wifi
*wfi
)
142 if (wfi
->ifnum
> 0) {
148 int get_max_ifs(void)
154 if (fp
= fopen("/proc/net/wireless", "r")) {
155 //if (fp = fopen("./demo.txt", "r")) {
156 for (i
= -3; fgets(buff
, sizeof(buff
), fp
); i
++) {