1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "WifiHotspotUtils.h"
#include <dlfcn.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <cutils/properties.h>
#include "prinit.h"
#include "mozilla/Assertions.h"
#include "mozilla/Sprintf.h"
#include "nsDebug.h"
#include "nsPrintfCString.h"
static void* sWifiHotspotUtilsLib;
static PRCallOnceType sInitWifiHotspotUtilsLib;
// Socket pair used to exit from a blocking read.
static struct wpa_ctrl* ctrl_conn;
static const char *ctrl_iface_dir = "/data/misc/wifi/hostapd";
static char *ctrl_ifname = nullptr;
DEFINE_DLFUNC(wpa_ctrl_open, struct wpa_ctrl*, const char*)
DEFINE_DLFUNC(wpa_ctrl_close, void, struct wpa_ctrl*)
DEFINE_DLFUNC(wpa_ctrl_attach, int32_t, struct wpa_ctrl*)
DEFINE_DLFUNC(wpa_ctrl_detach, int32_t, struct wpa_ctrl*)
DEFINE_DLFUNC(wpa_ctrl_request, int32_t, struct wpa_ctrl*,
const char*, size_t cmd_len, char *reply,
size_t *reply_len, void (*msg_cb)(char *msg, size_t len))
static PRStatus
InitWifiHotspotUtilsLib()
{
sWifiHotspotUtilsLib = dlopen("/system/lib/libwpa_client.so", RTLD_LAZY);
// We might fail to open the hardware lib. That's OK.
return PR_SUCCESS;
}
static void*
GetWifiHotspotLibHandle()
{
PR_CallOnce(&sInitWifiHotspotUtilsLib, InitWifiHotspotUtilsLib);
return sWifiHotspotUtilsLib;
}
struct wpa_ctrl *
WifiHotspotUtils::openConnection(const char *ifname)
{
if (!ifname) {
return nullptr;
}
USE_DLFUNC(wpa_ctrl_open)
ctrl_conn = wpa_ctrl_open(nsPrintfCString("%s/%s", ctrl_iface_dir, ifname).get());
return ctrl_conn;
}
int32_t
WifiHotspotUtils::sendCommand(struct wpa_ctrl *ctrl, const char *cmd,
char *reply, size_t *reply_len)
{
int32_t ret;
if (!ctrl_conn) {
NS_WARNING(nsPrintfCString("Not connected to hostapd - \"%s\" command dropped.\n", cmd).get());
return -1;
}
USE_DLFUNC(wpa_ctrl_request)
ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), reply, reply_len, nullptr);
if (ret == -2) {
NS_WARNING(nsPrintfCString("'%s' command timed out.\n", cmd).get());
return -2;
} else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) {
return -1;
}
// Make the reply printable.
reply[*reply_len] = '\0';
if (strncmp(cmd, "STA-FIRST", 9) == 0 ||
strncmp(cmd, "STA-NEXT", 8) == 0) {
char *pos = reply;
while (*pos && *pos != '\n')
pos++;
*pos = '\0';
}
return 0;
}
// static
void*
WifiHotspotUtils::GetSharedLibrary()
{
void* wpaClientLib = GetWifiHotspotLibHandle();
if (!wpaClientLib) {
NS_WARNING("No /system/lib/libwpa_client.so");
}
return wpaClientLib;
}
int32_t WifiHotspotUtils::do_wifi_connect_to_hostapd()
{
struct dirent *dent;
DIR *dir = opendir(ctrl_iface_dir);
if (dir) {
while ((dent = readdir(dir))) {
if (strcmp(dent->d_name, ".") == 0 ||
strcmp(dent->d_name, "..") == 0) {
continue;
}
ctrl_ifname = strdup(dent->d_name);
break;
}
closedir(dir);
}
ctrl_conn = openConnection(ctrl_ifname);
if (!ctrl_conn) {
NS_WARNING(nsPrintfCString("Unable to open connection to hostapd on \"%s\": %s",
ctrl_ifname, strerror(errno)).get());
return -1;
}
USE_DLFUNC(wpa_ctrl_attach)
if (wpa_ctrl_attach(ctrl_conn) != 0) {
USE_DLFUNC(wpa_ctrl_close)
wpa_ctrl_close(ctrl_conn);
ctrl_conn = nullptr;
return -1;
}
return 0;
}
int32_t WifiHotspotUtils::do_wifi_close_hostapd_connection()
{
if (!ctrl_conn) {
NS_WARNING("Invalid ctrl_conn.");
return -1;
}
USE_DLFUNC(wpa_ctrl_detach)
if (wpa_ctrl_detach(ctrl_conn) < 0) {
NS_WARNING("Failed to detach wpa_ctrl.");
}
USE_DLFUNC(wpa_ctrl_close)
wpa_ctrl_close(ctrl_conn);
ctrl_conn = nullptr;
return 0;
}
int32_t WifiHotspotUtils::do_wifi_hostapd_command(const char *command,
char *reply,
size_t *reply_len)
{
return sendCommand(ctrl_conn, command, reply, reply_len);
}
int32_t WifiHotspotUtils::do_wifi_hostapd_get_stations()
{
char addr[32], cmd[64];
int stations = 0;
size_t addrLen = sizeof(addr);
if (sendCommand(ctrl_conn, "STA-FIRST", addr, &addrLen)) {
return 0;
}
stations++;
SprintfLiteral(cmd, "STA-NEXT %s", addr);
while (sendCommand(ctrl_conn, cmd, addr, &addrLen) == 0) {
stations++;
SprintfLiteral(cmd, "STA-NEXT %s", addr);
}
return stations;
}
|