blob: 37e1c25ec67450cdc768fa4b6f10b9a55c2b5a88 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* 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 <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
27
Harald Weltef4625b12010-02-20 16:24:02 +010028#include <osmocore/msgb.h>
Harald Weltef4625b12010-02-20 16:24:02 +010029#include <osmocore/talloc.h>
Harald Welte40152872010-05-16 20:52:23 +020030#include <osmocore/logging.h>
31#include <openbsc/telnet_interface.h>
Harald Welte59b04682009-06-10 05:40:52 +080032
33#include <vty/buffer.h>
34
Harald Welte59b04682009-06-10 05:40:52 +080035/* per connection data */
36LLIST_HEAD(active_connections);
37
Harald Weltea8379772009-06-20 22:36:41 +020038static void *tall_telnet_ctx;
39
Harald Welte59b04682009-06-10 05:40:52 +080040/* per network data */
41static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
Harald Welte59b04682009-06-10 05:40:52 +080042
43static struct bsc_fd server_socket = {
44 .when = BSC_FD_READ,
45 .cb = telnet_new_connection,
46 .priv_nr = 0,
47};
48
Harald Welte40152872010-05-16 20:52:23 +020049int telnet_init(void *tall_ctx, void *priv, int port)
Harald Welte67798f72010-05-16 19:30:28 +020050{
Harald Welte59b04682009-06-10 05:40:52 +080051 struct sockaddr_in sock_addr;
Harald Welte40152872010-05-16 20:52:23 +020052 int fd, rc, on = 1;
Harald Welte59b04682009-06-10 05:40:52 +080053
Harald Welte40152872010-05-16 20:52:23 +020054 tall_telnet_ctx = talloc_named_const(tall_ctx, 1,
Harald Weltea8379772009-06-20 22:36:41 +020055 "telnet_connection");
56
Harald Welte59b04682009-06-10 05:40:52 +080057 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
58
59 if (fd < 0) {
Harald Welte40152872010-05-16 20:52:23 +020060 LOGP(0, LOGL_ERROR, "Telnet interface socket creation failed\n");
61 return fd;
Harald Welte59b04682009-06-10 05:40:52 +080062 }
63
64 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
65
66 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 Welte40152872010-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;
Harald Welte59b04682009-06-10 05:40:52 +080076 }
77
Harald Welte40152872010-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;
Harald Welte59b04682009-06-10 05:40:52 +080083 }
84
Harald Welte40152872010-05-16 20:52:23 +020085 server_socket.data = priv;
Harald Welte59b04682009-06-10 05:40:52 +080086 server_socket.fd = fd;
87 bsc_register_fd(&server_socket);
Harald Welte40152872010-05-16 20:52:23 +020088
89 return 0;
Harald Welte59b04682009-06-10 05:40:52 +080090}
91
Harald Weltefa13cad2010-03-23 00:09:32 +080092extern const char *openbsc_copyright;
Harald Weltefa13cad2010-03-23 00:09:32 +080093
Harald Welte67798f72010-05-16 19:30:28 +020094static void print_welcome(int fd)
95{
Harald Welte59b04682009-06-10 05:40:52 +080096 int ret;
97 static char *msg =
Harald Weltefa13cad2010-03-23 00:09:32 +080098 "Welcome to the OpenBSC Control interface\n";
Harald Welte59b04682009-06-10 05:40:52 +080099
100 ret = write(fd, msg, strlen(msg));
Harald Weltefa13cad2010-03-23 00:09:32 +0800101 ret = write(fd, openbsc_copyright, strlen(openbsc_copyright));
Harald Welte59b04682009-06-10 05:40:52 +0800102}
103
Harald Welte67798f72010-05-16 19:30:28 +0200104int telnet_close_client(struct bsc_fd *fd)
105{
Harald Welte59b04682009-06-10 05:40:52 +0800106 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
107
108 close(fd->fd);
109 bsc_unregister_fd(fd);
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100110
111 if (conn->dbg) {
Harald Welte51d2a592010-03-26 21:28:59 +0800112 log_del_target(conn->dbg);
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100113 talloc_free(conn->dbg);
114 }
115
Harald Welte59b04682009-06-10 05:40:52 +0800116 llist_del(&conn->entry);
Harald Welte49a84052009-06-22 01:36:25 +0200117 talloc_free(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800118 return 0;
119}
120
121static int client_data(struct bsc_fd *fd, unsigned int what)
122{
123 struct telnet_connection *conn = fd->data;
124 int rc = 0;
125
126 if (what & BSC_FD_READ) {
127 conn->fd.when &= ~BSC_FD_READ;
128 rc = vty_read(conn->vty);
129 }
130
Harald Welted1bc77e2009-08-07 00:31:23 +0200131 /* vty might have been closed from vithin vty_read() */
132 if (!conn->vty)
133 return rc;
134
Harald Welte59b04682009-06-10 05:40:52 +0800135 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;
142}
143
Harald Welte67798f72010-05-16 19:30:28 +0200144static int telnet_new_connection(struct bsc_fd *fd, unsigned int what)
145{
Harald Welte59b04682009-06-10 05:40:52 +0800146 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 Welte40152872010-05-16 20:52:23 +0200152 LOGP(0, LOGL_ERROR, "telnet accept failed\n");
153 return new_connection;
Harald Welte59b04682009-06-10 05:40:52 +0800154 }
155
Harald Welte857e00d2009-06-26 20:25:23 +0200156 connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
Harald Welte40152872010-05-16 20:52:23 +0200157 connection->priv = fd->data;
Harald Welte59b04682009-06-10 05:40:52 +0800158 connection->fd.data = connection;
159 connection->fd.fd = new_connection;
160 connection->fd.when = BSC_FD_READ;
161 connection->fd.cb = client_data;
Harald Welte59b04682009-06-10 05:40:52 +0800162 bsc_register_fd(&connection->fd);
163 llist_add_tail(&connection->entry, &active_connections);
164
165 print_welcome(new_connection);
166
167 connection->vty = vty_create(new_connection, connection);
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100168 if (!connection->vty) {
Harald Welte40152872010-05-16 20:52:23 +0200169 LOGP(0, LOGL_ERROR, "couldn't create VTY\n");
170 close(new_connection);
171 talloc_free(connection);
Harald Welte59b04682009-06-10 05:40:52 +0800172 return -1;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100173 }
Harald Welte59b04682009-06-10 05:40:52 +0800174
175 return 0;
176}
177
178/* 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 Welted1bc77e2009-08-07 00:31:23 +0200184 if (vty->type != VTY_TERM)
185 return;
186
Harald Welte59b04682009-06-10 05:40:52 +0800187 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 Welted1bc77e2009-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 Welte59b04682009-06-10 05:40:52 +0800199 default:
200 break;
201 }
202}
203