int rtmon_read_event(struct rtmon_t *rtmon) { struct sockaddr_nl nladdr; struct msghdr msg; struct iovec iov[2]; struct nlmsghdr nlh; char buffer[65536]; int ret; iov[0].iov_base = (void *)&nlh; iov[0].iov_len = sizeof(nlh); iov[1].iov_base = (void *)buffer; iov[1].iov_len = sizeof(buffer); msg.msg_name = (void *)&(nladdr); msg.msg_namelen = sizeof(nladdr); msg.msg_iov = iov; msg.msg_iovlen = sizeof(iov)/sizeof(iov[0]); ret = recvmsg(rtmon->fd, &msg, 0); if (ret < 0) { return ret; } syslog(LOG_DEBUG, "Type: %i (%s)",(nlh.nlmsg_type),lookup_name(typenames,nlh.nlmsg_type)); #define FLAG(x) if (nlh.nlmsg_flags & x) printf(" %s",#x) FLAG(NLM_F_REQUEST); FLAG(NLM_F_MULTI); FLAG(NLM_F_ACK); FLAG(NLM_F_ECHO); FLAG(NLM_F_REPLACE); FLAG(NLM_F_EXCL); FLAG(NLM_F_CREATE); FLAG(NLM_F_APPEND); #undef FLAG syslog(LOG_DEBUG, "Seq : %i Pid : %i",nlh.nlmsg_seq,nlh.nlmsg_pid); rtmon_discover_ifaces(rtmon); rtmon_discover_routes(rtmon); if (_options.debug) { rtmon_print_ifaces(rtmon, 1); rtmon_print_routes(rtmon, 1); } rtmon_check_updates(rtmon); return 0; }
int rtmon_init(struct rtmon_t *rtmon, rtmon_callback func) { memset(rtmon, 0, sizeof(struct rtmon_t)); rtmon->fd = open_netlink(); rtmon->cb = func; if (rtmon->fd > 0) { rtmon_discover_ifaces(rtmon); rtmon_discover_routes(rtmon); if (_options.debug) { rtmon_print_ifaces(rtmon, 1); rtmon_print_routes(rtmon, 1); } rtmon_check_updates(rtmon); return 0; } return -1; }