summaryrefslogtreecommitdiffstats
path: root/src/common/pcap.cpp
blob: 6dd5451473290fe18f7e3aa3e34bcc07bd73fcb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
Copyright (c) 2015-2015 Cisco Systems, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "pcap.h"
#include <errno.h>
#include <string.h>
#include "pal_utl.h"




static uint32_t MAGIC_NUM_FLIP = 0xd4c3b2a1;
static uint32_t MAGIC_NUM_DONT_FLIP = 0xa1b2c3d4;


LibPCapReader::LibPCapReader()
{
	m_is_open = false;
	m_last_time = 0;
	m_is_valid = false;
    m_file_handler = NULL;
	m_is_flip = false;
}

LibPCapReader::~LibPCapReader()
{
	if (m_is_open && m_file_handler) {
        fclose(m_file_handler);
	}
}

void LibPCapReader::Rewind() {
   if (m_is_open && m_file_handler) {
       rewind(m_file_handler);
       this->init();
   }
}

/**
 * open file for read.
 * @param name
 * 
 * @return bool
 */
bool LibPCapReader::Create(char * name, int loops)
{
    this->m_loops = loops;

    if(name == NULL) {
	return false;
    }

    if (m_is_open) {
        return true;
    }
    m_file_handler = CAP_FOPEN_64(name,"rb");
   
    if (m_file_handler == 0) {
        printf(" failed to open cap file %s : errno : %d\n",name, errno);
        return false;
    }

   CAP_FSEEK_64 (m_file_handler, 0, SEEK_END);
   m_file_size = CAP_FTELL_64 (m_file_handler);
   rewind (m_file_handler);
 
   if (init()) {
	   m_is_open = true;
	   return true;
   }

   fclose(m_file_handler);

   return false;
}

/**
 * init the reader.
 * First read the header of the file make sure it is libpacp.
 * If so read the flip value. records are senstive to the local
 * recording machine endianity.
 * 
 * @return bool
 */
bool LibPCapReader::init()
{
	packet_file_header_t header;
	memset(&header,0,sizeof(packet_file_header_t));
    size_t n = fread(&header,1,sizeof(packet_file_header_t),m_file_handler);
	if (n < sizeof(packet_file_header_t)) {
		return false;
	}

	if (header.magic == MAGIC_NUM_FLIP) {
		m_is_flip = true;
	} else if (header.magic == MAGIC_NUM_DONT_FLIP){
		m_is_flip = false;
	} else {
		// capture file in not libpcap format.
		m_is_valid = false;
		return false;
	}

	m_is_valid = true;
	return true;
}

/**
 * flip header values.
 * @param toflip
 */
void LibPCapReader::flip(sf_pkthdr_t * toflip)
{
   toflip->ts.sec  = PAL_NTOHL(toflip->ts.sec);
   toflip->ts.msec = PAL_NTOHL(toflip->ts.msec);
   toflip->len     = PAL_NTOHL(toflip->len);
   toflip->caplen  = PAL_NTOHL(toflip->caplen);
}

bool LibPCapReader::ReadPacket(CCapPktRaw *lpPacket)
{
	if(!m_is_valid || !m_is_open)
		return false;

   sf_pkthdr_t pkt_header;
   memset(&pkt_header,0,sizeof(sf_pkthdr_t));

   if (CAP_FTELL_64(m_file_handler) == m_file_size) {
       /* reached end of file - do we loop ?*/
       if (m_loops > 0) {
           rewind(m_file_handler);
           this->init();
          
       }
   }
   int n = fread(&pkt_header,1,sizeof(sf_pkthdr_t),m_file_handler);
   if (n < sizeof(sf_pkthdr_t)) {
	   return false;
   }

   if (m_is_flip) {
		flip(&pkt_header);
   }
   if (pkt_header.len > READER_MAX_PACKET_SIZE) {
       /* cannot read this packet */
	   assert(0);
       return false;
   }

   lpPacket->pkt_len = fread(lpPacket->raw,1,pkt_header.len,m_file_handler);

   lpPacket->time_sec  = pkt_header.ts.sec;
   lpPacket->time_nsec = pkt_header.ts.msec*1000;

   if ( lpPacket->pkt_len < pkt_header.len) {
       lpPacket->pkt_len = 0;
	   return false;
   }

   /* decrease packet limit count */
   if (m_loops > 0) {
       m_loops--;
   }
   lpPacket->pkt_cnt++;
   return true;
}

LibPCapWriter::LibPCapWriter()
{
	m_file_handler = NULL;
	m_timestamp = 0;
    m_is_open = false;
}

LibPCapWriter::~LibPCapWriter()
{
	Close();
}

/**
 * close and release file desc.
*/
void LibPCapWriter::Close()
{
	if (m_is_open) {
		fclose(m_file_handler);
		m_file_handler = NULL;
		m_is_open = false;
	}
}

/**
 * Try to open file for writing.
 * @param name - file nae
 * 
 * @return bool
 */
bool LibPCapWriter::Create(char * name)
{
	if (name == NULL) {
		return false;
	}

	if (m_is_open) {
		return true;
	}

	m_file_handler = CAP_FOPEN_64(name,"wb");
    if (m_file_handler == 0) {
        printf(" ERROR create file \n");
        return(false);
    }
    /* prepare the write counter */
    m_pkt_count = 0;
    return init();
}

/**
 * 
 * Write the libpcap header.
 * 
 * @return bool - true on success
 */
bool LibPCapWriter::init()
{

	// prepare the file header (one time header for each libpcap file)
	// and write it.
	packet_file_header_t header;
    header.magic   = MAGIC_NUM_DONT_FLIP;
    header.version_major  = 0x0002;
    header.version_minor  = 0x0004;
    header.thiszone       = 0;
    header.sigfigs        = 0;
    header.snaplen        = 2000;
	header.linktype       = 1;

    int n = fwrite(&header,1,sizeof(header),m_file_handler);
	if ( n == sizeof(packet_file_header_t )) {
		m_is_open = true;
		return true;
	}
    fclose(m_file_handler);
	return false;
}


/**
 * Write packet to file.
 * Must be called after successfull Create call.
 * @param p
 * @param size
 * 
 * @return bool  - true on success.
 */
bool LibPCapWriter::write_packet(CCapPktRaw * lpPacket)
{
	if (!m_is_open) {
		return false;
	}

	// build the packet libpcap header
    sf_pkthdr_t pkt_header;
	pkt_header.caplen = lpPacket->pkt_len;
	pkt_header.len = lpPacket->pkt_len;
	pkt_header.ts.msec = (lpPacket->time_nsec/1000);
	pkt_header.ts.sec  = lpPacket->time_sec;

	m_timestamp++;

	// write header and then the packet.
	int n = fwrite(&pkt_header,1,sizeof(sf_pkthdr_t),m_file_handler);
    n+= fwrite(lpPacket->raw,1,lpPacket->pkt_len,m_file_handler);

	if (n< ( (int)sizeof(sf_pkthdr_t) + lpPacket->pkt_len)) {
		return false;
	}
    /* advance the counter on success */
    m_pkt_count++;
    return true;
}

uint32_t LibPCapWriter::get_pkt_count() {
    return m_pkt_count;
}