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> |
| 29 | |
| 30 | extern void telnet_parse(struct telnet_connection *connection, char *line); |
| 31 | |
| 32 | /* per connection data */ |
| 33 | LLIST_HEAD(active_connections); |
| 34 | |
| 35 | /* per network data */ |
| 36 | static int telnet_new_connection(struct bsc_fd *fd, unsigned int what); |
| 37 | static struct bsc_fd server_socket = { |
| 38 | .when = BSC_FD_READ, |
| 39 | .cb = telnet_new_connection, |
| 40 | .priv_nr = 0, |
| 41 | }; |
| 42 | |
| 43 | void telnet_init(struct gsm_network *network, int port) { |
| 44 | struct sockaddr_in sock_addr; |
| 45 | int fd; |
| 46 | |
| 47 | fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 48 | |
| 49 | if (fd < 0) { |
| 50 | perror("Telnet interface socket creation failed"); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | memset(&sock_addr, 0, sizeof(sock_addr)); |
| 55 | sock_addr.sin_family = AF_INET; |
| 56 | sock_addr.sin_port = htons(port); |
| 57 | sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 58 | |
| 59 | if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) { |
| 60 | perror("Telnet interface failed to bind"); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (listen(fd, 0) < 0) { |
| 65 | perror("Telnet interface failed to listen"); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | server_socket.data = network; |
| 70 | server_socket.fd = fd; |
| 71 | bsc_register_fd(&server_socket); |
| 72 | } |
| 73 | |
| 74 | void telnet_write_help(int fd) { |
| 75 | int ret; |
| 76 | static char *msg = |
| 77 | "Help for the ad-hoc telnet command line interface\n" |
| 78 | "The generic pattern is CMD LEN DATA\\n or just CMD\n" |
| 79 | "where CMD is one of the following:\n" |
| 80 | "help\n" |
| 81 | "page IMSI (type)\n" |
| 82 | "call IMSI (number)\n" |
| 83 | "get_channel IMSI Add use count on an active channel\n" |
| 84 | "put_channel IMSI Remove use count on an active channel\n" |
| 85 | "48 IMSI 0xAB 0xEF...Send GSM 04.08\n" |
| 86 | "11 IMSI 0xAB 0xEF...Send GSM 04.11\n"; |
| 87 | |
| 88 | ret = write(fd, msg, strlen(msg)); |
| 89 | } |
| 90 | |
| 91 | static void print_welcome(int fd) { |
| 92 | int ret; |
| 93 | static char *msg = |
| 94 | "Welcome to the OpenBSC Control interface\n" |
| 95 | "Copyright (C) 2008, 2009 Harald Welte\n" |
| 96 | "Contributions by Daniel Willmann, Jan Lübbe, " |
| 97 | "Stefan Schmidt, Holger Freyther\n\n" |
| 98 | "License GPLv2+: GNU GPL version 2 or later " |
| 99 | "<http://gnu.org/licenses/gpl.html>\n" |
| 100 | "This is free software: you are free to change " |
| 101 | "and redistribute it.\n" |
| 102 | "There is NO WARRANTY, to the extent permitted " |
| 103 | "by law.\nType \"help\" to get a short introduction.\n"; |
| 104 | |
| 105 | ret = write(fd, msg, strlen(msg)); |
| 106 | } |
| 107 | |
| 108 | int telnet_close_client(struct bsc_fd *fd) { |
| 109 | struct telnet_connection *conn = (struct telnet_connection*)fd->data; |
| 110 | |
| 111 | close(fd->fd); |
| 112 | bsc_unregister_fd(fd); |
| 113 | llist_del(&conn->entry); |
| 114 | free(conn); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | void telnet_error_client(int fd) { |
| 119 | int ret; |
| 120 | static char *msg = "Something went wrong. Please try again.\n"; |
| 121 | |
| 122 | printf("Error\n"); |
| 123 | ret = write(fd, msg, strlen(msg)); |
| 124 | } |
| 125 | |
| 126 | void telnet_page(struct telnet_connection *connection, const char *imsi, int page) { |
| 127 | printf("going to page: '%s' %d\n", imsi, page); |
| 128 | } |
| 129 | |
| 130 | void telnet_put_channel(struct telnet_connection *connection, const char *imsi) { |
| 131 | printf("put_channel: '%s'\n", imsi); |
| 132 | } |
| 133 | |
| 134 | void telnet_get_channel(struct telnet_connection *connection, const char *imsi) { |
| 135 | printf("get_channel: '%s'\n", imsi); |
| 136 | } |
| 137 | |
| 138 | void telnet_call(struct telnet_connection *connection, const char* imsi, |
| 139 | const char *origin) { |
| 140 | printf("calling: '%s' from: '%s'\n", imsi, origin); |
| 141 | } |
| 142 | |
| 143 | void telnet_send_gsm_48(struct telnet_connection *connection) { |
| 144 | printf("sending gsm04.08 message\n"); |
| 145 | } |
| 146 | |
| 147 | void telnet_send_gsm_11(struct telnet_connection *connection) { |
| 148 | printf("sending gsm04.11 message\n"); |
| 149 | } |
| 150 | |
| 151 | static int client_data(struct bsc_fd *fd, unsigned int what) { |
| 152 | char buf[4096]; |
| 153 | int ret; |
| 154 | |
| 155 | ret = read(fd->fd, buf, sizeof(buf)-1); |
| 156 | buf[ret] = '\0'; |
| 157 | |
| 158 | /* connection is gone */ |
| 159 | if (ret <= 0) |
| 160 | return telnet_close_client(fd); |
| 161 | |
| 162 | /* time to parse. This code assumes that the input is line based */ |
| 163 | telnet_parse((struct telnet_connection*)fd->data, buf); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) { |
| 169 | struct telnet_connection *connection; |
| 170 | struct sockaddr_in sockaddr; |
| 171 | socklen_t len = sizeof(sockaddr); |
| 172 | int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len); |
| 173 | |
| 174 | if (new_connection < 0) { |
| 175 | perror("telnet accept failed"); |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | connection = (struct telnet_connection*)malloc(sizeof(*connection)); |
| 181 | memset(connection, 0, sizeof(*connection)); |
| 182 | connection->network = (struct gsm_network*)fd->data; |
| 183 | connection->fd.data = connection; |
| 184 | connection->fd.fd = new_connection; |
| 185 | connection->fd.when = BSC_FD_READ; |
| 186 | connection->fd.cb = client_data; |
| 187 | bsc_register_fd(&connection->fd); |
| 188 | llist_add_tail(&connection->entry, &active_connections); |
| 189 | |
| 190 | print_welcome(new_connection); |
| 191 | |
| 192 | return 0; |
| 193 | } |