📄 tcp.lst
字号:
458 3 // Since timestamps are optional and we do not use
459 3 // them, do not have to send them
460 3 // After sending the SYN ACK the client browser will
461 3 // blast me with 2 messages, an ACK, and a HTTP GET
462 3 tcp_send(FLG_SYN | FLG_ACK, 28, nr);
463 3
464 3 // My SYN flag increments my sequence number
465 3 // My sequence number is always updated to point to
466 3 // the next byte to be sent. So the incoming ack
467 3 // number should equal my sequence number
468 3 conxn[nr].my_sequence++;
469 3
470 3 conxn[nr].state = STATE_SYN_RCVD;
471 3 if (debug) SendCommString("TCP: Entered SYN RCVD state\r");
472 3 }
473 2 else
474 2 {
475 3 // Sender is out of sync so send reset
476 3 conxn[nr].ipaddr = 0;
477 3 tcp_send(FLG_RST, 20, NO_CONNECTION);
478 3 }
479 2 break;
480 2
481 2
482 2 case STATE_SYN_RCVD://
483 2 // He may already be sending me data - should process it
484 2 conxn[nr].his_sequence += data_len;
485 2 conxn[nr].his_ack = tcp->ack_number;
486 2
487 2 if (tcp->flags & FLG_FIN)
488 2 {
489 3 // His FIN counts as a byte of data
C51 COMPILER V7.20 TCP 03/07/2006 14:49:13 PAGE 9
490 3 conxn[nr].his_sequence++;
491 3 tcp_send(FLG_ACK, 20, nr);
492 3 conxn[nr].state = STATE_CLOSE_WAIT;
493 3 if (debug) SendCommString("TCP: Entered CLOSE_WAIT state\r");
494 3
495 3 // At this point we would normally wait for the application
496 3 // to close. For now, send FIN right away.
497 3 tcp_send(FLG_FIN | FLG_ACK, 20, nr);
498 3 conxn[nr].my_sequence++; // For my FIN
499 3 conxn[nr].state = STATE_LAST_ACK;
500 3 if (debug) SendCommString("TCP: Entered LAST ACK state\r");
501 3 }
502 2
503 2 // Make sure he is ACKing my SYN
504 2 else if (tcp->ack_number == conxn[nr].my_sequence)
505 2 {
506 3 conxn[nr].state = STATE_ESTABLISHED;
507 3 if (debug) SendCommString("TCP: Entered ESTABLISHED state\r");
508 3 // If sender sent data ignore it and he will resend
509 3 // Do not send response because we received no
510 3 // data... wait for client to send something to me
511 3 }
512 2 break;
513 2
514 2 case STATE_ESTABLISHED: //
515 2 conxn[nr].his_ack = tcp->ack_number;
516 2
517 2 if (tcp->flags & FLG_FIN)
518 2 {
519 3 // His FIN counts as a byte of data
520 3 conxn[nr].his_sequence++;
521 3 tcp_send(FLG_ACK, 20, nr);
522 3 conxn[nr].state = STATE_CLOSE_WAIT;
523 3 if (debug) SendCommString("TCP: Entered CLOSE_WAIT state\r");
524 3
525 3 // At this point we would normally wait for the application
526 3 // to close. For now, send FIN immediately.
527 3 tcp_send(FLG_FIN | FLG_ACK, 20, nr);
528 3 conxn[nr].my_sequence++; // For my FIN
529 3 conxn[nr].state = STATE_LAST_ACK;
530 3 if (debug) SendCommString("TCP: Entered LAST ACK state\r");
531 3 }
532 2 else if (data_len != 0)
533 2 {
534 3 // Received normal TCP segment from sender with data
535 3 // Send an ACK immediately and pass the data on to
536 3 // the application
537 3 conxn[nr].his_sequence += data_len;
538 3 tcp_send(FLG_ACK, 20, nr); // Send ACK
539 3
540 3
541 3 // Send pointer to start of TCP payload
542 3 // http_server increments my sequence number when
543 3 // sending so don't worry about it here
544 3 result = http_server(inbuf, header_len, nr, 0);
545 3
546 3 // Start timer to close conxn if no activity
547 3 conxn[nr].inactivity = INACTIVITY_TIME;
548 3 }
549 2 break;
550 2
551 2
C51 COMPILER V7.20 TCP 03/07/2006 14:49:13 PAGE 10
552 2 case STATE_CLOSE_WAIT://
553 2 // With this code, should not get here
554 2 if (debug) SendCommString("TCP: Oops! Rcvd unexpected message\r");
555 2 break;
556 2
557 2
558 2 case STATE_LAST_ACK:
559 2 conxn[nr].his_ack = tcp->ack_number;
560 2
561 2 // If he ACK's my FIN then close
562 2 if (tcp->ack_number == conxn[nr].my_sequence)
563 2 {
564 3 conxn[nr].state = STATE_CLOSED;
565 3 conxn[nr].ipaddr = 0; // Free up struct area
566 3 just_closed = TRUE;
567 3 }
568 2 break;
569 2
570 2
571 2 case STATE_FIN_WAIT_1:
572 2 // He may still be sending me data - should process it
573 2 conxn[nr].his_sequence += data_len;
574 2 conxn[nr].his_ack = tcp->ack_number;
575 2
576 2 if (tcp->flags & FLG_FIN)
577 2 {
578 3 // His FIN counts as a byte of data
579 3 conxn[nr].his_sequence++;
580 3 tcp_send(FLG_ACK, 20, nr);
581 3
582 3 // If he has ACK'd my FIN then we can close connection
583 3 if (tcp->ack_number == conxn[nr].my_sequence)
584 3 {
585 4 conxn[nr].state = STATE_TIME_WAIT;
586 4 if (debug) SendCommString("TCP: Entered TIME_WAIT state\r");
587 4
588 4 conxn[nr].state = STATE_CLOSED;
589 4 conxn[nr].ipaddr = 0; // Free up connection
590 4 just_closed = TRUE;
591 4 }
592 3 else
593 3 {
594 4 // He has not ACK'd my FIN. This happens when there is a simultaneous
595 4 // close - I got his FIN but he has not yet ACK'd my FIN
596 4 conxn[nr].state = STATE_CLOSING;
597 4 if (debug) SendCommString("TCP: Entered CLOSING state\r");
598 4 }
599 3 }
600 2 else if (tcp->ack_number == conxn[nr].my_sequence)
601 2 {
602 3 // He has ACK'd my FIN but has not sent a FIN yet himself
603 3 conxn[nr].state = STATE_FIN_WAIT_2;
604 3 if (debug) SendCommString("TCP: Entered FIN_WAIT_2 state\r");
605 3 }
606 2 break;
607 2
608 2
609 2 case STATE_FIN_WAIT_2:
610 2 // He may still be sending me data - should process it
611 2 conxn[nr].his_sequence += data_len;
612 2 conxn[nr].his_ack = tcp->ack_number;
613 2
C51 COMPILER V7.20 TCP 03/07/2006 14:49:13 PAGE 11
614 2 if (tcp->flags & FLG_FIN)
615 2 {
616 3 conxn[nr].his_sequence++; // For his FIN flag
617 3 tcp_send(FLG_ACK, 20, nr);
618 3 conxn[nr].state = STATE_TIME_WAIT;
619 3 if (debug) SendCommString("TCP: Entered TIME_WAIT state\r");
620 3 conxn[nr].state = STATE_CLOSED;
621 3 conxn[nr].ipaddr = 0; // Free up struct area
622 3 just_closed = TRUE;
623 3 }
624 2 break;
625 2
626 2
627 2 case STATE_TIME_WAIT:
628 2 // With this code, should not get here
629 2 if (debug) SendCommString("TCP: Oops! In TIME_WAIT state\r");
630 2 break;
631 2
632 2
633 2 case STATE_CLOSING://关闭状态机
634 2 // Simultaneous close has happened. I have received his FIN
635 2 // but he has not yet ACK'd my FIN. Waiting for ACK.
636 2 // Will not receive data in this state
637 2 conxn[nr].his_ack = tcp->ack_number;
638 2
639 2 if (tcp->ack_number == conxn[nr].my_sequence)
640 2 {
641 3 conxn[nr].state = STATE_TIME_WAIT;
642 3 if (debug) SendCommString("TCP: Entered TIME_WAIT state\r");
643 3
644 3 // Do not send any response to his ACK
645 3 conxn[nr].state = STATE_CLOSED;
646 3 conxn[nr].ipaddr = 0; // Free up struct area
647 3 just_closed = TRUE;
648 3 }
649 2 break;
650 2
651 2
652 2 default:
653 2 if (debug) SendCommString("TCP: Error, no handler\r");
654 2 break;
655 2 }
656 1
657 1 // This is for debug, to see when conxn closes
658 1 if (just_closed)
659 1 {
660 2 just_closed = FALSE;
661 2 if (debug)
662 2 {
663 3 SendCommString("TCP: Closed connection ");
664 3 memset(text, 0, 10);
665 3 itoa((UINT)nr, text, 10);
666 3 SendCommString(text);
667 3 SendCommString("\r");
668 3 }
669 2 }
670 1 }
671
672
673
C51 COMPILER V7.20 TCP 03/07/2006 14:49:13 PAGE 12
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 2978 ----
CONSTANT SIZE = 838 ----
XDATA SIZE = 231 ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 19
IDATA SIZE = 6 25
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -