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> |
| 23 | #include <malloc.h> |
| 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> |
Holger Freyther | 868b8cd | 2009-01-04 03:57:27 +0000 | [diff] [blame] | 33 | #include <openbsc/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> |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 37 | |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 38 | #include <vty/buffer.h> |
| 39 | |
Holger Freyther | ae61cae | 2009-01-04 03:46:01 +0000 | [diff] [blame] | 40 | #define WRITE_CONNECTION(fd, msg...) \ |
| 41 | int ret; \ |
| 42 | char buf[4096]; \ |
| 43 | snprintf(buf, sizeof(buf), msg); \ |
| 44 | ret = write(fd, buf, strlen(buf)); |
| 45 | |
| 46 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 47 | /* per connection data */ |
| 48 | LLIST_HEAD(active_connections); |
| 49 | |
| 50 | /* per network data */ |
| 51 | static int telnet_new_connection(struct bsc_fd *fd, unsigned int what); |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 52 | static int telnet_paging_callback(unsigned int subsys, unsigned int signal, |
| 53 | void *handler_data, void *signal_data); |
| 54 | static int telnet_sms_callback(unsigned int subsys, unsigned int signal, |
| 55 | void *handler_data, void *signal_data); |
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 | 404cdd8 | 2009-03-10 12:21:45 +0000 | [diff] [blame] | 67 | bsc_vty_init(network); |
| 68 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 69 | fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 70 | |
| 71 | if (fd < 0) { |
| 72 | perror("Telnet interface socket creation failed"); |
| 73 | return; |
| 74 | } |
| 75 | |
Holger Freyther | f077689 | 2009-02-03 20:49:51 +0000 | [diff] [blame] | 76 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 77 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 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 | if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) { |
| 84 | perror("Telnet interface failed to bind"); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | if (listen(fd, 0) < 0) { |
| 89 | perror("Telnet interface failed to listen"); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | server_socket.data = network; |
| 94 | server_socket.fd = fd; |
| 95 | bsc_register_fd(&server_socket); |
Holger Freyther | 7aaf112 | 2009-02-14 22:51:13 +0000 | [diff] [blame] | 96 | |
Holger Freyther | 9b17776 | 2009-02-16 19:07:18 +0000 | [diff] [blame] | 97 | /* register callbacks */ |
Harald Welte | 404cdd8 | 2009-03-10 12:21:45 +0000 | [diff] [blame] | 98 | #if 0 |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 99 | register_signal_handler(SS_PAGING, telnet_paging_callback, network); |
| 100 | register_signal_handler(SS_SMS, telnet_sms_callback, network); |
Harald Welte | 404cdd8 | 2009-03-10 12:21:45 +0000 | [diff] [blame] | 101 | #endif |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void print_welcome(int fd) { |
| 105 | int ret; |
| 106 | static char *msg = |
| 107 | "Welcome to the OpenBSC Control interface\n" |
| 108 | "Copyright (C) 2008, 2009 Harald Welte\n" |
| 109 | "Contributions by Daniel Willmann, Jan Lübbe, " |
| 110 | "Stefan Schmidt, Holger Freyther\n\n" |
| 111 | "License GPLv2+: GNU GPL version 2 or later " |
| 112 | "<http://gnu.org/licenses/gpl.html>\n" |
| 113 | "This is free software: you are free to change " |
| 114 | "and redistribute it.\n" |
| 115 | "There is NO WARRANTY, to the extent permitted " |
| 116 | "by law.\nType \"help\" to get a short introduction.\n"; |
| 117 | |
| 118 | ret = write(fd, msg, strlen(msg)); |
| 119 | } |
| 120 | |
| 121 | int telnet_close_client(struct bsc_fd *fd) { |
| 122 | struct telnet_connection *conn = (struct telnet_connection*)fd->data; |
| 123 | |
| 124 | close(fd->fd); |
| 125 | bsc_unregister_fd(fd); |
| 126 | llist_del(&conn->entry); |
| 127 | free(conn); |
| 128 | return 0; |
| 129 | } |
| 130 | |
Harald Welte | 404cdd8 | 2009-03-10 12:21:45 +0000 | [diff] [blame] | 131 | static int client_data(struct bsc_fd *fd, unsigned int what) |
| 132 | { |
| 133 | struct telnet_connection *conn = fd->data; |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 134 | int rc; |
| 135 | |
| 136 | if (what & BSC_FD_READ) { |
| 137 | conn->fd.when &= ~BSC_FD_READ; |
| 138 | rc = vty_read(conn->vty); |
| 139 | } |
| 140 | |
| 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; |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 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 | |
| 162 | connection = (struct telnet_connection*)malloc(sizeof(*connection)); |
| 163 | memset(connection, 0, sizeof(*connection)); |
| 164 | connection->network = (struct gsm_network*)fd->data; |
| 165 | connection->fd.data = connection; |
| 166 | connection->fd.fd = new_connection; |
| 167 | connection->fd.when = BSC_FD_READ; |
| 168 | connection->fd.cb = client_data; |
Holger Freyther | f87573d | 2009-01-04 03:49:41 +0000 | [diff] [blame] | 169 | connection->bts = 0; |
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 | 404cdd8 | 2009-03-10 12:21:45 +0000 | [diff] [blame] | 176 | if (!connection->vty) |
| 177 | return -1; |
| 178 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 179 | return 0; |
| 180 | } |
Holger Freyther | 7aaf112 | 2009-02-14 22:51:13 +0000 | [diff] [blame] | 181 | |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 182 | /* callback from VTY code */ |
| 183 | void vty_event(enum event event, int sock, struct vty *vty) |
| 184 | { |
| 185 | struct telnet_connection *connection = vty->priv; |
| 186 | struct bsc_fd *bfd = &connection->fd; |
| 187 | |
| 188 | switch (event) { |
| 189 | case VTY_READ: |
| 190 | bfd->when |= BSC_FD_READ; |
| 191 | break; |
| 192 | case VTY_WRITE: |
| 193 | bfd->when |= BSC_FD_WRITE; |
| 194 | break; |
| 195 | default: |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
Harald Welte | 404cdd8 | 2009-03-10 12:21:45 +0000 | [diff] [blame] | 200 | #if 0 |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 201 | static int telnet_paging_callback(unsigned int subsys, unsigned int singal, |
| 202 | void *handler_data, void *signal_data) |
Holger Freyther | 7aaf112 | 2009-02-14 22:51:13 +0000 | [diff] [blame] | 203 | { |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 204 | struct paging_signal_data *paging = signal_data; |
Holger Freyther | 7aaf112 | 2009-02-14 22:51:13 +0000 | [diff] [blame] | 205 | struct telnet_connection *con; |
| 206 | |
| 207 | llist_for_each_entry(con, &active_connections, entry) { |
| 208 | if (paging->lchan) { |
| 209 | WRITE_CONNECTION(con->fd.fd, "Paging succeeded\n"); |
| 210 | show_lchan(con->fd.fd, paging->lchan); |
| 211 | } else { |
| 212 | WRITE_CONNECTION(con->fd.fd, "Paging failed for subscriber: %s/%s/%s\n", |
| 213 | paging->subscr->imsi, |
| 214 | paging->subscr->tmsi, |
| 215 | paging->subscr->name); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return 0; |
| 220 | } |
Holger Freyther | 9b17776 | 2009-02-16 19:07:18 +0000 | [diff] [blame] | 221 | |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 222 | static int telnet_sms_callback(unsigned int subsys, unsigned int signal, |
| 223 | void *handler_data, void *signal_data) |
Holger Freyther | 9b17776 | 2009-02-16 19:07:18 +0000 | [diff] [blame] | 224 | { |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 225 | struct sms_submit *sms = signal_data; |
Holger Freyther | 9b17776 | 2009-02-16 19:07:18 +0000 | [diff] [blame] | 226 | struct telnet_connection *con; |
| 227 | |
| 228 | llist_for_each_entry(con, &active_connections, entry) { |
Harald Welte | 595ad7b | 2009-02-16 22:05:44 +0000 | [diff] [blame] | 229 | WRITE_CONNECTION(con->fd.fd, "Incoming SMS: %s\n", sms->user_data); |
Holger Freyther | 9b17776 | 2009-02-16 19:07:18 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
Harald Welte | 404cdd8 | 2009-03-10 12:21:45 +0000 | [diff] [blame] | 234 | #endif |