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