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

📄 uip.c

📁 基于uIP框架的8051tcpip源码
💻 C
📖 第 1 页 / 共 3 页
字号:
  c = BUF->seqno[1] != uip_conn->rcv_nxt[1] || c;  c = BUF->seqno[2] != uip_conn->rcv_nxt[2] || c;  c = BUF->seqno[3] != uip_conn->rcv_nxt[3] || c;  if(uip_len > 0 && c) {    goto tcp_send_ack;  }  /* Next, check if the incoming segment acknowledges any outstanding     data. If so, we also reset the retransmission timer. */  c = BUF->ackno[0] == uip_conn->ack_nxt[0];  c = BUF->ackno[1] == uip_conn->ack_nxt[1] && c;  c = BUF->ackno[2] == uip_conn->ack_nxt[2] && c;  c = BUF->ackno[3] == uip_conn->ack_nxt[3] && c;  if(c) {    uip_conn->snd_nxt[0] = uip_conn->ack_nxt[0];    uip_conn->snd_nxt[1] = uip_conn->ack_nxt[1];    uip_conn->snd_nxt[2] = uip_conn->ack_nxt[2];    uip_conn->snd_nxt[3] = uip_conn->ack_nxt[3];    if(uip_conn->tcpstateflags & UIP_OUTSTANDING) {      uip_flags = UIP_ACKDATA;      uip_conn->tcpstateflags &= ~UIP_OUTSTANDING;      UIP_LOG("Acked outstading,");      uip_conn->timer = UIP_RTO;    }  }  /* Do different things depending on in what state the connection is. */  switch(uip_conn->tcpstateflags & TS_MASK) {    /* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not	implemented, since we force the application to close when the	peer sends a FIN (hence the application goes directly from	ESTABLISHED to LAST_ACK). */  case SYN_RCVD:    /* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and       we are waiting for an ACK that acknowledges the data we sent       out the last time. Therefore, we want to have the UIP_ACKDATA       flag set. If so, we enter the ESTABLISHED state. */    if(uip_flags & UIP_ACKDATA) {      uip_conn->tcpstateflags = ESTABLISHED;      UIP_LOG("Established,");      uip_flags = UIP_CONNECTED;      uip_len = 0;      UIP_APPCALL();      goto appsend;    }    goto drop;#if UIP_ACTIVE_OPEN  case SYN_SENT:    /* In SYN_SENT, we wait for a SYNACK that is sent in response to       our SYN. The rcv_nxt is set to sequence number in the SYNACK       plus one, and we send an ACK. We move into the ESTABLISHED       state. */    if((uip_flags & UIP_ACKDATA) &&       BUF->flags == (TCP_SYN | TCP_ACK)) {      /* Parse the TCP MSS option, if present. */      if((BUF->tcpoffset & 0xf0) > 0x50) {	for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {	  opt = uip_buf[40 + UIP_LLH_LEN + c];	  if(opt == 0x00) {	    /* End of options. */	    break;	  } else if(opt == 0x01) {	    ++c;	    /* NOP option. */	  } else if(opt == 0x02 &&		    uip_buf[40 + UIP_LLH_LEN + c + 1] == 0x04) {	    /* An MSS option with the right option length. */	    tmpport = (uip_buf[40 + UIP_LLH_LEN + c + 2] << 8) |	      uip_buf[40 + UIP_LLH_LEN + c + 3];	    uip_conn->mss = tmpport > UIP_TCP_MSS? UIP_TCP_MSS: tmpport;	    /* And we are done processing options. */	    break;	  } else {	    /* All other options have a length field, so that we easily	       can skip past them. */	    c += uip_buf[40 + UIP_LLH_LEN + c + 1];	  }	}      }      uip_conn->tcpstateflags = ESTABLISHED;      uip_conn->rcv_nxt[0] = BUF->seqno[0];      uip_conn->rcv_nxt[1] = BUF->seqno[1];      uip_conn->rcv_nxt[2] = BUF->seqno[2];      uip_conn->rcv_nxt[3] = BUF->seqno[3];      uip_add_rcv_nxt(1);      uip_flags = UIP_CONNECTED | UIP_NEWDATA;      uip_len = 0;      UIP_APPCALL();      goto appsend;    }    goto drop;#endif /* UIP_ACTIVE_OPEN */  case ESTABLISHED:    /* In the ESTABLISHED state, we call upon the application to feed    data into the uip_buf. If the UIP_ACKDATA flag is set, the    application should put new data into the buffer, otherwise we are    retransmitting an old segment, and the application should put that    data into the buffer.    If the incoming packet is a FIN, we should close the connection on    this side as well, and we send out a FIN and enter the LAST_ACK    state. We require that the FIN will have to acknowledge all    outstanding data, otherwise we drop it. */    if(BUF->flags & TCP_FIN) {      uip_add_rcv_nxt(1 + uip_len);      uip_flags = UIP_CLOSE;      uip_len = 0;      UIP_APPCALL();      uip_add_ack_nxt(1);      uip_conn->tcpstateflags = LAST_ACK | UIP_OUTSTANDING;      UIP_LOG("LAST_ACK,");      uip_conn->nrtx = 0;    tcp_send_finack:      BUF->flags = TCP_FIN | TCP_ACK;      goto tcp_send_nodata;    }    /* If uip_len > 0 we have TCP data in the packet, and we flag this       by setting the UIP_NEWDATA flag and update the sequence number       we acknowledge. If the application has stopped the dataflow       using uip_stop(), we must not accept any data packets from the       remote host. */    if(uip_len > 0 && !(uip_conn->tcpstateflags & UIP_STOPPED)) {      uip_flags |= UIP_NEWDATA;      uip_add_rcv_nxt(uip_len);    }    /* If this packet constitutes an ACK for outstanding data (flagged       by the UIP_ACKDATA flag, we should call the application since it       might want to send more data. If the incoming packet had data       from the peer (as flagged by the UIP_NEWDATA flag), the       application must also be notified.       When the application is called, the global variable uip_len       contains the length of the incoming data. The application can       access the incoming data through the global pointer       uip_appdata, which usually points 40 bytes into the uip_buf       array.       If the application wishes to send any data, this data should be       put into the uip_appdata and the length of the data should be       put into uip_len. If the application don't have any data to       send, uip_len must be set to 0. */    if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) {	  UIP_LOG("UIP_NEWDATA and UIP_ACKDATA,");      UIP_APPCALL();    appsend:      if(uip_flags & UIP_ABORT) {		UIP_LOG("CLOSED in appsend.");    	uip_conn->tcpstateflags = CLOSED;	    BUF->flags = TCP_RST | TCP_ACK;	    goto tcp_send_nodata;      }      if(uip_flags & UIP_CLOSE) {	    uip_add_ack_nxt(1);	    uip_conn->tcpstateflags = FIN_WAIT_1 | UIP_OUTSTANDING;	    uip_conn->nrtx = 0;	    BUF->flags = TCP_FIN | TCP_ACK;	    UIP_LOG("FIN_WAIT_1");	    goto tcp_send_nodata;      }      /* If uip_len > 0, the application has data to be sent, in which         case we set the UIP_OUTSTANDING flag in the connection         structure. But we cannot send data if the application already         has outstanding data. */      if(uip_len > 0 &&	     !(uip_conn->tcpstateflags & UIP_OUTSTANDING)) {	     uip_conn->tcpstateflags |= UIP_OUTSTANDING;	     uip_conn->nrtx = 0;	     uip_add_ack_nxt(uip_len);      } else {	     uip_len = 0;      }    apprexmit:      /* If the application has data to be sent, or if the incoming         packet had new data in it, we must send out a packet. */      if(uip_len > 0 || (uip_flags & UIP_NEWDATA)) {	    /* Add the length of the IP and TCP headers. */	    uip_len = uip_len + 40;	    /* We always set the ACK flag in response packets. */	    BUF->flags = TCP_ACK;	    /* Send the packet. */	    goto tcp_send_noopts;      }    }    goto drop;  case LAST_ACK:    /* We can close this connection if the peer has acknowledged our       FIN. This is indicated by the UIP_ACKDATA flag. */    if(uip_flags & UIP_ACKDATA) {      uip_conn->tcpstateflags = CLOSED;      UIP_LOG("CLOSED from LAST_ACK.");    }    break;  case FIN_WAIT_1:    /* The application has closed the connection, but the remote host       hasn't closed its end yet. Thus we do nothing but wait for a       FIN from the other side. */    if(uip_len > 0) {      uip_add_rcv_nxt(uip_len);    }    if(BUF->flags & TCP_FIN) {      if(uip_flags & UIP_ACKDATA) {	   uip_conn->tcpstateflags = TIME_WAIT;	   uip_conn->timer = 0;	   UIP_LOG("TIME_WAIT from FIN_WAIT_1.");      } else {	   uip_conn->tcpstateflags = CLOSING | UIP_OUTSTANDING;	   UIP_LOG("CLOSING.");      }      uip_add_rcv_nxt(1);      goto tcp_send_ack;    } else if(uip_flags & UIP_ACKDATA) {      uip_conn->tcpstateflags = FIN_WAIT_2;      UIP_LOG("FIN_WAIT_2.");      goto drop;    }    if(uip_len > 0) {      goto tcp_send_ack;    }    goto drop;  case FIN_WAIT_2:    if(uip_len > 0) {      uip_add_rcv_nxt(uip_len);    }    if(BUF->flags & TCP_FIN) {      uip_conn->tcpstateflags = TIME_WAIT;      UIP_LOG("TIME_WAIT from FIN_WAIT_2.");      uip_conn->timer = 0;      uip_add_rcv_nxt(1);      goto tcp_send_ack;    }    if(uip_len > 0) {      goto tcp_send_ack;    }    goto drop;  case TIME_WAIT:    goto tcp_send_ack;  case CLOSING:    if(uip_flags & UIP_ACKDATA) {      uip_conn->tcpstateflags = TIME_WAIT;      uip_conn->timer = 0;      UIP_LOG("TIME_WAIT from CLOSING.");    }  }  goto drop;  /* We jump here when we are ready to send the packet, and just want     to set the appropriate TCP sequence numbers in the TCP header. */ tcp_send_ack:  BUF->flags = TCP_ACK; tcp_send_nodata:  uip_len = 40; tcp_send_noopts:  BUF->tcpoffset = 5 << 4; tcp_send:  /* We're done with the input processing. We are now ready to send a     reply. Our job is to fill in all the fields of the TCP and IP     headers before calculating the checksum and finally send the     packet. */  BUF->ackno[0] = uip_conn->rcv_nxt[0];  BUF->ackno[1] = uip_conn->rcv_nxt[1];  BUF->ackno[2] = uip_conn->rcv_nxt[2];  BUF->ackno[3] = uip_conn->rcv_nxt[3];  BUF->seqno[0] = uip_conn->snd_nxt[0];  BUF->seqno[1] = uip_conn->snd_nxt[1];  BUF->seqno[2] = uip_conn->snd_nxt[2];  BUF->seqno[3] = uip_conn->snd_nxt[3];  BUF->srcport  = uip_conn->lport;  BUF->destport = uip_conn->rport;#if BYTE_ORDER == BIG_ENDIAN  BUF->srcipaddr[0] = ((UIP_IPADDR0 << 8) | UIP_IPADDR1);  BUF->srcipaddr[1] = ((UIP_IPADDR2 << 8) | UIP_IPADDR3);#else  BUF->srcipaddr[0] = ((UIP_IPADDR1 << 8) | UIP_IPADDR0);  BUF->srcipaddr[1] = ((UIP_IPADDR3 << 8) | UIP_IPADDR2);#endif /* BYTE_ORDER == BIG_ENDIAN */  BUF->destipaddr[0] = uip_conn->ripaddr[0];  BUF->destipaddr[1] = uip_conn->ripaddr[1];  if(uip_conn->tcpstateflags & UIP_STOPPED) {    /* If the connection has issued uip_stop(), we advertise a zero       window so that the remote host will stop sending data. */    BUF->wnd[0] = BUF->wnd[1] = 0;  } else {#if (UIP_TCP_MSS) >= 256    BUF->wnd[0] = (uip_conn->mss >> 8);#else    BUF->wnd[0] = 0;#endif /* UIP_MSS */    BUF->wnd[1] = (uip_conn->mss & 0xff);  } tcp_send_noconn:  BUF->vhl = 0x45;  BUF->tos = 0;  BUF->ipoffset[0] = BUF->ipoffset[1] = 0;  BUF->ttl  = UIP_TTL;  BUF->proto = IP_PROTO_TCP;#if UIP_BUFSIZE >= 256  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 */  ++ipid;  BUF->ipid[0] = ipid >> 8;  BUF->ipid[1] = ipid & 0xff;  /* Calculate IP and TCP checksums. */  BUF->ipchksum = 0;  BUF->ipchksum = ~(uip_ipchksum());  BUF->tcpchksum = 0;  BUF->tcpchksum = ~(uip_tcpchksum());  UIP_STAT(++uip_stat.tcp.sent); send:  UIP_STAT(++uip_stat.ip.sent);  /* The data that should be sent is not present in the uip_buf, and     the length of the data is in the variable uip_len. It is not our     responsibility to do the actual sending of the data however. That     is taken care of by the wrapper code, and only if uip_len > 0. */  return; drop:  uip_len = 0;  return;}/*-----------------------------------------------------------------------------------*/

⌨️ 快捷键说明

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