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 | 868b8cd | 2009-01-04 03:57:27 +0000 | [diff] [blame] | 32 | #include <openbsc/msgb.h> |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 33 | |
| 34 | extern void telnet_parse(struct telnet_connection *connection, char *line); |
| 35 | |
Holger Freyther | ae61cae | 2009-01-04 03:46:01 +0000 | [diff] [blame] | 36 | #define WRITE_CONNECTION(fd, msg...) \ |
| 37 | int ret; \ |
| 38 | char buf[4096]; \ |
| 39 | snprintf(buf, sizeof(buf), msg); \ |
| 40 | ret = write(fd, buf, strlen(buf)); |
| 41 | |
| 42 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 43 | /* per connection data */ |
| 44 | LLIST_HEAD(active_connections); |
| 45 | |
| 46 | /* per network data */ |
| 47 | static int telnet_new_connection(struct bsc_fd *fd, unsigned int what); |
| 48 | static struct bsc_fd server_socket = { |
| 49 | .when = BSC_FD_READ, |
| 50 | .cb = telnet_new_connection, |
| 51 | .priv_nr = 0, |
| 52 | }; |
| 53 | |
| 54 | void telnet_init(struct gsm_network *network, int port) { |
| 55 | struct sockaddr_in sock_addr; |
| 56 | int fd; |
| 57 | |
| 58 | fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 59 | |
| 60 | if (fd < 0) { |
| 61 | perror("Telnet interface socket creation failed"); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | memset(&sock_addr, 0, sizeof(sock_addr)); |
| 66 | sock_addr.sin_family = AF_INET; |
| 67 | sock_addr.sin_port = htons(port); |
| 68 | sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 69 | |
| 70 | if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) { |
| 71 | perror("Telnet interface failed to bind"); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if (listen(fd, 0) < 0) { |
| 76 | perror("Telnet interface failed to listen"); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | server_socket.data = network; |
| 81 | server_socket.fd = fd; |
| 82 | bsc_register_fd(&server_socket); |
| 83 | } |
| 84 | |
| 85 | void telnet_write_help(int fd) { |
| 86 | int ret; |
| 87 | static char *msg = |
| 88 | "Help for the ad-hoc telnet command line interface\n" |
| 89 | "The generic pattern is CMD LEN DATA\\n or just CMD\n" |
| 90 | "where CMD is one of the following:\n" |
| 91 | "help\n" |
| 92 | "page IMSI (type)\n" |
| 93 | "call IMSI (number)\n" |
| 94 | "get_channel IMSI Add use count on an active channel\n" |
| 95 | "put_channel IMSI Remove use count on an active channel\n" |
Holger Freyther | ae61cae | 2009-01-04 03:46:01 +0000 | [diff] [blame] | 96 | "show This will show the channel allocation\n" |
Holger Freyther | 868b8cd | 2009-01-04 03:57:27 +0000 | [diff] [blame] | 97 | "48 IMSI 0xAB 0xEF...Send GSM 04.08. proto and msg byte then data\n" |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 98 | "11 IMSI 0xAB 0xEF...Send GSM 04.11\n"; |
| 99 | |
| 100 | ret = write(fd, msg, strlen(msg)); |
| 101 | } |
| 102 | |
| 103 | static void print_welcome(int fd) { |
| 104 | int ret; |
| 105 | static char *msg = |
| 106 | "Welcome to the OpenBSC Control interface\n" |
| 107 | "Copyright (C) 2008, 2009 Harald Welte\n" |
| 108 | "Contributions by Daniel Willmann, Jan Lübbe, " |
| 109 | "Stefan Schmidt, Holger Freyther\n\n" |
| 110 | "License GPLv2+: GNU GPL version 2 or later " |
| 111 | "<http://gnu.org/licenses/gpl.html>\n" |
| 112 | "This is free software: you are free to change " |
| 113 | "and redistribute it.\n" |
| 114 | "There is NO WARRANTY, to the extent permitted " |
| 115 | "by law.\nType \"help\" to get a short introduction.\n"; |
| 116 | |
| 117 | ret = write(fd, msg, strlen(msg)); |
| 118 | } |
| 119 | |
| 120 | int telnet_close_client(struct bsc_fd *fd) { |
| 121 | struct telnet_connection *conn = (struct telnet_connection*)fd->data; |
| 122 | |
| 123 | close(fd->fd); |
| 124 | bsc_unregister_fd(fd); |
| 125 | llist_del(&conn->entry); |
| 126 | free(conn); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | void telnet_error_client(int fd) { |
| 131 | int ret; |
| 132 | static char *msg = "Something went wrong. Please try again.\n"; |
| 133 | |
| 134 | printf("Error\n"); |
| 135 | ret = write(fd, msg, strlen(msg)); |
| 136 | } |
| 137 | |
| 138 | void telnet_page(struct telnet_connection *connection, const char *imsi, int page) { |
| 139 | printf("going to page: '%s' %d\n", imsi, page); |
| 140 | } |
| 141 | |
Holger Freyther | f87573d | 2009-01-04 03:49:41 +0000 | [diff] [blame] | 142 | static struct gsm_lchan* find_channel(struct gsm_bts *bts, const char *imsi, |
| 143 | const char **error, int fd) { |
| 144 | int ret; |
| 145 | struct gsm_lchan *lchan; |
| 146 | struct gsm_subscriber *subscr; |
| 147 | |
| 148 | subscr = subscr_get_by_imsi(imsi); |
| 149 | if (!subscr) { |
| 150 | ret = write(fd, error[0], strlen(error[0])); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | lchan = lchan_find(bts, subscr); |
| 155 | if (!lchan) |
| 156 | ret = write(fd, error[1], strlen(error[1])); |
| 157 | |
| 158 | subscr_put(subscr); |
| 159 | return lchan; |
| 160 | } |
| 161 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 162 | void telnet_put_channel(struct telnet_connection *connection, const char *imsi) { |
Holger Freyther | f87573d | 2009-01-04 03:49:41 +0000 | [diff] [blame] | 163 | static const char* error[] = { |
| 164 | "put_channel: IMSI not found\n", |
| 165 | "put_channel: No channel allocated for IMSI\n" }; |
| 166 | struct gsm_bts *bts = &connection->network->bts[connection->bts]; |
| 167 | struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd); |
| 168 | |
| 169 | if (!lchan) |
| 170 | return; |
| 171 | |
| 172 | put_lchan(lchan); |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void telnet_get_channel(struct telnet_connection *connection, const char *imsi) { |
Holger Freyther | f87573d | 2009-01-04 03:49:41 +0000 | [diff] [blame] | 176 | static const char* error[] = { |
| 177 | "get_channel: IMSI not found\n", |
| 178 | "get_channel: No channel allocated for IMSI\n" }; |
| 179 | struct gsm_bts *bts = &connection->network->bts[connection->bts]; |
| 180 | struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd); |
| 181 | |
| 182 | if (!lchan) |
| 183 | return; |
| 184 | |
| 185 | use_lchan(lchan); |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void telnet_call(struct telnet_connection *connection, const char* imsi, |
| 189 | const char *origin) { |
Holger Freyther | 3ae8fd2 | 2009-01-04 03:50:40 +0000 | [diff] [blame] | 190 | static const char* error[] = { |
| 191 | "call: IMSI not found\n", |
| 192 | "call: No channel allocated for IMSI\n" }; |
| 193 | struct gsm_bts *bts = &connection->network->bts[connection->bts]; |
| 194 | struct gsm_lchan *lchan = find_channel(bts, imsi, error, connection->fd.fd); |
| 195 | |
| 196 | if (!lchan) |
| 197 | return; |
| 198 | |
| 199 | /* TODO: add the origin */ |
| 200 | gsm48_cc_tx_setup(lchan); |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void telnet_send_gsm_48(struct telnet_connection *connection) { |
Holger Freyther | 868b8cd | 2009-01-04 03:57:27 +0000 | [diff] [blame] | 204 | static const char* error[] = { |
| 205 | "48: IMSI not found\n", |
| 206 | "48: No channel allocated for IMSI\n" }; |
| 207 | struct gsm_bts *bts = &connection->network->bts[connection->bts]; |
| 208 | struct gsm_lchan *lchan = find_channel(bts, connection->imsi, error, connection->fd.fd); |
| 209 | |
| 210 | if (!lchan) |
| 211 | return; |
| 212 | |
| 213 | struct msgb *msg = gsm48_msgb_alloc(); |
| 214 | struct gsm48_hdr *gh; |
| 215 | int i; |
| 216 | |
| 217 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + connection->read-2); |
| 218 | msg->lchan = lchan; |
| 219 | |
| 220 | gh->proto_discr = connection->commands[0]; |
| 221 | gh->msg_type = connection->commands[1]; |
| 222 | for (i = 2; i < connection->read; ++i) |
| 223 | gh->data[i-2] = connection->commands[i]; |
| 224 | |
| 225 | return gsm48_sendmsg(msg); |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void telnet_send_gsm_11(struct telnet_connection *connection) { |
| 229 | printf("sending gsm04.11 message\n"); |
| 230 | } |
| 231 | |
Holger Freyther | ae61cae | 2009-01-04 03:46:01 +0000 | [diff] [blame] | 232 | static void show_bts(int fd, struct gsm_bts *bts) { |
| 233 | WRITE_CONNECTION(fd, |
| 234 | "BTS #%u on link %u LOC: %u TRX: %d CCCH0: arfcn:%u,#%u\n", |
| 235 | bts->nr, bts->bts_nr, bts->location_area_code, |
| 236 | bts->num_trx, bts->c0->arfcn, bts->c0->nr) |
| 237 | } |
| 238 | |
| 239 | static void show_trx(int fd, struct gsm_bts_trx *trx) { |
| 240 | WRITE_CONNECTION(fd, |
| 241 | " TRX: %u ARFCN: %u\n", |
| 242 | trx->nr, trx->arfcn) |
| 243 | } |
| 244 | |
| 245 | static void show_ts(int fd, struct gsm_bts_trx_ts *ts) { |
| 246 | WRITE_CONNECTION(fd, |
| 247 | " TS: #%u pchan: %d flags: %u\n", |
| 248 | ts->nr, ts->pchan, ts->flags); |
| 249 | } |
| 250 | |
| 251 | static void show_lchan(int fd, struct gsm_lchan *lchan) { |
| 252 | struct gsm_subscriber *subscr = lchan->subscr; |
| 253 | WRITE_CONNECTION(fd, |
| 254 | " LCHAN: #%u type: %d subscriber: %s/%s/%s use: %d loc: %p\n", |
| 255 | lchan->nr, lchan->type, |
| 256 | subscr ? subscr->imsi : "na", |
| 257 | subscr ? subscr->tmsi : "na", |
| 258 | subscr ? subscr->name : "na", |
| 259 | lchan->use_count, lchan->loc_operation); |
| 260 | } |
| 261 | |
| 262 | void telnet_list_channels(struct telnet_connection *connection) { |
| 263 | int bts_no, trx, lchan_no, ts_no; |
| 264 | struct gsm_network *network = connection->network; |
| 265 | |
| 266 | for (bts_no = 0; bts_no < network->num_bts; ++bts_no) { |
| 267 | struct gsm_bts *bts = &network->bts[bts_no]; |
| 268 | show_bts(connection->fd.fd, bts); |
| 269 | |
| 270 | for (trx = 0; trx < bts->num_trx; ++trx) { |
| 271 | show_trx(connection->fd.fd, &bts->trx[trx]); |
| 272 | for (ts_no = 0; ts_no < 8; ++ts_no) { |
| 273 | show_ts(connection->fd.fd, &bts->trx[trx].ts[ts_no]); |
| 274 | for (lchan_no = 0; lchan_no < TS_MAX_LCHAN; ++lchan_no) { |
| 275 | struct gsm_lchan *lchan = |
| 276 | &bts->trx[trx].ts[ts_no].lchan[lchan_no]; |
| 277 | show_lchan(connection->fd.fd, lchan); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 284 | static int client_data(struct bsc_fd *fd, unsigned int what) { |
| 285 | char buf[4096]; |
| 286 | int ret; |
| 287 | |
| 288 | ret = read(fd->fd, buf, sizeof(buf)-1); |
| 289 | buf[ret] = '\0'; |
| 290 | |
| 291 | /* connection is gone */ |
| 292 | if (ret <= 0) |
| 293 | return telnet_close_client(fd); |
| 294 | |
| 295 | /* time to parse. This code assumes that the input is line based */ |
| 296 | telnet_parse((struct telnet_connection*)fd->data, buf); |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) { |
| 302 | struct telnet_connection *connection; |
| 303 | struct sockaddr_in sockaddr; |
| 304 | socklen_t len = sizeof(sockaddr); |
| 305 | int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len); |
| 306 | |
| 307 | if (new_connection < 0) { |
| 308 | perror("telnet accept failed"); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | connection = (struct telnet_connection*)malloc(sizeof(*connection)); |
| 314 | memset(connection, 0, sizeof(*connection)); |
| 315 | connection->network = (struct gsm_network*)fd->data; |
| 316 | connection->fd.data = connection; |
| 317 | connection->fd.fd = new_connection; |
| 318 | connection->fd.when = BSC_FD_READ; |
| 319 | connection->fd.cb = client_data; |
Holger Freyther | f87573d | 2009-01-04 03:49:41 +0000 | [diff] [blame] | 320 | connection->bts = 0; |
Holger Freyther | 219518d | 2009-01-02 22:04:43 +0000 | [diff] [blame] | 321 | bsc_register_fd(&connection->fd); |
| 322 | llist_add_tail(&connection->entry, &active_connections); |
| 323 | |
| 324 | print_welcome(new_connection); |
| 325 | |
| 326 | return 0; |
| 327 | } |