| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #include <stdio.h>
- #include <stdint.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <regex>
- #include <string>
- #include <sstream>
- #include <iostream>
- #include "util.h"
- #include "gfanet.h"
- /////////////////////////////////////////////////////////////////////////////
- #define _PROC_NET_DEV "/proc/net/dev"
- /////////////////////////////////////////////////////////////////////////////
- int GetAvailableInterfaces(NETINTERFACE_LIST &nil)
- {
- static const std::string strRegExLine = "^\\s*([^:]+):\\s*([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)$";
- static std::regex regl(strRegExLine, std::regex_constants::ECMAScript | std::regex_constants::optimize);
- std::string buf;
- int nCntItf = 0;
- if(ReadFile(_PROC_NET_DEV, buf) > 0)
- {
- std::string line;
- std::istringstream iss(buf);
- std::istream stm(iss.rdbuf());
- while(std::getline(stm, line))
- {
- std::smatch resl;
- if( line.size() > 0 &&
- regex_search(line, resl, regl))
- {
- if(resl.size() == 18)
- {
- NETINTERFACE ni;
- memset(&ni, 0, sizeof(ni));
- resl[1].str().copy(ni.name, sizeof(ni.name) - 1);
- ni.stats.rx_bytes = std::stoul(resl[2].str());
- ni.stats.rx_packets = std::stoul(resl[3].str());
- ni.stats.rx_errors = std::stoul(resl[4].str());
- ni.stats.rx_dropped = std::stoul(resl[5].str());
- ni.stats.rx_fifo_errors = std::stoul(resl[6].str());
- ni.stats.rx_frame_errors = std::stoul(resl[7].str());
- ni.stats.rx_compressed = std::stoul(resl[8].str());
- ni.stats.rx_multicast = std::stoul(resl[9].str());
- ni.stats.tx_bytes = std::stoul(resl[10].str());
- ni.stats.tx_packets = std::stoul(resl[11].str());
- ni.stats.tx_errors = std::stoul(resl[12].str());
- ni.stats.tx_dropped = std::stoul(resl[13].str());
- ni.stats.tx_fifo_errors = std::stoul(resl[14].str());
- ni.stats.tx_collisions = std::stoul(resl[15].str());
- ni.stats.tx_carrier_errors = std::stoul(resl[16].str());
- ni.stats.tx_compressed = std::stoul(resl[17].str());
- nil.push_back(ni);
- nCntItf++;
- }
- }
- }
- }
- return nCntItf;
- }
|