blob: 5901e25a03e64655d90d4bbd36ec90084a454f45 [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 *
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +01007 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080010 * (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
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +010015 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080016 *
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080019 *
20 */
21
22#include <mtp_pcap.h>
23
24#include <sys/time.h>
25
26#include <unistd.h>
27
28#define static_assert(exp, name) typedef int dummy##name [(exp) ? 1 : -1];
29
30/*
31 * pcap writing of the misdn load
32 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
33 */
34struct pcap_hdr {
Holger Hans Peter Freyther9ed3e1b2010-07-31 05:22:56 +080035 uint32_t magic_number;
Holger Hans Peter Freyther585f3d92010-07-31 04:38:17 +080036 uint16_t version_major;
37 uint16_t version_minor;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080038 int32_t thiszone;
Holger Hans Peter Freyther9ed3e1b2010-07-31 05:22:56 +080039 uint32_t sigfigs;
40 uint32_t snaplen;
41 uint32_t network;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080042} __attribute__((packed));
43
44struct pcaprec_hdr {
Holger Hans Peter Freyther9ed3e1b2010-07-31 05:22:56 +080045 uint32_t ts_sec;
46 uint32_t ts_usec;
47 uint32_t incl_len;
48 uint32_t orig_len;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080049} __attribute__((packed));
50
51int mtp_pcap_write_header(int fd)
52{
53 static struct pcap_hdr hdr = {
54 .magic_number = 0xa1b2c3d4,
55 .version_major = 2,
56 .version_minor = 4,
57 .thiszone = 0,
58 .sigfigs = 0,
59 .snaplen = 65535,
60 .network = 141,
61 };
62
63 return write(fd, &hdr, sizeof(hdr));
64}
65
Holger Hans Peter Freyther5aa17012010-07-31 04:37:26 +080066int mtp_pcap_write_msu(int fd, const uint8_t *data, int length)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080067{
68 int rc_h, rc_d;
69 struct timeval tv;
70 struct pcaprec_hdr payload_header = {
71 .ts_sec = 0,
72 .ts_usec = 0,
73 .incl_len = length,
74 .orig_len = length,
75 };
76
77 gettimeofday(&tv, NULL);
78 payload_header.ts_sec = tv.tv_sec;
79 payload_header.ts_usec = tv.tv_usec;
80
81 rc_h = write(fd, &payload_header, sizeof(payload_header));
82 rc_d = write(fd, data, length);
83
84 return rc_h == sizeof(payload_header) && rc_d == length;
85}