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

📄 udp.lst

📁 C8051单片机的TCPIP源程序 好用.
💻 LST
字号:
C51 COMPILER V8.01   UDP                                                                   11/23/2005 11:07:40 PAGE 1   


C51 COMPILER V8.01, COMPILATION OF MODULE UDP
OBJECT MODULE PLACED IN Udp.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE Udp.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          //-----------------------------------------------------------------------------
   2          // Net UDP.C
   3          //
   4          // This module handles UDP messages
   5          // Refer to RFC 768, 1122
   6          // Also RFC 862 echo, RFC 867 daytime, and RFC 868 time
   7          //-----------------------------------------------------------------------------
   8          #include <string.h>
   9          #include <stdlib.h>
  10          #include "C8051f.h"
  11          #include "net.h"
  12          #include "ip.h"
  13          #include "cksum.h"
  14          #include "serial.h"
  15          #include "icmp.h"
  16          #include "udp.h"
  17          
  18          extern UCHAR idata debug;
  19          extern ULONG code my_ipaddr;
  20          UINT xdata sender_udpport;
  21          static ULONG xdata sender_ipaddr;
  22          extern char xdata text[];
  23          
  24          
  25          //------------------------------------------------------------------------
  26          //      UDP Echo service - see RFC 862
  27          // This simply echos what it received back to the sender
  28          //------------------------------------------------------------------------
  29          void udp_echo_service(UCHAR xdata * inbuf, UINT len)
  30          {
  31   1         if (debug)
  32   1         {
  33   2            serial_send("ECHO: Nr chars = ");
  34   2            memset(text, 0, 10);
  35   2            itoa(len, text, 10);
  36   2            serial_send(text);
  37   2                 serial_send("\r");
  38   2         }
  39   1              udp_send(inbuf, ECHO_PORT, len);      
  40   1      }
  41          
  42          
  43          
  44          //------------------------------------------------------------------------
  45          //      This handles outgoing UDP messages
  46          // See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
  47          //------------------------------------------------------------------------
  48          void udp_send(UCHAR xdata * inbuf, UINT port, UINT len)
  49          {
  50   1              ULONG idata sum;
  51   1              UINT idata result;
  52   1              UCHAR xdata * outbuf;
  53   1              UDP_HEADER xdata * udp;
  54   1         IP_HEADER xdata * ip;
  55   1              
C51 COMPILER V8.01   UDP                                                                   11/23/2005 11:07:40 PAGE 2   

  56   1         // Allocate memory for entire outgoing message including
  57   1         // eth & IP headers. Total ethernet message length is:
  58   1         // 14 byte eth header + 20 byte IP header + 8 byte UDP header
  59   1         // + length of this data 
  60   1         outbuf = (UCHAR xdata *)malloc(42 + len);
  61   1         if (outbuf == NULL)
  62   1         {
  63   2            if (debug) serial_send("UDP: Oops, out of memory\r");
  64   2            return;
  65   2         }
  66   1         
  67   1         udp = (UDP_HEADER xdata *)(outbuf + 34);
  68   1         ip = (IP_HEADER xdata *)(outbuf + 14);
  69   1         
  70   1         // Direct message back to the senders port. 
  71   1              udp->dest_port = sender_udpport;
  72   1              udp->source_port = port;
  73   1              udp->length = 8 + len;
  74   1              udp->checksum = 0;
  75   1                      
  76   1              // Fill in data
  77   1         // Important do not free receive buffer prior to this
  78   1         memcpy(&udp->msg_data, (inbuf + 42), len); 
  79   1              
  80   1              
  81   1              // Compute checksum including 12 bytes of pseudoheader
  82   1              // Must pre-fill 2 items in outbuf to do this
  83   1              // Direct message back to senders ip address
  84   1              ip->dest_ipaddr = sender_ipaddr;
  85   1              ip->source_ipaddr = my_ipaddr;
  86   1               
  87   1              
  88   1              // Sum source_ipaddr, dest_ipaddr, and entire UDP message 
  89   1              sum = (ULONG)cksum(outbuf + 26, 8 + udp->length);
  90   1                              
  91   1              // Add in the rest of pseudoheader which is
  92   1              // zero, protocol id, and UDP length
  93   1              sum += (ULONG)0x0011;
  94   1              sum += (ULONG)udp->length;
  95   1      
  96   1              // In case there was a carry, add it back around
  97   1              result = (UINT)(sum + (sum >> 16));
  98   1              udp->checksum = ~result;
  99   1         if (debug) serial_send("UDP: Sending msg to IP layer\r");
 100   1              ip_send(outbuf, sender_ipaddr, UDP_TYPE, udp->length);
 101   1      }
 102          
 103          
 104          
 105          //------------------------------------------------------------------------
 106          // This handles incoming UDP messages
 107          // See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
 108          //------------------------------------------------------------------------
 109          void udp_rcve(UCHAR xdata * inbuf, UINT len)
 110          {
 111   1         UINT idata result;
 112   1         UDP_HEADER xdata * udp;
 113   1         IP_HEADER xdata * ip;
 114   1         ULONG idata sum;
 115   1            
 116   1              // Total of eth & IP headers = 34 bytes      
 117   1         udp = (UDP_HEADER xdata *)(inbuf + 34);
C51 COMPILER V8.01   UDP                                                                   11/23/2005 11:07:40 PAGE 3   

 118   1         ip = (IP_HEADER xdata *)(inbuf + 14);
 119   1                              
 120   1              // The IP length "len" should be the same as the redundant length
 121   1              // udp->length.  TCP/IP Illustrated, Vol 2, Sect 23.7 says to use the
 122   1              // UDP length, unless IP length < UDP length, in which case the frame
 123   1              // should be discarded.
 124   1         if (len < udp->length) return;
 125   1                         
 126   1              // If the checksum is zero it means that the sender did not compute
 127   1              // it and we should not try to check it.
 128   1              if (udp->checksum == 0)
 129   1              {
 130   2                      if (debug) serial_send("UDP: Sender did not compute cksum\r");
 131   2              }
 132   1              else
 133   1              {
 134   2                      // Compute UDP checksum including 12 byte pseudoheader
 135   2                      // Sum source_ipaddr, dest_ipaddr, and entire UDP message 
 136   2                      sum = (ULONG)cksum(inbuf + 26, 8 + udp->length);
 137   2                      
 138   2                      // Add in the rest of pseudoheader which is
 139   2                      // zero, protocol id, and UDP length
 140   2                      sum += (ULONG)0x0011;     
 141   2                      sum += (ULONG)udp->length;
 142   2      
 143   2                      // In case there was a carry, add it back around
 144   2                      result = (UINT)(sum + (sum >> 16));
 145   2                      
 146   2                      if (result != 0xFFFF)
 147   2                      {
 148   3                              if (debug) serial_send("UDP: Error, bad cksum\r");
 149   3                              return;
 150   3                      }
 151   2              
 152   2                      if (debug) serial_send("UDP: Msg rcvd with good cksum\r");
 153   2              }
 154   1                      
 155   1              // Capture sender's port number and ip_addr
 156   1              // to send return message to
 157   1              sender_udpport = udp->source_port;
 158   1         sender_ipaddr = ip->source_ipaddr;
 159   1                                            
 160   1              // See if any applications are on any ports
 161   1              switch (udp->dest_port)
 162   1              {
 163   2                      case ECHO_PORT:
 164   2                      // Pass it the payload length
 165   2                      udp_echo_service(inbuf, udp->length - 8);
 166   2                      break;
 167   2      
 168   2                      default:
 169   2                      // If no application is registered to handle incoming
 170   2                      // UDP message then send ICMP destination unreachable
 171   2                      dest_unreach_send(inbuf, ip->source_ipaddr);
 172   2                      break;
 173   2              }
 174   1      }
 175          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    922    ----
C51 COMPILER V8.01   UDP                                                                   11/23/2005 11:07:40 PAGE 4   

   CONSTANT SIZE    =    165    ----
   XDATA SIZE       =      6    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----      22
   IDATA SIZE       =   ----      12
   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 + -