static int fdb_show(int argc, char **argv) { char *filter_dev = NULL; while (argc > 0) { if (strcmp(*argv, "dev") == 0) { NEXT_ARG(); if (filter_dev) duparg("dev", *argv); filter_dev = *argv; } argc--; argv++; } if (filter_dev) { filter_index = if_nametoindex(filter_dev); if (filter_index == 0) { fprintf(stderr, "Cannot find device \"%s\"\n", filter_dev); return -1; } } if (rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETNEIGH) < 0) { perror("Cannot send dump request"); exit(1); } if (rtnl_dump_filter(&rth, print_fdb, stdout) < 0) { fprintf(stderr, "Dump terminated\n"); exit(1); } return 0; }
/**************************************************************** NAME : ipaddr_list 00/06/02 20:02:23 AIM : REMARK : ****************************************************************/ int ipaddr_list( int ifindex, uint32_t *array, int max_elem ) { struct rtnl_handle rth; iplist_ctx ctx; /* init the struct */ ctx.ifindex = ifindex; ctx.addr = array; ctx.max_elem = max_elem; ctx.nb_elem = 0; /* open the rtnetlink socket */ if( rtnl_open( &rth, 0) ) return -1; /* send the request */ if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETADDR) < 0) { perror("Cannot send dump request"); return -1; } /* parse the answer */ if (rtnl_dump_filter(&rth, get_addrinfo, &ctx, NULL, NULL) < 0) { fprintf(stderr, "Flush terminated\n"); exit(1); } /* to close the clocket */ rtnl_close( &rth ); return ctx.nb_elem; }
static PyObject* pyrtnl_dump_filter(PyObject* obj, PyObject* args) { PyRtnlObject* self = (PyRtnlObject*)obj; PyObject *filter; if (!PyArg_ParseTuple(args, "O:dump_filter", &filter)) return NULL; if (!PyCallable_Check(filter)) { PyErr_SetString(PyExc_TypeError, "parameter must be callable"); return NULL; } Py_INCREF(filter); if (rtnl_dump_filter(&self->rth, dump_filter_helper, filter, NULL, NULL) < 0) { Py_DECREF(filter); return NULL; } Py_DECREF(filter); Py_INCREF(Py_None); return Py_None; }
int iprule_list(int argc, char **argv) { struct rtnl_handle rth; int af = preferred_family; if (af == AF_UNSPEC) af = AF_INET; if (argc > 0) { fprintf(stderr, "\"ip rule show\" does not take any arguments.\n"); return -1; } if (rtnl_open(&rth, 0) < 0) return 1; if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) { perror("Cannot send dump request"); return 1; } if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) { fprintf(stderr, "Dump terminated\n"); return 1; } return 0; }
void ipoe_nl_get_sessions(struct list_head *list) { struct nlmsghdr *nlh; struct genlmsghdr *ghdr; struct { struct nlmsghdr n; char buf[1024]; } req; if (rth.fd == -1) return; nlh = &req.n; nlh->nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN); nlh->nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; nlh->nlmsg_type = ipoe_genl_id; nlh->nlmsg_seq = rth.dump = ++rth.seq; ghdr = NLMSG_DATA(&req.n); ghdr->cmd = IPOE_CMD_GET; if (rtnl_send(&rth, (char *)nlh, nlh->nlmsg_len) < 0) { log_emerg("ipoe: failed to send dump request: %s\n", strerror(errno)); return; } rtnl_dump_filter(&rth, dump_session, list, NULL, NULL); }
/* * returns: -1 - address not found, 0 - addr is ok, 1 - addr is tentative */ int is_addr_tentative(char * ifacename, int iface, char * addr) { char buf[256]; char packed1[16]; char packed2[16]; struct rtattr * rta_tb[IFA_MAX+1]; struct nlmsg_list *ainfo = NULL; struct nlmsg_list *head = NULL; struct rtnl_handle rth; int tentative = LOWLEVEL_TENTATIVE_DONT_KNOW; inet_pton6(addr,packed1); rtnl_open(&rth, 0); /* 2nd attribute: AF_UNSPEC, AF_INET, AF_INET6 */ /* rtnl_wilddump_request(&rth, AF_PACKET, RTM_GETLINK); */ rtnl_wilddump_request(&rth, AF_INET6, RTM_GETADDR); rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL); head = ainfo; while (ainfo) { struct nlmsghdr *n = &ainfo->h; struct ifaddrmsg *ifa = NLMSG_DATA(n); memset(rta_tb, 0, sizeof(*rta_tb)); if (ifa->ifa_index == iface && ifa->ifa_family==AF_INET6) { parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); if (!rta_tb[IFA_LOCAL]) rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; if (!rta_tb[IFA_ADDRESS]) rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL]; inet_ntop6(RTA_DATA(rta_tb[IFA_LOCAL]), buf /*, sizeof(buf)*/); memcpy(packed2,RTA_DATA(rta_tb[IFA_LOCAL]),16); /* print_packed(packed1); printf(" "); print_packed(packed2); printf("\n"); */ /* is this addr which are we looking for? */ if (!memcmp(packed1,packed2,16) ) { if (ifa->ifa_flags & IFA_F_TENTATIVE) tentative = LOWLEVEL_TENTATIVE_YES; else tentative = LOWLEVEL_TENTATIVE_NO; } } ainfo = ainfo->next; } /* now delete list */ while (head) { ainfo = head; head = head->next; free(ainfo); } rtnl_close(&rth); return tentative; }
void neigh_flush_table(char *iface) { struct rtnl_handle rth; char flushb[4096-512]; DEBUG_MSG("neigh_flush_table %s", iface); memset(&filter, 0, sizeof(filter)); filter.state = ~0; filter.family = AF_INET; /* flush all but permanent and noarp */ filter.state = ~(NUD_PERMANENT|NUD_NOARP); /* open the netlink socket */ if (rtnl_open(&rth, 0) < 0) ERROR_MSG("rtnl_open()"); ll_init_map(&rth); /* fill the device data */ if ((filter.index = ll_name_to_index(iface)) == 0) ERROR_MSG("ll_name_to_index(%s)", iface); filter.flushb = flushb; filter.flushp = 0; filter.flushe = sizeof(flushb); filter.rth = &rth; filter.state &= ~NUD_FAILED; for (;;) { if (rtnl_wilddump_request(&rth, filter.family, RTM_GETNEIGH) < 0) ERROR_MSG("rtnl_wilddump_request()"); filter.flushed = 0; /* * count how many neigh are to be flushed * and prepare the data */ if (rtnl_dump_filter(&rth, count_neigh, stdout, NULL, NULL) < 0) ERROR_MSG("rtnl_dump_filter()"); if (filter.flushed == 0) return; if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) ERROR_MSG("rtnl_send()"); filter.flushp = 0; DEBUG_MSG("*** deleting %d entries ***", filter.flushed); } }
static int iprule_flush(int argc, char **argv) { int af = preferred_family; #ifdef HTC_IPRULE_DEBUG ALOGD("[IPROUTE2]iprule_flush +"); #endif if (af == AF_UNSPEC) af = AF_INET; if (argc > 0) { fprintf(stderr, "\"ip rule flush\" does not allow arguments\n"); return -1; } if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) { perror("Cannot send dump request"); return 1; } if (rtnl_dump_filter(&rth, flush_rule, NULL) < 0) { fprintf(stderr, "Flush terminated\n"); return 1; } #ifdef HTC_IPRULE_DEBUG ALOGD("[IPROUTE2]iprule_flush -"); #endif return 0; }
/* * rule_flush_table_range: deletes all the rules which lookup the table X. * The table X is any table in the range of `a' <= X <= `b'. */ int rule_flush_table_range(int family, int a, int b) { struct rtnl_handle rth; int arg[2]; if (rtnl_open(&rth, 0) < 0) return 1; if (rtnl_wilddump_request(&rth, family, RTM_GETRULE) < 0) { error("Cannot dump the routing rule table"); return -1; } arg[0] = a; arg[1] = b; if (rtnl_dump_filter (&rth, rule_flush_table_range_filter, arg, NULL, NULL) < 0) { error("Flush terminated"); return -1; } rtnl_close(&rth); return 0; }
struct rt_entry * rt_fetch(struct rt_entry *r) { struct rtnl_handle rth; // open netlink socket of NETLINK_ROUTE if (rtnl_open(&rth, 0) < 0) { printf("Can not initialize netlink interface...\n"); return NULL; } ll_init_map(&rth); if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETROUTE) < 0) { printf("Cannot send dump request\n"); close(rth.fd); return NULL; } if (rtnl_dump_filter(&rth, rt_filter, r, NULL, NULL) < 0) { printf("Dump terminated.\n"); close(rth.fd); return NULL; } close(rth.fd); return r; }
static void load_info(void) { struct ifstat_ent *db, *n; struct rtnl_handle rth; if (rtnl_open(&rth, 0) < 0) exit(1); if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETLINK) < 0) { perror("Cannot send dump request"); exit(1); } if (rtnl_dump_filter(&rth, get_netstat_nlmsg, NULL, NULL, NULL) < 0) { fprintf(stderr, "Dump terminated\n"); exit(1); } rtnl_close(&rth); db = kern_db; kern_db = NULL; while (db) { n = db; db = db->next; n->next = kern_db; kern_db = n; } }
int tc_qdisc_list(int argc, char **argv) { struct tcmsg t; struct rtnl_handle rth; char d[16]; memset(&t, 0, sizeof(t)); t.tcm_family = AF_UNSPEC; memset(&d, 0, sizeof(d)); while (argc > 0) { if (strcmp(*argv, "dev") == 0) { NEXT_ARG(); strncpy(d, *argv, sizeof(d)-1); #ifdef TC_H_INGRESS } else if (strcmp(*argv, "ingress") == 0) { if (t.tcm_parent) { fprintf(stderr, "Duplicate parent ID\n"); usage(); } t.tcm_parent = TC_H_INGRESS; #endif } else if (matches(*argv, "help") == 0) { usage(); } else { fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv); return -1; } argc--; argv++; } if (rtnl_open(&rth, 0) < 0) { fprintf(stderr, "Cannot open rtnetlink\n"); exit(1); } ll_init_map(&rth); if (d[0]) { if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) { fprintf(stderr, "Cannot find device \"%s\"\n", d); exit(1); } filter_ifindex = t.tcm_ifindex; } if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) { perror("Cannot send dump request"); exit(1); } if (rtnl_dump_filter(&rth, print_qdisc, stdout, NULL, NULL) < 0) { fprintf(stderr, "Dump terminated\n"); exit(1); } rtnl_close(&rth); return 0; }
static int do_show(int argc, char **argv) { struct { struct nlmsghdr n; struct netconfmsg ncm; char buf[1024]; } req; ipnetconf_reset_filter(0); filter.family = preferred_family; if (filter.family == AF_UNSPEC) filter.family = AF_INET; while (argc > 0) { if (strcmp(*argv, "dev") == 0) { NEXT_ARG(); filter.ifindex = ll_name_to_index(*argv); if (filter.ifindex <= 0) { fprintf(stderr, "Device \"%s\" does not exist.\n", *argv); return -1; } } argv++; argc--; } ll_init_map(&rth); if (filter.ifindex) { memset(&req, 0, sizeof(req)); req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct netconfmsg)); req.n.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK; req.n.nlmsg_type = RTM_GETNETCONF; req.ncm.ncm_family = filter.family; if (filter.ifindex) addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX, &filter.ifindex, sizeof(filter.ifindex)); if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) { perror("Can not send request"); exit(1); } rtnl_listen(&rth, print_netconf, stdout); } else { dump: if (rtnl_wilddump_request(&rth, filter.family, RTM_GETNETCONF) < 0) { perror("Cannot send dump request"); exit(1); } if (rtnl_dump_filter(&rth, print_netconf2, stdout) < 0) { fprintf(stderr, "Dump terminated\n"); exit(1); } if (preferred_family == AF_UNSPEC) { preferred_family = AF_INET6; filter.family = AF_INET6; goto dump; } } return 0; }
static int iprule_list_or_save(int argc, char **argv, int save) { rtnl_filter_t filter = print_rule; int af = preferred_family; if (af == AF_UNSPEC) af = AF_INET; if (argc > 0) { fprintf(stderr, "\"ip rule %s\" does not take any arguments.\n", save ? "save" : "show"); return -1; } if (save) { if (save_rule_prep()) return -1; filter = save_rule; } if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) { perror("Cannot send dump request"); return 1; } if (rtnl_dump_filter(&rth, filter, stdout) < 0) { fprintf(stderr, "Dump terminated\n"); return 1; } return 0; }
static int vlan_show(int argc, char **argv) { char *filter_dev = NULL; while (argc > 0) { if (strcmp(*argv, "dev") == 0) { NEXT_ARG(); if (filter_dev) duparg("dev", *argv); filter_dev = *argv; } else if (strcmp(*argv, "vid") == 0) { NEXT_ARG(); if (filter_vlan) duparg("vid", *argv); filter_vlan = atoi(*argv); } argc--; argv++; } if (filter_dev) { if ((filter_index = if_nametoindex(filter_dev)) == 0) { fprintf(stderr, "Cannot find device \"%s\"\n", filter_dev); return -1; } } if (rtnl_wilddump_req_filter(&rth, PF_BRIDGE, RTM_GETLINK, (compress_vlans ? RTEXT_FILTER_BRVLAN_COMPRESSED : RTEXT_FILTER_BRVLAN)) < 0) { perror("Cannont send dump request"); exit(1); } if (json_output) { jw_global = jsonw_new(stdout); if (!jw_global) { fprintf(stderr, "Error allocation json object\n"); exit(1); } jsonw_start_object(jw_global); } else { printf("port\tvlan ids\n"); } if (rtnl_dump_filter(&rth, print_vlan, stdout) < 0) { fprintf(stderr, "Dump ternminated\n"); exit(1); } if (jw_global) { jsonw_end_object(jw_global); jsonw_destroy(&jw_global); } return 0; }
int FAST_FUNC xrtnl_dump_filter(struct rtnl_handle *rth, int (*filter)(const struct sockaddr_nl *, struct nlmsghdr *, void *), void *arg1) { int ret = rtnl_dump_filter(rth, filter, arg1/*, NULL, NULL*/); if (ret < 0) bb_error_msg_and_die("dump terminated"); return ret; }
void TunManager::probe() { std::lock_guard<std::mutex> lock(mutex_); stop(); // stop all interfaces intfs_.clear(); // clear all interface info auto ret = rtnl_wilddump_request(&rth_, AF_UNSPEC, RTM_GETLINK); sysCheckError(ret, "Cannot send RTM_GETLINK request"); ret = rtnl_dump_filter(&rth_, getLinkRespParser, this); sysCheckError(ret, "Cannot process RTM_GETLINK response"); ret = rtnl_wilddump_request(&rth_, AF_UNSPEC, RTM_GETADDR); sysCheckError(ret, "Cannot send RTM_GETADDR request"); ret = rtnl_dump_filter(&rth_, getAddrRespParser, this); sysCheckError(ret, "Cannot process RTM_GETADDR response"); // Bring up all interfaces. Interfaces could be already up. for (const auto& intf : intfs_) { bringupIntf(intf.second->getName(), intf.second->getIfIndex()); } start(); }
int iplink_ifla_xstats(int argc, char **argv) { struct link_util *lu = NULL; __u32 filt_mask; if (!argc) { fprintf(stderr, "xstats: missing argument\n"); return -1; } if (matches(*argv, "type") == 0) { NEXT_ARG(); lu = get_link_kind(*argv); if (!lu) invarg("invalid type", *argv); } else if (matches(*argv, "help") == 0) { print_explain(stdout); return 0; } else { invarg("unknown argument", *argv); } if (!lu) { print_explain(stderr); return -1; } if (!lu->print_ifla_xstats) { fprintf(stderr, "xstats: link type %s doesn't support xstats\n", lu->id); return -1; } if (lu->parse_ifla_xstats && lu->parse_ifla_xstats(lu, argc-1, argv+1)) return -1; if (strstr(lu->id, "_slave")) filt_mask = IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS_SLAVE); else filt_mask = IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS); if (rtnl_wilddump_stats_req_filter(&rth, AF_UNSPEC, RTM_GETSTATS, filt_mask) < 0) { perror("Cannont send dump request"); return -1; } if (rtnl_dump_filter(&rth, lu->print_ifla_xstats, stdout) < 0) { fprintf(stderr, "Dump terminated\n"); return -1; } return 0; }
int ll_init_map(struct rtnl_handle *rth) { if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) { bb_perror_msg_and_die("cannot send dump request"); } if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) { bb_error_msg_and_die("dump terminated"); } return 0; }
int ll_init_map(struct rtnl_handle *rth) { if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) { // perror("Cannot send dump request"); exit(1); } if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) { // fprintf(stderr, "Dump terminated\n"); exit(1); } return 0; }
void add_classes() { // call dump of classes, these needs to be dumped on per device basis struct tcmsg t; struct rtnl_handle rth; char d[16]; char line[1024]; FILE *dev; char *p; memset(&rth,0,sizeof(struct rtnl_handle)); memset(&t, 0, sizeof(t)); t.tcm_family = AF_UNSPEC; memset(&d, 0, sizeof(d)); if (rtnl_open(&rth, 0) < 0) { snmp_log(LOG_NOTICE, "Cannot open rtnetlink"); return; } dev=fopen("/proc/net/dev","r"); if(dev==NULL) { snmp_log(LOG_WARNING,"qos-ext: cannot open /proc/net/dev for reading, class stats won't be avaiable"); return; } while(fscanf(dev,"%s",line)!=EOF) { //find existing devices p=index(line,':'); if(p==NULL) continue; *p='\0'; // line contains device_name.... need to translate name into index t.tcm_ifindex = ll_name_to_index(line); if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) { //dump all classes on device with index t.tcm_ifindex snmp_log(LOG_WARNING,"qos-ext: add_new_entries to table: cannot send dump request"); fclose(dev); return; } if (rtnl_dump_filter(&rth, insert_entry, NULL, NULL, NULL) < 0) { //append all classes entris into the internal table snmp_log(LOG_WARNING, "qos-ext: add_new entries to table: class dump terminated"); fclose(dev); return; } } fclose(dev); rtnl_close(&rth); }
/* Based on iproute2/ip/iproute.c:iproute_list_flush_or_save. */ static int dump(int tbl_id) { reset_filter(); filter.tb = tbl_id; if (rtnl_wilddump_request(&rth, AF_XIA, RTM_GETROUTE) < 0) { perror("Cannot send dump request"); exit(1); } if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) { fprintf(stderr, "Dump terminated\n"); exit(1); } return 0; }
/* XXX This should become a function in a library, there're very * little variance of this repeated code instances */ static int showneighs(void) { reset_filter(); if (rtnl_wilddump_request(&rth, AF_XIA, RTM_GETROUTE) < 0) { perror("Cannot send dump request"); exit(1); } if (rtnl_dump_filter(&rth, print_neigh, stdout, NULL, NULL) < 0) { fprintf(stderr, "Dump terminated\n"); exit(1); } return 0; }
void list(void) { struct rtnl_handle rth; if (rtnl_open(&rth,0)) { perror("Cannot open rtnetlink"); return; } if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) { perror("Cannot send dump request"); return; } if (rtnl_dump_filter(&rth, print_link, NULL) < 0) { fprintf(stderr, "Dump terminated\n"); return; } rtnl_close(&rth); }
int init_bridge_ops(void) { if(rtnl_open(&rth, RTMGRP_LINK) < 0) { fprintf(stderr, "Couldn't open rtnl socket for monitoring\n"); return -1; } if(rtnl_open(&rth_state, 0) < 0) { fprintf(stderr, "Couldn't open rtnl socket for setting state\n"); return -1; } if(rtnl_wilddump_request(&rth, PF_BRIDGE, RTM_GETLINK) < 0) { //fprintf(stderr, "Cannot send dump request: %m\n"); fprintf(stderr, "Cannot send dump request: %s\n", strerror(errno)); return -1; } if(rtnl_dump_filter(&rth, dump_msg, stdout, NULL, NULL) < 0) { fprintf(stderr, "Dump terminated\n"); return -1; } if(fcntl(rth.fd, F_SETFL, O_NONBLOCK) < 0) { //fprintf(stderr, "Error setting O_NONBLOCK: %m\n"); fprintf(stderr, "Error setting O_NONBLOCK: %s\n", strerror(errno)); return -1; } br_handler.fd = rth.fd; br_handler.arg = NULL; br_handler.handler = br_ev_handler; if(add_epoll(&br_handler) < 0){ return -1; } return 0; }
int rtnl_iterate(int proto, int type, rtnl_filter_t func, void *extarg) { struct rtnl_handle rth; if (rtnl_open_byproto(&rth, 0, proto) < 0) return -1; if (rtnl_wilddump_request(&rth, AF_INET6, type) < 0) { rtnl_close(&rth); return -1; } if (rtnl_dump_filter(&rth, func, extarg, NULL, NULL) < 0) { rtnl_close(&rth); return -1; } rtnl_close(&rth); return 0; }
static int get_tunnel(struct l2tp_data *p) { GENL_REQUEST(req, 1024, genl_family, 0, L2TP_GENL_VERSION, L2TP_CMD_TUNNEL_GET, NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST); req.n.nlmsg_seq = genl_rth.dump = ++genl_rth.seq; if (p->config.tunnel_id) addattr32(&req.n, 1024, L2TP_ATTR_CONN_ID, p->config.tunnel_id); if (rtnl_send(&genl_rth, &req, req.n.nlmsg_len) < 0) return -2; if (rtnl_dump_filter(&genl_rth, tunnel_nlmsg, p) < 0) { fprintf(stderr, "Dump terminated\n"); exit(1); } return 0; }
int ll_init_map(struct rtnl_handle *rth) { static int initialized; if (initialized) return 0; if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) { perror("Cannot send dump request"); exit(1); } if (rtnl_dump_filter(rth, ll_remember_index, NULL, NULL, NULL) < 0) { fprintf(stderr, "Dump terminated\n"); exit(1); } initialized = 1; return 0; }
int mpls_list(int cmd,int argc, char **argv) { struct genlmsghdr *ghdr; struct rtnl_handle rth; struct { struct nlmsghdr n; char buf[4096]; } req; if (rtnl_open_byproto(&rth, 0, NETLINK_GENERIC) < 0) { fprintf (stderr, "Error opening nl socket\n"); //exit(-1); return -1; } memset(&req, 0, sizeof(req)); req.n.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN); req.n.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST; req.n.nlmsg_type = PF_MPLS; req.n.nlmsg_seq = rth.dump = ++rth.seq; ghdr = NLMSG_DATA(&req.n); ghdr->cmd = cmd; if (rtnl_send(&rth, (const char *)&req.n, req.n.nlmsg_len) < 0) { perror("Cannot send dump request"); //exit(1); return 1; } if (rtnl_dump_filter(&rth, print_mpls, stdout, NULL, NULL) < 0) { fprintf(stderr, "Dump terminated\n"); //exit(1); return 1; } rtnl_close(&rth); return 0; }
/* * route_get_exact_prefix: it dumps the routing table and search for a route * which has the prefix equal to `prefix', if it is found its destination * address is stored in `dst' and its interface name in `dev_name' (which must * be IFNAMSIZ big). */ int route_get_exact_prefix_dst(inet_prefix prefix, inet_prefix * dst, char *dev_name) { int do_ipv6 = AF_UNSPEC; struct rtnl_handle rth; char dst_data[sizeof(inet_prefix) + IFNAMSIZ]; route_reset_filter(); filter.tb = RT_TABLE_MAIN; filter.mdst = prefix; filter.rdst = filter.mdst; if (do_ipv6 == AF_UNSPEC && filter.tb) do_ipv6 = AF_INET; if (rtnl_open(&rth, 0) < 0) return -1; ll_init_map(&rth); if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) { error(ERROR_MSG "Cannot send dump request" ERROR_POS); return -1; } setzero(dst_data, sizeof(dst_data)); if (rtnl_dump_filter(&rth, route_get_gw, dst_data, NULL, NULL) < 0) { debug(DBG_NORMAL, ERROR_MSG "Dump terminated" ERROR_POS); return -1; } inet_copy(dst, (inet_prefix *) dst_data); memcpy(dev_name, dst_data + sizeof(inet_prefix), IFNAMSIZ); rtnl_close(&rth); return 0; }