• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
30 int show_stats = 0;
31 int show_details = 0;
32 int show_raw = 0;
33 
34 static void *BODY;
35 static struct genl_util * genl_list;
36 
37 
print_nofopt(const struct sockaddr_nl * who,struct nlmsghdr * n,void * arg)38 static int print_nofopt(const struct sockaddr_nl *who, struct nlmsghdr *n,
39 			void *arg)
40 {
41 	fprintf((FILE *) arg, "unknown genl type ..\n");
42 	return 0;
43 }
44 
parse_nofopt(struct genl_util * f,int argc,char ** argv)45 static int parse_nofopt(struct genl_util *f, int argc, char **argv)
46 {
47 	if (argc) {
48 		fprintf(stderr, "Unknown genl \"%s\", hence option \"%s\" "
49 			"is unparsable\n", f->name, *argv);
50 		return -1;
51 	}
52 
53 	return 0;
54 }
55 
get_genl_kind(const char * str)56 static struct genl_util *get_genl_kind(const char *str)
57 {
58 	void *dlh;
59 	char buf[256];
60 	struct genl_util *f;
61 
62 	for (f = genl_list; f; f = f->next)
63 		if (strcmp(f->name, str) == 0)
64 			return f;
65 
66 	snprintf(buf, sizeof(buf), "%s.so", str);
67 	dlh = dlopen(buf, RTLD_LAZY);
68 	if (dlh == NULL) {
69 		dlh = BODY;
70 		if (dlh == NULL) {
71 			dlh = BODY = dlopen(NULL, RTLD_LAZY);
72 			if (dlh == NULL)
73 				goto noexist;
74 		}
75 	}
76 
77 	snprintf(buf, sizeof(buf), "%s_genl_util", str);
78 
79 	f = dlsym(dlh, buf);
80 	if (f == NULL)
81 		goto noexist;
82 reg:
83 	f->next = genl_list;
84 	genl_list = f;
85 	return f;
86 
87 noexist:
88 	f = calloc(1, sizeof(*f));
89 	if (f) {
90 		strncpy(f->name, str, 15);
91 		f->parse_genlopt = parse_nofopt;
92 		f->print_genlopt = print_nofopt;
93 		goto reg;
94 	}
95 	return f;
96 }
97 
98 static void usage(void) __attribute__((noreturn));
99 
usage(void)100 static void usage(void)
101 {
102 	fprintf(stderr, "Usage: genl [ OPTIONS ] OBJECT | help }\n"
103 	                "where  OBJECT := { ctrl etc }\n"
104 	                "       OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] }\n");
105 	exit(-1);
106 }
107 
main(int argc,char ** argv)108 int main(int argc, char **argv)
109 {
110 	while (argc > 1) {
111 		if (argv[1][0] != '-')
112 			break;
113 		if (matches(argv[1], "-stats") == 0 ||
114 		    matches(argv[1], "-statistics") == 0) {
115 			++show_stats;
116 		} else if (matches(argv[1], "-details") == 0) {
117 			++show_details;
118 		} else if (matches(argv[1], "-raw") == 0) {
119 			++show_raw;
120 		} else if (matches(argv[1], "-Version") == 0) {
121 			printf("genl utility, iproute2-ss%s\n", SNAPSHOT);
122 			exit(0);
123 		} else if (matches(argv[1], "-help") == 0) {
124 			usage();
125 		} else {
126 			fprintf(stderr, "Option \"%s\" is unknown, try "
127 				"\"genl -help\".\n", argv[1]);
128 			exit(-1);
129 		}
130 		argc--;	argv++;
131 	}
132 
133 	if (argc > 1) {
134 		int ret;
135 		struct genl_util *a = NULL;
136 		a = get_genl_kind(argv[1]);
137 		if (!a) {
138 			fprintf(stderr,"bad genl %s\n", argv[1]);
139 			exit(-1);
140 		}
141 
142 		ret = a->parse_genlopt(a, argc-1, argv+1);
143 		return ret;
144 	}
145 
146 	usage();
147 }
148