⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uip.c

📁 uIP1.0完整源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
				       processing. */    goto tcp_input;  }#if UIP_UDP  if(BUF->proto == UIP_PROTO_UDP) {    goto udp_input;  }#endif /* UIP_UDP */#if !UIP_CONF_IPV6  /* ICMPv4 processing code follows. */  if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from					here. */    UIP_STAT(++uip_stat.ip.drop);    UIP_STAT(++uip_stat.ip.protoerr);    UIP_LOG("ip: neither tcp nor icmp.");    goto drop;  }#if UIP_PINGADDRCONF icmp_input:#endif /* UIP_PINGADDRCONF */  UIP_STAT(++uip_stat.icmp.recv);  /* ICMP echo (i.e., ping) processing. This is simple, we only change     the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP     checksum before we return the packet. */  if(ICMPBUF->type != ICMP_ECHO) {    UIP_STAT(++uip_stat.icmp.drop);    UIP_STAT(++uip_stat.icmp.typeerr);    UIP_LOG("icmp: not icmp echo.");    goto drop;  }  /* If we are configured to use ping IP address assignment, we use     the destination IP address of this ping packet and assign it to     ourself. */#if UIP_PINGADDRCONF  if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) {    uip_hostaddr[0] = BUF->destipaddr[0];    uip_hostaddr[1] = BUF->destipaddr[1];  }#endif /* UIP_PINGADDRCONF */  ICMPBUF->type = ICMP_ECHO_REPLY;  if(ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8))) {    ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;  } else {    ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8);  }  /* Swap IP addresses. */  uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);  uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);  UIP_STAT(++uip_stat.icmp.sent);  goto send;  /* End of IPv4 input header processing code. */#else /* !UIP_CONF_IPV6 */  /* This is IPv6 ICMPv6 processing code. */  DEBUG_PRINTF("icmp6_input: length %d\n", uip_len);  if(BUF->proto != UIP_PROTO_ICMP6) { /* We only allow ICMPv6 packets from					 here. */    UIP_STAT(++uip_stat.ip.drop);    UIP_STAT(++uip_stat.ip.protoerr);    UIP_LOG("ip: neither tcp nor icmp6.");    goto drop;  }  UIP_STAT(++uip_stat.icmp.recv);  /* If we get a neighbor solicitation for our address we should send     a neighbor advertisement message back. */  if(ICMPBUF->type == ICMP6_NEIGHBOR_SOLICITATION) {    if(uip_ipaddr_cmp(ICMPBUF->icmp6data, uip_hostaddr)) {      if(ICMPBUF->options[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS) {	/* Save the sender's address in our neighbor list. */	uip_neighbor_add(ICMPBUF->srcipaddr, &(ICMPBUF->options[2]));      }            /* We should now send a neighbor advertisement back to where the	 neighbor solicication came from. */      ICMPBUF->type = ICMP6_NEIGHBOR_ADVERTISEMENT;      ICMPBUF->flags = ICMP6_FLAG_S; /* Solicited flag. */            ICMPBUF->reserved1 = ICMPBUF->reserved2 = ICMPBUF->reserved3 = 0;            uip_ipaddr_copy(ICMPBUF->destipaddr, ICMPBUF->srcipaddr);      uip_ipaddr_copy(ICMPBUF->srcipaddr, uip_hostaddr);      ICMPBUF->options[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS;      ICMPBUF->options[1] = 1;  /* Options length, 1 = 8 bytes. */      memcpy(&(ICMPBUF->options[2]), &uip_ethaddr, sizeof(uip_ethaddr));      ICMPBUF->icmpchksum = 0;      ICMPBUF->icmpchksum = ~uip_icmp6chksum();      goto send;          }    goto drop;  } else if(ICMPBUF->type == ICMP6_ECHO) {    /* ICMP echo (i.e., ping) processing. This is simple, we only       change the ICMP type from ECHO to ECHO_REPLY and update the       ICMP checksum before we return the packet. */    ICMPBUF->type = ICMP6_ECHO_REPLY;        uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);    uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);    ICMPBUF->icmpchksum = 0;    ICMPBUF->icmpchksum = ~uip_icmp6chksum();        UIP_STAT(++uip_stat.icmp.sent);    goto send;  } else {    DEBUG_PRINTF("Unknown icmp6 message type %d\n", ICMPBUF->type);    UIP_STAT(++uip_stat.icmp.drop);    UIP_STAT(++uip_stat.icmp.typeerr);    UIP_LOG("icmp: unknown ICMP message.");    goto drop;  }  /* End of IPv6 ICMP processing. */  #endif /* !UIP_CONF_IPV6 */#if UIP_UDP  /* UDP input processing. */ udp_input:  /* UDP processing is really just a hack. We don't do anything to the     UDP/IP headers, but let the UDP application do all the hard     work. If the application sets uip_slen, it has a packet to     send. */#if UIP_UDP_CHECKSUMS  uip_len = uip_len - UIP_IPUDPH_LEN;  uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];  if(UDPBUF->udpchksum != 0 && uip_udpchksum() != 0xffff) {    UIP_STAT(++uip_stat.udp.drop);    UIP_STAT(++uip_stat.udp.chkerr);    UIP_LOG("udp: bad checksum.");    goto drop;  }#else /* UIP_UDP_CHECKSUMS */  uip_len = uip_len - UIP_IPUDPH_LEN;#endif /* UIP_UDP_CHECKSUMS */  /* Demultiplex this UDP packet between the UDP "connections". */  for(uip_udp_conn = &uip_udp_conns[0];      uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];      ++uip_udp_conn) {    /* If the local UDP port is non-zero, the connection is considered       to be used. If so, the local port number is checked against the       destination port number in the received packet. If the two port       numbers match, the remote port number is checked if the       connection is bound to a remote port. Finally, if the       connection is bound to a remote IP address, the source IP       address of the packet is checked. */    if(uip_udp_conn->lport != 0 &&       UDPBUF->destport == uip_udp_conn->lport &&       (uip_udp_conn->rport == 0 ||        UDPBUF->srcport == uip_udp_conn->rport) &&       (uip_ipaddr_cmp(uip_udp_conn->ripaddr, all_zeroes_addr) ||	uip_ipaddr_cmp(uip_udp_conn->ripaddr, all_ones_addr) ||	uip_ipaddr_cmp(BUF->srcipaddr, uip_udp_conn->ripaddr))) {      goto udp_found;    }  }  UIP_LOG("udp: no matching connection found");  goto drop;   udp_found:  uip_conn = NULL;  uip_flags = UIP_NEWDATA;  uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];  uip_slen = 0;  UIP_UDP_APPCALL(); udp_send:  if(uip_slen == 0) {    goto drop;  }  uip_len = uip_slen + UIP_IPUDPH_LEN;#if UIP_CONF_IPV6  /* For IPv6, the IP length field does not include the IPv6 IP header     length. */  BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);  BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);#else /* UIP_CONF_IPV6 */  BUF->len[0] = (uip_len >> 8);  BUF->len[1] = (uip_len & 0xff);#endif /* UIP_CONF_IPV6 */  BUF->ttl = uip_udp_conn->ttl;  BUF->proto = UIP_PROTO_UDP;  UDPBUF->udplen = HTONS(uip_slen + UIP_UDPH_LEN);  UDPBUF->udpchksum = 0;  BUF->srcport  = uip_udp_conn->lport;  BUF->destport = uip_udp_conn->rport;  uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);  uip_ipaddr_copy(BUF->destipaddr, uip_udp_conn->ripaddr);     uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN];#if UIP_UDP_CHECKSUMS  /* Calculate UDP checksum. */  UDPBUF->udpchksum = ~(uip_udpchksum());  if(UDPBUF->udpchksum == 0) {    UDPBUF->udpchksum = 0xffff;  }#endif /* UIP_UDP_CHECKSUMS */    goto ip_send_nolen;#endif /* UIP_UDP */    /* TCP input processing. */ tcp_input:  UIP_STAT(++uip_stat.tcp.recv);  /* Start of TCP input header processing code. */    if(uip_tcpchksum() != 0xffff) {   /* Compute and check the TCP				       checksum. */    UIP_STAT(++uip_stat.tcp.drop);    UIP_STAT(++uip_stat.tcp.chkerr);    UIP_LOG("tcp: bad checksum.");    goto drop;  }      /* Demultiplex this segment. */  /* First check any active connections. */  for(uip_connr = &uip_conns[0]; uip_connr <= &uip_conns[UIP_CONNS - 1];      ++uip_connr) {    if(uip_connr->tcpstateflags != UIP_CLOSED &&       BUF->destport == uip_connr->lport &&       BUF->srcport == uip_connr->rport &&       uip_ipaddr_cmp(BUF->srcipaddr, uip_connr->ripaddr)) {      goto found;    }  }  /* If we didn't find and active connection that expected the packet,     either this packet is an old duplicate, or this is a SYN packet     destined for a connection in LISTEN. If the SYN flag isn't set,     it is an old packet and we send a RST. */  if((BUF->flags & TCP_CTL) != TCP_SYN) {    goto reset;  }    tmp16 = BUF->destport;  /* Next, check listening connections. */  for(c = 0; c < UIP_LISTENPORTS; ++c) {    if(tmp16 == uip_listenports[c])      goto found_listen;  }    /* No matching connection found, so we send a RST packet. */  UIP_STAT(++uip_stat.tcp.synrst); reset:  /* We do not send resets in response to resets. */  if(BUF->flags & TCP_RST) {    goto drop;  }  UIP_STAT(++uip_stat.tcp.rst);    BUF->flags = TCP_RST | TCP_ACK;  uip_len = UIP_IPTCPH_LEN;  BUF->tcpoffset = 5 << 4;  /* Flip the seqno and ackno fields in the TCP header. */  c = BUF->seqno[3];  BUF->seqno[3] = BUF->ackno[3];  BUF->ackno[3] = c;    c = BUF->seqno[2];  BUF->seqno[2] = BUF->ackno[2];  BUF->ackno[2] = c;    c = BUF->seqno[1];  BUF->seqno[1] = BUF->ackno[1];  BUF->ackno[1] = c;    c = BUF->seqno[0];  BUF->seqno[0] = BUF->ackno[0];  BUF->ackno[0] = c;  /* We also have to increase the sequence number we are     acknowledging. If the least significant byte overflowed, we need     to propagate the carry to the other bytes as well. */  if(++BUF->ackno[3] == 0) {    if(++BUF->ackno[2] == 0) {      if(++BUF->ackno[1] == 0) {	++BUF->ackno[0];      }    }  }   /* Swap port numbers. */  tmp16 = BUF->srcport;  BUF->srcport = BUF->destport;  BUF->destport = tmp16;    /* Swap IP addresses. */  uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);  uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);    /* And send out the RST packet! */  goto tcp_send_noconn;  /* This label will be jumped to if we matched the incoming packet     with a connection in LISTEN. In that case, we should create a new     connection and send a SYNACK in return. */ found_listen:  /* First we check if there are any connections avaliable. Unused     connections are kept in the same table as used connections, but     unused ones have the tcpstate set to CLOSED. Also, connections in     TIME_WAIT are kept track of and we'll use the oldest one if no     CLOSED connections are found. Thanks to Eddie C. Dost for a very     nice algorithm for the TIME_WAIT search. */  uip_connr = 0;  for(c = 0; c < UIP_CONNS; ++c) {    if(uip_conns[c].tcpstateflags == UIP_CLOSED) {      uip_connr = &uip_conns[c];      break;    }    if(uip_conns[c].tcpstateflags == UIP_TIME_WAIT) {      if(uip_connr == 0 ||	 uip_conns[c].timer > uip_connr->timer) {	uip_connr = &uip_conns[c];      }    }  }  if(uip_connr == 0) {    /* All connections are used already, we drop packet and hope that       the remote end will retransmit the packet at a time when we       have more spare connections. */    UIP_STAT(++uip_stat.tcp.syndrop);    UIP_LOG("tcp: found no unused connections.");    goto drop;  }  uip_conn = uip_connr;    /* Fill in the necessary fields for the new connection. */  uip_connr->rto = uip_connr->timer = UIP_RTO;  uip_connr->sa = 0;  uip_connr->sv = 4;  uip_connr->nrtx = 0;  uip_connr->lport = BUF->destport;  uip_connr->rport = BUF->srcport;  uip_ipaddr_copy(uip_connr->ripaddr, BUF->srcipaddr);  uip_connr->tcpstateflags = UIP_SYN_RCVD;  uip_connr->snd_nxt[0] = iss[0];  uip_connr->snd_nxt[1] = iss[1];  uip_connr->snd_nxt[2] = iss[2];  uip_connr->snd_nxt[3] = iss[3];  uip_connr->len = 1;  /* rcv_nxt should be the seqno from the incoming packet + 1. */  uip_connr->rcv_nxt[3] = BUF->seqno[3];  uip_connr->rcv_nxt[2] = BUF->seqno[2];  uip_connr->rcv_nxt[1] = BUF->seqno[1];  uip_connr->rcv_nxt[0] = BUF->seqno[0];  uip_add_rcv_nxt(1);  /* Parse the TCP MSS option, if present. */  if((BUF->tcpoffset & 0xf0) > 0x50) {    for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {      opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];      if(opt == TCP_OPT_END) {	/* End of options. */	break;      } else if(opt == TCP_OPT_NOOP) {	++c;	/* NOP option. */      } else if(opt == TCP_OPT_MSS &&		uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {	/* An MSS option with the right option length. */	tmp16 = ((u16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) |	  (u16_t)uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + 3 + c];	uip_connr->initialmss = uip_connr->mss =	  tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16;		/* And we are done processing options. */	break;      } else {	/* All other options have a length field, so that we easily	   can skip past them. */	if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) {	  /* If the length field is zero, the options are malformed	     and we don't process them further. */	  break;	}	c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c];      }    }  }    /* Our response will be a SYNACK. */#if UIP_ACTIVE_OPEN tcp_send_synack:  BUF->flags = TCP_ACK;   tcp_send_syn:  BUF->flags |= TCP_SYN;#else /* UIP_ACTIVE_OPEN */ tcp_send_synack:  BUF->flags = TCP_SYN | TCP_ACK;#endif /* UIP_ACTIVE_OPEN */    /* We send out the TCP Maximum Segment Size option with our     SYNACK. */  BUF->optdata[0] = TCP_OPT_MSS;  BUF->optdata[1] = TCP_OPT_MSS_LEN;  BUF->optdata[2] = (UIP_TCP_MSS) / 256;  BUF->optdata[3] = (UIP_TCP_MSS) & 255;  uip_len = UIP_IPTCPH_LEN + TCP_OPT_MSS_LEN;  BUF->tcpoffset = ((UIP_TCPH_LEN + TCP_OPT_MSS_LEN) / 4) << 4;  goto tcp_send;  /* This label will be jumped to if we found an active connection. */ found:  uip_conn = uip_connr;  uip_flags = 0;  /* We do a very naive form of TCP reset processing; we just accept     any RST and kill our connection. We should in fact check if the     sequence number of this reset is wihtin our advertised window     before we accept the reset. */  if(BUF->flags & TCP_RST) {    uip_connr->tcpstateflags = UIP_CLOSED;    UIP_LOG("tcp: got reset, aborting connection.");    uip_flags = UIP_ABORT;    UIP_APPCALL();    goto drop;  }  /* Calculated the length of the data, if the application has sent     any data to us. */  c = (BUF->tcpoffset >> 4) << 2;  /* uip_len will contain the length of the actual TCP data. This is     calculated by subtracing the length of the TCP header (in     c) and the length of the IP header (20 bytes). */  uip_len = uip_len - c - UIP_IPH_LEN;  /* First, check if the sequence number of the incoming packet is     what we're expecting next. If not, we send out an ACK with the     correct numbers in. */  if(!(((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) &&       ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)))) {    if((uip_len > 0 || ((BUF->flags & (TCP_SYN | TCP_FIN)) != 0)) &&       (BUF->seqno[0] != uip_connr->rcv_nxt[0] ||	BUF->seqno[1] != uip_connr->rcv_nxt[1] ||	BUF->seqno[2] != uip_connr->rcv_nxt[2] ||	BUF->seqno[3] != uip_connr->rcv_nxt[3])) {      goto tcp_send_ack;    }  }  /* Next, check if the incoming segment acknowledges any outstanding     data. If so, we update the sequence number, reset the length of     the outstanding data, calculate RTT estimations, and reset the     retransmission timer. */  if((BUF->flags & TCP_ACK) && uip_outstanding(uip_connr)) {    uip_add32(uip_connr->snd_nxt, uip_connr->len);    if(BUF->ackno[0] == uip_acc32[0] &&

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -