]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
some work for making command more dynamic
[thirdparty/iw.git] / iw.c
1 /*
2 * nl80211 userspace tool
3 *
4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
5 */
6
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <net/if.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15
16 #include <netlink/genl/genl.h>
17 #include <netlink/genl/family.h>
18 #include <netlink/genl/ctrl.h>
19 #include <netlink/msg.h>
20 #include <netlink/attr.h>
21 #include <linux/nl80211.h>
22
23 #include "iw.h"
24
25
26 static int nl80211_init(struct nl80211_state *state)
27 {
28 int err;
29
30 state->nl_handle = nl_handle_alloc();
31 if (!state->nl_handle) {
32 fprintf(stderr, "Failed to allocate netlink handle.\n");
33 return -ENOMEM;
34 }
35
36 if (genl_connect(state->nl_handle)) {
37 fprintf(stderr, "Failed to connect to generic netlink.\n");
38 err = -ENOLINK;
39 goto out_handle_destroy;
40 }
41
42 state->nl_cache = genl_ctrl_alloc_cache(state->nl_handle);
43 if (!state->nl_cache) {
44 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
45 err = -ENOMEM;
46 goto out_handle_destroy;
47 }
48
49 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
50 if (!state->nl80211) {
51 fprintf(stderr, "nl80211 not found.\n");
52 err = -ENOENT;
53 goto out_cache_free;
54 }
55
56 return 0;
57
58 out_cache_free:
59 nl_cache_free(state->nl_cache);
60 out_handle_destroy:
61 nl_handle_destroy(state->nl_handle);
62 return err;
63 }
64
65 static void nl80211_cleanup(struct nl80211_state *state)
66 {
67 genl_family_put(state->nl80211);
68 nl_cache_free(state->nl_cache);
69 nl_handle_destroy(state->nl_handle);
70 }
71
72 static void usage(const char *argv0)
73 {
74 struct cmd *cmd;
75
76 fprintf(stderr, "Usage:\t%s help\n", argv0);
77 for (cmd = &__start___cmd; cmd < &__stop___cmd; cmd++) {
78 switch (cmd->idby) {
79 case CIB_NONE:
80 fprintf(stderr, "\t%s %s %s\n", argv0, cmd->section, cmd->name);
81 break;
82 case CIB_PHY:
83 fprintf(stderr, "\t%s phy <phyname> ", argv0);
84 /* fall through */
85 case CIB_NETDEV:
86 if (cmd->idby == CIB_NETDEV)
87 fprintf(stderr, "\t%s dev <devname> ", argv0);
88 if (cmd->section)
89 fprintf(stderr, "%s ", cmd->section);
90 fprintf(stderr, "%s", cmd->name);
91 if (cmd->args)
92 fprintf(stderr, " %s", cmd->args);
93 fprintf(stderr, "\n");
94 break;
95 }
96 }
97 }
98
99 static int phy_lookup(char *name)
100 {
101 char buf[200];
102 int fd, pos;
103
104 snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
105
106 fd = open(buf, O_RDONLY);
107 pos = read(fd, buf, sizeof(buf) - 1);
108 if (pos < 0)
109 return -1;
110 buf[pos] = '\0';
111 return atoi(buf);
112 }
113
114 static int handle_phydev_cmd(struct nl80211_state *state,
115 enum command_identify_by idby,
116 int argc, char **argv)
117 {
118 struct cmd *cmd;
119 struct nl_msg *msg;
120 int devidx = 0;
121 const char *command, *section;
122
123 if (argc <= 1)
124 return -1;
125
126 switch (idby) {
127 case CIB_PHY:
128 devidx = phy_lookup(*argv);
129 argc--;
130 argv++;
131 break;
132 case CIB_NETDEV:
133 devidx = if_nametoindex(*argv);
134 argc--;
135 argv++;
136 break;
137 default:
138 break;
139 }
140
141 section = command = *argv;
142 argc--;
143 argv++;
144
145 for (cmd = &__start___cmd; cmd < &__stop___cmd; cmd++) {
146 if (cmd->idby != idby)
147 continue;
148 if (cmd->section) {
149 if (strcmp(cmd->section, section))
150 continue;
151 /* this is a bit icky ... */
152 if (command == section) {
153 if (argc <= 0)
154 return -1;
155 command = *argv;
156 argc--;
157 argv++;
158 }
159 } else if (section != command)
160 continue;
161 if (strcmp(cmd->name, command))
162 continue;
163 if (argc && !cmd->args)
164 continue;
165 break;
166 }
167
168 /* XXX: handle dev for phy */
169 if (cmd == &__stop___cmd)
170 return -1;
171
172 msg = nlmsg_alloc();
173 if (!msg) {
174 fprintf(stderr, "out of memory\n");
175 return 1;
176 }
177
178 genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0,
179 cmd->nl_msg_flags, cmd->cmd, 0);
180
181 switch (idby) {
182 case CIB_PHY:
183 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, devidx);
184 break;
185 case CIB_NETDEV:
186 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devidx);
187 break;
188 default:
189 break;
190 }
191
192 return cmd->handler(state, msg, argc, argv);
193 nla_put_failure:
194 fprintf(stderr, "building message failed\n");
195 return 1;
196 }
197
198 static int handle_other_cmd(struct nl80211_state *state, int argc, char **argv)
199 {
200 return -1;
201 }
202
203 int main(int argc, char **argv)
204 {
205 struct nl80211_state nlstate;
206 int err;
207 const char *argv0;
208
209 err = nl80211_init(&nlstate);
210 if (err)
211 return 1;
212
213 /* strip off self */
214 argc--;
215 argv0 = *argv++;
216
217 if (argc == 0 || strcmp(*argv, "help") == 0) {
218 usage(argv0);
219 goto out;
220 }
221
222 if (strcmp(*argv, "dev") == 0) {
223 argc--;
224 argv++;
225 err = handle_phydev_cmd(&nlstate, CIB_NETDEV, argc, argv);
226 } else if (strcmp(*argv, "phy") == 0) {
227 argc--;
228 argv++;
229 err = handle_phydev_cmd(&nlstate, CIB_PHY, argc, argv);
230 } else
231 err = handle_other_cmd(&nlstate, argc, argv);
232
233 if (err < 0)
234 usage(argv0);
235
236 out:
237 nl80211_cleanup(&nlstate);
238
239 return err;
240 }