mausezahn: Fix reading of hex payload file
[netsniff-ng.git] / staging / layer1.c
blobbf00a5220e037eb7b67355eebc9fb688d1d0a0b2
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
21 // ****************************************************************************
22 //
23 // This section contains functions to send an arbitrary byte stream out of
24 // the network card. Currently it works perfect for Ethernet cards.
25 //
26 // TODO: Access to the 802.11 header
27 //
28 // ****************************************************************************
30 #include "mz.h"
31 #include "cli.h"
33 int send_eth(void)
35 // Tasks:
36 // 1. Check 'eth_src_txt' and 'eth_dst_txt' which contain either a MAC address or a keyword
37 // 'eth_dst' can be set without having 'eth_src_txt' specified (the next 6 bytes of the
38 // 'arg_string' will be used). But if 'eth_src_txt' is given then also 'eth_dst_txt'
39 // should have been specified, otherwise a default (ff-ff-ff-ff-ff-ff) will be used.
40 // 2. Check whether 'arg_string' contains a hex-string. If YES then convert it into an
41 // 'eth_payload' and extract eth_type.
42 // 3. Apply 'padding' if specified
43 // 4. Check if frame has at least minimum length (14 Bytes).
44 // 5. Send frame 'count' times and
45 // 6. Apply 'delay' (make precautions for better delay functions)
47 int
48 src, // flag telling whether user has specified a source address
49 dst, // flag telling whether user has specified a destination address
50 src_random=0,
51 dst_random=0,
52 byte_ptr=1,
53 bytestring_s=0,
54 min_size=15,
55 pad=0,
56 repeat, loop, update,
57 i=0,
58 j=0;
59 char
60 err_buf[LIBNET_ERRBUF_SIZE],
61 message[MAX_PAYLOAD_SIZE*3],
62 argval[MAX_PAYLOAD_SIZE*2];
64 u_int8_t bytestring[MAX_PAYLOAD_SIZE];
65 libnet_ptag_t t;
66 libnet_t *l;
70 if (tx.dot1Q)
72 fprintf(stderr," Note: raw layer 2 mode does not support 802.1Q builder.\n"
73 " If you want to create VLAN tags then you must do it by hand.\n");
74 exit(1);
77 if (tx.mpls)
79 fprintf(stderr," Note: raw layer 2 mode does not support MPLS builder.\n"
80 " If you want to create MPLS labels then you must do it by hand.\n");
81 exit(1);
87 // So other functions can use this function for sending Ethernet frames
88 // These other functions must set dst, src, type and payload!
89 if (tx.eth_params_already_set) goto ALL_SPECIFIED;
92 if ((tx.padding) && (tx.padding<15)) // Note: ignored if padding==0
94 tx.padding=15;
95 if (mz_port) {
96 cli_print(gcli, "Note: Set padding to 15 bytes (total length)\n");
97 } else
98 fprintf(stderr, " mz/send_eth: [Note] adjusted minimum frame size to 15 bytes.\n");
101 // Create a temporal, local bytestring:
103 for (i=0; i<MAX_PAYLOAD_SIZE; i++) bytestring[i]=0x00;
104 if ( (getarg(tx.arg_string,"payload", argval)==1) || (getarg(tx.arg_string,"p", argval)==1))
106 bytestring_s = str2hex (argval, bytestring, MAX_PAYLOAD_SIZE);
109 // Set the flags to shorten subsequent decisions:
110 src = strlen(tx.eth_src_txt);
111 dst = strlen(tx.eth_dst_txt);
114 // IN ANY CASE if src has been specified:
116 if (src)
118 // Evaluate Ethernet CLI options (-a and -b)
119 if (check_eth_mac_txt(ETH_SRC)) // if true then problem!
121 // use own (already set in init.c)
123 src_random = tx.eth_src_rand; // local vars are faster
126 // IN ANY CASE if dst has been specified:
128 if (dst)
130 // Evaluate Ethernet CLI options (-a and -b)
131 if (check_eth_mac_txt(ETH_DST)) // if true then problem!
133 str2hex("ff:ff:ff:ff:ff:ff",tx.eth_dst, 6); // the default
135 dst_random = tx.eth_dst_rand; // local vars are faster
139 // Catch errors with too short bytestrings:
141 if (src)
143 // bytestring only needs to contain eth_type
144 min_size-=12;
146 else if (dst)
148 // bytstring must contain src and type
149 min_size-=6;
151 if (bytestring_s < min_size)
153 j = min_size - bytestring_s; // this is what's missing
154 bytestring_s += j; // note that bytestring has been filled up with 0x00, so we can do that
158 // ADDENDUM: If src specified, dst missing:
160 if ( (!dst) && (src) )
162 str2hex_mac ("ff:ff:ff:ff:ff:ff", tx.eth_dst);
167 // ADDENDUM: If dst specified, src missing:
169 if ((dst) && (!src))
171 // Get eth_src from bytestring:
172 if (bytestring_s>=6) {
173 (void) getbytes (bytestring, tx.eth_src, byte_ptr, byte_ptr+5);
174 byte_ptr=7; // now points to eth_type within bytestring
178 // FINALLY: If both dst and src have NOT been specified:
180 if ((!dst) && (!src))
182 if (bytestring_s>=6) {
183 (void) getbytes (bytestring, tx.eth_dst, byte_ptr, byte_ptr+5);
184 byte_ptr=7;
187 if (bytestring_s>=12) {
188 (void) getbytes (bytestring, tx.eth_src, byte_ptr, byte_ptr+5);
189 byte_ptr=13; // points to eth_type
193 // Set eth_type:
195 if (bytestring_s>=2) {
196 tx.eth_type = 256 * bytestring[byte_ptr-1] + bytestring[byte_ptr]; // don't forget: byte_ptr counts from 1 not 0
197 byte_ptr+=2; // points to first payload byte (if available)
201 // Get remaining payload:
203 if ( (tx.eth_payload_s = bytestring_s - byte_ptr +1) > 0 ) // if there are any remaining bytes
205 (void) getbytes (bytestring, tx.eth_payload, byte_ptr, bytestring_s);
210 // Add padding if desired.
211 // Note: padding means 'extend to given length' (not 'add these number of bytes')
212 if (tx.padding)
214 pad = tx.padding - (14 + tx.eth_payload_s); // number of additonal pad bytes required
215 for (i=0; i<pad; i++)
217 // tx.eth_payload[i+tx.eth_payload_s] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
218 tx.eth_payload[i+tx.eth_payload_s] = 0x00;
220 tx.eth_payload_s += pad;
225 ALL_SPECIFIED:
226 // *** All Ethernet parameters have been determined !
227 // *** Now let's send the frame!
229 l = libnet_init (LIBNET_LINK_ADV, tx.device, err_buf );
231 if (l == NULL)
233 fprintf(stderr, " mz/send_eth: libnet_init() failed (%s)", err_buf);
234 return -1;
237 repeat=1;
239 if (tx.count == 0)
241 loop = 1000000;
242 if (!quiet)
243 fprintf(stderr, " mz: !!! Infinite mode! Will send frames until you stop Mausezahn!!!\n");
245 else
246 loop = tx.count;
248 if ( (!quiet) && (!tx.delay) && (tx.count==0) )
249 fprintf(stderr, " mz: !!! Will send at maximum frame rate without feedback!!!\n");
251 t=0;
252 update=1;
254 // this is for the statistics:
255 mz_start = clock();
256 total_d = tx.count;
258 while (repeat)
260 if (tx.count!=0) repeat=0; // count=0 means repeat ad inifinitum
262 for (i=0; i<loop; i++)
264 if (src_random)
266 tx.eth_src[0] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256) & 0xFE;
267 tx.eth_src[1] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
268 tx.eth_src[2] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
269 tx.eth_src[3] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
270 tx.eth_src[4] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
271 tx.eth_src[5] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
272 update=1;
275 if (dst_random)
277 tx.eth_dst[0] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
278 tx.eth_dst[1] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
279 tx.eth_dst[2] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
280 tx.eth_dst[3] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
281 tx.eth_dst[4] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
282 tx.eth_dst[5] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
283 update=1;
286 if (update) // new frame parameters
288 t = libnet_build_ethernet (tx.eth_dst,
289 tx.eth_src,
290 tx.eth_type,
291 tx.eth_payload,
292 tx.eth_payload_s,
294 t);
296 if (t == -1)
298 fprintf(stderr, " mz/send_eth: %s", libnet_geterror(l));
299 return -1;
302 if (verbose)
304 bs2str (tx.eth_dst, message, 6); // DA
305 fprintf(stderr, " mz: send %s",message);
307 bs2str (tx.eth_src, message, 6); // SA
308 fprintf(stderr, " %s",message);
310 type2str(tx.eth_type, message);
311 fprintf(stderr, " %s",message); // Type
313 bs2str (tx.eth_payload, message, tx.eth_payload_s); // Payload
314 fprintf(stderr, " %s\n",message);
316 update=0;
317 if (verbose==2)
319 fprintf(stderr, "\n");
320 fprintf(stderr, "*** NOTE: Simulation only! Nothing has been sent! ***\n");
321 libnet_destroy(l);
322 return 0;
328 libnet_write(l);
330 if (tx.delay)
332 SLEEP (tx.delay);
333 if ( (verbose) && (!src_random) && (!dst_random) )
335 fprintf(stderr, ".");
339 } // end for
341 } // end while
343 if (verbose)
345 if ((tx.delay) || (tx.count==0))
347 fprintf(stderr,"\n");
350 fprintf(stderr, " mz: sent %u frames.\n",loop);
355 libnet_destroy(l);
358 return 0;
363 // ==========================================================================================
366 if (verbose)
368 fprintf(stderr," mz/send_bytes: \n");
369 bs2str(da,dast,6);
370 fprintf(stderr," DA = %s", dast);
371 bs2str(sa,sast,6);
372 fprintf(stderr," SA = %s", sast);
373 fprintf(stderr," type = %x",et);
374 bs2str(payload,pl,payload_s);
375 fprintf(stderr," data = %s\n",pl);