Belle II Software  release-05-01-25
IOInfo.cc
1 #include "daq/slc/readout/IOInfo.h"
2 
3 #include <cstdlib>
4 #include <cstdio>
5 
6 #include <fstream>
7 #include <sstream>
8 #include <arpa/inet.h>
9 
10 using namespace Belle2;
11 
12 int IOInfo::checkTCP(IOInfo& info)
13 {
14  std::vector<IOInfo> info_v;
15  info_v.push_back(info);
16  int ret = checkTCP(info_v);
17  info = info_v[0];
18  return ret;
19 }
20 
21 int IOInfo::checkTCP(std::vector<IOInfo>& info)
22 {
23  size_t count = 0;
24  for (size_t i = 0; i < info.size(); i++) {
25  info[i].setState(0);
26  info[i].setTXQueue(0);
27  info[i].setRXQueue(0);
28  }
29  count = 0;
30  std::string line;
31  std::stringstream ss;
32  std::string sl, local_address, rem_address, st, queue;
33  std::ifstream fin("/proc/net/tcp");
34  getline(fin, line);
35  while (fin && getline(fin, line)) {
36  ss.str("");
37  ss << line;
38  ss >> sl >> local_address >> rem_address >> st >> queue;
39  unsigned int addr = strtoul(local_address.substr(0, 8).c_str(), NULL, 16);
40  int port = strtoul(local_address.substr(9).c_str(), NULL, 16);
41  unsigned int raddr = strtoul(rem_address.substr(0, 8).c_str(), NULL, 16);
42  int rport = strtoul(rem_address.substr(9).c_str(), NULL, 16);
43  for (size_t i = 0; i < info.size(); i++) {
44  if (info[i].getState() > 0) continue;
45  int sti = strtoul(st.substr(0).c_str(), NULL, 16);
46  if (//addr == info[i].getLocalAddress() &&
47  port == info[i].getLocalPort() && sti == 1) {
48  info[i].setRemoteAddress(raddr);
49  info[i].setRemotePort(rport);
50  info[i].setState(sti);
51  info[i].setTXQueue(strtoul(queue.substr(0, 8).c_str(), NULL, 16));
52  info[i].setRXQueue(strtoul(queue.substr(9).c_str(), NULL, 16));
53  count++;
54  } else if (raddr == info[i].getRemoteAddress() &&
55  rport == info[i].getRemotePort() && sti == 1) {
56  info[i].setLocalAddress(addr);
57  info[i].setLocalPort(port);
58  info[i].setState(sti);
59  info[i].setTXQueue(strtoul(queue.substr(0, 8).c_str(), NULL, 16));
60  info[i].setRXQueue(strtoul(queue.substr(9).c_str(), NULL, 16));
61  count++;
62  }
63  if (count == info.size()) return count;
64  }
65  }
66  return count;
67 }
68 
69 const char* IOInfo::getLocalIP() const
70 {
71  sockaddr_in addr;
72  addr.sin_addr.s_addr = m_local_addr;
73  return inet_ntoa(addr.sin_addr);
74 }
75 
76 const char* IOInfo::getRemoteIP() const
77 {
78  sockaddr_in addr;
79  addr.sin_addr.s_addr = m_remote_addr;
80  return inet_ntoa(addr.sin_addr);
81 }
Belle2::IOInfo
Definition: IOInfo.h:12
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19