18b52a31eSHanoh Haim#ifndef __CAPTURE_FILE_H__
28b52a31eSHanoh Haim#define __CAPTURE_FILE_H__
38b52a31eSHanoh Haim/*
48b52a31eSHanoh HaimCopyright (c) 2015-2015 Cisco Systems, Inc.
58b52a31eSHanoh Haim
68b52a31eSHanoh HaimLicensed under the Apache License, Version 2.0 (the "License");
78b52a31eSHanoh Haimyou may not use this file except in compliance with the License.
88b52a31eSHanoh HaimYou may obtain a copy of the License at
98b52a31eSHanoh Haim
108b52a31eSHanoh Haim    http://www.apache.org/licenses/LICENSE-2.0
118b52a31eSHanoh Haim
128b52a31eSHanoh HaimUnless required by applicable law or agreed to in writing, software
138b52a31eSHanoh Haimdistributed under the License is distributed on an "AS IS" BASIS,
148b52a31eSHanoh HaimWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
158b52a31eSHanoh HaimSee the License for the specific language governing permissions and
168b52a31eSHanoh Haimlimitations under the License.
178b52a31eSHanoh Haim*/
188b52a31eSHanoh Haim
198b52a31eSHanoh Haim
208b52a31eSHanoh Haim
218b52a31eSHanoh Haim#include "c_common.h"
228b52a31eSHanoh Haim#include <stdio.h>
238b52a31eSHanoh Haim#include "bitMan.h"
248b52a31eSHanoh Haim#include <math.h>
258b52a31eSHanoh Haim#include <stdlib.h>
268b52a31eSHanoh Haim#include <string>
278691f401Simarom#include <iostream>
2858d22eefSimarom#include <cmath>
298691f401Simarom
308b52a31eSHanoh Haim#ifdef WIN32
318b52a31eSHanoh Haim#pragma warning(disable:4786)
328b52a31eSHanoh Haim#endif
338b52a31eSHanoh Haim
348b52a31eSHanoh Haim
358b52a31eSHanoh Haimtypedef enum capture_type {
368b52a31eSHanoh Haim	LIBPCAP,
378b52a31eSHanoh Haim	ERF,
388b52a31eSHanoh Haim	LAST_TYPE
398b52a31eSHanoh Haim} capture_type_e;
408b52a31eSHanoh Haim
4185d536ebSHanoh Haim#define MAX_PKT_SIZE (9*1024+22) /* 9k IP +14+4 FCS +some spare */
428b52a31eSHanoh Haim
438b52a31eSHanoh Haim#define READER_MAX_PACKET_SIZE MAX_PKT_SIZE
448b52a31eSHanoh Haim
458b52a31eSHanoh Haimclass CAlignMalloc {
468b52a31eSHanoh Haimpublic:
478b52a31eSHanoh Haim    CAlignMalloc(){
488b52a31eSHanoh Haim        m_p=0;
498b52a31eSHanoh Haim    }
508b52a31eSHanoh Haim    void * malloc(uint16_t size,uint8_t align);
518b52a31eSHanoh Haim    void free();
528b52a31eSHanoh Haimpublic:
538b52a31eSHanoh Haim    char * m_p;
548b52a31eSHanoh Haim};
558b52a31eSHanoh Haim
568b52a31eSHanoh Haimstatic inline uintptr_t my_utl_align_up(uintptr_t num,uint16_t round){
578b52a31eSHanoh Haim    if ((num & ((round-1)) )==0) {
588b52a31eSHanoh Haim        //the number align
598b52a31eSHanoh Haim        return(num);
608b52a31eSHanoh Haim    }
618b52a31eSHanoh Haim    return( (num+round) & (~(round-1)) );
628b52a31eSHanoh Haim}
638b52a31eSHanoh Haim
648b52a31eSHanoh Haim
658b52a31eSHanoh Haim
668b52a31eSHanoh Haim
678b52a31eSHanoh Haimclass CPktNsecTimeStamp {
688b52a31eSHanoh Haimpublic:
698b52a31eSHanoh Haim
708b52a31eSHanoh Haim    #define _NSEC_TO_SEC 1000000000.0
718b52a31eSHanoh Haim    CPktNsecTimeStamp(){
728b52a31eSHanoh Haim        m_time_sec  =0;
738b52a31eSHanoh Haim        m_time_nsec =0;
748b52a31eSHanoh Haim    }
758b52a31eSHanoh Haim
768b52a31eSHanoh Haim    CPktNsecTimeStamp(uint32_t sec,uint32_t nsec){
778b52a31eSHanoh Haim        m_time_sec =sec;
788b52a31eSHanoh Haim        m_time_nsec =nsec;
798b52a31eSHanoh Haim    }
808b52a31eSHanoh Haim    CPktNsecTimeStamp(double nsec){
818b52a31eSHanoh Haim        m_time_sec = (uint32_t)floor (nsec);
828b52a31eSHanoh Haim        nsec -= m_time_sec;
838b52a31eSHanoh Haim        m_time_nsec = (uint32_t)floor(nsec*_NSEC_TO_SEC);
848b52a31eSHanoh Haim    }
858b52a31eSHanoh Haim
868b52a31eSHanoh Haim    double getNsec() const {
878b52a31eSHanoh Haim         return ((double)m_time_sec +(double)m_time_nsec/(_NSEC_TO_SEC));
888b52a31eSHanoh Haim    }
898b52a31eSHanoh Haim
908b52a31eSHanoh Haim    double diff(const CPktNsecTimeStamp & obj){
9158d22eefSimarom        return (std::abs(getNsec() - obj.getNsec() ) );
928b52a31eSHanoh Haim    }
938b52a31eSHanoh Haim
948b52a31eSHanoh Haim    void Dump(FILE *fd);
958b52a31eSHanoh Haimpublic:
968b52a31eSHanoh Haim    uint32_t     m_time_sec;
978b52a31eSHanoh Haim    uint32_t     m_time_nsec;
988b52a31eSHanoh Haim};
998b52a31eSHanoh Haim
1008b52a31eSHanoh Haim
1018b52a31eSHanoh Haim
1028b52a31eSHanoh Haimclass CCapPktRaw {
1038b52a31eSHanoh Haim
1048b52a31eSHanoh Haimpublic:
1058b52a31eSHanoh Haim	CCapPktRaw();
1068b52a31eSHanoh Haim    CCapPktRaw(int size);
1078b52a31eSHanoh Haim    CCapPktRaw(CCapPktRaw  *obj);
1088b52a31eSHanoh Haim	virtual ~CCapPktRaw();
1098b52a31eSHanoh Haim
1108b52a31eSHanoh Haim    uint32_t     time_sec;
1118b52a31eSHanoh Haim    uint32_t     time_nsec;
1128b52a31eSHanoh Haim    char       * raw;
1138b52a31eSHanoh Haim    uint64_t	 pkt_cnt;
1148b52a31eSHanoh Haim
1158b52a31eSHanoh Haim    uint16_t     pkt_len;
1168b52a31eSHanoh Haimprivate:
1178b52a31eSHanoh Haim    uint16_t     flags;
1188b52a31eSHanoh Haim    CAlignMalloc  m_handle;
1198b52a31eSHanoh Haimpublic:
1208b52a31eSHanoh Haim    double get_time(void) {
1218b52a31eSHanoh Haim        CPktNsecTimeStamp t1(time_sec,time_nsec);
1228b52a31eSHanoh Haim        return ( t1.getNsec());
1238b52a31eSHanoh Haim    }
1248b52a31eSHanoh Haim    void set_new_time(double new_time){
1258b52a31eSHanoh Haim        CPktNsecTimeStamp t1(new_time);
1268b52a31eSHanoh Haim        time_sec =t1.m_time_sec;
1278b52a31eSHanoh Haim        time_nsec=t1.m_time_nsec;
1288b52a31eSHanoh Haim    }
1298b52a31eSHanoh Haim
1308b52a31eSHanoh Haim    /* enlarge the packet */
1318b52a31eSHanoh Haim    char * append(uint16_t len);
1328b52a31eSHanoh Haim
1338b52a31eSHanoh Haim    void CloneShalow(CCapPktRaw  *obj);
1348b52a31eSHanoh Haim
1358b52a31eSHanoh Haim    void setInterface(uint8_t _if){
1368b52a31eSHanoh Haim        btSetMaskBit16(flags,10,8,_if);
1378b52a31eSHanoh Haim    }
1388b52a31eSHanoh Haim
1398b52a31eSHanoh Haim    uint8_t getInterface(){
1408b52a31eSHanoh Haim        return ((uint8_t)btGetMaskBit16(flags,10,8));
1418b52a31eSHanoh Haim    }
1428b52a31eSHanoh Haim
1438b52a31eSHanoh Haim    void setDoNotFree(bool do_not_free){
1448b52a31eSHanoh Haim        btSetMaskBit16(flags,0,0,do_not_free?1:0);
1458b52a31eSHanoh Haim    }
1468b52a31eSHanoh Haim
1478b52a31eSHanoh Haim    bool getDoNotFree(){
1488b52a31eSHanoh Haim        return ( ( btGetMaskBit16(flags,0,0) ? true:false) );
1498b52a31eSHanoh Haim    }
1508b52a31eSHanoh Haim
1518b52a31eSHanoh Haim    bool Compare(CCapPktRaw * obj,int dump,double dsec);
1528b52a31eSHanoh Haim
1538b52a31eSHanoh Haim
1548b52a31eSHanoh Haimpublic:
1558b52a31eSHanoh Haim	inline uint16_t getTotalLen(void) {
1568b52a31eSHanoh Haim		return (pkt_len);
1578b52a31eSHanoh Haim	}
1588b52a31eSHanoh Haim	void Dump(FILE *fd,int verbose);
1598b52a31eSHanoh Haim};
1608b52a31eSHanoh Haim
1618b52a31eSHanoh Haim/**
1628b52a31eSHanoh Haim * Interface for capture file reader.
1638b52a31eSHanoh Haim *
1648b52a31eSHanoh Haim */
1658b52a31eSHanoh Haimclass CCapReaderBase
1668b52a31eSHanoh Haim{
1678b52a31eSHanoh Haimpublic:
1688b52a31eSHanoh Haim
1698b52a31eSHanoh Haim    virtual ~CCapReaderBase(){}
1708b52a31eSHanoh Haim
1718b52a31eSHanoh Haim    virtual bool ReadPacket(CCapPktRaw * lpPacket)=0;
1728b52a31eSHanoh Haim
1738b52a31eSHanoh Haim
1748b52a31eSHanoh Haim    /* by default all reader reads one packet
1758b52a31eSHanoh Haim       and gives the feature one packet
1768b52a31eSHanoh Haim    */
1778b52a31eSHanoh Haim    virtual uint32_t  get_last_pkt_count() {return 1;}
1788b52a31eSHanoh Haim
1798b52a31eSHanoh Haim    /* method for rewind the reader
1808b52a31eSHanoh Haim      abstract and optional
1818b52a31eSHanoh Haim    */
1828b52a31eSHanoh Haim    virtual void Rewind() {};
1838b52a31eSHanoh Haim	/**
1848b52a31eSHanoh Haim     * open file for reading.
1858b52a31eSHanoh Haim     */
1868b52a31eSHanoh Haim	virtual bool Create(char * name, int loops = 0) = 0;
187540da82eSimarom
188540da82eSimarom    virtual capture_type_e get_type() = 0;
189540da82eSimarom
1908b52a31eSHanoh Haimprotected:
1918b52a31eSHanoh Haim    int 		m_loops;
1928b52a31eSHanoh Haim    uint64_t 	m_file_size;
1938b52a31eSHanoh Haim};
1948b52a31eSHanoh Haim
1958b52a31eSHanoh Haim/**
1968b52a31eSHanoh Haim * Factory for creating reader inteface of some of the supported
1978b52a31eSHanoh Haim * formats.
1988b52a31eSHanoh Haim *
1998b52a31eSHanoh Haim */
2008b52a31eSHanoh Haimclass CCapReaderFactory {
2018b52a31eSHanoh Haimpublic:
2028b52a31eSHanoh Haim	/**
2038b52a31eSHanoh Haim     * The function will try to create the matching reader for the
2048b52a31eSHanoh Haim     * file format (libpcap,ngsniffer...etc). Since there is no real
2058b52a31eSHanoh Haim     * connection (stile repository) between file suffix and its
2068b52a31eSHanoh Haim     * type we just try one bye one,
2078b52a31eSHanoh Haim     * @param name - cature file name
2088b52a31eSHanoh Haim     * @param loops - number of loops for the same capture. use 0
2098b52a31eSHanoh Haim     *                for one time transmition
2108691f401Simarom     * @param err - IO stream to print error
2118691f401Simarom     *
2128b52a31eSHanoh Haim     * @return CCapReaderBase* - pointer to new instance (allocated
2138b52a31eSHanoh Haim     *         by the function). the user should release the
2148b52a31eSHanoh Haim     *         instance once it has no use any more.
2158b52a31eSHanoh Haim	 */
2168691f401Simarom	static CCapReaderBase * CreateReader(char * name, int loops = 0, std::ostream &err = std::cout);
2178b52a31eSHanoh Haim
2188b52a31eSHanoh Haim
2198b52a31eSHanoh Haimprivate:
2208b52a31eSHanoh Haim	static CCapReaderBase * CreateReaderInstace(capture_type_e type);
2218b52a31eSHanoh Haim};
2228b52a31eSHanoh Haim
2238b52a31eSHanoh Haim/**
2248b52a31eSHanoh Haim * Interface for capture file writer.
2258b52a31eSHanoh Haim *
2268b52a31eSHanoh Haim */
2278b52a31eSHanoh Haimclass CFileWriterBase {
2288b52a31eSHanoh Haim
2298b52a31eSHanoh Haimpublic:
2308b52a31eSHanoh Haim
231537f5831Simarom    virtual ~CFileWriterBase(){};
232537f5831Simarom    virtual bool Create(char * name) = 0;
2338b52a31eSHanoh Haim    virtual bool write_packet(CCapPktRaw * lpPacket)=0;
234537f5831Simarom    virtual void flush_to_disk() = 0;
2358b52a31eSHanoh Haim
2368b52a31eSHanoh Haim};
2378b52a31eSHanoh Haim
2388b52a31eSHanoh Haim
2398b52a31eSHanoh Haim/**
2408b52a31eSHanoh Haim * Factory for creating capture file interface of some of the
2418b52a31eSHanoh Haim * supported formats.
2428b52a31eSHanoh Haim *
2438b52a31eSHanoh Haim */
2448b52a31eSHanoh Haimclass CCapWriterFactory {
2458b52a31eSHanoh Haimpublic:
2468b52a31eSHanoh Haim
2478b52a31eSHanoh Haim	/**
2488b52a31eSHanoh Haim     * The factory function will create the matching reader instance
2498b52a31eSHanoh Haim     * according to the type.
2508b52a31eSHanoh Haim     *
2518b52a31eSHanoh Haim     * @param type - the foramt
2528b52a31eSHanoh Haim     * @param name - new file name
2538b52a31eSHanoh Haim	 *
2548b52a31eSHanoh Haim     * @return CCapWriter* - return pointer to the writer instance
2558b52a31eSHanoh Haim     *         or NULL if failed from some reason (or unsupported
2568b52a31eSHanoh Haim     *         format).Instance user
2578b52a31eSHanoh Haim     *         should relase memory when instance not needed
2588b52a31eSHanoh Haim     *         anymore.
2598b52a31eSHanoh Haim	 */
2608b52a31eSHanoh Haim	static CFileWriterBase * CreateWriter(capture_type_e type ,char * name);
2618b52a31eSHanoh Haim
2628b52a31eSHanoh Haimprivate:
2638b52a31eSHanoh Haim
2648b52a31eSHanoh Haim    static CFileWriterBase * createWriterInsance(capture_type_e type );
2658b52a31eSHanoh Haim};
2668b52a31eSHanoh Haim
2678b52a31eSHanoh Haim
2688b52a31eSHanoh Haim#if WIN32
2698b52a31eSHanoh Haim
2708b52a31eSHanoh Haim#define CAP_FOPEN_64 fopen
2718b52a31eSHanoh Haim#define CAP_FSEEK_64 fseek
2728b52a31eSHanoh Haim#define CAP_FTELL_64 ftell
2738b52a31eSHanoh Haim
2748b52a31eSHanoh Haim#else
2758b52a31eSHanoh Haim
2768b52a31eSHanoh Haim#define CAP_FOPEN_64 fopen64
2778b52a31eSHanoh Haim#define CAP_FSEEK_64 fseeko64
2788b52a31eSHanoh Haim#define CAP_FTELL_64 ftello64
2798b52a31eSHanoh Haim
2808b52a31eSHanoh Haim#endif
2818b52a31eSHanoh Haim
2828b52a31eSHanoh Haim
2838b52a31eSHanoh Haimclass CErfCmp
2848b52a31eSHanoh Haim {
2858b52a31eSHanoh Haimpublic:
2868b52a31eSHanoh Haim    CErfCmp(){
2878b52a31eSHanoh Haim        dump=false;
2888b52a31eSHanoh Haim        d_sec=0.001;
2898b52a31eSHanoh Haim    }
2908b52a31eSHanoh Haim    bool compare(std::string f1, std::string f2 );
291f6901ca1SHanoh Haim
292f6901ca1SHanoh Haim    bool cpy(std::string src,std::string dst);
2938b52a31eSHanoh Haimpublic:
2948b52a31eSHanoh Haim    bool dump;
2958b52a31eSHanoh Haim    double d_sec;
2968b52a31eSHanoh Haim};
2978b52a31eSHanoh Haim
2988b52a31eSHanoh Haim
2998b52a31eSHanoh Haim
3008b52a31eSHanoh Haim#endif
301