ifpps: use uint32_t instead of u32
[netsniff-ng.git] / staging / send.c
blob1cbf4341c533783529103fe9d5cff7790e1a42ec
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(void)
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 if (ipv6_mode)
95 nr_da = get_ip6_range_count(tx.ip6_dst_start, tx.ip6_dst_stop);
97 else
99 nr_da = tx.ip_dst_stop - tx.ip_dst_start + 1;
101 //fprintf(stderr,"DA Range = %lu\n",nr_da);
104 if (tx.ip_src_isrange)
106 if (ipv6_mode)
108 nr_sa = get_ip6_range_count(tx.ip6_src_start, tx.ip6_src_stop);
110 else
112 nr_sa = tx.ip_src_stop - tx.ip_src_start + 1;
114 //fprintf(stderr,"SA Range = %lu\n",nr_sa);
117 total_d *= tx.count;
118 total_d *= nr_sqnr;
119 total_d *= nr_dp;
120 total_d *= nr_sp;
121 total_d *= nr_da;
122 total_d *= nr_sa;
127 ref=0xffffffff;
129 ref*=ref;
131 if (total_d>ref)
133 fprintf(stderr, "You must be crazy...\n");
135 else if (total_d>0xffffffff)
137 fprintf(stderr, "Do you REALLY know what you are doing?\n");
139 else if (total_d>0xffffff)
141 fprintf(stderr, "Do you know what you are doing?\n");
144 if (mz_port)
146 cli_print(gcli, "Mausezahn will send %.Lf frames...\r", total_d);
148 else
150 fprintf(stderr, "Mausezahn will send %.Lf frames... ", total_d);
151 fflush(stderr);
152 if (verbose) fprintf(stderr,"\n");
157 mz_start = clock();
159 infinity:
162 if (tx.count==0)
164 if (mz_port)
166 cli_print(gcli, "Mausezahn will send frames infinitely...\n");
168 else
170 fprintf(stderr, "Mausezahn will send frames infinitely...\n");
175 return 0;
180 ///////////////////////////////////////////////////////////////////////
182 // Send complete frame (layers 2, 3, 4) multiple times if required
185 int send_frame (libnet_t *l, libnet_ptag_t t3, libnet_ptag_t t4)
187 int i=0, count;
189 int // local vars are faster ;-)
190 tcp_seq_delta,
191 dp_isrange,
192 sp_isrange,
193 ip_dst_isrange,
194 ip_src_isrange,
195 rtp_mode=0;
198 count = tx.count;
199 tcp_seq_delta = tx.tcp_seq_delta;
200 dp_isrange = tx.dp_isrange;
201 sp_isrange = tx.sp_isrange;
202 ip_dst_isrange = tx.ip_dst_isrange;
203 ip_src_isrange = tx.ip_src_isrange | tx.ip_src_rand;
204 if (mode == RTP) rtp_mode = 1;
206 if (count==0) goto AGAIN;
208 for (i=0; i<count; i++)
211 AGAIN:
212 if (ipv6_mode) {
213 switch (mode) {
214 case ICMP6:
215 update_ISUM(l, t4);
216 break;
217 case UDP:
218 case DNS:
219 case RTP:
220 case SYSLOG:
221 update_USUM(l, t4);
222 break;
223 case TCP:
224 update_TSUM(l, t4);
225 break;
229 if (verbose) (void) print_frame_details();
230 libnet_write(l);
231 if (mz_rand) tx.delay=(unsigned int) tx.delay*rand()/RAND_MAX;
232 if (tx.delay) SLEEP (tx.delay);
234 // No layer-2 modifications done here
235 // (see create_eth_frame which does L2 modifications additionally)
238 if (tcp_seq_delta)
240 if (update_TCP_SQNR(l, t4)==0) // end of range not yet reached
242 goto AGAIN;
246 if (dp_isrange)
248 if (update_DPORT(l, t4)==0) // end of range not yet reached
250 goto AGAIN;
254 if (sp_isrange)
256 if (update_SPORT(l, t4)==0) // end of range not yet reached
258 goto AGAIN;
263 if (ip_dst_isrange)
265 if (update_IP_DA(l, t3)==0) // end of range not yet reached
267 goto AGAIN;
271 if (ip_src_isrange) // also catches random SA (see above)
273 if (update_IP_SA(l, t3)==0) // end of range not yet reached
275 goto AGAIN;
279 if (rtp_mode) // update SQNR and Timestamps in RTP header and payload
281 update_RTP(l, t4);
285 if (!count) goto AGAIN;
290 libnet_destroy(l);
292 return 0;