summaryrefslogtreecommitdiffstats
path: root/net/travelmate/src/check.uc
blob: 327451a0be8336cab26d94b61e95ad0d1968c5dd (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
126
127
128
129
130
131
132
133
134
135
136
// travelmate - uplink check decisions: init() resolves the uplink before the
// poll, round() returns the steps (op) of one polling round

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

import { ref as uplink_ref, option as uplink_option } from 'travelmate.uplink';

// st: { mode, status (bool), radio, essid, bssid, ifstatus, wifireload (bool) }
export function init(st, uplinks) {
	let uref = uplink_ref(uplinks, st.radio, st.essid, st.bssid);
	let enabled = true;

	if (st.mode != 'rev' && length(st.radio) && st.radio != '-' && length(st.essid) && st.essid != '-')
		enabled = uplink_option(uplinks, uref, 'enabled', '0') == '1';

	// wifi reload on disconnects (not initial/dev), on disabled-uplink dev
	// events or on idle dev events if requested by the main cycle
	let wifi = (st.mode != 'initial' && st.mode != 'dev' && !st.status) ||
		(st.mode == 'dev' && ((!st.status && st.wifireload) ||
			(st.ifstatus !== st.status && !enabled)));

	return { uplinkcfg: uref, enabled, wifi };
};

// drop the connection and settle the status
function drop(out, st, msg) {
	if (msg)
		push(out, { op: 'log', class: 'info', msg }, { op: 'vpn' });

	push(out, { op: 'conn' }, { op: 'ifstatus', value: st.status }, { op: 'genstatus' }, { op: 'break' });

	return out;
}

// st: { mode, status, enabled, ifstatus, connection, wait, maxwait,
// minquality, quality (of an earlier round), sta_id }, q: probe queries
export function round(st, q) {
	let out = [], mode = st.mode, sta_id = st.sta_id;

	if (q.wireless()) {
		// dev mode: persist a status change and end
		if (mode == 'dev') {
			if (st.ifstatus !== st.status)
				push(out, { op: 'ifstatus', value: st.status }, { op: 'genstatus' });

			if (!st.status)
				push(out, { op: 'sleep', s: int(st.maxwait / 6) });

			push(out, { op: 'break' });

			return out;
		}

		// rev mode: drop the connection state and end
		if (mode == 'rev') {
			push(out, { op: 'conn' }, { op: 'ifstatus', value: st.status }, { op: 'break' });

			return out;
		}

		// initial/sta mode: look at the sta interface
		let ifname = q.ifname();

		if (length(ifname) && st.enabled) {
			let quality = q.quality(ifname) ?? st.quality;

			push(out, { op: 'probe', ifname, quality });

			// no signal: connection dropped or overall wait timeout
			if (quality == null) {
				let up = q.up();

				push(out, { op: 'ifstatus', value: up });

				if (st.connection && up === false)
					return drop(out, st, sprintf("no signal from uplink '%s'", sta_id));

				if (st.wait == st.maxwait)
					return drop(out, st, sprintf("uplink connection could not be established after %d seconds '%s'", st.maxwait, sta_id));

				push(out, { op: 'continue' });

				return out;
			}

			// acceptable signal: net/captive check once the interface is up
			if (quality >= st.minquality) {
				let up = q.up();

				push(out, { op: 'ifstatus', value: up });

				if (up === true) {
					push(out, { op: 'net' });

					return out;
				}
			}

			// signal below minquality on an existing link
			else if (st.connection && mode == 'initial') {
				return drop(out, st, sprintf("uplink is out of range '%s' (%d/%d)", sta_id, quality, st.minquality));
			}

			// signal below minquality on an initial/sta probe
			else if (mode == 'initial' || mode == 'sta') {
				return drop(out, st);
			}
		}
		else {
			push(out, { op: 'probe', ifname, quality: st.quality });

			// sta interface gone while connected
			if (st.connection)
				return drop(out, st, sprintf("uplink connection lost '%s' (interface gone)", sta_id));

			// initial probe, no sta interface yet
			if (mode == 'initial') {
				push(out, { op: 'ifstatus', value: st.status }, { op: 'genstatus' }, { op: 'break' });

				return out;
			}
		}
	}

	// initial mode safety net, also on an empty wireless status
	if (mode == 'initial') {
		if (st.connection)
			push(out, { op: 'log', class: 'info', msg: sprintf("uplink connection lost '%s' (interface down)", sta_id) },
				{ op: 'vpn' }, { op: 'conn' });

		push(out, { op: 'ifstatus', value: st.status }, { op: 'genstatus' }, { op: 'break' });
	}

	return out;
};