comparison tfo/find-is-hdr.c @ 14:98c0881c2af0

tfo/find-is-hdr: move here from freecalypso-reveng This little program was developed in freecalypso-reveng Hg repository in 2023-03, when we didn't have a separate repository for network-side reverse eng, but now that we do have separate repositories for FreeCalypso (mobile side) vs network side reverse eng, move TFO RE work to the proper place.
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 24 May 2024 21:18:22 +0000
parents
children
comparison
equal deleted inserted replaced
13:e34029530a80 14:98c0881c2af0
1 /*
2 * This program reads a binary file containing a G.711 PCM stream capture
3 * and looks for an IS_Header pattern as defined in ETSI TS 101 504
4 * (GSM 08.62) section A.1.2. The objective is to analyze PCM streams
5 * originating from extant commercial GSM network operators and see if
6 * they implement in-band TFO.
7 */
8
9 #include <sys/types.h>
10 #include <sys/file.h>
11 #include <sys/stat.h>
12 #include <sys/mman.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16
17 static char *pcmfile;
18 static size_t pcm_file_size;
19 static u_char *filemap;
20
21 static const u_char hdr_pattern[20] = {0, 1, 0, 1, 0, 1, 1, 0, 1, 0,
22 0, 1, 1, 0, 1, 0, 1, 0, 0, 1};
23
24 static void
25 mmap_pcm_file()
26 {
27 int fd;
28 struct stat st;
29
30 fd = open(pcmfile, O_RDONLY);
31 if (fd < 0) {
32 perror(pcmfile);
33 exit(1);
34 }
35 fstat(fd, &st);
36 if (!S_ISREG(st.st_mode)) {
37 fprintf(stderr, "error: %s is not a regular file\n", pcmfile);
38 exit(1);
39 }
40 pcm_file_size = st.st_size;
41 if (pcm_file_size < 320) {
42 fprintf(stderr, "error: %s is too short\n", pcmfile);
43 exit(1);
44 }
45 filemap = mmap(NULL, pcm_file_size, PROT_READ, MAP_PRIVATE, fd, 0L);
46 if (filemap == MAP_FAILED) {
47 perror("mmap");
48 exit(1);
49 }
50 close(fd);
51 }
52
53 static void
54 try_offset(offset)
55 size_t offset;
56 {
57 unsigned n;
58
59 for (n = 0; n < 20; n++) {
60 if ((filemap[offset + n * 16] & 1) != hdr_pattern[n])
61 return;
62 }
63 printf("Found IS_Header at offset %lu (0x%lx)\n", (u_long) offset,
64 (u_long) offset);
65 }
66
67 main(argc, argv)
68 char **argv;
69 {
70 size_t offset, endoff;
71
72 if (argc != 2) {
73 fprintf(stderr, "usage: %s pcm-capture-file\n", argv[0]);
74 exit(1);
75 }
76 pcmfile = argv[1];
77 mmap_pcm_file();
78 endoff = pcm_file_size - 320;
79 for (offset = 0; offset <= endoff; offset++)
80 try_offset(offset);
81 exit(0);
82 }