📄 tcp.lst.svn-base
字号:
686 3 else
687 3 {
688 4 /* packet fall outof window, drop it. and send a ack back, because
689 4 this is probably ocurr when our pre send ack is lose.*/
690 4 MemFree(MemHead);
691 4 TCPSendSeg(pTCB,TCPAllocate(0),TCP_ACK);
692 4 return;
693 4 }
694 3 }/* else sequence equal. ok */
695 2 }/* else is syn packet */
696 1
697 1 /* deal incoming packet */
698 1 TCPRecvSeg(pTCB,MemHead);
699 1
700 1 /* if seg in ExceedSeq can receive now? */
701 1 while(pTCB->QExceedSeq != NULL &&
702 1 pTCB->SeqHis == pTCB->QExceedSeq->Seq)
703 1 {
704 2 TCPRecvSeg(pTCB,pTCB->QExceedSeq->MemHead);
705 2 TCPOutQ(&(pTCB->QExceedSeq));
706 2 }
707 1 }
708
709 unsigned char TCPSendEx(struct STCB xdata * pTCB,struct SMemHead xdata *MemHead) reentrant
710 {
711 1 /* if state is "closed, listen, syn recvd, syn sent",
712 1 need connected first */
713 1 if(pTCB->TCPState <= TCP_STATE_SYNSENT)
714 1 {
715 2 MemFree(MemHead);
716 2 return FALSE;
717 2 }
718 1
719 1 /* if unsend queue is empty */
720 1 if(pTCB->QUnSend == NULL)
721 1 {
722 2 /* if this packet send completely? */
723 2 if(TCPSendSegJudgeWnd(pTCB,MemHead) == FALSE)
724 2 {
725 3 /* insert the remain for later sending */
726 3 return TCPInsertQ(&(pTCB->QUnSend),MemHead,0);
727 3 }
728 2 else
729 2 return TRUE;
730 2 }
731 1 else
732 1 return TCPInsertQ(&(pTCB->QUnSend),MemHead,0);
733 1 }
734
735 unsigned char TCPSend(struct STCB xdata * pTCB,void xdata *buf,unsigned int DataSize) reentrant
736 {
C51 COMPILER V7.06 TCP 07/24/2007 16:32:43 PAGE 13
737 1 struct SMemHead xdata *MemHead;
738 1
739 1 /* allocate */
740 1 if((MemHead = TCPAllocate(DataSize)) == NULL)
741 1 return FALSE;
742 1
743 1 /* copy */
744 1 MemCopy(MemHead->pStart,buf,DataSize);
745 1
746 1 return TCPSendEx(pTCB,MemHead);
747 1 }
748
749 unsigned char TCPConnect(struct STCB xdata * pTCB, unsigned short DestIP, unsigned int DestPort,
750 void (code * recv)(void xdata * buf,unsigned int size) reentrant,
751 void (code * close)(struct STCB xdata * pSocket) reentrant) reentrant
752 {
753 1 /* is it in closed state? */
754 1 if(pTCB->TCPState != TCP_STATE_CLOSED)
755 1 return FALSE;
756 1
757 1 /* tcb renew */
758 1 pTCB->IPDest = htonl(DestIP);
759 1 pTCB->PortDest = htons(DestPort);
760 1 pTCB->recv = recv;
761 1 pTCB->close = close;
762 1
763 1 /* send syn */
764 1 if(TCPSendSeg(pTCB,TCPAllocate(0),TCP_SYN) == TRUE)
765 1 {
766 2 pTCB->TCPState = TCP_STATE_SYNSENT;
767 2
768 2 /* wait for establish */
769 2 while(TRUE)
770 2 {
771 3 switch(pTCB->TCPState)
772 3 {
773 4 case TCP_STATE_ESTABLISHED:
774 4 return TRUE;
775 4 case TCP_STATE_CLOSED:
776 4 /* 1. if receive a rst packet from him
777 4 2. retransmittimes exceed sreshold */
778 4 return FALSE;
779 4 }
780 3 }
781 2 }
782 1 else
783 1 return FALSE;
784 1 }
785 /* call this func to innitiate closing a connection. connection
786 will not close unless peer send a fin also.*/
787 void TCPClose(struct STCB xdata *pTCB) reentrant
788 {
789 1 if(pTCB->TCPState != TCP_STATE_CLOSED)
790 1 {
791 2 switch(pTCB->TCPState)
792 2 {
793 3 case TCP_STATE_LISTEN:
794 3 /* close right now */
795 3 pTCB->TCPState = TCP_STATE_CLOSED;
796 3 break;
797 3 case TCP_STATE_SYNRECVD:
798 3 /* close when peer send a fin */
C51 COMPILER V7.06 TCP 07/24/2007 16:32:43 PAGE 14
799 3 pTCB->TCPState = TCP_STATE_FINWAIT1;
800 3 TCPSendSeg(pTCB,TCPAllocate(0),TCP_FIN | TCP_ACK);
801 3 break;
802 3 case TCP_STATE_SYNSENT:
803 3 /* close right now */
804 3 pTCB->TCPState = TCP_STATE_CLOSED;
805 3 TCPRelease(pTCB);
806 3 break;
807 3 case TCP_STATE_ESTABLISHED:
808 3 /* close when peer send a fin */
809 3 pTCB->TCPState = TCP_STATE_FINWAIT1;
810 3 TCPSendSeg(pTCB,TCPAllocate(0),TCP_FIN | TCP_ACK);
811 3 break;
812 3 case TCP_STATE_CLOSEWAIT:
813 3 /* close when lastack time out */
814 3 pTCB->TCPState = TCP_STATE_LASTACK;
815 3 pTCB->LastAckTimer = TCP_LASTACK_TIME_OUT;
816 3 TCPSendSeg(pTCB,TCPAllocate(0),TCP_FIN | TCP_ACK);
817 3 break;
818 3 }
819 2 }
820 1 }
821
822 unsigned char TCPListen(struct STCB xdata *pTCB,unsigned int ScrPort,
823 void (code * accept)(struct STCB xdata *pNewTCB) reentrant) reentrant
824 {
825 1 struct STCB xdata *pTCBt;
826 1
827 1 /* is it in closed state? */
828 1 if(pTCB->TCPState != TCP_STATE_CLOSED)
829 1 return FALSE;
830 1
831 1 ScrPort = htons(ScrPort);
832 1
833 1 /* is there any other socket already listen in this port? */
834 1 for(pTCBt = TCBList; pTCBt != NULL; pTCBt = pTCBt->pNext)
835 1 {
836 2 if(pTCBt->PortScr == ScrPort)
837 2 return FALSE;
838 2 }
839 1
840 1 /* renew tcb */
841 1 pTCB->PortScr = ScrPort;
842 1 pTCB->TCPState = TCP_STATE_LISTEN;
843 1 pTCB->accept = accept;
844 1
845 1 return TRUE;
846 1 }
847
848 struct STCB xdata * TCPSocket(IP_ADDR ScrIP) reentrant
849 {
850 1 struct STCB xdata * pTCB;
851 1 struct STCB xdata * pTCBt;
852 1 unsigned int MaxScrPort; /* max port number in use */
853 1
854 1 /* get a tcb */
855 1 if((pTCB = TCPGetTCB()) == NULL)
856 1 {
857 2 return NULL;
858 2 }
859 1
860 1 /* allocate a scrport. that is number of
C51 COMPILER V7.06 TCP 07/24/2007 16:32:43 PAGE 15
861 1 the highest number of port in use add 1 */
862 1 for(pTCBt = TCBList,MaxScrPort = TCP_DEFAULT_PORT;
863 1 pTCBt != NULL; pTCBt = pTCBt->pNext)
864 1 {
865 2 if(ntohs(pTCBt->PortScr) > MaxScrPort)
866 2 MaxScrPort = ntohs(pTCBt->PortScr);
867 2 }
868 1 pTCB->PortScr = htons((unsigned int)(MaxScrPort + 1));
869 1
870 1 /* other tcb set */
871 1 pTCB->TCPState = TCP_STATE_CLOSED;
872 1 pTCB->IPScr = htonl(ScrIP);
873 1 pTCB->WndMine = MemFreeSize();
874 1 pTCB->bNeedAck = FALSE;
875 1 pTCB->QExceedSeq = NULL;
876 1 pTCB->QUnacked = NULL;
877 1 pTCB->QUnSend = NULL;
878 1
879 1 /* Insert int tcb */
880 1 TCPInsertTCB(pTCB);
881 1
882 1 return pTCB;
883 1 }
884
885 /* reclaim TCB */
886 void TCPAbort(struct STCB xdata *pTCB) reentrant
887 {
888 1 struct STCB xdata *pTCBt;
889 1
890 1 TCPRelease(pTCB);
891 1
892 1 /*
893 1 * search through the tcb list and delete it from list
894 1 */
895 1 /* if it is the hear of the list */
896 1 if(TCBList == pTCB)
897 1 {
898 2 TCBList = TCBList->pNext;
899 2 }
900 1 else
901 1 {
902 2 /* else search start from the second one */
903 2 for(pTCBt = TCBList; pTCBt != NULL; pTCBt = pTCBt->pNext)
904 2 {
905 3 if(pTCBt->pNext == pTCB)
906 3 {
907 4 pTCBt->pNext = pTCB->pNext;
908 4 break;
909 4 }
910 3 }
911 2 }
912 1
913 1 /* reclaim it. link it to TCBFreelist */
914 1 pTCB->pNext = TCBFreeList;
915 1 TCBFreeList = pTCB;
916 1 }
917 /* this func is called by user to allocate a packet and pstart
918 point to TCPplayload */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -