Jamal Hadi Salim | 65018ae | 2006-08-08 12:13:34 -0700 | [diff] [blame] | 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 | int resolve_hosts = 0; |
| 34 | |
| 35 | static void *BODY; |
| 36 | static struct genl_util * genl_list; |
| 37 | |
| 38 | |
| 39 | static 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 | |
| 46 | static 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 Hemminger | b27f005 | 2015-12-30 17:17:45 -0800 | [diff] [blame] | 57 | static struct genl_util *get_genl_kind(const char *str) |
Jamal Hadi Salim | 65018ae | 2006-08-08 12:13:34 -0700 | [diff] [blame] | 58 | { |
| 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; |
| 83 | reg: |
| 84 | f->next = genl_list; |
| 85 | genl_list = f; |
| 86 | return f; |
| 87 | |
| 88 | noexist: |
Phil Sutter | f89bb02 | 2016-07-18 16:48:43 +0200 | [diff] [blame] | 89 | f = calloc(1, sizeof(*f)); |
Jamal Hadi Salim | 65018ae | 2006-08-08 12:13:34 -0700 | [diff] [blame] | 90 | if (f) { |
Jamal Hadi Salim | 65018ae | 2006-08-08 12:13:34 -0700 | [diff] [blame] | 91 | 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 | |
| 99 | static void usage(void) __attribute__((noreturn)); |
| 100 | |
| 101 | static 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 | |
| 109 | int main(int argc, char **argv) |
| 110 | { |
Jamal Hadi Salim | 65018ae | 2006-08-08 12:13:34 -0700 | [diff] [blame] | 111 | 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 McGee | 1b129bf | 2011-08-31 12:15:22 -0700 | [diff] [blame] | 138 | if (!a) { |
| 139 | fprintf(stderr,"bad genl %s\n", argv[1]); |
| 140 | exit(-1); |
Jamal Hadi Salim | 65018ae | 2006-08-08 12:13:34 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | ret = a->parse_genlopt(a, argc-1, argv+1); |
| 144 | return ret; |
Jamal Hadi Salim | 65018ae | 2006-08-08 12:13:34 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | usage(); |
| 148 | } |