OPAL  Version 3.14.3
pcapfile.h
Go to the documentation of this file.
1 /*
2  * pcapfile.h
3  *
4  * Ethernet capture (PCAP) file declaration
5  *
6  * Portable Tools Library
7  *
8  * Copyright (C) 2011 Vox Lucida Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Tools Library.
21  *
22  * The Initial Developer of the Original Code is Vox Lucida
23  *
24  * All Rights Reserved.
25  *
26  * Contributor(s): ______________________________________.
27  *
28  * $Revision: 32352 $
29  * $Author: rjongbloed $
30  * $Date: 2014-07-14 17:35:39 +1000 (Mon, 14 Jul 2014) $
31  */
32 
33 #ifndef PTLIB_PCAPFILE_H
34 #define PTLIB_PCAPFILE_H
35 
36 #ifdef P_USE_PRAGMA
37 #pragma interface
38 #endif
39 
40 #include <rtp/rtp.h>
41 #include <opal/mediafmt.h>
42 #include <ptlib/sockets.h>
43 
44 
47 class OpalPCAPFile : public PFile
48 {
49  PCLASSINFO(OpalPCAPFile, PFile);
50  public:
51  OpalPCAPFile();
52 
53  bool Open(const PFilePath & filename, OpenMode mode = ReadOnly);
54  bool Restart();
55 
56  void PrintOn(ostream & strm) const;
57 
58  bool WriteFrame(const PEthSocket::Frame & frame);
59 
60  int GetDataLink(PBYTEArray & payload);
61  int GetIP(PBYTEArray & payload);
62  int GetTCP(PBYTEArray & payload);
63  int GetUDP(PBYTEArray & payload);
64  int GetRTP(RTP_DataFrame & rtp);
65 
66  const PTime & GetPacketTime() const { return m_rawPacket.GetTimestamp(); }
67  const PIPSocket::Address & GetSrcIP() const { return m_packetSrc.GetAddress(); }
68  const PIPSocket::Address & GetDstIP() const { return m_packetDst.GetAddress(); }
69  unsigned IsFragmentated() const { return m_rawPacket.IsFragmentated(); }
70  WORD GetSrcPort() const { return m_packetSrc.GetPort(); }
71  WORD GetDstPort() const { return m_packetDst.GetPort(); }
72 
74  const PIPSocket::Address & ip
75  ) { m_filterSrc.SetAddress(ip); }
76  const PIPSocket::Address & GetFilterSrcIP() const { return m_filterSrc.GetAddress(); }
77 
79  const PIPSocket::Address & ip
80  ) { m_filterDst.SetAddress(ip); }
81  const PIPSocket::Address & GetFilterDstIP() const { return m_filterDst.GetAddress(); }
82 
84  WORD port
85  ) { m_filterSrc.SetPort(port); }
86  WORD GetFilterSrcPort() const { return m_filterSrc.GetPort(); }
87 
89  WORD port
90  ) { m_filterDst.SetPort(port); }
91  WORD GetFilterDstPort() const { return m_filterDst.GetPort(); }
92 
93 
96 
97  PIPSocketAddressAndPort m_addr[2];
99  bool m_found[2];
100 
101  DWORD m_ssrc[2];
102  WORD m_seq[2];
103  DWORD m_ts[2];
104 
105  unsigned m_ssrc_matches[2];
106  unsigned m_seq_matches[2];
107  unsigned m_ts_matches[2];
108 
110 
111  PString m_type[2];
112  PString m_format[2];
113 
114  size_t m_index[2];
115  };
116  class DiscoveredRTPMap : public PObject, public std::map<std::string, DiscoveredRTPInfo>
117  {
118  PCLASSINFO(DiscoveredRTPMap, PObject);
119  public:
120  void PrintOn(ostream & strm) const;
121  };
122 
123  bool DiscoverRTP(DiscoveredRTPMap & discoveredRTPMap);
124 
125  bool SetFilters(
126  const DiscoveredRTPInfo & rtp,
127  int dir,
128  const PString & format = PString::Empty()
129  );
130  bool SetFilters(
131  const DiscoveredRTPMap & rtp,
132  size_t index,
133  const PString & format = PString::Empty()
134  );
135 
136  bool SetPayloadMap(
138  const OpalMediaFormat & format
139  );
140 
141  OpalMediaFormat GetMediaFormat(const RTP_DataFrame & rtp) const;
142 
143  protected:
144  struct FileHeader {
145  DWORD magic_number; /* magic number */
146  WORD version_major; /* major version number */
147  WORD version_minor; /* minor version number */
148  DWORD thiszone; /* GMT to local correction */
149  DWORD sigfigs; /* accuracy of timestamps */
150  DWORD snaplen; /* max length of captured packets, in octets */
151  DWORD network; /* data link type */
152  };
153 
154  struct RecordHeader {
155  DWORD ts_sec; /* timestamp seconds */
156  DWORD ts_usec; /* timestamp microseconds */
157  DWORD incl_len; /* number of octets of packet saved in file */
158  DWORD orig_len; /* actual length of packet */
159  };
160 
161 
163 
164  class Frame : public PEthSocket::Frame {
165  public:
166  Frame() : m_otherEndian(false) { }
167 
168  virtual bool Read(
169  PChannel & channel,
170  PINDEX packetSize = P_MAX_INDEX
171  );
172 
174  };
176  PCriticalSection m_writeMutex;
177 
178  PIPSocketAddressAndPort m_filterSrc;
179  PIPSocketAddressAndPort m_filterDst;
180  PIPSocketAddressAndPort m_packetSrc;
181  PIPSocketAddressAndPort m_packetDst;
182 
183  std::map<RTP_DataFrame::PayloadTypes, OpalMediaFormat> m_payloadType2mediaFormat;
184 };
185 
186 
187 #endif // PTLIB_PCAPFILE_H
188 
189 
190 // End Of File ///////////////////////////////////////////////////////////////