Ignore:
Timestamp:
Nov 18, 2016, 2:58:30 PM (7 years ago)
Author:
coas-nagasima
Message:

uIPを更新
プロジェクトファイルを更新

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uKadecot/trunk/uip/net/ipv4/uip_arp.c

    r158 r262  
    7878  uint16_t opcode;
    7979  struct uip_eth_addr shwaddr;
    80   uint16_t sipaddr[2];
     80  uip_ipaddr_t sipaddr;
    8181  struct uip_eth_addr dhwaddr;
    82   uint16_t dipaddr[2];
     82  uip_ipaddr_t dipaddr;
    8383};
    8484
     
    9494    proto;
    9595  uint16_t ipchksum;
    96   uint16_t srcipaddr[2],
    97     destipaddr[2];
     96  uip_ipaddr_t srcipaddr, destipaddr;
    9897};
    9998
     
    110109
    111110struct arp_entry {
    112   uint16_t ipaddr[2];
     111  uip_ipaddr_t ipaddr;
    113112  struct uip_eth_addr ethaddr;
    114113  uint8_t time;
     
    118117  {{0xff,0xff,0xff,0xff,0xff,0xff}};
    119118static 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 };
    122119
    123120static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
    124 static uint16_t ipaddr[2];
     121static uip_ipaddr_t ipaddr;
    125122static uint8_t i, c;
    126123
     
    140137{
    141138  for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
    142     memset(arp_table[i].ipaddr, 0, 4);
     139    memset(&arp_table[i].ipaddr, 0, 4);
    143140  }
    144141}
     
    161158  for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
    162159    tabptr = &arp_table[i];
    163     if((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 &&
     160    if(uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr) &&
    164161       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
    170168/*-----------------------------------------------------------------------------------*/
    171169static void
    172 uip_arp_update(uint16_t *ipaddr, struct uip_eth_addr *ethaddr)
    173 {
    174   register struct arp_entry *tabptr = NULL;
     170uip_arp_update(uip_ipaddr_t *ipaddr, struct uip_eth_addr *ethaddr)
     171{
     172  register struct arp_entry *tabptr = arp_table;
     173
    175174  /* Walk through the ARP mapping table and try to find an entry to
    176175     update. If none is found, the IP -> MAC address mapping is
    177176     inserted in the ARP table. */
    178177  for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
    179 
    180178    tabptr = &arp_table[i];
     179
    181180    /* 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)) {
    184182
    185183      /* Check if the source IP address of the incoming packet matches
    186184         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)) {
    189186
    190187        /* An old entry found, update this and return. */
     
    203200  for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
    204201    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)) {
    207203      break;
    208204    }
     
    227223  /* Now, i is the ARP table entry which we will fill with the new
    228224     information. */
    229   memcpy(tabptr->ipaddr, ipaddr, 4);
     225  uip_ipaddr_copy(&tabptr->ipaddr, ipaddr);
    230226  memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
    231227  tabptr->time = arptime;
     
    300296
    301297  switch(BUF->opcode) {
    302   case HTONS(ARP_REQUEST):
     298  case UIP_HTONS(ARP_REQUEST):
    303299    /* ARP request. If it asked for our address, we send out a
    304300       reply. */
    305     if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) {
     301    if(uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr)) {
    306302      /* First, we register the one who made the request in our ARP
    307303         table, since it is likely that we will do more communication
    308304         with this host in the future. */
    309       uip_arp_update(BUF->sipaddr, &BUF->shwaddr);
     305      uip_arp_update(&BUF->sipaddr, &BUF->shwaddr);
    310306
    311307      /* The reply opcode is 2. */
    312       BUF->opcode = HTONS(2);
     308      BUF->opcode = UIP_HTONS(2);
    313309
    314310      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);
    317313      memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6);
    318314
    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);
    325319      uip_len = sizeof(struct arp_hdr);
    326320    }
    327321    break;
    328   case HTONS(ARP_REPLY):
     322  case UIP_HTONS(ARP_REPLY):
    329323    /* ARP reply. We insert or update the ARP table if it was meant
    330324       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);
    333327    }
    334328    break;
     
    378372
    379373  /* 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)) {
    381375    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. */
    384378    IPBUF->ethhdr.dest.addr[0] = 0x01;
    385379    IPBUF->ethhdr.dest.addr[1] = 0x00;
    386     IPBUF->ethhdr.dest.addr[2] = 0x5E;
    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];
    390384  } else {
    391385    /* 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)) {
    393387      /* Destination address was not on the local network, so we need to
    394388         use the default router's IP address instead of the destination
    395389         address when determining the MAC address. */
    396       uip_ipaddr_copy(ipaddr, uip_draddr);
     390      uip_ipaddr_copy(&ipaddr, &uip_draddr);
    397391    } else {
    398392      /* Else, we use the destination IP address. */
    399       uip_ipaddr_copy(ipaddr, IPBUF->destipaddr);
    400     }
    401 
     393      uip_ipaddr_copy(&ipaddr, &IPBUF->destipaddr);
     394    }
    402395    for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
    403396      tabptr = &arp_table[i];
    404       if(uip_ipaddr_cmp(ipaddr, tabptr->ipaddr)) {
     397      if(uip_ipaddr_cmp(&ipaddr, &tabptr->ipaddr)) {
    405398        break;
    406399      }
     
    413406      memset(BUF->ethhdr.dest.addr, 0xff, 6);
    414407      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);
    423416      BUF->hwlen = 6;
    424417      BUF->protolen = 4;
    425       BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
     418      BUF->ethhdr.type = UIP_HTONS(UIP_ETHTYPE_ARP);
    426419
    427420      uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN];
     
    434427    memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6);
    435428  }
    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);
    439432
    440433  uip_len += sizeof(struct uip_eth_hdr);
     
    444437/** @} */
    445438/** @} */
     439
Note: See TracChangeset for help on using the changeset viewer.