std::vector<std::string> interfaces::list_addresses(protocol proc, bool include_localhost) { return list_addresses({proc}, include_localhost); }
/*** msg_handler ***/ int msg_handler (struct sockaddr_nl *nl, struct nlmsghdr *msg) { int rc = EXIT_FAILURE; char *notifystr = NULL; unsigned int errcount = 0; GError *error = NULL; struct ifaddrmsg *ifa; struct ifinfomsg *ifi; struct rtattr *rth; int rtl; char buf[INET6_ADDRSTRLEN]; NotifyNotification *tmp_notification = NULL, *notification = NULL; char *icon = NULL; ifa = (struct ifaddrmsg *) NLMSG_DATA (msg); ifi = (struct ifinfomsg *) NLMSG_DATA (msg); /* make sure we have alloced memory for NotifyNotification and addresses_seen struct array */ if (maxinterface < ifi->ifi_index) { ifs = realloc(ifs, (ifi->ifi_index + 1) * sizeof(struct ifs)); while(maxinterface < ifi->ifi_index) { maxinterface++; if (verbose > 0) printf("%s: Initializing interface %d: ", program, maxinterface); /* get interface name and store it * in case the interface does no longer exist this may fail, * use static string '(unknown)' instead */ if (if_indextoname(maxinterface, ifs[maxinterface].name) == NULL) strcpy(ifs[maxinterface].name, "(unknown)"); if (verbose > 0) printf("%s\n", ifs[maxinterface].name); ifs[maxinterface].state = -1; ifs[maxinterface].deleted = 0; ifs[maxinterface].notification = # if NOTIFY_CHECK_VERSION(0, 7, 0) notify_notification_new(TEXT_TOPIC, NULL, NULL); # else notify_notification_new(TEXT_TOPIC, NULL, NULL, NULL); # endif notify_notification_set_category(ifs[maxinterface].notification, PROGNAME); notify_notification_set_urgency(ifs[maxinterface].notification, NOTIFY_URGENCY_NORMAL); notify_notification_set_timeout(ifs[maxinterface].notification, notification_timeout); ifs[maxinterface].addresses_seen = NULL; } } else if (ifs[ifi->ifi_index].deleted == 1) { if (verbose > 0) printf("%s: Ignoring event for deleted interface %d.\n", program, ifi->ifi_index); rc = EXIT_SUCCESS; goto out; } /* make notification point to the array element, will be overwritten * later when needed for address notification */ notification = ifs[ifi->ifi_index].notification; /* get interface name and store it * in case the interface does no longer exist this may fail, but it does not overwrite */ if_indextoname(ifi->ifi_index, ifs[ifi->ifi_index].name); if (verbose > 1) printf("%s: Event for interface %s (%d): flags = %x, msg type = %d\n", program, ifs[ifi->ifi_index].name, ifi->ifi_index, ifa->ifa_flags, msg->nlmsg_type); switch (msg->nlmsg_type) { /* just return for cases we want to ignore * use break if a notification has to be displayed */ case RTM_NEWADDR: rth = IFA_RTA (ifa); rtl = IFA_PAYLOAD (msg); while (rtl && RTA_OK (rth, rtl)) { if ((rth->rta_type == IFA_LOCAL /* IPv4 */ || rth->rta_type == IFA_ADDRESS /* IPv6 */) && ifa->ifa_scope == RT_SCOPE_UNIVERSE /* no IPv6 scope link */) { inet_ntop(ifa->ifa_family, RTA_DATA (rth), buf, sizeof(buf)); /* check if we already notified about this address */ if (match_address(ifs[ifi->ifi_index].addresses_seen, buf, ifa->ifa_prefixlen)) { if (verbose > 0) printf("%s: Address %s/%d already known for %s, ignoring.\n", program, buf, ifa->ifa_prefixlen, ifs[ifi->ifi_index].name); break; } /* add address to struct */ ifs[ifi->ifi_index].addresses_seen = add_address(ifs[ifi->ifi_index].addresses_seen, buf, ifa->ifa_prefixlen); if (verbose > 1) list_addresses(ifs[ifi->ifi_index].addresses_seen, ifs[ifi->ifi_index].name); /* display notification */ notifystr = newstr_addr(ifs[ifi->ifi_index].name, ifa->ifa_family, buf, ifa->ifa_prefixlen); /* we are done, no need to run more loops */ break; } rth = RTA_NEXT (rth, rtl); } /* we did not find anything to notify */ if (notifystr == NULL) { rc = EXIT_SUCCESS; goto out; } /* do we want new notification, not update the notification about link status */ tmp_notification = # if NOTIFY_CHECK_VERSION(0, 7, 0) notify_notification_new(TEXT_TOPIC, NULL, NULL); # else notify_notification_new(TEXT_TOPIC, NULL, NULL, NULL); # endif notify_notification_set_category(tmp_notification, PROGNAME); notify_notification_set_urgency(tmp_notification, NOTIFY_URGENCY_NORMAL); notify_notification_set_timeout(tmp_notification, notification_timeout); notification = tmp_notification; icon = ICON_NETWORK_ADDRESS; break; case RTM_DELADDR: rth = IFA_RTA (ifa); rtl = IFA_PAYLOAD (msg); while (rtl && RTA_OK (rth, rtl)) { if ((rth->rta_type == IFA_LOCAL /* IPv4 */ || rth->rta_type == IFA_ADDRESS /* IPv6 */) && ifa->ifa_scope == RT_SCOPE_UNIVERSE /* no IPv6 scope link */) { inet_ntop(ifa->ifa_family, RTA_DATA (rth), buf, sizeof(buf)); ifs[ifi->ifi_index].addresses_seen = remove_address(ifs[ifi->ifi_index].addresses_seen, buf, ifa->ifa_prefixlen); if (verbose > 1) list_addresses(ifs[ifi->ifi_index].addresses_seen, ifs[ifi->ifi_index].name); /* we are done, no need to run more loops */ break; } rth = RTA_NEXT (rth, rtl); } rc = EXIT_SUCCESS; goto out; case RTM_NEWROUTE: rc = EXIT_SUCCESS; goto out; case RTM_DELROUTE: rc = EXIT_SUCCESS; goto out; case RTM_NEWLINK: /* ignore if state did not change */ if ((ifi->ifi_flags & CHECK_CONNECTED) == ifs[ifi->ifi_index].state) { rc = EXIT_SUCCESS; goto out; } ifs[ifi->ifi_index].state = ifi->ifi_flags & CHECK_CONNECTED; notifystr = newstr_link(ifs[ifi->ifi_index].name, ifi->ifi_flags); icon = ifi->ifi_flags & CHECK_CONNECTED ? ICON_NETWORK_UP : ICON_NETWORK_DOWN; /* free only if interface goes down */ if (!(ifi->ifi_flags & CHECK_CONNECTED)) { free_addresses(ifs[ifi->ifi_index].addresses_seen); ifs[ifi->ifi_index].addresses_seen = NULL; } break; case RTM_DELLINK: notifystr = newstr_away(ifs[ifi->ifi_index].name); icon = ICON_NETWORK_AWAY; free_addresses(ifs[ifi->ifi_index].addresses_seen); /* marking interface deleted makes events for this interface to be ignored */ ifs[ifi->ifi_index].deleted = 1; break; default: /* we should not get here... */ fprintf(stderr, "msg_handler: Unknown netlink nlmsg_type %d.\n", msg->nlmsg_type); goto out; } if (verbose > 0) printf("%s: %s\n", program, notifystr); notify_notification_update(notification, TEXT_TOPIC, notifystr, icon); while (notify_notification_show(notification, &error) == FALSE) { if (errcount > 1) { fprintf(stderr, "%s: Looks like we can not reconnect to notification daemon... Exiting.\n", program); goto out; } else { g_printerr("%s: Error \"%s\" while trying to show notification. Trying to reconnect.\n", program, error->message); errcount++; g_error_free(error); error = NULL; notify_uninit(); usleep (500 * 1000); if (notify_init(PROGNAME) == FALSE) { fprintf(stderr, "%s: Can't create notify.\n", program); goto out; } } } rc = EXIT_SUCCESS; out: if (tmp_notification) g_object_unref(G_OBJECT(tmp_notification)); errcount = 0; free(notifystr); return rc; }