📄 tcpip.h
字号:
#ifndef _TCPIP_H
#define _TCPIP_H
#include "global.h"
#include "list.h"
#ifdef TCPIP_GLOBALS
#define TCPIP_EXTERN
#else
#define TCPIP_EXTERN extern
#endif // TCPIP_GLOBALS
//定义路由器A的接口IP信息
#define InF11 128
#define InF12 128
#define InF13 127
#define InF14 1
#define InF21 128
#define InF22 128
#define InF23 128
#define InF24 1
//定义路由器A的接口MAC信息
/*-------------------------------------------------------------------------
*- IP Module configuration
*-------------------------------------------------------------------------*/
//interface1 IP info
#define IP0 (128)
#define IP1 (128)
#define IP2 (126)
#define IP3 (1)
//interface1 MAC info
#define MAC0 (0x00)
#define MAC1 (0x11)
#define MAC2 (0x22)
#define MAC3 (0x33)
#define MAC4 (IP2)
#define MAC5 (IP3)
//interface2 IP info
#define IP4 (128)
#define IP5 (128)
#define IP6 (127)
#define IP7 (1)
//interface2 MAC info
#define MAC6 (0x00)
#define MAC7 (0x11)
#define MAC8 (0x22)
#define MAC9 (0x33)
#define MAC10 (IP6)
#define MAC11 (IP7)
//submask
#define MASK0 (255)
#define MASK1 (255)
#define MASK2 (255)
#define MASK3 (0)
//定义MAC广播地址
#define BMAC0 0xff
#define BMAC1 0xff
#define BMAC2 0xff
#define BMAC3 0xff
#define BMAC4 0xff
#define BMAC5 0xff
#define IP_RETRY_TIMES (0x03)
#define IP_RETRY_INTERVAL (0x0A)
#define RIP_RETRY_INTERVAL (0xff)
#define IP_VALIDCHK_EN (0)
#define ARP_VALIDCHK_EN (0)
#define ICMP_VALIDCHK_EN (0)
#define UDP_VALIDCHK_EN (0)
#define ARP_ENTRY_AMOUNT (8)
#define IP_NI_AMOUNT (4)
#define SOCK_AMOUNT_ALL (0x10)
/*------------------------------------------------------------------------------*
*- Return values
*------------------------------------------------------------------------------*/
#define IP_NO_ERR (0)
#define IP_RETRY_REACHED_LIMIT (0xC1)
#define IP_MAC_NOT_FOUND (0xC0)
#define IP_ADDR_ERR (0xC2)
#define IP_ADDR_ROUTE (0xC3)
#define IP_SOCK_OVERFLOW (0xC4)
#define IP_SOCK_ILLEGAL (0xC5)
#define IP_NO_ROUTE (0xC6)
#define IP_TYPE (0x0800)
#define ARP_TYPE (0x0806)
#define ARP_OP_QUERY (1)
#define ARP_OP_REPLY (2)
#define RARP_OP_QUERY (3)
#define RARP_OP_REPLY (4)
#define ICMP_PROTOCOL (0x01)
#define IGMP_PROTOCOL (0x02)
#define TCP_PROTOCOL (0x06)
#define UDP_PROTOCOL (0x11)
#define PROTOCOL_ETHER (0)
#define PROTOCOL_IP_OR_ARP (14)
#define PROTOCOL_UDP_OR_ICMP (34)
#define PROTOCOL_UDP_DATA (42)
typedef struct {
uint32 ip;
uint8 mac[6];
uint16 state;
} ArpEntry, *PArpEntry;
typedef struct {
uint8* payload;
uint32 src_ip;
uint32 dst_ip;
uint16 src_port;
uint16 dst_port;
uint16 length;
uint16 blank;
uint16 res;
uint8 retrys;
uint8 state;
uint8 dst_mac[6];
uint8 src_mac[6];
struct NI* pni;
Time stamp;
Foo node;
uint8 buff[SOCK_BUFF_SIZE];
}Sock, *PSock;
typedef uint8 (*PInputF)(PSock);
typedef uint8 (*POutputF)(PSock);
typedef struct NI{
uint32 ip;
uint32 mask;
uint32 gate;
uint8 mac[6];
uint8 hw_id;
uint8 arp_idx;
PInputF input;
POutputF output;
ArpEntry arptbl[ARP_ENTRY_AMOUNT];
uint32 state;
}NetIF, *PNetIF;
typedef struct {
uint16 hardware;
uint16 protocol;
uint8 haddrlen;
uint8 paddrlen;
uint16 operation;
uint8 srchaddr[6];
uint32 srcipaddr;
uint8 dsthaddr[6];
uint32 dstipaddr;
} ARPHeader, *PARPHeader;
typedef struct {
uint8 version;
uint8 services;
uint16 length;
uint16 identification;
uint16 fragment;
uint8 livetime;
uint8 protocol;
uint16 checksum;
uint32 srcip;
uint32 dstip;
} IPHeader, *PIPHeader;
typedef struct {
uint8 type;
uint8 code;
uint16 chksum;
} ICMPHeader, *PICMPHeader;
typedef struct {
uint16 srcport;
uint16 dstport;
uint16 length;
uint16 checksum;
} UDPHeader, *PUDPHeader;
TCPIP_EXTERN OS_EVENT* gpSockSem;
TCPIP_EXTERN OS_MEM* gpSockMCB;
TCPIP_EXTERN OS_EVENT* gpTcpIpMsgQ;
TCPIP_EXTERN NetIF gnitbl[IP_NI_AMOUNT];
/*------------------------------------------------------------------------------*
*- Function name: inetaddr -*
*- Parameter: a, 1st octet of ip address -*
*- b, 2nd octet of ip address -*
*- c, 3rd octet of ip address -*
*- d, 4th octet of ip address -*
*- Return value: An ip address of 32 bit width number -*
*- Brief: Compose 4 octect into a 32 bit ip address -*
*------------------------------------------------------------------------------*/
#define InetAddr(A, B, C, D) ((uint32)(A) << 24 |(uint32)(B) << 16 | (uint32)(C) << 8 | (D))
#define BCInetAddr(PNI) (((PNI)->ip & (PNI)->mask) | (~(PNI)->mask))
#define InetNetNum(PNI) ((PNI)->ip & (PNI)->mask)
#define InetHostNum(PNI) ((PNI)->ip & (~(PNI)->mask))
__inline PNetIF NISelect(uint32 ip) {
PNetIF pni;
pni = &gnitbl[IP_NI_AMOUNT - 1];
while(pni != gnitbl) {
if((pni->ip & pni->mask) == (ip & pni->mask)) {
break;
}
pni--;
}
return (pni);
}
__inline PNetIF GetDefNI(void) {
return (gnitbl);
}
/*------------------------------------------------------------------------------*
*- Function name: GetSock
*- Parameter: protocol, indicates which protocol the buffer allocates for
*- length, how long this sock should be initialized
*- dst_port, destination port
*- dst_ip, destination ip address
*- pni, which interface to send this sock
*- Return value: pointer to the new sock struct
*- Brief: Allocate a sock
*------------------------------------------------------------------------------*/
__inline PSock GetSock(uint16 protocol, uint16 length, uint16 dst_port, uint32 dst_ip, PNetIF pni) {
CpuSr sr;
uint8 err;
PSock psock;
SpinLock(sr);
OSSemPend(gpSockSem, 0, &err);
psock = (PSock)OSMemGet(gpSockMCB, &err);
SpinUnLock(sr);
psock->payload = (uint8*)(psock->buff) + protocol;
psock->blank = protocol;
psock->length = length;
psock->dst_ip = dst_ip;
psock->dst_port = dst_port;
psock->pni = pni;
return (psock);
}
/*------------------------------------------------------------------------------*
*- Function name: PutSock -*
*- Parameter: psock, pointer of sock struct to free -*
*- Return value: renturn value of OSMemPut() -*
*- Brief: Free a sock -*
*------------------------------------------------------------------------------*/
__inline void PutSock(PSock psock) {
CpuSr sr;
SpinLock(sr);
OSSemPost(gpSockSem);
OSMemPut(gpSockMCB, (void*)psock);
SpinUnLock(sr);
}
/*------------------------------------------------------------------------------*
*- Function name: ShiftSock -*
*- Parameter: psock, original sock struct pointer -*
*- src, the original layer -*
*- dst, destination layer -*
*- Return value: New In Sock pointer -*
*- Brief: Shift buffer pointer towards start address from original layer to -*
*- destination layer -*
*------------------------------------------------------------------------------*/
__inline void ShiftSock(PSock psock, uint16 src, uint16 dst) {
psock->payload += (dst - src);
psock->length -= (dst - src);
psock->blank += (dst - src);
}
void TcpIpInit(void);
void SockInit(void);
void InitNI(void);
uint8 CreatNI(uint32 ip, uint32 mask, uint32 gate, PInputF input, POutputF output, uint8 mac[]);
uint8 IPFilter(PNetIF pni, uint32 ip, PSock psock);
uint8 AddrResolve(PSock psock, PNetIF pni);
uint8 ArpInput(PSock psock);
uint8 ArpOutput(uint32 ip, uint8 mac[], uint16 op);
uint8 ArpSeek(PSock psock, PNetIF pni);
uint8 ArpAddEntry(uint32 ip, uint8 mac[], PNetIF pni);
uint8 ICMPInput(PSock psock);
uint8 ICMPOutput(uint32 ip, uint8 type, uint8 code, uint16 len, Octet data[]);
uint8 UDPInput(PSock psock);
uint8 UDPOutput(PSock psock);
uint8 IPOutput(PSock psock, uint8 protocol);
uint8 IPInput(PSock psock);
uint16 ChkSum(uint8* payload, uint16 length);
uint8 LLInput(PSock psock);
uint8 LLOutput(PSock psock);
#endif // _TCPIP_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -