blob: 72a71becd50f07a6cdf92017325e81c1dc601bc1 [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 Freyther219518d2009-01-02 22:04:43 +000032
33extern void telnet_parse(struct telnet_connection *connection, char *line);
34
Holger Freytherae61cae2009-01-04 03:46:01 +000035#define WRITE_CONNECTION(fd, msg...) \
36 int ret; \
37 char buf[4096]; \
38 snprintf(buf, sizeof(buf), msg); \
39 ret = write(fd, buf, strlen(buf));
40
41
Holger Freyther219518d2009-01-02 22:04:43 +000042/* per connection data */
43LLIST_HEAD(active_connections);
44
45/* per network data */
46static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
47static struct bsc_fd server_socket = {
48 .when = BSC_FD_READ,
49 .cb = telnet_new_connection,
50 .priv_nr = 0,
51};
52
53void telnet_init(struct gsm_network *network, int port) {
54 struct sockaddr_in sock_addr;
55 int fd;
56
57 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
58
59 if (fd < 0) {
60 perror("Telnet interface socket creation failed");
61 return;
62 }
63
64 memset(&sock_addr, 0, sizeof(sock_addr));
65 sock_addr.sin_family = AF_INET;
66 sock_addr.sin_port = htons(port);
67 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
68
69 if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) {
70 perror("Telnet interface failed to bind");
71 return;
72 }
73
74 if (listen(fd, 0) < 0) {
75 perror("Telnet interface failed to listen");
76 return;
77 }
78
79 server_socket.data = network;
80 server_socket.fd = fd;
81 bsc_register_fd(&server_socket);
82}
83
84void telnet_write_help(int fd) {
85 int ret;
86 static char *msg =
87 "Help for the ad-hoc telnet command line interface\n"
88 "The generic pattern is CMD LEN DATA\\n or just CMD\n"
89 "where CMD is one of the following:\n"
90 "help\n"
91 "page IMSI (type)\n"
92 "call IMSI (number)\n"
93 "get_channel IMSI Add use count on an active channel\n"
94 "put_channel IMSI Remove use count on an active channel\n"
Holger Freytherae61cae2009-01-04 03:46:01 +000095 "show This will show the channel allocation\n"
Holger Freyther219518d2009-01-02 22:04:43 +000096 "48 IMSI 0xAB 0xEF...Send GSM 04.08\n"
97 "11 IMSI 0xAB 0xEF...Send GSM 04.11\n";
98
99 ret = write(fd, msg, strlen(msg));
100}
101
102static void print_welcome(int fd) {
103 int ret;
104 static char *msg =
105 "Welcome to the OpenBSC Control interface\n"
106 "Copyright (C) 2008, 2009 Harald Welte\n"
107 "Contributions by Daniel Willmann, Jan Lübbe, "
108 "Stefan Schmidt, Holger Freyther\n\n"
109 "License GPLv2+: GNU GPL version 2 or later "
110 "<http://gnu.org/licenses/gpl.html>\n"
111 "This is free software: you are free to change "
112 "and redistribute it.\n"
113 "There is NO WARRANTY, to the extent permitted "
114 "by law.\nType \"help\" to get a short introduction.\n";
115
116 ret = write(fd, msg, strlen(msg));
117}
118
119int telnet_close_client(struct bsc_fd *fd) {
120 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
121
122 close(fd->fd);
123 bsc_unregister_fd(fd);
124 llist_del(&conn->entry);
125 free(conn);
126 return 0;
127}
128
129void telnet_error_client(int fd) {
130 int ret;
131 static char *msg = "Something went wrong. Please try again.\n";
132
133 printf("Error\n");
134 ret = write(fd, msg, strlen(msg));
135}
136
137void telnet_page(struct telnet_connection *connection, const char *imsi, int page) {
138 printf("going to page: '%s' %d\n", imsi, page);
139}
140
Holger Freytherf87573d2009-01-04 03:49:41 +0000141static struct gsm_lchan* find_channel(struct gsm_bts *bts, const char *imsi,
142 const char **error, int fd) {
143 int ret;
144 struct gsm_lchan *lchan;
145 struct gsm_subscriber *subscr;
146
147 subscr = subscr_get_by_imsi(imsi);
148 if (!subscr) {
149 ret = write(fd, error[0], strlen(error[0]));
150 return NULL;
151 }
152
153 lchan = lchan_find(bts, subscr);
154 if (!lchan)
155 ret = write(fd, error[1], strlen(error[1]));
156
157 subscr_put(subscr);
158 return lchan;
159}
160
Holger Freyther219518d2009-01-02 22:04:43 +0000161void telnet_put_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000162 static const char* error[] = {
163 "put_channel: IMSI not found\n",
164 "put_channel: No channel allocated for IMSI\n" };
165 struct gsm_bts *bts = &connection->network->bts[connection->bts];
166 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
167
168 if (!lchan)
169 return;
170
171 put_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000172}
173
174void telnet_get_channel(struct telnet_connection *connection, const char *imsi) {
Holger Freytherf87573d2009-01-04 03:49:41 +0000175 static const char* error[] = {
176 "get_channel: IMSI not found\n",
177 "get_channel: No channel allocated for IMSI\n" };
178 struct gsm_bts *bts = &connection->network->bts[connection->bts];
179 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
180
181 if (!lchan)
182 return;
183
184 use_lchan(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000185}
186
187void telnet_call(struct telnet_connection *connection, const char* imsi,
188 const char *origin) {
Holger Freyther3ae8fd22009-01-04 03:50:40 +0000189 static const char* error[] = {
190 "call: IMSI not found\n",
191 "call: No channel allocated for IMSI\n" };
192 struct gsm_bts *bts = &connection->network->bts[connection->bts];
193 struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd);
194
195 if (!lchan)
196 return;
197
198 /* TODO: add the origin */
199 gsm48_cc_tx_setup(lchan);
Holger Freyther219518d2009-01-02 22:04:43 +0000200}
201
202void telnet_send_gsm_48(struct telnet_connection *connection) {
203 printf("sending gsm04.08 message\n");
204}
205
206void telnet_send_gsm_11(struct telnet_connection *connection) {
207 printf("sending gsm04.11 message\n");
208}
209
Holger Freytherae61cae2009-01-04 03:46:01 +0000210static void show_bts(int fd, struct gsm_bts *bts) {
211 WRITE_CONNECTION(fd,
212 "BTS #%u on link %u LOC: %u TRX: %d CCCH0: arfcn:%u,#%u\n",
213 bts->nr, bts->bts_nr, bts->location_area_code,
214 bts->num_trx, bts->c0->arfcn, bts->c0->nr)
215}
216
217static void show_trx(int fd, struct gsm_bts_trx *trx) {
218 WRITE_CONNECTION(fd,
219 " TRX: %u ARFCN: %u\n",
220 trx->nr, trx->arfcn)
221}
222
223static void show_ts(int fd, struct gsm_bts_trx_ts *ts) {
224 WRITE_CONNECTION(fd,
225 " TS: #%u pchan: %d flags: %u\n",
226 ts->nr, ts->pchan, ts->flags);
227}
228
229static void show_lchan(int fd, struct gsm_lchan *lchan) {
230 struct gsm_subscriber *subscr = lchan->subscr;
231 WRITE_CONNECTION(fd,
232 " LCHAN: #%u type: %d subscriber: %s/%s/%s use: %d loc: %p\n",
233 lchan->nr, lchan->type,
234 subscr ? subscr->imsi : "na",
235 subscr ? subscr->tmsi : "na",
236 subscr ? subscr->name : "na",
237 lchan->use_count, lchan->loc_operation);
238}
239
240void telnet_list_channels(struct telnet_connection *connection) {
241 int bts_no, trx, lchan_no, ts_no;
242 struct gsm_network *network = connection->network;
243
244 for (bts_no = 0; bts_no < network->num_bts; ++bts_no) {
245 struct gsm_bts *bts = &network->bts[bts_no];
246 show_bts(connection->fd.fd, bts);
247
248 for (trx = 0; trx < bts->num_trx; ++trx) {
249 show_trx(connection->fd.fd, &bts->trx[trx]);
250 for (ts_no = 0; ts_no < 8; ++ts_no) {
251 show_ts(connection->fd.fd, &bts->trx[trx].ts[ts_no]);
252 for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) {
253 struct gsm_lchan *lchan =
254 &bts->trx[trx].ts[ts_no].lchan[lchan_no];
255 show_lchan(connection->fd.fd, lchan);
256 }
257 }
258 }
259 }
260}
261
Holger Freyther219518d2009-01-02 22:04:43 +0000262static int client_data(struct bsc_fd *fd, unsigned int what) {
263 char buf[4096];
264 int ret;
265
266 ret = read(fd->fd, buf, sizeof(buf)-1);
267 buf[ret] = '\0';
268
269 /* connection is gone */
270 if (ret <= 0)
271 return telnet_close_client(fd);
272
273 /* time to parse. This code assumes that the input is line based */
274 telnet_parse((struct telnet_connection*)fd->data, buf);
275
276 return 0;
277}
278
279static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
280 struct telnet_connection *connection;
281 struct sockaddr_in sockaddr;
282 socklen_t len = sizeof(sockaddr);
283 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
284
285 if (new_connection < 0) {
286 perror("telnet accept failed");
287 return -1;
288 }
289
290
291 connection = (struct telnet_connection*)malloc(sizeof(*connection));
292 memset(connection, 0, sizeof(*connection));
293 connection->network = (struct gsm_network*)fd->data;
294 connection->fd.data = connection;
295 connection->fd.fd = new_connection;
296 connection->fd.when = BSC_FD_READ;
297 connection->fd.cb = client_data;
Holger Freytherf87573d2009-01-04 03:49:41 +0000298 connection->bts = 0;
Holger Freyther219518d2009-01-02 22:04:43 +0000299 bsc_register_fd(&connection->fd);
300 llist_add_tail(&connection->entry, &active_connections);
301
302 print_welcome(new_connection);
303
304 return 0;
305}