-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
112 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright 2020-present Daniel Trugman | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <regex> | ||
|
||
#include "format.hpp" | ||
#include "log.hpp" | ||
#include "tool.hpp" | ||
|
||
void usage() | ||
{ | ||
LOG("Usage: [type] [task-id]"); | ||
LOG(""); | ||
LOG(" type Socket type: tcp, udp, unix"); | ||
LOG(" task-id The ID of the task to enumerate"); | ||
LOG(""); | ||
} | ||
|
||
int tool_netstat(std::vector<std::string>&& args) | ||
{ | ||
if (args.size() != 2) | ||
{ | ||
usage(); | ||
return 1; | ||
} | ||
|
||
try | ||
{ | ||
LOG("========================================================="); | ||
LOG("netstat"); | ||
LOG("========================================================="); | ||
|
||
static const std::string TCP("tcp"); | ||
static const std::string UDP("udp"); | ||
|
||
std::string type(args[0]); | ||
auto id = std::stoi(args[1]); | ||
|
||
pfs::procfs pfs; | ||
auto task = pfs.get_task(id); | ||
|
||
// Get all inodes for the given task | ||
std::set<ino_t> inodes; | ||
auto fds = task.get_fds(); | ||
for (auto& fd : fds) | ||
{ | ||
inodes.insert(fd.second.get_target_stat().st_ino); | ||
} | ||
|
||
auto net = task.get_net(); | ||
std::vector<pfs::net_socket> sockets; | ||
if (type == TCP) | ||
{ | ||
sockets = net.get_tcp(); | ||
} | ||
else if (type == UDP) | ||
{ | ||
sockets = net.get_udp(); | ||
} | ||
else | ||
{ | ||
usage(); | ||
return 1; | ||
} | ||
|
||
std::vector<pfs::net_socket> task_sockets; | ||
for (auto& socket : sockets) | ||
{ | ||
if (inodes.find(socket.inode) != inodes.end()) | ||
{ | ||
task_sockets.push_back(socket); | ||
} | ||
} | ||
|
||
print(task_sockets); | ||
} | ||
catch (const std::runtime_error& ex) | ||
{ | ||
LOG("Error when printing netstat:"); | ||
LOG(TAB << ex.what()); | ||
} | ||
|
||
return 0; | ||
} |