1 /* minimalistic telnet/network interface it might turn into a wire interface */
2 /* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
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.
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.
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.
21 #include <sys/socket.h>
22 #include <netinet/in.h>
28 #include <osmocom/core/msgb.h>
29 #include <osmocom/core/talloc.h>
30 #include <osmocom/core/logging.h>
32 #include <osmocom/vty/telnet_interface.h>
33 #include <osmocom/vty/buffer.h>
34 #include <osmocom/vty/command.h>
36 /* per connection data */
37 LLIST_HEAD(active_connections);
39 static void *tall_telnet_ctx;
41 /* per network data */
42 static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
44 static struct bsc_fd server_socket = {
46 .cb = telnet_new_connection,
50 int telnet_init(void *tall_ctx, void *priv, int port)
52 struct sockaddr_in sock_addr;
55 tall_telnet_ctx = talloc_named_const(tall_ctx, 1,
58 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
61 LOGP(0, LOGL_ERROR, "Telnet interface socket creation failed\n");
65 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
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);
72 rc = bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
74 LOGP(0, LOGL_ERROR, "Telnet interface failed to bind\n");
81 LOGP(0, LOGL_ERROR, "Telnet interface failed to listen\n");
86 server_socket.data = priv;
87 server_socket.fd = fd;
88 bsc_register_fd(&server_socket);
93 extern struct host host;
95 static void print_welcome(int fd)
99 "Welcome to the OpenBSC Control interface\r\n";
101 ret = write(fd, msg, strlen(msg));
103 if (host.app_info->copyright)
104 ret = write(fd, host.app_info->copyright, strlen(host.app_info->copyright));
107 int telnet_close_client(struct bsc_fd *fd)
109 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
112 bsc_unregister_fd(fd);
115 log_del_target(conn->dbg);
116 talloc_free(conn->dbg);
119 llist_del(&conn->entry);
124 static int client_data(struct bsc_fd *fd, unsigned int what)
126 struct telnet_connection *conn = fd->data;
129 if (what & BSC_FD_READ) {
130 conn->fd.when &= ~BSC_FD_READ;
131 rc = vty_read(conn->vty);
134 /* vty might have been closed from vithin vty_read() */
138 if (what & BSC_FD_WRITE) {
139 rc = buffer_flush_all(conn->vty->obuf, fd->fd);
140 if (rc == BUFFER_EMPTY)
141 conn->fd.when &= ~BSC_FD_WRITE;
147 static int telnet_new_connection(struct bsc_fd *fd, unsigned int what)
149 struct telnet_connection *connection;
150 struct sockaddr_in sockaddr;
151 socklen_t len = sizeof(sockaddr);
152 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
154 if (new_connection < 0) {
155 LOGP(0, LOGL_ERROR, "telnet accept failed\n");
156 return new_connection;
159 connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
160 connection->priv = fd->data;
161 connection->fd.data = connection;
162 connection->fd.fd = new_connection;
163 connection->fd.when = BSC_FD_READ;
164 connection->fd.cb = client_data;
165 bsc_register_fd(&connection->fd);
166 llist_add_tail(&connection->entry, &active_connections);
168 print_welcome(new_connection);
170 connection->vty = vty_create(new_connection, connection);
171 if (!connection->vty) {
172 LOGP(0, LOGL_ERROR, "couldn't create VTY\n");
173 close(new_connection);
174 talloc_free(connection);
181 /* callback from VTY code */
182 void vty_event(enum event event, int sock, struct vty *vty)
184 struct telnet_connection *connection = vty->priv;
185 struct bsc_fd *bfd = &connection->fd;
187 if (vty->type != VTY_TERM)
192 bfd->when |= BSC_FD_READ;
195 bfd->when |= BSC_FD_WRITE;
198 /* vty layer is about to free() vty */
199 connection->vty = NULL;
200 telnet_close_client(bfd);