aboutsummaryrefslogtreecommitdiffstats
path: root/src/telnet_parser.l
blob: e4327191dbb3a4418d240ce894c0306dfe7e545f (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
 * 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.
 *
 */
/*
 * I'm lazy and will not introduce lemon to this game. Our telnet
 * interface is matching line based so we can have a pattern that
 * is matching a line and everyone will be happy.
 */

%option never-interactive
%option noyywrap
%option	reentrant

%{
#include <string.h>
#include <openbsc/telnet_interface.h>

extern char *strndup(const char *s, size_t n);
extern void telnet_write_help(int);
extern void telnet_close_client(struct bsc_fd*);
extern void telnet_error_client(int fd);
extern void telnet_page(struct telnet_connection *con, const char *imsi, int page);
extern void telnet_call(struct telnet_connection *con, const char *imsi,
			const char* origin);
extern void telnet_put_channel(struct telnet_connection*, const char *imsi);
extern void telnet_get_channel(struct telnet_connection*, const char *imsi);
extern void telnet_send_gsm_48(struct telnet_connection*);
extern void telnet_send_gsm_11(struct telnet_connection*);
extern void telnet_list_channels(struct telnet_connection*);

static const int PAGE_LEN = 5; /* "page " */
static const int CALL_LEN = 5; /* "call " */
static const int PUT_LEN = 12; /* "put_channel " */
static const int GET_LEN = 12; /* "get_channel " */
static const int NET_LEN = 3;  /* "48 " "11 " */
static const int SHOW_LEN = 5; /* "show " */

#define YY_EXTRA_TYPE struct telnet_connection*

/* the string is null terminated */
static int parse_hex(char *hex)
{
	int byte;
	sscanf(hex, "%x", &byte);
	return byte;
}

#define PREPARE_STRING(len) \
		yytext[yyleng-1] = '\0'; \
                char *str = yytext + len; \
		char *pag = strstr(str, "\r"); \
		if (pag) pag[0] = '\0'; \
		pag = strstr(str, "\n"); \
		if (pag) pag[0] = '\0';

%}

CMD_HELP	"help"
CMD_EXIT	"exit"
CMD_CLOSE	"close"
CMD_PAGE	"page"
CMD_GET_CHANNEL	"get_channel"
CMD_PUT_CHANNEL "put_channel"
CMD_CALL	"call"
CMD_48		"48"
CMD_11		"11"
CMD_SHOW	"show"

LINE_END	\n|\r\n
HEX		[0][x][0-9a-zA-Z][0-9a-zA-Z]

%s  READ_HEX_BYTES

%%
{CMD_HELP}{LINE_END}		{telnet_write_help(yyextra->fd.fd); yyterminate();}
{CMD_EXIT}{LINE_END}		{telnet_close_client(&yyextra->fd); yyterminate();}
{CMD_CLOSE}{LINE_END}		{telnet_close_client(&yyextra->fd); yyterminate();}
{CMD_SHOW}{LINE_END}		{telnet_list_channels(yyextra); yyterminate();}
{CMD_PAGE}[ ][0-9]+{LINE_END} {
				    PREPARE_STRING(PAGE_LEN)
                                    telnet_page(yyextra, str, 0);
				    yyterminate();
				}
{CMD_PAGE}[ ][0-9]+[ ][0-2]{LINE_END} {
				    PREPARE_STRING(PAGE_LEN)
				    char *sp = strstr(str, " ");
				    sp[0] = '\0';
                                    telnet_page(yyextra, str, atoi(sp+1));
				    yyterminate();
				}
{CMD_PUT_CHANNEL}[ ][0-9]+{LINE_END} {
				    PREPARE_STRING(PUT_LEN)
                                    telnet_put_channel(yyextra, str);
				    yyterminate();
				}
{CMD_GET_CHANNEL}[ ][0-9]+{LINE_END} {
				    PREPARE_STRING(GET_LEN)
                                    telnet_get_channel(yyextra, str);
				    yyterminate();
				}
{CMD_CALL}[ ][0-9]+[ ][0-9]+{LINE_END} {
				    PREPARE_STRING(CALL_LEN)
				    char *sp = strstr(str, " ");
				    sp[0] = '\0';
                                    telnet_call(yyextra, str, sp+1);
				    yyterminate();
				}
{CMD_CALL}[ ][0-9]+{LINE_END} {
				    PREPARE_STRING(CALL_LEN)
                                    telnet_call(yyextra, str, NULL);
				    yyterminate();
				}
<READ_HEX_BYTES>{HEX}		{
				    if (yyextra->read >= sizeof(yyextra->commands)) {
					yyterminate();
				    }
				    yytext[yyleng] = '\0';
				    yyextra->commands[yyextra->read++] = parse_hex(yytext+2);
				}
<READ_HEX_BYTES>{LINE_END}	{
				    if (yyextra->command == TELNET_COMMAND_11) {
					telnet_send_gsm_11(yyextra);
				    } else if (yyextra->command == TELNET_COMMAND_48) {
					telnet_send_gsm_48(yyextra);
				    }

				    if (yyextra->imsi) {
					free(yyextra->imsi);
					yyextra->imsi = NULL;
				    }
				    yyterminate();
				}
<INITIAL>{CMD_48}[ ][0-9]+	{
				    BEGIN READ_HEX_BYTES;
				    yyextra->read = 0;
				    yyextra->command = TELNET_COMMAND_48;
				    yytext[yyleng-1] = '\0';
				    yyextra->imsi = strdup(yytext);
				}

<INITIAL>{CMD_11}[ ][0-9]+	{
				    BEGIN READ_HEX_BYTES;
				    yyextra->read = 0;
				    yyextra->command = TELNET_COMMAND_11;
				    yytext[yyleng-1] = '\0';
				    yyextra->imsi = strdup(yytext);
				}



[ \t\r\n]			/* Ommit */
.				{ telnet_error_client(yyextra->fd.fd); yyterminate(); }

%%

void telnet_parse(struct telnet_connection *conn, char *buf)
{
	yyscan_t scanner;
	yylex_init(&scanner);
	yyset_extra(conn, scanner);
	yy_scan_string(buf, scanner);
	yylex(scanner);
	yylex_destroy(scanner);

	if (conn->imsi) {
		free(conn->imsi);
		conn->imsi = NULL;
	}
}

__attribute__((unused)) void telnet_unused(void)
{
	yyunput(0, 0, 0);
	input(0);
}