static int proc_route(struct rtmon_t *rtmon, struct rtmon_iface *iface, struct rtmon_route *route) { rtmon_print_ifaces(rtmon, 1); rtmon_print_routes(rtmon, 1); return 0; }
int main(int argc, char *argv[]) { int keep_going = 1; int reload_config = 1; int i; struct rtmon_t _rtmon; /*int selfpipe = selfpipe_init();*/ memset(&_options, 0, sizeof(_options)); if (rtmon_init(&_rtmon, proc_route)) { err(1,"netlink"); } for (i=1; i < argc; i++) { if (strcmp(argv[i], "-debug")==0) { debug = 1; } else if (strcmp(argv[i], "-file")==0) { chilli_conf = argv[i+1]; } else if (strcmp(argv[i], "-pid")==0) { chilli_pid = atoi(argv[i+1]); } } _options.foreground = debug; _options.debug = debug; log_dbg("running"); chilli_signals(&keep_going, &reload_config); rtmon_discover_ifaces(&_rtmon); rtmon_discover_routes(&_rtmon); if (debug) { rtmon_print_ifaces(&_rtmon, 1); rtmon_print_routes(&_rtmon, 1); } rtmon_check_updates(&_rtmon); while (keep_going) { /* select */ /* check selfpipe */ rtmon_read_event(&_rtmon); } /* selfpipe_finish();*/ return 0; }
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; }