ifa_local 和 ifa_address

ifa_local 和 ifa_address区别联系:

1. 在配置了支持广播的接口上,与IFA_LOCAL一样,同样表示本地ip地址;

2. 对于点对点链路,IFA_ADDRESS表示的是对端的地址,IFA_LOCAL表示的是本地ip地址;

 

inetdevice.h

struct in_ifaddr {
    struct hlist_node    hash;
    struct in_ifaddr    *ifa_next;
    struct in_device    *ifa_dev;
    struct rcu_head        rcu_head;
    __be32            ifa_local;
    __be32            ifa_address;
        ...
}

 

在if_addr.h中可以找到这样一段注释:

/*
* Important comment:
* IFA_ADDRESS is prefix address, rather than local interface address.
* It makes no difference for normally configured broadcast interfaces,
* but for point-to-point IFA_ADDRESS is DESTINATION address,
* local address is supplied in IFA_LOCAL attribute.
*/

 

注释说IFA_ADDRESS是一个前缀地址,而不是本地接口地址。只不过是在广播接口上,其值相同;

 

devinet.c:

插入ip地址中截取的一段代码,其中判断同一子网使用了ifa_address,而判断ip地址相同则使用了ifa_local,可见,ifa_local是绝对无误的本地ip地址,也就是说ifa_address更关注前缀部分?

static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh,
                 u32 portid)
{
       if (ifa1->ifa_mask == ifa->ifa_mask &&
            inet_ifa_match(ifa1->ifa_address, ifa)) {
            if (ifa1->ifa_local == ifa->ifa_local) {
                inet_free_ifa(ifa);
                return -EEXIST;
            }
            if (ifa1->ifa_scope != ifa->ifa_scope) {
                inet_free_ifa(ifa);
                return -EINVAL;
            }
            ifa->ifa_flags |= IFA_F_SECONDARY;
        }
}    

 

----对比BSD----

在TCP/IP详解卷2的BSD实现中,其中的ifaddr结构中的两个字段如下:

struct sockaddr *ifa_addr; /* address of interface */

struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */

#define ifa_broadaddr ifa_dstaddr; /* broadcast address interface */

其中ifa_addr表示的是接口地址,与上面的ifa_local一致;

而ifa_dstaddr表示,一个点对点链路上的另一端的接口地址,或者是一个广播网中分配给接口的广播地址(以太网)。两种表示是互斥的。

 

 

刚刚开始阅读源码,后续有进展再进行补充...

posted @ 2017-08-18 00:04  AlexAlex  阅读(2254)  评论(0编辑  收藏  举报