Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1 | /* 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 Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 28 | #include <osmocom/core/msgb.h> |
| 29 | #include <osmocom/core/talloc.h> |
| 30 | #include <osmocom/core/logging.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 31 | |
| 32 | #include <osmocom/vty/telnet_interface.h> |
| 33 | #include <osmocom/vty/buffer.h> |
Holger Hans Peter Freyther | 2e228fc | 2010-09-11 13:41:41 +0800 | [diff] [blame] | 34 | #include <osmocom/vty/command.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 35 | |
Harald Welte | e881b1b | 2011-08-17 18:52:30 +0200 | [diff] [blame] | 36 | /*! \addtogroup telnet_interface |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 37 | * @{ |
| 38 | */ |
| 39 | /*! \file telnet_interface.c */ |
| 40 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 41 | /* per connection data */ |
| 42 | LLIST_HEAD(active_connections); |
| 43 | |
| 44 | static void *tall_telnet_ctx; |
| 45 | |
| 46 | /* per network data */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 47 | static int telnet_new_connection(struct osmo_fd *fd, unsigned int what); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 48 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 49 | static struct osmo_fd server_socket = { |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 50 | .when = BSC_FD_READ, |
| 51 | .cb = telnet_new_connection, |
| 52 | .priv_nr = 0, |
| 53 | }; |
| 54 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 55 | /*! \brief Initialize telnet based VTY interface |
| 56 | * \param[in] tall_ctx \ref talloc context |
| 57 | * \param[in] priv private data to be passed to callback |
| 58 | * \param[in] port UDP port number |
| 59 | */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 60 | int telnet_init(void *tall_ctx, void *priv, int port) |
| 61 | { |
| 62 | struct sockaddr_in sock_addr; |
| 63 | int fd, rc, on = 1; |
| 64 | |
| 65 | tall_telnet_ctx = talloc_named_const(tall_ctx, 1, |
| 66 | "telnet_connection"); |
| 67 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 68 | /* FIXME: use new socket.c code of libosmocore */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 69 | fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 70 | |
| 71 | if (fd < 0) { |
| 72 | LOGP(0, LOGL_ERROR, "Telnet interface socket creation failed\n"); |
| 73 | return fd; |
| 74 | } |
| 75 | |
| 76 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 77 | |
| 78 | memset(&sock_addr, 0, sizeof(sock_addr)); |
| 79 | sock_addr.sin_family = AF_INET; |
| 80 | sock_addr.sin_port = htons(port); |
| 81 | sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 82 | |
| 83 | rc = bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)); |
Sylvain Munaut | 4a4f96d | 2011-01-03 22:19:40 +0100 | [diff] [blame] | 84 | if (rc < 0) { |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 85 | LOGP(0, LOGL_ERROR, "Telnet interface failed to bind\n"); |
| 86 | close(fd); |
| 87 | return rc; |
| 88 | } |
| 89 | |
| 90 | rc = listen(fd, 0); |
| 91 | if (rc < 0) { |
| 92 | LOGP(0, LOGL_ERROR, "Telnet interface failed to listen\n"); |
| 93 | close(fd); |
| 94 | return rc; |
| 95 | } |
| 96 | |
| 97 | server_socket.data = priv; |
| 98 | server_socket.fd = fd; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 99 | osmo_fd_register(&server_socket); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Holger Hans Peter Freyther | 2e228fc | 2010-09-11 13:41:41 +0800 | [diff] [blame] | 104 | extern struct host host; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 105 | |
| 106 | static void print_welcome(int fd) |
| 107 | { |
Harald Welte | b62b04b | 2011-05-22 19:15:07 +0200 | [diff] [blame] | 108 | static const char *msg1 = "Welcome to the "; |
| 109 | static const char *msg2 = " control interface\r\n"; |
Harald Welte | 9c3cbfb | 2011-07-16 11:59:44 +0200 | [diff] [blame] | 110 | const char *app_name = "<unnamed>"; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 111 | |
Harald Welte | b62b04b | 2011-05-22 19:15:07 +0200 | [diff] [blame] | 112 | if (host.app_info->name) |
| 113 | app_name = host.app_info->name; |
| 114 | |
Harald Welte | 2b32215 | 2011-07-16 12:01:52 +0200 | [diff] [blame] | 115 | write(fd, msg1, strlen(msg1)); |
| 116 | write(fd, app_name, strlen(app_name)); |
| 117 | write(fd, msg2, strlen(msg2)); |
Holger Hans Peter Freyther | 2e228fc | 2010-09-11 13:41:41 +0800 | [diff] [blame] | 118 | |
| 119 | if (host.app_info->copyright) |
Harald Welte | 2b32215 | 2011-07-16 12:01:52 +0200 | [diff] [blame] | 120 | write(fd, host.app_info->copyright, strlen(host.app_info->copyright)); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 123 | /*! \brief close a telnet connection */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 124 | int telnet_close_client(struct osmo_fd *fd) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 125 | { |
| 126 | struct telnet_connection *conn = (struct telnet_connection*)fd->data; |
| 127 | |
| 128 | close(fd->fd); |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 129 | osmo_fd_unregister(fd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 130 | |
| 131 | if (conn->dbg) { |
| 132 | log_del_target(conn->dbg); |
| 133 | talloc_free(conn->dbg); |
| 134 | } |
| 135 | |
| 136 | llist_del(&conn->entry); |
| 137 | talloc_free(conn); |
| 138 | return 0; |
| 139 | } |
| 140 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 141 | static int client_data(struct osmo_fd *fd, unsigned int what) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 142 | { |
| 143 | struct telnet_connection *conn = fd->data; |
| 144 | int rc = 0; |
| 145 | |
| 146 | if (what & BSC_FD_READ) { |
| 147 | conn->fd.when &= ~BSC_FD_READ; |
| 148 | rc = vty_read(conn->vty); |
| 149 | } |
| 150 | |
| 151 | /* vty might have been closed from vithin vty_read() */ |
| 152 | if (!conn->vty) |
| 153 | return rc; |
| 154 | |
| 155 | if (what & BSC_FD_WRITE) { |
| 156 | rc = buffer_flush_all(conn->vty->obuf, fd->fd); |
| 157 | if (rc == BUFFER_EMPTY) |
| 158 | conn->fd.when &= ~BSC_FD_WRITE; |
| 159 | } |
| 160 | |
| 161 | return rc; |
| 162 | } |
| 163 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 164 | static int telnet_new_connection(struct osmo_fd *fd, unsigned int what) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 165 | { |
| 166 | struct telnet_connection *connection; |
| 167 | struct sockaddr_in sockaddr; |
| 168 | socklen_t len = sizeof(sockaddr); |
| 169 | int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len); |
| 170 | |
| 171 | if (new_connection < 0) { |
| 172 | LOGP(0, LOGL_ERROR, "telnet accept failed\n"); |
| 173 | return new_connection; |
| 174 | } |
| 175 | |
| 176 | connection = talloc_zero(tall_telnet_ctx, struct telnet_connection); |
| 177 | connection->priv = fd->data; |
| 178 | connection->fd.data = connection; |
| 179 | connection->fd.fd = new_connection; |
| 180 | connection->fd.when = BSC_FD_READ; |
| 181 | connection->fd.cb = client_data; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 182 | osmo_fd_register(&connection->fd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 183 | llist_add_tail(&connection->entry, &active_connections); |
| 184 | |
| 185 | print_welcome(new_connection); |
| 186 | |
| 187 | connection->vty = vty_create(new_connection, connection); |
| 188 | if (!connection->vty) { |
| 189 | LOGP(0, LOGL_ERROR, "couldn't create VTY\n"); |
| 190 | close(new_connection); |
| 191 | talloc_free(connection); |
| 192 | return -1; |
| 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 198 | /*! \brief callback from core VTY code about VTY related events */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 199 | void vty_event(enum event event, int sock, struct vty *vty) |
| 200 | { |
| 201 | struct telnet_connection *connection = vty->priv; |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 202 | struct osmo_fd *bfd = &connection->fd; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 203 | |
| 204 | if (vty->type != VTY_TERM) |
| 205 | return; |
| 206 | |
| 207 | switch (event) { |
| 208 | case VTY_READ: |
| 209 | bfd->when |= BSC_FD_READ; |
| 210 | break; |
| 211 | case VTY_WRITE: |
| 212 | bfd->when |= BSC_FD_WRITE; |
| 213 | break; |
| 214 | case VTY_CLOSED: |
| 215 | /* vty layer is about to free() vty */ |
| 216 | connection->vty = NULL; |
| 217 | telnet_close_client(bfd); |
| 218 | break; |
| 219 | default: |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 224 | /*! }@ */ |