-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkPage.cpp
More file actions
187 lines (176 loc) · 4.8 KB
/
Copy pathNetworkPage.cpp
File metadata and controls
187 lines (176 loc) · 4.8 KB
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
/*
* This file is part of the Eclipse2024 project. It is subject to the GPLv3
* license terms in the LICENSE file found in the top-level directory of this
* distribution and at
* https://github.com/jjackowski/eclipse2024/blob/master/LICENSE.
* No part of the Eclipse2024 project, including this file, may be copied,
* modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*
* Copyright (C) 2024 Jeff Jackowski
*/
#include "NetworkPage.hpp"
#include "Screen.hpp"
#include <linux/wireless.h>
NetInterface::NetInterface(const std::string &n, const sockaddr_in *sa) :
ifname(n),
addr(boost::asio::ip::address_v4(
htonl(sa->sin_addr.s_addr)
// sin_addr is allways big endian; must not use int constructor since it
// expects host byte order
//(boost::asio::ip::address_v4::bytes_type*)&(sa->sin_addr.s_addr)
)) {
wlQuery();
}
NetInterface::NetInterface(
const std::string &n, const boost::asio::ip::address_v4 &sa
) : ifname(n), addr(sa) {
wlQuery();
}
void NetInterface::wlQuery() {
// query for wireless network data
// request struct
iwreq req;
// dummy socket
int sock = socket(AF_INET, SOCK_DGRAM, 0);
// copy in network interface name
std::copy_n(
ifname.begin(),
std::min(ifname.size() + 1, sizeof(req.ifr_name)),
req.ifr_name
);
// ask for network name
char essidbuff[32];
req.u.essid.pointer = essidbuff;
req.u.essid.length = 32;
/**
* @bug Always fails on my development machine (kernel 6.6.13), but
* succeeds on the target Raspberry Pi (kernel 6.1.?). Why?
*/
if (ioctl(sock, SIOCGIWESSID, &req) >= 0) {
// reported string is not NULL terminated, but length is reported
id.append(essidbuff, req.u.essid.length);
}
close(sock);
}
Page::SelectionResponse NetworkPage::select(
const DisplayInfo &di,
SelectionCause sc
) {
// if change is already non-zero, keep it non-zero
change |= fillnetifs();
if (
// user wants it
(sc == SelectUser) ||
(
// networks present
!netifs.empty() &&
// don't auto-show once the eclipse starts
(di.goodfix && (di.now < (di.start - DisplayInfo::beforeTotality)))
// auto-show without a good fix
|| !di.goodfix
)
) {
return SelectPage;
}
return SkipPage;
}
int NetworkPage::fillnetifs() {
std::set<std::string> seen;
ifaddrs *ifAddrStruct = nullptr;
ifaddrs *ifa = nullptr;
getifaddrs(&ifAddrStruct);
int updates = 0;
for (ifa = ifAddrStruct; ifa; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr) {
continue;
}
// IPv4 address?
if (ifa->ifa_addr->sa_family == AF_INET) {
boost::asio::ip::address_v4 ip4(
htonl(((sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr)
//(boost::asio::ip::address_v4::bytes_type)
//&((sockaddr_in*)ifa->ifa_addr)->sin_addr
);
if (!ip4.is_loopback() && !ip4.is_multicast()) {
//std::cout << "Net: " << ifa->ifa_name << " addr: " <<
//ip4.to_string() << std::endl;
// Test system has multiple addresses per adapter; only the
// first is reported by ifconfig, so ignore the rest. KDE shows
// the other address. Neither shows both addresses.
// One day figure out what is going on.
if (seen.count(ifa->ifa_name) > 0) {
continue;
}
// look for an existing network
NetInterfaceSet::iterator iter = netifs.find(ifa->ifa_name);
// found it?
if (iter != netifs.end()) {
// different address?
if (iter->address() != ip4) {
// replace the object
netifs.erase(iter);
netifs.emplace(ifa->ifa_name, ip4);
++updates;
}
// if same address, leave object as is
} else {
// new network, new object
netifs.emplace(ifa->ifa_name, ip4);
++updates;
}
// always mark as seen
seen.insert(ifa->ifa_name);
}
}
}
if (ifAddrStruct) {
freeifaddrs(ifAddrStruct);
}
// check for removals
NetInterfaceSet::iterator iter = netifs.begin();
while (iter != netifs.end()) {
std::set<std::string>::iterator siter = seen.find(iter->name());
// not found?
if (siter == seen.end()) {
// remove it
iter = netifs.erase(iter);
++updates;
} else {
++iter;
}
}
return updates;
}
void NetworkPage::show(const DisplayInfo &, Screen *scr) {
scr->showTitle("Network");
scr->largeTime(false);
if (change) {
last = netifs.begin();
} else if (netifs.empty()) {
scr->showText("No networks", 0, 0);
return;
} else {
++last;
if (last == netifs.end()) {
last = netifs.begin();
}
}
change = 0;
std::ostringstream oss;
oss << last->name();
scr->showText(oss.str(), 0, 0);
oss.str(std::string());
if (last->isWireless()) {
oss << last->essid();
scr->showText(oss.str(), 0, 1);
oss.str(std::string());
}
oss << last->address().to_string();
scr->showText(oss.str(), 0, 2);
}
void NetworkPage::update(const DisplayInfo &, Screen *) { }
void NetworkPage::hide(const DisplayInfo &, Screen *scr) {
scr->hideText();
scr->largeTime(true);
}