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