File: PctestIpv4File.cc

package info (click to toggle)
pchar 1.5-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 644 kB
  • ctags: 402
  • sloc: cpp: 6,096; sh: 2,510; makefile: 192
file content (329 lines) | stat: -rw-r--r-- 7,795 bytes parent folder | download | duplicates (5)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
static char rcsid[] = "$Id: PctestIpv4File.cc 1082 2005-02-12 19:40:04Z bmah $";
//
// $Id: PctestIpv4File.cc 1082 2005-02-12 19:40:04Z bmah $
//
// PctestIpv4File.cc
// Bruce A. Mah <bmah@acm.org>
//
// This work was first produced by an employee of Sandia National
// Laboratories under a contract with the U.S. Department of Energy.
// Sandia National Laboratories dedicates whatever right, title or
// interest it may have in this software to the public. Although no
// license from Sandia is needed to copy and use this software,
// copying and using the software might infringe the rights of
// others. This software is provided as-is. SANDIA DISCLAIMS ANY
// WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.
//
// Class of IPv4 tests reading test data from previously-saved results
//

#include <sys/types.h>
#include <ctype.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>

#include "pc.h"
#include "PctestIpv4File.h"
#include "TestRecord.h"

extern unsigned int Mtu;

//
// PctestIpv4File::GetSocketOut
//
// Input:  None
//
// Output:  In return value, returns socket number.
//
// Get output socket of an appropriate type, but for us it's a no-op.
//
int PctestIpv4File::GetSocketOut() {

    return 0;

}

//
// PctestIpv4File::GetSocketIn
//
// Input:  None
//
// Output:  In return value, returns socket number.
//
// Override the superclass behavior...we don't use sockets.
//
int PctestIpv4File::GetSocketIn() {

    return 0;

}

//
// PctestIpv4File::SetOriginName
//
// Input:  ignored
//
// Output:  success code (negative if an error)
//
// This method exists primarily to override PctestIpv4::SetOriginName
// to prevent it from overwriting the originAddress and originName
// members, which were set when the trace file was read using 
// PctestIpv4File::SetTargetName.  All we need to do here is a 
// name lookup.
//
int PctestIpv4File::SetOriginName(char *t) {
    struct hostent *host;	// resolver hostname entry

    originName = strdup(GetName((char *) &originAddress));
    if (originName == NULL) {
	fprintf(stderr, "Couldn't allocate memory for origin hostname.\n");
	return -1;
    }
}

//
// PctestIpv4File::SetTargetName
//
// Input:  ignored
//
// Output:  success code (negative if error)
//
// For protocols that actually send packets on the network,
// do name resolution on the target host.  For this case, however,
// we read in the savefile, for later use by PctestIpv4File::Test.
// As with the other, similar routine(s), we're responsible for
// printing any error messages that come up, since they're likely
// to be domain-specific.
//
int PctestIpv4File::SetTargetName(char *t)
{

    extern unsigned int Burst;	// maximum burst size
    extern unsigned int Hops;	// number of hops
    extern unsigned int Increment; // packet size increment
    extern unsigned int Mtu;	// transfer MTU
    extern char *ReadFilename;	// user-supplied filename
    extern unsigned int Repetitions; // number of repetitions per packet size
    extern unsigned int StartHop; // starting hop number
    extern int VerboseFlag;	// -v from command-line

    bool done = false;		// done reading?
    int linenum = 1;		// line number
    const unsigned int buflen = 1024; // maximum line length
    char buf[buflen];		// line buffer
    char *s;			// return value from fgets
    TestRecord *tr;		// test record read from file
    TestRecord *trstail = NULL;       

    // If the user didn't supply us with a command-line filename,
    // it's an error.
    if (!ReadFilename) {
	fprintf(stderr, "No filename specified for -r\n");
	return -1;
    }

    // Try to open the file
    f = fopen(ReadFilename, "r");
    if (!f) {
	perror("fopen");
	return -1;
    }

    // Loop until finished
    while (!done) {

	s = fgets(buf, buflen, f);

	// See if we're done...
	if (!s) {
	    if (ferror(f)) {
		// error condition
		perror("fgets");
	    }
	    done = true;
	    goto doneline;
	}

	// Process a line.  We're going to make a very simple parser
	// here and in TestRecord::atoh().
	//
	char *cur;

	// First, throw out all blank (or almost blank) lines...
	for (cur = s; *cur != '\0'; cur++) {
	    if (!isspace(*cur)) {
		break;
	    }
	}
	if (*cur == '\0') {
	    goto doneline;
	}

	// Then, look for comment lines
	if (*s == '#') {
	    goto doneline;
	}

	if (strncasecmp(s, "probe ", 6) == 0) {
	    tr = TestRecord::atoh(s, this);

	    if (tr == NULL) {
		return -1;
	    }

	    tr->used = false;

	    // Try to keep the SLL in the same order that we read stuff.
	    // To do this efficiently means we need to (temporarily)
	    // keep a tail pointer.
	    if (trstail) {
		trstail->next = tr;
		trstail = tr;
		tr->next = NULL;
	    }
	    else {
		trs = tr;
		trstail = tr;
		tr->next = NULL;
	    }
	}

	else if (strncasecmp(s, "src ", 4) == 0) {
	    char t[128];
	    sscanf(s, "src %127s", t);
	    originAddress.s_addr = inet_addr(t);
	}

	else if (strncasecmp(s, "dest ", 5) == 0) {
	    char t[128];
	    sscanf(s, "dest %127s", t);
	    targetAddress.s_addr = inet_addr(t);
	}

	else if (strncasecmp(s, "burst ", 6) == 0) {
	    sscanf(s, "burst %d", &Burst);
	}

	else if (strncasecmp(s, "minsize ", 8) == 0) {
	    sscanf(s, "minsize %d", &minsize);
	}

	// hops:
	else if (strncasecmp(s, "hops ", 5) == 0) {
	    sscanf(s, "hops %d", &Hops);
	}

	else if (strncasecmp(s, "increment ", 10) == 0) {
	    sscanf(s, "increment %d", &Increment);
	}

	else if (strncasecmp(s, "mtu ", 4) == 0) {
	    sscanf(s, "mtu %d", &Mtu);
	}

	else if (strncasecmp(s, "repetitions ", 12) == 0) {
	    sscanf(s, "repetitions %d", &Repetitions);
	}

	else if (strncasecmp(s, "starthop ", 9) == 0) {
	    sscanf(s, "starthop %d", &StartHop);
	}

	else if (strncasecmp(s, "targethost ", 11) == 0) {
	    char t[128];
	    sscanf(s, "targethost %127s", t);
	    PctestIpv4::SetTargetName(t);
	}

	else if (strncasecmp(s, "addresses ", 10) == 0) {
	    // Ignore lines that look like this; we already parsed
	    // them to select this object.
	}

	// We can semi-quietly ignore everything else from this point.
	// If we're in verbose mode, we can yell about it.
	else if (VerboseFlag) {
	    fprintf(stderr, "warning: ignoring line %s", s);
	}

      doneline:
	linenum++;

    }

    // Done with file
    fclose(f);
    f = NULL;

    return 0;

}

//
// PctestIpv4File::Test
//
// Input:
//
// Output:
//
// A negative icmpCode indicates a timeout.
//
int PctestIpv4File::Test(TestRecord &tr)
{

    TestRecord *cur;

    // Loop through our set of TestRecords that we've already read
    // in, find the first one that matches both the hops and size,
    // and isn't used yet.
    for (cur = trs; cur; cur = cur->next) {

	if ((!cur->used) && (cur->size == tr.size + ((tr.burst - 1) * Mtu)) && 
	    (cur->hops == tr.hops)) {

	    // Found one!  Now copy everything over from our TestRecord
	    // into the object provided by the caller.
	    tr.tvstart.tv_sec = cur->tvstart.tv_sec;
	    tr.tvstart.tv_usec = cur->tvstart.tv_usec;
	    tr.tv.tv_sec = cur->tv.tv_sec;
	    tr.tv.tv_usec = cur->tv.tv_usec;
	    
	    tr.icmpSourceAddress = new char[sizeof(in_addr)];
	    memcpy(tr.icmpSourceAddress, cur->icmpSourceAddress, sizeof(in_addr));
	    tr.icmpSourceAddressLength = sizeof(in_addr);

	    tr.size = cur->size;
	    tr.replsize = cur->replsize;
	    tr.result = cur->result;

	    cur->used = true;
	    return 0;

	}

    }

    // Error exit
    fprintf(stderr, "Couldn't find enough records\n");
    return -1;

}

//
// PctestIpv4File::GetMinSize
//
// Input:  None
//
// Output:  Minimum packet size possible for this protocol (in return
// value).
//
unsigned int PctestIpv4File::GetMinSize() 
{
    return (minsize);
}