]> git.saurik.com Git - apple/network_cmds.git/blob - ecnprobe/inet.h
network_cmds-480.tar.gz
[apple/network_cmds.git] / ecnprobe / inet.h
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
58 /* TCP Flags */
59 #define TCPFLAGS_FIN 0x01
60 #define TCPFLAGS_SYN 0x02
61 #define TCPFLAGS_RST 0x04
62 #define TCPFLAGS_PSH 0x08
63 #define TCPFLAGS_ACK 0x10
64 #define TCPFLAGS_URG 0x20
65 #define TCPFLAGS_ECN_ECHO 0x40
66 #define TCPFLAGS_CWR 0x80
67
68 /* IP Options Parameters -- for IP Options te*/
69 #define IPOPT_EOL 0x0
70 #define IPOLEN_EOL 0x1
71 #define IPOPT_NOP 0x1
72 #define IPOLEN_NOP 0x1
73 #define IPOPT_RR 0x7
74 #define IPOLEN_RR 0x27 /* Maximum length; up to 9 IP addresses */
75 #define IPOPT_TS 0x44
76 #define IPOLEN_TS 0x28
77 #define IPOPT_FAKED 0xff
78 #define IPOLEN_FAKED 0x4
79
80 /* TCP Options Parameters */
81 #define TCPOPT_EOL 0
82 #define TCPOLEN_EOL 1
83 #define TCPOPT_NOP 1
84 #define TCPOLEN_NOP 1
85 #define TCPOPT_MAXSEG 2
86 #define TCPOLEN_MAXSEG 4
87 #define TCPOPT_WINDOW 3
88 #define TCPOLEN_WINDOW 3
89 #define TCPOPT_SACK_PERMITTED 4
90 #define TCPOLEN_SACK_PERMITTED 2
91 #define TCPOPT_SACK 5
92 #define TCPOPT_TIMESTAMP 8
93 #define TCPOLEN_TIMESTAMP 10
94 #define TCPOPT_FAKED 0x19
95 #define TCPOLEN_FAKED 0x4
96
97 struct IpHeader {
98 uint8 ip_vhl; /* version (4bits) & header length (4 bits) */
99 uint8 ip_tos; /* type of service */
100 uint16 ip_len; /* length of IP datagram */
101 uint16 ip_id; /* identification (for frags) */
102 uint16 ip_off; /* offset (within a fragment) and flags (3 bits) */
103 uint8 ip_ttl; /* time to live */
104 uint8 ip_p; /* protocol number */
105 uint16 ip_xsum; /* checksum */
106 uint32 ip_src; /* source address */
107 uint32 ip_dst; /* destination address */
108 };
109
110 /* Pseudo header for doing TCP checksum calculation */
111 struct PseudoIpHeader {
112 uint32 filler[2];
113 uint8 zero;
114 uint8 ip_p;
115 uint16 ip_len;
116 uint32 ip_src;
117 uint32 ip_dst;
118 };
119
120 struct TcpHeader {
121 uint16 tcp_sport; /* source port */
122 uint16 tcp_dport; /* destination port */
123 uint32 tcp_seq; /* sequence number */
124 uint32 tcp_ack; /* acknoledgement number */
125 uint8 tcp_hl; /* header length (4 bits) */
126 uint8 tcp_flags; /* flags */
127 uint16 tcp_win; /* advertized window size */
128 uint16 tcp_xsum; /* checksum */
129 uint16 tcp_urp; /* urgent pointer */
130 };
131
132
133
134 struct IcmpHeader {
135 uint8 icmp_type; /* ICMP message type */
136 uint8 icmp_code; /* Message code */
137 uint16 icmp_xsum; /* checksum */
138 uint16 icmp_unused; /* unused field */
139 uint16 icmp_mtu; /* MTU of limiting interface */
140 };
141
142 struct IPPacket {
143 struct IpHeader *ip;
144 struct TcpHeader *tcp;
145 };
146
147 struct ICMPUnreachableErrorPacket {
148 struct IpHeader ip;
149 struct IcmpHeader icmp;
150 struct IpHeader off_ip;
151 /* 8-first bytes of TCP header */
152 uint16 tcp_sport;
153 uint16 tcp_dport;
154 uint32 tcp_seqno;
155 };
156
157 char *InetAddress(uint32 addr);
158
159 uint16 InetChecksum(uint16 *ip_addr, uint16 *tcp_addr, uint16 ip_len, uint16 tcp_len);
160
161 void PrintTcpPacket(struct IPPacket *p);
162 void PrintICMPUnreachableErrorPacket(struct ICMPUnreachableErrorPacket *p);
163
164 void WriteIPPacket(struct IPPacket *p,
165 uint32 src,
166 uint32 dst,
167 uint16 sport,
168 uint16 dport,
169 uint32 seq,
170 uint32 ack,
171 uint8 flags,
172 uint16 win,
173 uint16 urp,
174 uint16 datalen,
175 uint16 ip_optlen,
176 uint16 optlen,
177 uint8 iptos,
178 uint8 u4tf);
179
180 void ReadIPPacket(struct IPPacket *p, uint32 *src, uint32 *dst,
181 uint16 *sport, uint16 *dport, uint32 *seq, uint32 *ack,
182 uint8 *flags, uint16 *win, uint16 *urp, uint16 *datalen,
183 uint16 *ip_optlen, uint16 *optlen);
184
185 void StorePacket (struct IPPacket *p);
186
187 struct IPPacket *FindHeaderBoundaries(char *p);
188
189 struct IPPacket *AllocateIPPacket(int ip_optlen, int tcp_optlen, int datalen, char *str);
190
191 void FreeIPPacket(struct IPPacket **pkt_p);
192
193 #endif /* _INET_H_ */