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