blob: 549afa901a6849f9296f1b8e5b1b6ac7934f6c16 [file] [log] [blame]
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +08001/* PCAP code from OpenBSC done by Holger Freyther */
2/*
3 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2010 by On-Waves
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <mtp_pcap.h>
24
25#include <sys/time.h>
26
27#include <unistd.h>
28
29#define static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1];
30
31/*
32 * pcap writing of the misdn load
33 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
34 */
35struct pcap_hdr {
36 u_int32_t magic_number;
37 u_int16_t version_major;
38 u_int16_t version_minor;
39 int32_t thiszone;
40 u_int32_t sigfigs;
41 u_int32_t snaplen;
42 u_int32_t network;
43} __attribute__((packed));
44
45struct pcaprec_hdr {
46 u_int32_t ts_sec;
47 u_int32_t ts_usec;
48 u_int32_t incl_len;
49 u_int32_t orig_len;
50} __attribute__((packed));
51
52int mtp_pcap_write_header(int fd)
53{
54 static struct pcap_hdr hdr = {
55 .magic_number = 0xa1b2c3d4,
56 .version_major = 2,
57 .version_minor = 4,
58 .thiszone = 0,
59 .sigfigs = 0,
60 .snaplen = 65535,
61 .network = 141,
62 };
63
64 return write(fd, &hdr, sizeof(hdr));
65}
66
67int mtp_pcap_write_msu(int fd, const u_int8_t *data, int length)
68{
69 int rc_h, rc_d;
70 struct timeval tv;
71 struct pcaprec_hdr payload_header = {
72 .ts_sec = 0,
73 .ts_usec = 0,
74 .incl_len = length,
75 .orig_len = length,
76 };
77
78 gettimeofday(&tv, NULL);
79 payload_header.ts_sec = tv.tv_sec;
80 payload_header.ts_usec = tv.tv_usec;
81
82 rc_h = write(fd, &payload_header, sizeof(payload_header));
83 rc_d = write(fd, data, length);
84
85 return rc_h == sizeof(payload_header) && rc_d == length;
86}