OPAL  Version 3.12.9
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: 28323 $
29  * $Author: rjongbloed $
30  * $Date: 2012-09-07 23:16:33 +1000 (Fri, 07 Sep 2012) $
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);
54  bool Restart();
55 
56  void PrintOn(ostream & strm) const;
57 
58  int GetDataLink(PBYTEArray & payload);
59  int GetIP(PBYTEArray & payload);
60  int GetUDP(PBYTEArray & payload);
61  int GetRTP(RTP_DataFrame & rtp);
62 
63  const PTime & GetPacketTime() const { return m_rawPacket.GetTimestamp(); }
64  const PIPSocket::Address & GetSrcIP() const { return m_packetSrc.GetAddress(); }
65  const PIPSocket::Address & GetDstIP() const { return m_packetDst.GetAddress(); }
66  unsigned IsFragmentated() const { return m_rawPacket.IsFragmentated(); }
67  WORD GetSrcPort() const { return m_packetSrc.GetPort(); }
68  WORD GetDstPort() const { return m_packetDst.GetPort(); }
69 
71  const PIPSocket::Address & ip
72  ) { m_filterSrc.SetAddress(ip); }
73  const PIPSocket::Address & GetFilterSrcIP() const { return m_filterSrc.GetAddress(); }
74 
76  const PIPSocket::Address & ip
77  ) { m_filterDst.SetAddress(ip); }
78  const PIPSocket::Address & GetFilterDstIP() const { return m_filterDst.GetAddress(); }
79 
81  WORD port
82  ) { m_filterSrc.SetPort(port); }
83  WORD GetFilterSrcPort() const { return m_filterSrc.GetPort(); }
84 
86  WORD port
87  ) { m_filterDst.SetPort(port); }
88  WORD GetFilterDstPort() const { return m_filterDst.GetPort(); }
89 
90 
93 
94  PIPSocketAddressAndPort m_addr[2];
96  bool m_found[2];
97 
98  DWORD m_ssrc[2];
99  WORD m_seq[2];
100  DWORD m_ts[2];
101 
102  unsigned m_ssrc_matches[2];
103  unsigned m_seq_matches[2];
104  unsigned m_ts_matches[2];
105 
107 
108  PString m_type[2];
109  PString m_format[2];
110 
111  size_t m_index[2];
112  };
113  class DiscoveredRTPMap : public PObject, public std::map<std::string, DiscoveredRTPInfo>
114  {
115  PCLASSINFO(DiscoveredRTPMap, PObject);
116  public:
117  void PrintOn(ostream & strm) const;
118  };
119 
120  bool DiscoverRTP(DiscoveredRTPMap & discoveredRTPMap);
121 
122  bool SetFilters(
123  const DiscoveredRTPInfo & rtp,
124  int dir,
125  const PString & format = PString::Empty()
126  );
127  bool SetFilters(
128  const DiscoveredRTPMap & rtp,
129  size_t index,
130  const PString & format = PString::Empty()
131  );
132 
133  bool SetPayloadMap(
135  const OpalMediaFormat & format
136  );
137 
138  OpalMediaFormat GetMediaFormat(const RTP_DataFrame & rtp) const;
139 
140  protected:
141  struct FileHeader {
142  DWORD magic_number; /* magic number */
143  WORD version_major; /* major version number */
144  WORD version_minor; /* minor version number */
145  DWORD thiszone; /* GMT to local correction */
146  DWORD sigfigs; /* accuracy of timestamps */
147  DWORD snaplen; /* max length of captured packets, in octets */
148  DWORD network; /* data link type */
149  };
150 
151  struct RecordHeader {
152  DWORD ts_sec; /* timestamp seconds */
153  DWORD ts_usec; /* timestamp microseconds */
154  DWORD incl_len; /* number of octets of packet saved in file */
155  DWORD orig_len; /* actual length of packet */
156  };
157 
158 
160 
161  class Frame : public PEthSocket::Frame {
162  public:
163  Frame() : m_otherEndian(false) { }
164 
165  virtual bool Read(
166  PChannel & channel,
167  PINDEX packetSize = P_MAX_INDEX
168  );
169 
171  };
173 
174  PIPSocketAddressAndPort m_filterSrc;
175  PIPSocketAddressAndPort m_filterDst;
176  PIPSocketAddressAndPort m_packetSrc;
177  PIPSocketAddressAndPort m_packetDst;
178 
179  std::map<RTP_DataFrame::PayloadTypes, OpalMediaFormat> m_payloadType2mediaFormat;
180 };
181 
182 
183 #endif // PTLIB_PCAPFILE_H
184 
185 
186 // End Of File ///////////////////////////////////////////////////////////////