blob: ba574704e750c5296cab9a8afef7c5cfeb654b67 [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>
Harald Welte12247c62009-05-21 07:23:02 +000023#include <stdlib.h>
Holger Freyther219518d2009-01-02 22:04:43 +000024#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 Freyther9b177762009-02-16 19:07:18 +000032#include <openbsc/gsm_04_11.h>
Holger Freyther868b8cd2009-01-04 03:57:27 +000033#include <openbsc/msgb.h>
Holger Freyther7448a532009-01-04 20:18:23 +000034#include <openbsc/abis_rsl.h>
Harald Welte38c2f132009-01-06 23:10:57 +000035#include <openbsc/paging.h>
Holger Freyther7aaf1122009-02-14 22:51:13 +000036#include <openbsc/signal.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020037#include <openbsc/talloc.h>
Holger Freyther219518d2009-01-02 22:04:43 +000038
Harald Weltec63e51d2009-03-10 19:46:16 +000039#include <vty/buffer.h>
40
Holger Freytherae61cae2009-01-04 03:46:01 +000041#define WRITE_CONNECTION(fd, msg...) \
42 int ret; \
43 char buf[4096]; \
44 snprintf(buf, sizeof(buf), msg); \
45 ret = write(fd, buf, strlen(buf));
46
47
Holger Freyther219518d2009-01-02 22:04:43 +000048/* per connection data */
49LLIST_HEAD(active_connections);
50
Harald Welte2cf161b2009-06-20 22:36:41 +020051static void *tall_telnet_ctx;
52
Holger Freyther219518d2009-01-02 22:04:43 +000053/* per network data */
54static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
Harald Welte703af982009-05-23 06:14:44 +000055#if 0
Harald Welte595ad7b2009-02-16 22:05:44 +000056static int telnet_paging_callback(unsigned int subsys, unsigned int signal,
57 void *handler_data, void *signal_data);
58static int telnet_sms_callback(unsigned int subsys, unsigned int signal,
59 void *handler_data, void *signal_data);
Harald Welte703af982009-05-23 06:14:44 +000060#endif
Holger Freyther7aaf1122009-02-14 22:51:13 +000061
Holger Freyther219518d2009-01-02 22:04:43 +000062static struct bsc_fd server_socket = {
63 .when = BSC_FD_READ,
64 .cb = telnet_new_connection,
65 .priv_nr = 0,
66};
67
68void telnet_init(struct gsm_network *network, int port) {
69 struct sockaddr_in sock_addr;
Holger Freytherf0776892009-02-03 20:49:51 +000070 int fd, on = 1;
Holger Freyther219518d2009-01-02 22:04:43 +000071
Harald Welte2cf161b2009-06-20 22:36:41 +020072 tall_telnet_ctx = talloc_named_const(tall_bsc_ctx, 1,
73 "telnet_connection");
74
Harald Welte404cdd82009-03-10 12:21:45 +000075 bsc_vty_init(network);
76
Holger Freyther219518d2009-01-02 22:04:43 +000077 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
78
79 if (fd < 0) {
80 perror("Telnet interface socket creation failed");
81 return;
82 }
83
Holger Freytherf0776892009-02-03 20:49:51 +000084 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
85
Holger Freyther219518d2009-01-02 22:04:43 +000086 memset(&sock_addr, 0, sizeof(sock_addr));
87 sock_addr.sin_family = AF_INET;
88 sock_addr.sin_port = htons(port);
89 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
90
91 if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) {
92 perror("Telnet interface failed to bind");
93 return;
94 }
95
96 if (listen(fd, 0) < 0) {
97 perror("Telnet interface failed to listen");
98 return;
99 }
100
101 server_socket.data = network;
102 server_socket.fd = fd;
103 bsc_register_fd(&server_socket);
Holger Freyther7aaf1122009-02-14 22:51:13 +0000104
Holger Freyther9b177762009-02-16 19:07:18 +0000105 /* register callbacks */
Harald Welte404cdd82009-03-10 12:21:45 +0000106#if 0
Harald Welte595ad7b2009-02-16 22:05:44 +0000107 register_signal_handler(SS_PAGING, telnet_paging_callback, network);
108 register_signal_handler(SS_SMS, telnet_sms_callback, network);
Harald Welte404cdd82009-03-10 12:21:45 +0000109#endif
Holger Freyther219518d2009-01-02 22:04:43 +0000110}
111
112static void print_welcome(int fd) {
113 int ret;
114 static char *msg =
115 "Welcome to the OpenBSC Control interface\n"
116 "Copyright (C) 2008, 2009 Harald Welte\n"
117 "Contributions by Daniel Willmann, Jan Lübbe, "
118 "Stefan Schmidt, Holger Freyther\n\n"
119 "License GPLv2+: GNU GPL version 2 or later "
120 "<http://gnu.org/licenses/gpl.html>\n"
121 "This is free software: you are free to change "
122 "and redistribute it.\n"
123 "There is NO WARRANTY, to the extent permitted "
124 "by law.\nType \"help\" to get a short introduction.\n";
125
126 ret = write(fd, msg, strlen(msg));
127}
128
129int telnet_close_client(struct bsc_fd *fd) {
130 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
131
132 close(fd->fd);
133 bsc_unregister_fd(fd);
134 llist_del(&conn->entry);
Harald Weltea4ffea92009-06-22 01:36:25 +0200135 talloc_free(conn);
Holger Freyther219518d2009-01-02 22:04:43 +0000136 return 0;
137}
138
Harald Welte404cdd82009-03-10 12:21:45 +0000139static int client_data(struct bsc_fd *fd, unsigned int what)
140{
141 struct telnet_connection *conn = fd->data;
Harald Welte703af982009-05-23 06:14:44 +0000142 int rc = 0;
Harald Weltec63e51d2009-03-10 19:46:16 +0000143
144 if (what & BSC_FD_READ) {
145 conn->fd.when &= ~BSC_FD_READ;
146 rc = vty_read(conn->vty);
147 }
148
Harald Welte38202542009-08-07 00:31:23 +0200149 /* vty might have been closed from vithin vty_read() */
150 if (!conn->vty)
151 return rc;
152
Harald Weltec63e51d2009-03-10 19:46:16 +0000153 if (what & BSC_FD_WRITE) {
154 rc = buffer_flush_all(conn->vty->obuf, fd->fd);
155 if (rc == BUFFER_EMPTY)
156 conn->fd.when &= ~BSC_FD_WRITE;
157 }
158
159 return rc;
Holger Freyther219518d2009-01-02 22:04:43 +0000160}
161
162static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
163 struct telnet_connection *connection;
164 struct sockaddr_in sockaddr;
165 socklen_t len = sizeof(sockaddr);
166 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
167
168 if (new_connection < 0) {
169 perror("telnet accept failed");
170 return -1;
171 }
172
173
Harald Welte470ec292009-06-26 20:25:23 +0200174 connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
Holger Freyther219518d2009-01-02 22:04:43 +0000175 connection->network = (struct gsm_network*)fd->data;
176 connection->fd.data = connection;
177 connection->fd.fd = new_connection;
178 connection->fd.when = BSC_FD_READ;
179 connection->fd.cb = client_data;
Holger Freytherf87573d2009-01-04 03:49:41 +0000180 connection->bts = 0;
Holger Freyther219518d2009-01-02 22:04:43 +0000181 bsc_register_fd(&connection->fd);
182 llist_add_tail(&connection->entry, &active_connections);
183
184 print_welcome(new_connection);
185
Harald Weltec63e51d2009-03-10 19:46:16 +0000186 connection->vty = vty_create(new_connection, connection);
Harald Welte404cdd82009-03-10 12:21:45 +0000187 if (!connection->vty)
188 return -1;
189
Holger Freyther219518d2009-01-02 22:04:43 +0000190 return 0;
191}
Holger Freyther7aaf1122009-02-14 22:51:13 +0000192
Harald Weltec63e51d2009-03-10 19:46:16 +0000193/* callback from VTY code */
194void vty_event(enum event event, int sock, struct vty *vty)
195{
196 struct telnet_connection *connection = vty->priv;
197 struct bsc_fd *bfd = &connection->fd;
198
Harald Welte38202542009-08-07 00:31:23 +0200199 if (vty->type != VTY_TERM)
200 return;
201
Harald Weltec63e51d2009-03-10 19:46:16 +0000202 switch (event) {
203 case VTY_READ:
204 bfd->when |= BSC_FD_READ;
205 break;
206 case VTY_WRITE:
207 bfd->when |= BSC_FD_WRITE;
208 break;
Harald Welte38202542009-08-07 00:31:23 +0200209 case VTY_CLOSED:
210 /* vty layer is about to free() vty */
211 connection->vty = NULL;
212 telnet_close_client(bfd);
213 break;
Harald Weltec63e51d2009-03-10 19:46:16 +0000214 default:
215 break;
216 }
217}
218
Harald Welte404cdd82009-03-10 12:21:45 +0000219#if 0
Harald Welte595ad7b2009-02-16 22:05:44 +0000220static int telnet_paging_callback(unsigned int subsys, unsigned int singal,
221 void *handler_data, void *signal_data)
Holger Freyther7aaf1122009-02-14 22:51:13 +0000222{
Harald Welte595ad7b2009-02-16 22:05:44 +0000223 struct paging_signal_data *paging = signal_data;
Holger Freyther7aaf1122009-02-14 22:51:13 +0000224 struct telnet_connection *con;
225
226 llist_for_each_entry(con, &active_connections, entry) {
227 if (paging->lchan) {
228 WRITE_CONNECTION(con->fd.fd, "Paging succeeded\n");
229 show_lchan(con->fd.fd, paging->lchan);
230 } else {
231 WRITE_CONNECTION(con->fd.fd, "Paging failed for subscriber: %s/%s/%s\n",
232 paging->subscr->imsi,
233 paging->subscr->tmsi,
234 paging->subscr->name);
235 }
236 }
237
238 return 0;
239}
Holger Freyther9b177762009-02-16 19:07:18 +0000240
Harald Welte595ad7b2009-02-16 22:05:44 +0000241static int telnet_sms_callback(unsigned int subsys, unsigned int signal,
242 void *handler_data, void *signal_data)
Holger Freyther9b177762009-02-16 19:07:18 +0000243{
Harald Welte595ad7b2009-02-16 22:05:44 +0000244 struct sms_submit *sms = signal_data;
Holger Freyther9b177762009-02-16 19:07:18 +0000245 struct telnet_connection *con;
246
247 llist_for_each_entry(con, &active_connections, entry) {
Harald Welte595ad7b2009-02-16 22:05:44 +0000248 WRITE_CONNECTION(con->fd.fd, "Incoming SMS: %s\n", sms->user_data);
Holger Freyther9b177762009-02-16 19:07:18 +0000249 }
250
251 return 0;
252}
Harald Welte404cdd82009-03-10 12:21:45 +0000253#endif