e1_input: Allow to change the pcap fd and/or unset it

This will update the pcap fd in all open lines and close
the previous one (if applicable).

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
Change-Id: I5c7dd740ba0a90b40c69a53b3dcc9d6d6a98f660
diff --git a/src/e1_input.c b/src/e1_input.c
index ad4e25c..379cc53 100644
--- a/src/e1_input.c
+++ b/src/e1_input.c
@@ -142,7 +142,7 @@
 
 int e1_set_pcap_fd(int fd)
 {
-	struct pcap_hdr header = {
+	const struct pcap_hdr header = {
 		.magic_number	= 0xa1b2c3d4,
 		.version_major	= 2,
 		.version_minor	= 4,
@@ -151,9 +151,34 @@
 		.snaplen	= 65535,
 		.network	= DLT_LINUX_LAPD,
 	};
+	struct e1inp_line *line;
+	int i;
 
+	/* write header */
+	if (fd >= 0) {
+		int rc = write(fd, &header, sizeof(header));
+		if (rc < 0)
+			return rc;
+	}
+
+	/* update fd in all lines in our global list of e1 lines */
+	llist_for_each_entry(line, &e1inp_line_list, list) {
+		/* Set the PCAP file descriptor for all timeslots that have
+		 * software LAPD instances, to ensure the osmo_lapd_pcap code is
+		 * used to write PCAP files (if requested) */
+		for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
+			struct e1inp_ts *e1i_ts = &line->ts[i];
+			if (e1i_ts->lapd)
+				e1i_ts->lapd->pcap_fd = fd;
+		}
+	}
+
+	/* close previous and update global */
+	if (pcap_fd >= 0)
+		close(pcap_fd);
 	pcap_fd = fd;
-	return write(pcap_fd, &header, sizeof(header));
+
+	return 0;
 }
 
 /* This currently only works for the D-Channel */