summaryrefslogtreecommitdiff
path: root/staging/cli_set.c
blob: 8b365fc855619bf74ec31970e1ae66c49ffb57e9 (plain)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * Mausezahn - A fast versatile traffic generator
 * Copyright (C) 2008 Herbert Haas
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the 
 * Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
 * 
*/


#include "mz.h"
#include "cli.h"


int cmd_set(struct cli_def *cli, const char *command, char *argv[], int argc)
{
   libnet_t       *l;
   unsigned int time_factor;   
   int i, cnt, found_dev;
   char *dum;
   unsigned char *x;

	
   if (argc < 2) {
       cli_print(cli, "Specify a variable to set:\r\n");
       cli_print(cli, "device      specify the primary network device\r");
	    
       cli_print(cli, "NOTE: The following options are non-MOPS and deprecated:\n");
	    
       cli_print(cli, "a|sa        specify a MAC source address\r");
       cli_print(cli, "b|da        specify a MAC destination address\r");
       cli_print(cli, "A|SA        specify a IP source address\r");
       cli_print(cli, "B|DA        specify a IP destination address\r");
       cli_print(cli, "c|count     specify a packet count value\r");
       cli_print(cli, "d|delay     specify an interpacket delay (usec, msec, or sec)\r");
       cli_print(cli, "P|payload   specify an ASCII payload\r");
       cli_print(cli, "H|hexload   specify a hexadecimal payload\r");
       cli_print(cli, "p|padding   specify a number of padding bytes (total for raw, added otherwise)\r");
       cli_print(cli, "Q|vlan      specify one ore more 802.1Q vlan tags\r");
       cli_print(cli, "M|mpls      specify one ore more MPLS labels\r");
       cli_print(cli, "\n");
	return CLI_OK;
    }
   
   // set primary device
   if  (strncmp(argv[0], "device", 2)==0) 
     {
	if (strncmp(argv[1],"?",1)==0)
	  {
	     cli_print(cli,"Specify the primary network device (use 'show dev' for a list)\n");
	  }
	else
	  {
	     if (strlen(argv[1])) 
	       {
		  found_dev = 0;
		  for (i=0; i<device_list_entries; i++)
		    {
		       if (strncmp(device_list[i].dev, argv[1], 16)==0) 
			 { 
			    found_dev=1;
			    break;
			 }
		    }
		  if (found_dev)
		    {
		       strncpy(tx.device, argv[1], 16);
		    }
		  else
		    cli_print(cli, "Unknown device, will stick on %s\n", tx.device);
	       }
	     else
	       cli_print(cli, "Nothing specified, will stick on %s\n", tx.device);
	  }
     }
	
	
   // set source MAC address
   else if ( (strncmp(argv[0], "a", 10)==0) ||
	     (strncmp(argv[0], "sa", 10)==0) )
     {
	if (strncmp(argv[1],"?",1)==0)
	  {
	     cli_print(cli,"Specify a source MAC address (format: XX:XX:XX:XX:XX:XX)\n");
	  }
	else
	  {
	     strncpy(tx.eth_src_txt, argv[1], 32);
	     if (check_eth_mac_txt(ETH_SRC))
	       {
		  cli_print(cli, "Invalid MAC address! Format: XX:XX:XX:XX:XX:XX\r");
		  cli_print(cli, "Current setting: sa = %02x:%02x:%02x:%02x:%02x:%02x\r", 
			    tx.eth_src[0], tx.eth_src[1], tx.eth_src[2],
			    tx.eth_src[3], tx.eth_src[4], tx.eth_src[5]);
	       }
	     
	     tx.packet_mode = 0;

	  }
     }
   
   // set destination MAC address
   else if ( (strncmp(argv[0], "b", 10)==0) ||
	     (strncmp(argv[0], "da", 10)==0) )
     {
	if (strncmp(argv[1],"?",1)==0)
	  {
	     cli_print(cli,"Specify a destination MAC address (format: XX:XX:XX:XX:XX:XX)\n");
	  }
	else
	  {
	     strncpy(tx.eth_dst_txt, argv[1], 32);
	     if (check_eth_mac_txt(ETH_DST))
	       {
		  cli_print(cli, "Invalid MAC address! Format: XX:XX:XX:XX:XX:XX\r");
		  cli_print(cli, "Current setting: da = %02x:%02x:%02x:%02x:%02x:%02x\r", 
			    tx.eth_dst[0], tx.eth_dst[1], tx.eth_dst[2],
			    tx.eth_dst[3], tx.eth_dst[4], tx.eth_dst[5]);
	       }
	     
	     tx.packet_mode = 0;
	  }
     }
   
   // set source IP address
   else if ( (strncmp(argv[0], "A", 10)==0) ||
	     (strncmp(argv[0], "SA", 10)==0) )
     {
	if (strncmp(argv[1],"?",1)==0)
	  {
	     cli_print(cli,"Specify a source IP address, a FQDN, 'rand', or a range\n");
	  }
	else
	  {
	     if (strcmp(argv[1], "rand") == 0)
	       {
		  tx.ip_src_rand = 1;
		  tx.ip_src_h  = (u_int32_t) ( ((float) rand()/RAND_MAX)*0xE0000000); //this is 224.0.0.0
// TODO:	  mops_hton32 (&tx.ip_src_h, &tx.ip_src);
	       }
	     else if (get_ip_range_src(argv[1])) // returns 1 when no range has been specified
	       {
		  l = libnet_init (LIBNET_LINK_ADV, tx.device, NULL);
		  if (l == NULL)
		    {
		       cli_print(cli, "Error: could not access the network device!\n");
		       return CLI_OK;
		    }
		  // name2addr4 accepts a DOTTED DECIMAL ADDRESS or a FQDN:
		  tx.ip_src = libnet_name2addr4 (l, argv[1], LIBNET_RESOLVE);
		  x = (unsigned char *) &tx.ip_src;
		  cli_print(cli, "Set source IP address to %i.%i.%i.%i\n",
			    *x,*(x+1),*(x+2),*(x+3));
// TODO:	  mops_hton32 (&tx.ip_src, &tx.ip_src_h);
		  libnet_destroy(l);
	       }
	  }
     }
   
   // set destination IP address
   else if ( (strncmp(argv[0], "B", 10)==0) ||
	     (strncmp(argv[0], "DA", 10)==0) )
     {
	if (strncmp(argv[1],"?",1)==0)
	  {
	     cli_print(cli,"Specify a destination IP address, a FQDN, or a range\n");
	  }
	else
	  {
	     if (get_ip_range_dst(argv[1])) // returns 1 when no range has been specified
	       {
		  l = libnet_init (LIBNET_LINK_ADV, tx.device, NULL);
		  if (l == NULL)
		    {
		       cli_print(cli, "Error: could not access the network device!\n");
		       return CLI_OK;
		    }
		  // name2addr4 accepts a DOTTED DECIMAL ADDRESS or a FQDN:
		  tx.ip_dst = libnet_name2addr4 (l, argv[1], LIBNET_RESOLVE);
		  x = (unsigned char *) &tx.ip_src;
		  cli_print(cli, "Set destination IP address to %i.%i.%i.%i\n",
			    *x,*(x+1),*(x+2),*(x+3));
// TODO:          mops_hton32 (&tx.ip_dst, &tx.ip_dst_h);
		  libnet_destroy(l);
	       }
	  }
     }
   
   // set packet count
   else if ( (strncmp(argv[0], "c", 10)==0) ||
	     (strncmp(argv[0], "count", 10)==0) )
     {
	if (strncmp(argv[1],"?",1)==0)
	  {
	     cli_print(cli,"Specify a packet count value\n");
	  }
	else
	  {
	     cnt = (unsigned int) str2int (argv[1]);
	     if (cnt==0)
	       {
		  cli_print(cli, "Warning: A packet count of zero means an infinite number of packets.\r");
		  cli_print(cli, "Infinite packets are only supported via MOPS (use the 'packet' command\r");
		  cli_print(cli, "in global configuration mode) or when running Mausezahn from the shell.\n");
		  cli_print(cli, "Note: The count value has NOT been changed.\n");
	       }
	     else
	       {
		  tx.count = cnt;
	       }
	  }
     }
   
   // set interpacket delay
   else if ( (strncmp(argv[0], "d", 10)==0) ||
	     (strncmp(argv[0], "delay", 10)==0) )
     {
	if (strncmp(argv[1],"?",1)==0)
	  {
	     cli_print(cli,"Specify an interpacket delay (usec, msec, or sec)\n");
	  }
	else
	  {
	     // determine whether seconds or msecs are used
	     // default is usec!!!
	     time_factor=1;
	     if (exists(argv[1],"s") || exists(argv[1],"sec")) time_factor=1000000;
	     if (exists(argv[1],"m") || exists(argv[1],"msec")) time_factor=1000;
	     dum = strtok(argv[1],"ms");
	     tx.delay = strtol(dum, (char **)NULL, 10) * time_factor;
	     if ((errno == ERANGE && (tx.delay == LONG_MAX || tx.delay == LONG_MIN))
		 || (errno != 0 && tx.delay == 0)) 
	       {
		  cli_print(cli, "Value out of range!\n");
	       }
	     if (tx.delay<0) tx.delay=0; // no delay
	     
	     cli_print(cli, "Set interpacket delay to %u usec\n", tx.delay);
	  }

     }
   
   // set ASCII payload
   else if ( (strncmp(argv[0], "P", 10)==0) ||
	     (strncmp(argv[0], "payload", 10)==0) )
     {
	if (strncmp(argv[1],"?",1)==0)
	  {
	     cli_print(cli,"Specify an ASCII payload enclosed in quotes\n");
	  }
	else
	  {
	     strncpy((char *)tx.ascii_payload, argv[1], MAX_PAYLOAD_SIZE);
	     tx.ascii=1;
	  }
     }
   
   
   // set HEX payload
   else if ( (strncmp(argv[0], "H", 10)==0) ||
	     (strncmp(argv[0], "hexload", 10)==0) )
     {
	if (strncmp(argv[1],"?",1)==0)
	  {
	     cli_print(cli,"Specify a hexadecimal payload (using ':' or '.' as delimiters)\n");
	  }
	else
	  {
	     tx.hex_payload_s = str2hex (argv[1], tx.hex_payload, 8192);
	     if (tx.hex_payload_s==0)
	       cli_print(cli, "Invalid hexadecimal string. Try something like aa:bb:cc:45:99:00:de:ad:be:ef: ...\n");
	  }
     }
   
   
   // set MPLS labels
   else if ( (strncmp(argv[0], "M", 10)==0) ||
	     (strncmp(argv[0], "mpls", 10)==0) )
     {
	if (strncmp(argv[1],"?",1)==0)
	  {
	     cli_print(cli,"Specify one or more MPLS labels\n");
	  }
	else
	  {
	     if (strlen(argv[1])) // TODO: Better verification of 802.1Q syntax
		 {
		    strncpy(tx.mpls_txt, argv[1], 128);
		    tx.mpls=1;
		 }
	  }
     }
   
   
   // set 802.1Q tags
   else if ( (strncmp(argv[0], "Q", 10)==0) ||
	     (strncmp(argv[0], "vlan", 10)==0) )
     {
	if (strncmp(argv[1],"?",1)==0)
	  {
	     cli_print(cli,"Specify one or more 802.1Q VLAN tags (and optionally 801.1P values)\n");
	  }
	else
	  {
	     if (strlen(argv[1])) // TODO: Better verification of 802.1Q syntax
		 {
		    strncpy(tx.dot1Q_txt, argv[1], 32);
		    tx.dot1Q=1;
		 }
	  }
     }
   
   
   // set padding
   else if ( (strncmp(argv[0], "p", 10)==0) ||
	     (strncmp(argv[0], "padding", 10)==0) )
     {
	if (strncmp(argv[1],"?",1)==0)
	  {
	     cli_print(cli,"Specify a number of padding bytes\n");
	  }
	else
	  {
	     tx.padding = (unsigned int) str2int(argv[1]);
	     if (tx.padding > MAX_PAYLOAD_SIZE)
	       {
		  cli_print(cli, "Note: Padding too big! However, let's try and see what happens...\n");
	       }
	  }
     }


   
   // DEFAULT ANSWER:
   else
     {
	cli_print(cli, "Unknown variable '%s'\n",argv[0]);
     }
   
    return CLI_OK;
}