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