Skip to content

Commit 900a8b6

Browse files
committed
remote_access: Fix local IP check
- Fix connection ID and connection index mix-up. - Perform IP check before accessing the config because it's quicker and more likely to fail. - Skip all checks if remote access isn't enabled or not connected yet.
1 parent 3184886 commit 900a8b6

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

software/src/modules/remote_access/remote_access.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,16 +2201,40 @@ int RemoteAccess::stop_ping() {
22012201
return 0;
22022202
}
22032203

2204-
bool RemoteAccess::is_connected_local_ip(const IPAddress &ip) {
2205-
if (connection_state.count() < static_cast<size_t>(ip[2] + 1)) {
2204+
bool RemoteAccess::is_connected_local_ip(const IPAddress &local_ip) {
2205+
if (!done) {
22062206
return false;
22072207
}
22082208

2209-
if (connection_state.get(ip[2])->get("state")->asUint8() == 2 && ip[0] == 10 && ip[1] == 123 && ip[3] == 3) {
2210-
return true;
2209+
const uint32_t ip = static_cast<uint32_t>(local_ip);
2210+
const uint32_t masked_ip = ip & 0xFF00FFFFul;
2211+
constexpr uint32_t expected_masked_ip = ntohl((10u << 24) + (123u << 16) + (0u << 8) + (2u << 0)); // 10.123.x.2
2212+
2213+
if (masked_ip != expected_masked_ip) {
2214+
return false;
2215+
}
2216+
2217+
const uint8_t conn_id = (ip >> 16) & 0xFF;
2218+
size_t conn_idx;
2219+
2220+
for (size_t i = 0; i < MAX_USER_CONNECTIONS; i++) {
2221+
if (remote_connections[i].id == conn_id) {
2222+
conn_idx = i;
2223+
goto conn_idx_found;
2224+
}
22112225
}
22122226

2227+
logger.printfln("Local IP query couldn't find connection ID %hhu", conn_id);
22132228
return false;
2229+
2230+
conn_idx_found:
2231+
const uint8_t conn_state = connection_state.get(conn_idx + 1)->get("state")->asUint8();
2232+
2233+
if (conn_state != 2) {
2234+
return false;
2235+
}
2236+
2237+
return true;
22142238
}
22152239

22162240
[[gnu::const]]

0 commit comments

Comments
 (0)