2 * A small program to generate the ASCII form of a capture with TCP
3 * segments of a reasonable nature. The payload is all zeros.
5 * By Ronnie Sahlberg and Richard Sharpe. From a program initially
7 * Copyright 2003 Ronnie Sahlberg and Richard Sharpe
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Using it to generate a capture file:
24 * ./mkcap [some-flags] > some-file
25 * text2pcap [some-other-flags] some-file some-file.cap
28 ./mkcap -a 2500 -s 15 -I "02 03 04 05" -i "45 45 45 45" -P "00 14" > ftp.cap.asci
29 text2pcap -t "%Y/%m/%d%t%H:%M:%S." ftp.cap.asci ftp.cap
37 #define ETH_1 "00 00 9c fa 1d 74"
38 #define ETH_2 "00 1a b8 93 f6 71"
39 #define IP_1 "0a 01 01 17"
40 #define IP_2 "0a 01 01 ea"
41 #define PORT_1 "01 00"
42 #define PORT_2 "10 00"
48 char *port_1 = PORT_1;
49 char *port_2 = PORT_2;
65 * The array of which segments should be dropped ...
67 seg_drop_t *drops = NULL;
68 int seg_drop_count = 0;
69 /* The array of which ACKs should be dropped. This is complicated because
70 * An ack might not be generated for a segment because of delayed ACKs.
72 seg_drop_t *ack_drops = NULL;
73 int ack_drop_count = 0;
75 int total_bytes = 32768;
82 int send_spacing = 10;
85 int tcp_delay_time = 1000; /* What is the real time here? */
87 * If tcp_nodelay is set, then this is the amount of data left ...
89 int remaining_data = 0;
97 #define SEG_ACK_LOST 1
98 #define SEG_SEG_LOST 2
101 int seq_num; /* First sequence number in segment */
102 int len; /* Number of bytes in segment */
103 int ts; /* Timestamp when sent */
104 int seg_num; /* Segment number sent. This can change */
105 /* but a retransmit will have a new seg */
106 int flags; /* Flags as above for ack and seg loss */
107 int acks_first_seq; /* How many times we have seen an ack
108 for the first seq number in this seg */
111 #define SEG_HIST_SIZE 128
112 struct seg_hist_s seg_hist[128]; /* This should be dynamic */
116 int delayed_ack = 1; /* Default is delayed ACKs in use ... */
117 int delayed_ack_wait = 30000; /* 30 mS before an ACK is generated if */
118 /* no other traffic */
121 makeseg(char *eth1, char *eth2, char *ip1, char *ip2, char *p1, char *p2, int *s1, int *s2, char *flags, int len)
125 printf("2002/01/07 00:00:%02d.%06d\n", ts/1000000, ts%1000000);
126 printf("0000 %s %s 08 00\n", eth1, eth2);
127 printf("000e 45 00 %02x %02x 00 00 00 00 40 06 00 00 %s %s\n", (len+40)>>8, (len+40)&0xff, ip1, ip2);
128 printf("0022 %s %s %02x %02x %02x %02x %02x %02x %02x %02x 50 %s 80 00 00 00 00 00", p1, p2,
138 for(i=0;i<(len<(snap_len-40)?len:snap_len-40);i++)printf(" 00");
145 * Figure out when the next ack is due ... here we must skip the acks for
146 * frames that are marked as ACKs dropped as well as the frames marked as
147 * frames dropped. These will be marked by the routine that generates ACKs.
148 * Returns a timestamp value. Returns 2^^31-1 if none are due at all
152 int slot = next_slot;
153 int ack_lost = 0, seg_lost = 0;
155 if (next_slot == first_slot)
156 return (((unsigned int)(1<<31)) - 1);
159 * Figure out if we need to issue an ACK. We skip all outstanding packets
160 * that are marked as ack lost or packet lost.
162 * We would not usually come in here with a frame marked as lost or ack lost
163 * rather, we will come in here and specify that the ack was due at a
164 * certain time, and gen_next_ack would then determine that the ack
165 * should be lost or the packet lost.
169 * Look for a seg slot that is not lost or dropped
172 while (seg_hist[slot].flags & (SEG_ACK_LOST || SEG_SEG_LOST)) {
173 if (seg_hist[slot].flags & SEG_ACK_LOST)
175 if (seg_hist[slot].flags & SEG_SEG_LOST)
177 slot = (slot + 1) % SEG_HIST_SIZE;
180 if (slot == next_slot)
181 return (((unsigned int)(1<<31)) - 1);
184 * If there is only one slot occupied, or a segment was lost then
185 * an ACK is due after the last [good] segment left plus ack_delay
188 if (slot == first_slot && next_slot == ((first_slot + 1) % SEG_HIST_SIZE))
189 return (seg_hist[first_slot].ts + ack_delay + jitter);
192 return (seg_hist[slot].ts + ack_delay + jitter);
195 * OK, now, either we have only seen lost acks, or there are more than
196 * one outstanding segments, so figure out when the ACK is due.
198 * If delayed ACK is in force, ACK is due after every second seg, but
199 * if we had a lost ack, then we must ignore 2*lost_ack segments. So,
200 * if there has not been that many segments sent, we return infinity
201 * as the next ACK time
206 if (((first_slot + 1 + 2 * ack_lost) % SEG_HIST_SIZE) >= next_slot)
207 /* XXX: FIXME, what about when the window is closed */
208 /* XXX: FIXME, use the correct value for this */
209 return (((unsigned int)(1<<31)) - 1);
211 return seg_hist[(first_slot + 1 + 2 * ack_lost) % SEG_HIST_SIZE].ts +
215 return seg_hist[slot].ts + ack_delay + jitter;
219 return (seg_hist[(first_slot + 1)%SEG_HIST_SIZE].ts+ack_delay+jitter);
221 return (seg_hist[first_slot].ts+ack_delay+jitter);
226 * Update the relevant info of the sent seg
228 add_seg_sent(int seq, int len)
232 * Should check we have not wrapped around and run into the unacked
235 /*if (next_slot == first_slot) ;*/
238 seg_hist[next_slot].seq_num = seq;
239 seg_hist[next_slot].len = len;
240 seg_hist[next_slot].ts = ts;
241 seg_hist[next_slot].seg_num = segment;
242 seg_hist[next_slot].flags = 0;
243 seg_hist[next_slot].acks_first_seq = 0;
244 used_win = used_win + len; /* Update the window used */
247 * Now, update next_slot ...
250 next_slot = (next_slot + 1) % SEG_HIST_SIZE;
255 * Generate the next ack based on the above reasoning ...
258 #define NO_FORCE_ACK 0
262 * Generate the next ACK. If we did not generate an ACK, return 0,
266 gen_next_ack(int force, int spacing)
268 int seq_to_ack, new_ts, data_acked;
271 * We need to check if the segment that we are about to generate an
272 * ack for is a segment that should be dropped ... or an ack that should
275 * Figure out what we are doing before freeing segments ...
278 seq_to_ack = seg_hist[first_slot].seq_num + seg_hist[first_slot].len;
279 used_win = used_win - seg_hist[first_slot].len;
280 data_acked = seg_hist[first_slot].len;
281 new_ts = seg_hist[first_slot].ts + ack_delay;
282 first_slot = (first_slot + 1) % SEG_HIST_SIZE;
285 * If delayed ACK in force, then ACK the next segment if there is one
287 if (delayed_ack && (first_slot != next_slot)) {
288 seq_to_ack += seg_hist[first_slot].len;
289 used_win = used_win - seg_hist[first_slot].len;
290 data_acked += seg_hist[first_slot].len;
291 new_ts = seg_hist[first_slot].ts + ack_delay;
292 first_slot = (first_slot + 1) % SEG_HIST_SIZE;
296 * We don't want time to go backward ...
298 if (new_ts + jitter <= ts)
301 ts = new_ts + jitter;
303 jitter = (rand() % 10 - 5); /* Update jitter ... */
305 makeseg(eth_2, eth_1, ip_2, ip_1, port_2, port_1, &seq_2, &seq_to_ack, "10", 0);
307 * Do we want the exponential part or the linear part?
309 if (cwnd >= ssthresh)
310 cwnd += (1460*data_acked)/cwnd; /* is this right? */
312 cwnd = cwnd + data_acked;
313 if (verbose) fprintf(stderr, "Ack rcvd. ts: %d, data_acked: %d, cwnd: %d, window: %d\n",
314 ts, data_acked, cwnd, window);
315 if (cwnd > window) cwnd = window;
319 makeackedrun(int len, int spacing, int ackdelay)
322 if (verbose) fprintf(stderr, "makeackedrun: Len=%d, spacing=%d, ackdelay=%d\n",
323 len, spacing, ackdelay);
327 * Each time we output a segment, we should check to see if an
328 * ack is due back before the next segment is due ...
330 int seglen, saved_seq;
331 seglen=(len>1460)?1460:len;
333 * Only output what is left in the cwnd.
334 * We assume there is space in the congestion window here
336 if (seglen > (cwnd - used_win)) seglen = cwnd - used_win;
340 if (verbose) fprintf(stderr, "Sending segment. ts: %d, jitter: %d\n", ts, jitter);
342 makeseg(eth_1, eth_2, ip_1, ip_2, port_1, port_2, &seq_1, &seq_2, "10", seglen);
344 makeseg(eth_1, eth_2, ip_1, ip_2, port_1, port_2, &seq_1, &seq_2, "18", seglen);
346 add_seg_sent(saved_seq, seglen);
349 * Now, if the window is closed, then we have to eject an
350 * ack, otherwise we can eject more data.
351 * Also, the other end will tend to ack two segments at
352 * a time ... and that ack might fall between two
355 jitter = (rand()%10) - 5; /* What if spacing too small */
357 if (verbose) fprintf(stderr, "used win: %d, cwnd: %d\n", used_win, cwnd);
359 if ((next_ack_ts = next_ack_due()) < ts + spacing + jitter) {
363 * Generate the ack and retire the segments
364 * If delayed ACK in use, there should be two
365 * or more outstanding segments ...
367 if (verbose) fprintf(stderr, "Non forced ACK ...ts + spacing + jitter:%d, jitter: %d\n", ts + spacing + jitter, jitter);
368 gen_next_ack(NO_FORCE_ACK, spacing);
370 * We don't want time to go backwards ...
372 if (old_ts + spacing + jitter <= ts)
375 ts = old_ts + spacing + jitter;
377 } else if (used_win == cwnd) {
380 * We need an ACK, so generate it and retire the
381 * segments and advance the ts to the time of the ack
384 if (verbose) fprintf(stderr, "Forced ACK ... \n");
385 gen_next_ack(FORCE_ACK, spacing);
387 ts+=(spacing+jitter); /* Should not use spacing here */
391 ts+=(spacing+jitter);
394 if (verbose) fprintf(stderr, "Next Ack Due: %d\n", next_ack_ts);
401 makeackedrundroppedtail8kb(int len, int spacing, int ackdelay)
407 if (verbose) fprintf(stderr, "makeackedrundroppedtail8kB: Len=%d, spacing=%d, ackdelay=%d\n",
408 len, spacing, ackdelay);
412 seglen=(len>1460)?1460:len;
415 makeseg(eth_1, eth_2, ip_1, ip_2, port_1, port_2, &seq_1, &seq_2, "10", seglen);
417 makeseg(eth_1, eth_2, ip_1, ip_2, port_1, port_2, &seq_1, &seq_2, "18", seglen);
427 while(old_seq1!=seq_1){
430 ack_len=((seq_1-old_seq1)>2920)?2920:(seq_1-old_seq1);
434 dropped_tail=old_seq1;
438 makeseg(eth_2, eth_1, ip_2, ip_1, port_2, port_1, &seq_2, &old_seq1, "10", 0);
440 makeseg(eth_2, eth_1, ip_2, ip_1, port_2, port_1, &seq_2, &dropped_tail, "10", 0);
443 makeseg(eth_2, eth_1, ip_2, ip_1, port_2, port_1, &seq_2, &dropped_tail, "10", 0);
444 makeseg(eth_2, eth_1, ip_2, ip_1, port_2, port_1, &seq_2, &dropped_tail, "10", 0);
457 seglen=((seq_1-dropped_tail)>1460)?1460:(seq_1-dropped_tail);
459 makeseg(eth_1, eth_2, ip_1, ip_2, port_1, port_2, &dropped_tail, &seq_2, "10", seglen);
461 makeseg(eth_1, eth_2, ip_1, ip_2, port_1, port_2, &dropped_tail, &seq_2, "18", seglen);
465 makeseg(eth_2, eth_1, ip_2, ip_1, port_2, port_1, &seq_2, &seq_1, "10", 0);
470 while(dropped_tail!=seq_1){
473 seglen=((seq_1-dropped_tail)>1460)?1460:(seq_1-dropped_tail);
475 makeseg(eth_1, eth_2, ip_1, ip_2, port_1, port_2, &dropped_tail, &seq_2, "10", seglen);
477 makeseg(eth_1, eth_2, ip_1, ip_2, port_1, port_2, &dropped_tail, &seq_2, "18", seglen);
482 makeseg(eth_2, eth_1, ip_2, ip_1, port_2, port_1, &seq_2, &ack, "10", 0);
489 fprintf(stderr, "Usage: mkcap [OPTIONS], where\n");
490 fprintf(stderr, "\t-a <ack-delay> is the delay to an ACK (SRT)\n");
491 fprintf(stderr, "\t-b <bytes-to-send> is the bytes to send on connection\n");
492 fprintf(stderr, "\t-i <ip-addr-hex> is the sender IP address in hex\n");
493 fprintf(stderr, "\t-I <ip-addr-hex> is the recipient IP address in hex\n");
494 fprintf(stderr, "\t-n <ISN> is almost the ISN for the sender\n");
495 fprintf(stderr, "\t-N <ISN> is almost the ISN for the recipient\n");
496 fprintf(stderr, "\t-p <port-number-hex> is the port number for sender\n");
497 fprintf(stderr, "\t-P <port-number-hex> is the port number for recipient\n");
498 fprintf(stderr, "\t-s <send-spacing> is the send spacing\n");
499 fprintf(stderr, "\t-w <window-size> is the window size\n");
503 all_digits(char *str)
506 if (!str || !(*str)) {
510 for (i = 0; str[i]; i++) {
511 if (!isdigit(str[i]))
519 * Process a list of drops. These are of the form:
521 * first_seg,seg_count[,first_seg,seg_count]*
524 process_drop_list(char *drop_list)
529 if (!drop_list || !(*drop_list)) {
530 fprintf(stderr, "Strange drop list. NULL or an empty string. No drops!\n");
533 save = (char *)g_strdup(drop_list);
535 for (tok=(char *)strtok(drop_list, ","); tok; tok=(char *)strtok(NULL, ",")) {
539 /* Now, we have commas, divide by two and round up */
541 seg_drop_count = (commas+1)/2;
542 drops = (seg_drop_t *)g_malloc(sizeof(seg_drop_t) * seg_drop_count);
544 fprintf(stderr, "Unable to allocate space for drops ... going without!\n");
550 /* Now, go through the list again and build the drop list. Any errors and */
551 /* we abort and print a usage message */
554 for (tok=(char *)strtok(save, ","); tok; tok=(char *)strtok(NULL, ",")) {
557 if (!all_digits(tok)) {
558 fprintf(stderr, "Error in segment offset or count. Not all digits: %s\n",
560 fprintf(stderr, "No packet drops being performed!\n");
563 seg_drop_count = 0; drops = NULL;
566 if (num == 0) num = 1;
568 drops[commas / 2].drop_seg_count = num;
570 drops[commas / 2].drop_seg_start = num;
578 main(int argc, char *argv[])
588 while ((opt = getopt(argc, argv, "a:b:d:Di:I:j:l:n:N:p:P:r:s:vw:")) != EOF) {
591 ack_delay = atoi(optarg);
594 case 'b': /* Bytes ... */
595 total_bytes = atoi(optarg);
598 case 'd': /* A list of drops to simulate */
599 process_drop_list(optarg);
602 case 'D': /* Toggle tcp_nodelay */
603 tcp_nodelay = (tcp_nodelay + 1) % 1;
615 snap_len = atoi(optarg);
618 case 'n': /* ISN for send dirn, ie, seq_1 */
619 seq_1 = atoi(optarg);
622 case 'N': /* ISN for recv dirn, ie, seq_2 */
623 seq_2 = atoi(optarg);
635 run_type = atoi(optarg);
639 send_spacing = atoi(optarg);
646 case 'w': /* Window ... */
647 window = atoi(optarg);
648 ssthresh = window / 2; /* Have to recalc this ... */
657 if (verbose) fprintf(stderr, "IP1: %s, IP2: %s, P1: %s, P2: %s, Ack Delay: %d, Send Spacing: %d\n",
658 ip_1, ip_2, port_1, port_2, ack_delay, send_spacing);
663 makeackedrun(total_bytes, send_spacing, ack_delay);
666 for(cnt=0;cnt<200;cnt++){
672 makeackedrun(32768, send_spacing, ack_delay);
678 len=100+rand()&0xfff;
679 makeackedrun(len, send_spacing, ack_delay);
683 len=100+rand()&0x3fff+0x1fff;
684 makeackedrun(len, send_spacing, ack_delay);
685 /*makeackedrundroppedtail8kb(len, send_spacing, ack_delay);*/