123
Diff: utility/uip_arp.c
- Revision:
- 8:4acb22344932
- Parent:
- 3:5b17e4656dd0
diff -r 1bc7e6120801 -r 4acb22344932 utility/uip_arp.c --- a/utility/uip_arp.c Tue Apr 26 18:37:14 2016 +0000 +++ b/utility/uip_arp.c Fri Jun 30 19:51:28 2017 +0000 @@ -3,7 +3,7 @@ * @{ */ /** - * \defgroup uiparp uIP Address Resolution Protocol + * \defgroup uiparp UIP Address Resolution Protocol * @{ * * The Address Resolution Protocol ARP is used for mapping between IP @@ -49,7 +49,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * This file is part of the uIP TCP/IP stack. + * This file is part of the UIP TCP/IP stack. * * $Id: uip_arp.c,v 1.8 2006/06/02 23:36:21 adam Exp $ * @@ -115,7 +115,7 @@ /*-----------------------------------------------------------------------------------*/ void uip_arp_init(void) { - for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { + for (i = 0; i < UIP_ARPTAB_SIZE; ++i) { memset(arp_table[i].ipaddr, 0, 4); } } @@ -135,9 +135,9 @@ struct arp_entry* tabptr; ++arptime; - for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { + for (i = 0; i < UIP_ARPTAB_SIZE; ++i) { tabptr = &arp_table[i]; - if((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 && arptime - tabptr->time >= UIP_ARP_MAXAGE) { + if ((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 && arptime - tabptr->time >= UIP_ARP_MAXAGE) { memset(tabptr->ipaddr, 0, 4); } } @@ -150,15 +150,15 @@ update. If none is found, the IP -> MAC address mapping is inserted in the ARP table. */ - for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { + for (i = 0; i < UIP_ARPTAB_SIZE; ++i) { tabptr = &arp_table[i]; /* Only check those entries that are actually in use. */ - if(tabptr->ipaddr[0] != 0 && tabptr->ipaddr[1] != 0) { + if (tabptr->ipaddr[0] != 0 && tabptr->ipaddr[1] != 0) { /* Check if the source IP address of the incoming packet matches the IP address in this ARP table entry. */ - if(ipaddr[0] == tabptr->ipaddr[0] && ipaddr[1] == tabptr->ipaddr[1]) { + if (ipaddr[0] == tabptr->ipaddr[0] && ipaddr[1] == tabptr->ipaddr[1]) { /* An old entry found, update this and return. */ memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); @@ -172,21 +172,21 @@ /* If we get here, no existing ARP table entry was found, so we create one. */ /* First, we try to find an unused entry in the ARP table. */ - for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { + for (i = 0; i < UIP_ARPTAB_SIZE; ++i) { tabptr = &arp_table[i]; - if(tabptr->ipaddr[0] == 0 && tabptr->ipaddr[1] == 0) { + if (tabptr->ipaddr[0] == 0 && tabptr->ipaddr[1] == 0) { break; } } /* If no unused entry is found, we try to find the oldest entry and throw it away. */ - if(i == UIP_ARPTAB_SIZE) { + if (i == UIP_ARPTAB_SIZE) { tmpage = 0; c = 0; - for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { + for (i = 0; i < UIP_ARPTAB_SIZE; ++i) { tabptr = &arp_table[i]; - if(arptime - tabptr->time > tmpage) { + if (arptime - tabptr->time > tmpage) { tmpage = arptime - tabptr->time; c = i; } @@ -224,11 +224,11 @@ /* Only insert/update an entry if the source IP address of the incoming IP packet comes from a host on the local network. */ - if((IPBUF->srcipaddr[0] & uip_netmask[0]) != (uip_hostaddr[0] & uip_netmask[0])) { + if ((IPBUF->srcipaddr[0] & uip_netmask[0]) != (uip_hostaddr[0] & uip_netmask[0])) { return; } - if((IPBUF->srcipaddr[1] & uip_netmask[1]) != (uip_hostaddr[1] & uip_netmask[1])) { + if ((IPBUF->srcipaddr[1] & uip_netmask[1]) != (uip_hostaddr[1] & uip_netmask[1])) { return; } @@ -263,49 +263,49 @@ /*-----------------------------------------------------------------------------------*/ void uip_arp_arpin(void) { - if(uip_len < sizeof(struct arp_hdr)) { + if (uip_len < sizeof(struct arp_hdr)) { uip_len = 0; return; } uip_len = 0; - switch(BUF->opcode) { - case HTONS(ARP_REQUEST): - /* ARP request. If it asked for our address, we send out a + switch (BUF->opcode) { + case HTONS(ARP_REQUEST): + /* ARP request. If it asked for our address, we send out a reply. */ - if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) { + if (uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) { - /* First, we register the one who made the request in our ARP + /* First, we register the one who made the request in our ARP table, since it is likely that we will do more communication with this host in the future. */ - uip_arp_update(BUF->sipaddr, &BUF->shwaddr); + uip_arp_update(BUF->sipaddr, &BUF->shwaddr); - /* The reply opcode is 2. */ - BUF->opcode = HTONS(2); + /* The reply opcode is 2. */ + BUF->opcode = HTONS(2); - memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6); - memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6); - memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6); - memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6); + memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6); + memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6); + memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6); + memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6); - BUF->dipaddr[0] = BUF->sipaddr[0]; - BUF->dipaddr[1] = BUF->sipaddr[1]; - BUF->sipaddr[0] = uip_hostaddr[0]; - BUF->sipaddr[1] = uip_hostaddr[1]; + BUF->dipaddr[0] = BUF->sipaddr[0]; + BUF->dipaddr[1] = BUF->sipaddr[1]; + BUF->sipaddr[0] = uip_hostaddr[0]; + BUF->sipaddr[1] = uip_hostaddr[1]; - BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP); - uip_len = sizeof(struct arp_hdr); - } - break; + BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP); + uip_len = sizeof(struct arp_hdr); + } + break; - case HTONS(ARP_REPLY): - /* ARP reply. We insert or update the ARP table if it was meant + case HTONS(ARP_REPLY): + /* ARP reply. We insert or update the ARP table if it was meant for us. */ - if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) { - uip_arp_update(BUF->sipaddr, &BUF->shwaddr); - } - break; + if (uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) { + uip_arp_update(BUF->sipaddr, &BUF->shwaddr); + } + break; } return; @@ -351,13 +351,13 @@ packet with an ARP request for the IP address. */ /* First check if destination is a local broadcast. */ - if(uip_ipaddr_cmp(IPBUF->destipaddr, broadcast_ipaddr)) { + if (uip_ipaddr_cmp(IPBUF->destipaddr, broadcast_ipaddr)) { memcpy(IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6); } else { /* Check if the destination address is on the local network. */ - if(!uip_ipaddr_maskcmp(IPBUF->destipaddr, uip_hostaddr, uip_netmask)) { + if (!uip_ipaddr_maskcmp(IPBUF->destipaddr, uip_hostaddr, uip_netmask)) { /* Destination address was not on the local network, so we need to use the default router's IP address instead of the destination @@ -370,14 +370,14 @@ uip_ipaddr_copy(ipaddr, IPBUF->destipaddr); } - for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { + for (i = 0; i < UIP_ARPTAB_SIZE; ++i) { tabptr = &arp_table[i]; - if(uip_ipaddr_cmp(ipaddr, tabptr->ipaddr)) { + if (uip_ipaddr_cmp(ipaddr, tabptr->ipaddr)) { break; } } - if(i == UIP_ARPTAB_SIZE) { + if (i == UIP_ARPTAB_SIZE) { /* The destination address was not in our ARP table, so we overwrite the IP packet with an ARP request. */