]>
git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
9aa118a1a4502985c7afac74919e4a2966e8d023
2 * nl80211 userspace tool
4 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
11 #include <sys/types.h>
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>
26 static int nl80211_init(struct nl80211_state
*state
)
30 state
->nl_handle
= nl_handle_alloc();
31 if (!state
->nl_handle
) {
32 fprintf(stderr
, "Failed to allocate netlink handle.\n");
36 if (genl_connect(state
->nl_handle
)) {
37 fprintf(stderr
, "Failed to connect to generic netlink.\n");
39 goto out_handle_destroy
;
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");
46 goto out_handle_destroy
;
49 state
->nl80211
= genl_ctrl_search_by_name(state
->nl_cache
, "nl80211");
50 if (!state
->nl80211
) {
51 fprintf(stderr
, "nl80211 not found.\n");
59 nl_cache_free(state
->nl_cache
);
61 nl_handle_destroy(state
->nl_handle
);
65 static void nl80211_cleanup(struct nl80211_state
*state
)
67 genl_family_put(state
->nl80211
);
68 nl_cache_free(state
->nl_cache
);
69 nl_handle_destroy(state
->nl_handle
);
72 static void usage(const char *argv0
)
76 fprintf(stderr
, "Usage:\t%s help\n", argv0
);
77 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
; cmd
++) {
80 fprintf(stderr
, "\t%s ", argv0
);
83 if (cmd
->idby
== CIB_PHY
)
84 fprintf(stderr
, "\t%s phy <phyname> ", argv0
);
87 if (cmd
->idby
== CIB_NETDEV
)
88 fprintf(stderr
, "\t%s dev <devname> ", argv0
);
90 fprintf(stderr
, "%s ", cmd
->section
);
91 fprintf(stderr
, "%s", cmd
->name
);
93 fprintf(stderr
, " %s", cmd
->args
);
94 fprintf(stderr
, "\n");
100 static int phy_lookup(char *name
)
105 snprintf(buf
, sizeof(buf
), "/sys/class/ieee80211/%s/index", name
);
107 fd
= open(buf
, O_RDONLY
);
108 pos
= read(fd
, buf
, sizeof(buf
) - 1);
115 static int handle_cmd(struct nl80211_state
*state
,
116 enum command_identify_by idby
,
117 int argc
, char **argv
)
122 const char *command
, *section
;
124 if (argc
<= 1 && idby
!= CIB_NONE
)
129 devidx
= phy_lookup(*argv
);
134 devidx
= if_nametoindex(*argv
);
142 section
= command
= *argv
;
146 for (cmd
= &__start___cmd
; cmd
< &__stop___cmd
; cmd
++) {
147 if (cmd
->idby
!= idby
)
150 if (strcmp(cmd
->section
, section
))
152 /* this is a bit icky ... */
153 if (command
== section
) {
160 } else if (section
!= command
)
162 if (strcmp(cmd
->name
, command
))
164 if (argc
&& !cmd
->args
)
169 if (cmd
== &__stop___cmd
)
174 fprintf(stderr
, "out of memory\n");
178 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
179 cmd
->nl_msg_flags
, cmd
->cmd
, 0);
183 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY
, devidx
);
186 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, devidx
);
192 return cmd
->handler(state
, msg
, argc
, argv
);
194 fprintf(stderr
, "building message failed\n");
198 int main(int argc
, char **argv
)
200 struct nl80211_state nlstate
;
204 err
= nl80211_init(&nlstate
);
212 if (argc
== 0 || strcmp(*argv
, "help") == 0) {
217 if (strcmp(*argv
, "dev") == 0) {
220 err
= handle_cmd(&nlstate
, CIB_NETDEV
, argc
, argv
);
221 } else if (strcmp(*argv
, "phy") == 0) {
224 err
= handle_cmd(&nlstate
, CIB_PHY
, argc
, argv
);
226 err
= handle_cmd(&nlstate
, CIB_NONE
, argc
, argv
);
232 nl80211_cleanup(&nlstate
);