blob: 747074b029a7b98626ba32b70e15dc2e3b7aded9 [file] [log] [blame]
Jamal Hadi Salim65018ae2006-08-08 12:13:34 -07001/*
2 * genl.c "genl" utility frontend.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Jamal Hadi Salim
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
17#include <fcntl.h>
18#include <dlfcn.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <string.h>
23#include <errno.h>
24#include <linux/netlink.h>
25#include <linux/rtnetlink.h> /* until we put our own header */
26#include "SNAPSHOT.h"
27#include "utils.h"
28#include "genl_utils.h"
29
30int show_stats = 0;
31int show_details = 0;
32int show_raw = 0;
33int resolve_hosts = 0;
34
35static void *BODY;
36static struct genl_util * genl_list;
37
38
39static int print_nofopt(const struct sockaddr_nl *who, struct nlmsghdr *n,
40 void *arg)
41{
42 fprintf((FILE *) arg, "unknown genl type ..\n");
43 return 0;
44}
45
46static int parse_nofopt(struct genl_util *f, int argc, char **argv)
47{
48 if (argc) {
49 fprintf(stderr, "Unknown genl \"%s\", hence option \"%s\" "
50 "is unparsable\n", f->name, *argv);
51 return -1;
52 }
53
54 return 0;
55}
56
Stephen Hemmingerb27f0052015-12-30 17:17:45 -080057static struct genl_util *get_genl_kind(const char *str)
Jamal Hadi Salim65018ae2006-08-08 12:13:34 -070058{
59 void *dlh;
60 char buf[256];
61 struct genl_util *f;
62
63 for (f = genl_list; f; f = f->next)
64 if (strcmp(f->name, str) == 0)
65 return f;
66
67 snprintf(buf, sizeof(buf), "%s.so", str);
68 dlh = dlopen(buf, RTLD_LAZY);
69 if (dlh == NULL) {
70 dlh = BODY;
71 if (dlh == NULL) {
72 dlh = BODY = dlopen(NULL, RTLD_LAZY);
73 if (dlh == NULL)
74 goto noexist;
75 }
76 }
77
78 snprintf(buf, sizeof(buf), "%s_genl_util", str);
79
80 f = dlsym(dlh, buf);
81 if (f == NULL)
82 goto noexist;
83reg:
84 f->next = genl_list;
85 genl_list = f;
86 return f;
87
88noexist:
Phil Sutterf89bb022016-07-18 16:48:43 +020089 f = calloc(1, sizeof(*f));
Jamal Hadi Salim65018ae2006-08-08 12:13:34 -070090 if (f) {
Jamal Hadi Salim65018ae2006-08-08 12:13:34 -070091 strncpy(f->name, str, 15);
92 f->parse_genlopt = parse_nofopt;
93 f->print_genlopt = print_nofopt;
94 goto reg;
95 }
96 return f;
97}
98
99static void usage(void) __attribute__((noreturn));
100
101static void usage(void)
102{
103 fprintf(stderr, "Usage: genl [ OPTIONS ] OBJECT | help }\n"
104 "where OBJECT := { ctrl etc }\n"
105 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] }\n");
106 exit(-1);
107}
108
109int main(int argc, char **argv)
110{
Jamal Hadi Salim65018ae2006-08-08 12:13:34 -0700111 while (argc > 1) {
112 if (argv[1][0] != '-')
113 break;
114 if (matches(argv[1], "-stats") == 0 ||
115 matches(argv[1], "-statistics") == 0) {
116 ++show_stats;
117 } else if (matches(argv[1], "-details") == 0) {
118 ++show_details;
119 } else if (matches(argv[1], "-raw") == 0) {
120 ++show_raw;
121 } else if (matches(argv[1], "-Version") == 0) {
122 printf("genl utility, iproute2-ss%s\n", SNAPSHOT);
123 exit(0);
124 } else if (matches(argv[1], "-help") == 0) {
125 usage();
126 } else {
127 fprintf(stderr, "Option \"%s\" is unknown, try "
128 "\"genl -help\".\n", argv[1]);
129 exit(-1);
130 }
131 argc--; argv++;
132 }
133
134 if (argc > 1) {
135 int ret;
136 struct genl_util *a = NULL;
137 a = get_genl_kind(argv[1]);
Dan McGee1b129bf2011-08-31 12:15:22 -0700138 if (!a) {
139 fprintf(stderr,"bad genl %s\n", argv[1]);
140 exit(-1);
Jamal Hadi Salim65018ae2006-08-08 12:13:34 -0700141 }
142
143 ret = a->parse_genlopt(a, argc-1, argv+1);
144 return ret;
Jamal Hadi Salim65018ae2006-08-08 12:13:34 -0700145 }
146
147 usage();
148}