5 * Copyright (C) 2007 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
38 #include "supplicant.h"
40 struct supplicant_task {
44 struct connman_iface *iface;
49 static GSList *tasks = NULL;
51 static struct supplicant_task *find_task(int ifindex)
55 for (list = tasks; list; list = list->next) {
56 struct supplicant_task *task = list->data;
58 if (task->ifindex == ifindex)
65 static int exec_cmd(struct supplicant_task *task, char *cmd)
67 return write(task->socket, cmd, strlen(cmd));
70 static gboolean control_event(GIOChannel *chan,
71 GIOCondition cond, gpointer data)
73 struct supplicant_task *task = data;
78 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
81 memset(buf, 0, sizeof(buf));
83 err = g_io_channel_read(chan, buf, sizeof(buf), &len);
85 if (err == G_IO_ERROR_AGAIN)
93 printf("[SUPPLICANT] %s\n", buf + 3);
95 if (g_str_has_prefix(buf + 3, "CTRL-EVENT-CONNECTED") == TRUE) {
96 printf("[SUPPLICANT] connected\n");
97 connman_iface_indicate_connected(task->iface);
100 if (g_str_has_prefix(buf + 3, "CTRL-EVENT-DISCONNECTED") == TRUE) {
101 printf("[SUPPLICANT] disconnected\n");
104 if (g_str_has_prefix(buf + 3, "CTRL-EVENT-TERMINATING") == TRUE) {
105 printf("[SUPPLICANT] terminating\n");
111 static int open_control(struct supplicant_task *task)
113 struct sockaddr_un addr;
116 printf("[SUPPLICANT] open control for %s\n", task->ifname);
118 sk = socket(PF_UNIX, SOCK_DGRAM, 0);
122 memset(&addr, 0, sizeof(addr));
123 addr.sun_family = AF_UNIX;
124 snprintf(addr.sun_path, sizeof(addr.sun_path),
125 "%s/%s.cli", STATEDIR, task->ifname);
126 //unlink(addr.sun_path);
128 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
133 memset(&addr, 0, sizeof(addr));
134 addr.sun_family = AF_UNIX;
135 snprintf(addr.sun_path, sizeof(addr.sun_path),
136 "%s/%s", STATEDIR, task->ifname);
138 if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
145 task->channel = g_io_channel_unix_new(sk);
146 g_io_channel_set_close_on_unref(task->channel, TRUE);
148 g_io_add_watch(task->channel,
149 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
150 control_event, task);
152 exec_cmd(task, "ATTACH");
153 exec_cmd(task, "ADD_NETWORK");
158 int __supplicant_start(struct connman_iface *iface)
161 struct supplicant_task *task;
165 sk = socket(PF_INET, SOCK_DGRAM, 0);
169 memset(&ifr, 0, sizeof(ifr));
170 ifr.ifr_ifindex = iface->index;
172 err = ioctl(sk, SIOCGIFNAME, &ifr);
179 printf("[SUPPLICANT] start %s\n", ifr.ifr_name);
181 task = g_try_new0(struct supplicant_task, 1);
185 task->ifindex = iface->index;
186 task->ifname = strdup(ifr.ifr_name);
189 if (task->ifname == NULL) {
194 argv[0] = "/sbin/wpa_supplicant";
201 argv[7] = task->ifname;
204 if (g_spawn_async(NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
205 NULL, NULL, &task->pid, NULL) == FALSE) {
206 printf("Failed to spawn wpa_supplicant\n");
210 tasks = g_slist_append(tasks, task);
212 printf("[SUPPLICANT] executed with pid %d\n", task->pid);
218 if (open_control(task) < 0)
219 printf("[SUPPLICANT] control failed\n");
224 int __supplicant_stop(struct connman_iface *iface)
226 struct supplicant_task *task;
227 char pathname[PATH_MAX];
229 task = find_task(iface->index);
233 printf("[SUPPLICANT] stop %s\n", task->ifname);
235 tasks = g_slist_remove(tasks, task);
237 exec_cmd(task, "DISABLE_NETWORK 0");
238 exec_cmd(task, "DETACH");
242 kill(task->pid, SIGTERM);
244 g_io_channel_shutdown(task->channel, TRUE, NULL);
245 g_io_channel_unref(task->channel);
247 snprintf(pathname, sizeof(pathname),
248 "%s/%s.cli", STATEDIR, task->ifname);
258 int __supplicant_connect(struct connman_iface *iface,
259 const char *network, const char *passphrase)
261 struct supplicant_task *task;
264 task = find_task(iface->index);
268 printf("[SUPPLICANT] connect %s\n", task->ifname);
270 exec_cmd(task, "DISABLE_NETWORK 0");
272 sprintf(cmd, "SET_NETWORK 0 ssid \"%s\"", network);
275 if (passphrase && strlen(passphrase) > 0) {
276 exec_cmd(task, "SET_NETWORK 0 proto RSN WPA");
277 exec_cmd(task, "SET_NETWORK 0 key_mgmt WPA-PSK");
279 sprintf(cmd, "SET_NETWORK 0 psk \"%s\"", passphrase);
282 exec_cmd(task, "SET_NETWORK 0 proto RSN WPA");
283 exec_cmd(task, "SET_NETWORK 0 key_mgmt NONE");
286 exec_cmd(task, "ENABLE_NETWORK 0");