add a command line tool for dumping the contents of a capture file
diff --git a/src/Makefile b/src/Makefile
index 1430296..734c658 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,11 +1,14 @@
 CFLAGS=-g -Wall
 LDFLAGS=-losmocore -losmogsm -losmovty -losmoabis -ltalloc
 
-all: osmo-e1-recorder
+all: osmo-e1-recorder osmo-e1cap-dump
 
 osmo-e1-recorder: e1_recorder.o storage.o vty.o
 	$(CC) $(LDFLAGS) -o$@ $^
 
+osmo-e1cap-dump: e1cap_dump.o storage.o
+	$(CC) $(LDFLAGS) -o$@ $^
+
 %.o: %.c
 	$(CC) $(CFLAGS) -o $@ -c $^
 
diff --git a/src/e1cap_dump.c b/src/e1cap_dump.c
new file mode 100644
index 0000000..3e91191
--- /dev/null
+++ b/src/e1cap_dump.c
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <sys/time.h>
+
+#include <osmocom/core/signal.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/application.h>
+
+#include "storage.h"
+#include "recorder.h"
+
+struct e1_recorder g_recorder;
+
+int main(int argc, char **argv)
+{
+	struct osmo_e1cap_file *f;
+	struct osmo_e1cap_pkthdr *pkt;
+
+	printf("sizeof(timeval) = %zu\n", sizeof(struct timeval));
+	printf("sizeof(osmo_e1cap_pkthdr) = %zu\n", sizeof(*pkt));
+
+	if (argc < 2)
+		exit(2);
+
+	f = osmo_e1cap_open(NULL, argv[1]);
+	if (!f)
+		exit(1);
+
+	while ((pkt = osmo_e1cap_read_next(f))) {
+		printf("%lu:%lu %02u/%02u %u (%u): %s\n",
+			pkt->ts.tv_sec, pkt->ts.tv_usec,
+			pkt->line_nr, pkt->ts_nr, pkt->capture_mode,
+			pkt->len,
+			osmo_hexdump_nospc(pkt->data, pkt->len));
+		talloc_free(pkt);
+	}
+}
diff --git a/src/storage.c b/src/storage.c
index 2b9fb9f..7248a59 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -6,6 +6,7 @@
 #include <sys/stat.h>
 
 #include <osmocom/core/msgb.h>
+#include <osmocom/core/talloc.h>
 #include <osmocom/abis/e1_input.h>
 
 #include "storage.h"
@@ -84,3 +85,56 @@
 
 	return 0;
 }
+
+/* reading */
+
+
+struct osmo_e1cap_file {
+	int fd;
+};
+
+#define OSMO_E1REC_ALLOC_SIZE	1024
+
+struct osmo_e1cap_file *osmo_e1cap_open(void *ctx, const char *path)
+{
+	struct osmo_e1cap_file *f = talloc_zero(ctx, struct osmo_e1cap_file);
+	if (!f)
+		return NULL;
+
+	f->fd = open(path, O_RDONLY);
+	if (f->fd < 0) {
+		talloc_free(f);
+		return NULL;
+	}
+	return f;
+}
+
+struct osmo_e1cap_pkthdr *osmo_e1cap_read_next(struct osmo_e1cap_file *f)
+{
+	struct osmo_e1cap_pkthdr *pkt = talloc_zero_size(f, OSMO_E1REC_ALLOC_SIZE);
+	int rc;
+
+	if (!pkt)
+		return NULL;
+
+	/* read header */
+	rc = read(f->fd, (uint8_t *)pkt, sizeof(*pkt));
+	if (rc < sizeof(*pkt)) {
+		talloc_free(pkt);
+		return NULL;
+	}
+	pkt->len = ntohl(pkt->len);
+	printf("len=%u\n", pkt->len);
+	/* read data */
+	if (pkt->len > OSMO_E1REC_ALLOC_SIZE - sizeof(*pkt)) {
+		pkt = talloc_realloc_size(f, pkt, sizeof(*pkt) + pkt->len);
+		if (!pkt)
+			return NULL;
+	}
+	rc = read(f->fd, (uint8_t*)(pkt+1), pkt->len);
+	if (rc < pkt->len) {
+		talloc_free(pkt);
+		return NULL;
+	}
+	return pkt;
+}
diff --git a/src/storage.h b/src/storage.h
index df58ff9..d256fdc 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -30,3 +30,7 @@
 struct e1inp_ts;
 
 int e1frame_store(struct e1inp_ts *ts, struct msgb *msg, enum osmo_e1cap_capture_mode mode);
+
+struct osmo_e1cap_file;
+struct osmo_e1cap_file *osmo_e1cap_open(void *ctx, const char *path);
+struct osmo_e1cap_pkthdr *osmo_e1cap_read_next(struct osmo_e1cap_file *f);