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