blob: f4ffb529efe6448b7618d6e44ce99b0e54c9d70d [file] [log] [blame]
Holger Freyther219518d2009-01-02 22:04:43 +00001/* 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 Welte12247c62009-05-21 07:23:02 +000023#include <stdlib.h>
Holger Freyther219518d2009-01-02 22:04:43 +000024#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
27
28#include <openbsc/telnet_interface.h>
Holger Freytherae61cae2009-01-04 03:46:01 +000029#include <openbsc/gsm_subscriber.h>
Holger Freytherf87573d2009-01-04 03:49:41 +000030#include <openbsc/chan_alloc.h>
Holger Freyther3ae8fd22009-01-04 03:50:40 +000031#include <openbsc/gsm_04_08.h>
Holger Freyther9b177762009-02-16 19:07:18 +000032#include <openbsc/gsm_04_11.h>
Holger Freyther868b8cd2009-01-04 03:57:27 +000033#include <openbsc/msgb.h>
Holger Freyther7448a532009-01-04 20:18:23 +000034#include <openbsc/abis_rsl.h>
Harald Welte38c2f132009-01-06 23:10:57 +000035#include <openbsc/paging.h>
Holger Freyther7aaf1122009-02-14 22:51:13 +000036#include <openbsc/signal.h>
Holger Freyther219518d2009-01-02 22:04:43 +000037
Harald Weltec63e51d2009-03-10 19:46:16 +000038#include <vty/buffer.h>
39
Holger Freytherae61cae2009-01-04 03:46:01 +000040#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 Freyther219518d2009-01-02 22:04:43 +000047/* per connection data */
48LLIST_HEAD(active_connections);
49
50/* per network data */
51static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
Harald Welte703af982009-05-23 06:14:44 +000052#if 0
Harald Welte595ad7b2009-02-16 22:05:44 +000053static int telnet_paging_callback(unsigned int subsys, unsigned int signal,
54 void *handler_data, void *signal_data);
55static int telnet_sms_callback(unsigned int subsys, unsigned int signal,
56 void *handler_data, void *signal_data);
Harald Welte703af982009-05-23 06:14:44 +000057#endif
Holger Freyther7aaf1122009-02-14 22:51:13 +000058
Holger Freyther219518d2009-01-02 22:04:43 +000059static struct bsc_fd server_socket = {
60 .when = BSC_FD_READ,
61 .cb = telnet_new_connection,
62 .priv_nr = 0,
63};
64
65void telnet_init(struct gsm_network *network, int port) {
66 struct sockaddr_in sock_addr;
Holger Freytherf0776892009-02-03 20:49:51 +000067 int fd, on = 1;
Holger Freyther219518d2009-01-02 22:04:43 +000068
Harald Welte404cdd82009-03-10 12:21:45 +000069 bsc_vty_init(network);
70
Holger Freyther219518d2009-01-02 22:04:43 +000071 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
72
73 if (fd < 0) {
74 perror("Telnet interface socket creation failed");
75 return;
76 }
77
Holger Freytherf0776892009-02-03 20:49:51 +000078 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
79
Holger Freyther219518d2009-01-02 22:04:43 +000080 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);
Holger Freyther7aaf1122009-02-14 22:51:13 +000098
Holger Freyther9b177762009-02-16 19:07:18 +000099 /* register callbacks */
Harald Welte404cdd82009-03-10 12:21:45 +0000100#if 0
Harald Welte595ad7b2009-02-16 22:05:44 +0000101 register_signal_handler(SS_PAGING, telnet_paging_callback, network);
102 register_signal_handler(SS_SMS, telnet_sms_callback, network);
Harald Welte404cdd82009-03-10 12:21:45 +0000103#endif
Holger Freyther219518d2009-01-02 22:04:43 +0000104}
105
106static void print_welcome(int fd) {
107 int ret;
108 static char *msg =
109 "Welcome to the OpenBSC Control interface\n"
110 "Copyright (C) 2008, 2009 Harald Welte\n"
111 "Contributions by Daniel Willmann, Jan Lübbe, "
112 "Stefan Schmidt, Holger Freyther\n\n"
113 "License GPLv2+: GNU GPL version 2 or later "
114 "<http://gnu.org/licenses/gpl.html>\n"
115 "This is free software: you are free to change "
116 "and redistribute it.\n"
117 "There is NO WARRANTY, to the extent permitted "
118 "by law.\nType \"help\" to get a short introduction.\n";
119
120 ret = write(fd, msg, strlen(msg));
121}
122
123int telnet_close_client(struct bsc_fd *fd) {
124 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
125
126 close(fd->fd);
127 bsc_unregister_fd(fd);
128 llist_del(&conn->entry);
129 free(conn);
130 return 0;
131}
132
Harald Welte404cdd82009-03-10 12:21:45 +0000133static int client_data(struct bsc_fd *fd, unsigned int what)
134{
135 struct telnet_connection *conn = fd->data;
Harald Welte703af982009-05-23 06:14:44 +0000136 int rc = 0;
Harald Weltec63e51d2009-03-10 19:46:16 +0000137
138 if (what & BSC_FD_READ) {
139 conn->fd.when &= ~BSC_FD_READ;
140 rc = vty_read(conn->vty);
141 }
142
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 Freyther219518d2009-01-02 22:04:43 +0000150}
151
152static 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) {
159 perror("telnet accept failed");
160 return -1;
161 }
162
163
164 connection = (struct telnet_connection*)malloc(sizeof(*connection));
165 memset(connection, 0, sizeof(*connection));
166 connection->network = (struct gsm_network*)fd->data;
167 connection->fd.data = connection;
168 connection->fd.fd = new_connection;
169 connection->fd.when = BSC_FD_READ;
170 connection->fd.cb = client_data;
Holger Freytherf87573d2009-01-04 03:49:41 +0000171 connection->bts = 0;
Holger Freyther219518d2009-01-02 22:04:43 +0000172 bsc_register_fd(&connection->fd);
173 llist_add_tail(&connection->entry, &active_connections);
174
175 print_welcome(new_connection);
176
Harald Weltec63e51d2009-03-10 19:46:16 +0000177 connection->vty = vty_create(new_connection, connection);
Harald Welte404cdd82009-03-10 12:21:45 +0000178 if (!connection->vty)
179 return -1;
180
Holger Freyther219518d2009-01-02 22:04:43 +0000181 return 0;
182}
Holger Freyther7aaf1122009-02-14 22:51:13 +0000183
Harald Weltec63e51d2009-03-10 19:46:16 +0000184/* callback from VTY code */
185void 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
190 switch (event) {
191 case VTY_READ:
192 bfd->when |= BSC_FD_READ;
193 break;
194 case VTY_WRITE:
195 bfd->when |= BSC_FD_WRITE;
196 break;
197 default:
198 break;
199 }
200}
201
Harald Welte404cdd82009-03-10 12:21:45 +0000202#if 0
Harald Welte595ad7b2009-02-16 22:05:44 +0000203static int telnet_paging_callback(unsigned int subsys, unsigned int singal,
204 void *handler_data, void *signal_data)
Holger Freyther7aaf1122009-02-14 22:51:13 +0000205{
Harald Welte595ad7b2009-02-16 22:05:44 +0000206 struct paging_signal_data *paging = signal_data;
Holger Freyther7aaf1122009-02-14 22:51:13 +0000207 struct telnet_connection *con;
208
209 llist_for_each_entry(con, &active_connections, entry) {
210 if (paging->lchan) {
211 WRITE_CONNECTION(con->fd.fd, "Paging succeeded\n");
212 show_lchan(con->fd.fd, paging->lchan);
213 } else {
214 WRITE_CONNECTION(con->fd.fd, "Paging failed for subscriber: %s/%s/%s\n",
215 paging->subscr->imsi,
216 paging->subscr->tmsi,
217 paging->subscr->name);
218 }
219 }
220
221 return 0;
222}
Holger Freyther9b177762009-02-16 19:07:18 +0000223
Harald Welte595ad7b2009-02-16 22:05:44 +0000224static int telnet_sms_callback(unsigned int subsys, unsigned int signal,
225 void *handler_data, void *signal_data)
Holger Freyther9b177762009-02-16 19:07:18 +0000226{
Harald Welte595ad7b2009-02-16 22:05:44 +0000227 struct sms_submit *sms = signal_data;
Holger Freyther9b177762009-02-16 19:07:18 +0000228 struct telnet_connection *con;
229
230 llist_for_each_entry(con, &active_connections, entry) {
Harald Welte595ad7b2009-02-16 22:05:44 +0000231 WRITE_CONNECTION(con->fd.fd, "Incoming SMS: %s\n", sms->user_data);
Holger Freyther9b177762009-02-16 19:07:18 +0000232 }
233
234 return 0;
235}
Harald Welte404cdd82009-03-10 12:21:45 +0000236#endif