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

📄 udp.c

📁 基于51的单片机的PPP协议实现
💻 C
字号:
/*/////////////////////////////////////////////////////////////////////////////

File Name : UDP.c

Author : Rene Trenado

Location : Motorola Applications Lab, Baja California

Date Created : December 2001

Current Revision : 0.0

Notes : This file contains the code to handle and create UDP transport
        packets.

//////////////////////////////////////////////////////////////////////////////*/

#include "IP.h"
#include "UDP.h"
#include "Ppp.h"

#define UDP_HEADER_LENGTH	8

static WORD UDPLocalPort = 1080;		// Default UDP port (can be set to anything)
static void UDPDefaultCallBack (BYTE *data, BYTE size, DWORD RemoteIP, WORD Port);
static UDPCALLBACK UDPCallback = UDPDefaultCallBack;

UDPDatagram *udp_in;				// Pointer to incoming UDP packet
UDPDatagram *udp_out;				// Pointer for output UDP packet


/***********************************************************************
Function :	UDPSetCallbackProc

Parameters :    Proc: A pointer to a function to callback each time a UDP/IP
		packet is received from the Internet 

Date : 		December 2000

Desc : 		Sets the callback function to call each time a UDP packet is received 
		over the physical interface

***********************************************************************/
void UDPSetCALLBACK (UDPCALLBACK Proc) {
	DisableInterrupts;
	UDPCallback = Proc;
	EnableInterrupts;
}


/***********************************************************************
Function :	UDPDefaultCallBack

Parameters :    None 

Date : 		December 2000

Desc : 		The default callback available after RESET not accesible
		from outside this module

***********************************************************************/
static void UDPDefaultCallBack (BYTE *data, BYTE size, DWORD RemoteIP, WORD Port) {
	
}


/***********************************************************************
Function :	UDPBind

Parameters :    Port: local port to use in UDP packets to transmit 

Date : 		November 2000

Desc : 		Specifies the local port to use for sending UDP 
		packets over IP

***********************************************************************/
void UDPBind (WORD Port) {
	UDPLocalPort = Port;			// Set source UDP port 
}



/**********************************************************************
Function :	UDP_Checksum

Parameters :    udp: A pointer to the start of a udp/ip packet (0x45) 

Date : 		November 2000

Desc : 		Calculates the pseudo-header checksum of a UDP packet

***********************************************************************/
WORD UDP_Checksum (BYTE* udp) {
DWORD Checksum = 0;

	Checksum = IPCheckSum (&udp[12], (8 + udp[25]) >> 1);

	Checksum = ~Checksum +  0x11;
	Checksum += udp [25];
	Checksum = (Checksum >> 16) + (Checksum & 0xFFFF);
	Checksum += (Checksum >> 16);

	return (WORD)~Checksum;
}


/***********************************************************************
Function :	UDPHandler

Parameters :    udp: a pointer to the udp (struct UDPDatagram) packet received

Date : 		November 2000

Desc : 		Invokes the callback proc so the application can handle the
		UDP data received

***********************************************************************/
void UDP_Handler (UDPDatagram *udp) {
	udp_in = udp;
	udp_in->Payload [udp_in->Length - UDP_HEADER_LENGTH] = 0x00;
	UDPCallback (						// Invoque the CALLBACK function
		(BYTE *)udp_in->Payload, 
		udp_in->Length - UDP_HEADER_LENGTH, 
		*((DWORD *)&udp_in->SourceIP),
		udp_in->DestPort);
}



/***********************************************************************
Function :	UDPSendData

Parameters :    BYTE Ip[]:	The IP address of the remote host 
		Port: 		UDP port of the remote host
		Payload:	Data to send
		Size:		Number of bytes to send to remote host 

Date : 		November 2000

Desc : 		Sends data (payload) over UDP to a remote host specified by IP [] using
		Port as the destination UDP port.

***********************************************************************/
void UDPSendData (BYTE Ip[], WORD Port, BYTE* Payload, BYTE size) {
WORD Checksum = 0;

	ip_out->DestAddress [0] = Ip [0];		// Store source and destination
	ip_out->DestAddress [1] = Ip [1];		// IP addresses
	ip_out->DestAddress [2] = Ip [2];
	ip_out->DestAddress [3] = Ip [3];

	ip_out->SourceAddress [0] = IPAddress [0];
	ip_out->SourceAddress [1] = IPAddress [1];
	ip_out->SourceAddress [2] = IPAddress [2];
	ip_out->SourceAddress [3] = IPAddress [3];

	udp_out = (UDPDatagram *) &ip_out->SourceAddress;

							// Insert Data Payload if available as an argument
	if (Payload)
		Move (Payload, &udp_out->Payload[0], size);

	udp_out->Payload[size] = 0x00;			// Format payload as a null terminated string

	if (size % 2) {					// Pad the payload
		size++;
	}

	udp_out->Length = size + UDP_HEADER_LENGTH;	// Calculate the UDP length 
	ip_out->Length = size + UDP_HEADER_LENGTH + 20;	// get IP packet length
	ip_out->Protocol = UDP;				// Protocol set to UDP

	udp_out->SourcePort = htons(UDPLocalPort);	// Set source and destination ports
	udp_out->DestPort = htons(Port);
	udp_out->LengthUpper = 0;			// Packet cannot be longer than 256 bytes
							// (in this implementation)
	udp_out->Checksum = 0;				// Set checksum to 0 
	Checksum = UDP_Checksum ((BYTE *)ip_out);	// Obtain the packet checksum
	udp_out->Checksum = htons (Checksum);			

	IPNetSend (ip_out);				// Send the packet to the IP layer
}


⌨️ 快捷键说明

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