]> git.ipfire.org Git - thirdparty/lldpd.git/blob - src/client/lldpctl.c
ae7a4aa7bf79b4e3c805495c3eeed4a351119292
[thirdparty/lldpd.git] / src / client / lldpctl.c
1 /* -*- mode: c; c-file-style: "openbsd" -*- */
2 /*
3 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <time.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <arpa/inet.h>
27
28 #include "../log.h"
29 #include "../ctl.h"
30 #include "client.h"
31
32 static void usage(void);
33
34 #ifdef HAVE___PROGNAME
35 extern const char *__progname;
36 #else
37 # define __progname "lldpctl"
38 #endif
39
40
41 static void
42 usage(void)
43 {
44 fprintf(stderr, "Usage: %s [OPTIONS ...] [INTERFACES ...]\n", __progname);
45 fprintf(stderr, "Version: %s\n", PACKAGE_STRING);
46
47 fprintf(stderr, "\n");
48
49 fprintf(stderr, "-d Enable more debugging information.\n");
50 fprintf(stderr, "-a Display all remote ports, including hidden ones.\n");
51 fprintf(stderr, "-f format Choose output format (plain, keyvalue or xml).\n");
52 fprintf(stderr, "-L location Enable the transmission of LLDP-MED location TLV for the\n");
53 fprintf(stderr, " given interfaces. Can be repeated to enable the transmission\n");
54 fprintf(stderr, " of the location in several formats.\n");
55 fprintf(stderr, "-P policy Enable the transmission of LLDP-MED Network Policy TLVs\n");
56 fprintf(stderr, " for the given interfaces. Can be repeated to specify\n");
57 fprintf(stderr, " different policies.\n");
58 fprintf(stderr, "-O poe Enable the transmission of LLDP-MED POE-MDI TLV\n");
59 fprintf(stderr, " for the given interfaces.\n");
60 fprintf(stderr, "-o poe Enable the transmission of Dot3 POE-MDI TLV\n");
61 fprintf(stderr, " for the given interfaces.\n");
62
63 fprintf(stderr, "\n");
64
65 fprintf(stderr, "see manual page lldpctl(8) for more information\n");
66 exit(1);
67 }
68
69 int
70 main(int argc, char *argv[])
71 {
72 int ch, debug = 1;
73 char * fmt = "plain";
74 int action = 0, hidden = 0;
75 lldpctl_conn_t *conn;
76
77 /* Get and parse command line options */
78 while ((ch = getopt(argc, argv, LLDPCTL_ARGS)) != -1) {
79 switch (ch) {
80 case 'h':
81 usage();
82 break;
83 case 'd':
84 debug++;
85 break;
86 case 'v':
87 fprintf(stdout, "%s\n", PACKAGE_VERSION);
88 exit(0);
89 break;
90 case 'a':
91 hidden = 1;
92 break;
93 case 'f':
94 fmt = optarg;
95 break;
96 case 'L':
97 case 'P':
98 case 'O':
99 case 'o':
100 action = 1;
101 break;
102 default:
103 usage();
104 }
105 }
106
107 log_init(debug, __progname);
108
109 if ((action != 0) && (getuid() != 0)) {
110 fatalx("mere mortals may not do that, 'root' privileges are required.");
111 }
112
113 conn = lldpctl_new(NULL, NULL, NULL);
114 if (conn == NULL) exit(EXIT_FAILURE);
115
116 if (!action) display_interfaces(conn, fmt, hidden, argc, argv);
117 else modify_interfaces(conn, argc, argv, optind);
118
119 lldpctl_release(conn);
120 return EXIT_SUCCESS;
121 }