1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #include "nsWifiMonitor.h"
6 #include "nsWifiAccessPoint.h"
7 
8 #include "nsCRT.h"
9 #include "nsServiceManagerUtils.h"
10 #include "nsComponentManagerUtils.h"
11 
12 #include <glib.h>
13 
14 #define DLADM_STRSIZE 256
15 #define DLADM_SECTIONS 3
16 
17 using namespace mozilla;
18 
19 struct val_strength_t {
20   const char* strength_name;
21   int signal_value;
22 };
23 
24 static val_strength_t strength_vals[] = {{"very weak", -112},
25                                          {"weak", -88},
26                                          {"good", -68},
27                                          {"very good", -40},
28                                          {"excellent", -16}};
29 
do_parse_str(char * bssid_str,char * essid_str,char * strength)30 static nsWifiAccessPoint* do_parse_str(char* bssid_str, char* essid_str,
31                                        char* strength) {
32   unsigned char mac_as_int[6] = {0};
33   sscanf(bssid_str, "%x:%x:%x:%x:%x:%x", &mac_as_int[0], &mac_as_int[1],
34          &mac_as_int[2], &mac_as_int[3], &mac_as_int[4], &mac_as_int[5]);
35 
36   int signal = 0;
37   uint32_t strength_vals_count = sizeof(strength_vals) / sizeof(val_strength_t);
38   for (uint32_t i = 0; i < strength_vals_count; i++) {
39     if (!nsCRT::strncasecmp(strength, strength_vals[i].strength_name,
40                             DLADM_STRSIZE)) {
41       signal = strength_vals[i].signal_value;
42       break;
43     }
44   }
45 
46   nsWifiAccessPoint* ap = new nsWifiAccessPoint();
47   if (ap) {
48     ap->setMac(mac_as_int);
49     ap->setSignal(signal);
50     size_t len = essid_str ? strnlen(essid_str, DLADM_STRSIZE) : 0;
51     ap->setSSID(essid_str, len);
52   }
53   return ap;
54 }
55 
do_dladm(nsCOMArray<nsWifiAccessPoint> & accessPoints)56 static void do_dladm(nsCOMArray<nsWifiAccessPoint>& accessPoints) {
57   GError* err = nullptr;
58   char* sout = nullptr;
59   char* serr = nullptr;
60   int exit_status = 0;
61   char* dladm_args[] = {
62       "/usr/bin/pfexec",     "/usr/sbin/dladm", "scan-wifi", "-p", "-o",
63       "BSSID,ESSID,STRENGTH"};
64 
65   gboolean rv = g_spawn_sync("/", dladm_args, nullptr, (GSpawnFlags)0, nullptr,
66                              nullptr, &sout, &serr, &exit_status, &err);
67   if (rv && !exit_status) {
68     char wlan[DLADM_SECTIONS][DLADM_STRSIZE + 1];
69     uint32_t section = 0;
70     uint32_t sout_scan = 0;
71     uint32_t wlan_put = 0;
72     bool escape = false;
73     nsWifiAccessPoint* ap;
74     char sout_char;
75     do {
76       sout_char = sout[sout_scan++];
77       if (escape) {
78         escape = false;
79         if (sout_char != '\0') {
80           wlan[section][wlan_put++] = sout_char;
81           continue;
82         }
83       }
84 
85       if (sout_char == '\\') {
86         escape = true;
87         continue;
88       }
89 
90       if (sout_char == ':') {
91         wlan[section][wlan_put] = '\0';
92         section++;
93         wlan_put = 0;
94         continue;
95       }
96 
97       if ((sout_char == '\0') || (sout_char == '\n')) {
98         wlan[section][wlan_put] = '\0';
99         if (section == DLADM_SECTIONS - 1) {
100           ap = do_parse_str(wlan[0], wlan[1], wlan[2]);
101           if (ap) {
102             accessPoints.AppendObject(ap);
103           }
104         }
105         section = 0;
106         wlan_put = 0;
107         continue;
108       }
109 
110       wlan[section][wlan_put++] = sout_char;
111 
112     } while ((wlan_put <= DLADM_STRSIZE) && (section < DLADM_SECTIONS) &&
113              (sout_char != '\0'));
114   }
115 
116   g_free(sout);
117   g_free(serr);
118 }
119 
DoScan()120 nsresult nsWifiMonitor::DoScan() {
121   // Regularly get the access point data.
122 
123   nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
124   nsCOMArray<nsWifiAccessPoint> accessPoints;
125 
126   while (mKeepGoing) {
127     accessPoints.Clear();
128     do_dladm(accessPoints);
129 
130     bool accessPointsChanged =
131         !AccessPointsEqual(accessPoints, lastAccessPoints);
132     ReplaceArray(lastAccessPoints, accessPoints);
133 
134     nsresult rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
135     NS_ENSURE_SUCCESS(rv, rv);
136 
137     LOG(("waiting on monitor\n"));
138 
139     ReentrantMonitorAutoEnter mon(mReentrantMonitor);
140     mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
141   }
142 
143   return NS_OK;
144 }
145