-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_codec.h
executable file
·161 lines (149 loc) · 4.43 KB
/
ip_codec.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
IP_CODEC.H
Designed by Dennis Tseng on Jan 1, 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
#ifndef _IP_CODEC_H_
#define _IP_CODEC_H_
#define IP_VERSION 4
#define IP_MIN_HDR_LEN 5 /* in 32 bits */
#define MY_IP_TTL 0x40
#define MAC_ADDR_LEN 6
#define IP_ADDR_LEN 4
#define UDP_HDR_LEN 8
#define VLAN_TAG_LEN 4
/* protocol type */
#define FRAME_TYPE_ARP 0x0806
#define FRAME_TYPE_IP 0x0800
#define PROTO_TYPE_ICMP 1
#define PROTO_TYPE_TCP 6
#define PROTO_TYPE_UDP 17
#define OP_ARP_REQ 1
#define OP_ARP_RSP 2
/* frame, ip, udp */
#define ETH_HDR_LEN 12
#define ETH_TYPE_LEN 2
//#define IP_LEN 20
#define UDP_LEN 8
#define BOOT_REQUEST 1
#define BOOT_REPLY 2
#define BOOT_CHADDR_LEN 16
#define BOOT_SNAME_LEN 64
#define BOOT_FILE_LEN 128
#define BOOT_VENDOR_LEN 64
#define BOOT_SRV_PORT 67 //0x43
#define BOOT_CLI_PORT 68 //0x44
#define ICMP_ECHO_REPLY 0x0
#define ICMP_UNREACHABLE 0x3
#define ICMP_ECHO_REQUEST 0x8
typedef enum {
ICMP_NETWORK_UNREACHABLE = 0,
ICMP_HOST_UNREACHABLE = 1,
ICMP_PROTOCOL_UNREACHABLE = 2,
ICMP_PORT_UNREACHABLE = 3,
ICMP_FRAG_NEED_DF_SET = 4,
}ICMP_UNREACH;
/*----------------------------------------------------------
* IP packet
*---------------------------------------------------------*/
struct _IP_PACKET {
struct VER_IHL {
# ifdef _BIG_ENDIAN
U8 ver : 4; /* high nibble */
U8 IHL : 4; /* IP Header Len - lower memory */
# else /*_LIT_ENDIAN */
U8 IHL : 4; /* IP Header Len - lower memory */
U8 ver : 4; /* high nibble */
# endif
} ver_ihl;
U8 tos; /* type of service */
U16 total_len; /* includes header & data */
U16 id;
struct FLAG_FRAG {
# ifdef _BIG_ENDIAN
U16 flag : 3;
U16 frag_off : 13;
# else /*_LIT_ENDIAN */
U16 frag_off : 13;
U16 flag : 3;
# endif
} flag_frag;
U8 ttl;
U8 proto;
U16 head_chksum;
U8 cSA[IP_ADDR_LEN];
U8 cDA[IP_ADDR_LEN];
U8 *opt_pad;
U8 *data;
};
typedef struct _IP_PACKET tIP_PKT;
extern U8 *ENCODE_IP_PKT(tIP_PKT *ip_pkt, U8 *cp);
extern STATUS DECODE_IP_PKT(tIP_PKT *ip_pkt, U8 *data, U16 *len);
/*----------------------------------------------------------
* UDP packet
*---------------------------------------------------------*/
struct UDP_PACKET {
U16 src;
U16 dst;
U16 len; /* includes header & data */
U16 chksum;
U8 *data;
};
typedef struct UDP_PACKET tUDP_PKT;
struct TCP_PACKET {
U16 src;
U16 dst;
U32 seq;
U32 ack;
union flags_field {
U16 flags;
struct flag_bits {
# if defined(_BIG_ENDIAN)
U16 doff:4;
U16 res1:4;
U16 cwr:1;
U16 ece:1;
U16 urg:1;
U16 ack:1;
U16 psh:1;
U16 rst:1;
U16 syn:1;
U16 fin:1;
# else /*_LIT_ENDIAN */
U16 res1:4;
U16 doff:4;
U16 fin:1;
U16 syn:1;
U16 rst:1;
U16 psh:1;
U16 ack:1;
U16 urg:1;
U16 ece:1;
U16 cwr:1;
# endif
}flag_struct;
}flags_union;
U16 window;
U16 chksum;
U16 urg_ptr;
U8 opts[0];
};
typedef struct TCP_PACKET tTCP_PKT;
extern U16 CHKSUM_TCP(tIP_PKT *ip_pkt, tTCP_PKT *tcp_pkt);
extern U16 CHECK_SUM(U32);
extern int DECODE_UDP_PKT(tUDP_PKT *udp_pkt, tIP_PKT *ip_pkt, U16 *data_len);
extern U8 *ENCODE_UDP_PKT(tIP_PKT *ip_pkt, tUDP_PKT *udp_pkt, U8 *mp);
extern void DHCPR_encode_frame(U8 *dm, U8 *sm, U8 *dip, U8 *sip, UINT vid, U8 *im, UINT len, U8 *mu, UINT *mulen);
struct ARP_PACKET {
U16 hardware; /* hardware type */
U16 proto; /* protocol type */
U8 hlen; /* hardware length */
U8 plen; /* protocol length */
U16 op;
U8 sa_mac[MAC_ADDR_LEN]; /* ethernet address */
U8 sa_ip[IP_ADDR_LEN];
U8 da_mac[MAC_ADDR_LEN];
U8 da_ip[IP_ADDR_LEN];
};
typedef struct ARP_PACKET tARP_PKT;
extern U8 *ENCODE_ARP_PKT(tARP_PKT *arp_pkt, U8 *cp);
#endif /* _IP_CODEC_H_ */