]>
Commit | Line | Data |
---|---|---|
cad53b3f JB |
1 | /* |
2 | * nl80211 userspace tool | |
3 | * | |
4 | * Copyright 2007 Johannes Berg <johannes@sipsolutions.net> | |
5 | * | |
6 | * GPLv2 | |
7 | */ | |
8 | ||
9 | #include <errno.h> | |
10 | #include <stdio.h> | |
11 | #include <netlink/genl/genl.h> | |
12 | #include <netlink/genl/family.h> | |
13 | #include <netlink/genl/ctrl.h> | |
14 | #include <netlink/msg.h> | |
15 | #include <netlink/attr.h> | |
16 | #include <linux/nl80211.h> | |
17 | ||
18 | #include "iw.h" | |
19 | ||
20 | ||
21 | static int nl80211_init(struct nl80211_state *state) | |
22 | { | |
23 | int err; | |
24 | ||
25 | state->nl_handle = nl_handle_alloc(); | |
26 | if (!state->nl_handle) { | |
27 | fprintf(stderr, "Failed to allocate netlink handle.\n"); | |
28 | return -ENOMEM; | |
29 | } | |
30 | ||
31 | if (genl_connect(state->nl_handle)) { | |
32 | fprintf(stderr, "Failed to connect to generic netlink.\n"); | |
33 | err = -ENOLINK; | |
34 | goto out_handle_destroy; | |
35 | } | |
36 | ||
37 | state->nl_cache = genl_ctrl_alloc_cache(state->nl_handle); | |
38 | if (!state->nl_cache) { | |
39 | fprintf(stderr, "Failed to allocate generic netlink cache.\n"); | |
40 | err = -ENOMEM; | |
41 | goto out_handle_destroy; | |
42 | } | |
43 | ||
44 | state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211"); | |
45 | if (!state->nl80211) { | |
46 | fprintf(stderr, "nl80211 not found.\n"); | |
47 | err = -ENOENT; | |
48 | goto out_cache_free; | |
49 | } | |
50 | ||
51 | return 0; | |
52 | ||
53 | out_cache_free: | |
54 | nl_cache_free(state->nl_cache); | |
55 | out_handle_destroy: | |
56 | nl_handle_destroy(state->nl_handle); | |
57 | return err; | |
58 | } | |
59 | ||
60 | static void nl80211_cleanup(struct nl80211_state *state) | |
61 | { | |
62 | genl_family_put(state->nl80211); | |
63 | nl_cache_free(state->nl_cache); | |
64 | nl_handle_destroy(state->nl_handle); | |
65 | } | |
66 | ||
67 | int main(int argc, char **argv) | |
68 | { | |
69 | struct nl80211_state nlstate; | |
70 | int err; | |
71 | ||
72 | err = nl80211_init(&nlstate); | |
73 | if (err) | |
74 | return 1; | |
75 | ||
76 | ||
77 | nl80211_cleanup(&nlstate); | |
78 | ||
79 | return 0; | |
80 | } |