[21a9869] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup remcons
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <async.h>
|
---|
| 36 | #include <errno.h>
|
---|
[5d94b16c] | 37 | #include <io/con_srv.h>
|
---|
| 38 | #include <stdio.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
[21a9869] | 40 | #include <str_error.h>
|
---|
| 41 | #include <loc.h>
|
---|
| 42 | #include <io/keycode.h>
|
---|
| 43 | #include <align.h>
|
---|
| 44 | #include <fibril_synch.h>
|
---|
| 45 | #include <task.h>
|
---|
[fab2746] | 46 | #include <inet/addr.h>
|
---|
| 47 | #include <inet/endpoint.h>
|
---|
| 48 | #include <inet/tcp.h>
|
---|
[21a9869] | 49 | #include <io/console.h>
|
---|
| 50 | #include <inttypes.h>
|
---|
[1d6dd2a] | 51 | #include <str.h>
|
---|
[3806317] | 52 | #include "telnet.h"
|
---|
[30d4706] | 53 | #include "user.h"
|
---|
[21a9869] | 54 |
|
---|
| 55 | #define APP_GETTERM "/app/getterm"
|
---|
[03e0a244] | 56 | #define APP_SHELL "/app/bdsh"
|
---|
[5576358] | 57 |
|
---|
[3806317] | 58 | /** Telnet commands to force character mode
|
---|
| 59 | * (redundant to be on the safe side).
|
---|
| 60 | * See
|
---|
[787b65b] | 61 | * http://stackoverflow.com/questions/273261/force-telnet-user-into-character-mode
|
---|
[3806317] | 62 | * for discussion.
|
---|
| 63 | */
|
---|
| 64 | static const telnet_cmd_t telnet_force_character_mode_command[] = {
|
---|
| 65 | TELNET_IAC, TELNET_WILL, TELNET_ECHO,
|
---|
| 66 | TELNET_IAC, TELNET_WILL, TELNET_SUPPRESS_GO_AHEAD,
|
---|
| 67 | TELNET_IAC, TELNET_WONT, TELNET_LINEMODE
|
---|
| 68 | };
|
---|
[3123d2a] | 69 |
|
---|
[3806317] | 70 | static const size_t telnet_force_character_mode_command_count =
|
---|
| 71 | sizeof(telnet_force_character_mode_command) / sizeof(telnet_cmd_t);
|
---|
| 72 |
|
---|
[b7fd2a0] | 73 | static errno_t remcons_open(con_srvs_t *, con_srv_t *);
|
---|
| 74 | static errno_t remcons_close(con_srv_t *);
|
---|
| 75 | static errno_t remcons_write(con_srv_t *, void *, size_t, size_t *);
|
---|
[5d94b16c] | 76 | static void remcons_sync(con_srv_t *);
|
---|
| 77 | static void remcons_clear(con_srv_t *);
|
---|
| 78 | static void remcons_set_pos(con_srv_t *, sysarg_t col, sysarg_t row);
|
---|
[b7fd2a0] | 79 | static errno_t remcons_get_pos(con_srv_t *, sysarg_t *, sysarg_t *);
|
---|
| 80 | static errno_t remcons_get_size(con_srv_t *, sysarg_t *, sysarg_t *);
|
---|
| 81 | static errno_t remcons_get_color_cap(con_srv_t *, console_caps_t *);
|
---|
| 82 | static errno_t remcons_get_event(con_srv_t *, cons_event_t *);
|
---|
[5d94b16c] | 83 |
|
---|
| 84 | static con_ops_t con_ops = {
|
---|
| 85 | .open = remcons_open,
|
---|
| 86 | .close = remcons_close,
|
---|
| 87 | .read = NULL,
|
---|
| 88 | .write = remcons_write,
|
---|
| 89 | .sync = remcons_sync,
|
---|
| 90 | .clear = remcons_clear,
|
---|
| 91 | .set_pos = remcons_set_pos,
|
---|
| 92 | .get_pos = remcons_get_pos,
|
---|
| 93 | .get_size = remcons_get_size,
|
---|
| 94 | .get_color_cap = remcons_get_color_cap,
|
---|
| 95 | .set_style = NULL,
|
---|
| 96 | .set_color = NULL,
|
---|
| 97 | .set_rgb_color = NULL,
|
---|
| 98 | .set_cursor_visibility = NULL,
|
---|
| 99 | .get_event = remcons_get_event
|
---|
| 100 | };
|
---|
[21a9869] | 101 |
|
---|
[fab2746] | 102 | static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn);
|
---|
| 103 |
|
---|
| 104 | static tcp_listen_cb_t listen_cb = {
|
---|
| 105 | .new_conn = remcons_new_conn
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 | static tcp_cb_t conn_cb = {
|
---|
| 109 | .connected = NULL
|
---|
| 110 | };
|
---|
| 111 |
|
---|
[5d94b16c] | 112 | static telnet_user_t *srv_to_user(con_srv_t *srv)
|
---|
[21a9869] | 113 | {
|
---|
[5d94b16c] | 114 | return srv->srvs->sarg;
|
---|
| 115 | }
|
---|
[21a9869] | 116 |
|
---|
[b7fd2a0] | 117 | static errno_t remcons_open(con_srvs_t *srvs, con_srv_t *srv)
|
---|
[5d94b16c] | 118 | {
|
---|
| 119 | telnet_user_t *user = srv_to_user(srv);
|
---|
[99c2e9f3] | 120 |
|
---|
[5d94b16c] | 121 | telnet_user_log(user, "New client connected (%p).", srv);
|
---|
[21a9869] | 122 |
|
---|
[5d94b16c] | 123 | /* Force character mode. */
|
---|
[fab2746] | 124 | (void) tcp_conn_send(user->conn, (void *)telnet_force_character_mode_command,
|
---|
| 125 | telnet_force_character_mode_command_count);
|
---|
[21a9869] | 126 |
|
---|
[5d94b16c] | 127 | return EOK;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[b7fd2a0] | 130 | static errno_t remcons_close(con_srv_t *srv)
|
---|
[5d94b16c] | 131 | {
|
---|
| 132 | telnet_user_t *user = srv_to_user(srv);
|
---|
| 133 |
|
---|
| 134 | telnet_user_notify_client_disconnected(user);
|
---|
| 135 | telnet_user_log(user, "Client disconnected (%p).", srv);
|
---|
| 136 |
|
---|
| 137 | return EOK;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[b7fd2a0] | 140 | static errno_t remcons_write(con_srv_t *srv, void *data, size_t size, size_t *nwritten)
|
---|
[5d94b16c] | 141 | {
|
---|
| 142 | telnet_user_t *user = srv_to_user(srv);
|
---|
[b7fd2a0] | 143 | errno_t rc;
|
---|
[5d94b16c] | 144 |
|
---|
| 145 | rc = telnet_user_send_data(user, data, size);
|
---|
| 146 | if (rc != EOK)
|
---|
| 147 | return rc;
|
---|
| 148 |
|
---|
[c8211849] | 149 | *nwritten = size;
|
---|
| 150 | return EOK;
|
---|
[5d94b16c] | 151 | }
|
---|
| 152 |
|
---|
| 153 | static void remcons_sync(con_srv_t *srv)
|
---|
| 154 | {
|
---|
| 155 | (void) srv;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | static void remcons_clear(con_srv_t *srv)
|
---|
| 159 | {
|
---|
| 160 | (void) srv;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | static void remcons_set_pos(con_srv_t *srv, sysarg_t col, sysarg_t row)
|
---|
| 164 | {
|
---|
| 165 | telnet_user_t *user = srv_to_user(srv);
|
---|
| 166 |
|
---|
| 167 | telnet_user_update_cursor_x(user, col);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[b7fd2a0] | 170 | static errno_t remcons_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
|
---|
[5d94b16c] | 171 | {
|
---|
| 172 | telnet_user_t *user = srv_to_user(srv);
|
---|
| 173 |
|
---|
| 174 | *col = user->cursor_x;
|
---|
| 175 | *row = 0;
|
---|
| 176 |
|
---|
| 177 | return EOK;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[b7fd2a0] | 180 | static errno_t remcons_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
|
---|
[5d94b16c] | 181 | {
|
---|
| 182 | (void) srv;
|
---|
| 183 |
|
---|
| 184 | *cols = 100;
|
---|
| 185 | *rows = 1;
|
---|
| 186 |
|
---|
| 187 | return EOK;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[b7fd2a0] | 190 | static errno_t remcons_get_color_cap(con_srv_t *srv, console_caps_t *ccaps)
|
---|
[5d94b16c] | 191 | {
|
---|
| 192 | (void) srv;
|
---|
| 193 | *ccaps = CONSOLE_CAP_NONE;
|
---|
| 194 |
|
---|
| 195 | return EOK;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[b7fd2a0] | 198 | static errno_t remcons_get_event(con_srv_t *srv, cons_event_t *event)
|
---|
[5d94b16c] | 199 | {
|
---|
| 200 | telnet_user_t *user = srv_to_user(srv);
|
---|
[902f0906] | 201 | kbd_event_t kevent;
|
---|
[b7fd2a0] | 202 | errno_t rc;
|
---|
[5d94b16c] | 203 |
|
---|
[902f0906] | 204 | rc = telnet_user_get_next_keyboard_event(user, &kevent);
|
---|
[5d94b16c] | 205 | if (rc != EOK) {
|
---|
| 206 | /* XXX What? */
|
---|
| 207 | memset(event, 0, sizeof(*event));
|
---|
| 208 | return EOK;
|
---|
[21a9869] | 209 | }
|
---|
[5d94b16c] | 210 |
|
---|
[902f0906] | 211 | event->type = CEV_KEY;
|
---|
| 212 | event->ev.key = kevent;
|
---|
| 213 |
|
---|
[5d94b16c] | 214 | return EOK;
|
---|
[21a9869] | 215 | }
|
---|
| 216 |
|
---|
[5923cf82] | 217 | /** Callback when client connects to a telnet terminal. */
|
---|
[984a9ba] | 218 | static void client_connection(ipc_call_t *icall, void *arg)
|
---|
[6f7cd5d] | 219 | {
|
---|
[787b65b] | 220 | /* Find the user. */
|
---|
[fafb8e5] | 221 | telnet_user_t *user = telnet_user_get_for_client_connection(ipc_get_arg2(icall));
|
---|
[787b65b] | 222 | if (user == NULL) {
|
---|
[984a9ba] | 223 | async_answer_0(icall, ENOENT);
|
---|
[6f7cd5d] | 224 | return;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[99c2e9f3] | 227 | /* Handle messages. */
|
---|
[984a9ba] | 228 | con_conn(icall, &user->srvs);
|
---|
[6f7cd5d] | 229 | }
|
---|
| 230 |
|
---|
[5923cf82] | 231 | /** Fibril for spawning the task running after user connects.
|
---|
| 232 | *
|
---|
| 233 | * @param arg Corresponding @c telnet_user_t structure.
|
---|
| 234 | */
|
---|
[b7fd2a0] | 235 | static errno_t spawn_task_fibril(void *arg)
|
---|
[21a9869] | 236 | {
|
---|
[787b65b] | 237 | telnet_user_t *user = arg;
|
---|
[a35b458] | 238 |
|
---|
[21a9869] | 239 | task_id_t task;
|
---|
[1c635d6] | 240 | task_wait_t wait;
|
---|
[55fe220] | 241 | task_wait_set(&wait, TASK_WAIT_EXIT | TASK_WAIT_RETVAL);
|
---|
[b7fd2a0] | 242 | errno_t rc = task_spawnl(&task, &wait, APP_GETTERM, APP_GETTERM, user->service_name,
|
---|
[593e023] | 243 | "/loc", "--msg", "--", APP_SHELL, NULL);
|
---|
[21a9869] | 244 | if (rc != EOK) {
|
---|
[593e023] | 245 | telnet_user_error(user, "Spawning `%s %s /loc --msg -- %s' "
|
---|
| 246 | "failed: %s.", APP_GETTERM, user->service_name, APP_SHELL,
|
---|
| 247 | str_error(rc));
|
---|
[261bbdc] | 248 | fibril_mutex_lock(&user->guard);
|
---|
[787b65b] | 249 | user->task_finished = true;
|
---|
[5d94b16c] | 250 | user->srvs.aborted = true;
|
---|
[787b65b] | 251 | fibril_condvar_signal(&user->refcount_cv);
|
---|
[261bbdc] | 252 | fibril_mutex_unlock(&user->guard);
|
---|
[21a9869] | 253 | return EOK;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[261bbdc] | 256 | fibril_mutex_lock(&user->guard);
|
---|
[787b65b] | 257 | user->task_id = task;
|
---|
[261bbdc] | 258 | fibril_mutex_unlock(&user->guard);
|
---|
[1870d81] | 259 |
|
---|
[21a9869] | 260 | task_exit_t task_exit;
|
---|
| 261 | int task_retval;
|
---|
[1c635d6] | 262 | task_wait(&wait, &task_exit, &task_retval);
|
---|
[d545d03] | 263 | telnet_user_log(user, "%s terminated %s, exit code %d.", APP_GETTERM,
|
---|
| 264 | task_exit == TASK_EXIT_NORMAL ? "normally" : "unexpectedly",
|
---|
| 265 | task_retval);
|
---|
[21a9869] | 266 |
|
---|
[7c2bb2c] | 267 | /* Announce destruction. */
|
---|
[261bbdc] | 268 | fibril_mutex_lock(&user->guard);
|
---|
[787b65b] | 269 | user->task_finished = true;
|
---|
[5d94b16c] | 270 | user->srvs.aborted = true;
|
---|
[787b65b] | 271 | fibril_condvar_signal(&user->refcount_cv);
|
---|
[261bbdc] | 272 | fibril_mutex_unlock(&user->guard);
|
---|
[7c2bb2c] | 273 |
|
---|
| 274 | return EOK;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[5923cf82] | 277 | /** Tell whether given user can be destroyed (has no active clients).
|
---|
| 278 | *
|
---|
| 279 | * @param user The telnet user in question.
|
---|
| 280 | */
|
---|
[787b65b] | 281 | static bool user_can_be_destroyed_no_lock(telnet_user_t *user)
|
---|
[1870d81] | 282 | {
|
---|
[787b65b] | 283 | return user->task_finished && user->socket_closed &&
|
---|
| 284 | (user->locsrv_connection_count == 0);
|
---|
[1870d81] | 285 | }
|
---|
[7c2bb2c] | 286 |
|
---|
[d6ff08a0] | 287 | /** Handle network connection.
|
---|
[5923cf82] | 288 | *
|
---|
[d6ff08a0] | 289 | * @param lst Listener
|
---|
| 290 | * @param conn Connection
|
---|
[5923cf82] | 291 | */
|
---|
[d6ff08a0] | 292 | static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)
|
---|
[7c2bb2c] | 293 | {
|
---|
[d6ff08a0] | 294 | telnet_user_t *user = telnet_user_create(conn);
|
---|
| 295 | assert(user);
|
---|
| 296 |
|
---|
| 297 | con_srvs_init(&user->srvs);
|
---|
| 298 | user->srvs.ops = &con_ops;
|
---|
| 299 | user->srvs.sarg = user;
|
---|
| 300 | user->srvs.abort_timeout = 1000;
|
---|
| 301 |
|
---|
| 302 | telnet_user_add(user);
|
---|
[7c2bb2c] | 303 |
|
---|
[b7fd2a0] | 304 | errno_t rc = loc_service_register(user->service_name, &user->service_id);
|
---|
[6f7cd5d] | 305 | if (rc != EOK) {
|
---|
[d545d03] | 306 | telnet_user_error(user, "Unable to register %s with loc: %s.",
|
---|
| 307 | user->service_name, str_error(rc));
|
---|
[d6ff08a0] | 308 | return;
|
---|
[6f7cd5d] | 309 | }
|
---|
[d545d03] | 310 |
|
---|
| 311 | telnet_user_log(user, "Service %s registerd with id %" PRIun ".",
|
---|
| 312 | user->service_name, user->service_id);
|
---|
[d6ff08a0] | 313 |
|
---|
[787b65b] | 314 | fid_t spawn_fibril = fibril_create(spawn_task_fibril, user);
|
---|
[7c2bb2c] | 315 | assert(spawn_fibril);
|
---|
| 316 | fibril_add_ready(spawn_fibril);
|
---|
[d6ff08a0] | 317 |
|
---|
[6f7cd5d] | 318 | /* Wait for all clients to exit. */
|
---|
[261bbdc] | 319 | fibril_mutex_lock(&user->guard);
|
---|
[787b65b] | 320 | while (!user_can_be_destroyed_no_lock(user)) {
|
---|
| 321 | if (user->task_finished) {
|
---|
[fab2746] | 322 | user->conn = NULL;
|
---|
[787b65b] | 323 | user->socket_closed = true;
|
---|
[5d94b16c] | 324 | user->srvs.aborted = true;
|
---|
[1870d81] | 325 | continue;
|
---|
[787b65b] | 326 | } else if (user->socket_closed) {
|
---|
| 327 | if (user->task_id != 0) {
|
---|
| 328 | task_kill(user->task_id);
|
---|
[1870d81] | 329 | }
|
---|
| 330 | }
|
---|
[261bbdc] | 331 | fibril_condvar_wait_timeout(&user->refcount_cv, &user->guard, 1000);
|
---|
[6f7cd5d] | 332 | }
|
---|
[261bbdc] | 333 | fibril_mutex_unlock(&user->guard);
|
---|
[d6ff08a0] | 334 |
|
---|
[787b65b] | 335 | rc = loc_service_unregister(user->service_id);
|
---|
[7c2bb2c] | 336 | if (rc != EOK) {
|
---|
[d545d03] | 337 | telnet_user_error(user,
|
---|
| 338 | "Unable to unregister %s from loc: %s (ignored).",
|
---|
| 339 | user->service_name, str_error(rc));
|
---|
[7c2bb2c] | 340 | }
|
---|
| 341 |
|
---|
[d545d03] | 342 | telnet_user_log(user, "Destroying...");
|
---|
[787b65b] | 343 | telnet_user_destroy(user);
|
---|
[fab2746] | 344 | }
|
---|
| 345 |
|
---|
[21a9869] | 346 | int main(int argc, char *argv[])
|
---|
| 347 | {
|
---|
[b7fd2a0] | 348 | errno_t rc;
|
---|
[fab2746] | 349 | tcp_listener_t *lst;
|
---|
| 350 | tcp_t *tcp;
|
---|
| 351 | inet_ep_t ep;
|
---|
| 352 |
|
---|
[b688fd8] | 353 | async_set_fallback_port_handler(client_connection, NULL);
|
---|
[fab2746] | 354 | rc = loc_server_register(NAME);
|
---|
[3123d2a] | 355 | if (rc != EOK) {
|
---|
| 356 | fprintf(stderr, "%s: Unable to register server\n", NAME);
|
---|
| 357 | return rc;
|
---|
[21a9869] | 358 | }
|
---|
| 359 |
|
---|
[fab2746] | 360 | rc = tcp_create(&tcp);
|
---|
[d6ff08a0] | 361 | if (rc != EOK) {
|
---|
| 362 | fprintf(stderr, "%s: Error initializing TCP.\n", NAME);
|
---|
[fab2746] | 363 | return rc;
|
---|
[21a9869] | 364 | }
|
---|
| 365 |
|
---|
[fab2746] | 366 | inet_ep_init(&ep);
|
---|
[bf7587b0] | 367 | ep.port = 2223;
|
---|
[21a9869] | 368 |
|
---|
[fab2746] | 369 | rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL,
|
---|
| 370 | &lst);
|
---|
[21a9869] | 371 | if (rc != EOK) {
|
---|
[fab2746] | 372 | fprintf(stderr, "%s: Error creating listener.\n", NAME);
|
---|
| 373 | return rc;
|
---|
[21a9869] | 374 | }
|
---|
| 375 |
|
---|
| 376 | printf("%s: HelenOS Remote console service\n", NAME);
|
---|
[6d5e378] | 377 | task_retval(0);
|
---|
[fab2746] | 378 | async_manager();
|
---|
[21a9869] | 379 |
|
---|
[fab2746] | 380 | /* Not reached */
|
---|
[21a9869] | 381 | return 0;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | /** @}
|
---|
| 385 | */
|
---|