-
Notifications
You must be signed in to change notification settings - Fork 8
/
lldptool.c
805 lines (711 loc) · 20 KB
/
lldptool.c
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*******************************************************************************
LLDP Agent Daemon (LLDPAD) Software
Copyright(c) 2007-2012 Intel Corporation.
Substantially modified from:
hostapd-0.5.7
Copyright (c) 2002-2007, Jouni Malinen <[email protected]> and
contributors
This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU General Public License,
version 2, as published by the Free Software Foundation.
This program is distributed in the hope 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, write to the Free Software Foundation, Inc.,
51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
The full GNU General Public License is included in this distribution in
the file called "COPYING".
Contact Information:
open-lldp Mailing List <[email protected]>
*******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <signal.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "clif.h"
#include "lldp_mand_clif.h"
#include "lldp_basman_clif.h"
#include "lldp_med_clif.h"
#include "lldp_8023_clif.h"
#include "lldp_dcbx_clif.h"
#include "lldp_evb22_clif.h"
#include "lldp_evb_clif.h"
#include "qbg_vdp_clif.h"
#include "lldp_8021qaz_clif.h"
#include "lldp_orgspec_clif.h"
#include "lldp_cisco_clif.h"
#include "lldptool.h"
#include "lldptool_cli.h"
#include "lldp_mod.h"
#include "lldp_mand.h"
#include "lldp_basman.h"
#include "lldp_med.h"
#include "lldp_dcbx.h"
#include "version.h"
#include "lldpad.h"
#include "lldp_util.h"
#include "lldpad_status.h"
static int show_raw;
static const char *cli_version =
"lldptool v" LLDPTOOL_VERSION "\n"
"Copyright (c) 2007-2010, Intel Corporation\n"
"\nSubstantially modified from: hostapd_cli v 0.5.7\n"
"Copyright (c) 2004-2007, Jouni Malinen <[email protected]> and contributors";
static const char *cli_license =
"This program is free software. You can distribute it and/or modify it\n"
"under the terms of the GNU General Public License version 2.\n"
"\n";
/*
"Alternatively, this software may be distributed under the terms of the\n"
"BSD license. See README and COPYING for more details.\n";
*/
static const char *cli_full_license =
"This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License version 2 as\n"
"published by the Free Software Foundation.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
"Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
"\n"
"Alternatively, this software may be distributed under the terms of the\n"
"BSD license.\n"
"\n"
"Redistribution and use in source and binary forms, with or without\n"
"modification, are permitted provided that the following conditions are\n"
"met:\n"
"\n"
"1. Redistributions of source code must retain the above copyright\n"
" notice, this list of conditions and the following disclaimer.\n"
"\n"
"2. Redistributions in binary form must reproduce the above copyright\n"
" notice, this list of conditions and the following disclaimer in the\n"
" documentation and/or other materials provided with the distribution.\n"
"\n"
"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
" names of its contributors may be used to endorse or promote products\n"
" derived from this software without specific prior written permission.\n"
"\n"
"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
"\n";
static const char *commands_usage =
"Usage:\n"
" lldptool <command> [options] [arg] general command line usage format\n"
" lldptool go into interactive mode\n"
" <command> [options] [arg] general interactive command format\n";
static const char *commands_options =
"Options:\n"
" -i [ifname] network interface\n"
" -V [tlvid] TLV identifier\n"
" may be numeric or keyword (see below)\n"
" -c <argument list> used with get TLV command to specify\n"
" that the list of configuration elements\n"
" should be retrieved\n"
" -d use to delete specified argument from\n"
" the configuration. (Currently\n"
" implemented for DCBX App TLV settings)\n"
" -n \"neighbor\" option for command\n"
" -r show raw message\n"
" -R show only raw messages\n"
" -g destination agent (may be one of):\n"
" - nearestbridge (nb) (default)\n"
" - nearestcustomerbridge (ncb)\n"
" - nearestnontpmrbridge (nntpmrb)\n";
static const char *commands_help =
"Commands:\n"
" license show license information\n"
" -h|help show command usage information\n"
" -v|version show version\n"
" -p|ping ping lldpad and query pid of lldpad\n"
" -q|quit exit lldptool (interactive mode)\n"
" -S|stats get LLDP statistics for ifname\n"
" -t|get-tlv get TLVs from ifname\n"
" -T|set-tlv set arg for tlvid to value\n"
" -l|get-lldp get the LLDP parameters for ifname\n"
" -L|set-lldp set the LLDP parameter for ifname\n";
static struct clif *clif_conn;
static int cli_quit = 0;
static int cli_attached = 0;
/*
* insert to head, so first one is last
*/
struct lldp_module *(*register_tlv_table[])(void) = {
mand_cli_register,
basman_cli_register,
ieee8023_cli_register,
med_cli_register,
dcbx_cli_register,
evb22_cli_register,
evb_cli_register,
vdp_cli_register,
ieee8021qaz_cli_register,
orgspec_cli_register,
cisco_cli_register,
NULL,
};
static void init_modules(void)
{
struct lldp_module *module;
struct lldp_module *premod = NULL;
int i = 0;
LIST_INIT(&lldp_cli_head);
for (i = 0; register_tlv_table[i]; i++) {
module = register_tlv_table[i]();
if (premod)
LIST_INSERT_AFTER(premod, module, lldp);
else
LIST_INSERT_HEAD(&lldp_head, module, lldp);
premod = module;
}
}
void deinit_modules(void)
{
struct lldp_module *module;
while (lldp_head.lh_first != NULL) {
module = lldp_head.lh_first;
LIST_REMOVE(lldp_head.lh_first, lldp);
module->ops->lldp_mod_unregister(module);
}
}
static void usage(void)
{
fprintf(stderr, "%s\n", cli_version);
fprintf(stderr, "\n%s\n%s\n%s\n",
commands_usage, commands_options, commands_help);
}
/* assumes input is pointer to two hex digits */
/* returns -1 on error */
int hex2int(char *b)
{
int i;
int n=0;
int m;
for (i=0,m=1; i<2; i++,m--) {
if (isxdigit(*(b+i))) {
if (*(b+i) <= '9')
n |= (*(b+i) & 0x0f) << (4*m);
else
n |= ((*(b+i) & 0x0f) + 9) << (4*m);
}
else {
return -1;
}
}
return n;
}
void print_raw_message(char *msg, int print)
{
if (!print || !(print & SHOW_RAW))
return;
if (!(print & SHOW_RAW_ONLY)) {
switch (msg[MSG_TYPE]) {
case EVENT_MSG:
printf("event: ");
break;
case CMD_RESPONSE:
printf("rsp: ");
break;
default:
printf("cmd: ");
break;
}
}
printf("%s\n", msg);
}
int parse_print_message(char *msg, int print)
{
int status = 0;
status = parse_response(msg);
print_raw_message(msg, print);
if (print & SHOW_RAW_ONLY)
return status;
if (msg[MSG_TYPE] == CMD_RESPONSE)
print_response(msg, status);
else if (msg[MSG_TYPE] == MOD_CMD && msg[MOD_MSG_TYPE] == EVENT_MSG)
print_event_msg(&msg[MOD_MSG_TYPE]);
return status;
}
static void cli_close_connection(void)
{
if (clif_conn == NULL)
return;
if (cli_attached) {
clif_detach(clif_conn);
cli_attached = 0;
}
clif_close(clif_conn);
clif_conn = NULL;
}
static void cli_msg_cb(char *msg, UNUSED size_t len)
{
parse_print_message(msg, SHOW_OUTPUT | show_raw);
}
/* structure of the print argument bitmap:
* SHOW_NO_OUTPUT (0x0) - don't print anything for the command
* SHOW_OUTPUT (0x01) - print output for the command
* SHOW_RAW (0x02) - print the raw clif command messages
* SHOW_RAW_ONLY (0x04) - print only the raw clif command messages
*/
static int _clif_command(struct clif *clif, char *cmd, int print)
{
char buf[MAX_CLIF_MSGBUF];
size_t len;
int ret;
print_raw_message(cmd, print);
if (clif_conn == NULL) {
printf("Not connected to lldpad - command dropped.\n");
return -1;
}
len = sizeof(buf) - 1;
ret = clif_request(clif, cmd, strlen(cmd), buf, &len,
cli_msg_cb);
if (ret == -2) {
printf("'%s' command timed out.\n", cmd);
return -2;
} else if (ret < 0) {
printf("'%s' command failed.\n", cmd);
return -1;
}
if (print) {
buf[len] = '\0';
ret = parse_print_message(buf, print);
}
return ret;
}
inline int clif_command(struct clif *clif, char *cmd, int raw)
{
return _clif_command(clif, cmd, SHOW_OUTPUT | raw);
}
static int cli_cmd_ping(struct clif *clif, UNUSED int argc, UNUSED char *argv[],
UNUSED struct cmd *command, int raw)
{
return clif_command(clif, "P", raw);
}
static int
cli_cmd_nop(UNUSED struct clif *clif, UNUSED int argc, UNUSED char *argv[],
UNUSED struct cmd *command, UNUSED int raw)
{
return 0;
}
static int
cli_cmd_help(UNUSED struct clif *clif, UNUSED int argc, UNUSED char *argv[],
UNUSED struct cmd *command, UNUSED int raw)
{
struct lldp_module *np;
printf("%s\n%s\n%s", commands_usage, commands_options, commands_help);
printf("\nTLV identifiers:\n");
LIST_FOREACH(np, &lldp_head, lldp)
if (np->ops->print_help)
np->ops->print_help();
return 0;
}
static int
cli_cmd_version(UNUSED struct clif *clif, UNUSED int argc, UNUSED char *argv[],
UNUSED struct cmd *command, UNUSED int raw)
{
printf("%s\n", cli_version);
return 0;
}
static int
cli_cmd_license(UNUSED struct clif *clif, UNUSED int argc, UNUSED char *argv[],
UNUSED struct cmd *command, UNUSED int raw)
{
printf("%s\n", cli_full_license);
return 0;
}
static int
cli_cmd_quit(UNUSED struct clif *clif, UNUSED int argc, UNUSED char *argv[],
UNUSED struct cmd *command, UNUSED int raw)
{
cli_quit = 1;
return 0;
}
struct cli_cmd {
lldp_cmd cmdcode;
const char *cmdstr;
int (*handler)(struct clif *clif, int argc, char *argv[],
struct cmd *cmd, int raw);
};
static struct cli_cmd cli_commands[] = {
{ cmd_ping, "ping", cli_cmd_ping },
{ cmd_help, "help", cli_cmd_help },
{ cmd_license, "license", cli_cmd_license },
{ cmd_version, "version", cli_cmd_version },
{ cmd_quit, "quit", cli_cmd_quit },
{ cmd_getstats, "stats", cli_cmd_getstats },
{ cmd_gettlv, "gettlv", cli_cmd_gettlv },
{ cmd_gettlv, "get-tlv", cli_cmd_gettlv },
{ cmd_settlv, "settlv", cli_cmd_settlv },
{ cmd_settlv, "set-tlv", cli_cmd_settlv },
{ cmd_get_lldp, "getlldp", cli_cmd_getlldp },
{ cmd_get_lldp, "get-lldp", cli_cmd_getlldp },
{ cmd_set_lldp, "setlldp", cli_cmd_setlldp },
{ cmd_set_lldp, "set-lldp", cli_cmd_setlldp },
{ cmd_nop, NULL, cli_cmd_nop }
};
u32 lookup_tlvid(char *tlvid_str)
{
struct lldp_module *np;
u32 tlvid = INVALID_TLVID;
LIST_FOREACH(np, &lldp_head, lldp) {
if (np->ops->lookup_tlv_name) {
tlvid = np->ops->lookup_tlv_name(tlvid_str);
if (tlvid != INVALID_TLVID)
break;
}
}
return tlvid;
}
void print_args(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++)
printf("\tremaining arg %d = %s\n", i, argv[i]);
}
static struct option lldptool_opts[] = {
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'},
{"stats", 0, NULL, 'S'},
{"get-tlv", 0, NULL, 't'},
{"set-tlv", 0, NULL, 'T'},
{"get-lldp", 0, NULL, 'l'},
{"set-lldp", 0, NULL, 'L'},
{0, 0, 0, 0}
};
static int request(struct clif *clif, int argc, char *argv[])
{
struct cli_cmd *cmd, *match = NULL;
struct cmd command;
int count;
int ret = 0;
int newraw = 0;
int numargs = 0;
char **argptr = &argv[0];
char *end;
int c;
int option_index;
memset((void *)&command, 0, sizeof(command));
command.cmd = cmd_nop;
command.type = NEAREST_BRIDGE;
command.module_id = LLDP_MOD_MAND;
command.tlvid = INVALID_TLVID;
opterr = 0;
for (;;) {
c = getopt_long(argc, argv, "Si:tTlLhcdnvrRpqV:g:",
lldptool_opts, &option_index);
if (c < 0)
break;
switch (c) {
case '?':
printf("missing argument for option %s\n\n", argv[optind-1]);
usage();
return -1;
case 'S':
command.cmd = cmd_getstats;
break;
case 'i':
strncpy(command.ifname, optarg, IFNAMSIZ);
command.ifname[IFNAMSIZ] ='\0';
break;
case 'g':
if (!strcasecmp(optarg, "nearestbridge") ||
!strcasecmp(optarg, "nearest_bridge") ||
!strcasecmp(optarg, "nb"))
command.type = NEAREST_BRIDGE;
else if (!strcasecmp(optarg, "nearestcustomerbridge") ||
!strcasecmp(optarg, "nearest_customer_bridge") ||
!strcasecmp(optarg, "ncb"))
command.type = NEAREST_CUSTOMER_BRIDGE;
else if (!strcasecmp(optarg, "nearestnontpmrbridge") ||
!strcasecmp(optarg, "nearest_nontpmr_bridge") ||
!strcasecmp(optarg, "nntpmrb"))
command.type = NEAREST_NONTPMR_BRIDGE;
else {
printf("Invalid agent specified !\n\n");
return -1;
}
break;
case 'V':
if (command.tlvid != INVALID_TLVID) {
printf("\nInvalid command: multiple TLV "
"identifiers: %s\n", optarg);
return -1;
}
/* Currently tlvid unset lookup and verify parameter */
errno = 0;
command.tlvid = strtoul(optarg, &end, 0);
if (!command.tlvid || errno || *end != '\0' ||
end == optarg) {
command.tlvid = lookup_tlvid(optarg);
if (!strcasecmp("vdp", optarg))
command.module_id = command.tlvid;
}
if (command.tlvid == INVALID_TLVID) {
printf("\nInvalid TLV identifier: %s\n",
optarg);
return -1;
}
break;
case 'p':
command.cmd = cmd_ping;
break;
case 'q':
command.cmd = cmd_quit;
break;
case 't':
command.cmd = cmd_gettlv;
break;
case 'T':
command.cmd = cmd_settlv;
break;
case 'l':
command.cmd = cmd_get_lldp;
break;
case 'L':
command.cmd = cmd_set_lldp;
break;
case 'c':
command.ops |= op_config;
break;
case 'd':
command.ops |= op_delete;
break;
case 'n':
command.ops |= op_neighbor;
break;
case 'h':
command.cmd = cmd_help;
break;
case 'r':
if (newraw) {
usage();
return -1;
}
newraw = SHOW_RAW;
break;
case 'R':
if (newraw) {
usage();
return -1;
}
newraw = (SHOW_RAW | SHOW_RAW_ONLY);
break;
case 'v':
command.cmd = cmd_version;
break;
default:
usage();
ret = -1;
}
}
/*
* If -V vdp option is set together with -c option, use standard
* module to retrieve data.
*/
if ((command.ops & op_config) && command.tlvid == command.module_id)
command.module_id = LLDP_MOD_MAND;
/* if no command was supplied via an option flag, then
* the first remaining argument should be the command.
*/
count = 0;
if (command.cmd == cmd_nop && optind < argc) {
cmd = cli_commands;
while (cmd->cmdcode != cmd_nop) {
if (strncasecmp(cmd->cmdstr, argv[optind],
strlen(argv[optind])) == 0) {
match = cmd;
command.cmd = match->cmdcode;
count++;
}
cmd++;
}
}
if (count > 1) {
printf("Ambiguous command '%s'; possible commands:",
argv[optind]);
cmd = cli_commands;
while (cmd->cmdstr) {
if (strncasecmp(cmd->cmdstr, argv[optind],
strlen(argv[optind])) == 0)
printf(" %s", cmd->cmdstr);
cmd++;
}
printf("\n");
ret = -1;
} else {
if (!match) {
cmd = cli_commands;
while (cmd->cmdcode != command.cmd)
cmd++;
match = cmd;
}
if (count == 0)
numargs = argc-optind;
else
numargs = argc-optind-1;
if (numargs)
argptr = &argv[argc-numargs];
ret = match->handler(clif, numargs, argptr, &command, newraw);
}
return ret;
}
static void cli_recv_pending(struct clif *clif, int in_read)
{
int first = 1;
if (clif == NULL)
return;
while (clif_pending(clif)) {
char buf[256];
size_t len = sizeof(buf) - 1;
if (clif_recv(clif, buf, &len) == 0) {
buf[len] = '\0';
if (in_read && first)
printf("\n");
first = 0;
cli_msg_cb(buf, len);
} else {
printf("Could not read pending message.\n");
break;
}
}
}
static void cli_interactive()
{
const int max_args = 20;
char *cmd, *argv[max_args], *pos;
int argc;
setlinebuf(stdout);
printf("\nInteractive mode\n\n");
using_history();
stifle_history(1000);
do {
cli_recv_pending(clif_conn, 0);
alarm(1);
cmd = readline("> ");
alarm(0);
if (!cmd)
break;
if (*cmd)
add_history(cmd);
argc = 1;
pos = cmd;
for (;;) {
while (*pos == ' ')
pos++;
if (*pos == '\0')
break;
argv[argc] = pos;
argc++;
if (argc == max_args)
break;
while (*pos != '\0' && *pos != ' ')
pos++;
if (*pos == ' ')
*pos++ = '\0';
}
if (argc) {
optind = 0;
request(clif_conn, argc, argv);
}
free(cmd);
} while (!cli_quit);
}
static void cli_terminate(UNUSED int sig)
{
cli_close_connection();
exit(0);
}
static void cli_alarm(UNUSED int sig)
{
if (clif_conn && _clif_command(clif_conn, "P", SHOW_NO_OUTPUT)) {
printf("Connection to lldpad lost - trying to reconnect\n");
cli_close_connection();
}
if (!clif_conn) {
clif_conn = clif_open();
if (clif_conn) {
char attach_str[9] = "";
u32 mod_id = LLDP_MOD_MAND;
bin2hexstr((u8*)&mod_id, 4, attach_str, 8);
printf("Connection to lldpad re-established\n");
if (clif_attach(clif_conn, attach_str) == 0)
cli_attached = 1;
else
printf("Warning: Failed to attach to lldpad.\n");
}
}
if (clif_conn)
cli_recv_pending(clif_conn, 1);
alarm(1);
}
int main(int argc, char *argv[])
{
int interactive = 1;
int warning_displayed = 0;
int ret = 0;
if (argc > 1)
interactive = 0;
if (interactive)
printf("%s\n\n%s\n\n", cli_version, cli_license);
for (;;) {
clif_conn = clif_open();
if (clif_conn) {
if (warning_displayed)
printf("Connection established.\n");
break;
}
if (!interactive) {
perror("Failed to connect to lldpad - clif_open");
return -1;
}
if (!warning_displayed) {
printf("Could not connect to lldpad - re-trying\n");
warning_displayed = 1;
}
sleep(1);
continue;
}
init_modules();
signal(SIGINT, cli_terminate);
signal(SIGTERM, cli_terminate);
signal(SIGALRM, cli_alarm);
if (interactive) {
char attach_str[9] = "";
u32 mod_id = LLDP_MOD_MAND;
bin2hexstr((u8*)&mod_id, 4, attach_str, 8);
if (clif_attach(clif_conn, attach_str) == 0)
cli_attached = 1;
else
printf("Warning: Failed to attach to lldpad.\n");
cli_interactive();
} else {
ret = request(clif_conn, argc, &argv[0]);
ret = !!ret;
}
cli_close_connection();
deinit_modules();
return ret;
}