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.c

    r158 r262  
    9999#if UIP_FIXEDADDR > 0
    100100const uip_ipaddr_t uip_hostaddr =
    101   {HTONS((UIP_IPADDR0 << 8) | UIP_IPADDR1),
    102    HTONS((UIP_IPADDR2 << 8) | UIP_IPADDR3)};
     101  { UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, UIP_IPADDR3 };
    103102const uip_ipaddr_t uip_draddr =
    104   {HTONS((UIP_DRIPADDR0 << 8) | UIP_DRIPADDR1),
    105    HTONS((UIP_DRIPADDR2 << 8) | UIP_DRIPADDR3)};
     103  { UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3 };
    106104const uip_ipaddr_t uip_netmask =
    107   {HTONS((UIP_NETMASK0 << 8) | UIP_NETMASK1),
    108    HTONS((UIP_NETMASK2 << 8) | UIP_NETMASK3)};
     105  { UIP_NETMASK0, UIP_NETMASK1, UIP_NETMASK2, UIP_NETMASK3 };
    109106#else
    110107uip_ipaddr_t uip_hostaddr, uip_draddr, uip_netmask;
    111108#endif /* UIP_FIXEDADDR */
    112109
    113 static const uip_ipaddr_t all_ones_addr =
     110const uip_ipaddr_t uip_broadcast_addr =
    114111#if UIP_CONF_IPV6
    115   {0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff};
     112  { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     113      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
    116114#else /* UIP_CONF_IPV6 */
    117   {0xffff,0xffff};
     115  { { 0xff, 0xff, 0xff, 0xff } };
    118116#endif /* UIP_CONF_IPV6 */
    119 static const uip_ipaddr_t all_zeroes_addr =
    120 #if UIP_CONF_IPV6
    121   {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
    122 #else /* UIP_CONF_IPV6 */
    123   {0x0000,0x0000};
    124 #endif /* UIP_CONF_IPV6 */
    125 
     117const uip_ipaddr_t uip_all_zeroes_addr = { { 0x0, /* rest is 0 */ } };
    126118
    127119#if UIP_FIXEDETHADDR
    128 const struct uip_eth_addr uip_ethaddr = {{UIP_ETHADDR0,
     120const struct uip_eth_addr uip_lladdr = {{UIP_ETHADDR0,
    129121                                          UIP_ETHADDR1,
    130122                                          UIP_ETHADDR2,
     
    133125                                          UIP_ETHADDR5}};
    134126#else
    135 struct uip_eth_addr uip_ethaddr = {{0,0,0,0,0,0}};
     127struct uip_eth_addr uip_lladdr = {{0,0,0,0,0,0}};
    136128#endif
    137129
     
    311303uip_chksum(uint16_t *data, uint16_t len)
    312304{
    313   return htons(chksum(0, (uint8_t *)data, len));
     305  return uip_htons(chksum(0, (uint8_t *)data, len));
    314306}
    315307/*---------------------------------------------------------------------------*/
     
    322314  sum = chksum(0, &uip_buf[UIP_LLH_LEN], UIP_IPH_LEN);
    323315  DEBUG_PRINTF("uip_ipchksum: sum 0x%04x\n", sum);
    324   return (sum == 0) ? 0xffff : htons(sum);
     316  return (sum == 0) ? 0xffff : uip_htons(sum);
    325317}
    326318#endif
     
    343335  sum = upper_layer_len + proto;
    344336  /* Sum IP source and destination addresses. */
    345   sum = chksum(sum, (uint8_t *)&BUF->srcipaddr[0], 2 * sizeof(uip_ipaddr_t));
     337  sum = chksum(sum, (uint8_t *)&BUF->srcipaddr, 2 * sizeof(uip_ipaddr_t));
    346338
    347339  /* Sum TCP header and data. */
     
    349341               upper_layer_len);
    350342
    351   return (sum == 0) ? 0xffff : htons(sum);
     343  return (sum == 0) ? 0xffff : uip_htons(sum);
    352344}
    353345/*---------------------------------------------------------------------------*/
     
    375367#endif /* UIP_UDP_CHECKSUMS */
    376368#endif /* UIP_ARCH_CHKSUM */
    377 uint8_t
    378 uip_ismulticast(uip_ipaddr_t ipaddr)
    379 {
    380 #if UIP_CONF_IPV6
    381   return 0;
    382 #else
    383   static const uint16_t multicast_ipaddr[2] = { 0x00e0, 0x0000 };
    384   static const uint16_t multicast_mask[2] = { 0x00f0, 0x0000 };
    385   return uip_ipaddr_maskcmp(ipaddr, multicast_ipaddr, multicast_mask);
    386 #endif
    387 }
    388369/*---------------------------------------------------------------------------*/
    389370void
     
    433414    conn = &uip_conns[c];
    434415    if(conn->tcpstateflags != UIP_CLOSED &&
    435        conn->lport == htons(lastport)) {
     416       conn->lport == uip_htons(lastport)) {
    436417      goto again;
    437418    }
     
    472453  conn->sa = 0;
    473454  conn->sv = 16;   /* Initial value of the RTT variance. */
    474   conn->lport = htons(lastport);
     455  conn->lport = uip_htons(lastport);
    475456  conn->rport = rport;
    476457  uip_ipaddr_copy(&conn->ripaddr, ripaddr);
     
    482463#if UIP_UDP
    483464struct uip_udp_conn *
    484 uip_udp_new(uip_ipaddr_t *ripaddr, uint16_t rport)
     465uip_udp_new(const uip_ipaddr_t *ripaddr, uint16_t rport)
    485466{
    486467  register struct uip_udp_conn *conn;
     
    495476
    496477  for(c = 0; c < UIP_UDP_CONNS; ++c) {
    497     if(uip_udp_conns[c].lport == htons(lastport)) {
     478    if(uip_udp_conns[c].lport == uip_htons(lastport)) {
    498479      goto again;
    499480    }
     
    513494  }
    514495
    515   conn->lport = HTONS(lastport);
     496  conn->lport = UIP_HTONS(lastport);
    516497  conn->rport = rport;
    517498  if(ripaddr == NULL) {
    518     memset(conn->ripaddr, 0, sizeof(uip_ipaddr_t));
     499    memset(&conn->ripaddr, 0, sizeof(uip_ipaddr_t));
    519500  } else {
    520501    uip_ipaddr_copy(&conn->ripaddr, ripaddr);
     
    748729         connection's timer and see if it has reached the RTO value
    749730         in which case we retransmit. */
     731
    750732      if(uip_outstanding(uip_connr)) {
    751733        if(uip_connr->timer-- == 0) {
     
    901883#endif /* UIP_CONF_IPV6 */
    902884
    903   if(uip_ipaddr_cmp(uip_hostaddr, all_zeroes_addr)) {
     885  if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) {
    904886    /* If we are configured to use ping IP address configuration and
    905887       hasn't been assigned an IP address yet, we accept all ICMP
     
    921903    DEBUG_PRINTF("UDP IP checksum 0x%04x\n", uip_ipchksum());
    922904    if(BUF->proto == UIP_PROTO_UDP &&
    923        (uip_ipaddr_cmp(BUF->destipaddr, all_ones_addr) ||
    924        uip_ismulticast(BUF->destipaddr))
    925        /*&&
    926          uip_ipchksum() == 0xffff*/) {
     905       (uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr) ||
     906        (BUF->destipaddr.u8[0] & 224) == 224)) {  /* XXX this is a
     907                                                     hack to be able
     908                                                     to receive UDP
     909                                                     multicast
     910                                                     packets. We check
     911                                                     for the bit
     912                                                     pattern of the
     913                                                     multicast
     914                                                     prefix. */
    927915      goto udp_input;
    928916    }
     
    931919    /* Check if the packet is destined for our IP address. */
    932920#if !UIP_CONF_IPV6
    933     if(!uip_ipaddr_cmp(BUF->destipaddr, uip_hostaddr)) {
     921    if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr)) {
    934922      UIP_STAT(++uip_stat.ip.drop);
    935923      goto drop;
     
    941929       address) as well. However, we will cheat here and accept all
    942930       multicast packets that are sent to the ff02::/16 addresses. */
    943     if(!uip_ipaddr_cmp(BUF->destipaddr, uip_hostaddr) &&
    944        BUF->destipaddr[0] != HTONS(0xff02)) {
     931    if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr) &&
     932       BUF->destipaddr.u16[0] != UIP_HTONS(0xff02)) {
    945933      UIP_STAT(++uip_stat.ip.drop);
    946934      goto drop;
     
    1000988     ourself. */
    1001989#if UIP_PINGADDRCONF
    1002   if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {
    1003     uip_hostaddr[0] = BUF->destipaddr[0];
    1004     uip_hostaddr[1] = BUF->destipaddr[1];
     990  if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) {
     991    uip_hostaddr = BUF->destipaddr;
    1005992  }
    1006993#endif /* UIP_PINGADDRCONF */
     
    1008995  ICMPBUF->type = ICMP_ECHO_REPLY;
    1009996
    1010   if(ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) {
    1011     ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;
     997  if(ICMPBUF->icmpchksum >= UIP_HTONS(0xffff - (ICMP_ECHO << 8))) {
     998    ICMPBUF->icmpchksum += UIP_HTONS(ICMP_ECHO << 8) + 1;
    1012999  } else {
    1013     ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);
     1000    ICMPBUF->icmpchksum += UIP_HTONS(ICMP_ECHO << 8);
    10141001  }
    10151002
    10161003  /* Swap IP addresses. */
    1017   uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);
    1018   uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
     1004  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
     1005  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
    10191006
    10201007  UIP_STAT(++uip_stat.icmp.sent);
    1021   goto send;
     1008  BUF->ttl = UIP_TTL;
     1009  goto ip_send_nolen;
    10221010
    10231011  /* End of IPv4 input header processing code. */
     
    10401028     a neighbor advertisement message back. */
    10411029  if(ICMPBUF->type == ICMP6_NEIGHBOR_SOLICITATION) {
    1042     if(uip_ipaddr_cmp(ICMPBUF->icmp6data, uip_hostaddr)) {
     1030    if(uip_ipaddr_cmp(&ICMPBUF->icmp6data, &uip_hostaddr)) {
    10431031
    10441032      if(ICMPBUF->options[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS) {
    10451033        /* Save the sender's address in our neighbor list. */
    1046         uip_neighbor_add(ICMPBUF->srcipaddr, &(ICMPBUF->options[2]));
     1034        uip_neighbor_add(&ICMPBUF->srcipaddr, &(ICMPBUF->options[2]));
    10471035      }
    10481036
     
    10541042      ICMPBUF->reserved1 = ICMPBUF->reserved2 = ICMPBUF->reserved3 = 0;
    10551043
    1056       uip_ipaddr_copy(ICMPBUF->destipaddr, ICMPBUF->srcipaddr);
    1057       uip_ipaddr_copy(ICMPBUF->srcipaddr, uip_hostaddr);
     1044      uip_ipaddr_copy(&ICMPBUF->destipaddr, &ICMPBUF->srcipaddr);
     1045      uip_ipaddr_copy(&ICMPBUF->srcipaddr, &uip_hostaddr);
    10581046      ICMPBUF->options[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS;
    10591047      ICMPBUF->options[1] = 1;  /* Options length, 1 = 8 bytes. */
    1060       memcpy(&(ICMPBUF->options[2]), &uip_ethaddr, sizeof(uip_ethaddr));
     1048      memcpy(&(ICMPBUF->options[2]), &uip_lladdr, sizeof(uip_lladdr));
    10611049      ICMPBUF->icmpchksum = 0;
    10621050      ICMPBUF->icmpchksum = ~uip_icmp6chksum();
     1051
    10631052      goto send;
    10641053
     
    10721061    ICMPBUF->type = ICMP6_ECHO_REPLY;
    10731062
    1074     uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);
    1075     uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
     1063    uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
     1064    uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
    10761065    ICMPBUF->icmpchksum = 0;
    10771066    ICMPBUF->icmpchksum = ~uip_icmp6chksum();
     
    11101099  uip_len = uip_len - UIP_IPUDPH_LEN;
    11111100#endif /* UIP_UDP_CHECKSUMS */
     1101
     1102  /* Make sure that the UDP destination port number is not zero. */
     1103  if(UDPBUF->destport == 0) {
     1104    UIP_LOG("udp: zero port.");
     1105    goto drop;
     1106  }
    11121107
    11131108  /* Demultiplex this UDP packet between the UDP "connections". */
     
    11261121       (uip_udp_conn->rport == 0 ||
    11271122        UDPBUF->srcport == uip_udp_conn->rport) &&
    1128        (uip_ipaddr_cmp(uip_udp_conn->ripaddr, all_zeroes_addr) ||
    1129         uip_ipaddr_cmp(uip_udp_conn->ripaddr, all_ones_addr) ||
    1130         uip_ipaddr_cmp(BUF->srcipaddr, uip_udp_conn->ripaddr))) {
     1123       (uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_all_zeroes_addr) ||
     1124        uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_broadcast_addr) ||
     1125        uip_ipaddr_cmp(&BUF->srcipaddr, &uip_udp_conn->ripaddr))) {
    11311126      goto udp_found;
    11321127    }
     
    11411136  uip_slen = 0;
    11421137  UIP_UDP_APPCALL();
     1138
    11431139 udp_send:
    11441140  if(uip_slen == 0) {
     
    11601156  BUF->proto = UIP_PROTO_UDP;
    11611157
    1162   UDPBUF->udplen = HTONS(uip_slen + UIP_UDPH_LEN);
     1158  UDPBUF->udplen = UIP_HTONS(uip_slen + UIP_UDPH_LEN);
    11631159  UDPBUF->udpchksum = 0;
    11641160
     
    11661162  BUF->destport = uip_udp_conn->rport;
    11671163
    1168   uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
    1169   uip_ipaddr_copy(BUF->destipaddr, uip_udp_conn->ripaddr);
     1164  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
     1165  uip_ipaddr_copy(&BUF->destipaddr, &uip_udp_conn->ripaddr);
    11701166
    11711167  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN];
     
    11961192  }
    11971193
     1194  /* Make sure that the TCP port number is not zero. */
     1195  if(BUF->destport == 0 || BUF->srcport == 0) {
     1196    UIP_LOG("tcp: zero port.");
     1197    goto drop;
     1198  }
    11981199
    11991200  /* Demultiplex this segment. */
     
    12041205       BUF->destport == uip_connr->lport &&
    12051206       BUF->srcport == uip_connr->rport &&
    1206        uip_ipaddr_cmp(BUF->srcipaddr, uip_connr->ripaddr)) {
     1207       uip_ipaddr_cmp(&BUF->srcipaddr, &uip_connr->ripaddr)) {
    12071208      goto found;
    12081209    }
     
    12201221  /* Next, check listening connections. */
    12211222  for(c = 0; c < UIP_LISTENPORTS; ++c) {
    1222     if(tmp16 == uip_listenports[c])
     1223    if(tmp16 == uip_listenports[c]) {
    12231224      goto found_listen;
     1225    }
    12241226  }
    12251227
    12261228  /* No matching connection found, so we send a RST packet. */
    12271229  UIP_STAT(++uip_stat.tcp.synrst);
     1230
    12281231 reset:
    1229 
    12301232  /* We do not send resets in response to resets. */
    12311233  if(BUF->flags & TCP_RST) {
     
    12731275
    12741276  /* Swap IP addresses. */
    1275   uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);
    1276   uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
     1277  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
     1278  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
    12771279
    12781280  /* And send out the RST packet! */
     
    12831285     connection and send a SYNACK in return. */
    12841286 found_listen:
    1285   /* First we check if there are any connections avaliable. Unused
     1287  /* First we check if there are any connections available. Unused
    12861288     connections are kept in the same table as used connections, but
    12871289     unused ones have the tcpstate set to CLOSED. Also, connections in
     
    13201322  uip_connr->lport = BUF->destport;
    13211323  uip_connr->rport = BUF->srcport;
    1322   uip_ipaddr_copy(uip_connr->ripaddr, BUF->srcipaddr);
     1324  uip_ipaddr_copy(&uip_connr->ripaddr, &BUF->srcipaddr);
    13231325  uip_connr->tcpstateflags = UIP_SYN_RCVD;
    13241326
     
    14061408    goto drop;
    14071409  }
    1408   /* Calculated the length of the data, if the application has sent
     1410  /* Calculate the length of the data, if the application has sent
    14091411     any data to us. */
    14101412  c = (BUF->tcpoffset >> 4) << 2;
     
    14161418  /* First, check if the sequence number of the incoming packet is
    14171419     what we're expecting next. If not, we send out an ACK with the
    1418      correct numbers in. */
    1419   if(!(((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) &&
    1420        ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)))) {
     1420     correct numbers in, unless we are in the SYN_RCVD state and
     1421     receive a SYN, in which case we should retransmit our SYNACK
     1422     (which is done futher down). */
     1423  if(!((((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) &&
     1424        ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK))) ||
     1425       (((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_RCVD) &&
     1426        ((BUF->flags & TCP_CTL) == TCP_SYN)))) {
    14211427    if((uip_len > 0 || ((BUF->flags & (TCP_SYN | TCP_FIN)) != 0)) &&
    14221428       (BUF->seqno[0] != uip_connr->rcv_nxt[0] ||
     
    14441450      uip_connr->snd_nxt[2] = uip_acc32[2];
    14451451      uip_connr->snd_nxt[3] = uip_acc32[3];
    1446 
    14471452
    14481453      /* Do RTT estimation, unless we have done retransmissions. */
     
    14951500      goto appsend;
    14961501    }
     1502    /* We need to retransmit the SYNACK */
     1503    if((BUF->flags & TCP_CTL) == TCP_SYN) {
     1504      goto tcp_send_synack;
     1505    }
    14971506    goto drop;
    14981507#if UIP_ACTIVE_OPEN
     
    17981807  goto drop;
    17991808
    1800 
    18011809  /* We jump here when we are ready to send the packet, and just want
    18021810     to set the appropriate TCP sequence numbers in the TCP header. */
    18031811 tcp_send_ack:
    18041812  BUF->flags = TCP_ACK;
     1813
    18051814 tcp_send_nodata:
    18061815  uip_len = UIP_IPTCPH_LEN;
     1816
    18071817 tcp_send_noopts:
    18081818  BUF->tcpoffset = (UIP_TCPH_LEN / 4) << 4;
    1809  tcp_send:
     1819
    18101820  /* We're done with the input processing. We are now ready to send a
    18111821     reply. Our job is to fill in all the fields of the TCP and IP
    18121822     headers before calculating the checksum and finally send the
    18131823     packet. */
     1824 tcp_send:
    18141825  BUF->ackno[0] = uip_connr->rcv_nxt[0];
    18151826  BUF->ackno[1] = uip_connr->rcv_nxt[1];
     
    18271838  BUF->destport = uip_connr->rport;
    18281839
    1829   uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
    1830   uip_ipaddr_copy(BUF->destipaddr, uip_connr->ripaddr);
     1840  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
     1841  uip_ipaddr_copy(&BUF->destipaddr, &uip_connr->ripaddr);
    18311842
    18321843  if(uip_connr->tcpstateflags & UIP_STOPPED) {
     
    18581869
    18591870 ip_send_nolen:
    1860 
    18611871#if UIP_CONF_IPV6
    18621872  BUF->vtc = 0x60;
     
    18751885  DEBUG_PRINTF("uip ip_send_nolen: chkecum 0x%04x\n", uip_ipchksum());
    18761886#endif /* UIP_CONF_IPV6 */
    1877 
    18781887  UIP_STAT(++uip_stat.tcp.sent);
     1888#if UIP_CONF_IPV6
    18791889 send:
     1890#endif /* UIP_CONF_IPV6 */
    18801891  DEBUG_PRINTF("Sending packet with length %d (%d)\n", uip_len,
    18811892               (BUF->len[0] << 8) | BUF->len[1]);
     
    18851896  uip_flags = 0;
    18861897  return;
     1898
    18871899 drop:
    18881900  uip_len = 0;
     
    18921904/*---------------------------------------------------------------------------*/
    18931905uint16_t
    1894 htons(uint16_t val)
     1906uip_htons(uint16_t val)
    18951907{
    1896   return HTONS(val);
     1908  return UIP_HTONS(val);
    18971909}
    18981910/*---------------------------------------------------------------------------*/
Note: See TracChangeset for help on using the changeset viewer.