-
Notifications
You must be signed in to change notification settings - Fork 32
/
diag_import.c
166 lines (142 loc) · 3.03 KB
/
diag_import.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include "diag_input.h"
#include "bit_func.h"
#include "session.h"
#include <stdlib.h>
void process_file(char *infile_name, int do_init);
static void usage(const char *progname, const char *reason)
{
printf("%s\n", reason);
printf("Usage: %s [-f <filelist>] [filenames]\n", progname);
printf(" -g <target> - Target host for GSMTAP UDP stream\n");
printf(" -p <pcapfile> - Write to PCAP file\n");
printf(" -f <filelist> - Read list of input files from <filelist>\n");
printf(" -i - Initialize device\n");
printf(" -v - Verbose messages\n");
printf(" [filenames] - Read DIAG data from [filenames]\n");
exit(1);
}
static void
chop_newline(char *line)
{
int newline_pos = strlen(line) - 1;
if (newline_pos >= 0 && line[newline_pos] == '\n')
{
line[newline_pos] = '\0';
}
}
int main(int argc, char *argv[])
{
char infile_name[FILENAME_MAX];
FILE *filelist = NULL;
char *filelist_name = NULL;
char *gsmtap_target = NULL;
char *pcap_target = NULL;
uint32_t appid = 0;
int ch;
long sid = 0;
long cid = 0;
int line = 0;
int init = 0;
msg_verbose = 0;
while ((ch = getopt(argc, argv, "p:g:f:vi")) != -1) {
switch (ch) {
case 'g':
gsmtap_target = strdup(optarg);
break;
case 'p':
pcap_target = strdup(optarg);
break;
case 'f':
filelist_name = strdup(optarg);
break;
case 'i':
init = 1;
break;
case 'v':
msg_verbose++;
break;
case '?':
default:
usage(argv[0], "Invalid arguments");
}
}
argc -= optind;
argv += optind;
if (filelist_name == NULL && argc == 0)
{
errx(1, "Invalid arguments");
}
diag_init(sid, cid, gsmtap_target, pcap_target, NULL, appid);
printf("PARSER_OK\n");
fflush(stdout);
// Handle files passed to command line first
while (argc > 0)
{
process_file(argv[0], init);
argc--;
argv++;
};
// Handle file list
if (filelist_name)
{
filelist = fopen(filelist_name, "rb");
if (!filelist)
{
err(1, "Cannot open file list: %s", filelist_name);
}
while (!feof(filelist))
{
char *ret = fgets(infile_name, sizeof(infile_name), filelist);
++line;
if (ferror(filelist))
{
err(1, "Error parsing file list %s:%d", filelist_name, line);
}
if (ret) {
chop_newline(infile_name);
process_file(infile_name, init);
}
}
fclose(filelist);
}
diag_destroy(&sid, &cid);
return 0;
}
void
process_file(char *infile_name, int do_init)
{
uint8_t msg[4096];
FILE *infile = NULL;
unsigned len = 0;
if (strcmp(infile_name, "-") == 0)
{
infile = stdin;
} else
{
infile = fopen(infile_name, "rb+");
}
if (!infile)
{
err(1, "Cannot open input file: %s", infile_name);
}
if (do_init)
diag_set_log(infile);
diag_set_filename(infile_name);
for (;;) {
len = fread_unescape(infile, msg, sizeof(msg));
if (len < 1) {
break;
}
/* Terminate message with standard GSM padding */
if (len < sizeof(msg) - 1) {
msg[len] = 0x2b;
}
handle_diag(msg, len);
}
fclose(infile);
}