⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tinytcp.lst

📁 在89C51上实现TCPIP协议
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V7.07   TINYTCP                                                               04/20/2004 18:04:42 PAGE 1   


C51 COMPILER V7.07, COMPILATION OF MODULE TINYTCP
OBJECT MODULE PLACED IN .\out\tinytcp.obj
COMPILER INVOKED BY: F:\Keil\C51\BIN\C51.EXE src\tinytcp.c BROWSE DEBUG OBJECTEXTEND PRINT(.\lst\tinytcp.lst) OBJECT(.\o
                    -ut\tinytcp.obj)

stmt level    source

   1          /*
   2           * Copyright (C) 2002 by TechiZ. All rights reserved.
   3           *
   4           * This program was written in Korean(Comment and some more).
   5           *
   6           * This program was developed by TechiZ(The Company name).
   7           * TechiZ want to share this program with you who loves the 8051 & the TCP/IP.
   8           * 
   9           * You MUST DOWNLOAD THIS CODE from TechiZ Homepage.
  10           * You DO NOT USE THIS CODE FOR COMMERCIAL PURPOSE.
  11           * This code is ONLY FREE FOR THE STUDY.
  12           * If you want more, send me E-mail.
  13           *
  14           * E-mail: techiz@techiz.com
  15           * ( Subject is : [T89C51RD2 & TinyTCP] bla~ bla bla.... )
  16           *
  17           * Homepage: http://www.techiz.com
  18           * 
  19           * You DO NOT DELETE THIS COPYRIGHT MESSAGE IN THE USING OF THIS CODE.
  20           *
  21           * In the using of this code, TechiZ does NOT GUARANTEE ABOUT WORKING WITHOUT ERROR.
  22           */
  23          
  24          /*
  25           * tinytcp.c - Tiny Implementation of the Transmission Control Protocol
  26           *
  27           * Written March 28, 1986 by Geoffrey Cooper, IMAGEN Corporation.
  28           *
  29           * This code is a small implementation of the TCP and IP protocols, suitable
  30           * for burning into ROM.  The implementation is bare-bones and represents
  31           * two days' coding efforts.  A timer and an ethernet board are assumed.  The
  32           * implementation is based on busy-waiting, but the tcp_handler procedure
  33           * could easily be integrated into an interrupt driven scheme.
  34           *
  35           * IP routing is accomplished on active opens by broadcasting the tcp SYN
  36           * packet when ARP mapping fails.  If anyone answers, the ethernet address
  37           * used is saved for future use.  This also allows IP routing on incoming
  38           * connections.
  39           * 
  40           * The TCP does not implement urgent pointers (easy to add), and discards
  41           * segments that are received out of order.  It ignores the received window
  42           * and always offers a fixed window size on input (i.e., it is not flow
  43           * controlled).
  44           *
  45           * Special care is taken to access the ethernet buffers only in word
  46           * mode.  This is to support boards that only allow word accesses.
  47           *
  48           * Copyright (C) 1986, IMAGEN Corporation
  49           *  "This code may be duplicated in whole or in part provided that [1] there
  50           *   is no commercial gain involved in the duplication, and [2] that this
  51           *   copyright notice is preserved on all copies.  Any other duplication
  52           *   requires written notice of the author (Geoffrey H. Cooper)."
  53           */
  54          
C51 COMPILER V7.07   TINYTCP                                                               04/20/2004 18:04:42 PAGE 2   

  55          #include "tinytcp.h"
  56          #include "include.h"
  57          
  58          #define WARNING
  59          /*  Local IP address  */
  60          //in_HwAddress sin_lclINAddr;
  61          /* IP identification numbers */
  62          
  63          WORD tcp_id;
  64          tcp_Socket *tcp_allsocs;
  65          
  66                          extern void putb_ser(byte byte_data);
  67                          
  68          /* Timer definitions */
  69          #define tcp_RETRANSMITTIME 100     /* interval at which retransmitter is called */
  70          #define tcp_LONGTIMEOUT 3100       /* timeout for opens */
  71          #define tcp_TIMEOUT 1000           /* timeout during a connection */
  72          
  73          #ifdef DEBUG
              /** Primitive logging facility **/
              #define tcp_LOGPACKETS 1        /* log packet headers */
              WORD tcp_logState;
              #endif
  78          void tcp_ProcessData(tcp_Socket *s, tcp_Header *tp, WORD len);
  79          int checksum(WORD *dp, WORD length);
  80          void tcp_Send(tcp_Socket *s);
  81          void tcp_Unthread(tcp_Socket *ds);
  82          void tcp_Handler( in_Header *ip );
  83          void tcp_Flush(tcp_Socket *s);
  84          void tcp_Abort(tcp_Socket *s);
  85          void tcp_Retransmitter(void);
  86          void tcp_Reset( in_Header *ip, tcp_Header *tp );
  87          
  88          in_Header *ip_tcp;
  89          DWORD timeout_tcp, start_tcp;
  90          DWORD x_tcp;
  91          
  92          
  93          //extern WORD i_test;           //      from test_application
  94          //extern BYTE tbuf_test[80];
  95          
  96          extern WORD i_hand;
  97          
  98          extern BYTE *sed_IsPacket(void);
  99          extern BYTE *sed_FormatPacket( BYTE *destEAddr, WORD ethType );
 100          extern BYTE sed_Send( WORD pkLengthInOctets );
 101          extern sar_MapIn2Eth(DWORD ina, eth_HwAddress *ethap );
 102          extern BYTE sed_Receive( BYTE *buf );
 103          extern BYTE sed_CheckPacket( BYTE *BufLocation, WORD expectedType );
 104          extern void sar_CheckPacket(arp_Header *ap);
 105          extern WORD test_dataHandler( tcp_Socket *s, BYTE *dp, WORD len );
 106          extern WORD test_application( void );
 107          extern void print(BYTE *ch);
 108          
 109          /************** Initialize the tcp implementation   ********************************/
 110          
 111          void tcp_Init(void)
 112          {
 113   1              //extern eth_HwAddress sed_lclEthAddr;
 114   1      
 115   1              /* initialize ethernet interface */
 116   1              ethernet_init();
C51 COMPILER V7.07   TINYTCP                                                               04/20/2004 18:04:42 PAGE 3   

 117   1              print("Ethernet initialize..\r\n");
 118   1              //sed_Init();
 119   1      
 120   1              tcp_allsocs = NIL;
 121   1              tcp_id = 0;
 122   1      
 123   1              /* hack - assume the network number */
 124   1              sin_lclINAddr = LOCAL_IP_ADDR;
 125   1              
 126   1      }
 127          
 128          /*
 129           * Actively open a TCP connection to a particular destination.
 130           */
 131          #ifndef WARNING
              //void tcp_Open(tcp_Socket *s, WORD lport, in_HwAddress ina, WORD port, procref datahandler)
              void tcp_Open(tcp_Socket *s, WORD lport, in_HwAddress ina, WORD port,procref datahandler) 
              {
                      //extern eth_HwAddress sed_ethBcastAddr;
              
                      s->state = tcp_StateSYNSENT;
                      s->timeout = tcp_LONGTIMEOUT;
                      if ( lport == 0 ) lport = clock_ValueRough();
                      s->myport = lport;
                      if ( ! sar_MapIn2Eth(ina, &s->hisethaddr[0]) ) {
              #ifdef DEBUG
                              print(" : defaulting ethernet address to broadcast\r\n\r");
              #endif
                              Move(&sed_ethBcastAddr[0], &s->hisethaddr[0], sizeof(eth_HwAddress));
                      }
                      s->hisaddr = ina;
                      s->hisport = port;
                      s->seqnum = 0;
                      s->dataSize = 0;
                      s->flags = tcp_FlagSYN;
                      s->unhappy = true;
                      s->dataHandler = datahandler;
                      s->next = tcp_allsocs;
                      tcp_allsocs = s;
                      tcp_Send(s);
              }
              #endif
 159          
 160          /*
 161           * Passive open: listen for a connection on a particular port
 162           */
 163          //void tcp_Listen(tcp_Socket *s, WORD port, procref datahandler, DWORD timeout)
 164          void tcp_Listen(tcp_Socket *s, WORD port, DWORD timeout)
 165          {
 166   1              s->state = tcp_StateLISTEN;
 167   1              if ( timeout == 0 ) s->timeout = 0x7ffffff; /* forever... */
 168   1              else s->timeout = timeout;
 169   1              s->myport = port;
 170   1              s->hisport = 0;
 171   1              s->seqnum = 0;
 172   1              s->dataSize = 0;
 173   1              s->flags = 0;
 174   1              s->unhappy = 0;
 175   1              //s->dataHandler = datahandler;
 176   1      //      s->dataHandler = (void *)test_dataHandler( (tcp_Socket *)s, (BYTE *)0, (WORD)0);
 177   1              
 178   1              s->next = tcp_allsocs;
C51 COMPILER V7.07   TINYTCP                                                               04/20/2004 18:04:42 PAGE 4   

 179   1              tcp_allsocs = s;
 180   1      }
 181          
 182          /*
 183           * Send a FIN on a particular port -- only works if it is open
 184           */
 185          
 186          #ifndef WARNING 
              void tcp_Close(tcp_Socket *s)
              {
                      if ( s->state == tcp_StateESTAB || s->state == tcp_StateSYNREC ) {
                              s->flags = tcp_FlagACK | tcp_FlagFIN;
                              s->state = tcp_StateFINWT1;
                              s->unhappy = true;
                              tcp_Send(s);
                      }
              }
              #endif
 197          /*
 198           * Abort a tcp connection
 199           */
 200          
 201          void tcp_Abort(tcp_Socket *s)
 202          {
 203   1              if ( s->state != tcp_StateLISTEN && s->state != tcp_StateCLOSED ) {
 204   2                      s->flags = tcp_FlagRST | tcp_FlagACK;
 205   2                      tcp_Send(s);
 206   2              }
 207   1              s->unhappy = 0;
 208   1              s->dataSize = 0;
 209   1              s->state = tcp_StateCLOSED;
 210   1      //      s->dataHandler((tcp_Socket*)s,(BYTE *)0, (WORD *)-1); 
 211   1              DATAHANDLER( (tcp_Socket *)s,(BYTE *)0, (WORD *)0 ) ;
 212   1      
 213   1      //      s->dataHandler(0,0,0); 
 214   1              tcp_Unthread(s);
 215   1      }
 216          
 217          /*
 218           * Retransmitter - called periodically to perform tcp retransmissions
 219           */
 220          void tcp_Retransmitter(void)
 221          {
 222   1              tcp_Socket *s;
 223   1              BOOL x;
 224   1      
 225   1              for ( s = tcp_allsocs; s; s = s->next ) {
 226   2                      x = false;
 227   2                      if ( s->dataSize > 0 || s->unhappy ) {  /* if we didn't received ack( dataSize > 0 ) or unhappy, then re-
             -xmit it */
 228   3                              tcp_Send(s);
 229   3                              x = true;
 230   3                      }
 231   2                      if ( x || s->state != tcp_StateESTAB )
 232   2                              s->timeout -= tcp_RETRANSMITTIME;
 233   2                      if ( s->timeout <= 0 ) {
 234   3                              if ( s->state == tcp_StateTIMEWT ) {
 235   4      #ifdef DEBUG
                                              print("Closed.    \r\n");
              #endif
 238   4                                      s->state = tcp_StateCLOSED;
 239   4      //                              s->dataHandler((tcp_Socket *)s,(BYTE *)0, (WORD *)0);
C51 COMPILER V7.07   TINYTCP                                                               04/20/2004 18:04:42 PAGE 5   

 240   4                                      DATAHANDLER((tcp_Socket *)s,(BYTE *)0, 0);
 241   4                                      tcp_Unthread(s);
 242   4                              } else {
 243   4      #ifdef DEBUG
                                              print(" : Timeout, aborting\r\n");
              #endif
 246   4                                      tcp_Abort(s);
 247   4                              }
 248   3                      }
 249   2              }
 250   1      }
 251          
 252          /*
 253           * Unthread a socket from the socket list, if it's there 
 254           */
 255          void tcp_Unthread(tcp_Socket *ds)
 256          {
 257   1              tcp_Socket *s, **sp;
 258   1      
 259   1              sp = &tcp_allsocs;
 260   1              for (;;) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -