]> git.ipfire.org Git - thirdparty/iw.git/blob - iw.c
fix stupid bug
[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 <netlink/genl/genl.h>
10 #include <netlink/genl/family.h>
11 #include <netlink/genl/ctrl.h>
12 #include <netlink/msg.h>
13 #include <netlink/attr.h>
14 #include <linux/nl80211.h>
15
16 #include "iw.h"
17
18
19 static int nl80211_init(struct nl80211_state *state)
20 {
21 int err;
22
23 state->nl_handle = nl_handle_alloc();
24 if (!state->nl_handle) {
25 fprintf(stderr, "Failed to allocate netlink handle.\n");
26 return -ENOMEM;
27 }
28
29 if (genl_connect(state->nl_handle)) {
30 fprintf(stderr, "Failed to connect to generic netlink.\n");
31 err = -ENOLINK;
32 goto out_handle_destroy;
33 }
34
35 state->nl_cache = genl_ctrl_alloc_cache(state->nl_handle);
36 if (!state->nl_cache) {
37 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
38 err = -ENOMEM;
39 goto out_handle_destroy;
40 }
41
42 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
43 if (!state->nl80211) {
44 fprintf(stderr, "nl80211 not found.\n");
45 err = -ENOENT;
46 goto out_cache_free;
47 }
48
49 return 0;
50
51 out_cache_free:
52 nl_cache_free(state->nl_cache);
53 out_handle_destroy:
54 nl_handle_destroy(state->nl_handle);
55 return err;
56 }
57
58 static void nl80211_cleanup(struct nl80211_state *state)
59 {
60 genl_family_put(state->nl80211);
61 nl_cache_free(state->nl_cache);
62 nl_handle_destroy(state->nl_handle);
63 }
64
65 /*
66 * return
67 * 0 - error
68 * 1 - phy
69 * 2 - dev
70 */
71 static int get_phy_or_dev(int *argc, char ***argv, char **name)
72 {
73 char *type = (*argv)[0];
74
75 if (*argc < 2)
76 return 0;
77
78 *name = (*argv)[1];
79
80 *argc -= 2;
81 *argv += 2;
82
83 if (strcmp(type, "phy") == 0)
84 return 1;
85 if (strcmp(type, "dev") == 0)
86 return 2;
87
88 return 0;
89 }
90
91 void usage(char *argv0)
92 {
93 fprintf(stderr, "Usage: %1$s dev <phydev> interface <COMMAND> [OPTIONS]\n"
94 " %1$s dev <phydev> info\n"
95 "\n"
96 "where COMMAND := { add | del }\n"
97 "\n"
98 "For add, OPTIONS := <name> type <type>\n"
99 "For del, OPTIONS should be blank and phydev is the interface to delete.\n", argv0);
100 }
101
102 int main(int argc, char **argv)
103 {
104 struct nl80211_state nlstate;
105 int err = 0, pod;
106 char *ifname = NULL, *phyname = NULL, *type, *argv0;
107
108 err = nl80211_init(&nlstate);
109 if (err)
110 return 1;
111
112 /* strip off self */
113 argc--;
114 argv0 = *argv++;
115
116 if (argc == 0 || (argc == 1 && strcmp(*argv, "help") == 0)) {
117 usage(argv0);
118 goto out;
119 }
120
121 pod = get_phy_or_dev(&argc, &argv, &ifname);
122 if (pod == 0) {
123 err = 1;
124 goto out;
125 }
126
127 if (pod == 1) {
128 phyname = ifname;
129 ifname = NULL;
130 }
131
132 if (argc <= 0) {
133 err = 1;
134 goto out;
135 }
136
137 type = argv[0];
138 argc--;
139 argv++;
140
141 if (strcmp(type, "interface") == 0)
142 err = handle_interface(&nlstate, phyname, ifname, argc, argv);
143 else if (strcmp(type, "info") == 0)
144 err = handle_info(&nlstate, phyname, ifname);
145 else {
146 fprintf(stderr, "No such object type %s\n", type);
147 err = 1;
148 }
149
150 out:
151 nl80211_cleanup(&nlstate);
152
153 return err;
154 }