-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollector.cpp
More file actions
184 lines (148 loc) · 4.1 KB
/
collector.cpp
File metadata and controls
184 lines (148 loc) · 4.1 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
#include "collector.h"
#include "slices.h"
#include "proc_net_parser.h"
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/errno.h>
#include <cstring>
bool readFile(const std::string& name, std::string* const contents) {
FILE* f = fopen(name.c_str(), "r");
if (!f) return false;
char buf[1024];
contents->reserve(sizeof(buf));
bool ok = true;
for (;;) {
int bytes_read = fread(buf, 1, sizeof(buf), f);
if (bytes_read > 0)
contents->append(buf, bytes_read);
else if (bytes_read == 0)
break;
else { // < 0
ok = false;
break;
}
}
fclose(f);
if (!ok)
contents->clear();
return ok;
}
std::set<int> getSocketInodes(const std::string& fd_path) {
std::set<int> i;
DIR* dir = opendir(fd_path.c_str());
if (!dir)
return std::set<int>();
while (struct dirent* dent = readdir(dir)) {
if (!dent && errno != 0) {
closedir(dir);
return std::set<int>();
}
if (dent->d_type != DT_LNK)
continue;
char buf[256] = {0};
ssize_t ssz = readlink((std::string(fd_path) + "/" + dent->d_name).c_str(), buf, sizeof(buf));
if (ssz <= 0)
continue;
int inode = 0;
if (sscanf(buf, "socket:[%d]", &inode) != 1)
continue;
i.insert(inode);
}
closedir(dir);
return i;
}
bool getSystemInterfaces(Slice::Interfaces* const ifaces) {
struct ifaddrs *ifaddr, *ifa;
int n;
if (getifaddrs(&ifaddr) == -1)
return false;
ifaces->clear();
for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
if (ifa->ifa_addr == NULL)
continue;
if (ifa->ifa_addr->sa_family == AF_INET) {
const char* ifname = ifa->ifa_name;
Carrier car = CARR_UNKNOWN;
if (strstr(ifname, "eth"))
car = CARR_ETHER;
else if (strstr(ifname, "lo"))
car = CARR_LOCAL;
else if (strstr(ifname, "wlan"))
car = CARR_WIFI;
else if (strstr(ifname, "rmnet"))
car = CARR_MOBILE;
const in_addr_t saddr = ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
ifaces->insert(std::make_pair(saddr, car));
}
}
freeifaddrs(ifaddr);
return true;
}
void collectSlice(Slice* const s) {
DIR* proc = opendir("/proc");
if (!proc)
return;
struct dirent* dent;
while ((dent = readdir(proc))) {
if (!dent && errno != 0) {
closedir(proc);
return;
}
if (dent->d_type != DT_DIR)
continue; // not a directory
Slice::App app;
if (sscanf(dent->d_name, "%d", &app.pid) != 1)
continue; // not a PID number
std::string base_dir = std::string("/proc/") + dent->d_name + "/";
std::string app_name;
if (!readFile(base_dir + "cmdline", &app_name))
continue;
//printf("app %s:\n", app_name.c_str());
std::string net_tcp4, net_tcp6; // TODO handle UDP also
if (!readFile(base_dir + "net/tcp", &net_tcp4))
continue;
if (!readFile(base_dir + "net/tcp6", &net_tcp6))
continue;
const std::set<int> inodes = getSocketInodes(base_dir + "fd");
// printf(" %d inodes: \n", (int)inodes.size());
//for (std::set<int>::const_iterator ino = inodes.begin(); ino != inodes.end(); ++ino)
// printf("%d ", *ino);
//putchar('\n');
ProcNetList4 conn_list4, conn_list4_as_6;
ProcNetList6 conn_list6;
parseProcNets4(net_tcp4, inodes, &conn_list4);
parseProcNets6(net_tcp6, inodes, &conn_list4_as_6, &conn_list6);
conn_list4.insert(conn_list4.end(), conn_list4_as_6.begin(), conn_list4_as_6.end());
// TODO use IPv6 list also
//printf(" %d conns\n", (int)conn_list.size());
for (ProcNetList4::const_iterator it = conn_list4.begin();
it != conn_list4.end(); ++it)
{
if (it->remote_port != 0 && it->remote_addr != 0) {
Slice::App::Connection c;
c.ip_local = it->local_addr;
c.port_local = it->local_port;
c.ip_remote = it->remote_addr;
c.port_remote = it->remote_port;
app.connections.push_back(c);
} else {
Slice::App::Server s;
s.ip_listen = it->local_addr;
s.port_listen = it->local_port;
app.servers.push_back(s);
}
}
s->apps.insert(std::make_pair(app_name, app));
}
closedir(proc);
if (!getSystemInterfaces(&s->ifaces))
s->apps.clear();
}