summaryrefslogtreecommitdiff
path: root/Userland/Utilities/netstat.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Utilities/netstat.cpp')
-rw-r--r--Userland/Utilities/netstat.cpp28
1 files changed, 20 insertions, 8 deletions
diff --git a/Userland/Utilities/netstat.cpp b/Userland/Utilities/netstat.cpp
index 7af16099ac..778b503cc2 100644
--- a/Userland/Utilities/netstat.cpp
+++ b/Userland/Utilities/netstat.cpp
@@ -19,6 +19,8 @@
#include <netdb.h>
#include <unistd.h>
+constexpr int max_formatted_address_length = 21;
+
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath unix"));
@@ -35,6 +37,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
bool flag_udp = false;
bool flag_numeric = false;
bool flag_program = false;
+ bool flag_wide = false;
Core::ArgsParser args_parser;
args_parser.set_general_help("Display network connections");
@@ -44,6 +47,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.add_option(flag_udp, "Display only UDP network connections", "udp", 'u');
args_parser.add_option(flag_numeric, "Display numerical addresses", "numeric", 'n');
args_parser.add_option(flag_program, "Show the PID and name of the program to which each socket belongs", "program", 'p');
+ args_parser.add_option(flag_wide, "Do not truncate IP addresses by printing out the whole symbolic host", "wide", 'W');
args_parser.parse(arguments);
bool has_protocol_flag = (flag_tcp || flag_udp);
@@ -92,8 +96,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
protocol_column = add_column("Proto", Alignment::Left, 5);
bytes_in_column = add_column("Bytes-In", Alignment::Right, 9);
bytes_out_column = add_column("Bytes-Out", Alignment::Right, 9);
- local_address_column = add_column("Local Address", Alignment::Left, 20);
- peer_address_column = add_column("Peer Address", Alignment::Left, 20);
+ local_address_column = add_column("Local Address", Alignment::Left, 22);
+ peer_address_column = add_column("Peer Address", Alignment::Left, 22);
state_column = add_column("State", Alignment::Left, 11);
program_column = flag_program ? add_column("PID/Program", Alignment::Left, 11) : -1;
@@ -109,6 +113,16 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
}
};
+ auto get_formatted_address = [&](String const& address, String const& port) {
+ if (flag_wide)
+ return String::formatted("{}:{}", address, port);
+
+ if ((address.length() + port.length()) <= max_formatted_address_length)
+ return String::formatted("{}:{}", address, port);
+
+ return String::formatted("{}:{}", address.substring_view(0, max_formatted_address_length - port.length()), port);
+ };
+
auto get_formatted_program = [&](pid_t pid) {
if (pid == -1)
return String("-");
@@ -185,7 +199,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
peer_port = s_name;
}
}
- auto formatted_peer_address = String::formatted("{}:{}", peer_address, peer_port);
auto local_address = if_object.get("local_address").to_string();
if (!flag_numeric) {
@@ -208,7 +221,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
local_port = s_name;
}
}
- auto formatted_local_address = String::formatted("{}:{}", local_address, local_port);
auto state = if_object.get("state").to_string();
auto origin_pid = (if_object.has("origin_pid")) ? if_object.get("origin_pid").to_u32() : -1;
@@ -223,9 +235,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (bytes_out_column != -1)
columns[bytes_out_column].buffer = bytes_out;
if (local_address_column != -1)
- columns[local_address_column].buffer = formatted_local_address;
+ columns[local_address_column].buffer = get_formatted_address(local_address, local_port);
if (peer_address_column != -1)
- columns[peer_address_column].buffer = formatted_peer_address;
+ columns[peer_address_column].buffer = get_formatted_address(peer_address, peer_port);
if (state_column != -1)
columns[state_column].buffer = state;
if (flag_program && program_column != -1)
@@ -303,9 +315,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (bytes_out_column != -1)
columns[bytes_out_column].buffer = "-";
if (local_address_column != -1)
- columns[local_address_column].buffer = String::formatted("{}:{}", local_address, local_port);
+ columns[local_address_column].buffer = get_formatted_address(local_address, local_port);
if (peer_address_column != -1)
- columns[peer_address_column].buffer = String::formatted("{}:{}", peer_address, peer_port);
+ columns[peer_address_column].buffer = get_formatted_address(peer_address, peer_port);
if (state_column != -1)
columns[state_column].buffer = "-";
if (flag_program && program_column != -1)