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

📄 udp.c

📁 44B0+8019系统
💻 C
字号:
//-----------------------------------------------------------------------------
// 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 <string.h>
#include <stdlib.h>
#include "net.h"
#include "ip.h"
#include "cksum.h"
#include "icmp.h"
#include "udp.h"

extern unsigned char debug;
extern unsigned int  my_ipaddr;
unsigned short  int  sender_udpport;
static unsigned int  sender_ipaddr;


unsigned char  udp_rxbuf[256],udp_rxlen;
unsigned char *udp_prxbuf = udp_rxbuf;

//========================================================================
void udp_getHader(void *inbuf,void *h)
{
   unsigned char *pinbuf = (unsigned char*)inbuf;
   unsigned char *pBuf;
   UDP_HEADER *ph = (UDP_HEADER*)h;

   //=========================================
   ph->source_port = get__int16(pinbuf + 34);
   ph->dest_port   = get__int16(pinbuf + 36);
   ph->length      = get__int16(pinbuf + 38);
   ph->checksum    = get__int16(pinbuf + 40);

   ph->msg_data    = pinbuf[42];
}

void udp_setHader(void *inbuf,void *h)
{
   unsigned char *pinbuf = (unsigned char*)inbuf;
   unsigned char *pBuf;
   UDP_HEADER *ph = (UDP_HEADER*)h;

   //=========================================
   set__int16(pinbuf + 34,ph->source_port);
   set__int16(pinbuf + 36,ph->dest_port);
   set__int16(pinbuf + 38,ph->length);
   set__int16(pinbuf + 40,ph->checksum);

   pinbuf[42] = ph->msg_data;
}




//------------------------------------------------------------------------
//	UDP Echo service - see RFC 862
// This simply echos what it received back to the sender
//------------------------------------------------------------------------
void udp_echo_service(unsigned char *inbuf, unsigned short int len)
{
   /*
   if (debug)
   {
      serial_send("ECHO: Nr chars = ");
      memset(text, 0, 10);
      itoa(len, text, 10);
      serial_send(text);
	   serial_send("\r");
   }
   */

	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(unsigned char *inbuf, unsigned short int port, unsigned short int len)
{
	unsigned int sum;
  	unsigned short int result;
  	//unsigned char outbuf[1520];
  	UDP_HEADER *udp,udph;
   IP_HEADER *ip,iph;
   	
   udp = &udph;
   ip = &iph;

   //udp_getHader(outbuf,udp);
   //ip_getHader(outbuf,ip);
    
	udp->dest_port = sender_udpport;
	udp->source_port = port;
	udp->length = 8 + len;
	udp->checksum = 0;
	udp->msg_data = *(inbuf + 42);
		
   udp_setHader(inbuf,udp);
   ip_setHader(inbuf,ip);

   // memcpy(&udp->msg_data, (inbuf + 42), len); 
   //udp_getHader(outbuf,udp);
   //ip_getHader(outbuf,ip);
	
    ip->dest_ipaddr = sender_ipaddr;
	ip->source_ipaddr = my_ipaddr;

   udp_setHader(inbuf,udp);
   ip_setHader(inbuf,ip);
   
   sum = (unsigned int)cksum(inbuf + 26, 8 + udp->length);
			
	sum += (unsigned int)0x0011;
	sum += (unsigned int)udp->length;

	result = (unsigned short int)(sum + (sum >> 16));

   //udp_getHader(outbuf,udp);
   //ip_getHader(outbuf,ip);
   
   udp->checksum = ~result;

   udp_setHader(inbuf,udp);
   ip_setHader(inbuf,ip);

		//sum = (unsigned int)cksum(outbuf + 26,8 + udp->length);
		//sum += (unsigned int)0x0011;     
		//sum += (unsigned int)udp->length;
		//result = (unsigned short int)(sum + (sum >> 16));
        //udp->checksum = ~result;
        //udp_setHader(outbuf,udp);


	ip_send(inbuf, sender_ipaddr, UDP_TYPE, udp->length);
}



//------------------------------------------------------------------------
// This handles incoming UDP messages
// See "TCP/IP Illustrated, Volume 1" Sect 11.1 - 11.3
//------------------------------------------------------------------------

void udp_rcve(unsigned char *inbuf, unsigned short int len)
{
   unsigned short int result;
   UDP_HEADER *udp,udph;
   IP_HEADER *ip,iph;
   unsigned int sum;
   unsigned int i;
  
	// Total of eth & IP headers = 34 bytes      
   udp = &udph;
   ip = &iph;
   udp_getHader(inbuf,udp);
   ip_getHader(inbuf,ip);

   //udp_prxbuf =   (unsigned char *)&udp->msg_data;
   udp_rxlen  = udp->length - 8;
     for( i= 0; i< udp_rxlen; i++)
	 {
		 udp_rxbuf[i] = *(inbuf + 42 + i);
	 }

	// 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.
   if (len < udp->length) 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 = (unsigned int)cksum(inbuf + 26, 8 + udp->length);
		
		// Add in the rest of pseudoheader which is
		// zero, protocol id, and UDP length
		sum += (unsigned int)0x0011;     
		sum += (unsigned int)udp->length;

		// In case there was a carry, add it back around
		result = (unsigned short int)(sum + (sum >> 16));
		
		//if (result != 0xFFFF)
		//{
	  		//if (debug) serial_send("UDP: Error, bad cksum\r");
		//	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;
				      

	// See if any applications are on any ports
	switch (udp->dest_port)
	{
		case ECHO_PORT:
		// Pass it the payload length
		udp_echo_service(inbuf, udp->length - 8);
		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 mytt(unsigned char *key_buf,unsigned char key_count)
{
   //unsigned int  dto_ipaddr = 0xC0A80164; 

    unsigned char mybuf1[256];
	unsigned int i;
  	//UDP_HEADER *udpt,udpht;
    //udpt = &udpht;

   //sender_udpport = 902;
   //sender_ipaddr =  dto_ipaddr;

   for( i= 0; i< key_count; i++)
   {
	   mybuf1[42 + i] = *key_buf++;
   }
				      
           //udpht.msg_data = 0x036;
           //udp_setHader(mybuf1,udpt);
   if ( key_count % 2 == 1){key_count++;}
    udp_send((unsigned char *)mybuf1, 901, key_count);

}

⌨️ 快捷键说明

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