首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

netlink与rtnetlink(2)

2012-10-09 
netlink与rtnetlink(二)转至:http://blogold.chinaunix.net/u/15993/showart_90109.htmlrtnetlink就是一组

netlink与rtnetlink(二)

转至:http://blogold.chinaunix.net/u/15993/showart_90109.html

rtnetlink就是一组操作rtnetlink消息的宏。
在netlink中,初始化一个netlink_route套接字时,最后一个字段使用NETLINK_ROUTE时就是rtnetlink_socket。
?#include <asm/types.h>
?????? ?#include <linux/netlink.h>
?????? ?#include <linux/rtnetlink.h>
?????? ?#include <sys/socket.h>
?????? ?rtnetlink_socket? =? socket(PF_NETLINK,?int? socket_type, NETLINK_ROUTE);
这组宏包括:
?int RTA_OK(struct rtattr *rta, int rtabuflen);
?????? ?void *RTA_DATA(struct rtattr *rta);
?????? ?unsigned int RTA_PAYLOAD(struct rtattr *rta);
?????? ?struct rtattr *RTA_NEXT(struct rtattr *rta,? unsigned? int? rtabuflen);
?????? ?unsigned int RTA_LENGTH(unsigned int length);
?????? ?unsigned int RTA_SPACE(unsigned int length);

使用这一组宏可以对路由表进行读写和修改。不仅对路由表,还有很多网络参数都可以通过rtnetlink来修改。路由属性:
一些rtnetlink消息在初始化头后面有附加的属性:
?struct rtattr
?????? {
??? unsigned short rta_len;???? /* Length of option */
??? unsigned short rta_type;??? /* Type of option */
??? /* Data follows */
?????? };除了标准的netlink消息之外,rtnetlink由这些消息类型组成。
RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK
?创建或者删除一个特定的网络接口,或者从一个特定的网络接口上获得信息。这些消息含有一个ifinfomsg类型的结构,紧跟在后面的是一系列的rtattr结构。
?struct ifinfomsg
?{
??unsigned char? ifi_family;? /* AF_UNSPEC */
??unsigned char? __ifi_pad;?? /* unused */
??unsigned short ifi_type;??? /* Device type */
??int?? ifi_index;?? /* Interface index?? */
??unsigned int? ifi_flags;?? /* Device flags? */
??unsigned int? ifi_change;? /* change mask */
?}?
?其中ifi_change是为将来预留的,总是被设为0xFFFFFFFF
?????? rta_type???? value type?????? description
?????? --------------------------
?????? IFLA_UNSPEC??? -??????? unspecified.
?????? IFLA_ADDRESS??? hardware address?? interface L2 address
?????? IFLA_BROADCAST??? hardware address?? L2 broadcast address.
?????? IFLA_IFNAME??? asciiz string????? Device name.
?????? IFLA_MTU???? unsigned int?????? MTU of the device.
?????? IFLA_LINK???? int??????? Link type.
?????? IFLA_QDISC??? asciiz string????? Queueing discipline.
?????? IFLA_STATS??? struct?????? Interface Statistics.
?????? net_device_stats
RTM_NEWADDR, RTM_DELADDR, RTM_GETADDR
?添加,删除或者接收一个和接口相关的IP地址的信息。在linux2.2中,一个网口是可以有多个IP地址信息的。这些消息含有一个ifaddrmsg类型的结构,紧跟在后面的是一系列的rtattr结构。
?struct ifaddrmsg
?????? {
??? unsigned char? ifa_family;? /* Address type */
??? unsigned char? ifa_prefixlen;/* Prefixlength of the address */
??? unsigned char? ifa_flags;?? /* Address flags */
??? unsigned char? ifa_scope;?? /* Address scope */
??? int?? ifa_index;?? /* Interface index?? */
?????? };
? ?rta_type?????? value type????? description
?????? -------------------------
?????? IFA_UNSPEC????? -??????? unspecified.
?????? IFA_ADDRESS???? raw protocol address?? interface address
?????? IFA_LOCAL?????? raw protocol address?? local address
?????? IFA_LABEL?????? asciiz string????? name of the interface
?????? IFA_BROADCAST?? raw protocol address?? broadcast address.
?????? IFA_ANYCAST???? raw protocol address?? anycast address
?????? IFA_CACHEINFO?? struct ifa_cacheinfo?? Address information.RTM_NEWROUTE, RTM_DELROUTE, RTM_GETROUTE
?创建,删除一个网络路由或者从一个网络路由上获得信息。这些消息包含了一个rtmsg结构,紧跟着的是一系列的rtattr结构,这是可选的。
??struct rtmsg
?????? {
??? unsigned char? rtm_family;? /* Address family of route */
??? unsigned char? rtm_dst_len; /* Length of source */
??? unsigned char? rtm_src_len; /* Length of destination */
??? unsigned char? rtm_tos;???? /* TOS filter */??? unsigned char? rtm_table;?? /* Routing table id */
??? unsigned char? rtm_protocol;/* Routing protocol; see below */
??? unsigned char? rtm_scope;?? /* See below */
??? unsigned char? rtm_type;??? /* See below */??? unsigned int? rtm_flags;
?????? };?????? rtm_type??Route type
?????? -----------------------
?????? RTN_UNSPEC?unknown route
?????? RTN_UNICAST?a gateway or direct route
?????? RTN_LOCAL??a local interface route
?????? RTN_BROADCAST?a? local? broadcast? route? (sent? as? a
????broadcast)
?????? RTN_ANYCAST?a local broadcast route (sent as a? uni-
????cast)
?????? RTN_MULTICAST?a multicast route
?????? RTN_BLACKHOLE?a packet dropping route
?????? RTN_UNREACHABLE?an unreachable destination
?????? RTN_PROHIBIT?a packet rejection route
?????? RTN_THROW??continue routing lookup in another table
?????? RTN_NAT??a network address translation rule
?????? RTN_XRESOLVE?refer?to? an?external? resolver? (not
????implemented)?????? rtm_protocol?Route origin.
?????? -----------------------------------------
?????? RTPROT_UNSPEC?unknown
?????? RTPROT_REDIRECT?by? an?ICMP? redirect
????(currently unused)
?????? RTPROT_KERNEL?by the kernel
?????? RTPROT_BOOT?during boot
?????? RTPROT_STATIC?by the administrator
?比RTPROT_STATIC大的值就不会再被kernel解释了,他们就只是用于用户信息了。?????? rtm_scope为到目标的距离
?????? RT_SCOPE_UNIVERSE?? global route
?????? RT_SCOPE_SITE?? interior?? route? in?the
????? local autonomous system
?????? RT_SCOPE_LINK?? route on this link
?????? RT_SCOPE_HOST?? route on the local host
?????? RT_SCOPE_NOWHERE?? destination doesn't exist?????? RT_SCOPE_UNIVERSE到RT_SCOPE_SITE之间的值是对用户可得的。?????? The rtm_flags有以下一些含义:
?????? RTM_F_NOTIFY???? if? the?route changes, notify the user via
????????? rtnetlink
?????? RTM_F_CLONED???? route is cloned from another route
?????? RTM_F_EQUALIZE?? a multicast equalizer (not yet implemented)?????? rtm_table表明了路由表
?????? RT_TABLE_UNSPEC? an unspecified routing table
?????? RT_TABLE_DEFAULT? the default table
?????? RT_TABLE_MAIN? the main table
?????? RT_TABLE_LOCAL? the local table?????? 用户可以分配RT_TABLE_UNSPEC和RT_TABLE_DEFAULT之间任意的值。?????? 属性
?????? rta_type?????? value type? description
?????? --------------------------
?????? RTA_UNSPEC????? -??? ignored.
?????? RTA_DST?????? protocol address? Route destination address.
?????? RTA_SRC?????? protocol address? Route source address.
?????? RTA_IIF?????? int?? Input interface index.
?????? RTA_OIF?????? int?? Output interface index.
?????? RTA_GATEWAY???? protocol address? The gateway of the route
?????? RTA_PRIORITY??? int?? Priority of route.
?????? RTA_PREFSRC
?????? RTA_METRICS???? int?? Route metric
?????? RTA_MULTIPATH
?????? RTA_PROTOINFO
?????? RTA_FLOW
?????? RTA_CACHEINFO?RTM_NEWNEIGH, RTM_DELNEIGH, RTM_GETNEIGH
?RTM_NEWQDISC, RTM_DELQDISC, RTM_GETQDISC
?RTM_NEWTCLASS, RTM_DELTCLASS, RTM_GETTCLASS
?RTM_NEWTFILTER, RTM_DELTFILTER, RTM_GETTFILTER?这四组宏我就不一一介绍了,大家可以man一下。

热点排行