📄 tcp.lst
字号:
C51 COMPILER V8.08 TCP 11/04/2008 18:45:34 PAGE 8
428 3 // Send header options with the next message
429 3 // Since timestamps are optional and we do not use
430 3 // them, do not have to send them
431 3 // After sending the SYN ACK the client browser will
432 3 // blast me with 2 messages, an ACK, and a HTTP GET
433 3 tcp_send(FLG_SYN | FLG_ACK, 28, nr);
434 3
435 3 // My SYN flag increments my sequence number
436 3 // My sequence number is always updated to point to
437 3 // the next byte to be sent. So the incoming ack
438 3 // number should equal my sequence number
439 3 conxn[nr].my_sequence++;
440 3
441 3 conxn[nr].state = STATE_SYN_RCVD;
442 3 }
443 2 else
444 2 {
445 3 // Sender is out of sync so send reset
446 3 conxn[nr].ipaddr = 0;
447 3 tcp_send(FLG_RST, 20, NO_CONNECTION);
448 3 }
449 2 break;
450 2
451 2
452 2 case STATE_SYN_RCVD:
453 2 // He may already be sending me data - should process it
454 2 conxn[nr].his_sequence += data_len;
455 2 conxn[nr].his_ack = tcp->ack_number;
456 2
457 2 if (tcp->flags & FLG_FIN)
458 2 {
459 3 // His FIN counts as a byte of data
460 3 conxn[nr].his_sequence++;
461 3 tcp_send(FLG_ACK, 20, nr);
462 3 conxn[nr].state = STATE_CLOSE_WAIT;
463 3
464 3 // At this point we would normally wait for the application
465 3 // to close. For now, send FIN right away.
466 3 tcp_send(FLG_FIN | FLG_ACK, 20, nr);
467 3 conxn[nr].my_sequence++; // For my FIN
468 3 conxn[nr].state = STATE_LAST_ACK;
469 3 }
470 2
471 2 // Make sure he is ACKing my SYN
472 2 else if (tcp->ack_number == conxn[nr].my_sequence)
473 2 {
474 3 conxn[nr].state = STATE_ESTABLISHED;
475 3 // If sender sent data ignore it and he will resend
476 3 // Do not send response because we received no
477 3 // data... wait for client to send something to me
478 3 }
479 2 break;
480 2
481 2
482 2 case STATE_ESTABLISHED:
483 2 conxn[nr].his_ack = tcp->ack_number;
484 2
485 2 if (tcp->flags & FLG_FIN)
486 2 {
487 3 // His FIN counts as a byte of data
488 3 conxn[nr].his_sequence++;
489 3 tcp_send(FLG_ACK, 20, nr);
C51 COMPILER V8.08 TCP 11/04/2008 18:45:34 PAGE 9
490 3 conxn[nr].state = STATE_CLOSE_WAIT;
491 3
492 3 // At this point we would normally wait for the application
493 3 // to close. For now, send FIN immediately.
494 3 tcp_send(FLG_FIN | FLG_ACK, 20, nr);
495 3 conxn[nr].my_sequence++; // For my FIN
496 3 conxn[nr].state = STATE_LAST_ACK;
497 3 }
498 2 else if (data_len != 0)
499 2 {
500 3 // Received normal TCP segment from sender with data
501 3 // Send an ACK immediately and pass the data on to
502 3 // the application
503 3 conxn[nr].his_sequence += data_len;
504 3 tcp_send(FLG_ACK, 20, nr); // Send ACK
505 3
506 3
507 3 // Send pointer to start of TCP payload
508 3 // http_server increments my sequence number when
509 3 // sending so don't worry about it here
510 3 result = http_server(inbuf, header_len, nr, 0);
511 3
512 3 // Start timer to close conxn if no activity
513 3 conxn[nr].inactivity = INACTIVITY_TIME;
514 3 }
515 2 break;
516 2
517 2
518 2 case STATE_CLOSE_WAIT:
519 2 // With this code, should not get here
520 2 ;
521 2 break;
522 2
523 2
524 2 case STATE_LAST_ACK:
525 2 conxn[nr].his_ack = tcp->ack_number;
526 2
527 2 // If he ACK's my FIN then close
528 2 if (tcp->ack_number == conxn[nr].my_sequence)
529 2 {
530 3 conxn[nr].state = STATE_CLOSED;
531 3 conxn[nr].ipaddr = 0; // Free up struct area
532 3 just_closed = TRUE;
533 3 }
534 2 break;
535 2
536 2
537 2 case STATE_FIN_WAIT_1:
538 2 // He may still be sending me data - should process it
539 2 conxn[nr].his_sequence += data_len;
540 2 conxn[nr].his_ack = tcp->ack_number;
541 2
542 2 if (tcp->flags & FLG_FIN)
543 2 {
544 3 // His FIN counts as a byte of data
545 3 conxn[nr].his_sequence++;
546 3 tcp_send(FLG_ACK, 20, nr);
547 3
548 3 // If he has ACK'd my FIN then we can close connection
549 3 if (tcp->ack_number == conxn[nr].my_sequence)
550 3 {
551 4 conxn[nr].state = STATE_TIME_WAIT;
C51 COMPILER V8.08 TCP 11/04/2008 18:45:34 PAGE 10
552 4
553 4 conxn[nr].state = STATE_CLOSED;
554 4 conxn[nr].ipaddr = 0; // Free up connection
555 4 just_closed = TRUE;
556 4 }
557 3 else
558 3 {
559 4 // He has not ACK'd my FIN. This happens when there is a simultaneous
560 4 // close - I got his FIN but he has not yet ACK'd my FIN
561 4 conxn[nr].state = STATE_CLOSING;
562 4 }
563 3 }
564 2 else if (tcp->ack_number == conxn[nr].my_sequence)
565 2 {
566 3 // He has ACK'd my FIN but has not sent a FIN yet himself
567 3 conxn[nr].state = STATE_FIN_WAIT_2;
568 3 }
569 2 break;
570 2
571 2
572 2 case STATE_FIN_WAIT_2:
573 2 // He may still be sending me data - should process it
574 2 conxn[nr].his_sequence += data_len;
575 2 conxn[nr].his_ack = tcp->ack_number;
576 2
577 2 if (tcp->flags & FLG_FIN)
578 2 {
579 3 conxn[nr].his_sequence++; // For his FIN flag
580 3 tcp_send(FLG_ACK, 20, nr);
581 3 conxn[nr].state = STATE_TIME_WAIT;
582 3 conxn[nr].state = STATE_CLOSED;
583 3 conxn[nr].ipaddr = 0; // Free up struct area
584 3 just_closed = TRUE;
585 3 }
586 2 break;
587 2
588 2
589 2 case STATE_TIME_WAIT:
590 2 // With this code, should not get here
591 2 ;
592 2 break;
593 2
594 2
595 2 case STATE_CLOSING:
596 2 // Simultaneous close has happened. I have received his FIN
597 2 // but he has not yet ACK'd my FIN. Waiting for ACK.
598 2 // Will not receive data in this state
599 2 conxn[nr].his_ack = tcp->ack_number;
600 2
601 2 if (tcp->ack_number == conxn[nr].my_sequence)
602 2 {
603 3 conxn[nr].state = STATE_TIME_WAIT;
604 3
605 3 // Do not send any response to his ACK
606 3 conxn[nr].state = STATE_CLOSED;
607 3 conxn[nr].ipaddr = 0; // Free up struct area
608 3 just_closed = TRUE;
609 3 }
610 2 break;
611 2
612 2
613 2 default:
C51 COMPILER V8.08 TCP 11/04/2008 18:45:34 PAGE 11
614 2 ;
615 2 break;
616 2 }
617 1
618 1 // This is for debug, to see when conxn closes
619 1 if (just_closed)
620 1 {
621 2 just_closed = FALSE;
622 2
623 2 }
624 1 }
625
626
627
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 3700 ----
CONSTANT SIZE = 10 ----
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 + -