LAPDm: When Rx DATA from L1, L1 does not know the SAPI
[osmocom-bb.git] / src / vty / telnet_interface.c
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 <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include <osmocom/core/msgb.h>
29 #include <osmocom/core/talloc.h>
30 #include <osmocom/core/logging.h>
31
32 #include <osmocom/vty/telnet_interface.h>
33 #include <osmocom/vty/buffer.h>
34 #include <osmocom/vty/command.h>
35
36 /* per connection data */
37 LLIST_HEAD(active_connections);
38
39 static void *tall_telnet_ctx;
40
41 /* per network data */
42 static int telnet_new_connection(struct osmo_fd *fd, unsigned int what);
43
44 static struct osmo_fd server_socket = {
45         .when       = BSC_FD_READ,
46         .cb         = telnet_new_connection,
47         .priv_nr    = 0,
48 };
49
50 int telnet_init(void *tall_ctx, void *priv, int port)
51 {
52         struct sockaddr_in sock_addr;
53         int fd, rc, on = 1;
54
55         tall_telnet_ctx = talloc_named_const(tall_ctx, 1,
56                                              "telnet_connection");
57
58         fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
59
60         if (fd < 0) {
61                 LOGP(0, LOGL_ERROR, "Telnet interface socket creation failed\n");
62                 return fd;
63         }
64
65         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
66
67         memset(&sock_addr, 0, sizeof(sock_addr));
68         sock_addr.sin_family = AF_INET;
69         sock_addr.sin_port = htons(port);
70         sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
71
72         rc = bind(fd, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
73         if (rc < 0) {
74                 LOGP(0, LOGL_ERROR, "Telnet interface failed to bind\n");
75                 close(fd);
76                 return rc;
77         }
78
79         rc = listen(fd, 0);
80         if (rc < 0) {
81                 LOGP(0, LOGL_ERROR, "Telnet interface failed to listen\n");
82                 close(fd);
83                 return rc;
84         }
85
86         server_socket.data = priv;
87         server_socket.fd = fd;
88         osmo_fd_register(&server_socket);
89
90         return 0;
91 }
92
93 extern struct host host;
94
95 static void print_welcome(int fd)
96 {
97         int ret;
98         static const char *msg1 = "Welcome to the ";
99         static const char *msg2 = " control interface\r\n";
100         char *app_name = "<unnamed>";
101
102         if (host.app_info->name)
103                 app_name = host.app_info->name;
104
105         ret = write(fd, msg1, strlen(msg1));
106         ret = write(fd, app_name, strlen(app_name));
107         ret = write(fd, msg2, strlen(msg2));
108
109         if (host.app_info->copyright)
110                 ret = write(fd, host.app_info->copyright, strlen(host.app_info->copyright));
111 }
112
113 int telnet_close_client(struct osmo_fd *fd)
114 {
115         struct telnet_connection *conn = (struct telnet_connection*)fd->data;
116
117         close(fd->fd);
118         osmo_fd_unregister(fd);
119
120         if (conn->dbg) {
121                 log_del_target(conn->dbg);
122                 talloc_free(conn->dbg);
123         }
124
125         llist_del(&conn->entry);
126         talloc_free(conn);
127         return 0;
128 }
129
130 static int client_data(struct osmo_fd *fd, unsigned int what)
131 {
132         struct telnet_connection *conn = fd->data;
133         int rc = 0;
134
135         if (what & BSC_FD_READ) {
136                 conn->fd.when &= ~BSC_FD_READ;
137                 rc = vty_read(conn->vty);
138         }
139
140         /* vty might have been closed from vithin vty_read() */
141         if (!conn->vty)
142                 return rc;
143
144         if (what & BSC_FD_WRITE) {
145                 rc = buffer_flush_all(conn->vty->obuf, fd->fd);
146                 if (rc == BUFFER_EMPTY)
147                         conn->fd.when &= ~BSC_FD_WRITE;
148         }
149
150         return rc;
151 }
152
153 static int telnet_new_connection(struct osmo_fd *fd, unsigned int what)
154 {
155         struct telnet_connection *connection;
156         struct sockaddr_in sockaddr;
157         socklen_t len = sizeof(sockaddr);
158         int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
159
160         if (new_connection < 0) {
161                 LOGP(0, LOGL_ERROR, "telnet accept failed\n");
162                 return new_connection;
163         }
164
165         connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
166         connection->priv = 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;
171         osmo_fd_register(&connection->fd);
172         llist_add_tail(&connection->entry, &active_connections);
173
174         print_welcome(new_connection);
175
176         connection->vty = vty_create(new_connection, connection);
177         if (!connection->vty) {
178                 LOGP(0, LOGL_ERROR, "couldn't create VTY\n");
179                 close(new_connection);
180                 talloc_free(connection);
181                 return -1;
182         }
183
184         return 0;
185 }
186
187 /* callback from VTY code */
188 void vty_event(enum event event, int sock, struct vty *vty)
189 {
190         struct telnet_connection *connection = vty->priv;
191         struct osmo_fd *bfd = &connection->fd;
192
193         if (vty->type != VTY_TERM)
194                 return;
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         case VTY_CLOSED:
204                 /* vty layer is about to free() vty */
205                 connection->vty = NULL;
206                 telnet_close_client(bfd);
207                 break;
208         default:
209                 break;
210         }
211 }
212