3 #include <netlink/genl/genl.h>
4 #include <netlink/genl/family.h>
5 #include <netlink/genl/ctrl.h>
6 #include <netlink/msg.h>
7 #include <netlink/attr.h>
12 static int iw_conn(struct nl80211_state
*state
, struct nl_cb
*cb
,
13 struct nl_msg
*msg
, int argc
, char **argv
)
16 unsigned char bssid
[6];
23 NLA_PUT(msg
, NL80211_ATTR_SSID
, strlen(argv
[0]), argv
[0]);
29 freq
= strtoul(argv
[0], &end
, 10);
31 NLA_PUT_U32(msg
, NL80211_ATTR_WIPHY_FREQ
, freq
);
39 if (mac_addr_a2n(bssid
, argv
[0]) == 0) {
40 NLA_PUT(msg
, NL80211_ATTR_MAC
, 6, bssid
);
49 if (strcmp(*argv
, "key") != 0 && strcmp(*argv
, "keys") != 0)
55 return parse_keys(msg
, argv
, argc
);
60 static int disconnect(struct nl80211_state
*state
,
63 int argc
, char **argv
)
67 TOPLEVEL(disconnect
, NULL
,
68 NL80211_CMD_DISCONNECT
, 0, CIB_NETDEV
, disconnect
,
69 "Disconnect from the current network.");
71 static int iw_connect(struct nl80211_state
*state
, struct nl_cb
*cb
,
72 struct nl_msg
*msg
, int argc
, char **argv
)
74 char **conn_argv
, *dev
= argv
[0];
75 static const __u32 cmds
[] = {
78 struct print_event_args printargs
= { };
83 /* strip "wlan0 connect" */
88 if (argc
&& strcmp(argv
[0], "-w") == 0) {
95 conn_argv
= calloc(conn_argc
, sizeof(*conn_argv
));
99 conn_argv
[1] = "connect";
100 conn_argv
[2] = "establish";
101 for (i
= 0; i
< argc
; i
++)
102 conn_argv
[i
+ 3] = argv
[i
];
103 err
= handle_cmd(state
, II_NETDEV
, conn_argc
, conn_argv
);
112 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
114 * This code has a bug, which requires creating a separate
115 * nl80211 socket to fix:
116 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
117 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
118 * before (!) we listen to it, because we only start listening
119 * after we send our scan request.
121 * Doing it the other way around has a race condition as well,
122 * if you first open the events socket you may get a notification
123 * for a previous scan.
125 * The only proper way to fix this would be to listen to events
126 * before sending the command, and for the kernel to send the
127 * connect request along with the event, so that you can match
128 * up whether the connect _you_ requested was finished or aborted.
130 * Alas, the kernel doesn't do that (yet).
133 __listen_events(state
, ARRAY_SIZE(cmds
), cmds
, &printargs
);
136 TOPLEVEL(connect
, "[-w] <SSID> [<freq in MHz>] [<bssid>] [key 0:abcde d:1:6162636465]",
137 0, 0, CIB_NETDEV
, iw_connect
,
138 "Join the network with the given SSID (and frequency, BSSID).\n"
139 "With -w, wait for the connect to finish or fail.");
140 HIDDEN(connect
, establish
, "", NL80211_CMD_CONNECT
, 0, CIB_NETDEV
, iw_conn
);