10337db2bSimarom#ifndef __TREX_LIBPCAP_H__
20337db2bSimarom#define __TREX_LIBPCAP_H__
38b52a31eSHanoh Haim
48b52a31eSHanoh Haim/*
58b52a31eSHanoh HaimCopyright (c) 2015-2015 Cisco Systems, Inc.
68b52a31eSHanoh Haim
78b52a31eSHanoh HaimLicensed under the Apache License, Version 2.0 (the "License");
88b52a31eSHanoh Haimyou may not use this file except in compliance with the License.
98b52a31eSHanoh HaimYou may obtain a copy of the License at
108b52a31eSHanoh Haim
118b52a31eSHanoh Haim    http://www.apache.org/licenses/LICENSE-2.0
128b52a31eSHanoh Haim
138b52a31eSHanoh HaimUnless required by applicable law or agreed to in writing, software
148b52a31eSHanoh Haimdistributed under the License is distributed on an "AS IS" BASIS,
158b52a31eSHanoh HaimWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
168b52a31eSHanoh HaimSee the License for the specific language governing permissions and
178b52a31eSHanoh Haimlimitations under the License.
188b52a31eSHanoh Haim*/
198b52a31eSHanoh Haim
208b52a31eSHanoh Haim
218b52a31eSHanoh Haim#include "captureFile.h"
228b52a31eSHanoh Haim#include <stdio.h>
238b52a31eSHanoh Haim
248b52a31eSHanoh Haimtypedef struct pcaptime {
258b52a31eSHanoh Haim   uint32_t sec;
268b52a31eSHanoh Haim   uint32_t msec;
278b52a31eSHanoh Haim} pcaptime_t;
288b52a31eSHanoh Haim
298b52a31eSHanoh Haimtypedef struct packet_file_header
308b52a31eSHanoh Haim{
318b52a31eSHanoh Haim	uint32_t magic;
328b52a31eSHanoh Haim	uint16_t version_major;
338b52a31eSHanoh Haim	uint16_t version_minor;
348b52a31eSHanoh Haim	uint32_t thiszone;
358b52a31eSHanoh Haim	uint32_t sigfigs;
368b52a31eSHanoh Haim	uint32_t snaplen;
378b52a31eSHanoh Haim	uint32_t linktype;
388b52a31eSHanoh Haim}packet_file_header_t ;
398b52a31eSHanoh Haim
408b52a31eSHanoh Haimtypedef struct sf_pkthdr {
418b52a31eSHanoh Haim	pcaptime_t  ts;
428b52a31eSHanoh Haim	uint32_t            caplen;
438b52a31eSHanoh Haim	uint32_t            len;
448b52a31eSHanoh Haim} sf_pkthdr_t;
458b52a31eSHanoh Haim
468b52a31eSHanoh Haim/**
478b52a31eSHanoh Haim * Implements the CCAPReaderBase interface.
488b52a31eSHanoh Haim *
498b52a31eSHanoh Haim */
508b52a31eSHanoh Haimclass LibPCapReader : public CCapReaderBase
518b52a31eSHanoh Haim{
528b52a31eSHanoh Haimpublic:
538b52a31eSHanoh Haim    LibPCapReader();
548b52a31eSHanoh Haim
558b52a31eSHanoh Haim    virtual ~LibPCapReader();
568b52a31eSHanoh Haim
578b52a31eSHanoh Haim	/**
588b52a31eSHanoh Haim     * open file for reading.
598b52a31eSHanoh Haim     * (can be called once).
608b52a31eSHanoh Haim	 * @param name
618b52a31eSHanoh Haim	 *
628b52a31eSHanoh Haim	 * @return bool
638b52a31eSHanoh Haim	 */
648b52a31eSHanoh Haim    bool Create(char * name, int loops = 0);
658b52a31eSHanoh Haim
668b52a31eSHanoh Haim	/**
678b52a31eSHanoh Haim     * When called after open will return true only if
688b52a31eSHanoh Haim     * capture file is libpcap format.
698b52a31eSHanoh Haim	 *
708b52a31eSHanoh Haim	 * @return bool
718b52a31eSHanoh Haim	 */
720d4c9932Simarom    bool isValid() { return m_is_valid; }
738b52a31eSHanoh Haim
748b52a31eSHanoh Haim	/**
758b52a31eSHanoh Haim     * Fill the structure with the new packet.
768b52a31eSHanoh Haim	 * @param lpPacket
778b52a31eSHanoh Haim	 *
788b52a31eSHanoh Haim     * @return bool - return true if packet were read and false
798b52a31eSHanoh Haim     *         otherwise (reached eof)
808b52a31eSHanoh Haim	 */
810d4c9932Simarom    virtual bool ReadPacket(CCapPktRaw *lpPacket);
82540da82eSimarom    virtual void Rewind();
838b52a31eSHanoh Haim
848b52a31eSHanoh Haim
85540da82eSimarom    virtual capture_type_e get_type() {
86540da82eSimarom        return LIBPCAP;
87540da82eSimarom    }
898b52a31eSHanoh Haimprivate:
908b52a31eSHanoh Haim	LibPCapReader(LibPCapReader &);
918b52a31eSHanoh Haim
928b52a31eSHanoh Haim
938b52a31eSHanoh Haim	bool init();
948b52a31eSHanoh Haim    void flip(sf_pkthdr_t * tofilp);
958b52a31eSHanoh Haim	bool m_is_open;
968b52a31eSHanoh Haim	uint64_t m_last_time;
978b52a31eSHanoh Haim	bool m_is_valid;
988b52a31eSHanoh Haim    FILE * m_file_handler;
998b52a31eSHanoh Haim    bool m_is_flip;
1008b52a31eSHanoh Haim
1018b52a31eSHanoh Haim};
1028b52a31eSHanoh Haim
1038b52a31eSHanoh Haim/**
1048b52a31eSHanoh Haim * Libpcap file format writer.
1058b52a31eSHanoh Haim * Implements CFileWrirerBase interface
1068b52a31eSHanoh Haim */
1078b52a31eSHanoh Haimclass LibPCapWriter: public CFileWriterBase
1088b52a31eSHanoh Haim{
1098b52a31eSHanoh Haim
1108b52a31eSHanoh Haimpublic:
1118b52a31eSHanoh Haim
1128b52a31eSHanoh Haim	LibPCapWriter();
1138b52a31eSHanoh Haim	virtual ~LibPCapWriter();
1148b52a31eSHanoh Haim
1158b52a31eSHanoh Haim	/**
1168b52a31eSHanoh Haim     * Open file for writing. Rewrite from scratch (no append).
1178b52a31eSHanoh Haim     * @param name - the file name
1188b52a31eSHanoh Haim	 *
1198b52a31eSHanoh Haim     * @return bool - return true if File was open successfully.
1208b52a31eSHanoh Haim	 */
1218b52a31eSHanoh Haim	bool Create(char * name);
1228b52a31eSHanoh Haim
1238b52a31eSHanoh Haim	/**
1248b52a31eSHanoh Haim     * Write packet to file (must be called only after successfull
1258b52a31eSHanoh Haim     * Create).
1268b52a31eSHanoh Haim     *
1278b52a31eSHanoh Haim     * @param p  - buffer pointer
1288b52a31eSHanoh Haim     * @param size - buffer length
1298b52a31eSHanoh Haim     *
1308b52a31eSHanoh Haim     * @return true on success.
1318b52a31eSHanoh Haim	 */
1328b52a31eSHanoh Haim	virtual bool write_packet(CCapPktRaw * lpPacket);
1338b52a31eSHanoh Haim    /**
1348b52a31eSHanoh Haim     *
1358b52a31eSHanoh Haim     * returns the count of packets so far written
1368b52a31eSHanoh Haim     *
1378b52a31eSHanoh Haim     * @return uint32_t
1388b52a31eSHanoh Haim     */
1398b52a31eSHanoh Haim    uint32_t get_pkt_count();
1408b52a31eSHanoh Haim
1418b52a31eSHanoh Haim    /**
1428b52a31eSHanoh Haim     * Close file and flush all.
1438b52a31eSHanoh Haim	*/
1448b52a31eSHanoh Haim	void Close();
1458b52a31eSHanoh Haim
146537f5831Simarom    /**
147537f5831Simarom     * flush all packets to disk
148537f5831Simarom     *
149537f5831Simarom     * @author imarom (11/24/2016)
150537f5831Simarom     */
151537f5831Simarom    void flush_to_disk();
1538b52a31eSHanoh Haimprivate:
1548b52a31eSHanoh Haim
1558b52a31eSHanoh Haim	bool init();
1568b52a31eSHanoh Haim	FILE * m_file_handler;
1578b52a31eSHanoh Haim	uint64_t m_timestamp;
1588b52a31eSHanoh Haim    bool m_is_open;
1598b52a31eSHanoh Haim    uint32_t m_pkt_count;
1608b52a31eSHanoh Haim};
1628b52a31eSHanoh Haim#endif