]>
Commit | Line | Data |
---|---|---|
5bec3484 JHS |
1 | /* |
2 | * tc_monitor.c "tc monitor". | |
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> | |
5bec3484 JHS |
16 | #include <fcntl.h> |
17 | #include <sys/socket.h> | |
18 | #include <netinet/in.h> | |
19 | #include <arpa/inet.h> | |
20 | #include <string.h> | |
21 | #include <time.h> | |
22 | #include "rt_names.h" | |
23 | #include "utils.h" | |
24 | #include "tc_util.h" | |
25 | #include "tc_common.h" | |
26 | ||
27 | ||
28 | static void usage(void) __attribute__((noreturn)); | |
29 | ||
30 | static void usage(void) | |
31 | { | |
32a6fbe5 | 32 | fprintf(stderr, "Usage: tc [-timestamp [-tshort] monitor\n"); |
5bec3484 JHS |
33 | exit(-1); |
34 | } | |
35 | ||
36 | ||
cd554f2c | 37 | static int accept_tcmsg(struct rtnl_ctrl_data *ctrl, |
d1f28cf1 | 38 | struct nlmsghdr *n, void *arg) |
5bec3484 | 39 | { |
32a121cb | 40 | FILE *fp = (FILE *)arg; |
5bec3484 | 41 | |
32a6fbe5 ED |
42 | if (timestamp) |
43 | print_timestamp(fp); | |
44 | ||
afcd0699 JP |
45 | if (n->nlmsg_type == RTM_NEWTFILTER || |
46 | n->nlmsg_type == RTM_DELTFILTER || | |
47 | n->nlmsg_type == RTM_NEWCHAIN || | |
48 | n->nlmsg_type == RTM_DELCHAIN) { | |
cd554f2c | 49 | print_filter(n, arg); |
5bec3484 JHS |
50 | return 0; |
51 | } | |
52 | if (n->nlmsg_type == RTM_NEWTCLASS || n->nlmsg_type == RTM_DELTCLASS) { | |
cd554f2c | 53 | print_class(n, arg); |
5bec3484 JHS |
54 | return 0; |
55 | } | |
56 | if (n->nlmsg_type == RTM_NEWQDISC || n->nlmsg_type == RTM_DELQDISC) { | |
cd554f2c | 57 | print_qdisc(n, arg); |
5bec3484 JHS |
58 | return 0; |
59 | } | |
60 | if (n->nlmsg_type == RTM_GETACTION || n->nlmsg_type == RTM_NEWACTION || | |
61 | n->nlmsg_type == RTM_DELACTION) { | |
cd554f2c | 62 | print_action(n, arg); |
5bec3484 JHS |
63 | return 0; |
64 | } | |
65 | if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP && | |
66 | n->nlmsg_type != NLMSG_DONE) { | |
67 | fprintf(fp, "Unknown message: length %08d type %08x flags %08x\n", | |
68 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
69 | } | |
70 | return 0; | |
71 | } | |
72 | ||
73 | int do_tcmonitor(int argc, char **argv) | |
74 | { | |
75 | struct rtnl_handle rth; | |
76 | char *file = NULL; | |
32a121cb | 77 | unsigned int groups = nl_mgrp(RTNLGRP_TC); |
5bec3484 JHS |
78 | |
79 | while (argc > 0) { | |
80 | if (matches(*argv, "file") == 0) { | |
81 | NEXT_ARG(); | |
82 | file = *argv; | |
83 | } else { | |
84 | if (matches(*argv, "help") == 0) { | |
85 | usage(); | |
86 | } else { | |
87 | fprintf(stderr, "Argument \"%s\" is unknown, try \"tc monitor help\".\n", *argv); | |
88 | exit(-1); | |
89 | } | |
90 | } | |
91 | argc--; argv++; | |
92 | } | |
93 | ||
94 | if (file) { | |
e49b51d6 SH |
95 | FILE *fp = fopen(file, "r"); |
96 | int ret; | |
97 | ||
5bec3484 JHS |
98 | if (fp == NULL) { |
99 | perror("Cannot fopen"); | |
100 | exit(-1); | |
101 | } | |
e49b51d6 SH |
102 | |
103 | ret = rtnl_from_file(fp, accept_tcmsg, stdout); | |
104 | fclose(fp); | |
105 | return ret; | |
5bec3484 JHS |
106 | } |
107 | ||
108 | if (rtnl_open(&rth, groups) < 0) | |
109 | exit(1); | |
110 | ||
111 | ll_init_map(&rth); | |
112 | ||
32a121cb | 113 | if (rtnl_listen(&rth, accept_tcmsg, (void *)stdout) < 0) { |
5bec3484 JHS |
114 | rtnl_close(&rth); |
115 | exit(2); | |
116 | } | |
117 | ||
118 | rtnl_close(&rth); | |
119 | exit(0); | |
120 | } |