summaryrefslogtreecommitdiffstats
path: root/net/travelmate/src/scan.uc
blob: f8d80736d061699dbb6781dd60b7bae836875fb6 (plain)
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
// travelmate - uplink scan: ranks an iwinfo scan reply, one entry per
// BSS, for the connection loop and the LuCI scan file

// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2016-2026 Dirk Brenken <dev@brenken.org>

const WPA = { '1': 'WPA1', '2': 'WPA2', '3': 'WPA3' };

// Drop control octets (awk [[:cntrl:]] under LC_ALL=C) bytewise: an ssid is
// arbitrary octets, and libc regex may refuse input that is not valid UTF-8.
function clean_ssid(s) {
	let out = '';

	for (let i = 0; i < length(s); i++) {
		let c = ord(s, i);

		if (c >= 32 && c != 127)
			out += substr(s, i, 1);
	}

	return out;
}

// -100 dBm = 0 %, -50 dBm = 100 %
export function quality(signal) {
	if (type(signal) != 'int')
		return 0;

	let q = 2 * (signal + 100);

	return q > 100 ? 100 : (q < 0 ? 0 : q);
}

// rpcd sends no 'wpa' array for WEP, only enabled plus 'wep'. Report it as
// such, otherwise the connection loop sees an open network.
function versions(enc) {
	let out = [];

	for (let n in (enc.wpa ?? []))
		if (WPA[n])
			push(out, WPA[n]);

	if (!length(out) && enc.enabled)
		return 'WEP';

	return length(out) ? join('+', out) : '-';
}

function words(list) {
	let out = [];

	for (let w in (list ?? []))
		if (length('' + w))
			push(out, uc('' + w));

	return length(out) ? join('+', out) : '-';
}

// quoted form used in scan lines and debug output
export function quote(s) {
	return '"' + join('\\"', split(s, '"')) + '"';
};

export function entry(r) {
	let enc = r?.encryption ?? {};
	let ssid = clean_ssid('' + (r?.ssid ?? ''));

	return {
		raw: ssid,
		quality: quality(r?.signal),
		channel: r?.channel ?? 0,
		bssid: length(r?.bssid ?? '') ? uc(r.bssid) : '-',
		wpa: versions(enc),
		cipher: words(enc.ciphers),
		auth: words(enc.authentication),
		ssid: length(ssid) ? quote(ssid) : 'hidden',
		hidden: !length(ssid),
		open: !enc.enabled,
		enc
	};
};

// the fields the connection loop compares, 'quality rsn wpa bssid essid',
// where rsn and wpa both '-' means open; also the sort key for ties
export function format(e) {
	return sprintf('%d %s - %s %s', e.quality, e.wpa, e.bssid, e.ssid);
};

// Same order as 'sort -rn' on the formatted lines: quality descending, ties
// by the whole line in reverse byte order (busybox and GNU fallback).
export function rank(results) {
	let rows = [];

	for (let r in (results ?? [])) {
		let e = entry(r);

		e.line = format(e);
		push(rows, e);
	}

	sort(rows, (a, b) => (b.quality - a.quality) ||
		(a.line < b.line ? 1 : (a.line > b.line ? -1 : 0)));

	return rows;
};

function upper(list) {
	return map(filter(list ?? [], (w) => length('' + w)), (w) => uc('' + w));
}

// the scan file for LuCI, a JSON array in ranking order (a hidden ssid is ''
// with hidden: true, encrypted: false for an open network)
export function report(results) {
	return map(rank(results), (e) => ({
		quality: e.quality,
		channel: e.channel,
		bssid: (e.bssid == '-') ? '' : e.bssid,
		ssid: e.raw,
		hidden: !length(e.raw),
		encrypted: !!e.enc.enabled,
		wpa: map(filter(e.enc.wpa ?? [], (n) => n in [ 1, 2, 3 ]), (n) => +n),
		ciphers: upper(e.enc.ciphers),
		auth: upper(e.enc.authentication)
	}));
};