pcapfile.h

Go to the documentation of this file.
00001 /*
00002  * pcapfile.h
00003  *
00004  * Ethernet capture (PCAP) file declaration
00005  *
00006  * Portable Tools Library
00007  *
00008  * Copyright (C) 2011 Vox Lucida Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Tools Library.
00021  *
00022  * The Initial Developer of the Original Code is Vox Lucida
00023  *
00024  * All Rights Reserved.
00025  *
00026  * Contributor(s): ______________________________________.
00027  *
00028  * $Revision: 28323 $
00029  * $Author: rjongbloed $
00030  * $Date: 2012-09-07 08:16:33 -0500 (Fri, 07 Sep 2012) $
00031  */
00032 
00033 #ifndef PTLIB_PCAPFILE_H
00034 #define PTLIB_PCAPFILE_H
00035 
00036 #ifdef P_USE_PRAGMA
00037 #pragma interface
00038 #endif
00039 
00040 #include <rtp/rtp.h>
00041 #include <opal/mediafmt.h>
00042 #include <ptlib/sockets.h>
00043 
00044 
00047 class OpalPCAPFile : public PFile
00048 {
00049     PCLASSINFO(OpalPCAPFile, PFile);
00050   public:
00051     OpalPCAPFile();
00052 
00053     bool Open(const PFilePath & filename);
00054     bool Restart();
00055 
00056     void PrintOn(ostream & strm) const;
00057 
00058     int GetDataLink(PBYTEArray & payload);
00059     int GetIP(PBYTEArray & payload);
00060     int GetUDP(PBYTEArray & payload);
00061     int GetRTP(RTP_DataFrame & rtp);
00062 
00063     const PTime & GetPacketTime() const { return m_rawPacket.GetTimestamp(); }
00064     const PIPSocket::Address & GetSrcIP() const { return m_packetSrc.GetAddress(); }
00065     const PIPSocket::Address & GetDstIP() const { return m_packetDst.GetAddress(); }
00066     unsigned IsFragmentated() const { return m_rawPacket.IsFragmentated(); }
00067     WORD GetSrcPort() const { return m_packetSrc.GetPort(); }
00068     WORD GetDstPort() const { return m_packetDst.GetPort(); }
00069 
00070     void SetFilterSrcIP(
00071       const PIPSocket::Address & ip
00072     ) { m_filterSrc.SetAddress(ip); }
00073     const PIPSocket::Address & GetFilterSrcIP() const { return m_filterSrc.GetAddress(); }
00074 
00075     void SetFilterDstIP(
00076       const PIPSocket::Address & ip
00077     ) { m_filterDst.SetAddress(ip); }
00078     const PIPSocket::Address & GetFilterDstIP() const { return m_filterDst.GetAddress(); }
00079 
00080     void SetFilterSrcPort(
00081       WORD port
00082     ) { m_filterSrc.SetPort(port); }
00083     WORD GetFilterSrcPort() const { return m_filterSrc.GetPort(); }
00084 
00085     void SetFilterDstPort(
00086       WORD port
00087     ) { m_filterDst.SetPort(port); }
00088     WORD GetFilterDstPort() const { return m_filterDst.GetPort(); }
00089 
00090 
00091     struct DiscoveredRTPInfo {
00092       DiscoveredRTPInfo();
00093 
00094       PIPSocketAddressAndPort     m_addr[2];
00095       RTP_DataFrame::PayloadTypes m_payload[2];
00096       bool                        m_found[2];
00097 
00098       DWORD m_ssrc[2];
00099       WORD  m_seq[2];
00100       DWORD m_ts[2];
00101 
00102       unsigned m_ssrc_matches[2];
00103       unsigned m_seq_matches[2];
00104       unsigned m_ts_matches[2];
00105 
00106       RTP_DataFrame m_firstFrame[2];
00107 
00108       PString m_type[2];
00109       PString m_format[2];
00110 
00111       size_t m_index[2];
00112     };
00113     class DiscoveredRTPMap : public PObject, public std::map<std::string, DiscoveredRTPInfo>
00114     {
00115         PCLASSINFO(DiscoveredRTPMap, PObject);
00116       public:
00117         void PrintOn(ostream & strm) const;
00118     };
00119 
00120     bool DiscoverRTP(DiscoveredRTPMap & discoveredRTPMap);
00121 
00122     bool SetFilters(
00123       const DiscoveredRTPInfo & rtp,
00124       int dir,
00125       const PString & format = PString::Empty()
00126     );
00127     bool SetFilters(
00128       const DiscoveredRTPMap & rtp,
00129       size_t index,
00130       const PString & format = PString::Empty()
00131     );
00132 
00133     bool SetPayloadMap(
00134       RTP_DataFrame::PayloadTypes pt,
00135       const OpalMediaFormat & format
00136     );
00137 
00138     OpalMediaFormat GetMediaFormat(const RTP_DataFrame & rtp) const;
00139 
00140   protected:
00141     struct FileHeader { 
00142       DWORD magic_number;   /* magic number */
00143       WORD  version_major;  /* major version number */
00144       WORD  version_minor;  /* minor version number */
00145       DWORD thiszone;       /* GMT to local correction */
00146       DWORD sigfigs;        /* accuracy of timestamps */
00147       DWORD snaplen;        /* max length of captured packets, in octets */
00148       DWORD network;        /* data link type */
00149     };
00150 
00151     struct RecordHeader { 
00152       DWORD ts_sec;         /* timestamp seconds */
00153       DWORD ts_usec;        /* timestamp microseconds */
00154       DWORD incl_len;       /* number of octets of packet saved in file */
00155       DWORD orig_len;       /* actual length of packet */
00156     };
00157 
00158 
00159     FileHeader m_fileHeader;
00160 
00161     class Frame : public PEthSocket::Frame {
00162       public:
00163         Frame() : m_otherEndian(false) { }
00164 
00165         virtual bool Read(
00166           PChannel & channel,
00167           PINDEX packetSize = P_MAX_INDEX
00168         );
00169 
00170         bool m_otherEndian;
00171     };
00172     Frame m_rawPacket;
00173 
00174     PIPSocketAddressAndPort m_filterSrc;
00175     PIPSocketAddressAndPort m_filterDst;
00176     PIPSocketAddressAndPort m_packetSrc;
00177     PIPSocketAddressAndPort m_packetDst;
00178 
00179     std::map<RTP_DataFrame::PayloadTypes, OpalMediaFormat> m_payloadType2mediaFormat;
00180 };
00181 
00182 
00183 #endif // PTLIB_PCAPFILE_H
00184 
00185 
00186 // End Of File ///////////////////////////////////////////////////////////////

Generated on 21 Jun 2013 for OPAL by  doxygen 1.4.7