]> git.saurik.com Git - apple/network_cmds.git/blob - ecnprobe/support.c
network_cmds-543.200.16.tar.gz
[apple/network_cmds.git] / ecnprobe / support.c
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 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/time.h>
46 #include <string.h>
47 #include <stdio.h>
48 #include <unistd.h>
49 #include "base.h"
50 #include "inet.h"
51 #include "session.h"
52 #include "capture.h"
53 #include "support.h"
54
55 extern struct TcpSession session;
56
57 void SendReset()
58 {
59 struct IPPacket *p;
60 int i;
61
62 if (session.dont_send_reset)
63 return;
64
65 if ((p = (struct IPPacket *)calloc(1, sizeof(struct IPPacket))) == NULL) {
66 perror("ERROR: Could not allocate RST packet:") ;
67 Quit(ERR_MEM_ALLOC) ;
68 }
69
70 if ((p->ip = (struct IpHeader *)calloc(1, sizeof(struct IpHeader))) == NULL) {
71 perror("ERROR: Could not allocate IP Header for RST packet:") ;
72 Quit(ERR_MEM_ALLOC) ;
73 }
74
75 if ((p->tcp = (struct TcpHeader *)calloc(1, sizeof(struct TcpHeader))) == NULL) {
76 perror("ERROR: Could not allocate TCP Header for RST packet:") ;
77 Quit(ERR_MEM_ALLOC) ;
78 }
79
80 for (i = 0; i < MAXRESETRETRANSMITS; i++) {
81 SendSessionPacket(p,
82 //sizeof(struct IPPacket),
83 sizeof(struct IpHeader) + sizeof(struct TcpHeader),
84 TCPFLAGS_RST,
85 0,
86 0,
87 0);
88 }
89
90 /* free(p->ip);
91 free(p->tcp);
92 free(p);
93 */
94
95 }
96
97 #if 0
98 /* make a clean exit on interrupts */
99 void SigHandle (int signo)
100 {
101 Cleanup () ;
102 fflush(stdout);
103 fflush(stderr);
104 exit(-1);
105 }
106
107
108 void Cleanup()
109 {
110
111 char ipfw_rule[100];
112 int r;
113
114 /* If a firewall rule has been installed then remove it */
115 if (session.initFirewall > 0) {
116
117 #ifdef linux
118 #define IP_FW_DEL (IP_FW_DELETE)
119 #endif /* linux */
120
121 sprintf(ipfw_rule, "ipfw del 00%d", session.firewall_rule_number);
122 r = system(ipfw_rule);
123
124 }
125
126 if (session.initSession > 0) {
127
128 SendReset();
129 shutdown(session.socket,2);
130
131 }
132
133 if (session.initCapture > 0) {
134 CaptureEnd();
135 }
136
137 }
138
139 void Quit(int how)
140 {
141
142 Cleanup();
143 fflush(stdout);
144 fflush(stderr);
145 exit(how);
146
147 }
148 #endif /* 0 */
149
150 double GetTime()
151 {
152 struct timeval tv;
153 struct timezone tz;
154 double postEpochSecs;
155
156 if (gettimeofday(&tv, &tz) < 0) {
157 perror("GetTime");
158 exit(-1);
159 }
160
161 postEpochSecs = (double)tv.tv_sec + ((double)tv.tv_usec/(double)1000000.0);
162 return postEpochSecs;
163 }
164
165 double GetTimeMicroSeconds()
166 {
167 struct timeval tv;
168 struct timezone tz;
169 double postEpochMicroSecs;
170
171 if (gettimeofday(&tv, &tz) < 0) {
172 perror("GetTimeMicroSeconds");
173 exit(-1);
174 }
175
176 postEpochMicroSecs = (double)tv.tv_sec * 1000000 + (double)tv.tv_usec;
177 return postEpochMicroSecs;
178
179 }
180
181 void PrintTimeStamp(struct timeval *ts)
182 {
183 (void)printf("%02d:%02d:%02d.%06u ",
184 (unsigned int)ts->tv_sec / 3600,
185 ((unsigned int)ts->tv_sec % 3600) / 60,
186 (unsigned int)ts->tv_sec % 60, (unsigned int)ts->tv_usec);
187 }
188
189 void processBadPacket (struct IPPacket *p)
190 {
191
192 if (session.debug == SESSION_DEBUG_HIGH) {
193 printf("In ProcessBadPacket...\n");
194 }
195 /*
196 * reset? the other guy does not like us?
197 */
198 if (INSESSION(p,session.dst,session.dport,session.src,session.sport) && (p->tcp->tcp_flags & TCPFLAGS_RST)) {
199 printf("ERROR: EARLY_RST.\nRETURN CODE: %d\n", EARLY_RST);
200 Quit(EARLY_RST);
201 }
202 /*
203 * some other packet between us that is none of the above
204 */
205 if (INSESSION(p, session.src, session.sport, session.dst, session.dport) ||
206 INSESSION(p, session.dst, session.dport, session.src, session.sport)) {
207
208 printf("ERROR: Unexpected packet\nRETURN CODE: %d\n", UNEXPECTED_PKT);
209 printf("Expecting:\n");
210 printf("\tsrc = %s:%d (seq=%u, ack=%u)\n",
211 InetAddress(session.src), session.sport,
212 session.snd_nxt-session.iss,
213 session.rcv_nxt-session.irs);
214 printf("\tdst = %s:%d (seq=%u, ack=%u)\n",
215 InetAddress(session.dst),session.dport,
216 session.rcv_nxt-session.irs, session.snd_una-session.iss);
217 printf("Received:\n\t");
218 PrintTcpPacket(p);
219 printf ("session.snd_nxt=%d, session.rcv_nxt=%d, session.snd_una=%d\n",
220 session.snd_nxt-session.iss, session.rcv_nxt-session.irs, session.snd_una-session.iss);
221 Quit(UNEXPECTED_PKT);
222 }
223 /*
224 * none of the above,
225 * so we must be seeing packets
226 * from some other flow!
227 */
228 else {
229 printf("ERRROR: Received packet from different flow\nRETURN CODE: %d\n", DIFF_FLOW);
230 PrintTcpPacket(p);
231 Quit(DIFF_FLOW) ;
232 }
233
234 if (session.debug == SESSION_DEBUG_HIGH) {
235 printf("Out ProcessBadPacket...\n");
236 }
237 }
238
239 void busy_wait (double wait)
240 {
241 double now = GetTime();
242 double x = now ;
243 while ((x - now) < wait) {
244 x = GetTime();
245 }
246 }