tcpip_stack.h
来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C头文件 代码 · 共 190 行
H
190 行
//-----------------------------------------------------------------------------
// TCPIP_PACKETS
//-----------------------------------------------------------------------------
#ifndef TCPIP_STACK
#define TCPIP_STACK
#include "hal_type.h"
#include "hal_mac.h"
#define IP_ADDR_LEN 4
#define IP_HEADER_LENGTH 20
#define UDP_HEADER_LENGTH 8
#define TCP_HEADER_LENGTH 28
#define ICMP_HEADER_LENGTH 8
//*****************************************************************************
//* Macros
//*****************************************************************************
#define ChangeEndianess_16(f) (f=(((f >> 8) & 0x00FF) + ((f << 8) & 0xFF00)))
#define ChangeEndianess_32(s) (s=(((s >> 24) & 0x000000FF) + ((s >> 8) & 0x0000FF00) + \
((s << 24) & 0xFF000000) + ((s << 8) & 0x00FF0000)))
//*****************************************************************************
//* Ethernet Header data types
//*****************************************************************************
#define ETHER_IP 0x0800
#define ETHER_IP_R 0x8
//*****************************************************************************
//* Ethernet Header
//*****************************************************************************
typedef struct
{
u8 dest_mac[MAC_ADDR_LEN]; // destination MAC address
u8 src_mac[MAC_ADDR_LEN]; // source MAC address
u8 type[2]; // packet type
} ether_hdr;
//*****************************************************************************
//* Ethernet ARP Header
//*****************************************************************************
#define ETHER_ARP 0x0806
typedef struct
{
u16 hw; // Hardware Address space, specifies the type of HW
u16 protocol; // Protocol Address format, the same as type in eth_hdr
u8 mac_hdr_len; // Byte length of each hardware address, for us = 0x6
u8 ip_hdr_len; // Byte length of each protocol address, for IP = 0x4
u16 opcode; // Opcode, ARP request (0x1) or reply (0x2)
} arp_hdr;
#define ARP_REQUEST 0x0100
#define ARP_REPLY 0x0200
#define ARP_HRD_ETHER 0x0100
typedef struct
{
arp_hdr eth_arp_hdr;
u8 srcMAC[MAC_ADDR_LEN]; // Source MAC address
u8 srcIP[4]; // 32 bit IP addresss of the Source
u8 destMAC[MAC_ADDR_LEN]; // Receiver's MAC address
u8 destIP[4]; // 32 bit IP addresss of the Receiver
} ether_arp;
//*****************************************************************************
//* Ethernet IP Header
//*****************************************************************************
typedef struct
{
u8 version; // version - 4 bits, Internet Header Length - 4 bits
u8 service; // Type of Service - 8 bits, quality of service requested
u16 length; // Total Length of the datagram - 16 bits, datagram + header + data
u16 id; // Identification of datagram - 16 bits, unique number set by the sender
u16 offset; // Flags - 3 bits + Fragment Offset - 13 bits
u8 ttl; // Time to Live - 8 bits, in seconds
u8 protocol; // Protocol - 8 bits, protcol which is addressed by this datagram: UDP 0x11, ICMP 0x1, TCP 0x6 ..
u16 hdrchksum; // Header Checksum - 16 bits, does not include data
u8 srcIP[4]; // Source IP Address - 32 bits, sender
u8 destIP[4]; // Destination IP Address - 32 bits, receiver
}ip_hdr;
#define IP_ICMP 0x1
#define IP_TCP 0x6
#define IP_UDP 0x11
//*****************************************************************************
//*****************************************************************************
//* HIGHER PROTOCOLS, ABOVE IP
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//* ICMP IP
//*****************************************************************************
typedef struct
{
u8 type; //specifies the type of the message: 0x0 - 0x18
u8 errcode; //error code reported on this ICMP message
u16 chksum;
u16 id;
u16 sequence;
}icmp_hdr;
#define ICMP_ECHOREPLY 0 /* Echo Reply */
#define ICMP_DEST_UNREACH 3 /* Destination Unreachable */
#define ICMP_SOURCE_QUENCH 4 /* Source Quench */
#define ICMP_REDIRECT 5 /* Redirect (change route) */
#define ICMP_ECHO 8 /* Echo Request */
#define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
#define ICMP_PARAMETERPROB 12 /* Parameter Problem */
#define ICMP_TIMESTAMP 13 /* Timestamp Request */
#define ICMP_TIMESTAMPREPLY 14 /* Timestamp Reply */
#define ICMP_INFO_REQUEST 15 /* Information Request */
#define ICMP_INFO_REPLY 16 /* Information Reply */
#define ICMP_ADDRESS 17 /* Address Mask Request */
#define ICMP_ADDRESSREPLY 18 /* Address Mask Reply */
//*****************************************************************************
//* UDP IP
//*****************************************************************************
typedef struct
{
u16 src_port; // Source port, optional
u16 dst_port; // Destination port
u16 length; // Length of datagram in bytes, incl. header
u16 checksum; // Checksum of psuedo-header
}udp_hdr;
#define SRC_UDP_PORT 0x88A4
#define DST_UDP_PORT 0x7777
//*****************************************************************************
//* TCP IP
//*****************************************************************************
typedef struct
{
u16 src_port; // Source port
u16 dst_port; // Destination port
u32 seq; // Sequence number
u32 ack; // Acknowledgement number
u8 data_off; // Data offset - upper 4 bits + 4 reserved
u8 control; // 2 reserved + 6 Control bits - lower 6 bits
u16 window; // Window, used in ACK segments
u16 checksum; // Checksum
u16 urgent_pt; // Points to the first data octet
u32 options; //may additionally follow
u32 options2;
}tcp_hdr;
// Global TCP/IP stack variables
extern u8 destIP[4];
extern u8 destMAC[6];
extern u8 srcMAC[6];
extern u8 srcIP[4];
extern u8 brcastADD[4];
extern u8 etherIP[2];
extern u8 etherARP[2];
extern u8 httpPORT[2];
//-----------------------------------------------------------------------------
// Functions
//-----------------------------------------------------------------------------
void Receive_ICMP(EthernetFrame *frame);
void Receive_UDP(EthernetFrame *frame);
void Receive_TCP(EthernetFrame *frame);
void Receive_ARP(EthernetFrame *frame);
void Send_ICMP(EthernetFrame *frame);
void Send_UDP(EthernetFrame *frame);
void Send_TCP(EthernetFrame *frame);
void Send_ARP(EthernetFrame *frame);
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?