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