From ea2432988cde62b4520e547d8b5d95be489a459a Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Tue, 6 Mar 2018 16:31:09 +0100 Subject: show the SSID for wireless interfaces --- config.def.h | 1 + netlink-notify.c | 30 ++++++++++++++++++++++++++++-- netlink-notify.h | 5 +++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/config.def.h b/config.def.h index 34a496e..937bd9f 100644 --- a/config.def.h +++ b/config.def.h @@ -20,6 +20,7 @@ /* define notification text messages */ #define TEXT_TOPIC "Netlink Notification" #define TEXT_NEWLINK "Interface %s is %s." +#define TEXT_WIRELESS "Interface %s is %s on %s." #define TEXT_NEWADDR "Interface %s has new %s address\n%s/%d." #define TEXT_DELLINK "Interface %s has gone away." diff --git a/netlink-notify.c b/netlink-notify.c index d197dd2..bc41954 100644 --- a/netlink-notify.c +++ b/netlink-notify.c @@ -111,12 +111,38 @@ void list_addresses(struct addresses_seen *addresses_seen, char *interface) { putchar('\n'); } +/*** get_ssid ***/ +void get_ssid(const char *interface, char *essid) { + int sockfd; + struct iwreq wreq; + + memset(&wreq, 0, sizeof(struct iwreq)); + snprintf(wreq.ifr_name, IFNAMSIZ, interface); + wreq.u.essid.pointer = essid; + wreq.u.essid.length = IW_ESSID_MAX_SIZE + 1; + + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) + return; + + if (ioctl(sockfd, SIOCGIWESSID, &wreq) == -1) + return; +} + /*** newstr_link ***/ char * newstr_link(char *interface, unsigned int flags) { char *notifystr; + char essid[IW_ESSID_MAX_SIZE + 1]; + + memset(&essid, 0, IW_ESSID_MAX_SIZE + 1); + get_ssid(interface, essid); - notifystr = malloc(sizeof(TEXT_NEWLINK) + strlen(interface) + 4); - sprintf(notifystr, TEXT_NEWLINK, interface, (flags & CHECK_CONNECTED) ? "up" : "down"); + if (strlen(essid) == 0) { + notifystr = malloc(sizeof(TEXT_NEWLINK) + strlen(interface) + 4); + sprintf(notifystr, TEXT_NEWLINK, interface, (flags & CHECK_CONNECTED) ? "up" : "down"); + } else { + notifystr = malloc(sizeof(TEXT_WIRELESS) + strlen(interface) + 4 + strlen(essid)); + sprintf(notifystr, TEXT_WIRELESS, interface, (flags & CHECK_CONNECTED) ? "up" : "down", essid); + } return notifystr; } diff --git a/netlink-notify.h b/netlink-notify.h index fec85ae..ee61335 100644 --- a/netlink-notify.h +++ b/netlink-notify.h @@ -23,11 +23,13 @@ #include #include #include +#include /* we have to undefine this before including net/if.h to * notget redefined structs, etc. */ #undef __USE_MISC #include #include +#include #include /* systemd headers */ @@ -73,6 +75,9 @@ int match_address(struct addresses_seen *addresses_seen, const char *address, un /*** list_addresses ***/ void list_addresses(struct addresses_seen *addresses_seen, char *interface); +/*** get_ssid ***/ +void get_ssid(const char *interface, char *essid); + /*** newstr_link ***/ char * newstr_link(char *interface, unsigned int flags); -- cgit v1.2.3-54-g00ecf