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

📄 udp.c

📁 cf8020+cp2200(网络)的驱动实现
💻 C
字号:
//-----------------------------------------------------------------------------
// Copyright (c) 2002 Jim Brady
// Do not use commercially without author's permission
// Last revised August 2002
// Net UDP.C
//
// This module handles UDP messages
// Refer to RFC 768, 1122
// Also RFC 862 echo, RFC 867 daytime, and RFC 868 time
//-----------------------------------------------------------------------------

#include <stdlib.h>

#include "net.h"
#include "ip.h"
#include "cksum.h"
#include "serial.h"
#include "icmp.h"
#include "udp.h"
#include "utils.h"
#include "tftp.h"

extern UCHAR  debug;
extern ulong  my_ipaddr;
uint  sender_udpport;
static ulong  sender_ipaddr;
extern char  text[];
extern unsigned char TFTP_BUF[];
ulong broadcast_ip = 0xFFFFFFFF;
//void udp_tftp_service(UCHAR  * inbuf, uint len, HANDLE Adapter, ULONG Op);
//------------------------------------------------------------------------
//	UDP Echo service - see RFC 862
// This simply echos what it received back to the sender
//------------------------------------------------------------------------
void udp_echo_service(UCHAR  * inbuf, uint len)
{
   if (debug)
   {
      //serial_send("ECHO: Nr chars = ");
    //  memset(text, 0, 10);
//      itoa(len, text, 10);
      //serial_send(text);
	  //serial_send("\n");
   }
	udp_send(inbuf, ECHO_PORT, len);      
}



//------------------------------------------------------------------------
//	This handles outgoing UDP messages
// See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
//------------------------------------------------------------------------
void udp_send(UCHAR  * inbuf, uint port, uint len)
{
	ulong  sum;
	uint  result;
	UCHAR  * outbuf;
	UDP_HEADER  * udp;
	IP_HEADER  * ip;
	int udplen = 0;
   	
	// Allocate memory for entire outgoing message including
	// eth & IP headers. Total ethernet message length is:
   // 14 byte eth header + 20 byte IP header + 8 byte UDP header
   // + length of this data 
//   outbuf = (UCHAR  *)malloc(42 + len);
   outbuf = inbuf;
   if (outbuf == NULL)
   {
      return;
   }
   
   udp = (UDP_HEADER  *)(outbuf + 34);
   ip = (IP_HEADER  *)(outbuf + 14);
   
   // Direct message back to the senders port. 
   udp->dest_port = sender_udpport;
   udp->source_port = port;
   udp->length = 8 + len;
   udp->checksum = 0;
   
   // Fill in data
   // Important do not free receive buffer prior to this
   memcpy((char*)udp->msg_data, (inbuf + 42), len); 
   
   
   // Compute checksum including 12 bytes of pseudoheader
   // Must pre-fill 2 items in outbuf to do this
   // Direct message back to senders ip address
   ip->dest_ipaddr = sender_ipaddr;
   ip->source_ipaddr = my_ipaddr;
   
#ifdef __LITTLEENDIAN__
   udp->dest_port = ntohs(udp->dest_port);
   udp->source_port = ntohs(udp->source_port);
   udplen = udp->length;
   udp->length = ntohs(udp->length);
   ip->source_ipaddr = ntohs(ip->source_ipaddr);

#endif
   
   
   // Sum source_ipaddr, dest_ipaddr, and entire UDP message 

   sum = (ulong)cksum(outbuf + 26, 8 + udplen);
			
   // Add in the rest of pseudoheader which is
   // zero, protocol id, and UDP length
   sum += (ulong)0x0011;
   sum += (ulong)udplen;
   
   // In case there was a carry, add it back around
   result = (uint)(sum + (sum >> 16));
   udp->checksum = ~result;

  udp->checksum = 0;
//   if (debug) serial_send("UDP: Sending msg to IP layer\r");
#ifdef __LITTLEENDIAN__
   udp->checksum = ntohs(udp->checksum);
   sender_ipaddr = ntohl(sender_ipaddr);
   udp->length = ntohs(udp->length);
#endif

   ip_send(outbuf, sender_ipaddr, UDP_TYPE, udp->length);

#ifdef __LITTLEENDIAN__
   free(outbuf);
#endif
}



//------------------------------------------------------------------------
// This handles incoming UDP messages
// See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
//------------------------------------------------------------------------
void udp_rcve(UCHAR  * inbuf, uint len)
{
   uint  result;
   UDP_HEADER  * udp;
   IP_HEADER  * ip;
   ulong  sum;
   uint udplen;
      
	// Total of eth & IP headers = 34 bytes      
   udp = (UDP_HEADER  *)(inbuf + 34);
   ip = (IP_HEADER  *)(inbuf + 14);
			
	// The IP length "len" should be the same as the redundant length
	// udp->length.  TCP/IP Illustrated, Vol 2, Sect 23.7 says to use the
	// UDP length, unless IP length < UDP length, in which case the frame
   // should be discarded.   
#ifdef __LITTLEENDIAN__
   udp->length = ntohs(udp->length);
#endif
   udplen = udp->length;
   if (len < udplen)
	   return;
		   
	// If the checksum is zero it means that the sender did not compute
	// it and we should not try to check it.
	if (udp->checksum == 0)
	{
//		if (debug) serial_send("UDP: Sender did not compute cksum\r");
	}
	else
	{
		// Compute UDP checksum including 12 byte pseudoheader
		// Sum source_ipaddr, dest_ipaddr, and entire UDP message 
		sum = (ulong)cksum(inbuf + 26, 8 + udplen);
		
		// Add in the rest of pseudoheader which is
		// zero, protocol id, and UDP length
		sum += (ulong)0x0011;     
		sum += (ulong)udplen;

		// In case there was a carry, add it back around
		result = (uint)(sum + (sum >> 16));
		
		if (result != 0xFFFF)
		{
//	  		if (debug) serial_send("UDP: Error, bad cksum\r");
			result = 0;
			return;
		}
	
//		if (debug) serial_send("UDP: Msg rcvd with good cksum\r");
	}
		
	// Capture sender's port number and ip_addr
	// to send return message to
	sender_udpport = udp->source_port;
	sender_ipaddr = ip->source_ipaddr;
	
#ifdef __LITTLEENDIAN__
	udp->dest_port = ntohs(udp->dest_port);
#endif
	// See if any applications are on any ports
	switch (udp->dest_port)
	{
	case ECHO_PORT:
		// Pass it the payload length
#ifdef __LITTLEENDIAN__
		udp->dest_port = ntohs(udp->dest_port);
#endif
		udp_echo_service(inbuf, udplen - 8);
		break;
		
	case TFTP_PORT:
#ifdef __LITTLEENDIAN__
		udp->dest_port = ntohs(udp->dest_port);
#endif
		tftp_rcv_packet(inbuf, udplen);
		break;
		
	default:
		// If no application is registered to handle incoming
		// UDP message then send ICMP destination unreachable
		dest_unreach_send(inbuf, ip->source_ipaddr);
		break;
	}
}

void udp_broadcast(UCHAR  * inbuf, uint port, uint len)
{	

	UCHAR  * outbuf;
	UDP_HEADER  * udp;
	IP_HEADER  * ip;
	int udplen = 0;
   	char test[]={'a', 'b', 'c'};

   outbuf = inbuf;
   if (outbuf == NULL)
   {
      return;
   }
   
   udp = (UDP_HEADER  *)(outbuf + 34);
   ip = (IP_HEADER  *)(outbuf + 14);
   
   // Direct message back to the senders port. 
   udp->dest_port = port;
   udp->source_port = port;
   udp->length = 8 + len;
   udp->checksum = 0;
   
   // Fill in data
   // Important do not free receive buffer prior to this
   memcpy((UCHAR *)udp->msg_data, &test[0], 3);    
   
   // Compute checksum including 12 bytes of pseudoheader
   // Must pre-fill 2 items in outbuf to do this
   // Direct message back to senders ip address
   ip->dest_ipaddr = sender_ipaddr;
   ip->source_ipaddr = my_ipaddr;
   
#ifdef __LITTLEENDIAN__
   udp->dest_port = ntohs(udp->dest_port);
   udp->source_port = ntohs(udp->source_port);
   udplen = udp->length;
   udp->length = ntohs(udp->length);
   ip->source_ipaddr = ntohs(ip->source_ipaddr);

#endif
   
#ifdef __LITTLEENDIAN__
   udp->checksum = ntohs(udp->checksum);
   sender_ipaddr = ntohl(sender_ipaddr);
   udp->length = ntohs(udp->length);
#endif

   ip_send(outbuf, broadcast_ip, UDP_TYPE, udp->length);

#ifdef __LITTLEENDIAN__
   free(outbuf);
#endif
}

⌨️ 快捷键说明

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