blob: b86afefc2fc3d9f9c40c967391965b55c980cff7 [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>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010033#include <osmocore/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 Weltedfe6c7d2010-02-20 16:24:02 +010037#include <osmocore/talloc.h>
Harald Welteb1d4c8e2009-12-17 23:10:46 +010038#include <openbsc/debug.h>
Holger Freyther219518d2009-01-02 22:04:43 +000039
Harald Weltec63e51d2009-03-10 19:46:16 +000040#include <vty/buffer.h>
41
Holger Freytherae61cae2009-01-04 03:46:01 +000042#define WRITE_CONNECTION(fd, msg...) \
43 int ret; \
44 char buf[4096]; \
45 snprintf(buf, sizeof(buf), msg); \
46 ret = write(fd, buf, strlen(buf));
47
48
Holger Freyther219518d2009-01-02 22:04:43 +000049/* per connection data */
50LLIST_HEAD(active_connections);
51
Harald Welte2cf161b2009-06-20 22:36:41 +020052static void *tall_telnet_ctx;
53
Holger Freyther219518d2009-01-02 22:04:43 +000054/* per network data */
55static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
Holger Freyther7aaf1122009-02-14 22:51:13 +000056
Holger Freyther219518d2009-01-02 22:04:43 +000057static struct bsc_fd server_socket = {
58 .when = BSC_FD_READ,
59 .cb = telnet_new_connection,
60 .priv_nr = 0,
61};
62
63void telnet_init(struct gsm_network *network, int port) {
64 struct sockaddr_in sock_addr;
Holger Freytherf0776892009-02-03 20:49:51 +000065 int fd, on = 1;
Holger Freyther219518d2009-01-02 22:04:43 +000066
Harald Welte2cf161b2009-06-20 22:36:41 +020067 tall_telnet_ctx = talloc_named_const(tall_bsc_ctx, 1,
68 "telnet_connection");
69
Harald Welte404cdd82009-03-10 12:21:45 +000070 bsc_vty_init(network);
71
Holger Freyther219518d2009-01-02 22:04:43 +000072 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
73
74 if (fd < 0) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +010075 LOGP(DNM, LOGL_ERROR, "Telnet interface socket creation failed\n");
Holger Freyther219518d2009-01-02 22:04:43 +000076 return;
77 }
78
Holger Freytherf0776892009-02-03 20:49:51 +000079 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
80
Holger Freyther219518d2009-01-02 22:04:43 +000081 memset(&sock_addr, 0, sizeof(sock_addr));
82 sock_addr.sin_family = AF_INET;
83 sock_addr.sin_port = htons(port);
84 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
85
86 if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) {
Harald Weltea992a362009-12-21 23:36:45 +010087 LOGP(DNM, LOGL_ERROR, "Telnet interface failed to bind\n");
Holger Freyther219518d2009-01-02 22:04:43 +000088 return;
89 }
90
91 if (listen(fd, 0) < 0) {
Harald Weltea992a362009-12-21 23:36:45 +010092 LOGP(DNM, LOGL_ERROR, "Telnet interface failed to listen\n");
Holger Freyther219518d2009-01-02 22:04:43 +000093 return;
94 }
95
96 server_socket.data = network;
97 server_socket.fd = fd;
98 bsc_register_fd(&server_socket);
Holger Freyther219518d2009-01-02 22:04:43 +000099}
100
Harald Welte5a29c7f2010-03-23 00:09:32 +0800101extern const char *openbsc_copyright;
Harald Welte5a29c7f2010-03-23 00:09:32 +0800102
Holger Freyther219518d2009-01-02 22:04:43 +0000103static void print_welcome(int fd) {
104 int ret;
105 static char *msg =
Harald Welte5a29c7f2010-03-23 00:09:32 +0800106 "Welcome to the OpenBSC Control interface\n";
Holger Freyther219518d2009-01-02 22:04:43 +0000107
108 ret = write(fd, msg, strlen(msg));
Harald Welte5a29c7f2010-03-23 00:09:32 +0800109 ret = write(fd, openbsc_copyright, strlen(openbsc_copyright));
Holger Freyther219518d2009-01-02 22:04:43 +0000110}
111
112int telnet_close_client(struct bsc_fd *fd) {
113 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
114
115 close(fd->fd);
116 bsc_unregister_fd(fd);
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100117
118 if (conn->dbg) {
Harald Weltedc5062b2010-03-26 21:28:59 +0800119 log_del_target(conn->dbg);
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100120 talloc_free(conn->dbg);
121 }
122
Holger Freyther219518d2009-01-02 22:04:43 +0000123 llist_del(&conn->entry);
Harald Weltea4ffea92009-06-22 01:36:25 +0200124 talloc_free(conn);
Holger Freyther219518d2009-01-02 22:04:43 +0000125 return 0;
126}
127
Harald Welte404cdd82009-03-10 12:21:45 +0000128static int client_data(struct bsc_fd *fd, unsigned int what)
129{
130 struct telnet_connection *conn = fd->data;
Harald Welte703af982009-05-23 06:14:44 +0000131 int rc = 0;
Harald Weltec63e51d2009-03-10 19:46:16 +0000132
133 if (what & BSC_FD_READ) {
134 conn->fd.when &= ~BSC_FD_READ;
135 rc = vty_read(conn->vty);
136 }
137
Harald Welte38202542009-08-07 00:31:23 +0200138 /* vty might have been closed from vithin vty_read() */
139 if (!conn->vty)
140 return rc;
141
Harald Weltec63e51d2009-03-10 19:46:16 +0000142 if (what & BSC_FD_WRITE) {
143 rc = buffer_flush_all(conn->vty->obuf, fd->fd);
144 if (rc == BUFFER_EMPTY)
145 conn->fd.when &= ~BSC_FD_WRITE;
146 }
147
148 return rc;
Holger Freyther219518d2009-01-02 22:04:43 +0000149}
150
151static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
152 struct telnet_connection *connection;
153 struct sockaddr_in sockaddr;
154 socklen_t len = sizeof(sockaddr);
155 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
156
157 if (new_connection < 0) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100158 LOGP(DNM, LOGL_ERROR, "telnet accept failed\n");
Holger Freyther219518d2009-01-02 22:04:43 +0000159 return -1;
160 }
161
162
Harald Welte470ec292009-06-26 20:25:23 +0200163 connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
Holger Freyther219518d2009-01-02 22:04:43 +0000164 connection->network = (struct gsm_network*)fd->data;
165 connection->fd.data = connection;
166 connection->fd.fd = new_connection;
167 connection->fd.when = BSC_FD_READ;
168 connection->fd.cb = client_data;
Holger Freyther219518d2009-01-02 22:04:43 +0000169 bsc_register_fd(&connection->fd);
170 llist_add_tail(&connection->entry, &active_connections);
171
172 print_welcome(new_connection);
173
Harald Weltec63e51d2009-03-10 19:46:16 +0000174 connection->vty = vty_create(new_connection, connection);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100175 if (!connection->vty) {
176 LOGP(DNM, LOGL_ERROR, "couldn't create VTY\n");
Harald Welte404cdd82009-03-10 12:21:45 +0000177 return -1;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100178 }
Harald Welte404cdd82009-03-10 12:21:45 +0000179
Holger Freyther219518d2009-01-02 22:04:43 +0000180 return 0;
181}
Holger Freyther7aaf1122009-02-14 22:51:13 +0000182
Harald Weltec63e51d2009-03-10 19:46:16 +0000183/* callback from VTY code */
184void vty_event(enum event event, int sock, struct vty *vty)
185{
186 struct telnet_connection *connection = vty->priv;
187 struct bsc_fd *bfd = &connection->fd;
188
Harald Welte38202542009-08-07 00:31:23 +0200189 if (vty->type != VTY_TERM)
190 return;
191
Harald Weltec63e51d2009-03-10 19:46:16 +0000192 switch (event) {
193 case VTY_READ:
194 bfd->when |= BSC_FD_READ;
195 break;
196 case VTY_WRITE:
197 bfd->when |= BSC_FD_WRITE;
198 break;
Harald Welte38202542009-08-07 00:31:23 +0200199 case VTY_CLOSED:
200 /* vty layer is about to free() vty */
201 connection->vty = NULL;
202 telnet_close_client(bfd);
203 break;
Harald Weltec63e51d2009-03-10 19:46:16 +0000204 default:
205 break;
206 }
207}
208