📄 uip.c
字号:
if(uip_outstanding(uip_connr)) { --(uip_connr->timer); if(uip_connr->timer == 0) { if(uip_connr->nrtx == UIP_MAXRTX || ((uip_connr->tcpstateflags == SYN_SENT || uip_connr->tcpstateflags == SYN_RCVD) && uip_connr->nrtx == UIP_MAXSYNRTX)) { uip_connr->tcpstateflags = CLOSED; /* We call UIP_APPCALL() with uip_flags set to UIP_TIMEDOUT to inform the application that the connection has timed out. */ uip_flags = UIP_TIMEDOUT; UIP_APPCALL(); /* We also send a reset packet to the remote host. */ BUF->flags = TCP_RST | TCP_ACK; goto tcp_send_nodata; } /* Exponential backoff. */ uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4? 4: uip_connr->nrtx); ++(uip_connr->nrtx); /* Ok, so we need to retransmit. We do this differently depending on which state we are in. In ESTABLISHED, we call upon the application so that it may prepare the data for the retransmit. In SYN_RCVD, we resend the SYNACK that we sent earlier and in LAST_ACK we have to retransmit our FINACK. */ UIP_STAT(++uip_stat.tcp.rexmit); switch(uip_connr->tcpstateflags & TS_MASK) { case SYN_RCVD: /* In the SYN_RCVD state, we should retransmit our SYNACK. */ goto tcp_send_synack; #if UIP_ACTIVE_OPEN case SYN_SENT: /* In the SYN_SENT state, we retransmit out SYN. */ BUF->flags = 0; goto tcp_send_syn;#endif /* UIP_ACTIVE_OPEN */ case ESTABLISHED: /* In the ESTABLISHED state, we call upon the application to do the actual retransmit after which we jump into the code for sending out the packet (the apprexmit label). */ uip_len = 0; uip_slen = 0; uip_flags = UIP_REXMIT; UIP_APPCALL(); goto apprexmit; case FIN_WAIT_1: case CLOSING: case LAST_ACK: /* In all these states we should retransmit a FINACK. */ goto tcp_send_finack; } } } else if((uip_connr->tcpstateflags & TS_MASK) == ESTABLISHED) { /* If there was no need for a retransmission, we poll the application for new data. */ uip_len = 0; uip_slen = 0; uip_flags = UIP_POLL; UIP_APPCALL(); goto appsend; } } goto drop; }#if UIP_UDP if(flag == UIP_UDP_TIMER) { if(uip_udp_conn->lport != 0) { uip_appdata = &uip_buf[UIP_LLH_LEN + 28]; uip_len = uip_slen = 0; uip_flags = UIP_POLL; UIP_UDP_APPCALL(); goto udp_send; } else { goto drop; } }#endif /* This is where the input processing starts. */ UIP_STAT(++uip_stat.ip.recv); /* Start of IPv4 input header processing code. */ /* Check validity of the IP header. */ if(BUF->vhl != 0x45) { /* IP version and header length. */ UIP_STAT(++uip_stat.ip.drop); UIP_STAT(++uip_stat.ip.vhlerr); UIP_LOG("ip: invalid version or header length."); goto drop; } /* Check the size of the packet. If the size reported to us in uip_len doesn't match the size reported in the IP header, there has been a transmission error and we drop the packet. */ #if UIP_BUFSIZE > 255 if(BUF->len[0] != (uip_len >> 8)) { /* IP length, high byte. */ uip_len = (uip_len & 0xff) | (BUF->len[0] << 8); } if(BUF->len[1] != (uip_len & 0xff)) { /* IP length, low byte. */ uip_len = (uip_len & 0xff00) | BUF->len[1]; }#else if(BUF->len[0] != 0) { /* IP length, high byte. */ UIP_STAT(++uip_stat.ip.drop); UIP_STAT(++uip_stat.ip.hblenerr); UIP_LOG("ip: invalid length, high byte."); goto drop; } if(BUF->len[1] != uip_len) { /* IP length, low byte. */ uip_len = BUF->len[1]; }#endif /* UIP_BUFSIZE > 255 */ if(BUF->ipoffset[0] & 0x3f) { /* We don't allow IP fragments. */ UIP_STAT(++uip_stat.ip.drop); UIP_STAT(++uip_stat.ip.fragerr); UIP_LOG("ip: fragment dropped."); goto drop; } /* If we are configured to use ping IP address configuration and hasn't been assigned an IP address yet, we accept all ICMP packets. */#if UIP_PINGADDRCONF if((uip_hostaddr[0] | uip_hostaddr[1]) == 0) { if(BUF->proto == UIP_PROTO_ICMP) { UIP_LOG("ip: possible ping config packet received."); goto icmp_input; } else { UIP_LOG("ip: packet dropped since no address assigned.."); goto drop; } }#endif /* UIP_PINGADDRCONF */ /* Check if the packet is destined for our IP address. */ if(BUF->destipaddr[0] != uip_hostaddr[0]) { UIP_STAT(++uip_stat.ip.drop); UIP_LOG("ip: packet not for us."); goto drop; } if(BUF->destipaddr[1] != uip_hostaddr[1]) { UIP_STAT(++uip_stat.ip.drop); UIP_LOG("ip: packet not for us."); goto drop; } if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header checksum. */ UIP_STAT(++uip_stat.ip.drop); UIP_STAT(++uip_stat.ip.chkerr); UIP_LOG("ip: bad checksum."); goto drop; } if(BUF->proto == UIP_PROTO_TCP) /* Check for TCP packet. If so, jump to the tcp_input label. */ goto tcp_input;#if UIP_UDP if(BUF->proto == UIP_PROTO_UDP) goto udp_input;#endif /* UIP_UDP */ 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; } icmp_input: 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. */ tmpport = BUF->destipaddr[0]; BUF->destipaddr[0] = BUF->srcipaddr[0]; BUF->srcipaddr[0] = tmpport; tmpport = BUF->destipaddr[1]; BUF->destipaddr[1] = BUF->srcipaddr[1]; BUF->srcipaddr[1] = tmpport; UIP_STAT(++uip_stat.icmp.sent); goto send; /* End of IPv4 input header processing code. */ #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 if(uip_udpchksum() != 0xffff) { UIP_STAT(++uip_stat.udp.drop); UIP_STAT(++uip_stat.udp.chkerr); UIP_LOG("udp: bad checksum."); goto drop; } #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(uip_udp_conn->lport != 0 && UDPBUF->destport == uip_udp_conn->lport && (uip_udp_conn->rport == 0 || UDPBUF->srcport == uip_udp_conn->rport) && BUF->srcipaddr[0] == uip_udp_conn->ripaddr[0] && BUF->srcipaddr[1] == uip_udp_conn->ripaddr[1]) { goto udp_found; } } goto drop; udp_found: uip_len = uip_len - 28; uip_appdata = &uip_buf[UIP_LLH_LEN + 28]; uip_flags = UIP_NEWDATA; uip_slen = 0; UIP_UDP_APPCALL(); udp_send: if(uip_slen == 0) { goto drop; } uip_len = uip_slen + 28;#if UIP_BUFSIZE > 255 BUF->len[0] = (uip_len >> 8); BUF->len[1] = (uip_len & 0xff);#else BUF->len[0] = 0; BUF->len[1] = uip_len;#endif /* UIP_BUFSIZE > 255 */ BUF->proto = UIP_PROTO_UDP; UDPBUF->udplen = HTONS(uip_slen + 8); UDPBUF->udpchksum = 0;#if UIP_UDP_CHECKSUMS /* Calculate UDP checksum. */ UDPBUF->udpchksum = ~(uip_udpchksum()); if(UDPBUF->udpchksum == 0) { UDPBUF->udpchksum = 0xffff; }#endif /* UIP_UDP_CHECKSUMS */ BUF->srcport = uip_udp_conn->lport; BUF->destport = uip_udp_conn->rport; BUF->srcipaddr[0] = uip_hostaddr[0]; BUF->srcipaddr[1] = uip_hostaddr[1]; BUF->destipaddr[0] = uip_udp_conn->ripaddr[0]; BUF->destipaddr[1] = uip_udp_conn->ripaddr[1]; uip_appdata = &uip_buf[UIP_LLH_LEN + 40]; 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]; ++uip_connr) { if(uip_connr->tcpstateflags != CLOSED && BUF->destport == uip_connr->lport && BUF->srcport == uip_connr->rport && BUF->srcipaddr[0] == uip_connr->ripaddr[0] && BUF->srcipaddr[1] == uip_connr->ripaddr[1]) { 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; tmpport = BUF->destport; /* Next, check listening connections. */ for(c = 0; c < UIP_LISTENPORTS; ++c) { if(tmpport == 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 = 40; 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. */ tmpport = BUF->srcport; BUF->srcport = BUF->destport; BUF->destport = tmpport; /* Swap IP addresses. */ tmpport = BUF->destipaddr[0]; BUF->destipaddr[0] = BUF->srcipaddr[0]; BUF->srcipaddr[0] = tmpport; tmpport = BUF->destipaddr[1]; BUF->destipaddr[1] = BUF->srcipaddr[1]; BUF->srcipaddr[1] = tmpport; /* 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 == CLOSED) { uip_connr = &uip_conns[c]; break; } if(uip_conns[c].tcpstateflags == 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->timer = UIP_RTO; uip_connr->nrtx = 0; uip_connr->lport = BUF->destport; uip_connr->rport = BUF->srcport; uip_connr->ripaddr[0] = BUF->srcipaddr[0]; uip_connr->ripaddr[1] = BUF->srcipaddr[1]; uip_connr->tcpstateflags = 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 == 0x00) { /* End of options. */ break; } else if(opt == 0x01) { ++c; /* NOP option. */ } else if(opt == 0x02 && uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0x04) { /* An MSS option with the right option length. */ tmpport = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) | uip_buf[40 + UIP_LLH_LEN + 3 + c]; uip_connr->mss = tmpport > UIP_TCP_MSS? UIP_TCP_MSS: tmpport; /* And we are done processing options. */ break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -