fork download
  1. #include<iostream>
  2. #include<queue>
  3. #include<string>
  4. #include<fstream>
  5.  
  6. std::string getProtocolOf(const char * frame){
  7.  
  8. enum Transport {
  9. TCP = 6,
  10. UDP = 17
  11. };
  12.  
  13. enum FrameType {
  14. IP = 0x0800,
  15. IP6 = 0x86dd
  16. };
  17.  
  18. typedef unsigned char uchar;
  19.  
  20. const char* ipVerString;
  21. const char* tpProtoString;
  22.  
  23. const char* p;
  24. const char* pIP = frame + 14;
  25.  
  26. p = frame + (6 + 6); // dst + src
  27.  
  28. // AF in Mac
  29. switch( (unsigned short)( (*p) << 8 | *(p+1) ) ){
  30. default: return "not IP";
  31. case IP: break;
  32. case IP6: break;
  33. }
  34.  
  35. p = pIP;
  36.  
  37. // Ver in IP
  38. switch( (uchar)( ( *p >> 4 ) & 0x0f ) ){
  39. default: return "IP? Unknown";
  40. case 0x4: p = pIP + 9; ipVerString = "IPv4"; break;
  41. case 0x6: p = pIP + 6; ipVerString = "IPv6"; break;
  42. }
  43.  
  44. // Proto in IP
  45. switch( (uchar)*p ){
  46. default: tpProtoString = "not TCP/UDP"; break;
  47. case TCP: tpProtoString = "TCP"; break;
  48. case UDP: tpProtoString = "UDP "; break;
  49. }
  50.  
  51. return std::string(ipVerString) + " " + tpProtoString;
  52. }
  53.  
  54. const int kSizeRead = 32;
  55.  
  56. int main ( int argc, char** argv ){
  57.  
  58. std::queue<std::string> queue;
  59.  
  60. // push to queue
  61. for( int i = 1; i < argc; i++ ){
  62. char frame[kSizeRead];
  63. std::ifstream(argv[i]).read(frame,sizeof(frame));
  64. queue.push( getProtocolOf(frame) );
  65. }
  66.  
  67. // extract
  68. for( ; !queue.empty(); queue.pop() )
  69. std::cout << queue.front() << std::endl;
  70.  
  71. return 0;
  72. }
  73.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Standard output is empty