6 * Main code for "iwevent". This listent for wireless events on rtnetlink.
7 * You need to link this code against "iwcommon.c" and "-lm".
9 * Part of this code is from Alexey Kuznetsov, part is from Casey Carter,
10 * I've just put the pieces together...
11 * By the way, if you know a way to remove the root restrictions, tell me
14 * This file is released under the GPL license.
15 * Copyright (c) 1997-2002 Jean Tourrilhes <jt@hpl.hp.com>
18 /***************************** INCLUDES *****************************/
20 #include "iwlib.h" /* Header */
22 #include <linux/netlink.h>
23 #include <linux/rtnetlink.h>
29 /************************ RTNETLINK HELPERS ************************/
31 * The following code is extracted from :
32 * ----------------------------------------------
33 * libnetlink.c RTnetlink service routines.
35 * This program is free software; you can redistribute it and/or
36 * modify it under the terms of the GNU General Public License
37 * as published by the Free Software Foundation; either version
38 * 2 of the License, or (at your option) any later version.
40 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
41 * -----------------------------------------------
47 struct sockaddr_nl local;
48 struct sockaddr_nl peer;
53 static inline void rtnl_close(struct rtnl_handle *rth)
58 static inline int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
62 memset(rth, 0, sizeof(rth));
64 rth->fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
66 perror("Cannot open netlink socket");
70 memset(&rth->local, 0, sizeof(rth->local));
71 rth->local.nl_family = AF_NETLINK;
72 rth->local.nl_groups = subscriptions;
74 if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
75 perror("Cannot bind netlink socket");
78 addr_len = sizeof(rth->local);
79 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
80 perror("Cannot getsockname");
83 if (addr_len != sizeof(rth->local)) {
84 fprintf(stderr, "Wrong address length %d\n", addr_len);
87 if (rth->local.nl_family != AF_NETLINK) {
88 fprintf(stderr, "Wrong address family %d\n", rth->local.nl_family);
91 rth->seq = time(NULL);
95 /********************* WIRELESS EVENT DECODING *********************/
97 * This is the bit I wrote...
100 #if WIRELESS_EXT > 13
101 /*------------------------------------------------------------------*/
103 * Print one element from the scanning results
106 print_event_token(struct iw_event * event, /* Extracted token */
107 struct iw_range * iwrange, /* Range info */
110 char buffer[128]; /* Temporary buffer */
112 /* Now, let's decode the event */
115 /* ----- set events ----- */
116 /* Events that result from a "SET XXX" operation by the user */
118 if(event->u.nwid.disabled)
119 printf("NWID:off/any\n");
121 printf("NWID:%X\n", event->u.nwid.value);
125 float freq; /* Frequency/channel */
126 freq = iw_freq2float(&(event->u.freq));
127 iw_print_freq(buffer, freq);
128 printf("%s\n", buffer);
133 iw_operation_mode[event->u.mode]);
137 char essid[IW_ESSID_MAX_SIZE+1];
138 if((event->u.essid.pointer) && (event->u.essid.length))
139 memcpy(essid, event->u.essid.pointer, event->u.essid.length);
140 essid[event->u.essid.length] = '\0';
141 if(event->u.essid.flags)
143 /* Does it have an ESSID index ? */
144 if((event->u.essid.flags & IW_ENCODE_INDEX) > 1)
145 printf("ESSID:\"%s\" [%d]\n", essid,
146 (event->u.essid.flags & IW_ENCODE_INDEX));
148 printf("ESSID:\"%s\"\n", essid);
151 printf("ESSID:off/any\n");
156 unsigned char key[IW_ENCODING_TOKEN_MAX];
157 if(event->u.data.pointer)
158 memcpy(key, event->u.essid.pointer, event->u.data.length);
160 event->u.data.flags |= IW_ENCODE_NOKEY;
161 printf("Encryption key:");
162 if(event->u.data.flags & IW_ENCODE_DISABLED)
166 /* Display the key */
167 iw_print_key(buffer, key, event->u.data.length,
168 event->u.data.flags);
169 printf("%s", buffer);
172 if((event->u.data.flags & IW_ENCODE_INDEX) > 1)
173 printf(" [%d]", event->u.data.flags & IW_ENCODE_INDEX);
174 if(event->u.data.flags & IW_ENCODE_RESTRICTED)
175 printf(" Encryption mode:restricted");
176 if(event->u.data.flags & IW_ENCODE_OPEN)
177 printf(" Encryption mode:open");
182 /* ----- driver events ----- */
183 /* Events generated by the driver when something important happens */
185 printf("New Access Point/Cell address:%s\n",
186 iw_pr_ether(buffer, event->u.ap_addr.sa_data));
189 printf("Scan request completed\n");
192 printf("Tx packet dropped:%s\n",
193 iw_pr_ether(buffer, event->u.addr.sa_data));
195 #if WIRELESS_EXT > 14
198 char custom[IW_CUSTOM_MAX+1];
199 if((event->u.data.pointer) && (event->u.data.length))
200 memcpy(custom, event->u.data.pointer, event->u.data.length);
201 custom[event->u.data.length] = '\0';
202 printf("Custom driver event:%s\n", custom);
206 printf("Registered node:%s\n",
207 iw_pr_ether(buffer, event->u.addr.sa_data));
210 printf("Expired node:%s\n",
211 iw_pr_ether(buffer, event->u.addr.sa_data));
213 #endif /* WIRELESS_EXT > 14 */
214 /* ----- junk ----- */
215 /* other junk not currently in use */
217 iw_print_bitrate(buffer, event->u.bitrate.value);
218 printf("Bit Rate:%s\n", buffer);
222 event->u.qual.updated = 0x0; /* Not that reliable, disable */
223 iw_print_stats(buffer, &event->u.qual, iwrange, has_range);
224 printf("Link %s\n", buffer);
228 printf("(Unknown Wireless event 0x%04X)\n", event->cmd);
229 } /* switch(event->cmd) */
234 /*------------------------------------------------------------------*/
236 * Print out all Wireless Events part of the RTNetlink message
237 * Most often, there will be only one event per message, but
238 * just make sure we read everything...
241 print_event_stream(char * ifname,
246 struct stream_descr stream;
250 struct timeval recv_time;
252 struct iw_range range;
257 has_range = (iw_get_range_info(skfd, ifname, &range) < 0);
260 /* In readable form */
261 gettimeofday(&recv_time, NULL);
262 iw_print_timeval(buffer, &recv_time);
264 iw_init_event_stream(&stream, data, len);
267 /* Extract an event and print it */
268 ret = iw_extract_event_stream(&stream, &iwe);
272 printf("%s %-8.8s ", buffer, ifname);
276 print_event_token(&iwe, NULL, 0);
278 printf("(Invalid event)\n");
285 #endif /* WIRELESS_EXT > 13 */
287 /*********************** RTNETLINK EVENT DUMP***********************/
289 * Dump the events we receive from rtnetlink
290 * This code is mostly from Casey
293 /*------------------------------------------------------------------*/
295 * Respond to a single RTM_NEWLINK event from the rtnetlink socket.
298 index2name(int index, char *name)
300 int skfd = -1; /* generic raw socket desc. */
304 memset(name, 0, IFNAMSIZ + 1);
306 /* Create a channel to the NET kernel. */
307 if((skfd = iw_sockets_open()) < 0)
313 /* Get interface name */
314 irq.ifr_ifindex = index;
315 if(ioctl(skfd, SIOCGIFNAME, &irq) < 0)
318 strncpy(name, irq.ifr_name, IFNAMSIZ);
325 /*------------------------------------------------------------------*/
327 * Respond to a single RTM_NEWLINK event from the rtnetlink socket.
330 LinkCatcher(struct nlmsghdr *nlh)
332 struct ifinfomsg* ifi;
333 char ifname[IFNAMSIZ + 1];
336 fprintf(stderr, "nlmsg_type = %d.\n", nlh->nlmsg_type);
339 if(nlh->nlmsg_type != RTM_NEWLINK)
342 ifi = NLMSG_DATA(nlh);
345 index2name(ifi->ifi_index, ifname);
347 #if WIRELESS_EXT > 13
348 /* Code is ugly, but sort of works - Jean II */
350 /* Check for attributes */
351 if (nlh->nlmsg_len > NLMSG_ALIGN(sizeof(struct ifinfomsg))) {
352 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(sizeof(struct ifinfomsg));
353 struct rtattr *attr = (void*)ifi + NLMSG_ALIGN(sizeof(struct ifinfomsg));
355 while (RTA_OK(attr, attrlen)) {
356 /* Check if the Wireless kind */
357 if(attr->rta_type == IFLA_WIRELESS) {
358 /* Go to display it */
359 print_event_stream(ifname,
360 (void *)attr + RTA_ALIGN(sizeof(struct rtattr)),
361 attr->rta_len - RTA_ALIGN(sizeof(struct rtattr)));
363 attr = RTA_NEXT(attr, attrlen);
366 #endif /* WIRELESS_EXT > 13 */
371 /* ---------------------------------------------------------------- */
373 * We must watch the rtnelink socket for events.
374 * This routine handles those events (i.e., call this when rth.fd
378 handle_netlink_events(struct rtnl_handle * rth)
382 struct sockaddr_nl sanl;
389 amt = recvfrom(rth->fd, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr*)&sanl, &sanllen);
392 if(errno != EINTR && errno != EAGAIN)
394 fprintf(stderr, "%s: error reading netlink: %s.\n",
395 __PRETTY_FUNCTION__, strerror(errno));
402 fprintf(stderr, "%s: EOF on netlink??\n", __PRETTY_FUNCTION__);
406 h = (struct nlmsghdr*)buf;
407 while(amt >= (int)sizeof(*h))
409 int len = h->nlmsg_len;
410 int l = len - sizeof(*h);
412 if(l < 0 || len > amt)
414 fprintf(stderr, "%s: malformed netlink message: len=%d\n", __PRETTY_FUNCTION__, len);
418 switch(h->nlmsg_type)
425 fprintf(stderr, "%s: got nlmsg of type %#x.\n", __PRETTY_FUNCTION__, h->nlmsg_type);
430 len = NLMSG_ALIGN(len);
432 h = (struct nlmsghdr*)((char*)h + len);
436 fprintf(stderr, "%s: remnant of size %d on netlink\n", __PRETTY_FUNCTION__, amt);
440 /**************************** MAIN LOOP ****************************/
442 /* ---------------------------------------------------------------- */
444 * Wait until we get an event
447 wait_for_event(struct rtnl_handle * rth)
450 struct timeval tv; /* Select timeout */
456 fd_set rfds; /* File descriptors for select */
457 int last_fd; /* Last fd */
460 /* Guess what ? We must re-generate rfds each time */
462 FD_SET(rth->fd, &rfds);
465 /* Wait until something happens */
466 ret = select(last_fd + 1, &rfds, NULL, NULL, NULL);
468 /* Check if there was an error */
471 if(errno == EAGAIN || errno == EINTR)
473 fprintf(stderr, "Unhandled signal - exiting...\n");
477 /* Check if there was a timeout */
483 /* Check for interface discovery events. */
484 if(FD_ISSET(rth->fd, &rfds))
485 handle_netlink_events(rth);
491 /******************************* MAIN *******************************/
493 /* ---------------------------------------------------------------- */
500 fputs("Usage: iwevent [OPTIONS]\n"
501 " Monitors and displays Wireless Events.\n"
503 " -h,--help Print this message.\n"
504 " -v,--version Show version of this program.\n",
505 status ? stderr : stdout);
508 /* Command line options */
509 static const struct option long_opts[] = {
510 { "help", no_argument, NULL, 'h' },
511 { "version", no_argument, NULL, 'v' },
515 /* ---------------------------------------------------------------- */
517 * main body of the program
523 struct rtnl_handle rth;
526 /* Check command line options */
527 while((opt = getopt_long(argc, argv, "hv", long_opts, NULL)) > 0)
536 return(iw_print_version_info("iwevent"));
546 fputs("Too many arguments.\n", stderr);
550 /* Open netlink channel */
551 if(rtnl_open(&rth, RTMGRP_LINK) < 0)
553 perror("Can't initialize rtnetlink socket");
557 #if WIRELESS_EXT > 13
558 fprintf(stderr, "Waiting for Wireless Events...\n");
559 #else /* WIRELESS_EXT > 13 */
560 fprintf(stderr, "Unsupported in Wireless Extensions <= 14 :-(\n");
562 #endif /* WIRELESS_EXT > 13 */
564 /* Do what we have to do */
565 wait_for_event(&rth);
567 /* Cleanup - only if you are pedantic */