blob: 37e1c25ec67450cdc768fa4b6f10b9a55c2b5a88 [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
Harald Weltedfe6c7d2010-02-20 16:24:02 +010028#include <osmocore/msgb.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010029#include <osmocore/talloc.h>
Harald Weltedcccb182010-05-16 20:52:23 +020030#include <osmocore/logging.h>
31#include <openbsc/telnet_interface.h>
Holger Freyther219518d2009-01-02 22:04:43 +000032
Harald Weltec63e51d2009-03-10 19:46:16 +000033#include <vty/buffer.h>
34
Holger Freyther219518d2009-01-02 22:04:43 +000035/* per connection data */
36LLIST_HEAD(active_connections);
37
Harald Welte2cf161b2009-06-20 22:36:41 +020038static void *tall_telnet_ctx;
39
Holger Freyther219518d2009-01-02 22:04:43 +000040/* per network data */
41static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
Holger Freyther7aaf1122009-02-14 22:51:13 +000042
Holger Freyther219518d2009-01-02 22:04:43 +000043static struct bsc_fd server_socket = {
44 .when = BSC_FD_READ,
45 .cb = telnet_new_connection,
46 .priv_nr = 0,
47};
48
Harald Weltedcccb182010-05-16 20:52:23 +020049int telnet_init(void *tall_ctx, void *priv, int port)
Harald Welte88907a22010-05-16 19:30:28 +020050{
Holger Freyther219518d2009-01-02 22:04:43 +000051 struct sockaddr_in sock_addr;
Harald Weltedcccb182010-05-16 20:52:23 +020052 int fd, rc, on = 1;
Holger Freyther219518d2009-01-02 22:04:43 +000053
Harald Weltedcccb182010-05-16 20:52:23 +020054 tall_telnet_ctx = talloc_named_const(tall_ctx, 1,
Harald Welte2cf161b2009-06-20 22:36:41 +020055 "telnet_connection");
56
Holger Freyther219518d2009-01-02 22:04:43 +000057 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
58
59 if (fd < 0) {
Harald Weltedcccb182010-05-16 20:52:23 +020060 LOGP(0, LOGL_ERROR, "Telnet interface socket creation failed\n");
61 return fd;
Holger Freyther219518d2009-01-02 22:04:43 +000062 }
63
Holger Freytherf0776892009-02-03 20:49:51 +000064 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
65
Holger Freyther219518d2009-01-02 22:04:43 +000066 memset(&sock_addr, 0, sizeof(sock_addr));
67 sock_addr.sin_family = AF_INET;
68 sock_addr.sin_port = htons(port);
69 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
70
Harald Weltedcccb182010-05-16 20:52:23 +020071 rc = bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
72 if (bind < 0) {
73 LOGP(0, LOGL_ERROR, "Telnet interface failed to bind\n");
74 close(fd);
75 return rc;
Holger Freyther219518d2009-01-02 22:04:43 +000076 }
77
Harald Weltedcccb182010-05-16 20:52:23 +020078 rc = listen(fd, 0);
79 if (rc < 0) {
80 LOGP(0, LOGL_ERROR, "Telnet interface failed to listen\n");
81 close(fd);
82 return rc;
Holger Freyther219518d2009-01-02 22:04:43 +000083 }
84
Harald Weltedcccb182010-05-16 20:52:23 +020085 server_socket.data = priv;
Holger Freyther219518d2009-01-02 22:04:43 +000086 server_socket.fd = fd;
87 bsc_register_fd(&server_socket);
Harald Weltedcccb182010-05-16 20:52:23 +020088
89 return 0;
Holger Freyther219518d2009-01-02 22:04:43 +000090}
91
Harald Welte5a29c7f2010-03-23 00:09:32 +080092extern const char *openbsc_copyright;
Harald Welte5a29c7f2010-03-23 00:09:32 +080093
Harald Welte88907a22010-05-16 19:30:28 +020094static void print_welcome(int fd)
95{
Holger Freyther219518d2009-01-02 22:04:43 +000096 int ret;
97 static char *msg =
Harald Welte5a29c7f2010-03-23 00:09:32 +080098 "Welcome to the OpenBSC Control interface\n";
Holger Freyther219518d2009-01-02 22:04:43 +000099
100 ret = write(fd, msg, strlen(msg));
Harald Welte5a29c7f2010-03-23 00:09:32 +0800101 ret = write(fd, openbsc_copyright, strlen(openbsc_copyright));
Holger Freyther219518d2009-01-02 22:04:43 +0000102}
103
Harald Welte88907a22010-05-16 19:30:28 +0200104int telnet_close_client(struct bsc_fd *fd)
105{
Holger Freyther219518d2009-01-02 22:04:43 +0000106 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
107
108 close(fd->fd);
109 bsc_unregister_fd(fd);
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100110
111 if (conn->dbg) {
Harald Weltedc5062b2010-03-26 21:28:59 +0800112 log_del_target(conn->dbg);
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100113 talloc_free(conn->dbg);
114 }
115
Holger Freyther219518d2009-01-02 22:04:43 +0000116 llist_del(&conn->entry);
Harald Weltea4ffea92009-06-22 01:36:25 +0200117 talloc_free(conn);
Holger Freyther219518d2009-01-02 22:04:43 +0000118 return 0;
119}
120
Harald Welte404cdd82009-03-10 12:21:45 +0000121static int client_data(struct bsc_fd *fd, unsigned int what)
122{
123 struct telnet_connection *conn = fd->data;
Harald Welte703af982009-05-23 06:14:44 +0000124 int rc = 0;
Harald Weltec63e51d2009-03-10 19:46:16 +0000125
126 if (what & BSC_FD_READ) {
127 conn->fd.when &= ~BSC_FD_READ;
128 rc = vty_read(conn->vty);
129 }
130
Harald Welte38202542009-08-07 00:31:23 +0200131 /* vty might have been closed from vithin vty_read() */
132 if (!conn->vty)
133 return rc;
134
Harald Weltec63e51d2009-03-10 19:46:16 +0000135 if (what & BSC_FD_WRITE) {
136 rc = buffer_flush_all(conn->vty->obuf, fd->fd);
137 if (rc == BUFFER_EMPTY)
138 conn->fd.when &= ~BSC_FD_WRITE;
139 }
140
141 return rc;
Holger Freyther219518d2009-01-02 22:04:43 +0000142}
143
Harald Welte88907a22010-05-16 19:30:28 +0200144static int telnet_new_connection(struct bsc_fd *fd, unsigned int what)
145{
Holger Freyther219518d2009-01-02 22:04:43 +0000146 struct telnet_connection *connection;
147 struct sockaddr_in sockaddr;
148 socklen_t len = sizeof(sockaddr);
149 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
150
151 if (new_connection < 0) {
Harald Weltedcccb182010-05-16 20:52:23 +0200152 LOGP(0, LOGL_ERROR, "telnet accept failed\n");
153 return new_connection;
Holger Freyther219518d2009-01-02 22:04:43 +0000154 }
155
Harald Welte470ec292009-06-26 20:25:23 +0200156 connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
Harald Weltedcccb182010-05-16 20:52:23 +0200157 connection->priv = fd->data;
Holger Freyther219518d2009-01-02 22:04:43 +0000158 connection->fd.data = connection;
159 connection->fd.fd = new_connection;
160 connection->fd.when = BSC_FD_READ;
161 connection->fd.cb = client_data;
Holger Freyther219518d2009-01-02 22:04:43 +0000162 bsc_register_fd(&connection->fd);
163 llist_add_tail(&connection->entry, &active_connections);
164
165 print_welcome(new_connection);
166
Harald Weltec63e51d2009-03-10 19:46:16 +0000167 connection->vty = vty_create(new_connection, connection);
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100168 if (!connection->vty) {
Harald Weltedcccb182010-05-16 20:52:23 +0200169 LOGP(0, LOGL_ERROR, "couldn't create VTY\n");
170 close(new_connection);
171 talloc_free(connection);
Harald Welte404cdd82009-03-10 12:21:45 +0000172 return -1;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100173 }
Harald Welte404cdd82009-03-10 12:21:45 +0000174
Holger Freyther219518d2009-01-02 22:04:43 +0000175 return 0;
176}
Holger Freyther7aaf1122009-02-14 22:51:13 +0000177
Harald Weltec63e51d2009-03-10 19:46:16 +0000178/* callback from VTY code */
179void vty_event(enum event event, int sock, struct vty *vty)
180{
181 struct telnet_connection *connection = vty->priv;
182 struct bsc_fd *bfd = &connection->fd;
183
Harald Welte38202542009-08-07 00:31:23 +0200184 if (vty->type != VTY_TERM)
185 return;
186
Harald Weltec63e51d2009-03-10 19:46:16 +0000187 switch (event) {
188 case VTY_READ:
189 bfd->when |= BSC_FD_READ;
190 break;
191 case VTY_WRITE:
192 bfd->when |= BSC_FD_WRITE;
193 break;
Harald Welte38202542009-08-07 00:31:23 +0200194 case VTY_CLOSED:
195 /* vty layer is about to free() vty */
196 connection->vty = NULL;
197 telnet_close_client(bfd);
198 break;
Harald Weltec63e51d2009-03-10 19:46:16 +0000199 default:
200 break;
201 }
202}
203