Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 1 | /* 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 Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 7 | * 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 Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 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 |
Holger Hans Peter Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 16 | * |
Holger Hans Peter Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 17 | * 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 Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 19 | * |
| 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 | */ |
| 34 | struct pcap_hdr { |
Holger Hans Peter Freyther | 9ed3e1b | 2010-07-31 05:22:56 +0800 | [diff] [blame] | 35 | uint32_t magic_number; |
Holger Hans Peter Freyther | 585f3d9 | 2010-07-31 04:38:17 +0800 | [diff] [blame] | 36 | uint16_t version_major; |
| 37 | uint16_t version_minor; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 38 | int32_t thiszone; |
Holger Hans Peter Freyther | 9ed3e1b | 2010-07-31 05:22:56 +0800 | [diff] [blame] | 39 | uint32_t sigfigs; |
| 40 | uint32_t snaplen; |
| 41 | uint32_t network; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 42 | } __attribute__((packed)); |
| 43 | |
| 44 | struct pcaprec_hdr { |
Holger Hans Peter Freyther | 9ed3e1b | 2010-07-31 05:22:56 +0800 | [diff] [blame] | 45 | uint32_t ts_sec; |
| 46 | uint32_t ts_usec; |
| 47 | uint32_t incl_len; |
| 48 | uint32_t orig_len; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 49 | } __attribute__((packed)); |
| 50 | |
| 51 | int 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 Freyther | 5aa1701 | 2010-07-31 04:37:26 +0800 | [diff] [blame] | 66 | int mtp_pcap_write_msu(int fd, const uint8_t *data, int length) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 67 | { |
| 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 | } |