SHARE
TWEET

Untitled

a guest May 25th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cstdlib>
  4. #include<sstream>
  5.  
  6. using namespace std;
  7.  
  8. //struct: Property of a tcp connection
  9. struct ConnectionProperty
  10. {
  11.     string local_addr;
  12.     string remote_addr;
  13.     string state;
  14. };
  15.  
  16. //Change hexadecimal number to decimal number
  17. int HexToInt(char h)
  18. {
  19.     if(h >= '0' && h <= '9')
  20.     {
  21.         return h - '0';
  22.     }
  23.     else if(h >= 'A' && h <= 'F')
  24.     {
  25.         return h - 'A' + 10;
  26.     }
  27.     else
  28.     {
  29.         cerr << "Error: Illegal Hex Number!" << endl;
  30.         return -1;
  31.     }
  32. }
  33.  
  34. //Get Ip address and port number from string XXXXXXXX:XXXX
  35. string GetIpAddress(char* str)
  36. {
  37.     int a, b, c, d, e;
  38.    
  39.     a = HexToInt(str[0]) * 16 + HexToInt(str[1]);
  40.     b = HexToInt(str[2]) * 16 + HexToInt(str[3]);
  41.     c = HexToInt(str[4]) * 16 + HexToInt(str[5]);
  42.     d = HexToInt(str[6]) * 16 + HexToInt(str[7]);
  43.     e = HexToInt(str[9]) * 16 * 16 * 16 +
  44.         HexToInt(str[10]) * 16 * 16 +
  45.         HexToInt(str[11]) * 16 +
  46.         HexToInt(str[12]);
  47.  
  48.     //change int to string
  49.     string sa, sb, sc, sd, se;
  50.     ostringstream oss;
  51.     oss << a;
  52.     sa = oss.str();
  53.     oss.str(""); //clear the content in oss
  54.     oss << b;
  55.     sb = oss.str();
  56.     oss.str("");
  57.     oss << c;
  58.     sc = oss.str();
  59.     oss.str("");
  60.     oss << d;
  61.     sd = oss.str();
  62.     oss.str("");
  63.     oss << e;
  64.     se = oss.str();
  65.     oss.str("");
  66.  
  67.     //return by order: d.c.b.a:e
  68.     return sd + '.' + sc + '.' + sb + '.' + sa + ':' + se;
  69. }
  70.  
  71. //Get tcp connection state
  72. string GetConnectionState(char* str)
  73. {
  74.  
  75.     if(str[0] == '0' && str[1] == '0') return "ERROR_STATUS";
  76.     if(str[0] == '0' && str[1] == '1') return "TCP_ESTABLISHED";
  77.     if(str[0] == '0' && str[1] == '2') return "TCP_SYN_SENT";
  78.     if(str[0] == '0' && str[1] == '3') return "TCP_SYN_RECV";
  79.     if(str[0] == '0' && str[1] == '4') return "TCP_FIN_WAIT1";
  80.     if(str[0] == '0' && str[1] == '5') return "TCP_FIN_WAIT2";
  81.     if(str[0] == '0' && str[1] == '6') return "TCP_TIME_WAIT";
  82.     if(str[0] == '0' && str[1] == '7') return "TCP_CLOSE";
  83.     if(str[0] == '0' && str[1] == '8') return "TCP_CLOSE_WAIT";
  84.     if(str[0] == '0' && str[1] == '9') return "TCP_LAST_ACK";
  85.     if(str[0] == '0' && str[1] == 'A') return "TCP_LISTEN";
  86.     if(str[0] == '0' && str[1] == 'B') return "TCP_CLOSING";
  87.     return "UNKNOWN_STATE";
  88. }
  89.  
  90. int main()
  91. {
  92.     //read from file /proc/net/tcp
  93.     ifstream infile("/proc/net/tcp", ios :: in);
  94.     if(!infile)
  95.     {
  96.         cerr << "open error!" << endl;
  97.         exit(1);
  98.     }
  99.  
  100.     string s;
  101.     char s_local_addr[20], s_remote_addr[20], s_state[20];
  102.     getline(infile, s); //title: every column's name
  103.     while(getline(infile, s))
  104.     {    
  105.         sscanf(s.c_str(), "%*s%s%s%s", s_local_addr, s_remote_addr, s_state);
  106.         printf("%s\t%s\t%s\n", s_local_addr, s_remote_addr, s_state);
  107.  
  108.         string ip_local = GetIpAddress(s_local_addr);
  109.         string ip_remote = GetIpAddress(s_remote_addr);
  110.         string conn_state = GetConnectionState(s_state);
  111.  
  112.         cout << ip_local << "\t" << ip_remote << "\t" << conn_state << endl;
  113.     }
  114.  
  115.     return 0;
  116. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top