Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [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 | |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 28 | #include <osmocore/msgb.h> |
Harald Welte | f4625b1 | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 29 | #include <osmocore/talloc.h> |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 30 | #include <osmocore/logging.h> |
| 31 | #include <openbsc/telnet_interface.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 32 | |
| 33 | #include <vty/buffer.h> |
| 34 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 35 | /* per connection data */ |
| 36 | LLIST_HEAD(active_connections); |
| 37 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 38 | static void *tall_telnet_ctx; |
| 39 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 40 | /* per network data */ |
| 41 | static int telnet_new_connection(struct bsc_fd *fd, unsigned int what); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 42 | |
| 43 | static struct bsc_fd server_socket = { |
| 44 | .when = BSC_FD_READ, |
| 45 | .cb = telnet_new_connection, |
| 46 | .priv_nr = 0, |
| 47 | }; |
| 48 | |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 49 | int telnet_init(void *tall_ctx, void *priv, int port) |
Harald Welte | 67798f7 | 2010-05-16 19:30:28 +0200 | [diff] [blame] | 50 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 51 | struct sockaddr_in sock_addr; |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 52 | int fd, rc, on = 1; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 53 | |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 54 | tall_telnet_ctx = talloc_named_const(tall_ctx, 1, |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 55 | "telnet_connection"); |
| 56 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 57 | fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 58 | |
| 59 | if (fd < 0) { |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 60 | LOGP(0, LOGL_ERROR, "Telnet interface socket creation failed\n"); |
| 61 | return fd; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 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 | |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 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; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 76 | } |
| 77 | |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 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; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 83 | } |
| 84 | |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 85 | server_socket.data = priv; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 86 | server_socket.fd = fd; |
| 87 | bsc_register_fd(&server_socket); |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 88 | |
| 89 | return 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 90 | } |
| 91 | |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 92 | extern const char *openbsc_copyright; |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 93 | |
Harald Welte | 67798f7 | 2010-05-16 19:30:28 +0200 | [diff] [blame] | 94 | static void print_welcome(int fd) |
| 95 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 96 | int ret; |
| 97 | static char *msg = |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 98 | "Welcome to the OpenBSC Control interface\n"; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 99 | |
| 100 | ret = write(fd, msg, strlen(msg)); |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 101 | ret = write(fd, openbsc_copyright, strlen(openbsc_copyright)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 102 | } |
| 103 | |
Harald Welte | 67798f7 | 2010-05-16 19:30:28 +0200 | [diff] [blame] | 104 | int telnet_close_client(struct bsc_fd *fd) |
| 105 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 106 | struct telnet_connection *conn = (struct telnet_connection*)fd->data; |
| 107 | |
| 108 | close(fd->fd); |
| 109 | bsc_unregister_fd(fd); |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 110 | |
| 111 | if (conn->dbg) { |
Harald Welte | 51d2a59 | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 112 | log_del_target(conn->dbg); |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 113 | talloc_free(conn->dbg); |
| 114 | } |
| 115 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 116 | llist_del(&conn->entry); |
Harald Welte | 49a8405 | 2009-06-22 01:36:25 +0200 | [diff] [blame] | 117 | talloc_free(conn); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static 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 Welte | d1bc77e | 2009-08-07 00:31:23 +0200 | [diff] [blame] | 131 | /* vty might have been closed from vithin vty_read() */ |
| 132 | if (!conn->vty) |
| 133 | return rc; |
| 134 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 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 | |
Harald Welte | 67798f7 | 2010-05-16 19:30:28 +0200 | [diff] [blame] | 144 | static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) |
| 145 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 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) { |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 152 | LOGP(0, LOGL_ERROR, "telnet accept failed\n"); |
| 153 | return new_connection; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 154 | } |
| 155 | |
Harald Welte | 857e00d | 2009-06-26 20:25:23 +0200 | [diff] [blame] | 156 | connection = talloc_zero(tall_telnet_ctx, struct telnet_connection); |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 157 | connection->priv = fd->data; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 158 | connection->fd.data = connection; |
| 159 | connection->fd.fd = new_connection; |
| 160 | connection->fd.when = BSC_FD_READ; |
| 161 | connection->fd.cb = client_data; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 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); |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 168 | if (!connection->vty) { |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 169 | LOGP(0, LOGL_ERROR, "couldn't create VTY\n"); |
| 170 | close(new_connection); |
| 171 | talloc_free(connection); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 172 | return -1; |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 173 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* callback from VTY code */ |
| 179 | void 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 Welte | d1bc77e | 2009-08-07 00:31:23 +0200 | [diff] [blame] | 184 | if (vty->type != VTY_TERM) |
| 185 | return; |
| 186 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 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; |
Harald Welte | d1bc77e | 2009-08-07 00:31:23 +0200 | [diff] [blame] | 194 | case VTY_CLOSED: |
| 195 | /* vty layer is about to free() vty */ |
| 196 | connection->vty = NULL; |
| 197 | telnet_close_client(bfd); |
| 198 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 199 | default: |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |