forked from harpreet-s/xgoldmon
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathxgoldmon.c
164 lines (142 loc) · 4.07 KB
/
xgoldmon.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
/* (C) 2013 by Tobias Engel <[email protected]>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "xgoldmon.h"
int dbglevel = 0;
struct phone2ltable p2t[] =
{
{ "s3", s3_ltable, READLOG_UNESCAPE },
{ "s4", s3_ltable, READLOG_UNESCAPE },
{ "note2", note2_ltable, READLOG_UNESCAPE },
{ "gnex", s2_ltable, 0 },
{ "s2", s2_ltable, 0 },
};
struct gsmtap_inst *init_gsmtap(char *ip_addr)
{
struct gsmtap_inst *gti;
gti = gsmtap_source_init(ip_addr, GSMTAP_UDP_PORT, 0);
gsmtap_source_add_sink(gti);
return gti;
}
void usage(char *cmdname)
{
printf("usage: %s [-t <phone type>] [-l] [-s] [-i <ip address>] [-v] <logfile or device>\n"
" -t: select 's4', 's3', 'gnex', 's2' or 'note2' (default: '%s')\n"
" -l: print baseband log messages\n"
" -m: print cell log information\n"
" -s: set proper serial device attributes\n"
" -i: send gsmtap packets to given ip address (default: 'localhost')\n"
" -v: show debugging messages (more than once for more messages)\n",
cmdname, p2t[0].ptype);
exit(EXIT_SUCCESS);
}
void check_n_perror(int value, const char *message) {
if (value >= 0) return;
perror(message);
exit(EXIT_FAILURE);
}
void set_serial_mode(const char *dev) {
struct termios mode;
bzero(&mode, sizeof(mode));
int fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
check_n_perror(fd, dev);
cfmakeraw(&mode); // raw mode
cfsetspeed(&mode, B115200); // speed
mode.c_cflag = (mode.c_cflag & ~PARENB & ~CSIZE) | CS8; // pass8
mode.c_iflag &= ~ISTRIP;
int ret = tcsetattr(fd, TCSANOW, &mode);
check_n_perror(ret, "try to manually set parameters, tcsetattr failed");
close(fd);
}
struct gsmtap_inst *parse_cmdline(int argc, char *argv[],
int *printlog,
struct phone2ltable **p2ltable,
FILE **logfile)
{
int ret, i = 0;
char *ip_addr = NULL;
bool set_mode = false;
struct gsmtap_inst *gti;
extern char *optarg;
extern int optind;
*p2ltable = NULL;
while((ret = getopt(argc, argv, "lvmshi:t:")) != -1) {
switch(ret) {
case 'l':
*printlog = 1;
break;
case 'm':
*printlog = 2;
break;
case 'v':
dbglevel++;
break;
case 't':
for(i = 0; i < (sizeof(p2t) / sizeof(struct phone2ltable)); i++) {
if(!strcmp(p2t[i].ptype, optarg)) {
*p2ltable = &p2t[i];
break;
}
}
if(!*p2ltable)
usage(argv[0]);
break;
case 's':
set_mode = true;
break;
case 'i':
ip_addr = strdup(optarg);
break;
case 'h':
default:
usage(argv[0]);
break;
}
}
if(!*p2ltable)
*p2ltable = &p2t[0];
if(argc <= optind)
usage(argv[0]);
if(set_mode)
set_serial_mode(argv[optind]);
*logfile = fopen(argv[optind], "r");
if(*logfile == NULL) {
perror(argv[optind]);
exit(EXIT_FAILURE);
}
gti = init_gsmtap(ip_addr);
free(ip_addr);
return gti;
}
int main(int argc, char *argv[])
{
int printlog = 0;
struct phone2ltable *p2ltable = NULL;
FILE *f;
struct gsmtap_inst *gti;
gti = parse_cmdline(argc, argv, &printlog, &p2ltable, &f);
while(1)
parse_logmsg(f, printlog, p2ltable, gti);
}