blob: a25c9bb43d3ef70078a0aede9b8f850be247737a [file] [log] [blame]
Holger Freyther219518d2009-01-02 22:04:43 +00001/* minimalistic telnet/network interface it might turn into a wire interface */
2/* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <malloc.h>
24#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
27
28#include <openbsc/telnet_interface.h>
Holger Freytherae61cae2009-01-04 03:46:01 +000029#include <openbsc/gsm_subscriber.h>
Holger Freytherf87573d2009-01-04 03:49:41 +000030#include <openbsc/chan_alloc.h>
Holger Freyther3ae8fd22009-01-04 03:50:40 +000031#include <openbsc/gsm_04_08.h>
Holger Freyther868b8cd2009-01-04 03:57:27 +000032#include <openbsc/msgb.h>
Holger Freyther219518d2009-01-02 22:04:43 +000033
34extern void telnet_parse(struct telnet_connection *connection, char *line);
35
Holger Freytherae61cae2009-01-04 03:46:01 +000036#define WRITE_CONNECTION(fd, msg...) \
37 int ret; \
38 char buf[4096]; \
39 snprintf(buf, sizeof(buf), msg); \
40 ret = write(fd, buf, strlen(buf));
41
42
Holger Freyther219518d2009-01-02 22:04:43 +000043/* per connection data */
44LLIST_HEAD(active_connections);
45
46/* per network data */
47static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
48static struct bsc_fd server_socket = {
49 .when = BSC_FD_READ,
50 .cb = telnet_new_connection,
51 .priv_nr = 0,
52};
53
54void telnet_init(struct gsm_network *network, int port) {
55 struct sockaddr_in sock_addr;
56 int fd;
57
58 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
59
60 if (fd < 0) {
61 perror("Telnet interface socket creation failed");
62 return;
63 }
64
65 memset(&sock_addr, 0, sizeof(sock_addr));
66 sock_addr.sin_family = AF_INET;
67 sock_addr.sin_port = htons(port);
68 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
69
70 if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) {
71 perror("Telnet interface failed to bind");
72 return;
73 }
74
75 if (listen(fd, 0) < 0) {
76 perror("Telnet interface failed to listen");
77 return;
78 }
79
80 server_socket.data = network;
81 server_socket.fd = fd;
82 bsc_register_fd(&server_socket);
83}
84
85void telnet_write_help(int fd) {
86 int ret;
87 static char *msg =
88 "Help for the ad-hoc telnet command line interface\n"
89 "The generic pattern is CMD LEN DATA\\n or just CMD\n"
90 "where CMD is one of the following:\n"
91 "help\n"
92 "page IMSI (type)\n"
93 "call IMSI (number)\n"
94 "get_channel IMSI Add use count on an active channel\n"
95 "put_channel IMSI Remove use count on an active channel\n"
Holger Freytherae61cae2009-01-04 03:46:01 +000096 "show This will show the channel allocation\n"
Holger Freyther868b8cd2009-01-04 03:57:27 +000097 "48 IMSI 0xAB 0xEF...Send GSM 04.08. proto and msg byte then data\n"
Holger Freyther219518d2009-01-02 22:04:43 +000098 "11 IMSI 0xAB 0xEF...Send GSM 04.11\n";
99
100 ret = write(fd, msg, strlen(msg));
101}
102
103static void print_welcome(int fd) {
104 int ret;
105 static char *msg =
106 "Welcome to the OpenBSC Control interface\n"
107 "Copyright (C) 2008, 2009 Harald Welte\n"
108 "Contributions by Daniel Willmann, Jan Lübbe, "
109 "Stefan Schmidt, Holger Freyther\n\n"
110 "License GPLv2+: GNU GPL version 2 or later "
111 "<http://gnu.org/licenses/gpl.html>\n"
112 "This is free software: you are free to change "
113 "and redistribute it.\n"
114 "There is NO WARRANTY, to the extent permitted "
115 "by law.\nType \"help\" to get a short introduction.\n";
116
117 ret = write(fd, msg, strlen(msg));
118}
119
120int telnet_close_client(struct bsc_fd *fd) {
121 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
122
123 close(fd->fd);
124 bsc_unregister_fd(fd);
125 llist_del(&conn->entry);
126 free(conn);
127 return 0;
128}
129
130void telnet_error_client(int fd) {
131 int ret;
132 static char *msg = "Something went wrong. Please try again.\n";
133
134 printf("Error\n");
135 ret = write(fd, msg, strlen(msg));
136}
137
138void telnet_page(struct telnet_connection *connection, const char *imsi, int page) {
139 printf("going to page: '%s' %d\n", imsi, page);
140}
141
Holger Freytherf87573d2009-01-04 03:49:41 +0000142static struct gsm_lchan* find_channel(struct gsm_bts *bts, const char *imsi,
143 const char **error, int fd) {
144 int ret;
145 struct gsm_lchan *lchan;
146 struct gsm_subscriber *subscr;
147
148 subscr = subscr_get_by_imsi(imsi);
149 if (!subscr) {
150 ret = write(fd, error[0], strlen(error[0]));
151 return NULL;
152 }
153
154 lchan = lchan_find(bts, subscr);
155 if (!lchan)
156 ret = write(fd, error[1], strlen(error[1]));
157
158 subscr_put(subscr);
159 return lchan;
160}
161
Holger Freyther219518d2009-01-02 22:04:43 +0000162void telnet_put_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000163 static const char* error[] = {
164 "put_channel: IMSI not found\n",
165 "put_channel: No channel allocated for IMSI\n" };
166 struct gsm_bts *bts = &connection->network->bts[connection->bts];
167 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
168
169 if (!lchan)
170 return;
171
172 put_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000173}
174
175void telnet_get_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000176 static const char* error[] = {
177 "get_channel: IMSI not found\n",
178 "get_channel: No channel allocated for IMSI\n" };
179 struct gsm_bts *bts = &connection->network->bts[connection->bts];
180 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
181
182 if (!lchan)
183 return;
184
185 use_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000186}
187
188void telnet_call(struct telnet_connection *connection, const char* imsi,
189 const char *origin) {
Holger Freyther3ae8fd22009-01-04 03:50:40 +0000190 static const char* error[] = {
191 "call: IMSI not found\n",
192 "call: No channel allocated for IMSI\n" };
193 struct gsm_bts *bts = &connection->network->bts[connection->bts];
194 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
195
196 if (!lchan)
197 return;
198
199 /* TODO: add the origin */
200 gsm48_cc_tx_setup(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000201}
202
203void telnet_send_gsm_48(struct telnet_connection *connection) {
Holger Freyther868b8cd2009-01-04 03:57:27 +0000204 static const char* error[] = {
205 "48: IMSI not found\n",
206 "48: No channel allocated for IMSI\n" };
Holger Freyther5e76ce62009-01-04 20:15:12 +0000207 int ret;
Holger Freyther868b8cd2009-01-04 03:57:27 +0000208 struct gsm_bts *bts = &connection->network->bts[connection->bts];
209 struct gsm_lchan *lchan = find_channel(bts, connection->imsi, error, connection->fd.fd);
210
211 if (!lchan)
212 return;
213
Holger Freyther5e76ce62009-01-04 20:15:12 +0000214 if (connection->read < 2) {
215 static const char *msg = "48: Need at least two bytes";
216 ret = write(connection->fd.fd, msg, strlen(msg));
217 return;
218 }
219
Holger Freyther868b8cd2009-01-04 03:57:27 +0000220 struct msgb *msg = gsm48_msgb_alloc();
221 struct gsm48_hdr *gh;
222 int i;
223
224 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + connection->read-2);
225 msg->lchan = lchan;
226
227 gh->proto_discr = connection->commands[0];
228 gh->msg_type = connection->commands[1];
229 for (i = 2; i < connection->read; ++i)
230 gh->data[i-2] = connection->commands[i];
231
232 return gsm48_sendmsg(msg);
Holger Freyther219518d2009-01-02 22:04:43 +0000233}
234
235void telnet_send_gsm_11(struct telnet_connection *connection) {
236 printf("sending gsm04.11 message\n");
237}
238
Holger Freytherae61cae2009-01-04 03:46:01 +0000239static void show_bts(int fd, struct gsm_bts *bts) {
240 WRITE_CONNECTION(fd,
241 "BTS #%u on link %u LOC: %u TRX: %d CCCH0: arfcn:%u,#%u\n",
242 bts->nr, bts->bts_nr, bts->location_area_code,
243 bts->num_trx, bts->c0->arfcn, bts->c0->nr)
244}
245
246static void show_trx(int fd, struct gsm_bts_trx *trx) {
247 WRITE_CONNECTION(fd,
248 " TRX: %u ARFCN: %u\n",
249 trx->nr, trx->arfcn)
250}
251
252static void show_ts(int fd, struct gsm_bts_trx_ts *ts) {
253 WRITE_CONNECTION(fd,
Harald Welted4fc1b22009-01-04 16:11:31 +0000254 " TS: #%u pchan: %12s flags: %u\n",
255 ts->nr, gsm_pchan_name(ts->pchan), ts->flags);
Holger Freytherae61cae2009-01-04 03:46:01 +0000256}
257
258static void show_lchan(int fd, struct gsm_lchan *lchan) {
259 struct gsm_subscriber *subscr = lchan->subscr;
260 WRITE_CONNECTION(fd,
Harald Welted4fc1b22009-01-04 16:11:31 +0000261 " LCHAN: #%u type: %7s subscriber: %s/%s/%s use: %d loc: %p\n",
262 lchan->nr, gsm_lchan_name(lchan->type),
Holger Freytherae61cae2009-01-04 03:46:01 +0000263 subscr ? subscr->imsi : "na",
264 subscr ? subscr->tmsi : "na",
265 subscr ? subscr->name : "na",
266 lchan->use_count, lchan->loc_operation);
267}
268
269void telnet_list_channels(struct telnet_connection *connection) {
270 int bts_no, trx, lchan_no, ts_no;
271 struct gsm_network *network = connection->network;
272
273 for (bts_no = 0; bts_no < network->num_bts; ++bts_no) {
274 struct gsm_bts *bts = &network->bts[bts_no];
275 show_bts(connection->fd.fd, bts);
276
277 for (trx = 0; trx < bts->num_trx; ++trx) {
278 show_trx(connection->fd.fd, &bts->trx[trx]);
279 for (ts_no = 0; ts_no < 8; ++ts_no) {
280 show_ts(connection->fd.fd, &bts->trx[trx].ts[ts_no]);
281 for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
282 struct gsm_lchan *lchan =
283 &bts->trx[trx].ts[ts_no].lchan[lchan_no];
284 show_lchan(connection->fd.fd, lchan);
285 }
286 }
287 }
288 }
289}
290
Holger Freyther219518d2009-01-02 22:04:43 +0000291static int client_data(struct bsc_fd *fd, unsigned int what) {
292 char buf[4096];
293 int ret;
294
295 ret = read(fd->fd, buf, sizeof(buf)-1);
296 buf[ret] = '\0';
297
298 /* connection is gone */
299 if (ret <= 0)
300 return telnet_close_client(fd);
301
302 /* time to parse. This code assumes that the input is line based */
303 telnet_parse((struct telnet_connection*)fd->data, buf);
304
305 return 0;
306}
307
308static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
309 struct telnet_connection *connection;
310 struct sockaddr_in sockaddr;
311 socklen_t len = sizeof(sockaddr);
312 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
313
314 if (new_connection < 0) {
315 perror("telnet accept failed");
316 return -1;
317 }
318
319
320 connection = (struct telnet_connection*)malloc(sizeof(*connection));
321 memset(connection, 0, sizeof(*connection));
322 connection->network = (struct gsm_network*)fd->data;
323 connection->fd.data = connection;
324 connection->fd.fd = new_connection;
325 connection->fd.when = BSC_FD_READ;
326 connection->fd.cb = client_data;
Holger Freytherf87573d2009-01-04 03:49:41 +0000327 connection->bts = 0;
Holger Freyther219518d2009-01-02 22:04:43 +0000328 bsc_register_fd(&connection->fd);
329 llist_add_tail(&connection->entry, &active_connections);
330
331 print_welcome(new_connection);
332
333 return 0;
334}