diff options
author | Harald Welte <laforge@gnumonks.org> | 2016-07-04 09:53:44 +0200 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2016-07-04 10:25:02 +0200 |
commit | 47bee5bed50d1b6d6004428a2fac50f57764e7bd (patch) | |
tree | 852a5d2072fe0ece1a8b2742733ee3d38568b21e | |
parent | c1cf14cae8a4ff8d60976eb188344595755448d5 (diff) |
Add osmo_pcap_lapd_set_fd() function
This way the caller can hand in an already-open file descriptor, rather
than calling osmo_pcap_lapd_open() with a file path name.
Change-Id: Ic7193907a96b0e2284cb6a8a942d2bf0fb95d7e4
-rw-r--r-- | include/osmocom/abis/lapd_pcap.h | 1 | ||||
-rw-r--r-- | src/input/lapd_pcap.c | 30 |
2 files changed, 23 insertions, 8 deletions
diff --git a/include/osmocom/abis/lapd_pcap.h b/include/osmocom/abis/lapd_pcap.h index 1c0d555..e1b587e 100644 --- a/include/osmocom/abis/lapd_pcap.h +++ b/include/osmocom/abis/lapd_pcap.h @@ -5,6 +5,7 @@ #define OSMO_LAPD_PCAP_OUTPUT 1 int osmo_pcap_lapd_open(char *filename, mode_t mode); +int osmo_pcap_lapd_set_fd(int fd); int osmo_pcap_lapd_write(int fd, int direction, struct msgb *msg); int osmo_pcap_lapd_close(int fd); diff --git a/src/input/lapd_pcap.c b/src/input/lapd_pcap.c index c83bc60..2a635cc 100644 --- a/src/input/lapd_pcap.c +++ b/src/input/lapd_pcap.c @@ -75,10 +75,9 @@ osmo_static_assert(offsetof(struct pcap_lapdhdr, addr) == 6, addr_offset); osmo_static_assert(offsetof(struct pcap_lapdhdr, protocol) == 14, proto_offset); osmo_static_assert(sizeof(struct pcap_lapdhdr) == 16, lapd_header_size); -int osmo_pcap_lapd_open(char *filename, mode_t mode) +int osmo_pcap_lapd_set_fd(int fd) { - int fd; - struct pcap_hdr pcap_header = { + struct pcap_hdr pcap_header = { .magic_number = 0xa1b2c3d4, .version_major = 2, .version_minor = 4, @@ -88,6 +87,21 @@ int osmo_pcap_lapd_open(char *filename, mode_t mode) .network = DLT_LINUX_LAPD, }; + if (write(fd, &pcap_header, sizeof(pcap_header)) + != sizeof(pcap_header)) { + LOGP(DLLAPD, LOGL_ERROR, "cannot write PCAP header: %s\n", + strerror(errno)); + close(fd); + return -1; + } + + return 0; +} + +int osmo_pcap_lapd_open(char *filename, mode_t mode) +{ + int fd, rc; + LOGP(DLLAPD, LOGL_NOTICE, "opening LAPD pcap file `%s'\n", filename); fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, mode); @@ -96,13 +110,13 @@ int osmo_pcap_lapd_open(char *filename, mode_t mode) strerror(errno)); return -1; } - if (write(fd, &pcap_header, sizeof(pcap_header)) - != sizeof(pcap_header)) { - LOGP(DLLAPD, LOGL_ERROR, "cannot write PCAP header: %s\n", - strerror(errno)); + + rc = osmo_pcap_lapd_set_fd(fd); + if (rc < 0) { close(fd); - return -1; + return rc; } + return fd; } |