dev: mark paths likely/unlikely
[netsniff-ng.git] / staging / cli_set.c
blob8b365fc855619bf74ec31970e1ae66c49ffb57e9
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
20 #include "mz.h"
21 #include "cli.h"
24 int cmd_set(struct cli_def *cli, const char *command, char *argv[], int argc)
26 libnet_t *l;
27 unsigned int time_factor;
28 int i, cnt, found_dev;
29 char *dum;
30 unsigned char *x;
33 if (argc < 2) {
34 cli_print(cli, "Specify a variable to set:\r\n");
35 cli_print(cli, "device specify the primary network device\r");
37 cli_print(cli, "NOTE: The following options are non-MOPS and deprecated:\n");
39 cli_print(cli, "a|sa specify a MAC source address\r");
40 cli_print(cli, "b|da specify a MAC destination address\r");
41 cli_print(cli, "A|SA specify a IP source address\r");
42 cli_print(cli, "B|DA specify a IP destination address\r");
43 cli_print(cli, "c|count specify a packet count value\r");
44 cli_print(cli, "d|delay specify an interpacket delay (usec, msec, or sec)\r");
45 cli_print(cli, "P|payload specify an ASCII payload\r");
46 cli_print(cli, "H|hexload specify a hexadecimal payload\r");
47 cli_print(cli, "p|padding specify a number of padding bytes (total for raw, added otherwise)\r");
48 cli_print(cli, "Q|vlan specify one ore more 802.1Q vlan tags\r");
49 cli_print(cli, "M|mpls specify one ore more MPLS labels\r");
50 cli_print(cli, "\n");
51 return CLI_OK;
54 // set primary device
55 if (strncmp(argv[0], "device", 2)==0)
57 if (strncmp(argv[1],"?",1)==0)
59 cli_print(cli,"Specify the primary network device (use 'show dev' for a list)\n");
61 else
63 if (strlen(argv[1]))
65 found_dev = 0;
66 for (i=0; i<device_list_entries; i++)
68 if (strncmp(device_list[i].dev, argv[1], 16)==0)
70 found_dev=1;
71 break;
74 if (found_dev)
76 strncpy(tx.device, argv[1], 16);
78 else
79 cli_print(cli, "Unknown device, will stick on %s\n", tx.device);
81 else
82 cli_print(cli, "Nothing specified, will stick on %s\n", tx.device);
87 // set source MAC address
88 else if ( (strncmp(argv[0], "a", 10)==0) ||
89 (strncmp(argv[0], "sa", 10)==0) )
91 if (strncmp(argv[1],"?",1)==0)
93 cli_print(cli,"Specify a source MAC address (format: XX:XX:XX:XX:XX:XX)\n");
95 else
97 strncpy(tx.eth_src_txt, argv[1], 32);
98 if (check_eth_mac_txt(ETH_SRC))
100 cli_print(cli, "Invalid MAC address! Format: XX:XX:XX:XX:XX:XX\r");
101 cli_print(cli, "Current setting: sa = %02x:%02x:%02x:%02x:%02x:%02x\r",
102 tx.eth_src[0], tx.eth_src[1], tx.eth_src[2],
103 tx.eth_src[3], tx.eth_src[4], tx.eth_src[5]);
106 tx.packet_mode = 0;
111 // set destination MAC address
112 else if ( (strncmp(argv[0], "b", 10)==0) ||
113 (strncmp(argv[0], "da", 10)==0) )
115 if (strncmp(argv[1],"?",1)==0)
117 cli_print(cli,"Specify a destination MAC address (format: XX:XX:XX:XX:XX:XX)\n");
119 else
121 strncpy(tx.eth_dst_txt, argv[1], 32);
122 if (check_eth_mac_txt(ETH_DST))
124 cli_print(cli, "Invalid MAC address! Format: XX:XX:XX:XX:XX:XX\r");
125 cli_print(cli, "Current setting: da = %02x:%02x:%02x:%02x:%02x:%02x\r",
126 tx.eth_dst[0], tx.eth_dst[1], tx.eth_dst[2],
127 tx.eth_dst[3], tx.eth_dst[4], tx.eth_dst[5]);
130 tx.packet_mode = 0;
134 // set source IP address
135 else if ( (strncmp(argv[0], "A", 10)==0) ||
136 (strncmp(argv[0], "SA", 10)==0) )
138 if (strncmp(argv[1],"?",1)==0)
140 cli_print(cli,"Specify a source IP address, a FQDN, 'rand', or a range\n");
142 else
144 if (strcmp(argv[1], "rand") == 0)
146 tx.ip_src_rand = 1;
147 tx.ip_src_h = (u_int32_t) ( ((float) rand()/RAND_MAX)*0xE0000000); //this is 224.0.0.0
148 // TODO: mops_hton32 (&tx.ip_src_h, &tx.ip_src);
150 else if (get_ip_range_src(argv[1])) // returns 1 when no range has been specified
152 l = libnet_init (LIBNET_LINK_ADV, tx.device, NULL);
153 if (l == NULL)
155 cli_print(cli, "Error: could not access the network device!\n");
156 return CLI_OK;
158 // name2addr4 accepts a DOTTED DECIMAL ADDRESS or a FQDN:
159 tx.ip_src = libnet_name2addr4 (l, argv[1], LIBNET_RESOLVE);
160 x = (unsigned char *) &tx.ip_src;
161 cli_print(cli, "Set source IP address to %i.%i.%i.%i\n",
162 *x,*(x+1),*(x+2),*(x+3));
163 // TODO: mops_hton32 (&tx.ip_src, &tx.ip_src_h);
164 libnet_destroy(l);
169 // set destination IP address
170 else if ( (strncmp(argv[0], "B", 10)==0) ||
171 (strncmp(argv[0], "DA", 10)==0) )
173 if (strncmp(argv[1],"?",1)==0)
175 cli_print(cli,"Specify a destination IP address, a FQDN, or a range\n");
177 else
179 if (get_ip_range_dst(argv[1])) // returns 1 when no range has been specified
181 l = libnet_init (LIBNET_LINK_ADV, tx.device, NULL);
182 if (l == NULL)
184 cli_print(cli, "Error: could not access the network device!\n");
185 return CLI_OK;
187 // name2addr4 accepts a DOTTED DECIMAL ADDRESS or a FQDN:
188 tx.ip_dst = libnet_name2addr4 (l, argv[1], LIBNET_RESOLVE);
189 x = (unsigned char *) &tx.ip_src;
190 cli_print(cli, "Set destination IP address to %i.%i.%i.%i\n",
191 *x,*(x+1),*(x+2),*(x+3));
192 // TODO: mops_hton32 (&tx.ip_dst, &tx.ip_dst_h);
193 libnet_destroy(l);
198 // set packet count
199 else if ( (strncmp(argv[0], "c", 10)==0) ||
200 (strncmp(argv[0], "count", 10)==0) )
202 if (strncmp(argv[1],"?",1)==0)
204 cli_print(cli,"Specify a packet count value\n");
206 else
208 cnt = (unsigned int) str2int (argv[1]);
209 if (cnt==0)
211 cli_print(cli, "Warning: A packet count of zero means an infinite number of packets.\r");
212 cli_print(cli, "Infinite packets are only supported via MOPS (use the 'packet' command\r");
213 cli_print(cli, "in global configuration mode) or when running Mausezahn from the shell.\n");
214 cli_print(cli, "Note: The count value has NOT been changed.\n");
216 else
218 tx.count = cnt;
223 // set interpacket delay
224 else if ( (strncmp(argv[0], "d", 10)==0) ||
225 (strncmp(argv[0], "delay", 10)==0) )
227 if (strncmp(argv[1],"?",1)==0)
229 cli_print(cli,"Specify an interpacket delay (usec, msec, or sec)\n");
231 else
233 // determine whether seconds or msecs are used
234 // default is usec!!!
235 time_factor=1;
236 if (exists(argv[1],"s") || exists(argv[1],"sec")) time_factor=1000000;
237 if (exists(argv[1],"m") || exists(argv[1],"msec")) time_factor=1000;
238 dum = strtok(argv[1],"ms");
239 tx.delay = strtol(dum, (char **)NULL, 10) * time_factor;
240 if ((errno == ERANGE && (tx.delay == LONG_MAX || tx.delay == LONG_MIN))
241 || (errno != 0 && tx.delay == 0))
243 cli_print(cli, "Value out of range!\n");
245 if (tx.delay<0) tx.delay=0; // no delay
247 cli_print(cli, "Set interpacket delay to %u usec\n", tx.delay);
252 // set ASCII payload
253 else if ( (strncmp(argv[0], "P", 10)==0) ||
254 (strncmp(argv[0], "payload", 10)==0) )
256 if (strncmp(argv[1],"?",1)==0)
258 cli_print(cli,"Specify an ASCII payload enclosed in quotes\n");
260 else
262 strncpy((char *)tx.ascii_payload, argv[1], MAX_PAYLOAD_SIZE);
263 tx.ascii=1;
268 // set HEX payload
269 else if ( (strncmp(argv[0], "H", 10)==0) ||
270 (strncmp(argv[0], "hexload", 10)==0) )
272 if (strncmp(argv[1],"?",1)==0)
274 cli_print(cli,"Specify a hexadecimal payload (using ':' or '.' as delimiters)\n");
276 else
278 tx.hex_payload_s = str2hex (argv[1], tx.hex_payload, 8192);
279 if (tx.hex_payload_s==0)
280 cli_print(cli, "Invalid hexadecimal string. Try something like aa:bb:cc:45:99:00:de:ad:be:ef: ...\n");
285 // set MPLS labels
286 else if ( (strncmp(argv[0], "M", 10)==0) ||
287 (strncmp(argv[0], "mpls", 10)==0) )
289 if (strncmp(argv[1],"?",1)==0)
291 cli_print(cli,"Specify one or more MPLS labels\n");
293 else
295 if (strlen(argv[1])) // TODO: Better verification of 802.1Q syntax
297 strncpy(tx.mpls_txt, argv[1], 128);
298 tx.mpls=1;
304 // set 802.1Q tags
305 else if ( (strncmp(argv[0], "Q", 10)==0) ||
306 (strncmp(argv[0], "vlan", 10)==0) )
308 if (strncmp(argv[1],"?",1)==0)
310 cli_print(cli,"Specify one or more 802.1Q VLAN tags (and optionally 801.1P values)\n");
312 else
314 if (strlen(argv[1])) // TODO: Better verification of 802.1Q syntax
316 strncpy(tx.dot1Q_txt, argv[1], 32);
317 tx.dot1Q=1;
323 // set padding
324 else if ( (strncmp(argv[0], "p", 10)==0) ||
325 (strncmp(argv[0], "padding", 10)==0) )
327 if (strncmp(argv[1],"?",1)==0)
329 cli_print(cli,"Specify a number of padding bytes\n");
331 else
333 tx.padding = (unsigned int) str2int(argv[1]);
334 if (tx.padding > MAX_PAYLOAD_SIZE)
336 cli_print(cli, "Note: Padding too big! However, let's try and see what happens...\n");
343 // DEFAULT ANSWER:
344 else
346 cli_print(cli, "Unknown variable '%s'\n",argv[0]);
349 return CLI_OK;