-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetstat.cpp
More file actions
84 lines (75 loc) · 2.77 KB
/
Copy pathnetstat.cpp
File metadata and controls
84 lines (75 loc) · 2.77 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
// netstat.cpp — display network sockets (BusyBox netstat minimal subset).
//
// Wave 1c scope: -t/-u/-x (proto filter), -a (include LISTEN/servers), -n
// (numeric — already numeric; accepted for compat). Read-only. With no proto
// flag, defaults to tcp+udp (BusyBox behavior); unix requires -x.
#include <cstdio>
#include <cfbox/args.hpp>
#include <cfbox/error.hpp>
#include <cfbox/help.hpp>
#include <cfbox/net_util.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "netstat",
.version = CFBOX_VERSION_STRING,
.one_line = "display network sockets (tcp/udp/unix, read-only)",
.usage = "netstat [-tuxan]",
.options =
" -t TCP sockets\n"
" -u UDP sockets\n"
" -x Unix domain sockets\n"
" -a show servers (LISTEN) too\n"
" -n numeric addresses (always numeric here)",
.extra = "",
};
} // namespace
auto netstat_main(int argc, char* argv[]) -> int {
using namespace cfbox;
auto parsed = args::parse(argc, argv,
{
args::OptSpec{'t', false, "tcp"},
args::OptSpec{'u', false, "udp"},
args::OptSpec{'x', false, "unix"},
args::OptSpec{'a', false, "all"},
args::OptSpec{'n', false, "numeric"},
});
if (parsed.has_long("help")) {
help::print_help(HELP);
return 0;
}
if (parsed.has_long("version")) {
help::print_version(HELP);
return 0;
}
// No proto flag → tcp+udp (BusyBox default). Any flag narrows to the union requested.
const bool any_proto = parsed.has('t') || parsed.has('u') || parsed.has('x');
const bool want_tcp = parsed.has('t') || !any_proto;
const bool want_udp = parsed.has('u') || !any_proto;
const bool want_unix = parsed.has('x');
const bool servers = parsed.has('a');
if (want_tcp) {
auto socks = net::read_tcp_sockets();
if (!socks) {
CFBOX_ERR("netstat", "%s", socks.error().msg.c_str());
return 1;
}
std::fputs(net::format_netstat_inet(*socks, servers).c_str(), stdout);
}
if (want_udp) {
auto socks = net::read_udp_sockets();
if (!socks) {
CFBOX_ERR("netstat", "%s", socks.error().msg.c_str());
return 1;
}
std::fputs(net::format_netstat_inet(*socks, servers).c_str(), stdout);
}
if (want_unix) {
auto socks = net::read_unix_sockets();
if (!socks) {
CFBOX_ERR("netstat", "%s", socks.error().msg.c_str());
return 1;
}
std::fputs(net::format_netstat_unix(*socks).c_str(), stdout);
}
return 0;
}