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 | |
| 28 | #include <openbsc/telnet_interface.h> |
| 29 | #include <openbsc/gsm_subscriber.h> |
| 30 | #include <openbsc/chan_alloc.h> |
| 31 | #include <openbsc/gsm_04_08.h> |
| 32 | #include <openbsc/gsm_04_11.h> |
| 33 | #include <openbsc/msgb.h> |
| 34 | #include <openbsc/abis_rsl.h> |
| 35 | #include <openbsc/paging.h> |
| 36 | #include <openbsc/signal.h> |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 37 | #include <openbsc/talloc.h> |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 38 | #include <openbsc/debug.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 39 | |
| 40 | #include <vty/buffer.h> |
| 41 | |
| 42 | #define WRITE_CONNECTION(fd, msg...) \ |
| 43 | int ret; \ |
| 44 | char buf[4096]; \ |
| 45 | snprintf(buf, sizeof(buf), msg); \ |
| 46 | ret = write(fd, buf, strlen(buf)); |
| 47 | |
| 48 | |
| 49 | /* per connection data */ |
| 50 | LLIST_HEAD(active_connections); |
| 51 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 52 | static void *tall_telnet_ctx; |
| 53 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 54 | /* per network data */ |
| 55 | static int telnet_new_connection(struct bsc_fd *fd, unsigned int what); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 56 | |
| 57 | static struct bsc_fd server_socket = { |
| 58 | .when = BSC_FD_READ, |
| 59 | .cb = telnet_new_connection, |
| 60 | .priv_nr = 0, |
| 61 | }; |
| 62 | |
| 63 | void telnet_init(struct gsm_network *network, int port) { |
| 64 | struct sockaddr_in sock_addr; |
| 65 | int fd, on = 1; |
| 66 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 67 | tall_telnet_ctx = talloc_named_const(tall_bsc_ctx, 1, |
| 68 | "telnet_connection"); |
| 69 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 70 | bsc_vty_init(network); |
| 71 | |
| 72 | fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 73 | |
| 74 | if (fd < 0) { |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 75 | LOGP(DNM, LOGL_ERROR, "Telnet interface socket creation failed\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
| 79 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 80 | |
| 81 | memset(&sock_addr, 0, sizeof(sock_addr)); |
| 82 | sock_addr.sin_family = AF_INET; |
| 83 | sock_addr.sin_port = htons(port); |
| 84 | sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 85 | |
| 86 | if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) { |
Harald Welte | 36ab545 | 2009-12-21 23:36:45 +0100 | [diff] [blame] | 87 | LOGP(DNM, LOGL_ERROR, "Telnet interface failed to bind\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | |
| 91 | if (listen(fd, 0) < 0) { |
Harald Welte | 36ab545 | 2009-12-21 23:36:45 +0100 | [diff] [blame] | 92 | LOGP(DNM, LOGL_ERROR, "Telnet interface failed to listen\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
| 96 | server_socket.data = network; |
| 97 | server_socket.fd = fd; |
| 98 | bsc_register_fd(&server_socket); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static void print_welcome(int fd) { |
| 102 | int ret; |
| 103 | static char *msg = |
| 104 | "Welcome to the OpenBSC Control interface\n" |
| 105 | "Copyright (C) 2008, 2009 Harald Welte\n" |
| 106 | "Contributions by Daniel Willmann, Jan Lübbe, " |
| 107 | "Stefan Schmidt, Holger Freyther\n\n" |
| 108 | "License GPLv2+: GNU GPL version 2 or later " |
| 109 | "<http://gnu.org/licenses/gpl.html>\n" |
| 110 | "This is free software: you are free to change " |
| 111 | "and redistribute it.\n" |
| 112 | "There is NO WARRANTY, to the extent permitted " |
| 113 | "by law.\nType \"help\" to get a short introduction.\n"; |
| 114 | |
| 115 | ret = write(fd, msg, strlen(msg)); |
| 116 | } |
| 117 | |
| 118 | int telnet_close_client(struct bsc_fd *fd) { |
| 119 | struct telnet_connection *conn = (struct telnet_connection*)fd->data; |
| 120 | |
| 121 | close(fd->fd); |
| 122 | bsc_unregister_fd(fd); |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 123 | |
| 124 | if (conn->dbg) { |
| 125 | debug_del_target(conn->dbg); |
| 126 | talloc_free(conn->dbg); |
| 127 | } |
| 128 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 129 | llist_del(&conn->entry); |
Harald Welte | 49a8405 | 2009-06-22 01:36:25 +0200 | [diff] [blame] | 130 | talloc_free(conn); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static int client_data(struct bsc_fd *fd, unsigned int what) |
| 135 | { |
| 136 | struct telnet_connection *conn = fd->data; |
| 137 | int rc = 0; |
| 138 | |
| 139 | if (what & BSC_FD_READ) { |
| 140 | conn->fd.when &= ~BSC_FD_READ; |
| 141 | rc = vty_read(conn->vty); |
| 142 | } |
| 143 | |
Harald Welte | d1bc77e | 2009-08-07 00:31:23 +0200 | [diff] [blame] | 144 | /* vty might have been closed from vithin vty_read() */ |
| 145 | if (!conn->vty) |
| 146 | return rc; |
| 147 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 148 | if (what & BSC_FD_WRITE) { |
| 149 | rc = buffer_flush_all(conn->vty->obuf, fd->fd); |
| 150 | if (rc == BUFFER_EMPTY) |
| 151 | conn->fd.when &= ~BSC_FD_WRITE; |
| 152 | } |
| 153 | |
| 154 | return rc; |
| 155 | } |
| 156 | |
| 157 | static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) { |
| 158 | struct telnet_connection *connection; |
| 159 | struct sockaddr_in sockaddr; |
| 160 | socklen_t len = sizeof(sockaddr); |
| 161 | int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len); |
| 162 | |
| 163 | if (new_connection < 0) { |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 164 | LOGP(DNM, LOGL_ERROR, "telnet accept failed\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | |
Harald Welte | 857e00d | 2009-06-26 20:25:23 +0200 | [diff] [blame] | 169 | connection = talloc_zero(tall_telnet_ctx, struct telnet_connection); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 170 | connection->network = (struct gsm_network*)fd->data; |
| 171 | connection->fd.data = connection; |
| 172 | connection->fd.fd = new_connection; |
| 173 | connection->fd.when = BSC_FD_READ; |
| 174 | connection->fd.cb = client_data; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 175 | bsc_register_fd(&connection->fd); |
| 176 | llist_add_tail(&connection->entry, &active_connections); |
| 177 | |
| 178 | print_welcome(new_connection); |
| 179 | |
| 180 | connection->vty = vty_create(new_connection, connection); |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 181 | if (!connection->vty) { |
| 182 | LOGP(DNM, LOGL_ERROR, "couldn't create VTY\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 183 | return -1; |
Harald Welte | cf2ec4a | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 184 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /* callback from VTY code */ |
| 190 | void vty_event(enum event event, int sock, struct vty *vty) |
| 191 | { |
| 192 | struct telnet_connection *connection = vty->priv; |
| 193 | struct bsc_fd *bfd = &connection->fd; |
| 194 | |
Harald Welte | d1bc77e | 2009-08-07 00:31:23 +0200 | [diff] [blame] | 195 | if (vty->type != VTY_TERM) |
| 196 | return; |
| 197 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 198 | switch (event) { |
| 199 | case VTY_READ: |
| 200 | bfd->when |= BSC_FD_READ; |
| 201 | break; |
| 202 | case VTY_WRITE: |
| 203 | bfd->when |= BSC_FD_WRITE; |
| 204 | break; |
Harald Welte | d1bc77e | 2009-08-07 00:31:23 +0200 | [diff] [blame] | 205 | case VTY_CLOSED: |
| 206 | /* vty layer is about to free() vty */ |
| 207 | connection->vty = NULL; |
| 208 | telnet_close_client(bfd); |
| 209 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 210 | default: |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | |