OPAL Version 3.10.2
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: 25534 $
00029  * $Author: rjongbloed $
00030  * $Date: 2011-04-08 04:02:23 -0500 (Fri, 08 Apr 2011) $
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 
00043 
00046 class OpalPCAPFile : public PFile
00047 {
00048     PCLASSINFO(OpalPCAPFile, PFile);
00049   public:
00050     OpalPCAPFile();
00051 
00052     bool Open(const PFilePath & filename);
00053     bool Restart();
00054 
00055     void PrintOn(ostream & strm) const;
00056 
00057     bool ReadRawPacket(PBYTEArray & payload);
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_packetTime; }
00064     const PIPSocket::Address & GetSrcIP() const { return m_packetSrcIP; }
00065     const PIPSocket::Address & GetDstIP() const { return m_packetDstIP; }
00066     unsigned IsFragmentated() const { return m_fragmentated; }
00067     WORD GetSrcPort() const { return m_packetSrcPort; }
00068     WORD GetDstPort() const { return m_packetDstPort; }
00069 
00070     void SetFilterSrcIP(
00071       const PIPSocket::Address & ip
00072     ) { m_filterSrcIP = ip; }
00073 
00074     void SetFilterDstIP(
00075       const PIPSocket::Address & ip
00076     ) { m_filterDstIP = ip; }
00077 
00078     void SetFilterSrcPort(
00079       WORD port
00080     ) { m_filterSrcPort = port; }
00081 
00082     void SetFilterDstPort(
00083       WORD port
00084     ) { m_filterDstPort = port; }
00085 
00086 
00087     struct DiscoveredRTPInfo {
00088       DiscoveredRTPInfo();
00089 
00090       PIPSocketAddressAndPort     m_addr[2];
00091       RTP_DataFrame::PayloadTypes m_payload[2];
00092       bool                        m_found[2];
00093 
00094       DWORD m_ssrc[2];
00095       WORD  m_seq[2];
00096       DWORD m_ts[2];
00097 
00098       unsigned m_ssrc_matches[2];
00099       unsigned m_seq_matches[2];
00100       unsigned m_ts_matches[2];
00101 
00102       RTP_DataFrame m_firstFrame[2];
00103 
00104       PString m_type[2];
00105       PString m_format[2];
00106 
00107       size_t m_index[2];
00108     };
00109     class DiscoveredRTPMap : public PObject, public std::map<std::string, DiscoveredRTPInfo>
00110     {
00111         PCLASSINFO(DiscoveredRTPMap, PObject);
00112       public:
00113         void PrintOn(ostream & strm) const;
00114     };
00115 
00116     bool DiscoverRTP(DiscoveredRTPMap & discoveredRTPMap);
00117 
00118     void SetFilters(
00119       const DiscoveredRTPInfo & rtp,
00120       int dir
00121     );
00122     bool SetFilters(
00123       const DiscoveredRTPMap & rtp,
00124       size_t index
00125     );
00126 
00127     bool SetPayloadMap(
00128       RTP_DataFrame::PayloadTypes pt,
00129       const OpalMediaFormat & format
00130     );
00131 
00132     OpalMediaFormat GetMediaFormat(const RTP_DataFrame & rtp) const;
00133 
00134   protected:
00135     PINDEX GetNetworkLayerHeaderSize();
00136 
00137     struct FileHeader { 
00138       DWORD magic_number;   /* magic number */
00139       WORD  version_major;  /* major version number */
00140       WORD  version_minor;  /* minor version number */
00141       DWORD thiszone;       /* GMT to local correction */
00142       DWORD sigfigs;        /* accuracy of timestamps */
00143       DWORD snaplen;        /* max length of captured packets, in octets */
00144       DWORD network;        /* data link type */
00145     };
00146 
00147     struct RecordHeader { 
00148       DWORD ts_sec;         /* timestamp seconds */
00149       DWORD ts_usec;        /* timestamp microseconds */
00150       DWORD incl_len;       /* number of octets of packet saved in file */
00151       DWORD orig_len;       /* actual length of packet */
00152     };
00153 
00154 
00155     FileHeader m_fileHeader;
00156     bool       m_otherEndian;
00157     PBYTEArray m_rawPacket;
00158     PTime      m_packetTime;
00159 
00160     PIPSocket::Address m_filterSrcIP;
00161     PIPSocket::Address m_filterDstIP;
00162     PIPSocket::Address m_packetSrcIP;
00163     PIPSocket::Address m_packetDstIP;
00164 
00165     PBYTEArray m_fragments;
00166     bool       m_fragmentated;
00167     unsigned   m_fragmentProto;
00168 
00169     WORD m_filterSrcPort;
00170     WORD m_filterDstPort;
00171     WORD m_packetSrcPort;
00172     WORD m_packetDstPort;
00173 
00174     std::map<RTP_DataFrame::PayloadTypes, OpalMediaFormat> m_payloadType2mediaFormat;
00175 };
00176 
00177 
00178 #endif // PTLIB_PCAPFILE_H
00179 
00180 
00181 // End Of File ///////////////////////////////////////////////////////////////