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 err
= __prepare_listen_events(state
);
104 conn_argv
[1] = "connect";
105 conn_argv
[2] = "establish";
106 for (i
= 0; i
< argc
; i
++)
107 conn_argv
[i
+ 3] = argv
[i
];
108 err
= handle_cmd(state
, II_NETDEV
, conn_argc
, conn_argv
);
117 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
119 * This code has a bug:
121 * It is possible for a connect result message from another
122 * connect attempt to be processed here first, because we
123 * start listening to the multicast group before starting
124 * our own connect request, which may succeed but we get a
125 * fail message from a previous attempt that raced with us,
128 * The only proper way to fix this would be to listen to events
129 * before sending the command, and for the kernel to send the
130 * connect request or a cookie along with the event, so that you
131 * can match up whether the connect _you_ requested was finished
134 * Alas, the kernel doesn't do that (yet).
137 __do_listen_events(state
, ARRAY_SIZE(cmds
), cmds
, &printargs
);
140 TOPLEVEL(connect
, "[-w] <SSID> [<freq in MHz>] [<bssid>] [key 0:abcde d:1:6162636465]",
141 0, 0, CIB_NETDEV
, iw_connect
,
142 "Join the network with the given SSID (and frequency, BSSID).\n"
143 "With -w, wait for the connect to finish or fail.");
144 HIDDEN(connect
, establish
, "", NL80211_CMD_CONNECT
, 0, CIB_NETDEV
, iw_conn
);