configure: do not use bash's expr match
[netsniff-ng.git] / staging / send.c
blob5ad1a203e52c71589c9751392631a012546c38e3
1 /*
2 * Mausezahn - A fast versatile traffic generator
3 * Copyright (C) 2008 Herbert Haas
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License version 2 as published by the
7 * Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 * details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
23 // ***************************************************************************
24 //
25 // This sections contains:
26 //
27 // - complexity() ... calculates and reports how many frames will
28 // be generated.
29 // - send_frame() ... the general and mighty SENDING FUNCTION.
31 // ***************************************************************************
33 #include "mz.h"
34 #include "cli.h"
37 // Calculates the number of frames to be sent.
38 // Should be used as standard output except the
39 // 'quiet' option (-q) has been specified.
40 int complexity()
42 unsigned long int
43 nr_sqnr = 1,
44 nr_dp = 1,
45 nr_sp = 1,
46 nr_da = 1,
47 nr_sa = 1;
49 u_int32_t
50 sn1,
51 sn2,
52 delta;
54 long double ref;
56 if (tx.count==0) goto infinity;
58 total_d = 1.0;
60 // How many sequence numbers?
61 if (tx.tcp_seq_delta)
63 sn1 = tx.tcp_seq_start;
64 sn2 = tx.tcp_seq_stop;
65 delta = tx.tcp_seq_delta;
67 if (sn1<sn2) // the easier case
69 nr_sqnr = (sn2-sn1)/delta;
71 else
73 nr_sqnr = (sn2 + (0xffffffff - sn1)) / delta;
75 //fprintf(stderr,"SQNR Range = %lu\n",nr_sqnr);
76 nr_sqnr +=1;
79 if (tx.dp_isrange)
81 nr_dp = tx.dp_stop - tx.dp_start + 1;
82 //fprintf(stderr,"DP Range = %lu\n",nr_dp);
85 if (tx.sp_isrange)
87 nr_sp = tx.sp_stop - tx.sp_start + 1;
88 //fprintf(stderr,"SP Range = %lu\n",nr_sp);
91 if (tx.ip_dst_isrange)
93 nr_da = tx.ip_dst_stop - tx.ip_dst_start + 1;
94 //fprintf(stderr,"DA Range = %lu\n",nr_da);
97 if (tx.ip_src_isrange)
99 nr_sa = tx.ip_src_stop - tx.ip_src_start + 1;
100 //fprintf(stderr,"SA Range = %lu\n",nr_sa);
103 total_d *= tx.count;
104 total_d *= nr_sqnr;
105 total_d *= nr_dp;
106 total_d *= nr_sp;
107 total_d *= nr_da;
108 total_d *= nr_sa;
113 ref=0xffffffff;
115 ref*=ref;
117 if (total_d>ref)
119 fprintf(stderr, "You must be crazy...\n");
121 else if (total_d>0xffffffff)
123 fprintf(stderr, "Do you REALLY know what you do?\n");
125 else if (total_d>0xffffff)
127 fprintf(stderr, "Do you know what you do?\n");
130 if (mz_port)
132 cli_print(gcli, "Mausezahn will send %.Lf frames...\r", total_d);
134 else
136 fprintf(stderr, "Mausezahn will send %.Lf frames... ", total_d);
137 fflush(stderr);
138 if (verbose) fprintf(stderr,"\n");
143 mz_start = clock();
145 infinity:
148 if (tx.count==0)
150 if (mz_port)
152 cli_print(gcli, "Mausezahn will send frames infinitly...\n");
154 else
156 fprintf(stderr, "Mausezahn will send frames infinitly...\n");
161 return 0;
166 ///////////////////////////////////////////////////////////////////////
168 // Send complete frame (layers 2, 3, 4) multiple times if required
171 int send_frame (libnet_t *l, libnet_ptag_t t3, libnet_ptag_t t4)
173 int i=0, count;
175 int // local vars are faster ;-)
176 tcp_seq_delta,
177 dp_isrange,
178 sp_isrange,
179 ip_dst_isrange,
180 ip_src_isrange,
181 rtp_mode=0;
184 count = tx.count;
185 tcp_seq_delta = tx.tcp_seq_delta;
186 dp_isrange = tx.dp_isrange;
187 sp_isrange = tx.sp_isrange;
188 ip_dst_isrange = tx.ip_dst_isrange;
189 ip_src_isrange = tx.ip_src_isrange | tx.ip_src_rand;
190 if (mode == RTP) rtp_mode = 1;
192 if (count==0) goto AGAIN;
194 for (i=0; i<count; i++)
197 AGAIN:
199 if (verbose) (void) print_frame_details();
200 libnet_write(l);
201 if (mz_rand) tx.delay=(unsigned int) tx.delay*rand()/RAND_MAX;
202 if (tx.delay) SLEEP (tx.delay);
204 // No layer-2 modifications done here
205 // (see create_eth_frame which does L2 modifications additionally)
208 if (tcp_seq_delta)
210 if (update_TCP_SQNR(l, t4)==0) // end of range not yet reached
212 goto AGAIN;
216 if (dp_isrange)
218 if (update_DPORT(l, t4)==0) // end of range not yet reached
220 goto AGAIN;
224 if (sp_isrange)
226 if (update_SPORT(l, t4)==0) // end of range not yet reached
228 goto AGAIN;
233 if (ip_dst_isrange)
235 if (update_IP_DA(l, t3)==0) // end of range not yet reached
237 goto AGAIN;
241 if (ip_src_isrange) // also catches random SA (see above)
243 if (update_IP_SA(l, t3)==0) // end of range not yet reached
245 goto AGAIN;
249 if (rtp_mode) // update SQNR and Timestamps in RTP header and payload
251 update_RTP(l, t4);
255 if (!count) goto AGAIN;
260 libnet_destroy(l);
262 return 0;