changeset 404:ceb71478414d

tfo: find-is-hdr program written
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Mar 2023 18:16:22 +0000
parents 50c0fac9a4a8
children f7df0f4d7d4f
files .hgignore tfo/Makefile tfo/find-is-hdr.c
diffstat 3 files changed, 97 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Jan 15 00:54:33 2023 +0000
+++ b/.hgignore	Sat Mar 11 18:16:22 2023 +0000
@@ -82,3 +82,5 @@
 ^pirollback/dumpjournal$
 ^pirollback/inopath$
 ^pirollback/rollback$
+
+^tfo/find-is-hdr$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tfo/Makefile	Sat Mar 11 18:16:22 2023 +0000
@@ -0,0 +1,14 @@
+CC=	gcc
+CFLAGS=	-O2
+STD=	find-is-hdr
+PROGS=	${STD}
+
+all:	${PROGS}
+
+${STD}:
+	${CC} ${CFLAGS} -o $@ $@.c
+
+find-is-hdr:	find-is-hdr.c
+
+clean:
+	rm -f ${PROGS} *.o *errs *.out
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tfo/find-is-hdr.c	Sat Mar 11 18:16:22 2023 +0000
@@ -0,0 +1,81 @@
+/*
+ * This program reads a binary file containing a G.711 PCM stream capture
+ * and looks for an IS_Header pattern as defined in ETSI TS 101 504
+ * (GSM 08.62) section A.1.2.  The objective is to analyze PCM streams
+ * originating from extant commercial GSM network operators and see if
+ * they implement in-band TFO.
+ */
+
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static char *pcmfile;
+static size_t pcm_file_size;
+static u_char *filemap;
+
+static const u_char hdr_pattern[20] =	{0, 1, 0, 1, 0, 1, 1, 0, 1, 0,
+					 0, 1, 1, 0, 1, 0, 1, 0, 0, 1};
+
+static void
+mmap_pcm_file()
+{
+	int fd;
+	struct stat st;
+
+	fd = open(pcmfile, O_RDONLY);
+	if (fd < 0) {
+		perror(pcmfile);
+		exit(1);
+	}
+	fstat(fd, &st);
+	if (!S_ISREG(st.st_mode)) {
+		fprintf(stderr, "error: %s is not a regular file\n", pcmfile);
+		exit(1);
+	}
+	pcm_file_size = st.st_size;
+	if (pcm_file_size < 320) {
+		fprintf(stderr, "error: %s is too short\n", pcmfile);
+		exit(1);
+	}
+	filemap = mmap(NULL, pcm_file_size, PROT_READ, MAP_PRIVATE, fd, 0L);
+	if (filemap == MAP_FAILED) {
+		perror("mmap");
+		exit(1);
+	}
+	close(fd);
+}
+
+static void
+try_offset(offset)
+	size_t offset;
+{
+	unsigned n;
+
+	for (n = 0; n < 20; n++) {
+		if ((filemap[offset + n * 16] & 1) != hdr_pattern[n])
+			return;
+	}
+	printf("Found IS_Header at offset %lu\n", (u_long) offset);
+}
+
+main(argc, argv)
+	char **argv;
+{
+	size_t offset, endoff;
+
+	if (argc != 2) {
+		fprintf(stderr, "usage: %s pcm-capture-file\n", argv[0]);
+		exit(1);
+	}
+	pcmfile = argv[1];
+	mmap_pcm_file();
+	endoff = pcm_file_size - 320;
+	for (offset = 0; offset <= endoff; offset++)
+		try_offset(offset);
+	exit(0);
+}