blob: 9069096003c6263f13b77ec7bb19fc98dc2f3d3f [file] [log] [blame]
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001/* 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
28#include <osmocore/msgb.h>
29#include <osmocore/talloc.h>
30#include <osmocore/logging.h>
31
32#include <osmocom/vty/telnet_interface.h>
33#include <osmocom/vty/buffer.h>
34
35/* per connection data */
36LLIST_HEAD(active_connections);
37
38static void *tall_telnet_ctx;
39
40/* per network data */
41static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
42
43static struct bsc_fd server_socket = {
44 .when = BSC_FD_READ,
45 .cb = telnet_new_connection,
46 .priv_nr = 0,
47};
48
49int telnet_init(void *tall_ctx, void *priv, int port)
50{
51 struct sockaddr_in sock_addr;
52 int fd, rc, on = 1;
53
54 tall_telnet_ctx = talloc_named_const(tall_ctx, 1,
55 "telnet_connection");
56
57 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
58
59 if (fd < 0) {
60 LOGP(0, LOGL_ERROR, "Telnet interface socket creation failed\n");
61 return fd;
62 }
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
71 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;
76 }
77
78 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;
83 }
84
85 server_socket.data = priv;
86 server_socket.fd = fd;
87 bsc_register_fd(&server_socket);
88
89 return 0;
90}
91
92extern const char *openbsc_copyright;
93
94static void print_welcome(int fd)
95{
96 int ret;
97 static char *msg =
98 "Welcome to the OpenBSC Control interface\n";
99
100 ret = write(fd, msg, strlen(msg));
101 ret = write(fd, openbsc_copyright, strlen(openbsc_copyright));
102}
103
104int telnet_close_client(struct bsc_fd *fd)
105{
106 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
107
108 close(fd->fd);
109 bsc_unregister_fd(fd);
110
111 if (conn->dbg) {
112 log_del_target(conn->dbg);
113 talloc_free(conn->dbg);
114 }
115
116 llist_del(&conn->entry);
117 talloc_free(conn);
118 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
131 /* vty might have been closed from vithin vty_read() */
132 if (!conn->vty)
133 return rc;
134
135 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
144static int telnet_new_connection(struct bsc_fd *fd, unsigned int what)
145{
146 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) {
152 LOGP(0, LOGL_ERROR, "telnet accept failed\n");
153 return new_connection;
154 }
155
156 connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
157 connection->priv = fd->data;
158 connection->fd.data = connection;
159 connection->fd.fd = new_connection;
160 connection->fd.when = BSC_FD_READ;
161 connection->fd.cb = client_data;
162 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);
168 if (!connection->vty) {
169 LOGP(0, LOGL_ERROR, "couldn't create VTY\n");
170 close(new_connection);
171 talloc_free(connection);
172 return -1;
173 }
174
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
184 if (vty->type != VTY_TERM)
185 return;
186
187 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;
194 case VTY_CLOSED:
195 /* vty layer is about to free() vty */
196 connection->vty = NULL;
197 telnet_close_client(bfd);
198 break;
199 default:
200 break;
201 }
202}
203