xio: refactor fopencookie related functions
[netsniff-ng.git] / staging / cli_lldp.c
blob899113735caf4de77b4a843cfa565a51f4a9a476
1 /*
2 * Mausezahn - A fast versatile traffic generator
3 * Copyright (C) 2008-2010 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 #include "mz.h"
22 #include "cli.h"
23 #include "mops.h"
26 int cmd_lldp_conformance (struct cli_def *cli, const char *command, char *argv[], int argc)
28 struct mops_ext_lldp * pd = clipkt->p_desc;
31 if ( (strcmp(argv[argc-1],"?")==0) || (argc!=1)) {
32 cli_print(cli, "Enables or disables LLDP standard conformance mode.\n");
33 cli_print(cli, "Keywords: enable | disable\n");
34 cli_print(cli, "Per default, standard LLDP messages are created which require a fixed\r");
35 cli_print(cli, "order of the mandatory TLVs. If the standard conformance mode is disabled\r");
36 cli_print(cli, "then you can configure an arbitrary sequence of LLDP TLVs. \n");
37 cli_print(cli, "Currently, the LLDP standard conformance mode is %s\n", (pd->non_conform) ? "DISABLED" : "ENABLED");
38 return CLI_OK;
41 if (mz_strcmp(argv[0], "enable", 1)==0) {
42 pd->non_conform = 0;
43 return CLI_OK;
47 if (mz_strcmp(argv[0], "disable", 1)==0) {
48 pd->non_conform = 1;
49 return CLI_OK;
52 cli_print(cli, "Enter enable or disable\n");
53 return CLI_OK;
57 int cmd_lldp_chassis_id (struct cli_def *cli, const char *command, char *argv[], int argc)
59 struct mops_ext_lldp * pd = clipkt->p_desc;
60 int subtype = 4;
61 char *cid;
62 int cl, cidl;
63 u_int8_t tmp[512];
65 if ( (strcmp(argv[argc-1],"?")==0) || (argc>2)) {
66 cli_print(cli, "Configure a Chassis ID TLV.\n");
67 cli_print(cli, "ARGUMENTS: [<subtype>] <chassis-id>\n");
68 cli_print(cli, "By default the <subtype> is of kind 'mac address (4)' and the <chassis-id>\r");
69 cli_print(cli, "must be a hexadecimal string (e. g. 00:01:ca:fe:de:ad) of max 255 bytes\n");
70 return CLI_OK;
73 if (argc==2) {
74 subtype = (int) str2int(argv[0]);
75 if ((subtype>255) || (mz_strisnum(argv[0])==0)) {
76 cli_print(cli, "Invalid subtype\n");
77 return CLI_OK;
79 cid = argv[1];
80 } else
81 cid = argv[0];
83 cl=strnlen(cid, 1024);
85 if (cl>=1024) {
86 cli_print(cli, "Chassis-ID too long\n");
87 return CLI_OK;
88 } else cidl=str2hex(cid, tmp, 511);
90 if (pd->non_conform == 0) {
91 pd->chassis_id_subtype = subtype;
92 memcpy((void*) pd->chassis_id, (void*)tmp, cidl);
93 pd->chassis_id_len = cidl;
94 } else {
95 // non_conform
96 mops_lldp_opt_tlv_chassis (clipkt, subtype, cidl, tmp);
99 return CLI_OK;
105 int cmd_lldp_port_id (struct cli_def *cli, const char *command, char *argv[], int argc)
107 struct mops_ext_lldp * pd = clipkt->p_desc;
108 int subtype = 4;
109 char *pid;
110 int pl;
112 if ( (strcmp(argv[argc-1],"?")==0) || (argc>2)) {
113 cli_print(cli, "Configure a Port ID TLV.\n");
114 cli_print(cli, "ARGUMENTS: [<subtype>] <port-id>\n");
115 cli_print(cli, "By default the <subtype> is of kind 'Interface name (5)' and the <port-id>\r");
116 cli_print(cli, "must be a ascii string (usually the name of the interface e. g. eth3) of\r");
117 cli_print(cli, "max 255 bytes.\n");
118 return CLI_OK;
121 if (argc==2) {
122 subtype = (int) str2int(argv[0]);
123 if ((subtype>255) || (mz_strisnum(argv[0])==0)) {
124 cli_print(cli, "Invalid subtype\n");
125 return CLI_OK;
127 pid = argv[1];
128 } else
129 pid = argv[0];
131 pl=strnlen(pid, 256);
133 if (pl>255) {
134 cli_print(cli, "Port-ID too long\n");
135 return CLI_OK;
139 if (pd->non_conform == 0) {
140 pd->port_id_subtype = subtype;
141 memcpy((void*) pd->port_id, (void*) pid, pl);
142 pd->port_id_len = pl;
143 } else {
144 // non_conform
145 mops_lldp_opt_tlv_port (clipkt, subtype, pl, (u_int8_t*) pid);
148 return CLI_OK;
153 int cmd_lldp_ttl (struct cli_def *cli, const char *command, char *argv[], int argc)
155 struct mops_ext_lldp * pd = clipkt->p_desc;
156 int ttl;
158 if ( (strcmp(argv[argc-1],"?")==0) || (argc!=1)) {
159 cli_print(cli, "Configure the LLDP TTL.\n");
160 cli_print(cli, "ARGUMENTS: <time-to-live>\n");
161 cli_print(cli, "The TTL must be within 0..65535\n");
162 return CLI_OK;
165 ttl = (int) str2int(argv[0]);
167 if (mz_strisnum(argv[0])==0) {
168 cli_print(cli, "Invalid argument\n");
169 return CLI_OK;
172 if (ttl>0xffff) {
173 cli_print(cli, "TTL must be within 0..65535\n");
174 return CLI_OK;
177 if (pd->non_conform == 0) {
178 pd->TTL = ttl;
179 } else {
180 // non_conform
181 mops_lldp_opt_tlv_TTL (clipkt, ttl);
184 return CLI_OK;
189 int cmd_lldp_vlan (struct cli_def *cli, const char *command, char *argv[], int argc)
191 int vlan;
193 if ( (strcmp(argv[argc-1],"?")==0) || (argc!=1)) {
194 cli_print(cli, "Configure the LLDP Port VLAN-ID.\n");
195 cli_print(cli, "ARGUMENTS: <vlan-id>\n");
196 cli_print(cli, "The vlan-id must be within 0..65535\n");
197 return CLI_OK;
200 vlan = (int) str2int(argv[0]);
202 if (mz_strisnum(argv[0])==0) {
203 cli_print(cli, "Invalid argument\n");
204 return CLI_OK;
207 if (vlan>0xffff) {
208 cli_print(cli, "The VLAN-ID must be within 0..65535\n");
209 return CLI_OK;
213 mops_lldp_opt_tlv_vlan (clipkt, vlan);
215 return CLI_OK;
220 int cmd_lldp_opt_tlv (struct cli_def *cli, const char *command, char *argv[], int argc)
222 int type=0, len=0;
223 u_int8_t tmp[512];
226 if ( (strcmp(argv[argc-1],"?")==0) || (argc!=3)) {
227 cli_print(cli, "Configure an arbitrary optional TLV.\n");
228 cli_print(cli, "ARGUMENTS: ascii|hex <type> <value>\n");
229 cli_print(cli, "The TLV type must be between 0..127, the value length is up to 511 bytes.\n");
230 return CLI_OK;
234 if (mz_strcmp(argv[0], "ascii", 1)==0) {
235 if ((len=strnlen(argv[2],512))>511) {
236 cli_print(cli, "<value> must be smaller or equal 511 characters\n");
237 return CLI_OK;
239 mz_strncpy((char*) tmp, argv[2], 511);
240 } else if (mz_strcmp(argv[0], "hex", 1)==0) {
241 len=str2hex(argv[2], tmp, 512);
242 if (len>511) {
243 cli_print(cli, "<value> must be smaller or equal 511 bytes\n");
244 return CLI_OK;
248 type = (int) str2int(argv[1]);
250 if (mz_strisnum(argv[1])==0) {
251 cli_print(cli, "Invalid type\n");
252 return CLI_OK;
255 if (type>127) {
256 cli_print(cli, "<type> must be within 0..127\n");
257 return CLI_OK;
261 if (mops_lldp_opt_tlv (clipkt, type, len, tmp)==0)
262 cli_print(cli, "Invalid TLV values\n");
264 return CLI_OK;
270 int cmd_lldp_opt_tlv_bad (struct cli_def *cli, const char *command, char *argv[], int argc)
272 int type, len=0, wronglen;
273 u_int8_t tmp[512];
276 if ( (strcmp(argv[argc-1],"?")==0) || (argc!=4)) {
277 cli_print(cli, "Configure an arbitrary optional *BAD* TLV.\n");
278 cli_print(cli, "ARGUMENTS: ascii|hex <type> <wrong-length> <value>\n");
279 cli_print(cli, "Using this command you can add a custom TLV with a wrong length parameter.\r");
280 cli_print(cli, "Such TLV can be used to verify whether LLDP receivers are robust enough\r\r");
281 cli_print(cli, "since a too small <wrong-length> could cause a buffer overflow. The TLV type\r");
282 cli_print(cli, "must be between 0..127, the <wrong-length> can be within 0..511 (and can be\r");
283 cli_print(cli, "also the true length of course\n");
284 return CLI_OK;
288 if (mz_strcmp(argv[0], "ascii", 1)==0) {
289 if ((len=strnlen(argv[3],512))>511) {
290 cli_print(cli, "<value> must be smaller or equal 511 characters\n");
291 return CLI_OK;
293 mz_strncpy((char*) tmp, argv[3], 511);
294 } else if (mz_strcmp(argv[0], "hex", 1)==0) {
295 len=str2hex(argv[3], tmp, 512);
296 if (len>511) {
297 cli_print(cli, "<value> must be smaller or equal 511 bytes\n");
298 return CLI_OK;
302 type = (int) str2int(argv[1]);
304 if (mz_strisnum(argv[1])==0) {
305 cli_print(cli, "Invalid type\n");
306 return CLI_OK;
309 if (type>127) {
310 cli_print(cli, "<type> must be within 0..127\n");
311 return CLI_OK;
314 wronglen = (int) str2int(argv[2]);
316 if (mz_strisnum(argv[2])==0) {
317 cli_print(cli, "Invalid length\n");
318 return CLI_OK;
321 if (wronglen>511) {
322 cli_print(cli, "<wrong-length> must be within 0..511\n");
323 return CLI_OK;
326 if (mops_lldp_opt_tlv_bad (clipkt, type, wronglen, len, tmp)==0)
327 cli_print(cli, "Invalid TLV values\n");
329 return CLI_OK;
334 int cmd_lldp_opt_org (struct cli_def *cli, const char *command, char *argv[], int argc)
336 int subtype, len=0, oui=0;
337 u_int8_t tmp[512];
340 if ( (strcmp(argv[argc-1],"?")==0) || (argc!=4)) {
341 cli_print(cli, "Configure an organisational TLV.\n");
342 cli_print(cli, "ARGUMENTS: ascii|hex <oui> <subtype> <value>\n");
343 cli_print(cli, "Using this command you can add an arbitrary organisational TLV. The <oui> represents\r");
344 cli_print(cli, "the 'Organisational Unique Identifier' and consists of exactly three bytes in hexadecimal\r");
345 cli_print(cli, "format, such as '00005e' The <subtype> is a value between <0..255>, and the length of the\r");
346 cli_print(cli, "value is up to 507 bytes.\n");
347 return CLI_OK;
351 if (mz_strcmp(argv[0], "ascii", 1)==0) {
352 if ((len=strnlen(argv[3],512))>511) {
353 cli_print(cli, "<value> must be smaller or equal 511 characters\n");
354 return CLI_OK;
356 mz_strncpy((char*) tmp, argv[3], 511);
357 } else if (mz_strcmp(argv[0], "hex", 1)==0) {
358 len=str2hex(argv[3], tmp, 512);
359 if (len>511) {
360 cli_print(cli, "<value> must be smaller or equal 511 bytes\n");
361 return CLI_OK;
365 oui = xstr2int(argv[1]);
366 if (mz_strishex(argv[1])==0) {
367 cli_print(cli, "Invalid oui value\n");
368 return CLI_OK;
371 if (oui>0xffffff) {
372 cli_print(cli, "<oui> must be within 0..ffffff\n");
373 return CLI_OK;
376 subtype = (int) str2int(argv[2]);
378 if (mz_strisnum(argv[2])==0) {
379 cli_print(cli, "Invalid subtype\n");
380 return CLI_OK;
383 if (subtype>255) {
384 cli_print(cli, "<subtype> must be within 0..255\n");
385 return CLI_OK;
389 if (mops_lldp_opt_tlv_org (clipkt, oui, subtype, len, tmp)==0)
390 cli_print(cli, "Invalid TLV values\n");
392 return CLI_OK;
399 int cmd_lldp_endtlv (struct cli_def *cli, const char *command, char *argv[], int argc)
402 if ( (strcmp(argv[argc-1],"?")==0) || (argc>0)) {
403 cli_print(cli, "Add an 'End of LLDP' TLV\n");
404 cli_print(cli, "ARGUMENTS: none\n");
405 cli_print(cli, "This command allows you to insert an 'End of LLDP' TLV at any\r");
406 cli_print(cli, "point within the optional TLV list. You usually want this to\r");
407 cli_print(cli, "create an invalid LLDPU to test the receiver.\n");
408 return CLI_OK;
412 mops_lldp_opt_tlv_end (clipkt);
414 return CLI_OK;
418 int cmd_lldp_reset (struct cli_def *cli, const char *command, char *argv[], int argc)
421 if ( (strcmp(argv[argc-1],"?")==0) || (argc>0)) {
422 cli_print(cli, "Reset the LLPDU and clear all optional TLVs.\n");
423 cli_print(cli, "ARGUMENTS: none\n");
424 cli_print(cli, "All optional TLVs are added in the sequence as you configure them.\r");
425 cli_print(cli, "Use this command to reset the LLDP and reconfigure all optional\r");
426 cli_print(cli, "TLVs again. Additionally the parameters of the mandatory TLVs are\r");
427 cli_print(cli, "reset to defaults.\n");
428 return CLI_OK;
431 mops_init_pdesc_lldp(clipkt);
433 return CLI_OK;