Changeset 262 for uKadecot/trunk/uip/net/ipv4/uip_arp.c
- Timestamp:
- Nov 18, 2016, 2:58:30 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uKadecot/trunk/uip/net/ipv4/uip_arp.c
r158 r262 78 78 uint16_t opcode; 79 79 struct uip_eth_addr shwaddr; 80 ui nt16_t sipaddr[2];80 uip_ipaddr_t sipaddr; 81 81 struct uip_eth_addr dhwaddr; 82 ui nt16_t dipaddr[2];82 uip_ipaddr_t dipaddr; 83 83 }; 84 84 … … 94 94 proto; 95 95 uint16_t ipchksum; 96 uint16_t srcipaddr[2], 97 destipaddr[2]; 96 uip_ipaddr_t srcipaddr, destipaddr; 98 97 }; 99 98 … … 110 109 111 110 struct arp_entry { 112 ui nt16_t ipaddr[2];111 uip_ipaddr_t ipaddr; 113 112 struct uip_eth_addr ethaddr; 114 113 uint8_t time; … … 118 117 {{0xff,0xff,0xff,0xff,0xff,0xff}}; 119 118 static const uint16_t broadcast_ipaddr[2] = {0xffff,0xffff}; 120 static const uint16_t multicast_ipaddr[2] = { 0x00e0, 0x0000 };121 static const uint16_t multicast_mask[2] = { 0x00f0, 0x0000 };122 119 123 120 static struct arp_entry arp_table[UIP_ARPTAB_SIZE]; 124 static ui nt16_t ipaddr[2];121 static uip_ipaddr_t ipaddr; 125 122 static uint8_t i, c; 126 123 … … 140 137 { 141 138 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { 142 memset( arp_table[i].ipaddr, 0, 4);139 memset(&arp_table[i].ipaddr, 0, 4); 143 140 } 144 141 } … … 161 158 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { 162 159 tabptr = &arp_table[i]; 163 if( (tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0&&160 if(uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr) && 164 161 arptime - tabptr->time >= UIP_ARP_MAXAGE) { 165 memset(tabptr->ipaddr, 0, 4); 166 } 167 } 168 169 } 162 memset(&tabptr->ipaddr, 0, 4); 163 } 164 } 165 166 } 167 170 168 /*-----------------------------------------------------------------------------------*/ 171 169 static void 172 uip_arp_update(uint16_t *ipaddr, struct uip_eth_addr *ethaddr) 173 { 174 register struct arp_entry *tabptr = NULL; 170 uip_arp_update(uip_ipaddr_t *ipaddr, struct uip_eth_addr *ethaddr) 171 { 172 register struct arp_entry *tabptr = arp_table; 173 175 174 /* Walk through the ARP mapping table and try to find an entry to 176 175 update. If none is found, the IP -> MAC address mapping is 177 176 inserted in the ARP table. */ 178 177 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { 179 180 178 tabptr = &arp_table[i]; 179 181 180 /* Only check those entries that are actually in use. */ 182 if(tabptr->ipaddr[0] != 0 && 183 tabptr->ipaddr[1] != 0) { 181 if(!uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr)) { 184 182 185 183 /* Check if the source IP address of the incoming packet matches 186 184 the IP address in this ARP table entry. */ 187 if(ipaddr[0] == tabptr->ipaddr[0] && 188 ipaddr[1] == tabptr->ipaddr[1]) { 185 if(uip_ipaddr_cmp(ipaddr, &tabptr->ipaddr)) { 189 186 190 187 /* An old entry found, update this and return. */ … … 203 200 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { 204 201 tabptr = &arp_table[i]; 205 if(tabptr->ipaddr[0] == 0 && 206 tabptr->ipaddr[1] == 0) { 202 if(uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr)) { 207 203 break; 208 204 } … … 227 223 /* Now, i is the ARP table entry which we will fill with the new 228 224 information. */ 229 memcpy(tabptr->ipaddr, ipaddr, 4);225 uip_ipaddr_copy(&tabptr->ipaddr, ipaddr); 230 226 memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6); 231 227 tabptr->time = arptime; … … 300 296 301 297 switch(BUF->opcode) { 302 case HTONS(ARP_REQUEST):298 case UIP_HTONS(ARP_REQUEST): 303 299 /* ARP request. If it asked for our address, we send out a 304 300 reply. */ 305 if(uip_ipaddr_cmp( BUF->dipaddr,uip_hostaddr)) {301 if(uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr)) { 306 302 /* First, we register the one who made the request in our ARP 307 303 table, since it is likely that we will do more communication 308 304 with this host in the future. */ 309 uip_arp_update( BUF->sipaddr, &BUF->shwaddr);305 uip_arp_update(&BUF->sipaddr, &BUF->shwaddr); 310 306 311 307 /* The reply opcode is 2. */ 312 BUF->opcode = HTONS(2);308 BUF->opcode = UIP_HTONS(2); 313 309 314 310 memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6); 315 memcpy(BUF->shwaddr.addr, uip_ ethaddr.addr, 6);316 memcpy(BUF->ethhdr.src.addr, uip_ ethaddr.addr, 6);311 memcpy(BUF->shwaddr.addr, uip_lladdr.addr, 6); 312 memcpy(BUF->ethhdr.src.addr, uip_lladdr.addr, 6); 317 313 memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6); 318 314 319 BUF->dipaddr[0] = BUF->sipaddr[0]; 320 BUF->dipaddr[1] = BUF->sipaddr[1]; 321 BUF->sipaddr[0] = uip_hostaddr[0]; 322 BUF->sipaddr[1] = uip_hostaddr[1]; 323 324 BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP); 315 uip_ipaddr_copy(&BUF->dipaddr, &BUF->sipaddr); 316 uip_ipaddr_copy(&BUF->sipaddr, &uip_hostaddr); 317 318 BUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP); 325 319 uip_len = sizeof(struct arp_hdr); 326 320 } 327 321 break; 328 case HTONS(ARP_REPLY):322 case UIP_HTONS(ARP_REPLY): 329 323 /* ARP reply. We insert or update the ARP table if it was meant 330 324 for us. */ 331 if(uip_ipaddr_cmp( BUF->dipaddr,uip_hostaddr)) {332 uip_arp_update( BUF->sipaddr, &BUF->shwaddr);325 if(uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr)) { 326 uip_arp_update(&BUF->sipaddr, &BUF->shwaddr); 333 327 } 334 328 break; … … 378 372 379 373 /* First check if destination is a local broadcast. */ 380 if(uip_ipaddr_cmp( IPBUF->destipaddr, broadcast_ipaddr)) {374 if(uip_ipaddr_cmp(&IPBUF->destipaddr, &uip_broadcast_addr)) { 381 375 memcpy(IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6); 382 } 383 else if(uip_ipaddr_maskcmp(IPBUF->destipaddr, multicast_ipaddr, multicast_mask)) {376 } else if(IPBUF->destipaddr.u8[0] == 224) { 377 /* Multicast. */ 384 378 IPBUF->ethhdr.dest.addr[0] = 0x01; 385 379 IPBUF->ethhdr.dest.addr[1] = 0x00; 386 IPBUF->ethhdr.dest.addr[2] = 0x5 E;387 IPBUF->ethhdr.dest.addr[3] = uip_ipaddr2(IPBUF->destipaddr) & 0x7F;388 IPBUF->ethhdr.dest.addr[4] = uip_ipaddr3(IPBUF->destipaddr);389 IPBUF->ethhdr.dest.addr[5] = uip_ipaddr4(IPBUF->destipaddr);380 IPBUF->ethhdr.dest.addr[2] = 0x5e; 381 IPBUF->ethhdr.dest.addr[3] = IPBUF->destipaddr.u8[1]; 382 IPBUF->ethhdr.dest.addr[4] = IPBUF->destipaddr.u8[2]; 383 IPBUF->ethhdr.dest.addr[5] = IPBUF->destipaddr.u8[3]; 390 384 } else { 391 385 /* Check if the destination address is on the local network. */ 392 if(!uip_ipaddr_maskcmp( IPBUF->destipaddr, uip_hostaddr,uip_netmask)) {386 if(!uip_ipaddr_maskcmp(&IPBUF->destipaddr, &uip_hostaddr, &uip_netmask)) { 393 387 /* Destination address was not on the local network, so we need to 394 388 use the default router's IP address instead of the destination 395 389 address when determining the MAC address. */ 396 uip_ipaddr_copy( ipaddr,uip_draddr);390 uip_ipaddr_copy(&ipaddr, &uip_draddr); 397 391 } else { 398 392 /* Else, we use the destination IP address. */ 399 uip_ipaddr_copy(ipaddr, IPBUF->destipaddr); 400 } 401 393 uip_ipaddr_copy(&ipaddr, &IPBUF->destipaddr); 394 } 402 395 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) { 403 396 tabptr = &arp_table[i]; 404 if(uip_ipaddr_cmp( ipaddr,tabptr->ipaddr)) {397 if(uip_ipaddr_cmp(&ipaddr, &tabptr->ipaddr)) { 405 398 break; 406 399 } … … 413 406 memset(BUF->ethhdr.dest.addr, 0xff, 6); 414 407 memset(BUF->dhwaddr.addr, 0x00, 6); 415 memcpy(BUF->ethhdr.src.addr, uip_ ethaddr.addr, 6);416 memcpy(BUF->shwaddr.addr, uip_ ethaddr.addr, 6);417 418 uip_ipaddr_copy( BUF->dipaddr,ipaddr);419 uip_ipaddr_copy( BUF->sipaddr,uip_hostaddr);420 BUF->opcode = HTONS(ARP_REQUEST); /* ARP request. */421 BUF->hwtype = HTONS(ARP_HWTYPE_ETH);422 BUF->protocol = HTONS(UIP_ETHTYPE_IP);408 memcpy(BUF->ethhdr.src.addr, uip_lladdr.addr, 6); 409 memcpy(BUF->shwaddr.addr, uip_lladdr.addr, 6); 410 411 uip_ipaddr_copy(&BUF->dipaddr, &ipaddr); 412 uip_ipaddr_copy(&BUF->sipaddr, &uip_hostaddr); 413 BUF->opcode = UIP_HTONS(ARP_REQUEST); /* ARP request. */ 414 BUF->hwtype = UIP_HTONS(ARP_HWTYPE_ETH); 415 BUF->protocol = UIP_HTONS(UIP_ETHTYPE_IP); 423 416 BUF->hwlen = 6; 424 417 BUF->protolen = 4; 425 BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);418 BUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP); 426 419 427 420 uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN]; … … 434 427 memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6); 435 428 } 436 memcpy(IPBUF->ethhdr.src.addr, uip_ ethaddr.addr, 6);437 438 IPBUF->ethhdr.type = HTONS(UIP_ETHTYPE_IP);429 memcpy(IPBUF->ethhdr.src.addr, uip_lladdr.addr, 6); 430 431 IPBUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_IP); 439 432 440 433 uip_len += sizeof(struct uip_eth_hdr); … … 444 437 /** @} */ 445 438 /** @} */ 439
Note:
See TracChangeset
for help on using the changeset viewer.