其主要是用来配置网络的标准化接口. 文章Understanding And Programming With Netlink Sockets(http://people.redhat.com/nhorman/papers/netlink.pdf)作了基本介绍.
使用示例见: http://www.wlug.org.nz/LinuxNetlinkSocketExample.
我将其代码略微修改,以得到内核层link状态报告,
#include <sys/socket.h>
#include <unistd.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if.h>
#if 0
//#define MYPROTO NETLINK_ARPD
#define MYMGRP RTMGRP_NEIGH
// if you want the above you'll find that the kernel must be compiled with CONFIG_ARPD, and
// that you need MYPROTO=NETLINK_ROUTE, since the kernel arp code {re,ab}uses rtnl (NETLINK_ROUTE)
#else
#if 0
#define MYPROTO NETLINK_ROUTE
#define MYMGRP RTMGRP_IPV4_ROUTE
#else
#define MYPROTO NETLINK_ROUTE
#define MYMGRP RTNLGRP_LINK
#endif
#endif
struct msgnames_t {
int id;
char *msg;
} typenames[] = {
#define MSG(x) { x, #x }
MSG(RTM_NEWROUTE),
MSG(RTM_DELROUTE),
MSG(RTM_GETROUTE),
#undef MSG
{0,0}
};
char *lookup_name(struct msgnames_t *db,int id)
{
static char name[512];
struct msgnames_t *msgnamesiter;
for(msgnamesiter=db;msgnamesiter->msg;++msgnamesiter) {
if (msgnamesiter->id == id)
break;
}
if (msgnamesiter->msg) {
return msgnamesiter->msg;
}
snprintf(name,sizeof(name),"#%i",id);
return name;
}
int open_netlink()
{
int sock = socket(AF_NETLINK,SOCK_RAW,MYPROTO);
struct sockaddr_nl addr;
memset((void *)&addr, 0, sizeof(addr));
if (sock<0)
return sock;
addr.nl_family = AF_NETLINK;
addr.nl_pid = getpid();
addr.nl_groups = MYMGRP;
if (bind(sock,(struct sockaddr *)&addr,sizeof(addr))<0)
return -1;
return sock;
}
int read_event(int sock)
{
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)+sizeof(struct ifinfomsg);
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(sock, &msg, 0);
if (ret<0) {
return ret;
}
printf("Type: %i (%s)\n",(nlh.nlmsg_type),lookup_name(typenames,nlh.nlmsg_type));
printf("Flag:");
#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
printf("\n");
printf("Seq : %i\n",nlh.nlmsg_seq);
printf("Pid : %i\n",nlh.nlmsg_pid);
printf("\n");
{
struct ifinfomsg *ifi;
ifi= NLMSG_DATA((unsigned char *) &nlh);
if(ifi->ifi_flags & IFF_LOWER_UP)
printf("link up:[%x]\n",ifi->ifi_flags);
else
printf("link down:[%x]\n",ifi->ifi_flags);
}
return 0;
}
int main(int argc, char *argv[])
{
int nls = open_netlink();
if (nls<0) {
err(1,"netlink");
}
while (1)
read_event(nls);
return 0;
}
使用示例见: http://www.wlug.org.nz/LinuxNetlinkSocketExample.
我将其代码略微修改,以得到内核层link状态报告,
#include <sys/socket.h>
#include <unistd.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if.h>
#if 0
//#define MYPROTO NETLINK_ARPD
#define MYMGRP RTMGRP_NEIGH
// if you want the above you'll find that the kernel must be compiled with CONFIG_ARPD, and
// that you need MYPROTO=NETLINK_ROUTE, since the kernel arp code {re,ab}uses rtnl (NETLINK_ROUTE)
#else
#if 0
#define MYPROTO NETLINK_ROUTE
#define MYMGRP RTMGRP_IPV4_ROUTE
#else
#define MYPROTO NETLINK_ROUTE
#define MYMGRP RTNLGRP_LINK
#endif
#endif
struct msgnames_t {
int id;
char *msg;
} typenames[] = {
#define MSG(x) { x, #x }
MSG(RTM_NEWROUTE),
MSG(RTM_DELROUTE),
MSG(RTM_GETROUTE),
#undef MSG
{0,0}
};
char *lookup_name(struct msgnames_t *db,int id)
{
static char name[512];
struct msgnames_t *msgnamesiter;
for(msgnamesiter=db;msgnamesiter->msg;++msgnamesiter) {
if (msgnamesiter->id == id)
break;
}
if (msgnamesiter->msg) {
return msgnamesiter->msg;
}
snprintf(name,sizeof(name),"#%i",id);
return name;
}
int open_netlink()
{
int sock = socket(AF_NETLINK,SOCK_RAW,MYPROTO);
struct sockaddr_nl addr;
memset((void *)&addr, 0, sizeof(addr));
if (sock<0)
return sock;
addr.nl_family = AF_NETLINK;
addr.nl_pid = getpid();
addr.nl_groups = MYMGRP;
if (bind(sock,(struct sockaddr *)&addr,sizeof(addr))<0)
return -1;
return sock;
}
int read_event(int sock)
{
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)+sizeof(struct ifinfomsg);
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(sock, &msg, 0);
if (ret<0) {
return ret;
}
printf("Type: %i (%s)\n",(nlh.nlmsg_type),lookup_name(typenames,nlh.nlmsg_type));
printf("Flag:");
#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
printf("\n");
printf("Seq : %i\n",nlh.nlmsg_seq);
printf("Pid : %i\n",nlh.nlmsg_pid);
printf("\n");
{
struct ifinfomsg *ifi;
ifi= NLMSG_DATA((unsigned char *) &nlh);
if(ifi->ifi_flags & IFF_LOWER_UP)
printf("link up:[%x]\n",ifi->ifi_flags);
else
printf("link down:[%x]\n",ifi->ifi_flags);
}
return 0;
}
int main(int argc, char *argv[])
{
int nls = open_netlink();
if (nls<0) {
err(1,"netlink");
}
while (1)
read_event(nls);
return 0;
}
3 comments:
chenlina20150720
jordan 6
ray ban eyeglasses
hollister
www.louisvuitton.com
christian louboutin shoes
abercrombie store
celine
jordan 6s
mont blanc pens
michael kors outlet
michael kors outlet
gucci bags
chanel outlet
oakley sunglasses
longchamp handbags
chi flat iron
louis vuitton outlet
louis vuitton outlet
christian louboutin outlet
true religion jeans
abercrombie and fitch
abercrombie & fitch
air max 90
ray ban sungalss
jordan 8s
hollister clothing store
christian louboutin outlet
louis vuitton outlet
gucci outlet online
christian louboutin sale
louis vuitton outlet
louis vuitton
michael kors
coach factorty outlet
jordan 5
fendi
longchamp handbags
lululemon sale
jordan retro 4
abercrombie
c2c01k8g61 j6b28n5o10 m8b33p6o71 s3m35w5h94 c7p95o8u19 h8x42m4j42
c4s30f1s65 u9m48u7u96 l9t97p8o29 v0o07d0v05 e7a25e6t88 g8q14t4e69
Post a Comment