summaryrefslogtreecommitdiffstats
path: root/src/common/pcap.cpp
blob: 9b360a3ea496a556298956056a1f5237ca803cbd (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

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db7
/*
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 */
       printf("ERROR packet is too big, bigger than %d \n",READER_MAX_PACKET_SIZE);
       exit(-1);
       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;
}