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

📄 udp.c

📁 MC9S12NE64串口与网络通信源代码
💻 C
字号:
 /*****************************************************************************
 *
 * File Name     : udp.c
 *
 * DESCRIPTION   : Used for UDP Protocol  
 *
 * Written by houlei  @2004.10.10 
 * Modified by houlei @2004.12.10  Tsinghua University
 *****************************************************************************/


#include "ethernet.h"
#include "MOTTYPES.h"
#include "ne64api.h"
#include "etherinit.h"
#include "IO_MAP.h"



extern tU16 checksum(tU16 *, tU16);
extern void led_control_application(udp_Header *);
extern eth_HwAddress sed_lclEthAddr;
extern in_HwAddress  sin_lclINAddr;
extern tU08 *sed_FormatPacket( tU08 *, tU08 *, tU16 );
extern tU16 checksum(tU16 *, tU16);
extern tU08 sed_Send( tU16 );
extern tU08  *EtherPtr;
extern tU16 myport;
udpsocket udp_socket;

 /******************************use for udp transmitt*******************************************************/
 void udp_init(void)
 {
 tU08 i;
 for (i=0;i<6;i++)
 {
 udp_socket.des_ethaddr[i]= 0xFF;}        //at the beginning , ip and ethaddress are null
 
 udp_socket.des_ip        = 0xFFFFFFFF;
 udp_socket.des_port      = 1221;
 
// SCI0CR2&=0xDF;                           //DISABLE RIE
 sci0_rie();                 //enable sci0 receive int

 } 
 
//-------------------------------------------------------------------
//when receive a packet from ethernet ,store remote ip ethaddress and port
//------------------------------------------------------------------- 
void udp_store(tU08 * ip)
{
    Move((tU08 *)(&(udp_socket.des_ethaddr)), ip-8 , 6);
    Move((tU08 *)(&(udp_socket.des_ip))     , ip+12, 4);
    Move((tU08 *)(&(udp_socket.des_port))   , ip+20, 2); 
    
    
 }   
 
//-------------------------------------------------------------------
//display chars on screem
//-------------------------------------------------------------------
void Out_chars(tU08 * charpoint, tU16 charlength)
 {  
    tU16 charcounter=charlength;
    for (charcounter;charcounter>0;charcounter--)
    {
      put_char(*(charpoint++));
     }
     _DEBUGNL;
  }
        
//-------------------------------------------------------------------
//process the udp packet
//-------------------------------------------------------------------
 
void udp_receive(in_Header *ip){
	tU16	old_checksum;
	tU16	old_ttl;
	tU16	old_length;
	udp_cks_int	*uci;

	uci = (udp_cks_int *)((tU08 *)ip + 8);
	old_ttl = uci->tuph.mbz;	//backup old ip ttl
	uci->tuph.mbz = 0x00;		//old ip ttl has been destroyed
	uci->tuph.length = uci->uh.length;	//old ip checksum has been destroyed
	old_length = uci->uh.length;		//backup old udp length
	old_checksum = uci->uh.checksum;	//backup old udp checksum
	uci->uh.checksum = 0x0000;		//begin calculate the incoming udp checksum
	if ( (checksum((tU16 *)uci, sizeof(tcpudp_PseudoHeader) + old_length)) == old_checksum ){
	    
		
		uci->tuph.mbz = old_ttl;	//recover old ip ttl
		
		if( uci->uh.dport==myport)
		      {	
	            udp_store((tU08 *)ip);        
		        Debugt("\r\nMessage from ethernet:\r\n");
		        Out_chars(((tU08 *)ip)+28, old_length-8);
           		Debugt("\r\nTo ethernet:\r\n");
              }
        else if(uci->uh.dport==21)
    	        Debugt("\r\nNow begin ftp connect!");
		
	}
}





⌨️ 快捷键说明

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