| 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>
|
|---|
| 37 | #include <io/con_srv.h>
|
|---|
| 38 | #include <stdio.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 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>
|
|---|
| 46 | #include <inet/addr.h>
|
|---|
| 47 | #include <inet/endpoint.h>
|
|---|
| 48 | #include <inet/tcp.h>
|
|---|
| 49 | #include <io/console.h>
|
|---|
| 50 | #include <inttypes.h>
|
|---|
| 51 | #include "telnet.h"
|
|---|
| 52 | #include "user.h"
|
|---|
| 53 |
|
|---|
| 54 | #define APP_GETTERM "/app/getterm"
|
|---|
| 55 | #define APP_SHELL "/app/bdsh"
|
|---|
| 56 |
|
|---|
| 57 | /** Telnet commands to force character mode
|
|---|
| 58 | * (redundant to be on the safe side).
|
|---|
| 59 | * See
|
|---|
| 60 | * http://stackoverflow.com/questions/273261/force-telnet-user-into-character-mode
|
|---|
| 61 | * for discussion.
|
|---|
| 62 | */
|
|---|
| 63 | static const telnet_cmd_t telnet_force_character_mode_command[] = {
|
|---|
| 64 | TELNET_IAC, TELNET_WILL, TELNET_ECHO,
|
|---|
| 65 | TELNET_IAC, TELNET_WILL, TELNET_SUPPRESS_GO_AHEAD,
|
|---|
| 66 | TELNET_IAC, TELNET_WONT, TELNET_LINEMODE
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 | static const size_t telnet_force_character_mode_command_count =
|
|---|
| 70 | sizeof(telnet_force_character_mode_command) / sizeof(telnet_cmd_t);
|
|---|
| 71 |
|
|---|
| 72 | static int remcons_open(con_srvs_t *, con_srv_t *);
|
|---|
| 73 | static int remcons_close(con_srv_t *);
|
|---|
| 74 | static int remcons_write(con_srv_t *, void *, size_t, size_t *);
|
|---|
| 75 | static void remcons_sync(con_srv_t *);
|
|---|
| 76 | static void remcons_clear(con_srv_t *);
|
|---|
| 77 | static void remcons_set_pos(con_srv_t *, sysarg_t col, sysarg_t row);
|
|---|
| 78 | static int remcons_get_pos(con_srv_t *, sysarg_t *, sysarg_t *);
|
|---|
| 79 | static int remcons_get_size(con_srv_t *, sysarg_t *, sysarg_t *);
|
|---|
| 80 | static int remcons_get_color_cap(con_srv_t *, console_caps_t *);
|
|---|
| 81 | static int remcons_get_event(con_srv_t *, cons_event_t *);
|
|---|
| 82 |
|
|---|
| 83 | static con_ops_t con_ops = {
|
|---|
| 84 | .open = remcons_open,
|
|---|
| 85 | .close = remcons_close,
|
|---|
| 86 | .read = NULL,
|
|---|
| 87 | .write = remcons_write,
|
|---|
| 88 | .sync = remcons_sync,
|
|---|
| 89 | .clear = remcons_clear,
|
|---|
| 90 | .set_pos = remcons_set_pos,
|
|---|
| 91 | .get_pos = remcons_get_pos,
|
|---|
| 92 | .get_size = remcons_get_size,
|
|---|
| 93 | .get_color_cap = remcons_get_color_cap,
|
|---|
| 94 | .set_style = NULL,
|
|---|
| 95 | .set_color = NULL,
|
|---|
| 96 | .set_rgb_color = NULL,
|
|---|
| 97 | .set_cursor_visibility = NULL,
|
|---|
| 98 | .get_event = remcons_get_event
|
|---|
| 99 | };
|
|---|
| 100 |
|
|---|
| 101 | static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn);
|
|---|
| 102 |
|
|---|
| 103 | static tcp_listen_cb_t listen_cb = {
|
|---|
| 104 | .new_conn = remcons_new_conn
|
|---|
| 105 | };
|
|---|
| 106 |
|
|---|
| 107 | static tcp_cb_t conn_cb = {
|
|---|
| 108 | .connected = NULL
|
|---|
| 109 | };
|
|---|
| 110 |
|
|---|
| 111 | static telnet_user_t *srv_to_user(con_srv_t *srv)
|
|---|
| 112 | {
|
|---|
| 113 | return srv->srvs->sarg;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | static int remcons_open(con_srvs_t *srvs, con_srv_t *srv)
|
|---|
| 117 | {
|
|---|
| 118 | telnet_user_t *user = srv_to_user(srv);
|
|---|
| 119 |
|
|---|
| 120 | telnet_user_log(user, "New client connected (%p).", srv);
|
|---|
| 121 |
|
|---|
| 122 | /* Force character mode. */
|
|---|
| 123 | (void) tcp_conn_send(user->conn, (void *)telnet_force_character_mode_command,
|
|---|
| 124 | telnet_force_character_mode_command_count);
|
|---|
| 125 |
|
|---|
| 126 | return EOK;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | static int remcons_close(con_srv_t *srv)
|
|---|
| 130 | {
|
|---|
| 131 | telnet_user_t *user = srv_to_user(srv);
|
|---|
| 132 |
|
|---|
| 133 | telnet_user_notify_client_disconnected(user);
|
|---|
| 134 | telnet_user_log(user, "Client disconnected (%p).", srv);
|
|---|
| 135 |
|
|---|
| 136 | return EOK;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | static int remcons_write(con_srv_t *srv, void *data, size_t size, size_t *nwritten)
|
|---|
| 140 | {
|
|---|
| 141 | telnet_user_t *user = srv_to_user(srv);
|
|---|
| 142 | int rc;
|
|---|
| 143 |
|
|---|
| 144 | rc = telnet_user_send_data(user, data, size);
|
|---|
| 145 | if (rc != EOK)
|
|---|
| 146 | return rc;
|
|---|
| 147 |
|
|---|
| 148 | *nwritten = size;
|
|---|
| 149 | return EOK;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | static void remcons_sync(con_srv_t *srv)
|
|---|
| 153 | {
|
|---|
| 154 | (void) srv;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | static void remcons_clear(con_srv_t *srv)
|
|---|
| 158 | {
|
|---|
| 159 | (void) srv;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | static void remcons_set_pos(con_srv_t *srv, sysarg_t col, sysarg_t row)
|
|---|
| 163 | {
|
|---|
| 164 | telnet_user_t *user = srv_to_user(srv);
|
|---|
| 165 |
|
|---|
| 166 | telnet_user_update_cursor_x(user, col);
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | static int remcons_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
|
|---|
| 170 | {
|
|---|
| 171 | telnet_user_t *user = srv_to_user(srv);
|
|---|
| 172 |
|
|---|
| 173 | *col = user->cursor_x;
|
|---|
| 174 | *row = 0;
|
|---|
| 175 |
|
|---|
| 176 | return EOK;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | static int remcons_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
|
|---|
| 180 | {
|
|---|
| 181 | (void) srv;
|
|---|
| 182 |
|
|---|
| 183 | *cols = 100;
|
|---|
| 184 | *rows = 1;
|
|---|
| 185 |
|
|---|
| 186 | return EOK;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | static int remcons_get_color_cap(con_srv_t *srv, console_caps_t *ccaps)
|
|---|
| 190 | {
|
|---|
| 191 | (void) srv;
|
|---|
| 192 | *ccaps = CONSOLE_CAP_NONE;
|
|---|
| 193 |
|
|---|
| 194 | return EOK;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | static int remcons_get_event(con_srv_t *srv, cons_event_t *event)
|
|---|
| 198 | {
|
|---|
| 199 | telnet_user_t *user = srv_to_user(srv);
|
|---|
| 200 | kbd_event_t kevent;
|
|---|
| 201 | int rc;
|
|---|
| 202 |
|
|---|
| 203 | rc = telnet_user_get_next_keyboard_event(user, &kevent);
|
|---|
| 204 | if (rc != EOK) {
|
|---|
| 205 | /* XXX What? */
|
|---|
| 206 | memset(event, 0, sizeof(*event));
|
|---|
| 207 | return EOK;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | event->type = CEV_KEY;
|
|---|
| 211 | event->ev.key = kevent;
|
|---|
| 212 |
|
|---|
| 213 | return EOK;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /** Callback when client connects to a telnet terminal. */
|
|---|
| 217 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 218 | {
|
|---|
| 219 | /* Find the user. */
|
|---|
| 220 | telnet_user_t *user = telnet_user_get_for_client_connection(IPC_GET_ARG2(*icall));
|
|---|
| 221 | if (user == NULL) {
|
|---|
| 222 | async_answer_0(iid, ENOENT);
|
|---|
| 223 | return;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /* Handle messages. */
|
|---|
| 227 | con_conn(iid, icall, &user->srvs);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | /** Fibril for spawning the task running after user connects.
|
|---|
| 231 | *
|
|---|
| 232 | * @param arg Corresponding @c telnet_user_t structure.
|
|---|
| 233 | */
|
|---|
| 234 | static int spawn_task_fibril(void *arg)
|
|---|
| 235 | {
|
|---|
| 236 | telnet_user_t *user = arg;
|
|---|
| 237 |
|
|---|
| 238 | task_id_t task;
|
|---|
| 239 | task_wait_t wait;
|
|---|
| 240 | int rc = task_spawnl(&task, &wait, APP_GETTERM, APP_GETTERM, user->service_name,
|
|---|
| 241 | "/loc", "--msg", "--", APP_SHELL, NULL);
|
|---|
| 242 | if (rc != EOK) {
|
|---|
| 243 | telnet_user_error(user, "Spawning `%s %s /loc --msg -- %s' "
|
|---|
| 244 | "failed: %s.", APP_GETTERM, user->service_name, APP_SHELL,
|
|---|
| 245 | str_error(rc));
|
|---|
| 246 | fibril_mutex_lock(&user->guard);
|
|---|
| 247 | user->task_finished = true;
|
|---|
| 248 | user->srvs.aborted = true;
|
|---|
| 249 | fibril_condvar_signal(&user->refcount_cv);
|
|---|
| 250 | fibril_mutex_unlock(&user->guard);
|
|---|
| 251 | return EOK;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | fibril_mutex_lock(&user->guard);
|
|---|
| 255 | user->task_id = task;
|
|---|
| 256 | fibril_mutex_unlock(&user->guard);
|
|---|
| 257 |
|
|---|
| 258 | task_exit_t task_exit;
|
|---|
| 259 | int task_retval;
|
|---|
| 260 | task_wait(&wait, &task_exit, &task_retval);
|
|---|
| 261 | telnet_user_log(user, "%s terminated %s, exit code %d.", APP_GETTERM,
|
|---|
| 262 | task_exit == TASK_EXIT_NORMAL ? "normally" : "unexpectedly",
|
|---|
| 263 | task_retval);
|
|---|
| 264 |
|
|---|
| 265 | /* Announce destruction. */
|
|---|
| 266 | fibril_mutex_lock(&user->guard);
|
|---|
| 267 | user->task_finished = true;
|
|---|
| 268 | user->srvs.aborted = true;
|
|---|
| 269 | fibril_condvar_signal(&user->refcount_cv);
|
|---|
| 270 | fibril_mutex_unlock(&user->guard);
|
|---|
| 271 |
|
|---|
| 272 | return EOK;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /** Tell whether given user can be destroyed (has no active clients).
|
|---|
| 276 | *
|
|---|
| 277 | * @param user The telnet user in question.
|
|---|
| 278 | */
|
|---|
| 279 | static bool user_can_be_destroyed_no_lock(telnet_user_t *user)
|
|---|
| 280 | {
|
|---|
| 281 | return user->task_finished && user->socket_closed &&
|
|---|
| 282 | (user->locsrv_connection_count == 0);
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /** Handle network connection.
|
|---|
| 286 | *
|
|---|
| 287 | * @param lst Listener
|
|---|
| 288 | * @param conn Connection
|
|---|
| 289 | */
|
|---|
| 290 | static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)
|
|---|
| 291 | {
|
|---|
| 292 | telnet_user_t *user = telnet_user_create(conn);
|
|---|
| 293 | assert(user);
|
|---|
| 294 |
|
|---|
| 295 | con_srvs_init(&user->srvs);
|
|---|
| 296 | user->srvs.ops = &con_ops;
|
|---|
| 297 | user->srvs.sarg = user;
|
|---|
| 298 | user->srvs.abort_timeout = 1000;
|
|---|
| 299 |
|
|---|
| 300 | telnet_user_add(user);
|
|---|
| 301 |
|
|---|
| 302 | int rc = loc_service_register(user->service_name, &user->service_id);
|
|---|
| 303 | if (rc != EOK) {
|
|---|
| 304 | telnet_user_error(user, "Unable to register %s with loc: %s.",
|
|---|
| 305 | user->service_name, str_error(rc));
|
|---|
| 306 | return;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | telnet_user_log(user, "Service %s registerd with id %" PRIun ".",
|
|---|
| 310 | user->service_name, user->service_id);
|
|---|
| 311 |
|
|---|
| 312 | fid_t spawn_fibril = fibril_create(spawn_task_fibril, user);
|
|---|
| 313 | assert(spawn_fibril);
|
|---|
| 314 | fibril_add_ready(spawn_fibril);
|
|---|
| 315 |
|
|---|
| 316 | /* Wait for all clients to exit. */
|
|---|
| 317 | fibril_mutex_lock(&user->guard);
|
|---|
| 318 | while (!user_can_be_destroyed_no_lock(user)) {
|
|---|
| 319 | if (user->task_finished) {
|
|---|
| 320 | user->conn = NULL;
|
|---|
| 321 | user->socket_closed = true;
|
|---|
| 322 | user->srvs.aborted = true;
|
|---|
| 323 | continue;
|
|---|
| 324 | } else if (user->socket_closed) {
|
|---|
| 325 | if (user->task_id != 0) {
|
|---|
| 326 | task_kill(user->task_id);
|
|---|
| 327 | }
|
|---|
| 328 | }
|
|---|
| 329 | fibril_condvar_wait_timeout(&user->refcount_cv, &user->guard, 1000);
|
|---|
| 330 | }
|
|---|
| 331 | fibril_mutex_unlock(&user->guard);
|
|---|
| 332 |
|
|---|
| 333 | rc = loc_service_unregister(user->service_id);
|
|---|
| 334 | if (rc != EOK) {
|
|---|
| 335 | telnet_user_error(user,
|
|---|
| 336 | "Unable to unregister %s from loc: %s (ignored).",
|
|---|
| 337 | user->service_name, str_error(rc));
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | telnet_user_log(user, "Destroying...");
|
|---|
| 341 | telnet_user_destroy(user);
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | int main(int argc, char *argv[])
|
|---|
| 345 | {
|
|---|
| 346 | int rc;
|
|---|
| 347 | tcp_listener_t *lst;
|
|---|
| 348 | tcp_t *tcp;
|
|---|
| 349 | inet_ep_t ep;
|
|---|
| 350 |
|
|---|
| 351 | async_set_fallback_port_handler(client_connection, NULL);
|
|---|
| 352 | rc = loc_server_register(NAME);
|
|---|
| 353 | if (rc != EOK) {
|
|---|
| 354 | fprintf(stderr, "%s: Unable to register server\n", NAME);
|
|---|
| 355 | return rc;
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | rc = tcp_create(&tcp);
|
|---|
| 359 | if (rc != EOK) {
|
|---|
| 360 | fprintf(stderr, "%s: Error initializing TCP.\n", NAME);
|
|---|
| 361 | return rc;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | inet_ep_init(&ep);
|
|---|
| 365 | ep.port = 2223;
|
|---|
| 366 |
|
|---|
| 367 | rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL,
|
|---|
| 368 | &lst);
|
|---|
| 369 | if (rc != EOK) {
|
|---|
| 370 | fprintf(stderr, "%s: Error creating listener.\n", NAME);
|
|---|
| 371 | return rc;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | printf("%s: HelenOS Remote console service\n", NAME);
|
|---|
| 375 | task_retval(0);
|
|---|
| 376 | async_manager();
|
|---|
| 377 |
|
|---|
| 378 | /* Not reached */
|
|---|
| 379 | return 0;
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | /** @}
|
|---|
| 383 | */
|
|---|