summaryrefslogtreecommitdiff
path: root/Userland/Utilities/netstat.cpp
blob: c174cad2f28c78ea8ac341f397309e188eb19de6 (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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright (c) 2021, Brandon Pruitt <brapru@pm.me>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/HashMap.h>
#include <AK/IPv4Address.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/QuickSort.h>
#include <AK/String.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibCore/ProcessStatisticsReader.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <arpa/inet.h>
#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"));

    bool flag_all = false;
    bool flag_list = false;
    bool flag_tcp = false;
    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");
    args_parser.add_option(flag_all, "Display both listening and non-listening sockets", "all", 'a');
    args_parser.add_option(flag_list, "Display only listening sockets", "list", 'l');
    args_parser.add_option(flag_tcp, "Display only TCP network connections", "tcp", 't');
    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);

    TRY(Core::System::unveil("/proc/net", "r"));
    TRY(Core::System::unveil("/proc/all", "r"));
    TRY(Core::System::unveil("/etc/passwd", "r"));
    TRY(Core::System::unveil("/etc/services", "r"));
    TRY(Core::System::unveil("/tmp/portal/lookup", "rw"));
    TRY(Core::System::unveil(nullptr, nullptr));

    bool has_protocol_flag = (flag_tcp || flag_udp);

    uid_t current_uid = getuid();

    HashMap<pid_t, String> programs;

    if (flag_program) {
        auto processes = Core::ProcessStatisticsReader::get_all();
        if (!processes.has_value())
            return 1;

        for (auto& proc : processes.value().processes) {
            programs.set(proc.pid, proc.name);
        }
    }

    enum class Alignment {
        Left,
        Right
    };

    struct Column {
        String title;
        Alignment alignment { Alignment::Left };
        int width { 0 };
        String buffer;
    };

    Vector<Column> columns;

    int protocol_column = -1;
    int bytes_in_column = -1;
    int bytes_out_column = -1;
    int local_address_column = -1;
    int peer_address_column = -1;
    int state_column = -1;
    int program_column = -1;

    auto add_column = [&](auto title, auto alignment, auto width) {
        columns.append({ title, alignment, width, {} });
        return columns.size() - 1;
    };

    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, 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;

    auto print_column = [](auto& column, auto& string) {
        if (!column.width) {
            out("{}", string);
            return;
        }
        if (column.alignment == Alignment::Right) {
            out("{:>{1}}  "sv, string, column.width);
        } else {
            out("{:<{1}}  "sv, string, column.width);
        }
    };

    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("-");

        auto program = programs.get(pid);
        return String::formatted("{}/{}", pid, program.value());
    };

    if (!has_protocol_flag || flag_tcp || flag_udp) {
        if (flag_program && current_uid != 0) {
            outln("(Some processes could not be identified, non-owned process info will not be shown)");
        }

        out("Active Internet connections ");

        if (flag_all) {
            outln("(servers and established)");
        } else {
            if (flag_list)
                outln("(only servers)");
            else
                outln("(without servers)");
        }

        for (auto& column : columns)
            print_column(column, column.title);
        outln();
    }

    if (!has_protocol_flag || flag_tcp) {
        auto file = Core::File::construct("/proc/net/tcp");
        if (!file->open(Core::OpenMode::ReadOnly)) {
            warnln("Error: {}", file->error_string());
            return 1;
        }

        auto file_contents = file->read_all();
        auto json_or_error = JsonValue::from_string(file_contents);
        if (json_or_error.is_error()) {
            warnln("Error: {}", json_or_error.error());
            return 1;
        }
        auto json = json_or_error.release_value();

        Vector<JsonValue> sorted_regions = json.as_array().values();
        quick_sort(sorted_regions, [](auto& a, auto& b) {
            return a.as_object().get("local_port"sv).to_u32() < b.as_object().get("local_port"sv).to_u32();
        });

        for (auto& value : sorted_regions) {
            auto& if_object = value.as_object();

            auto bytes_in = if_object.get("bytes_in"sv).to_string();
            auto bytes_out = if_object.get("bytes_out"sv).to_string();

            auto peer_address = if_object.get("peer_address"sv).to_string();
            if (!flag_numeric) {
                auto from_string = IPv4Address::from_string(peer_address);
                auto addr = from_string.value().to_in_addr_t();
                auto* hostent = gethostbyaddr(&addr, sizeof(in_addr), AF_INET);
                if (hostent != nullptr) {
                    auto host_name = StringView { hostent->h_name, strlen(hostent->h_name) };
                    if (!host_name.is_empty())
                        peer_address = host_name;
                }
            }

            auto peer_port = if_object.get("peer_port"sv).to_string();
            if (!flag_numeric) {
                auto service = getservbyport(htons(if_object.get("peer_port"sv).to_u32()), "tcp");
                if (service != nullptr) {
                    auto s_name = StringView { service->s_name, strlen(service->s_name) };
                    if (!s_name.is_empty())
                        peer_port = s_name;
                }
            }

            auto local_address = if_object.get("local_address"sv).to_string();
            if (!flag_numeric) {
                auto from_string = IPv4Address::from_string(local_address);
                auto addr = from_string.value().to_in_addr_t();
                auto* hostent = gethostbyaddr(&addr, sizeof(in_addr), AF_INET);
                if (hostent != nullptr) {
                    auto host_name = StringView { hostent->h_name, strlen(hostent->h_name) };
                    if (!host_name.is_empty())
                        local_address = host_name;
                }
            }

            auto local_port = if_object.get("local_port"sv).to_string();
            if (!flag_numeric) {
                auto service = getservbyport(htons(if_object.get("local_port"sv).to_u32()), "tcp");
                if (service != nullptr) {
                    auto s_name = StringView { service->s_name, strlen(service->s_name) };
                    if (!s_name.is_empty())
                        local_port = s_name;
                }
            }

            auto state = if_object.get("state"sv).to_string();
            auto origin_pid = (if_object.has("origin_pid"sv)) ? if_object.get("origin_pid"sv).to_u32() : -1;

            if (!flag_all && ((state == "Listen" && !flag_list) || (state != "Listen" && flag_list)))
                continue;

            if (protocol_column != -1)
                columns[protocol_column].buffer = "tcp";
            if (bytes_in_column != -1)
                columns[bytes_in_column].buffer = bytes_in;
            if (bytes_out_column != -1)
                columns[bytes_out_column].buffer = bytes_out;
            if (local_address_column != -1)
                columns[local_address_column].buffer = get_formatted_address(local_address, local_port);
            if (peer_address_column != -1)
                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)
                columns[program_column].buffer = get_formatted_program(origin_pid);

            for (auto& column : columns)
                print_column(column, column.buffer);
            outln();
        };
    }

    if (!has_protocol_flag || flag_udp) {
        auto file = TRY(Core::File::open("/proc/net/udp", Core::OpenMode::ReadOnly));
        auto file_contents = file->read_all();
        auto json = TRY(JsonValue::from_string(file_contents));

        Vector<JsonValue> sorted_regions = json.as_array().values();
        quick_sort(sorted_regions, [](auto& a, auto& b) {
            return a.as_object().get("local_port"sv).to_u32() < b.as_object().get("local_port"sv).to_u32();
        });

        for (auto& value : sorted_regions) {
            auto& if_object = value.as_object();

            auto local_address = if_object.get("local_address"sv).to_string();
            if (!flag_numeric) {
                auto from_string = IPv4Address::from_string(local_address);
                auto addr = from_string.value().to_in_addr_t();
                auto* hostent = gethostbyaddr(&addr, sizeof(in_addr), AF_INET);
                if (hostent != nullptr) {
                    auto host_name = StringView { hostent->h_name, strlen(hostent->h_name) };
                    if (!host_name.is_empty())
                        local_address = host_name;
                }
            }

            auto local_port = if_object.get("local_port"sv).to_string();
            if (!flag_numeric) {
                auto service = getservbyport(htons(if_object.get("local_port"sv).to_u32()), "udp");
                if (service != nullptr) {
                    auto s_name = StringView { service->s_name, strlen(service->s_name) };
                    if (!s_name.is_empty())
                        local_port = s_name;
                }
            }

            auto peer_address = if_object.get("peer_address"sv).to_string();
            if (!flag_numeric) {
                auto from_string = IPv4Address::from_string(peer_address);
                auto addr = from_string.value().to_in_addr_t();
                auto* hostent = gethostbyaddr(&addr, sizeof(in_addr), AF_INET);
                if (hostent != nullptr) {
                    auto host_name = StringView { hostent->h_name, strlen(hostent->h_name) };
                    if (!host_name.is_empty())
                        peer_address = host_name;
                }
            }

            auto peer_port = if_object.get("peer_port"sv).to_string();
            if (!flag_numeric) {
                auto service = getservbyport(htons(if_object.get("peer_port"sv).to_u32()), "udp");
                if (service != nullptr) {
                    auto s_name = StringView { service->s_name, strlen(service->s_name) };
                    if (!s_name.is_empty())
                        peer_port = s_name;
                }
            }

            auto origin_pid = (if_object.has("origin_pid"sv)) ? if_object.get("origin_pid"sv).to_u32() : -1;

            if (protocol_column != -1)
                columns[protocol_column].buffer = "udp";
            if (bytes_in_column != -1)
                columns[bytes_in_column].buffer = "-";
            if (bytes_out_column != -1)
                columns[bytes_out_column].buffer = "-";
            if (local_address_column != -1)
                columns[local_address_column].buffer = get_formatted_address(local_address, local_port);
            if (peer_address_column != -1)
                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)
                columns[program_column].buffer = get_formatted_program(origin_pid);

            for (auto& column : columns)
                print_column(column, column.buffer);
            outln();
        };
    }

    return 0;
}