μHAL (v2.6.5)
Part of the IPbus software repository
WiresharkParser.cxx
Go to the documentation of this file.
1 
2 #include "uhal/uhal.hpp"
3 #include "uhal/log/log.hpp"
4 
6 
7 #include <string>
8 #include <sstream>
9 #include <fstream>
10 
11 #include <iomanip>
12 #include <arpa/inet.h>
13 
14 
15 using namespace uhal;
16 
17 
18 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
21 {
22  private:
24  uint8_t d_MAC[6]; //0-5
26  uint8_t s_MAC[6]; //6-11
28  uint8_t junk1[14]; //12-25
30  uint8_t s_IP[4]; //26-29
32  uint8_t d_IP[4]; //30-33
34  uint8_t s_port[2]; //34-35
36  uint8_t d_port[2]; //36-37
38  uint8_t l[2]; //38-39
40  uint8_t junk2[2]; //40-41
42  uint8_t d[1458]; //to the end of the ethernet ethernet_frame
43  public:
44 
49  uint64_t source_MAC()
50  {
51  return ( uint64_t ( s_MAC[0] ) << 40 ) | ( uint64_t ( s_MAC[1] ) << 32 ) | ( uint64_t ( s_MAC[2] ) << 24 ) | ( uint64_t ( s_MAC[3] ) << 16 ) | ( uint64_t ( s_MAC[4] ) << 8 ) | ( uint64_t ( s_MAC[5] ) );
52  }
57  uint32_t source_IP()
58  {
59  return ntohl ( *reinterpret_cast<uint32_t*> ( s_IP ) );
60  }
65  uint16_t source_port()
66  {
67  return ntohs ( *reinterpret_cast<uint16_t*> ( s_port ) );
68  }
69 
74  uint64_t destination_MAC()
75  {
76  return ( uint64_t ( d_MAC[0] ) << 40 ) | ( uint64_t ( d_MAC[1] ) << 32 ) | ( uint64_t ( d_MAC[2] ) << 24 ) | ( uint64_t ( d_MAC[3] ) << 16 ) | ( uint64_t ( d_MAC[4] ) << 8 ) | ( uint64_t ( d_MAC[5] ) );
77  }
82  uint32_t destination_IP()
83  {
84  return ntohl ( *reinterpret_cast<uint32_t*> ( d_IP ) );
85  }
90  uint16_t destination_port()
91  {
92  return ntohs ( *reinterpret_cast<uint16_t*> ( d_port ) );
93  }
98  uint16_t length()
99  {
100  return ntohs ( *reinterpret_cast<uint16_t*> ( l ) );
101  }
102 
107  std::vector<uint32_t> data()
108  {
109  uint32_t lLength ( ( length()-8 ) >>2 );
110  std::vector<uint32_t> lReturn;
111 
112  for ( uint32_t i=0 ; i!=lLength ; ++i )
113  {
114  lReturn.push_back ( *reinterpret_cast<uint32_t*> ( &d[i<<2] ) );
115  }
116 
117  return lReturn;
118  }
119 };
120 
121 // --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
122 
124 int main ( int argc, char* argv[] )
125 {
126  HostToTargetInspector< 1 , 3 > lHostToTarget;
127  TargetToHostInspector< 1 , 3 > lTargetToHost;
128  std::ifstream file ( "etc/uhal/tests/wireshark.txt" );
129 
130  if ( file.is_open() )
131  {
132  std::string line;
133  uint16_t lTemp , lValue;
134  uint8_t block[1500] = { 0 };
135  uint8_t* block_ptr = block;
136 
137  while ( file.good() )
138  {
139  getline ( file,line );
140 
141  if ( line.length() )
142  {
143  log ( Info() , "Raw : " , line );
144  std::stringstream lStr ( line );
145  lStr >> std::hex >> lTemp;
146 
147  for ( uint32_t i=0 ; i!=16 ; ++i )
148  {
149  lStr >> lValue;
150  * ( block_ptr++ ) = uint8_t ( lValue );
151  }
152  }
153  else
154  {
155  if ( block_ptr != block )
156  {
157  ethernet_frame y = ethernet_frame ( * reinterpret_cast< ethernet_frame* > ( block ) );
158  log ( Notice() , "Destination : MAC = " , Integer ( y.destination_MAC(), IntFmt<hex,fixed>() )
159  , ", IP = " , Integer ( y.destination_IP(), IntFmt<hex,fixed>() )
160  , ", Port = " , Integer ( y.destination_port() )
161  );
162  log ( Notice() , " Source : MAC = " , Integer ( y.source_MAC(), IntFmt<hex,fixed>() )
163  , ", IP = " , Integer ( y.source_IP(), IntFmt<hex,fixed>() )
164  , ", Port = " , Integer ( y.source_port() )
165  );
166  std::vector<uint32_t> lData ( y.data() );
167  std::vector<uint32_t>::const_iterator lBegin ( lData.begin() );
168  std::vector<uint32_t>::const_iterator lEnd ( lData.end() );
169 
170  if ( y.destination_port() == 50001 )
171  {
172  log ( Notice() , "Host To Target" );
173  lHostToTarget.analyze ( lBegin , lEnd );
174  }
175  else
176  {
177  log ( Notice() , "Target to Host" );
178  lTargetToHost.analyze ( lBegin , lEnd );
179  }
180 
181  block_ptr = block;
182  std::cout << std::string ( 122 , '-' ) << std::endl;
183  }
184  }
185  }
186 
187  file.close();
188  }
189 }
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
bool analyze(std::vector< uint32_t >::const_iterator &aIt, const std::vector< uint32_t >::const_iterator &aEnd, const bool &aContinueOnError=true)
Analyse an IPbus packet held as a vector of uint32_t&#39;s.
uint64_t source_MAC()
Return source MAC address.
NoticeLevel Notice
Definition: LogLevels.cpp:96
uint16_t destination_port()
Return destination port.
std::vector< uint32_t > data()
Return payload.
Helper class to decode IPbus packets as passed from the Client to the Target.
uint32_t source_IP()
Return source IP address.
Helper class to decode IPbus packets as passed from the Target to the Client.
uint16_t length()
Return length.
Empty struct which acts as a dummy variable for passing the formatting information around...
uint16_t source_port()
Return source port.
uint64_t destination_MAC()
Return destination MAC address.
bool analyze(std::vector< uint32_t >::const_iterator &aIt, const std::vector< uint32_t >::const_iterator &aEnd, const bool &aContinueOnError=true)
Analyse an IPbus packet held as a vector of uint32_t&#39;s.
Hexadecimal.
Struct for storing an ethernet frame.
int main(int argc, char *argv[])
Parse an IPbus 1.3 packet from a wireshark text file.
uint32_t destination_IP()
Return destination IP address.
InfoLevel Info
Definition: LogLevels.cpp:114
_Integer< T, IntFmt<> > Integer(const T &aT)
Forward declare a function which creates an instance of the ultra-lightweight wrapper from an integer...