blob: 5cc6ea0afa5f84d84f817e71375fbace74c19fd1 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* 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 <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
27
28#include <openbsc/telnet_interface.h>
29#include <openbsc/gsm_subscriber.h>
30#include <openbsc/chan_alloc.h>
31#include <openbsc/gsm_04_08.h>
32#include <openbsc/gsm_04_11.h>
33#include <openbsc/msgb.h>
34#include <openbsc/abis_rsl.h>
35#include <openbsc/paging.h>
36#include <openbsc/signal.h>
Harald Weltea8379772009-06-20 22:36:41 +020037#include <openbsc/talloc.h>
Harald Welte59b04682009-06-10 05:40:52 +080038
39#include <vty/buffer.h>
40
41#define WRITE_CONNECTION(fd, msg...) \
42 int ret; \
43 char buf[4096]; \
44 snprintf(buf, sizeof(buf), msg); \
45 ret = write(fd, buf, strlen(buf));
46
47
48/* per connection data */
49LLIST_HEAD(active_connections);
50
Harald Weltea8379772009-06-20 22:36:41 +020051static void *tall_telnet_ctx;
52
Harald Welte59b04682009-06-10 05:40:52 +080053/* per network data */
54static int telnet_new_connection(struct bsc_fd *fd, unsigned int what);
55#if 0
56static int telnet_paging_callback(unsigned int subsys, unsigned int signal,
57 void *handler_data, void *signal_data);
58static int telnet_sms_callback(unsigned int subsys, unsigned int signal,
59 void *handler_data, void *signal_data);
60#endif
61
62static struct bsc_fd server_socket = {
63 .when = BSC_FD_READ,
64 .cb = telnet_new_connection,
65 .priv_nr = 0,
66};
67
68void telnet_init(struct gsm_network *network, int port) {
69 struct sockaddr_in sock_addr;
70 int fd, on = 1;
71
Harald Weltea8379772009-06-20 22:36:41 +020072 tall_telnet_ctx = talloc_named_const(tall_bsc_ctx, 1,
73 "telnet_connection");
74
Harald Welte59b04682009-06-10 05:40:52 +080075 bsc_vty_init(network);
76
77 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
78
79 if (fd < 0) {
80 perror("Telnet interface socket creation failed");
81 return;
82 }
83
84 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
85
86 memset(&sock_addr, 0, sizeof(sock_addr));
87 sock_addr.sin_family = AF_INET;
88 sock_addr.sin_port = htons(port);
89 sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
90
91 if (bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) < 0) {
92 perror("Telnet interface failed to bind");
93 return;
94 }
95
96 if (listen(fd, 0) < 0) {
97 perror("Telnet interface failed to listen");
98 return;
99 }
100
101 server_socket.data = network;
102 server_socket.fd = fd;
103 bsc_register_fd(&server_socket);
104
105 /* register callbacks */
106#if 0
107 register_signal_handler(SS_PAGING, telnet_paging_callback, network);
108 register_signal_handler(SS_SMS, telnet_sms_callback, network);
109#endif
110}
111
112static void print_welcome(int fd) {
113 int ret;
114 static char *msg =
115 "Welcome to the OpenBSC Control interface\n"
116 "Copyright (C) 2008, 2009 Harald Welte\n"
117 "Contributions by Daniel Willmann, Jan Lübbe, "
118 "Stefan Schmidt, Holger Freyther\n\n"
119 "License GPLv2+: GNU GPL version 2 or later "
120 "<http://gnu.org/licenses/gpl.html>\n"
121 "This is free software: you are free to change "
122 "and redistribute it.\n"
123 "There is NO WARRANTY, to the extent permitted "
124 "by law.\nType \"help\" to get a short introduction.\n";
125
126 ret = write(fd, msg, strlen(msg));
127}
128
129int telnet_close_client(struct bsc_fd *fd) {
130 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
131
132 close(fd->fd);
133 bsc_unregister_fd(fd);
134 llist_del(&conn->entry);
Harald Welte49a84052009-06-22 01:36:25 +0200135 talloc_free(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800136 return 0;
137}
138
139static int client_data(struct bsc_fd *fd, unsigned int what)
140{
141 struct telnet_connection *conn = fd->data;
142 int rc = 0;
143
144 if (what & BSC_FD_READ) {
145 conn->fd.when &= ~BSC_FD_READ;
146 rc = vty_read(conn->vty);
147 }
148
149 if (what & BSC_FD_WRITE) {
150 rc = buffer_flush_all(conn->vty->obuf, fd->fd);
151 if (rc == BUFFER_EMPTY)
152 conn->fd.when &= ~BSC_FD_WRITE;
153 }
154
155 return rc;
156}
157
158static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
159 struct telnet_connection *connection;
160 struct sockaddr_in sockaddr;
161 socklen_t len = sizeof(sockaddr);
162 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
163
164 if (new_connection < 0) {
165 perror("telnet accept failed");
166 return -1;
167 }
168
169
Harald Weltea8379772009-06-20 22:36:41 +0200170 connection = talloc(tall_telnet_ctx, struct telnet_connection);
Harald Welte59b04682009-06-10 05:40:52 +0800171 memset(connection, 0, sizeof(*connection));
172 connection->network = (struct gsm_network*)fd->data;
173 connection->fd.data = connection;
174 connection->fd.fd = new_connection;
175 connection->fd.when = BSC_FD_READ;
176 connection->fd.cb = client_data;
177 connection->bts = 0;
178 bsc_register_fd(&connection->fd);
179 llist_add_tail(&connection->entry, &active_connections);
180
181 print_welcome(new_connection);
182
183 connection->vty = vty_create(new_connection, connection);
184 if (!connection->vty)
185 return -1;
186
187 return 0;
188}
189
190/* callback from VTY code */
191void vty_event(enum event event, int sock, struct vty *vty)
192{
193 struct telnet_connection *connection = vty->priv;
194 struct bsc_fd *bfd = &connection->fd;
195
196 switch (event) {
197 case VTY_READ:
198 bfd->when |= BSC_FD_READ;
199 break;
200 case VTY_WRITE:
201 bfd->when |= BSC_FD_WRITE;
202 break;
203 default:
204 break;
205 }
206}
207
208#if 0
209static int telnet_paging_callback(unsigned int subsys, unsigned int singal,
210 void *handler_data, void *signal_data)
211{
212 struct paging_signal_data *paging = signal_data;
213 struct telnet_connection *con;
214
215 llist_for_each_entry(con, &active_connections, entry) {
216 if (paging->lchan) {
217 WRITE_CONNECTION(con->fd.fd, "Paging succeeded\n");
218 show_lchan(con->fd.fd, paging->lchan);
219 } else {
220 WRITE_CONNECTION(con->fd.fd, "Paging failed for subscriber: %s/%s/%s\n",
221 paging->subscr->imsi,
222 paging->subscr->tmsi,
223 paging->subscr->name);
224 }
225 }
226
227 return 0;
228}
229
230static int telnet_sms_callback(unsigned int subsys, unsigned int signal,
231 void *handler_data, void *signal_data)
232{
233 struct sms_submit *sms = signal_data;
234 struct telnet_connection *con;
235
236 llist_for_each_entry(con, &active_connections, entry) {
237 WRITE_CONNECTION(con->fd.fd, "Incoming SMS: %s\n", sms->user_data);
238 }
239
240 return 0;
241}
242#endif