blob: 1a285102e7d257096aba703de665957166044524 [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
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010028#include <osmocom/core/msgb.h>
29#include <osmocom/core/talloc.h>
30#include <osmocom/core/logging.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020031
32#include <osmocom/vty/telnet_interface.h>
33#include <osmocom/vty/buffer.h>
Holger Hans Peter Freyther2e228fc2010-09-11 13:41:41 +080034#include <osmocom/vty/command.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020035
36/* per connection data */
37LLIST_HEAD(active_connections);
38
39static void *tall_telnet_ctx;
40
41/* per network data */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020042static int telnet_new_connection(struct osmo_fd *fd, unsigned int what);
Harald Welte3fb0b6f2010-05-19 19:02:52 +020043
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020044static struct osmo_fd server_socket = {
Harald Welte3fb0b6f2010-05-19 19:02:52 +020045 .when = BSC_FD_READ,
46 .cb = telnet_new_connection,
47 .priv_nr = 0,
48};
49
50int telnet_init(void *tall_ctx, void *priv, int port)
51{
52 struct sockaddr_in sock_addr;
53 int fd, rc, on = 1;
54
55 tall_telnet_ctx = talloc_named_const(tall_ctx, 1,
56 "telnet_connection");
57
58 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
59
60 if (fd < 0) {
61 LOGP(0, LOGL_ERROR, "Telnet interface socket creation failed\n");
62 return fd;
63 }
64
65 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
66
67 memset(&sock_addr, 0, sizeof(sock_addr));
68 sock_addr.sin_family = AF_INET;
69 sock_addr.sin_port = htons(port);
70 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
71
72 rc = bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
Sylvain Munaut4a4f96d2011-01-03 22:19:40 +010073 if (rc < 0) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +020074 LOGP(0, LOGL_ERROR, "Telnet interface failed to bind\n");
75 close(fd);
76 return rc;
77 }
78
79 rc = listen(fd, 0);
80 if (rc < 0) {
81 LOGP(0, LOGL_ERROR, "Telnet interface failed to listen\n");
82 close(fd);
83 return rc;
84 }
85
86 server_socket.data = priv;
87 server_socket.fd = fd;
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020088 osmo_fd_register(&server_socket);
Harald Welte3fb0b6f2010-05-19 19:02:52 +020089
90 return 0;
91}
92
Holger Hans Peter Freyther2e228fc2010-09-11 13:41:41 +080093extern struct host host;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020094
95static void print_welcome(int fd)
96{
Harald Welteb62b04b2011-05-22 19:15:07 +020097 static const char *msg1 = "Welcome to the ";
98 static const char *msg2 = " control interface\r\n";
Harald Welte9c3cbfb2011-07-16 11:59:44 +020099 const char *app_name = "<unnamed>";
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200100
Harald Welteb62b04b2011-05-22 19:15:07 +0200101 if (host.app_info->name)
102 app_name = host.app_info->name;
103
Harald Welte2b322152011-07-16 12:01:52 +0200104 write(fd, msg1, strlen(msg1));
105 write(fd, app_name, strlen(app_name));
106 write(fd, msg2, strlen(msg2));
Holger Hans Peter Freyther2e228fc2010-09-11 13:41:41 +0800107
108 if (host.app_info->copyright)
Harald Welte2b322152011-07-16 12:01:52 +0200109 write(fd, host.app_info->copyright, strlen(host.app_info->copyright));
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200110}
111
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200112int telnet_close_client(struct osmo_fd *fd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200113{
114 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
115
116 close(fd->fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200117 osmo_fd_unregister(fd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200118
119 if (conn->dbg) {
120 log_del_target(conn->dbg);
121 talloc_free(conn->dbg);
122 }
123
124 llist_del(&conn->entry);
125 talloc_free(conn);
126 return 0;
127}
128
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200129static int client_data(struct osmo_fd *fd, unsigned int what)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200130{
131 struct telnet_connection *conn = fd->data;
132 int rc = 0;
133
134 if (what & BSC_FD_READ) {
135 conn->fd.when &= ~BSC_FD_READ;
136 rc = vty_read(conn->vty);
137 }
138
139 /* vty might have been closed from vithin vty_read() */
140 if (!conn->vty)
141 return rc;
142
143 if (what & BSC_FD_WRITE) {
144 rc = buffer_flush_all(conn->vty->obuf, fd->fd);
145 if (rc == BUFFER_EMPTY)
146 conn->fd.when &= ~BSC_FD_WRITE;
147 }
148
149 return rc;
150}
151
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200152static int telnet_new_connection(struct osmo_fd *fd, unsigned int what)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200153{
154 struct telnet_connection *connection;
155 struct sockaddr_in sockaddr;
156 socklen_t len = sizeof(sockaddr);
157 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
158
159 if (new_connection < 0) {
160 LOGP(0, LOGL_ERROR, "telnet accept failed\n");
161 return new_connection;
162 }
163
164 connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
165 connection->priv = fd->data;
166 connection->fd.data = connection;
167 connection->fd.fd = new_connection;
168 connection->fd.when = BSC_FD_READ;
169 connection->fd.cb = client_data;
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200170 osmo_fd_register(&connection->fd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200171 llist_add_tail(&connection->entry, &active_connections);
172
173 print_welcome(new_connection);
174
175 connection->vty = vty_create(new_connection, connection);
176 if (!connection->vty) {
177 LOGP(0, LOGL_ERROR, "couldn't create VTY\n");
178 close(new_connection);
179 talloc_free(connection);
180 return -1;
181 }
182
183 return 0;
184}
185
186/* callback from VTY code */
187void vty_event(enum event event, int sock, struct vty *vty)
188{
189 struct telnet_connection *connection = vty->priv;
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200190 struct osmo_fd *bfd = &connection->fd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200191
192 if (vty->type != VTY_TERM)
193 return;
194
195 switch (event) {
196 case VTY_READ:
197 bfd->when |= BSC_FD_READ;
198 break;
199 case VTY_WRITE:
200 bfd->when |= BSC_FD_WRITE;
201 break;
202 case VTY_CLOSED:
203 /* vty layer is about to free() vty */
204 connection->vty = NULL;
205 telnet_close_client(bfd);
206 break;
207 default:
208 break;
209 }
210}
211