pcap.h revision 540da82e
1#ifndef __LIBPCAP_H__
2#define __LIBPCAP_H__
3
4/*
5Copyright (c) 2015-2015 Cisco Systems, Inc.
6
7Licensed under the Apache License, Version 2.0 (the "License");
8you may not use this file except in compliance with the License.
9You may obtain a copy of the License at
10
11    http://www.apache.org/licenses/LICENSE-2.0
12
13Unless required by applicable law or agreed to in writing, software
14distributed under the License is distributed on an "AS IS" BASIS,
15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16See the License for the specific language governing permissions and
17limitations under the License.
18*/
19
20
21#include "captureFile.h"
22#include <stdio.h>
23
24typedef struct pcaptime {
25   uint32_t sec;
26   uint32_t msec;
27} pcaptime_t;
28
29typedef struct packet_file_header
30{
31	uint32_t magic;
32	uint16_t version_major;
33	uint16_t version_minor;
34	uint32_t thiszone;
35	uint32_t sigfigs;
36	uint32_t snaplen;
37	uint32_t linktype;
38}packet_file_header_t ;
39
40typedef struct sf_pkthdr {
41	pcaptime_t  ts;
42	uint32_t            caplen;
43	uint32_t            len;
44} sf_pkthdr_t;
45
46/**
47 * Implements the CCAPReaderBase interface.
48 *
49 */
50class LibPCapReader : public CCapReaderBase
51{
52public:
53    LibPCapReader();
54
55    virtual ~LibPCapReader();
56
57	/**
58     * open file for reading.
59     * (can be called once).
60	 * @param name
61	 *
62	 * @return bool
63	 */
64    bool Create(char * name, int loops = 0);
65
66	/**
67     * When called after open will return true only if
68     * capture file is libpcap format.
69	 *
70	 * @return bool
71	 */
72	bool isValid() { return m_is_valid; }
73
74	/**
75     * Fill the structure with the new packet.
76	 * @param lpPacket
77	 *
78     * @return bool - return true if packet were read and false
79     *         otherwise (reached eof)
80	 */
81	virtual bool ReadPacket(CCapPktRaw *lpPacket);
82    virtual void Rewind();
83
84
85    virtual capture_type_e get_type() {
86        return LIBPCAP;
87    }
88
89private:
90	LibPCapReader(LibPCapReader &);
91
92
93	bool init();
94    void flip(sf_pkthdr_t * tofilp);
95	bool m_is_open;
96	uint64_t m_last_time;
97	bool m_is_valid;
98    FILE * m_file_handler;
99    bool m_is_flip;
100
101};
102
103/**
104 * Libpcap file format writer.
105 * Implements CFileWrirerBase interface
106 */
107class LibPCapWriter: public CFileWriterBase
108{
109
110public:
111
112	LibPCapWriter();
113	virtual ~LibPCapWriter();
114
115	/**
116     * Open file for writing. Rewrite from scratch (no append).
117     * @param name - the file name
118	 *
119     * @return bool - return true if File was open successfully.
120	 */
121	bool Create(char * name);
122
123	/**
124     * Write packet to file (must be called only after successfull
125     * Create).
126     *
127     * @param p  - buffer pointer
128     * @param size - buffer length
129     *
130     * @return true on success.
131	 */
132	virtual bool write_packet(CCapPktRaw * lpPacket);
133    /**
134     *
135     * returns the count of packets so far written
136     *
137     * @return uint32_t
138     */
139    uint32_t get_pkt_count();
140
141    /**
142     * Close file and flush all.
143	*/
144	void Close();
145
146private:
147
148	bool init();
149	FILE * m_file_handler;
150	uint64_t m_timestamp;
151    bool m_is_open;
152    uint32_t m_pkt_count;
153};
154#endif
155