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

📄 ethernet.h

📁 uCOS-II下实现的lwip协议栈实现Ping功能
💻 H
字号:
#ifndef ETHERNET_H
#define ETHERNET_H

//********************** Including Libs **********************
#include <stdio.h>
#include <string.h>
//#include "comdef.h"

#include "dm9000.h"

#include "cc.h"
//#include "2440addr.h"
#include "config.h"
//#include "2440slib.h"
//#include "2440lib.h"
//#include "uart.h"
//#include "global.h"


//**************** Structure & Type Definition ****************
typedef struct {
	u_char		ip_hl_v;	/* header length and version	*/
	u_char		ip_tos;		/* type of service		*/
	u_short		ip_len;		/* total length			*/
	u_short		ip_id;		/* identification		*/
	u_short		ip_off;		/* fragment offset field	*/
	u_char		ip_ttl;		/* time to live			*/
	u_char		ip_p;		/* protocol			*/
	u_short		ip_sum;		/* checksum			*/
	IPaddr_t	ip_src;		/* Source IP address		*/
	IPaddr_t	ip_dst;		/* Destination IP address	*/
	u_short		udp_src;	/* UDP source port		,如果是ICMP包则为类型和代码*/
	u_short		udp_dst;	/* UDP destination port,	如果是ICMP包为校验和	*/
	u_short		udp_len;	/* Length of UDP packet	,如果是ICMP包为标识符	*/
	u_short		udp_xsum;	/* Checksum	,如果是ICMP包为序号,
	序列号从0开始,每发送一次新的回显请求就加1		*/
} IP_t;

typedef struct
{
	u_char		et_dest[6]; 	/* Destination node	*/
	u_char		et_src[6];		/* Source node */
	u_short		et_protlen;		/* Protocol or length */
	u_char		et_dsap;		/* 802 DSAP	*/
	u_char		et_ssap;		/* 802 SSAP	*/
	u_char		et_ctl;			/* 802 control */
	u_char		et_snap1;		/* SNAP	*/
	u_char		et_snap2;
	u_char		et_snap3;
	u_short		et_prot;		/* 802 protocol	*/
} Ethernet_t;

typedef struct
{
	u_short		ar_hrd;		/* Format of hardware address	*/
	u_short		ar_pro;		/* Format of protocol address	*/
	u_char		ar_hln;		/* Length of hardware address,ethernet :6	*/
	u_char		ar_pln;		/* Length of protocol address,ip:4*/
	u_short		ar_op;		/* Operation			*/

	/*
	 * The remaining fields are variable in size, according to
	 * the sizes above, and are defined as appropriate for
	 * specific hardware/protocol combinations.
	 */
	u_char		ar_data[20];//20bytes
#if 0
	u_char		ar_sha[];	/* Sender hardware address	*/
	u_char		ar_spa[];	/* Sender protocol address	*/
	u_char		ar_tha[];	/* Target hardware address	*/
	u_char		ar_tpa[];	/* Target protocol address	*/
#endif /* 0 */
} ARP_t;

typedef struct icmphdr {
	u_char		type;
	u_char		code;
	u_short		checksum;
	union {
		struct {
			u_short	id;
			u_short	sequence;
		} echo;
		u_long	gateway;
		struct {
			u_short	__unused;
			u_short	mtu;
		} frag;
	} un;
} ICMP_t;

typedef void rxhand_f(u_char *, unsigned, unsigned, unsigned);
typedef void thand_f(void);


//********************** Macro Definition **********************
#define IP_HDR_SIZE_NO_UDP		(sizeof (IP_t) - 8)
#define IP_HDR_SIZE				(sizeof (IP_t))

#define 	ARP_ETHER	    1		// Ethernet  hardware address

#define 	ARPOP_REQUEST	1		// Request  to resolve  address
#define 	ARPOP_REPLY		2		// Response to previous request
#define 	RARPOP_REQUEST	3		// Request  to resolve  address
#define 	RARPOP_REPLY	4		// Response to previous request

#define 	ICMP_ECHO_REPLY		0	// Echo reply
#define 	ICMP_REDIRECT		5	// Redirect (change route)
#define 	ICMP_ECHO_REQUEST	8	// Echo request
#define 	ICMP_REDIR_NET		0	// Redirect Net
#define 	ICMP_REDIR_HOST		1	// Redirect Host

#define 	IPPROTO_ICMP	 	1	// Internet Control Message Protocol
#define 	IPPROTO_UDP			17	// User Datagram Protocol

#define 	NETLOOP_CONTINUE	1
#define 	NETLOOP_RESTART		2
#define 	NETLOOP_SUCCESS		3
#define 	NETLOOP_FAIL		4

#define 	ARP_HDR_SIZE		(8+20)		// Size assuming ethernet

#define NAMESIZE 		16
#define ETHER_HDR_SIZE	14		// Ethernet header size
#define E802_HDR_SIZE	22		// 802 ethernet header size
#define PROT_IP		0x0800		// IP protocol
#define PROT_ARP	0x0806		// IP ARP protocol
#define PROT_RARP	0x8035		// IP ARP protocol
#define __be16_to_cpu(x) __swab16((x))
#define ___ntohs(x) __be16_to_cpu(x)
#define ntohs(x) ___ntohs(x)
#define __swab32(x)    ((u32  )( \
						(((u32  )(x) & (u32  )0x000000ffUL) << 24) | \
						(((u32  )(x) & (u32  )0x0000ff00UL) <<  8) | \
						(((u32  )(x) & (u32  )0x00ff0000UL) >>  8) | \
						(((u32  )(x) & (u32  )0xff000000UL) >> 24) ))

#define __be32_to_cpu(x) __swab32((x))
#define ___ntohl(x) __be32_to_cpu(x)
#define ntohl(x) ___ntohl(x)
#define __swab16(x) \
						((u16_t  )( \
						(((u16_t  )(x) & (u16_t  )0x00ffU) << 8) | \
						(((u16_t  )(x) & (u16_t  )0xff00U) >> 8) ))
#define __cpu_to_be16(x) __swab16((x))
#define ___htons(x) __cpu_to_be16(x)
#define htons(x) ___htons(x)
#define __cpu_to_be32(x) ((__swab32)(x))
#define ___htonl(x) __be32_to_cpu(x)
#define htonl(x) ((unsigned long)___htonl(x))

#define ARP_TIMEOUT			5		// Seconds before trying ARP again
#define ARP_TIMEOUT_COUNT	5		// # of timeouts before giving up


//********************** Local Function **********************
#ifdef __TEMPLATE
	#define EXTERN
#else
	#define EXTERN	extern
#endif

EXTERN void NetTRPackInit(void);
EXTERN u_short NetRxPack (u_char *udpdata, char *srcipaddr,u_short *srcport);
EXTERN int NetTrPack (u_char *udpdata, char *ipstring, int sport, int len);
EXTERN void PingAck (u_char *ipaddr, u_char *etheraddr);
EXTERN void NetReceiveProcess (volatile u_char * pkt, int len);

#undef EXTERN


//********************** Extern Variable **********************
extern volatile u_char * NetTxPacket;	// THE transmit packet
extern volatile u_char * NetRxPackets[PKTBUFSRX];/* Receive packets	*/

extern char MACAddr[];
extern IPaddr_t IPAddr;
extern IPaddr_t GateAddr;
extern IPaddr_t MaskAddr;
extern IPaddr_t ServerAddr;


//********************** Extern Function **********************
u_long get_timer(int t);

//*****************************************************************
#endif

⌨️ 快捷键说明

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