-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathosdp_diag.c
65 lines (54 loc) · 1.47 KB
/
osdp_diag.c
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
/*
* Copyright (c) 2024 Siddharth Chandrasekaran <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <utils/pcap_gen.h>
#include "osdp_common.h"
static void pcap_file_name(struct osdp_pd *pd, char *buf, size_t size)
{
int n;
char *p;
n = snprintf(buf, size, "osdp-trace-%spd-%d-",
is_pd_mode(pd) ? "" : "cp-", pd->address);
n += add_iso8601_utc_datetime(buf + n, size - n);
strcpy(buf + n, ".pcap");
while ((p = strchr(buf, ':')) != NULL) {
*p = '_';
}
}
void osdp_packet_capture_init(struct osdp_pd *pd)
{
pcap_t *cap;
char path[128];
pcap_file_name(pd, path, sizeof(path));
cap = pcap_start(path, OSDP_PACKET_BUF_SIZE, OSDP_PCAP_LINK_TYPE);
if (cap) {
LOG_WRN("Capturing packets to '%s'", path);
LOG_WRN("A graceful teardown of libosdp ctx is required"
" for a complete trace file to be produced.");
} else {
LOG_ERR("Packet capture init failed; check if path '%s'"
" is accessible", path);
}
pd->packet_capture_ctx = (void *)cap;
}
void osdp_packet_capture_finish(struct osdp_pd *pd)
{
pcap_t *cap = pd->packet_capture_ctx;
size_t num_packets;
assert(cap);
num_packets = cap->num_packets;
if (pcap_stop(cap)) {
LOG_ERR("Unable to stop capture (flush/close failed)");
return;
}
LOG_INF("Captured %d packets", num_packets);
}
void osdp_capture_packet(struct osdp_pd *pd, uint8_t *buf, int len)
{
pcap_t *cap = pd->packet_capture_ctx;
assert(cap);
assert(len <= OSDP_PACKET_BUF_SIZE);
pcap_add(cap, buf, len);
}