Reproduced LWIP's IP/MAC address conflict detection

Source: Internet
Author: User

from:http://blog.csdn.net/tianjueyiyi/article/details/51097447

LWIP is a lightweight TCP/IP stack, which is lightweight because the author concentrates the main functions into a relatively simple protocol stack, which is mainly used in rom/ram rare environments. it is because of the light weight, simple, so usually a lot of things for granted, we will feel that in this can also be achieved, in fact, otherwise, people who have used lwIP will feel. If a situation, you set the Ip/mac and other devices in the network conflict, at this time can not detect, but also find out what is the reason, always link will be broken for no reason. here, in response to the problem of conflict, proposed a solution, welcome to correct. First, you need to understand what gratuitous ARP is. Interested can Baidu a bit, here I summarize. When the device access network, you need to send a message to ask other devices, "I sent this IP, who has?" ”。 If this IP is present in the network, it will reply; it is using gratuitous ARP to judge its reply, if the received response or reply, the IP is the same as the device itself, it is marked as an IP conflict. Mac conflicts in the same vein. in lwIP, improvements can be made to detect Mac and IP collisions in the following areas. The main part of the code is posted below.

Etharp_arp_input (struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p) {

...

Case Pp_htons (arp_request):
if (ip_addr_cmp (&sipaddr, & (NETIF->IP_ADDR))) {
Etharperror |= duplicate_ip;
}
if ((hdr->shwaddr.addr[0] = = netif->hwaddr[0]) &&
(hdr->shwaddr.addr[1] = = netif->hwaddr[1]) &&
(hdr->shwaddr.addr[2] = = netif->hwaddr[2]) &&
(Hdr->shwaddr.addr[3] = = netif->hwaddr[3]) &&
(Hdr->shwaddr.addr[4] = = netif->hwaddr[4]) &&
(Hdr->shwaddr.addr[5] = = netif->hwaddr[5])) {
Etharperror |= Duplicate_mac;
}

...

Case Pp_htons (arp_reply):
if (ip_addr_cmp (&sipaddr, & (NETIF->IP_ADDR))) {
Etharperror |= duplicate_ip;
}
if ((hdr->shwaddr.addr[0] = = netif->hwaddr[0]) &&
(hdr->shwaddr.addr[1] = = netif->hwaddr[1]) &&
(hdr->shwaddr.addr[2] = = netif->hwaddr[2]) &&
(Hdr->shwaddr.addr[3] = = netif->hwaddr[3]) &&
(Hdr->shwaddr.addr[4] = = netif->hwaddr[4]) &&
(Hdr->shwaddr.addr[5] = = netif->hwaddr[5])) {
Etharperror |= Duplicate_mac;
}

...

In other places of use just judge the Etharperror variable to see if there is a conflict. Because gratuitous ARP is emitted when the machine is initialized when it is connected to the network, there is no need to worry about whether or not a conflict has been detected while judging the variable.

Reproduced LWIP's IP/MAC address conflict detection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.