Debian Codegraph @

tipc/media.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * media.c TIPC link functionality.
4 *
5 * Authors: Richard Alpe <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12
13 #include <linux/tipc_netlink.h>
14 #include <linux/genetlink.h>
15
16 #include "cmdl.h"
17 #include "msg.h"
18 #include "media.h"
19
20 static int media_list_cb(const struct nlmsghdr *nlh, void *data)
21 {
22 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
23 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
24 struct nlattr *attrs[TIPC_NLA_MEDIA_MAX + 1] = {};
25
26 mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
27 if (!info[TIPC_NLA_MEDIA])
28 return MNL_CB_ERROR;
29
30 mnl_attr_parse_nested(info[TIPC_NLA_MEDIA], parse_attrs, attrs);
31 if (!attrs[TIPC_NLA_MEDIA_NAME])
32 return MNL_CB_ERROR;
33
34 printf("%s\n", mnl_attr_get_str(attrs[TIPC_NLA_MEDIA_NAME]));
35
36 return MNL_CB_OK;
37 }
38
39 static int cmd_media_list(struct nlmsghdr *nlh, const struct cmd *cmd,
40 struct cmdl *cmdl, void *data)
41 {
42 if (help_flag) {
43 fprintf(stderr, "Usage: %s media list\n", cmdl->argv[0]);
44 return -EINVAL;
45 }
46
47 nlh = msg_init(TIPC_NL_MEDIA_GET);
48 if (!nlh) {
49 fprintf(stderr, "error, message initialisation failed\n");
50 return -1;
51 }
52
53 return msg_dumpit(nlh, media_list_cb, NULL);
54 }
55
56 static int media_get_cb(const struct nlmsghdr *nlh, void *data)
57 {
58 int *prop = data;
59 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
60 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
61 struct nlattr *attrs[TIPC_NLA_MEDIA_MAX + 1] = {};
62 struct nlattr *props[TIPC_NLA_PROP_MAX + 1] = {};
63
64 mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
65 if (!info[TIPC_NLA_MEDIA])
66 return MNL_CB_ERROR;
67
68 mnl_attr_parse_nested(info[TIPC_NLA_MEDIA], parse_attrs, attrs);
69 if (!attrs[TIPC_NLA_MEDIA_PROP])
70 return MNL_CB_ERROR;
71
72 mnl_attr_parse_nested(attrs[TIPC_NLA_MEDIA_PROP], parse_attrs, props);
73 if (!props[*prop])
74 return MNL_CB_ERROR;
75
76 printf("%u\n", mnl_attr_get_u32(props[*prop]));
77
78 return MNL_CB_OK;
79 }
80
81 static int cmd_media_get_prop(struct nlmsghdr *nlh, const struct cmd *cmd,
82 struct cmdl *cmdl, void *data)
83 {
84 int prop;
85 struct nlattr *nest;
86 struct opt *opt;
87 struct opt opts[] = {
88 { "media", OPT_KEYVAL, NULL },
89 { NULL }
90 };
91
92 if (strcmp(cmd->cmd, "priority") == 0)
93 prop = TIPC_NLA_PROP_PRIO;
94 else if ((strcmp(cmd->cmd, "tolerance") == 0))
95 prop = TIPC_NLA_PROP_TOL;
96 else if ((strcmp(cmd->cmd, "window") == 0))
97 prop = TIPC_NLA_PROP_WIN;
98 else if ((strcmp(cmd->cmd, "mtu") == 0))
99 prop = TIPC_NLA_PROP_MTU;
100 else
101 return -EINVAL;
102
103 if (help_flag) {
104 (cmd->help)(cmdl);
105 return -EINVAL;
106 }
107
108 if (parse_opts(opts, cmdl) < 0)
109 return -EINVAL;
110
111 nlh = msg_init(TIPC_NL_MEDIA_GET);
112 if (!nlh) {
113 fprintf(stderr, "error, message initialisation failed\n");
114 return -1;
115 }
116
117 if (!(opt = get_opt(opts, "media"))) {
118 fprintf(stderr, "error, missing media\n");
119 return -EINVAL;
120 }
121
122 if ((prop == TIPC_NLA_PROP_MTU) &&
123 (strcmp(opt->val, "udp"))) {
124 fprintf(stderr, "error, not supported for media\n");
125 return -EINVAL;
126 }
127 nest = mnl_attr_nest_start(nlh, TIPC_NLA_MEDIA);
128 mnl_attr_put_strz(nlh, TIPC_NLA_MEDIA_NAME, opt->val);
129 mnl_attr_nest_end(nlh, nest);
130
131 return msg_doit(nlh, media_get_cb, &prop);
132 }
133
134 static void cmd_media_get_help(struct cmdl *cmdl)
135 {
136 fprintf(stderr, "Usage: %s media get PPROPERTY media MEDIA\n\n"
137 "PROPERTIES\n"
138 " tolerance - Get media tolerance\n"
139 " priority - Get media priority\n"
140 " window - Get media window\n"
141 " mtu - Get media mtu\n",
142 cmdl->argv[0]);
143 }
144
145 static int cmd_media_get(struct nlmsghdr *nlh, const struct cmd *cmd,
146 struct cmdl *cmdl, void *data)
147 {
148 const struct cmd cmds[] = {
149 { "priority", cmd_media_get_prop, cmd_media_get_help },
150 { "tolerance", cmd_media_get_prop, cmd_media_get_help },
151 { "window", cmd_media_get_prop, cmd_media_get_help },
152 { "mtu", cmd_media_get_prop, cmd_media_get_help },
153 { NULL }
154 };
155
156 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
157 }
158
159 static void cmd_media_set_help(struct cmdl *cmdl)
160 {
161 fprintf(stderr, "Usage: %s media set PPROPERTY media MEDIA\n\n"
162 "PROPERTIES\n"
163 " tolerance TOLERANCE - Set media tolerance\n"
164 " priority PRIORITY - Set media priority\n"
165 " window WINDOW - Set media window\n"
166 " mtu MTU - Set media mtu\n",
167 cmdl->argv[0]);
168 }
169
170 static int cmd_media_set_prop(struct nlmsghdr *nlh, const struct cmd *cmd,
171 struct cmdl *cmdl, void *data)
172 {
173 int val;
174 int prop;
175 struct nlattr *props;
176 struct nlattr *attrs;
177 struct opt *opt;
178 struct opt opts[] = {
179 { "media", OPT_KEYVAL, NULL },
180 { NULL }
181 };
182
183 if (strcmp(cmd->cmd, "priority") == 0)
184 prop = TIPC_NLA_PROP_PRIO;
185 else if ((strcmp(cmd->cmd, "tolerance") == 0))
186 prop = TIPC_NLA_PROP_TOL;
187 else if ((strcmp(cmd->cmd, "window") == 0))
188 prop = TIPC_NLA_PROP_WIN;
189 else if ((strcmp(cmd->cmd, "mtu") == 0))
190 prop = TIPC_NLA_PROP_MTU;
191 else
192 return -EINVAL;
193
194 if (help_flag) {
195 (cmd->help)(cmdl);
196 return -EINVAL;
197 }
198
199 if (cmdl->optind >= cmdl->argc) {
200 fprintf(stderr, "error, missing value\n");
201 return -EINVAL;
202 }
203 val = atoi(shift_cmdl(cmdl));
204
205 if (parse_opts(opts, cmdl) < 0)
206 return -EINVAL;
207
208 nlh = msg_init(TIPC_NL_MEDIA_SET);
209 if (!nlh) {
210 fprintf(stderr, "error, message initialisation failed\n");
211 return -1;
212 }
213 attrs = mnl_attr_nest_start(nlh, TIPC_NLA_MEDIA);
214
215 if (!(opt = get_opt(opts, "media"))) {
216 fprintf(stderr, "error, missing media\n");
217 return -EINVAL;
218 }
219
220 if ((prop == TIPC_NLA_PROP_MTU) &&
221 (strcmp(opt->val, "udp"))) {
222 fprintf(stderr, "error, not supported for media\n");
223 return -EINVAL;
224 }
225 mnl_attr_put_strz(nlh, TIPC_NLA_MEDIA_NAME, opt->val);
226
227 props = mnl_attr_nest_start(nlh, TIPC_NLA_MEDIA_PROP);
228 mnl_attr_put_u32(nlh, prop, val);
229 mnl_attr_nest_end(nlh, props);
230
231 mnl_attr_nest_end(nlh, attrs);
232
233 return msg_doit(nlh, NULL, NULL);
234 }
235
236 static int cmd_media_set(struct nlmsghdr *nlh, const struct cmd *cmd,
237 struct cmdl *cmdl, void *data)
238 {
239 const struct cmd cmds[] = {
240 { "priority", cmd_media_set_prop, cmd_media_set_help },
241 { "tolerance", cmd_media_set_prop, cmd_media_set_help },
242 { "window", cmd_media_set_prop, cmd_media_set_help },
243 { "mtu", cmd_media_set_prop, cmd_media_set_help },
244 { NULL }
245 };
246
247 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
248 }
249
250 void cmd_media_help(struct cmdl *cmdl)
251 {
252 fprintf(stderr,
253 "Usage: %s media COMMAND [ARGS] ...\n"
254 "\n"
255 "Commands:\n"
256 " list - List active media types\n"
257 " get - Get various media properties\n"
258 " set - Set various media properties\n",
259 cmdl->argv[0]);
260 }
261
262 int cmd_media(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
263 void *data)
264 {
265 const struct cmd cmds[] = {
266 { "get", cmd_media_get, cmd_media_get_help },
267 { "list", cmd_media_list, NULL },
268 { "set", cmd_media_set, cmd_media_set_help },
269 { NULL }
270 };
271
272 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
273 }