]>
Commit | Line | Data |
---|---|---|
89c4ed63 A |
1 | /* |
2 | Copyright (c) 2000 | |
3 | International Computer Science Institute | |
4 | All rights reserved. | |
5 | ||
6 | This file may contain software code originally developed for the | |
7 | Sting project. The Sting software carries the following copyright: | |
8 | ||
9 | Copyright (c) 1998, 1999 | |
10 | Stefan Savage and the University of Washington. | |
11 | All rights reserved. | |
12 | ||
13 | Redistribution and use in source and binary forms, with or without | |
14 | modification, are permitted provided that the following conditions | |
15 | are met: | |
16 | 1. Redistributions of source code must retain the above copyright | |
17 | notice, this list of conditions and the following disclaimer. | |
18 | 2. Redistributions in binary form must reproduce the above copyright | |
19 | notice, this list of conditions and the following disclaimer in the | |
20 | documentation and/or other materials provided with the distribution. | |
21 | 3. All advertising materials mentioning features or use of this software | |
22 | must display the following acknowledgment: | |
23 | This product includes software developed by ACIRI, the AT&T | |
24 | Center for Internet Research at ICSI (the International Computer | |
25 | Science Institute). This product may also include software developed | |
26 | by Stefan Savage at the University of Washington. | |
27 | 4. The names of ACIRI, ICSI, Stefan Savage and University of Washington | |
28 | may not be used to endorse or promote products derived from this software | |
29 | without specific prior written permission. | |
30 | ||
31 | THIS SOFTWARE IS PROVIDED BY ICSI AND CONTRIBUTORS ``AS IS'' AND | |
32 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
33 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
34 | ARE DISCLAIMED. IN NO EVENT SHALL ICSI OR CONTRIBUTORS BE LIABLE | |
35 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
36 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
37 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
38 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
39 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
40 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
41 | SUCH DAMAGE. | |
42 | */ | |
43 | ||
44 | #ifndef _INET_H_ | |
45 | #define _INET_H_ | |
46 | ||
47 | /* XXX These are machine/compiler dependent */ | |
48 | typedef unsigned char uint8; | |
49 | typedef unsigned short uint16; | |
50 | typedef unsigned int uint32; | |
51 | ||
52 | #define IPPROTOCOL_ICMP 1 | |
53 | #define IPPROTOCOL_IGMP 2 | |
54 | #define IPPROTOCOL_TCP 6 | |
55 | #define IPPROTOCOL_UDP 17 | |
56 | #define IP_DF 0x4000 | |
57 | ||
fa34b6f5 A |
58 | /* ICMP type */ |
59 | #define ICMP_TIMXCEED 11 | |
60 | ||
89c4ed63 A |
61 | /* TCP Flags */ |
62 | #define TCPFLAGS_FIN 0x01 | |
63 | #define TCPFLAGS_SYN 0x02 | |
64 | #define TCPFLAGS_RST 0x04 | |
65 | #define TCPFLAGS_PSH 0x08 | |
66 | #define TCPFLAGS_ACK 0x10 | |
67 | #define TCPFLAGS_URG 0x20 | |
68 | #define TCPFLAGS_ECN_ECHO 0x40 | |
69 | #define TCPFLAGS_CWR 0x80 | |
70 | ||
71 | /* IP Options Parameters -- for IP Options te*/ | |
72 | #define IPOPT_EOL 0x0 | |
73 | #define IPOLEN_EOL 0x1 | |
74 | #define IPOPT_NOP 0x1 | |
75 | #define IPOLEN_NOP 0x1 | |
76 | #define IPOPT_RR 0x7 | |
77 | #define IPOLEN_RR 0x27 /* Maximum length; up to 9 IP addresses */ | |
78 | #define IPOPT_TS 0x44 | |
79 | #define IPOLEN_TS 0x28 | |
80 | #define IPOPT_FAKED 0xff | |
81 | #define IPOLEN_FAKED 0x4 | |
82 | ||
83 | /* TCP Options Parameters */ | |
84 | #define TCPOPT_EOL 0 | |
85 | #define TCPOLEN_EOL 1 | |
86 | #define TCPOPT_NOP 1 | |
87 | #define TCPOLEN_NOP 1 | |
88 | #define TCPOPT_MAXSEG 2 | |
89 | #define TCPOLEN_MAXSEG 4 | |
90 | #define TCPOPT_WINDOW 3 | |
91 | #define TCPOLEN_WINDOW 3 | |
92 | #define TCPOPT_SACK_PERMITTED 4 | |
93 | #define TCPOLEN_SACK_PERMITTED 2 | |
94 | #define TCPOPT_SACK 5 | |
95 | #define TCPOPT_TIMESTAMP 8 | |
96 | #define TCPOLEN_TIMESTAMP 10 | |
97 | #define TCPOPT_FAKED 0x19 | |
98 | #define TCPOLEN_FAKED 0x4 | |
99 | ||
100 | struct IpHeader { | |
101 | uint8 ip_vhl; /* version (4bits) & header length (4 bits) */ | |
102 | uint8 ip_tos; /* type of service */ | |
103 | uint16 ip_len; /* length of IP datagram */ | |
104 | uint16 ip_id; /* identification (for frags) */ | |
105 | uint16 ip_off; /* offset (within a fragment) and flags (3 bits) */ | |
106 | uint8 ip_ttl; /* time to live */ | |
107 | uint8 ip_p; /* protocol number */ | |
108 | uint16 ip_xsum; /* checksum */ | |
109 | uint32 ip_src; /* source address */ | |
110 | uint32 ip_dst; /* destination address */ | |
111 | }; | |
112 | ||
113 | /* Pseudo header for doing TCP checksum calculation */ | |
114 | struct PseudoIpHeader { | |
115 | uint32 filler[2]; | |
116 | uint8 zero; | |
117 | uint8 ip_p; | |
118 | uint16 ip_len; | |
119 | uint32 ip_src; | |
120 | uint32 ip_dst; | |
121 | }; | |
122 | ||
123 | struct TcpHeader { | |
124 | uint16 tcp_sport; /* source port */ | |
125 | uint16 tcp_dport; /* destination port */ | |
126 | uint32 tcp_seq; /* sequence number */ | |
127 | uint32 tcp_ack; /* acknoledgement number */ | |
128 | uint8 tcp_hl; /* header length (4 bits) */ | |
129 | uint8 tcp_flags; /* flags */ | |
130 | uint16 tcp_win; /* advertized window size */ | |
131 | uint16 tcp_xsum; /* checksum */ | |
132 | uint16 tcp_urp; /* urgent pointer */ | |
133 | }; | |
134 | ||
135 | ||
136 | ||
137 | struct IcmpHeader { | |
138 | uint8 icmp_type; /* ICMP message type */ | |
139 | uint8 icmp_code; /* Message code */ | |
140 | uint16 icmp_xsum; /* checksum */ | |
141 | uint16 icmp_unused; /* unused field */ | |
142 | uint16 icmp_mtu; /* MTU of limiting interface */ | |
143 | }; | |
144 | ||
145 | struct IPPacket { | |
146 | struct IpHeader *ip; | |
147 | struct TcpHeader *tcp; | |
148 | }; | |
149 | ||
150 | struct ICMPUnreachableErrorPacket { | |
151 | struct IpHeader ip; | |
152 | struct IcmpHeader icmp; | |
153 | struct IpHeader off_ip; | |
154 | /* 8-first bytes of TCP header */ | |
155 | uint16 tcp_sport; | |
156 | uint16 tcp_dport; | |
157 | uint32 tcp_seqno; | |
158 | }; | |
159 | ||
fa34b6f5 A |
160 | struct ICMPTimeExceededErrorPacket { |
161 | struct IpHeader ip; | |
162 | struct IcmpHeader icmp; | |
163 | struct IpHeader off_ip; | |
164 | /* 8-first bytes of Tcpheader */ | |
165 | uint16 tcp_sport; | |
166 | uint16 tcp_dport; | |
167 | uint32 tcp_seqno; | |
168 | }; | |
169 | ||
89c4ed63 A |
170 | char *InetAddress(uint32 addr); |
171 | ||
172 | uint16 InetChecksum(uint16 *ip_addr, uint16 *tcp_addr, uint16 ip_len, uint16 tcp_len); | |
173 | ||
174 | void PrintTcpPacket(struct IPPacket *p); | |
175 | void PrintICMPUnreachableErrorPacket(struct ICMPUnreachableErrorPacket *p); | |
176 | ||
177 | void WriteIPPacket(struct IPPacket *p, | |
178 | uint32 src, | |
179 | uint32 dst, | |
180 | uint16 sport, | |
181 | uint16 dport, | |
182 | uint32 seq, | |
183 | uint32 ack, | |
184 | uint8 flags, | |
185 | uint16 win, | |
186 | uint16 urp, | |
187 | uint16 datalen, | |
188 | uint16 ip_optlen, | |
189 | uint16 optlen, | |
190 | uint8 iptos, | |
191 | uint8 u4tf); | |
192 | ||
193 | void ReadIPPacket(struct IPPacket *p, uint32 *src, uint32 *dst, | |
194 | uint16 *sport, uint16 *dport, uint32 *seq, uint32 *ack, | |
195 | uint8 *flags, uint16 *win, uint16 *urp, uint16 *datalen, | |
196 | uint16 *ip_optlen, uint16 *optlen); | |
197 | ||
198 | void StorePacket (struct IPPacket *p); | |
199 | ||
200 | struct IPPacket *FindHeaderBoundaries(char *p); | |
201 | ||
202 | struct IPPacket *AllocateIPPacket(int ip_optlen, int tcp_optlen, int datalen, char *str); | |
203 | ||
204 | void FreeIPPacket(struct IPPacket **pkt_p); | |
205 | ||
206 | #endif /* _INET_H_ */ |