29{
30 size_t count = 0;
31 for (size_t i = 0; i < info.size(); i++) {
32 info[i].setState(0);
33 info[i].setTXQueue(0);
34 info[i].setRXQueue(0);
35 }
36 count = 0;
37 std::string line;
38 std::stringstream ss;
39 std::string sl, local_address, rem_address, st, queue;
40 std::ifstream fin("/proc/net/tcp");
41 getline(fin, line);
42 while (fin && getline(fin, line)) {
43 ss.str("");
44 ss << line;
45 ss >> sl >> local_address >> rem_address >> st >> queue;
46 unsigned int addr = strtoul(local_address.substr(0, 8).c_str(), NULL, 16);
47 int port = strtoul(local_address.substr(9).c_str(), NULL, 16);
48 unsigned int raddr = strtoul(rem_address.substr(0, 8).c_str(), NULL, 16);
49 int rport = strtoul(rem_address.substr(9).c_str(), NULL, 16);
50 for (size_t i = 0; i < info.size(); i++) {
51 if (info[i].getState() > 0) continue;
52 int sti = strtoul(st.c_str(), NULL, 16);
53 if (
54 port == info[i].getLocalPort() && sti == 1) {
55 info[i].setRemoteAddress(raddr);
56 info[i].setRemotePort(rport);
57 info[i].setState(sti);
58 info[i].setTXQueue(strtoul(queue.substr(0, 8).c_str(), NULL, 16));
59 info[i].setRXQueue(strtoul(queue.substr(9).c_str(), NULL, 16));
60 count++;
61 } else if (raddr == info[i].getRemoteAddress() &&
62 rport == info[i].getRemotePort() && sti == 1) {
63 info[i].setLocalAddress(addr);
64 info[i].setLocalPort(port);
65 info[i].setState(sti);
66 info[i].setTXQueue(strtoul(queue.substr(0, 8).c_str(), NULL, 16));
67 info[i].setRXQueue(strtoul(queue.substr(9).c_str(), NULL, 16));
68 count++;
69 }
70 if (count == info.size()) return count;
71 }
72 }
73 return count;
74}