Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [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> |
Harald Welte | 12247c6 | 2009-05-21 07:23:02 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <openbsc/telnet_interface.h> |
Holger Freyther | ae61cae | 2009-01-04 03:46:01 +0000 | [diff] [blame] | 29 | #include <openbsc/gsm_subscriber.h> |
Holger Freyther | f87573d | 2009-01-04 03:49:41 +0000 | [diff] [blame] | 30 | #include <openbsc/chan_alloc.h> |
Holger Freyther | 3ae8fd2 | 2009-01-04 03:50:40 +0000 | [diff] [blame] | 31 | #include <openbsc/gsm_04_08.h> |
Holger Freyther | 9b17776 | 2009-02-16 19:07:18 +0000 | [diff] [blame] | 32 | #include <openbsc/gsm_04_11.h> |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 33 | #include <osmocore/msgb.h> |
Holger Freyther | 7448a53 | 2009-01-04 20:18:23 +0000 | [diff] [blame] | 34 | #include <openbsc/abis_rsl.h> |
Harald Welte | 38c2f13 | 2009-01-06 23:10:57 +0000 | [diff] [blame] | 35 | #include <openbsc/paging.h> |
Holger Freyther | 7aaf112 | 2009-02-14 22:51:13 +0000 | [diff] [blame] | 36 | #include <openbsc/signal.h> |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 37 | #include <osmocore/talloc.h> |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 38 | #include <openbsc/debug.h> |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 39 | |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 40 | #include <vty/buffer.h> |
| 41 | |
Holger Freyther | ae61cae | 2009-01-04 03:46:01 +0000 | [diff] [blame] | 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 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 49 | /* per connection data */ |
| 50 | LLIST_HEAD(active_connections); |
| 51 | |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 52 | static void *tall_telnet_ctx; |
| 53 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 54 | /* per network data */ |
| 55 | static int telnet_new_connection(struct bsc_fd *fd, unsigned int what); |
Holger Freyther | 7aaf112 | 2009-02-14 22:51:13 +0000 | [diff] [blame] | 56 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 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; |
Holger Freyther | f077689 | 2009-02-03 20:49:51 +0000 | [diff] [blame] | 65 | int fd, on = 1; |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 66 | |
Harald Welte | 2cf161b | 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 | 404cdd8 | 2009-03-10 12:21:45 +0000 | [diff] [blame] | 70 | bsc_vty_init(network); |
| 71 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 72 | fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 73 | |
| 74 | if (fd < 0) { |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 75 | LOGP(DNM, LOGL_ERROR, "Telnet interface socket creation failed\n"); |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
Holger Freyther | f077689 | 2009-02-03 20:49:51 +0000 | [diff] [blame] | 79 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 80 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 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 | a992a36 | 2009-12-21 23:36:45 +0100 | [diff] [blame] | 87 | LOGP(DNM, LOGL_ERROR, "Telnet interface failed to bind\n"); |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | |
| 91 | if (listen(fd, 0) < 0) { |
Harald Welte | a992a36 | 2009-12-21 23:36:45 +0100 | [diff] [blame] | 92 | LOGP(DNM, LOGL_ERROR, "Telnet interface failed to listen\n"); |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
| 96 | server_socket.data = network; |
| 97 | server_socket.fd = fd; |
| 98 | bsc_register_fd(&server_socket); |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Harald Welte | 5a29c7f | 2010-03-23 00:09:32 +0800 | [diff] [blame^] | 101 | extern const char *openbsc_copyright; |
| 102 | extern const char *openbsc_version; |
| 103 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 104 | static void print_welcome(int fd) { |
| 105 | int ret; |
| 106 | static char *msg = |
Harald Welte | 5a29c7f | 2010-03-23 00:09:32 +0800 | [diff] [blame^] | 107 | "Welcome to the OpenBSC Control interface\n"; |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 108 | |
| 109 | ret = write(fd, msg, strlen(msg)); |
Harald Welte | 5a29c7f | 2010-03-23 00:09:32 +0800 | [diff] [blame^] | 110 | ret = write(fd, openbsc_copyright, strlen(openbsc_copyright)); |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | int telnet_close_client(struct bsc_fd *fd) { |
| 114 | struct telnet_connection *conn = (struct telnet_connection*)fd->data; |
| 115 | |
| 116 | close(fd->fd); |
| 117 | bsc_unregister_fd(fd); |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 118 | |
| 119 | if (conn->dbg) { |
| 120 | debug_del_target(conn->dbg); |
| 121 | talloc_free(conn->dbg); |
| 122 | } |
| 123 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 124 | llist_del(&conn->entry); |
Harald Welte | a4ffea9 | 2009-06-22 01:36:25 +0200 | [diff] [blame] | 125 | talloc_free(conn); |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
Harald Welte | 404cdd8 | 2009-03-10 12:21:45 +0000 | [diff] [blame] | 129 | static int client_data(struct bsc_fd *fd, unsigned int what) |
| 130 | { |
| 131 | struct telnet_connection *conn = fd->data; |
Harald Welte | 703af98 | 2009-05-23 06:14:44 +0000 | [diff] [blame] | 132 | int rc = 0; |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 133 | |
| 134 | if (what & BSC_FD_READ) { |
| 135 | conn->fd.when &= ~BSC_FD_READ; |
| 136 | rc = vty_read(conn->vty); |
| 137 | } |
| 138 | |
Harald Welte | 3820254 | 2009-08-07 00:31:23 +0200 | [diff] [blame] | 139 | /* vty might have been closed from vithin vty_read() */ |
| 140 | if (!conn->vty) |
| 141 | return rc; |
| 142 | |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 143 | if (what & BSC_FD_WRITE) { |
| 144 | rc = buffer_flush_all(conn->vty->obuf, fd->fd); |
| 145 | if (rc == BUFFER_EMPTY) |
| 146 | conn->fd.when &= ~BSC_FD_WRITE; |
| 147 | } |
| 148 | |
| 149 | return rc; |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) { |
| 153 | struct telnet_connection *connection; |
| 154 | struct sockaddr_in sockaddr; |
| 155 | socklen_t len = sizeof(sockaddr); |
| 156 | int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len); |
| 157 | |
| 158 | if (new_connection < 0) { |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 159 | LOGP(DNM, LOGL_ERROR, "telnet accept failed\n"); |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | |
Harald Welte | 470ec29 | 2009-06-26 20:25:23 +0200 | [diff] [blame] | 164 | connection = talloc_zero(tall_telnet_ctx, struct telnet_connection); |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 165 | connection->network = (struct gsm_network*)fd->data; |
| 166 | connection->fd.data = connection; |
| 167 | connection->fd.fd = new_connection; |
| 168 | connection->fd.when = BSC_FD_READ; |
| 169 | connection->fd.cb = client_data; |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 170 | bsc_register_fd(&connection->fd); |
| 171 | llist_add_tail(&connection->entry, &active_connections); |
| 172 | |
| 173 | print_welcome(new_connection); |
| 174 | |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 175 | connection->vty = vty_create(new_connection, connection); |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 176 | if (!connection->vty) { |
| 177 | LOGP(DNM, LOGL_ERROR, "couldn't create VTY\n"); |
Harald Welte | 404cdd8 | 2009-03-10 12:21:45 +0000 | [diff] [blame] | 178 | return -1; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 179 | } |
Harald Welte | 404cdd8 | 2009-03-10 12:21:45 +0000 | [diff] [blame] | 180 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 181 | return 0; |
| 182 | } |
Holger Freyther | 7aaf112 | 2009-02-14 22:51:13 +0000 | [diff] [blame] | 183 | |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 184 | /* callback from VTY code */ |
| 185 | void vty_event(enum event event, int sock, struct vty *vty) |
| 186 | { |
| 187 | struct telnet_connection *connection = vty->priv; |
| 188 | struct bsc_fd *bfd = &connection->fd; |
| 189 | |
Harald Welte | 3820254 | 2009-08-07 00:31:23 +0200 | [diff] [blame] | 190 | if (vty->type != VTY_TERM) |
| 191 | return; |
| 192 | |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 193 | switch (event) { |
| 194 | case VTY_READ: |
| 195 | bfd->when |= BSC_FD_READ; |
| 196 | break; |
| 197 | case VTY_WRITE: |
| 198 | bfd->when |= BSC_FD_WRITE; |
| 199 | break; |
Harald Welte | 3820254 | 2009-08-07 00:31:23 +0200 | [diff] [blame] | 200 | case VTY_CLOSED: |
| 201 | /* vty layer is about to free() vty */ |
| 202 | connection->vty = NULL; |
| 203 | telnet_close_client(bfd); |
| 204 | break; |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 205 | default: |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |