forked from seclab-ucr/INTANG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.h
More file actions
154 lines (131 loc) · 3.03 KB
/
protocol.h
File metadata and controls
154 lines (131 loc) · 3.03 KB
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
#ifndef __PROTOCOL_H__
#define __PROTOCOL_H__
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#define MAX_QNAME_LEN 64
#define MAX_REQLINE_LEN 1000
/*
* IP header
*/
struct myiphdr {
u_int8_t ihl:4,
version:4;
u_int8_t tos;
u_int16_t tot_len;
u_int16_t id;
u_int16_t frag_off;
u_int8_t ttl;
u_int8_t protocol;
u_int16_t check;
u_int32_t saddr;
u_int32_t daddr;
};
/*
* UDP header
*/
struct myudphdr {
u_int16_t uh_sport; /* source port */
u_int16_t uh_dport; /* destination port */
u_int16_t uh_ulen; /* udp length */
u_int16_t uh_sum; /* udp checksum */
};
/*
* TCP header.
* Per RFC 793, September, 1981.
*/
struct mytcphdr {
u_int16_t th_sport; /* source port */
u_int16_t th_dport; /* destination port */
u_int32_t th_seq; /* sequence number */
u_int32_t th_ack; /* acknowledgement number */
u_int8_t th_x2:4, /* (unused) */
th_off:4; /* data offset */
u_int8_t th_flags;
u_int16_t th_win; /* window */
u_int16_t th_sum; /* checksum */
u_int16_t th_urp; /* urgent pointer */
};
/*
* UDP/TCP pseudo header
* for cksum computing
*/
struct pseudohdr
{
u_int32_t saddr;
u_int32_t daddr;
u_int8_t zero;
u_int8_t protocol;
u_int16_t length;
};
/*
* DNS header
*/
struct mydnsquery
{
char qname[100];
u_int16_t qtype;
u_int16_t qclass;
};
struct mydnshdr
{
u_int16_t txn_id;
u_int16_t flags;
u_int16_t questions;
u_int16_t answer_rrs;
u_int16_t authority_rrs;
u_int16_t addtional_rrs;
};
struct fourtuple
{
u_int32_t saddr;
u_int32_t daddr;
u_int16_t sport;
u_int16_t dport;
};
struct mypacket
{
unsigned char *data;
unsigned int len;
struct myiphdr *iphdr; // layer 3 IP header
union {
struct mytcphdr *tcphdr; // layer 4 TCP header
struct myudphdr *udphdr; // layer 4 UDP header
};
unsigned char *payload; // layer 4 payload
unsigned int payload_len;
};
struct tcpinfo
{
u_int32_t saddr;
u_int32_t daddr;
u_int16_t sport;
u_int16_t dport;
u_int8_t flags;
u_int32_t seq;
u_int32_t ack;
u_int8_t ttl;
u_int16_t win;
u_int16_t fragoff;
};
static inline struct myiphdr* ip_hdr(unsigned char *pkt_data)
{
return (struct myiphdr*)pkt_data;
}
static inline struct mytcphdr* tcp_hdr(unsigned char *pkt_data)
{
return (struct mytcphdr*)(pkt_data+ip_hdr(pkt_data)->ihl*4);
}
static inline unsigned char* tcp_payload(unsigned char *pkt_data)
{
return pkt_data+ip_hdr(pkt_data)->ihl*4+tcp_hdr(pkt_data)->th_off*4;
}
static inline unsigned char* udp_payload(unsigned char *pkt_data)
{
return pkt_data+ip_hdr(pkt_data)->ihl*4+8;
}
static inline struct myudphdr* udp_hdr(unsigned char *pkt_data)
{
return (struct myudphdr*)(pkt_data+((struct myiphdr*)pkt_data)->ihl*4);
}
#endif