本文整理汇总了C++中RTM_RTA函数的典型用法代码示例。如果您正苦于以下问题:C++ RTM_RTA函数的具体用法?C++ RTM_RTA怎么用?C++ RTM_RTA使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RTM_RTA函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: flush_addrlabel
static int flush_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
{
struct rtnl_handle rth2;
struct rtmsg *r = NLMSG_DATA(n);
int len = n->nlmsg_len;
struct rtattr * tb[IFAL_MAX+1];
len -= NLMSG_LENGTH(sizeof(*r));
if (len < 0)
return -1;
parse_rtattr(tb, IFAL_MAX, RTM_RTA(r), len);
if (tb[IFAL_ADDRESS]) {
n->nlmsg_type = RTM_DELADDRLABEL;
n->nlmsg_flags = NLM_F_REQUEST;
if (rtnl_open(&rth2, 0) < 0)
return -1;
if (rtnl_talk(&rth2, n, NULL, 0) < 0)
return -2;
rtnl_close(&rth2);
}
return 0;
}
示例2: rule_flush_table_range_filter
/*
* rule_flush_table_range_filter: rtnl_dump filter for
* rule_flush_table_range() (see below)
*/
int
rule_flush_table_range_filter(const struct sockaddr_nl *who,
struct nlmsghdr *n, void *arg)
{
struct rtnl_handle rth2;
struct rtmsg *r = NLMSG_DATA(n);
int len = n->nlmsg_len;
struct rtattr *tb[RTA_MAX + 1];
u_int a = *(u_int *) arg;
u_int b = *((u_int *) arg + 1);
len -= NLMSG_LENGTH(sizeof(*r));
if (len < 0)
return -1;
parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
if (tb[RTA_PRIORITY] && (r->rtm_table >= a && r->rtm_table <= b)) {
n->nlmsg_type = RTM_DELRULE;
n->nlmsg_flags = NLM_F_REQUEST;
if (rtnl_open(&rth2, 0) < 0)
return -1;
if (rtnl_talk(&rth2, n, 0, 0, NULL, NULL, NULL) < 0)
return -2;
rtnl_close(&rth2);
}
return 0;
}
示例3: parseRoutes
static void parseRoutes(struct nlmsghdr *nlHdr, struct route_info *rtInfo) {
struct rtmsg *rtMsg = (struct rtmsg *)NLMSG_DATA(nlHdr);
if ((rtMsg->rtm_family != AF_INET) || (rtMsg->rtm_table != RT_TABLE_MAIN))
return;
struct rtattr *rtAttr = (struct rtattr *)RTM_RTA(rtMsg);
int rtLen = RTM_PAYLOAD(nlHdr);
for (; RTA_OK(rtAttr, rtLen); rtAttr = RTA_NEXT(rtAttr, rtLen)) {
switch (rtAttr->rta_type) {
case RTA_OIF:
if_indextoname(*(int *)RTA_DATA(rtAttr), rtInfo->ifName);
break;
case RTA_GATEWAY:
rtInfo->gateWay = *(in_addr *)RTA_DATA(rtAttr);
break;
case RTA_PREFSRC:
rtInfo->srcAddr = *(in_addr *)RTA_DATA(rtAttr);
break;
case RTA_DST:
rtInfo->dstAddr = *(in_addr *)RTA_DATA(rtAttr);
break;
}
}
}
示例4: NLMSG_DATA
bool route_table_mgr::parse_enrty(nlmsghdr *nl_header, route_val *p_val)
{
int len;
struct rtmsg *rt_msg;
struct rtattr *rt_attribute;
// get route entry header
rt_msg = (struct rtmsg *) NLMSG_DATA(nl_header);
// we are not concerned about the local and default route table
if (rt_msg->rtm_family != AF_INET || rt_msg->rtm_table == RT_TABLE_LOCAL || rt_msg->rtm_table == RT_TABLE_DEFAULT)
return false;
p_val->set_protocol(rt_msg->rtm_protocol);
p_val->set_scope(rt_msg->rtm_scope);
p_val->set_type(rt_msg->rtm_type);
p_val->set_table_id(rt_msg->rtm_table);
in_addr_t dst_mask = htonl(VMA_NETMASK(rt_msg->rtm_dst_len));
p_val->set_dst_mask(dst_mask);
p_val->set_dst_pref_len(rt_msg->rtm_dst_len);
len = RTM_PAYLOAD(nl_header);
rt_attribute = (struct rtattr *) RTM_RTA(rt_msg);
for (;RTA_OK(rt_attribute, len);rt_attribute=RTA_NEXT(rt_attribute,len)) {
parse_attr(rt_attribute, p_val);
}
p_val->set_state(true);
p_val->set_str();
return true;
}
示例5: resolve_mac_from_cache_parse
static int resolve_mac_from_cache_parse(struct ndmsg *ndmsg, size_t len_payload,
struct ether_addr *mac_addr,
uint8_t *l3addr,
size_t l3_len)
{
int l3found, llfound;
struct rtattr *rtattr;
struct ether_addr mac_empty;
l3found = 0;
llfound = 0;
memset(&mac_empty, 0, sizeof(mac_empty));
for (rtattr = RTM_RTA(ndmsg); RTA_OK(rtattr, len_payload);
rtattr = RTA_NEXT(rtattr, len_payload)) {
switch (rtattr->rta_type) {
case NDA_DST:
memcpy(l3addr, RTA_DATA(rtattr), l3_len);
l3found = 1;
break;
case NDA_LLADDR:
memcpy(mac_addr, RTA_DATA(rtattr), ETH_ALEN);
if (memcmp(mac_addr, &mac_empty,
sizeof(mac_empty)) == 0)
llfound = 0;
else
llfound = 1;
break;
}
}
return l3found && llfound;
}
示例6: flush_rule
static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
{
struct rtnl_handle rth2;
struct rtmsg *r = NLMSG_DATA(n);
int len = n->nlmsg_len;
struct rtattr * tb[FRA_MAX+1];
len -= NLMSG_LENGTH(sizeof(*r));
if (len < 0)
return -1;
parse_rtattr(tb, FRA_MAX, RTM_RTA(r), len);
if (tb[FRA_PRIORITY]) {
n->nlmsg_type = RTM_DELRULE;
n->nlmsg_flags = NLM_F_REQUEST;
if (rtnl_open(&rth2, 0) < 0)
return -1;
if (rtnl_talk(&rth2, n, 0, 0, NULL) < 0)
return -2;
rtnl_close(&rth2);
}
return 0;
}
示例7: netlink_query_add
/*
* Add RTA_SRC or RTA_DST attribute to netlink query message.
*/
void netlink_query_add(char *msgbuf, int rta_type, ip_address *addr)
{
struct nlmsghdr *nlmsg;
struct rtmsg *rtmsg;
struct rtattr *rtattr;
int len, rtlen;
void *p;
nlmsg = (struct nlmsghdr *)msgbuf;
rtmsg = (struct rtmsg *)NLMSG_DATA(nlmsg);
/* Find first empty attribute slot */
rtlen = RTM_PAYLOAD(nlmsg);
rtattr = (struct rtattr *)RTM_RTA(rtmsg);
while (RTA_OK(rtattr, rtlen))
rtattr = RTA_NEXT(rtattr, rtlen);
/* Add attribute */
if (rtmsg->rtm_family == AF_INET) {
len = 4;
p = (void*)&addr->u.v4.sin_addr.s_addr;
} else {
len = 16;
p = (void*)addr->u.v6.sin6_addr.s6_addr;
}
rtattr->rta_type = rta_type;
rtattr->rta_len = sizeof(struct rtattr) + len; /* bytes */
memmove(RTA_DATA(rtattr), p, len);
if (rta_type == RTA_SRC)
rtmsg->rtm_src_len = len * 8; /* bits */
else
rtmsg->rtm_dst_len = len * 8;
nlmsg->nlmsg_len += rtattr->rta_len;
}
示例8: getmsg
static int
getmsg(struct rtmsg *rtm, int msglen, struct rpfctl *rpf)
{
mifi_t vifi;
struct uvif *v;
struct rtattr *rta[RTA_MAX + 1];
if (rtm->rtm_type == RTN_LOCAL) {
/* tracef(TRF_NETLINK, "NETLINK: local address"); */
log_msg(LOG_DEBUG, 0, "NETLINK: local address");
if ((rpf->iif = local_address(&rpf->source)) != MAXMIFS) {
rpf->rpfneighbor = rpf->source;
return TRUE;
}
return FALSE;
}
memset(&rpf->rpfneighbor, 0, sizeof(rpf->rpfneighbor)); /* initialized */
if (rtm->rtm_type != RTN_UNICAST) {
/* tracef(TRF_NETLINK, "NETLINK: route type is %d", rtm->rtm_type); */
log_msg(LOG_DEBUG, 0, "NETLINK: route type is %d", rtm->rtm_type);
return FALSE;
}
memset(rta, 0, sizeof(rta));
parse_rtattr(rta, RTA_MAX, RTM_RTA(rtm), msglen - sizeof(*rtm));
if (rta[RTA_OIF]) {
int ifindex = *(int *) RTA_DATA(rta[RTA_OIF]);
for (vifi = 0, v = uvifs; vifi < numvifs; ++vifi, ++v) {
if (v->uv_ifindex == ifindex)
break;
}
if (vifi >= numvifs) {
log_msg(LOG_WARNING, 0, "NETLINK: ifindex=%d, but no vif", ifindex);
return FALSE;
}
/* tracef(TRF_NETLINK, "NETLINK: vif %d, ifindex=%d", vifi, ifindex);*/
log_msg(LOG_DEBUG, 0, "NETLINK: vif %d, ifindex=%d", vifi, ifindex);
} else {
log_msg(LOG_WARNING, 0, "NETLINK: no interface");
return FALSE;
}
if (rta[RTA_GATEWAY]) {
struct in6_addr gw;
memcpy(&gw,RTA_DATA(rta[RTA_GATEWAY]),sizeof(gw));
/* __u32 gw = *(__u32 *) RTA_DATA(rta[RTA_GATEWAY]); */
/* tracef(TRF_NETLINK, "NETLINK: gateway is %s", inet6_fmt(gw)); */
log_msg(LOG_DEBUG, 0, "NETLINK: gateway is %s", inet6_fmt(&gw));
init_sin6(&rpf->rpfneighbor);
rpf->rpfneighbor.sin6_addr = gw;
rpf->rpfneighbor.sin6_scope_id = v->uv_ifindex;
} else
rpf->rpfneighbor = rpf->source;
rpf->iif = vifi;
return TRUE;
}
示例9: get_default_gw
// gw and iface[IF_NAMESIZE] MUST be allocated
int get_default_gw(struct in_addr *gw, char *iface)
{
struct rtmsg req;
unsigned int nl_len;
char buf[8192];
struct nlmsghdr *nlhdr;
if (!gw || !iface) {
return -1;
}
// Send RTM_GETROUTE request
memset(&req, 0, sizeof(req));
int sock = send_nl_req(RTM_GETROUTE, 0, &req, sizeof(req));
// Read responses
nl_len = read_nl_sock(sock, buf, sizeof(buf));
if (nl_len <= 0) {
return -1;
}
// Parse responses
nlhdr = (struct nlmsghdr *)buf;
while (NLMSG_OK(nlhdr, nl_len)) {
struct rtattr *rt_attr;
struct rtmsg *rt_msg;
int rt_len;
int has_gw = 0;
rt_msg = (struct rtmsg *) NLMSG_DATA(nlhdr);
if ((rt_msg->rtm_family != AF_INET) || (rt_msg->rtm_table != RT_TABLE_MAIN)) {
return -1;
}
rt_attr = (struct rtattr *) RTM_RTA(rt_msg);
rt_len = RTM_PAYLOAD(nlhdr);
while (RTA_OK(rt_attr, rt_len)) {
switch (rt_attr->rta_type) {
case RTA_OIF:
if_indextoname(*(int *) RTA_DATA(rt_attr), iface);
break;
case RTA_GATEWAY:
gw->s_addr = *(unsigned int *) RTA_DATA(rt_attr);
has_gw = 1;
break;
}
rt_attr = RTA_NEXT(rt_attr, rt_len);
}
if (has_gw) {
return 0;
}
nlhdr = NLMSG_NEXT(nlhdr, nl_len);
}
return -1;
}
示例10: handle_route_msg
static int handle_route_msg(struct nlmsghdr *nlh, int n)
{
struct rtmsg *rtm = NLMSG_DATA(nlh);
struct rtattr *tb[RTN_MAX];
parse_rt_attrs(tb, RTN_MAX, RTM_RTA(rtm), RTM_PAYLOAD(nlh));
handle_route_attrs(rtm, tb, nlh->nlmsg_type);
return 0;
}
示例11: handle_neigh_msg
static int handle_neigh_msg(struct nlmsghdr *nlh, int n)
{
struct rtattr *tb[NDA_MAX];
struct ndmsg *ndm = NLMSG_DATA(nlh);
parse_rt_attrs(tb, NDA_MAX, RTM_RTA(ndm), RTM_PAYLOAD(nlh));
handle_neigh_attrs(ndm, tb, nlh->nlmsg_type);
return 0;
}
示例12: read_reply
int read_reply()
{
//string to hold content of the route
// table (i.e. one entry)
char dsts[24], gws[24], ifs[16], ms[24];
// outer loop: loops thru all the NETLINK
// headers that also include the route entry
// header
nlp = (struct nlmsghdr *) buf;
for(; NLMSG_OK(nlp, nll); nlp = NLMSG_NEXT(nlp, nll))
{
// get route entry header
rtp = (struct rtmsg *) NLMSG_DATA(nlp);
// we are only concerned about the
// main route table
if(rtp->rtm_table != RT_TABLE_MAIN)
continue;
// init all the strings
bzero(dsts, sizeof(dsts));
bzero(gws, sizeof(gws));
bzero(ifs, sizeof(ifs));
bzero(ms, sizeof(ms));
// inner loop: loop thru all the attributes of
// one route entry
rtap = (struct rtattr *) RTM_RTA(rtp);
rtl = RTM_PAYLOAD(nlp);
for( ; RTA_OK(rtap, rtl); rtap = RTA_NEXT(rtap,rtl))
{
switch(rtap->rta_type)
{
// destination IPv4 address
case RTA_DST:
inet_ntop(AF_INET, RTA_DATA(rtap),
dsts, 24);
break;
// next hop IPv4 address
case RTA_GATEWAY:
inet_ntop(AF_INET, RTA_DATA(rtap),
gws, 24);
break;
// unique ID associated with the network
// interface
case RTA_OIF:
sprintf(ifs, "%d",
*((int *) RTA_DATA(rtap)));
default:
break;
}
}
sprintf(ms, "%d", rtp->rtm_dst_len);
test_msg("dst %s/%s gw %s if %s\n",
dsts, ms, gws, ifs);
}
return 0;
}
示例13: test_route
static void test_route(void) {
_cleanup_rtnl_message_unref_ sd_rtnl_message *req;
struct in_addr addr;
uint32_t index = 2;
uint16_t type;
void *data;
uint32_t u32_data;
int r;
struct rtmsg *rtm;
r = sd_rtnl_message_new_route(NULL, &req, RTM_NEWROUTE, AF_INET);
if (r < 0) {
log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
return;
}
addr.s_addr = htonl(INADDR_LOOPBACK);
r = sd_rtnl_message_append_in_addr(req, RTA_GATEWAY, &addr);
if (r < 0) {
log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r));
return;
}
r = sd_rtnl_message_append_u32(req, RTA_OIF, index);
if (r < 0) {
log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
return;
}
assert_se(rtnl_message_seal(NULL, req) >= 0);
assert_se(sd_rtnl_message_read(req, &type, &data) > 0);
assert_se(type == RTA_GATEWAY);
assert_se(((struct in_addr *)data)->s_addr == addr.s_addr);
assert_se(sd_rtnl_message_read(req, &type, &data) > 0);
assert_se(type == RTA_OIF);
assert_se(*(uint32_t *) data == index);
rtm = NLMSG_DATA(req->hdr);
r = rtnl_message_parse(req,
&req->rta_offset_tb,
&req->rta_tb_size,
RTA_MAX,
RTM_RTA(rtm),
RTM_PAYLOAD(req->hdr));
assert_se(sd_rtnl_message_read_u32(req, RTA_GATEWAY, &u32_data) == 0);
assert_se(sd_rtnl_message_read_u32(req, RTA_OIF, &u32_data) == 0);
assert_se((req = sd_rtnl_message_unref(req)) == NULL);
}
示例14: switch
struct ip4_routing_table *parse_nlmsg(struct nlmsghdr *msg) {
char dst_temp[IP4_ALEN];
char gw_temp[IP4_ALEN];
uint32_t priority;
uint32_t interface;
struct ip4_routing_table *table_pointer = NULL;
switch (msg->nlmsg_type) {
case NLMSG_ERROR: {
struct nlmsgerr* errorMsg = (struct nlmsgerr*) NLMSG_DATA(msg);
PRINT_DEBUG("recvd NLMSG_ERROR error seq:%d code:%d...", msg->nlmsg_seq, errorMsg->error);
break;
}
case RTM_NEWROUTE: {
struct rtmsg* rtm = (struct rtmsg*) NLMSG_DATA(msg);
struct rtattr* rta = RTM_RTA(rtm);
int rtaLen = msg->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
if (rtm->rtm_type == RTN_UNICAST) // don't consider local, broadcast and unreachable routes
{
table_pointer = (struct ip4_routing_table*) secure_malloc(sizeof(struct ip4_routing_table));
for (; RTA_OK(rta, rtaLen); rta = RTA_NEXT(rta, rtaLen)) {
switch (rta->rta_type) {
case RTA_DST: //destination
table_pointer->mask = rtm->rtm_dst_len;
memcpy(dst_temp, RTA_DATA(rta), IP4_ALEN);
//PRINT_DEBUG("received RTA_DST");
PRINT_DEBUG("dst_str = %u.%u.%u.%u", dst_temp[0] & 0xFF, dst_temp[1] & 0xFF, dst_temp[2] & 0xFF, dst_temp[3] & 0xFF);
table_pointer->dst = IP4_ADR_P2H(dst_temp[0]&0xFF, dst_temp[1]&0xFF, dst_temp[2]&0xFF, dst_temp[3]&0xFF);
break;
case RTA_GATEWAY: //next hop
table_pointer->mask = rtm->rtm_dst_len;
memcpy(gw_temp, RTA_DATA(rta), IP4_ALEN);
//PRINT_DEBUG("received RTA_GATEWAY");
PRINT_DEBUG("gw_str = %u.%u.%u.%u", gw_temp[0] & 0xFF, gw_temp[1] & 0xFF, gw_temp[2] & 0xFF, gw_temp[3] & 0xFF);
table_pointer->gw = IP4_ADR_P2H(gw_temp[0]&0xFF, gw_temp[1]&0xFF, gw_temp[2]&0xFF, gw_temp[3]&0xFF);
break;
case RTA_OIF: //interface
memcpy(&table_pointer->interface, RTA_DATA(rta), sizeof(interface)); //TODO won't work with current hack
PRINT_DEBUG("interface:%u", table_pointer->interface);
break;
case RTA_PRIORITY: //metric
memcpy(&table_pointer->metric, RTA_DATA(rta), sizeof(priority));
PRINT_DEBUG("metric:%u", table_pointer->metric);
break;
} //switch(rta->)
} // for()
} // if RTN_UNICAST
return (table_pointer);
}
} //switch (msg->nlmsg_type)
return (NULL);
}
示例15: parseRoutes
/* For parsing the route info returned */
static int parseRoutes(const char* ifName, struct nlmsghdr *nlHdr, struct route_info *rtInfo)
{
struct rtmsg *rtMsg;
struct rtattr *rtAttr;
int rtLen;
int i_ret = ZQERROR;
rtMsg = (struct rtmsg *)NLMSG_DATA(nlHdr);
/* If the route is not for AF_INET or does not belong to main routing table
then return. */
if((rtMsg->rtm_family != AF_INET) || (rtMsg->rtm_table != RT_TABLE_MAIN))
{
///NOTE , free must be called , otherwise memory leak will occur
goto ExitPoint;
}
/* get the rtattr field */
rtAttr = (struct rtattr *)RTM_RTA(rtMsg);
rtLen = RTM_PAYLOAD(nlHdr);
for(;RTA_OK(rtAttr,rtLen);rtAttr = RTA_NEXT(rtAttr,rtLen))
{
switch(rtAttr->rta_type)
{
case RTA_OIF:
if_indextoname(*(int *)RTA_DATA(rtAttr), rtInfo->ifName);
break;
case RTA_GATEWAY:
rtInfo->gateWay = *(u_int *)RTA_DATA(rtAttr);
break;
case RTA_PREFSRC:
rtInfo->srcAddr = *(u_int *)RTA_DATA(rtAttr);
break;
case RTA_DST:
rtInfo->dstAddr = *(u_int *)RTA_DATA(rtAttr);
break;
}
}
struct in_addr* p_dstAddr = (struct in_addr*)&rtInfo->dstAddr;
struct in_addr* p_gateWay = (struct in_addr*)&rtInfo->gateWay;
if ( !strcmp( ifName, rtInfo->ifName ) &&
strstr((char *)inet_ntoa(*p_dstAddr), "0.0.0.0"))
{
sprintf(gateway, (char *)inet_ntoa(*p_gateWay));
i_ret = ZQSUCCESS;
}
ExitPoint:
return i_ret;
}