本文整理汇总了C++中RTA_OK函数的典型用法代码示例。如果您正苦于以下问题:C++ RTA_OK函数的具体用法?C++ RTA_OK怎么用?C++ RTA_OK使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RTA_OK函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: rtnl_print_route
static void rtnl_print_route(struct nlmsghdr *hdr)
{
struct rtmsg *rtm = NLMSG_DATA(hdr);
uint32_t attrs_len = RTM_PAYLOAD(hdr);
struct rtattr *attr = RTM_RTA(rtm);
struct rta_cacheinfo *ci;
int hz = get_user_hz();
char addr_str[256];
char flags[256];
tprintf(" [ Route Family %d (%s%s%s)", rtm->rtm_family,
colorize_start(bold),
addr_family2str(rtm->rtm_family),
colorize_end());
tprintf(", Dst Len %d", rtm->rtm_dst_len);
tprintf(", Src Len %d", rtm->rtm_src_len);
tprintf(", ToS %d", rtm->rtm_tos);
tprintf(", Table %d (%s%s%s)", rtm->rtm_table,
colorize_start(bold),
route_table2str(rtm->rtm_table),
colorize_end());
tprintf(", Proto %d (%s%s%s)", rtm->rtm_protocol,
colorize_start(bold),
route_proto2str(rtm->rtm_protocol),
colorize_end());
tprintf(", Scope %d (%s%s%s)", rtm->rtm_scope,
colorize_start(bold),
scope2str(rtm->rtm_scope),
colorize_end());
tprintf(", Type %d (%s%s%s)", rtm->rtm_type,
colorize_start(bold),
route_type2str(rtm->rtm_type),
colorize_end());
tprintf(", Flags 0x%x (%s%s%s) ]\n", rtm->rtm_flags,
colorize_start(bold),
flags2str(route_flags, rtm->rtm_flags, flags,
sizeof(flags)),
colorize_end());
for (; RTA_OK(attr, attrs_len); attr = RTA_NEXT(attr, attrs_len)) {
switch (attr->rta_type) {
case RTA_DST:
attr_fmt(attr, "Dst %s", addr2str(rtm->rtm_family,
RTA_DATA(attr), addr_str, sizeof(addr_str)));
break;
case RTA_SRC:
attr_fmt(attr, "Src %s", addr2str(rtm->rtm_family,
RTA_DATA(attr), addr_str, sizeof(addr_str)));
break;
case RTA_IIF:
attr_fmt(attr, "Iif %d", RTA_INT(attr));
break;
case RTA_OIF:
attr_fmt(attr, "Oif %d", RTA_INT(attr));
break;
case RTA_GATEWAY:
attr_fmt(attr, "Gateway %s", addr2str(rtm->rtm_family,
RTA_DATA(attr), addr_str, sizeof(addr_str)));
break;
case RTA_PRIORITY:
attr_fmt(attr, "Priority %u", RTA_UINT32(attr));
break;
case RTA_PREFSRC:
attr_fmt(attr, "Pref Src %s", addr2str(rtm->rtm_family,
RTA_DATA(attr), addr_str, sizeof(addr_str)));
break;
case RTA_MARK:
attr_fmt(attr, "Mark 0x%x", RTA_UINT(attr));
break;
case RTA_FLOW:
attr_fmt(attr, "Flow 0x%x", RTA_UINT(attr));
break;
case RTA_TABLE:
attr_fmt(attr, "Table %d (%s%s%s)", RTA_UINT32(attr),
colorize_start(bold),
route_table2str(RTA_UINT32(attr)),
colorize_end());
break;
case RTA_CACHEINFO:
ci = RTA_DATA(attr);
tprintf("\tA: Cache (");
tprintf("expires(%ds)", ci->rta_expires / hz);
tprintf(", error(%d)", ci->rta_error);
tprintf(", users(%d)", ci->rta_clntref);
tprintf(", used(%d)", ci->rta_used);
tprintf(", last use(%ds)", ci->rta_lastuse / hz);
tprintf(", id(%d)", ci->rta_id);
tprintf(", ts(%d)", ci->rta_ts);
tprintf(", ts age(%ds))", ci->rta_tsage);
tprintf(", Len %lu\n", RTA_LEN(attr));
break;
}
}
}
示例2: get_bind_addr
//.........这里部分代码省略.........
case S5ATIPV6:
return -1;
case S5ATFQDN:
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
memcpy(host, &dest->fqdn, dest->len_fqdn);
host[dest->len_fqdn] = '\0';
error = getaddrinfo(host, NULL, &hints, &res0);
if (error) {
return -1;
}
for (res = res0; res; res = res->ai_next) {
if (res->ai_family != AF_INET)
continue;
sin = (struct sockaddr_in *)res->ai_addr;
memcpy(&ia, &(sin->sin_addr), sizeof(ia));
found++; break;
}
freeaddrinfo(res0);
if (!found)
return -1;
break;
default:
return -1;
}
memset(&rreq, 0, sizeof(rreq));
rreq.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
rreq.n.nlmsg_flags = NLM_F_REQUEST;
rreq.n.nlmsg_type = RTM_GETROUTE;
rreq.r.rtm_family = AF_INET;
len = RTA_LENGTH(4);
if (NLMSG_ALIGN(rreq.n.nlmsg_len) + len > sizeof(rreq))
return(-1);
rta = (struct rtattr*)((char *)&rreq.n + NLMSG_ALIGN(rreq.n.nlmsg_len));
rta->rta_type = RTA_DST;
rta->rta_len = len;
memcpy(RTA_DATA(rta), &ia, 4);
rreq.n.nlmsg_len = NLMSG_ALIGN(rreq.n.nlmsg_len) + len;
rreq.r.rtm_dst_len = 32; /* 32 bit */
s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
nladdr.nl_pid = 0;
nladdr.nl_groups = 0;
rreq.n.nlmsg_seq = seq = 9999;
iov.iov_base = (void*)&rreq.n;
iov.iov_len = rreq.n.nlmsg_len;
status = sendmsg(s, &msg, 0);
if (status < 0) {
/* perror("Cannot talk to rtnetlink"); */
return -1;
}
pid = getpid();
iov.iov_base = buf;
do {
iov.iov_len = sizeof(buf);
status = recvmsg(s, &msg, 0);
h = (struct nlmsghdr*)buf;
} while (h->nlmsg_pid != pid || h->nlmsg_seq != seq);
close(s);
/*
msg_out(norm,"nlmsg_pid: %d, nlmsg_seq: %d",
h->nlmsg_pid, h->nlmsg_seq);
*/
len = h->nlmsg_len;
r = NLMSG_DATA(buf);
rta = RTM_RTA(r);
while (RTA_OK(rta, len)) {
if (rta->rta_type <= RTA_MAX)
tb[rta->rta_type] = rta;
rta = RTA_NEXT(rta,len);
}
/*
if (tb[RTA_DST]) {
inet_ntop(AF_INET, RTA_DATA(tb[RTA_DST]), str, sizeof(str));
msg_out(norm, "DST %s", str);
}
if (tb[RTA_GATEWAY]) {
inet_ntop(AF_INET, RTA_DATA(tb[RTA_GATEWAY]), str, sizeof(str));
msg_out(norm, "GW %s", str);
}
*/
if (tb[RTA_OIF]) {
d = RTA_DATA(tb[RTA_OIF]);
return(get_ifconf(*d, ba));
}
return(-1);
}
示例3: process_nlmsg
static int process_nlmsg(struct nlmsghdr *n) {
assert(n);
if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
/* A link appeared or was removed */
struct ifinfomsg *ifi;
ifi = NLMSG_DATA(n);
if (ifi->ifi_family != AF_UNSPEC || (int) ifi->ifi_index != ifindex)
return 0;
if (n->nlmsg_type == RTM_DELLINK) {
daemon_log(LOG_ERR, "Interface vanished.");
return -1;
}
assert(n->nlmsg_type == RTM_NEWLINK);
if ((ifi->ifi_flags & IFF_LOOPBACK) ||
(ifi->ifi_flags & IFF_NOARP) ||
ifi->ifi_type != ARPHRD_ETHER) {
daemon_log(LOG_ERR, "Interface not suitable.");
return -1;
}
} else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
/* An address was added or removed */
struct rtattr *a = NULL;
struct ifaddrmsg *ifa;
int l;
uint32_t address = 0;
Address *i;
ifa = NLMSG_DATA(n);
if (ifa->ifa_family != AF_INET || (int) ifa->ifa_index != ifindex)
return 0;
l = NLMSG_PAYLOAD(n, sizeof(*ifa));
a = IFLA_RTA(ifa);
while(RTA_OK(a, l)) {
switch(a->rta_type) {
case IFA_LOCAL:
case IFA_ADDRESS:
assert(RTA_PAYLOAD(a) == 4);
memcpy(&address, RTA_DATA(a), sizeof(uint32_t));
break;
}
a = RTA_NEXT(a, l);
}
if (!address || is_ll_address(address))
return 0;
for (i = addresses; i; i = i->addresses_next)
if (i->address == address)
break;
if (n->nlmsg_type == RTM_DELADDR && i) {
AVAHI_LLIST_REMOVE(Address, addresses, addresses, i);
avahi_free(i);
} if (n->nlmsg_type == RTM_NEWADDR && !i) {
i = avahi_new(Address, 1);
i->address = address;
AVAHI_LLIST_PREPEND(Address, addresses, addresses, i);
}
}
return 0;
}
示例4: interpretAddr
static void interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_links, struct ifaddrs **p_resultList)
{
struct ifaddrmsg *l_info = (struct ifaddrmsg *)NLMSG_DATA(p_hdr);
size_t l_nameSize = 0;
size_t l_addrSize = 0;
int l_addedNetmask = 0;
size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
struct rtattr *l_rta;
for(l_rta = (struct rtattr *)(((char *)l_info) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
if(l_info->ifa_family == AF_PACKET)
{
continue;
}
switch(l_rta->rta_type)
{
case IFA_ADDRESS:
case IFA_LOCAL:
if((l_info->ifa_family == AF_INET || l_info->ifa_family == AF_INET6) && !l_addedNetmask)
{ // make room for netmask
l_addrSize += NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
l_addedNetmask = 1;
}
case IFA_BROADCAST:
l_addrSize += NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
break;
case IFA_LABEL:
l_nameSize += NLMSG_ALIGN(l_rtaSize + 1);
break;
default:
break;
}
}
struct ifaddrs *l_entry = malloc(sizeof(struct ifaddrs) + l_nameSize + l_addrSize);
memset(l_entry, 0, sizeof(struct ifaddrs));
l_entry->ifa_name = p_links[l_info->ifa_index - 1]->ifa_name;
char *l_name = ((char *)l_entry) + sizeof(struct ifaddrs);
char *l_addr = l_name + l_nameSize;
l_entry->ifa_flags = l_info->ifa_flags | p_links[l_info->ifa_index - 1]->ifa_flags;
l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
for(l_rta = (struct rtattr *)(((char *)l_info) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
void *l_rtaData = RTA_DATA(l_rta);
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
switch(l_rta->rta_type)
{
case IFA_ADDRESS:
case IFA_BROADCAST:
case IFA_LOCAL:
{
size_t l_addrLen = calcAddrLen(l_info->ifa_family, l_rtaDataSize);
makeSockaddr(l_info->ifa_family, (struct sockaddr *)l_addr, l_rtaData, l_rtaDataSize);
if(l_info->ifa_family == AF_INET6)
{
if(IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)l_rtaData) || IN6_IS_ADDR_MC_LINKLOCAL((struct in6_addr *)l_rtaData))
{
((struct sockaddr_in6 *)l_addr)->sin6_scope_id = l_info->ifa_index;
}
}
if(l_rta->rta_type == IFA_ADDRESS)
{ // apparently in a point-to-point network IFA_ADDRESS contains the dest address and IFA_LOCAL contains the local address
if(l_entry->ifa_addr)
{
l_entry->ifa_dstaddr = (struct sockaddr *)l_addr;
}
else
{
l_entry->ifa_addr = (struct sockaddr *)l_addr;
}
}
else if(l_rta->rta_type == IFA_LOCAL)
{
if(l_entry->ifa_addr)
{
l_entry->ifa_dstaddr = l_entry->ifa_addr;
}
l_entry->ifa_addr = (struct sockaddr *)l_addr;
}
else
{
l_entry->ifa_broadaddr = (struct sockaddr *)l_addr;
}
l_addr += NLMSG_ALIGN(l_addrLen);
break;
}
case IFA_LABEL:
strncpy(l_name, l_rtaData, l_rtaDataSize);
l_name[l_rtaDataSize] = '\0';
l_entry->ifa_name = l_name;
break;
//.........这里部分代码省略.........
示例5: wlanifLinkEventsHandleNewLink
/**
* @brief Handle an RTM_NEWLINK message from the driver, generating the
* appropriate events from it.
*
* @param [in] state the internal state for this instance
* @param [in] hdr the netlink message header; the length field has
* already been validated
* @param [in] payloadLen the length of the payload (not including the
* netlink header)
*/
static void wlanifLinkEventsHandleNewLink(wlanifLinkEventsHandle_t state,
const struct nlmsghdr *hdr,
u_int32_t payloadLen) {
const struct ifinfomsg *ifaceInfo = NLMSG_DATA(hdr);
size_t ifaceLen = NLMSG_ALIGN(sizeof(*ifaceInfo));
if (payloadLen < ifaceLen) {
dbgf(state->dbgModule, DBGERR, "%s: Malformed netlink payload "
"length %u", __func__, payloadLen);
return;
}
wlanif_band_e band =
wlanifBSteerControlResolveBandFromSystemIndex(state->bsteerControlHandle,
ifaceInfo->ifi_index);
if (wlanif_band_invalid == band) {
dbgf(state->dbgModule, DBGDUMP,
"%s: Received message from ifindex %u not managed by lbd",
__func__, ifaceInfo->ifi_index);
return;
}
const struct rtattr *attr = IFLA_RTA(ifaceInfo);
const size_t RTATTR_LEN = RTA_ALIGN(sizeof(*attr));
// This will keep track of the amount of data remaining in the payload
// for the RT attributes.
size_t attrLen = payloadLen - ifaceLen;
// Iterate over all of the RT attributes, looking for a wireless one
// and then dispatch to a separate function to parse the event.
while (RTA_OK(attr, attrLen)) {
const u_int8_t *data =
((const u_int8_t *) attr) + RTATTR_LEN;
switch (attr->rta_type) {
case IFLA_WIRELESS:
{
wlanifLinkEventsHandleIWEvent(state, data,
attr->rta_len - RTATTR_LEN,
band, ifaceInfo->ifi_index);
break;
}
case IFLA_OPERSTATE:
{
wlanifLinkEventsHandleOperState(state, data,
attr->rta_len - RTATTR_LEN,
band, ifaceInfo->ifi_index);
break;
}
default:
{
// Nop (other than a log)
dbgf(state->dbgModule, DBGDUMP,
"%s: Unhandled attribute: type=%04x len=%u",
__func__, attr->rta_type, attr->rta_len);
break;
}
}
attr = RTA_NEXT(attr, attrLen);
}
if (attrLen != 0) {
dbgf(state->dbgModule, DBGERR, "%s: Did not consume all attributes: %u bytes left",
__func__, attrLen);
}
}
示例6: if_nameindex_netlink
static struct if_nameindex *
if_nameindex_netlink (void)
{
struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
struct if_nameindex *idx = NULL;
if (__no_netlink_support || __netlink_open (&nh) < 0)
return NULL;
/* Tell the kernel that we wish to get a list of all
active interfaces. Collect all data for every interface. */
if (__netlink_request (&nh, RTM_GETLINK) < 0)
goto exit_free;
/* Count the interfaces. */
unsigned int nifs = 0;
for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next)
{
struct nlmsghdr *nlh;
size_t size = nlp->size;
if (nlp->nlh == NULL)
continue;
/* Walk through all entries we got from the kernel and look, which
message type they contain. */
for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
{
/* Check if the message is what we want. */
if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
continue;
if (nlh->nlmsg_type == NLMSG_DONE)
break; /* ok */
if (nlh->nlmsg_type == RTM_NEWLINK)
++nifs;
}
}
idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
if (idx == NULL)
{
nomem:
__set_errno (ENOBUFS);
goto exit_free;
}
/* Add the interfaces. */
nifs = 0;
for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next)
{
struct nlmsghdr *nlh;
size_t size = nlp->size;
if (nlp->nlh == NULL)
continue;
/* Walk through all entries we got from the kernel and look, which
message type they contain. */
for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
{
/* Check if the message is what we want. */
if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
continue;
if (nlh->nlmsg_type == NLMSG_DONE)
break; /* ok */
if (nlh->nlmsg_type == RTM_NEWLINK)
{
struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
struct rtattr *rta = IFLA_RTA (ifim);
size_t rtasize = IFLA_PAYLOAD (nlh);
idx[nifs].if_index = ifim->ifi_index;
while (RTA_OK (rta, rtasize))
{
char *rta_data = RTA_DATA (rta);
size_t rta_payload = RTA_PAYLOAD (rta);
if (rta->rta_type == IFLA_IFNAME)
{
idx[nifs].if_name = __strndup (rta_data, rta_payload);
if (idx[nifs].if_name == NULL)
{
idx[nifs].if_index = 0;
if_freenameindex (idx);
idx = NULL;
goto nomem;
}
break;
}
rta = RTA_NEXT (rta, rtasize);
}
++nifs;
//.........这里部分代码省略.........
示例7: reportRoutinTable
//.........这里部分代码省略.........
}
if(nbytes == 0)
printf("EOF in netlink\n");
nlp = (struct nlmsghdr*)(&reply_ptr[reply_len]);
if (nlp->nlmsg_type == NLMSG_DONE)
{
// All data has been received.
// Truncate the reply to exclude this message,
// i.e. do not increase reply_len.
break;
}
if (nlp->nlmsg_type == NLMSG_ERROR)
{
printf("Error in msg\n");
exit(1);
}
reply_len += nbytes;
counter -= nbytes;
}
/*======================================================*/
bufsize = reply_len;
// string to hold content of the route
// table (i.e. one entry)
// unsigned int flags;
// outer loop: loops thru all the NETLINK
// headers that also include the route entry
// header
nlp = (struct nlmsghdr *) buf;
for(i= -1; NLMSG_OK(nlp, bufsize); nlp=NLMSG_NEXT(nlp, bufsize))
{
// // get route entry header
rtp = (struct rtmsg *) NLMSG_DATA(nlp);
// we are only concerned about the
// tableId route table
if(rtp->rtm_table != 254)
continue;
i++;
// init all the strings
bzero(&route[i], sizeof(struct RouteInfo));
// flags = rtp->rtm_flags;
// route[i].proto = rtp->rtm_protocol;
// // 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:
// count = 32 - rtp->rtm_dst_len;
route[i].dstAddr = *(unsigned long *) RTA_DATA(rtap);
break;
case RTA_GATEWAY:
route[i].gateWay = *(unsigned long *) RTA_DATA(rtap);
//printf("gw:%s\t",inet_ntoa(route[i].gateWay));
break;
// // unique ID associated with the network
// // interface
case RTA_OIF:
ifname(*((int *) RTA_DATA(rtap)),route[i].ifName);
//printf( "ifname %s\n", route[i].ifName);
break;
default:
break;
}
}
}
for( j = 0; j<= i; j++)
{
if(strcmp(route[j].ifName, ifNameVar)==0)
{
char ipbuf[INET_ADDRSTRLEN];
char ipbuf2[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &route[j].dstAddr, ipbuf, INET_ADDRSTRLEN);
inet_ntop(AF_INET, &route[j].gateWay, ipbuf2, INET_ADDRSTRLEN);
sprintf(outputRouting, "%lu.%lu:%s:%s", (unsigned long) tv.tv_sec,(unsigned long) tv.tv_nsec, ipbuf ,ipbuf2);
fprintf(fpRouting, "%s\n" ,outputRouting);
}
}
fflush(fpRouting);
// close(route_sock);
return 0;
}
示例8: netlink_callback
static void netlink_callback(AvahiNetlink *nl, struct nlmsghdr *n, void* userdata) {
AvahiInterfaceMonitor *m = userdata;
/* This routine is called for every RTNETLINK response packet */
assert(m);
assert(n);
assert(m->osdep.netlink == nl);
if (n->nlmsg_type == RTM_NEWLINK) {
/* A new interface appeared or an existing one has been modified */
struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
AvahiHwInterface *hw;
struct rtattr *a = NULL;
size_t l;
/* A (superfluous?) sanity check */
if (ifinfomsg->ifi_family != AF_UNSPEC)
return;
/* Check whether there already is an AvahiHwInterface object
* for this link, so that we can update its data. Note that
* Netlink sends us an RTM_NEWLINK not only when a new
* interface appears, but when it changes, too */
if (!(hw = avahi_interface_monitor_get_hw_interface(m, ifinfomsg->ifi_index)))
/* No object found, so let's create a new
* one. avahi_hw_interface_new() will call
* avahi_interface_new() internally twice for IPv4 and
* IPv6, so there is no need for us to do that
* ourselves */
if (!(hw = avahi_hw_interface_new(m, (AvahiIfIndex) ifinfomsg->ifi_index)))
return; /* OOM */
/* Check whether the flags of this interface are OK for us */
hw->flags_ok =
(ifinfomsg->ifi_flags & IFF_UP) &&
(!m->server->config.use_iff_running || (ifinfomsg->ifi_flags & IFF_RUNNING)) &&
!(ifinfomsg->ifi_flags & IFF_LOOPBACK) &&
(ifinfomsg->ifi_flags & IFF_MULTICAST) &&
(m->server->config.allow_point_to_point || !(ifinfomsg->ifi_flags & IFF_POINTOPOINT));
/* Handle interface attributes */
l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
a = IFLA_RTA(ifinfomsg);
while (RTA_OK(a, l)) {
switch(a->rta_type) {
case IFLA_IFNAME:
/* Fill in interface name */
avahi_free(hw->name);
hw->name = avahi_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
break;
case IFLA_MTU:
/* Fill in MTU */
assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
hw->mtu = *((unsigned int*) RTA_DATA(a));
break;
case IFLA_ADDRESS:
/* Fill in hardware (MAC) address */
hw->mac_address_size = RTA_PAYLOAD(a);
if (hw->mac_address_size > AVAHI_MAC_ADDRESS_MAX)
hw->mac_address_size = AVAHI_MAC_ADDRESS_MAX;
memcpy(hw->mac_address, RTA_DATA(a), hw->mac_address_size);
break;
default:
;
}
a = RTA_NEXT(a, l);
}
/* Check whether this interface is now "relevant" for us. If
* it is Avahi will start to announce its records on this
* interface and send out queries for subscribed records on
* it */
avahi_hw_interface_check_relevant(hw, AVAHI_MDNS);
avahi_hw_interface_check_relevant(hw, AVAHI_LLMNR);
/* Update any associated RRs of this interface. (i.e. the
* _workstation._tcp record containing the MAC address) */
avahi_hw_interface_update_rrs(hw, 0);
} else if (n->nlmsg_type == RTM_DELLINK) {
/* An interface has been removed */
struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
AvahiHwInterface *hw;
//.........这里部分代码省略.........
示例9: get_if_prefix
static void
get_if_prefix(struct nlmsghdr *nlm, int nlm_len, int request,
struct dhcp6_if *ifp)
{
struct rtmsg *rtm = (struct rtmsg *)NLMSG_DATA(nlm);
struct rtattr *rta;
size_t rtasize, rtapayload;
void *rtadata;
struct ra_info *rainfo;
char addr[64];
if (rtm->rtm_family != AF_INET6 || nlm->nlmsg_type != request)
return;
if (!(rtm->rtm_flags & RTM_F_PREFIX))
return;
rtasize = NLMSG_PAYLOAD(nlm, nlm_len) - NLMSG_ALIGN(sizeof(*rtm));
rta = (struct rtattr *) (((char *) NLMSG_DATA(nlm)) +
NLMSG_ALIGN(sizeof(*rtm)));
if (!RTA_OK(rta, rtasize))
return;
rtadata = RTA_DATA(rta);
rtapayload = RTA_PAYLOAD(rta);
switch(rta->rta_type) {
case RTA_OIF:
break;
default:
break;
}
switch (rta->rta_type) {
case RTA_DST:
rainfo = (struct ra_info *)malloc(sizeof(*rainfo));
if (rainfo == NULL)
return;
memset(rainfo, 0, sizeof(rainfo));
memcpy(&(rainfo->prefix), (struct in6_addr *)rtadata,
sizeof(struct in6_addr));
rainfo->plen = rtm->rtm_dst_len;
if (ifp->ralist == NULL) {
ifp->ralist = rainfo;
rainfo->next = NULL;
} else {
struct ra_info *ra, *ra_prev;
ra_prev = ifp->ralist;
for (ra = ifp->ralist; ra; ra = ra->next) {
if (rainfo->plen >= ra->plen) {
if (ra_prev == ra) {
ifp->ralist = rainfo;
rainfo->next = ra;
} else {
ra_prev->next = rainfo;
rainfo->next = ra;
}
break;
} else {
if (ra->next == NULL) {
ra->next = rainfo;
rainfo->next = NULL;
break;
} else {
ra_prev = ra;
continue;
}
}
}
}
inet_ntop(AF_INET6, &(rainfo->prefix), addr, INET6_ADDRSTRLEN);
dprintf(LOG_DEBUG, "get prefix address %s", addr);
dprintf(LOG_DEBUG, "get prefix plen %d",rtm->rtm_dst_len);
break;
case RTA_CACHEINFO:
dprintf(LOG_DEBUG, "prefix route life time is %d\n",
((struct rta_cacheinfo *)rtadata)->rta_expires);
break;
default:
break;
}
return;
}
示例10: process_nl_new_route
void process_nl_new_route (struct nlmsghdr *nlh)
{
struct rtmsg *rtm = NULL;
struct rtattr *rt_attr = NULL;
int rt_length = 0;
lispd_iface_elt *iface = NULL;
int iface_index = 0;
char iface_name[IF_NAMESIZE];
lisp_addr_t gateway = {.afi=AF_UNSPEC};
lisp_addr_t dst = {.afi=AF_UNSPEC};;
rtm = (struct rtmsg *) NLMSG_DATA (nlh);
if ((rtm->rtm_family != AF_INET) && (rtm->rtm_family != AF_INET6)) {
lispd_log_msg(LISP_LOG_DEBUG_2,"process_nl_new_route: Unknown adddress family");
return;
}
if (rtm->rtm_table != RT_TABLE_MAIN) {
/* Not interested in routes/gateways affecting tables other the main routing table */
return;
}
rt_attr = (struct rtattr *)RTM_RTA(rtm);
rt_length = RTM_PAYLOAD(nlh);
for (; RTA_OK(rt_attr, rt_length); rt_attr = RTA_NEXT(rt_attr, rt_length)) {
switch (rt_attr->rta_type) {
case RTA_OIF:
iface_index = *(int *)RTA_DATA(rt_attr);
iface = get_interface_from_index(iface_index);
if_indextoname(iface_index, iface_name);
if (iface == NULL) {
lispd_log_msg(LISP_LOG_DEBUG_2, "process_nl_new_route: the netlink message is not for any interface associated with RLOCs (%s)",
iface_name);
return;
}
break;
case RTA_GATEWAY:
gateway.afi = rtm->rtm_family;
switch (gateway.afi) {
case AF_INET:
memcpy(&(gateway.address),(struct in_addr *)RTA_DATA(rt_attr), sizeof(struct in_addr));
break;
case AF_INET6:
memcpy(&(gateway.address),(struct in6_addr *)RTA_DATA(rt_attr), sizeof(struct in6_addr));
break;
default:
break;
}
break;
case RTA_DST: // We check if the new route message contains a destintaion. If it is, then the gateway address is not a default route. Discard it
dst.afi = rtm->rtm_family;
switch (dst.afi) {
case AF_INET:
memcpy(&(dst.address),(struct in_addr *)RTA_DATA(rt_attr), sizeof(struct in_addr));
break;
case AF_INET6:
memcpy(&(dst.address),(struct in6_addr *)RTA_DATA(rt_attr), sizeof(struct in6_addr));
break;
default:
break;
}
break;
default:
break;
}
}
if (gateway.afi != AF_UNSPEC && iface_index != 0 && dst.afi == AF_UNSPEC) {
/* Check default afi*/
if (default_rloc_afi != AF_UNSPEC && default_rloc_afi != gateway.afi) {
lispd_log_msg(LISP_LOG_DEBUG_1, "process_nl_new_route: Default RLOC afi defined (-a #): Skipped %s gateway in iface %s",
(gateway.afi == AF_INET) ? "IPv4" : "IPv6",iface->iface_name);
return;
}
/* Check if the addres is a global address*/
if (is_link_local_addr(gateway) == TRUE) {
lispd_log_msg(LISP_LOG_DEBUG_2,"process_nl_new_route: the extractet address from the netlink "
"messages is a local link address: %s discarded", get_char_from_lisp_addr_t(gateway));
return;
}
/* Process the new gateway */
lispd_log_msg(LISP_LOG_DEBUG_1, "process_nl_new_route: Process new gateway associated to the interface %s: %s",
iface_name, get_char_from_lisp_addr_t(gateway));
process_new_gateway(gateway,iface);
}
}
示例11: check_existence_through_netlink
static NOINLINE int check_existence_through_netlink(void)
{
int iface_len;
/* Buffer was 1K, but on linux-3.9.9 it was reported to be too small.
* netlink.h: "limit to 8K to avoid MSG_TRUNC when PAGE_SIZE is very large".
* Note: on error returns (-1) we exit, no need to free replybuf.
*/
enum { BUF_SIZE = 8 * 1024 };
char *replybuf = xmalloc(BUF_SIZE);
iface_len = strlen(G.iface);
while (1) {
struct nlmsghdr *mhdr;
ssize_t bytes;
bytes = recv(netlink_fd, replybuf, BUF_SIZE, MSG_DONTWAIT);
if (bytes < 0) {
if (errno == EAGAIN)
goto ret;
if (errno == EINTR)
continue;
bb_perror_msg("netlink: recv");
return -1;
}
mhdr = (struct nlmsghdr*)replybuf;
while (bytes > 0) {
if (!NLMSG_OK(mhdr, bytes)) {
bb_error_msg("netlink packet too small or truncated");
return -1;
}
if (mhdr->nlmsg_type == RTM_NEWLINK || mhdr->nlmsg_type == RTM_DELLINK) {
struct rtattr *attr;
int attr_len;
if (mhdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg))) {
bb_error_msg("netlink packet too small or truncated");
return -1;
}
attr = IFLA_RTA(NLMSG_DATA(mhdr));
attr_len = IFLA_PAYLOAD(mhdr);
while (RTA_OK(attr, attr_len)) {
if (attr->rta_type == IFLA_IFNAME) {
int len = RTA_PAYLOAD(attr);
if (len > IFNAMSIZ)
len = IFNAMSIZ;
if (iface_len <= len
&& strncmp(G.iface, RTA_DATA(attr), len) == 0
) {
G.iface_exists = (mhdr->nlmsg_type == RTM_NEWLINK);
}
}
attr = RTA_NEXT(attr, attr_len);
}
}
mhdr = NLMSG_NEXT(mhdr, bytes);
}
}
ret:
free(replybuf);
return G.iface_exists;
}
示例12: rtnetlink_rcv_msg
static __inline__ int
rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
{
struct rtnetlink_link *link;
struct rtnetlink_link *link_tab;
struct rtattr *rta[RTATTR_MAX];
int exclusive = 0;
int sz_idx, kind;
int min_len;
int family;
int type;
int err;
/* Only requests are handled by kernel now */
if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
return 0;
type = nlh->nlmsg_type;
/* A control message: ignore them */
if (type < RTM_BASE)
return 0;
/* Unknown message: reply with EINVAL */
if (type > RTM_MAX)
goto err_inval;
type -= RTM_BASE;
/* All the messages must have at least 1 byte length */
if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
return 0;
family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
if (family > NPROTO) {
*errp = -EAFNOSUPPORT;
return -1;
}
link_tab = rtnetlink_links[family];
if (link_tab == NULL)
link_tab = rtnetlink_links[PF_UNSPEC];
link = &link_tab[type];
sz_idx = type>>2;
kind = type&3;
if (kind != 2 && !cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN)) {
*errp = -EPERM;
return -1;
}
if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
u32 rlen;
if (link->dumpit == NULL)
link = &(rtnetlink_links[PF_UNSPEC][type]);
if (link->dumpit == NULL)
goto err_inval;
if ((*errp = netlink_dump_start(rtnl, skb, nlh,
link->dumpit,
rtnetlink_done)) != 0) {
return -1;
}
rlen = NLMSG_ALIGN(nlh->nlmsg_len);
if (rlen > skb->len)
rlen = skb->len;
skb_pull(skb, rlen);
return -1;
}
if (kind != 2) {
if (rtnl_exlock_nowait()) {
*errp = 0;
return -1;
}
exclusive = 1;
}
memset(&rta, 0, sizeof(rta));
min_len = rtm_min[sz_idx];
if (nlh->nlmsg_len < min_len)
goto err_inval;
if (nlh->nlmsg_len > min_len) {
int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
while (RTA_OK(attr, attrlen)) {
unsigned flavor = attr->rta_type;
if (flavor) {
if (flavor > rta_max[sz_idx])
goto err_inval;
rta[flavor-1] = attr;
}
attr = RTA_NEXT(attr, attrlen);
//.........这里部分代码省略.........
示例13: rtnl_print_neigh
static void rtnl_print_neigh(struct nlmsghdr *hdr)
{
struct ndmsg *ndm = NLMSG_DATA(hdr);
uint32_t attrs_len = NDA_PAYLOAD(hdr);
struct rtattr *attr = NDA_RTA(ndm);
struct nda_cacheinfo *ci;
int hz = get_user_hz();
char addr_str[256];
char hw_addr[30];
char states[256];
char flags[256];
tprintf(" [ Neigh Family %d (%s%s%s)", ndm->ndm_family,
colorize_start(bold),
addr_family2str(ndm->ndm_family),
colorize_end());
tprintf(", Link Index %d", ndm->ndm_ifindex);
tprintf(", State %d (%s%s%s)", ndm->ndm_state,
colorize_start(bold),
flags2str(neigh_states, ndm->ndm_state, states,
sizeof(states)),
colorize_end());
tprintf(", Flags %d (%s%s%s)", ndm->ndm_flags,
colorize_start(bold),
flags2str(neigh_flags, ndm->ndm_flags, flags,
sizeof(flags)),
colorize_end());
tprintf(", Type %d (%s%s%s)", ndm->ndm_type,
colorize_start(bold),
route_type2str(ndm->ndm_type),
colorize_end());
tprintf(" ]\n");
for (; RTA_OK(attr, attrs_len); attr = RTA_NEXT(attr, attrs_len)) {
switch (attr->rta_type) {
case NDA_DST:
attr_fmt(attr, "Address %s", addr2str(ndm->ndm_family,
RTA_DATA(attr), addr_str,
sizeof(addr_str)));
break;
case NDA_LLADDR:
attr_fmt(attr, "HW Address %s",
device_addr2str(RTA_DATA(attr),
RTA_LEN(attr), 0, hw_addr,
sizeof(hw_addr)));
break;
case NDA_PROBES:
attr_fmt(attr, "Probes %d", RTA_UINT32(attr));
break;
case NDA_CACHEINFO:
ci = RTA_DATA(attr);
tprintf("\tA: Cache (");
tprintf("confirmed(%ds)", ci->ndm_confirmed / hz);
tprintf(", used(%ds)", ci->ndm_used / hz);
tprintf(", updated(%ds)", ci->ndm_updated / hz);
tprintf(", refcnt(%d))", ci->ndm_refcnt);
tprintf(", Len %lu\n", RTA_LEN(attr));
break;
}
}
}
示例14: drv_iwevt_handler
/**
* @brief This function parses for IWEVENT events
*
* @param h Pointer to Netlink message header
* @param left Number of bytes to be read
* @param evt_conn A pointer to a output buffer. It sets TRUE when it gets
* the event CUS_EVT_OBSS_SCAN_PARAM, otherwise FALSE
* @return Number of bytes left to be read
*/
static int
drv_iwevt_handler(struct nlmsghdr *h, int left, int *evt_conn)
{
int len, plen, attrlen, nlmsg_len, rta_len;
struct ifinfomsg *ifi;
struct rtattr *attr;
*evt_conn = FALSE;
while (left >= sizeof(*h)) {
len = h->nlmsg_len;
plen = len - sizeof(*h);
if (len > left || plen < 0) {
/* malformed netlink message */
break;
}
if (plen < sizeof(*ifi)) {
break;
}
ifi = NLMSG_DATA(h);
nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
attrlen = h->nlmsg_len - nlmsg_len;
if (attrlen < 0) {
break;
}
attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
rta_len = RTA_ALIGN(sizeof(struct rtattr));
while (RTA_OK(attr, attrlen)) {
if (attr->rta_type == IFLA_WIRELESS) {
struct iw_event *iwe;
char *pos = ((char *) attr) + rta_len;
char *end = pos + (attr->rta_len - rta_len);
unsigned short dlen;
while (pos + IW_EV_LCP_LEN <= end) {
iwe = (struct iw_event *) pos;
if (iwe->len <= IW_EV_LCP_LEN)
break;
switch (iwe->cmd) {
case SIOCGIWAP:
{
struct ether_addr *wap;
struct ether_addr etherzero =
{ {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
char buf[32];
wap = (struct ether_addr *)
((struct sockaddr *) (&iwe->u.ap_addr))->
sa_data;
if (!memcmp
(wap, ðerzero, sizeof(struct ether_addr))) {
printf("---< Disconnected from AP >---\n");
memset(&cur_obss_scan_param, 0,
sizeof(cur_obss_scan_param));
assoc_flag = FALSE;
is_ht_ap = FALSE;
} else {
memset(buf, 0, sizeof(buf));
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
wap->ether_addr_octet[0],
wap->ether_addr_octet[1],
wap->ether_addr_octet[2],
wap->ether_addr_octet[3],
wap->ether_addr_octet[4],
wap->ether_addr_octet[5]);
printf("---< Connected to AP: %s >---\n", buf);
/** set TRUE, if connected */
assoc_flag = TRUE;
}
pos += iwe->len;
}
break;
case IWEVCUSTOM:
{
char *custom;
custom = pos + IW_EV_POINT_LEN;
if (IW_EV_POINT_LEN ==
IW_EV_LCP_LEN + sizeof(struct iw_point)) {
dlen = iwe->u.data.length;
} else { /* WIRELESS_EXT >= 19 */
dlen =
*((unsigned short *) (pos + IW_EV_LCP_LEN));
}
if (custom + dlen > end)
break;
printf("---< %s >---\n", custom);
if (!strncmp
(CUS_EVT_OBSS_SCAN_PARAM, custom,
//.........这里部分代码省略.........
示例15: IFLA_RTA
rtnetlink::if_link& rtnetlink::if_link::operator=(message& msg)
{
static_cast<data&>(*this) = msg;
std::pair<const void*, size_t> pl = msg.payload();
if (msg.type() < 16 || msg.type() > 19 || pl.second < sizeof(::ifinfomsg))
throw "bad msg type";
const ::ifinfomsg* ifi = reinterpret_cast<const ifinfomsg*>(pl.first);
_type = ifi->ifi_type;
_index = ifi->ifi_index;
_flags = ifi->ifi_flags;
const ::rtattr* rta = IFLA_RTA(ifi);
size_t attrlen = IFLA_PAYLOAD(reinterpret_cast<const ::nlmsghdr*>(_msg->header()));
for (; RTA_OK(rta, attrlen); rta = RTA_NEXT(rta, attrlen)) {
switch (rta->rta_type) {
case IFLA_ADDRESS:
_address = attr<void>(RTA_DATA(rta), RTA_PAYLOAD(rta));
break;
case IFLA_BROADCAST:
_bcast_addr = attr<void>(RTA_DATA(rta), RTA_PAYLOAD(rta));
break;
case IFLA_IFNAME:
_name = attr<char>(RTA_DATA(rta), RTA_PAYLOAD(rta));
break;
case IFLA_MTU:
ODTONE_ASSERT(sizeof(odtone::uint) == RTA_PAYLOAD(rta));
_mtu = reinterpret_cast<odtone::uint*>(RTA_DATA(rta));
break;
case IFLA_LINK:
ODTONE_ASSERT(sizeof(odtone::sint) == RTA_PAYLOAD(rta));
_link_type = reinterpret_cast<odtone::sint*>(RTA_DATA(rta));
break;
case IFLA_QDISC:
_qdisc = attr<char>(RTA_DATA(rta), RTA_PAYLOAD(rta));
break;
case IFLA_STATS:
ODTONE_ASSERT(sizeof(::rtnl_link_stats) == RTA_PAYLOAD(rta));
_stats = RTA_DATA(rta);
break;
case IFLA_WIRELESS:
std::cout << "IFLA_WIRELESS(" << rta->rta_type << ", " << RTA_PAYLOAD(rta) << ")\n";
// std::cout << "\tcmd: "
// << reinterpret_cast<iw_event*>(RTA_DATA(rta))->cmd
// << " len: "
// << reinterpret_cast<iw_event*>(RTA_DATA(rta))->len
// << std::endl;
_wifi = attr<void>(RTA_DATA(rta), RTA_PAYLOAD(rta));
break;
default:
std::cout << "IFLA_UNSPEC(" << rta->rta_type << ", " << RTA_PAYLOAD(rta) << ")\n";
}
}
return *this;
}